xref: /openbmc/linux/drivers/net/wireless/mediatek/mt76/mt76x0/pci_mcu.c (revision 05cf4fe738242183f1237f1b3a28b4479348c0a1)
1 /*
2  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include <linux/kernel.h>
17 #include <linux/firmware.h>
18 
19 #include "mt76x0.h"
20 #include "mcu.h"
21 
22 #define MT7610E_FIRMWARE	"mediatek/mt7610e.bin"
23 #define MT7650E_FIRMWARE	"mediatek/mt7650e.bin"
24 
25 #define MT_MCU_IVB_ADDR		(MT_MCU_ILM_ADDR + 0x54000 - MT_MCU_IVB_SIZE)
26 
27 static int mt76x0e_load_firmware(struct mt76x02_dev *dev)
28 {
29 	bool is_combo_chip = mt76_chip(&dev->mt76) != 0x7610;
30 	u32 val, ilm_len, dlm_len, offset = 0;
31 	const struct mt76x02_fw_header *hdr;
32 	const struct firmware *fw;
33 	const char *firmware;
34 	const u8 *fw_payload;
35 	int len, err;
36 
37 	if (is_combo_chip)
38 		firmware = MT7650E_FIRMWARE;
39 	else
40 		firmware = MT7610E_FIRMWARE;
41 
42 	err = request_firmware(&fw, firmware, dev->mt76.dev);
43 	if (err)
44 		return err;
45 
46 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
47 		err = -EIO;
48 		goto out;
49 	}
50 
51 	hdr = (const struct mt76x02_fw_header *)fw->data;
52 
53 	len = sizeof(*hdr);
54 	len += le32_to_cpu(hdr->ilm_len);
55 	len += le32_to_cpu(hdr->dlm_len);
56 
57 	if (fw->size != len) {
58 		err = -EIO;
59 		goto out;
60 	}
61 
62 	fw_payload = fw->data + sizeof(*hdr);
63 
64 	val = le16_to_cpu(hdr->fw_ver);
65 	dev_info(dev->mt76.dev, "Firmware Version: %d.%d.%02d\n",
66 		 (val >> 12) & 0xf, (val >> 8) & 0xf, val & 0xf);
67 
68 	val = le16_to_cpu(hdr->fw_ver);
69 	dev_dbg(dev->mt76.dev,
70 		"Firmware Version: %d.%d.%02d Build: %x Build time: %.16s\n",
71 		(val >> 12) & 0xf, (val >> 8) & 0xf, val & 0xf,
72 		le16_to_cpu(hdr->build_ver), hdr->build_time);
73 
74 	if (is_combo_chip && !mt76_poll(dev, MT_MCU_SEMAPHORE_00, 1, 1, 600)) {
75 		dev_err(dev->mt76.dev,
76 			"Could not get hardware semaphore for loading fw\n");
77 		err = -ETIMEDOUT;
78 		goto out;
79 	}
80 
81 	/* upload ILM. */
82 	mt76_wr(dev, MT_MCU_PCIE_REMAP_BASE4, 0);
83 	ilm_len = le32_to_cpu(hdr->ilm_len);
84 	if (is_combo_chip) {
85 		ilm_len -= MT_MCU_IVB_SIZE;
86 		offset = MT_MCU_IVB_SIZE;
87 	}
88 	dev_dbg(dev->mt76.dev, "loading FW - ILM %u\n", ilm_len);
89 	mt76_wr_copy(dev, MT_MCU_ILM_ADDR + offset, fw_payload + offset,
90 		     ilm_len);
91 
92 	/* upload IVB. */
93 	if (is_combo_chip) {
94 		dev_dbg(dev->mt76.dev, "loading FW - IVB %u\n",
95 			MT_MCU_IVB_SIZE);
96 		mt76_wr_copy(dev, MT_MCU_IVB_ADDR, fw_payload, MT_MCU_IVB_SIZE);
97 	}
98 
99 	/* upload DLM. */
100 	mt76_wr(dev, MT_MCU_PCIE_REMAP_BASE4, MT_MCU_DLM_OFFSET);
101 	dlm_len = le32_to_cpu(hdr->dlm_len);
102 	dev_dbg(dev->mt76.dev, "loading FW - DLM %u\n", dlm_len);
103 	mt76_wr_copy(dev, MT_MCU_ILM_ADDR,
104 		     fw_payload + le32_to_cpu(hdr->ilm_len), dlm_len);
105 
106 	/* trigger firmware */
107 	mt76_wr(dev, MT_MCU_PCIE_REMAP_BASE4, 0);
108 	if (is_combo_chip)
109 		mt76_wr(dev, MT_MCU_INT_LEVEL, 0x3);
110 	else
111 		mt76_wr(dev, MT_MCU_RESET_CTL, 0x300);
112 
113 	if (!mt76_poll_msec(dev, MT_MCU_COM_REG0, 1, 1, 1000)) {
114 		dev_err(dev->mt76.dev, "Firmware failed to start\n");
115 		err = -ETIMEDOUT;
116 		goto out;
117 	}
118 
119 	mt76x02_set_ethtool_fwver(dev, hdr);
120 	dev_dbg(dev->mt76.dev, "Firmware running!\n");
121 
122 out:
123 	if (is_combo_chip)
124 		mt76_wr(dev, MT_MCU_SEMAPHORE_00, 0x1);
125 	release_firmware(fw);
126 
127 	return err;
128 }
129 
130 int mt76x0e_mcu_init(struct mt76x02_dev *dev)
131 {
132 	static const struct mt76_mcu_ops mt76x0e_mcu_ops = {
133 		.mcu_msg_alloc = mt76x02_mcu_msg_alloc,
134 		.mcu_send_msg = mt76x02_mcu_msg_send,
135 	};
136 	int err;
137 
138 	dev->mt76.mcu_ops = &mt76x0e_mcu_ops;
139 
140 	err = mt76x0e_load_firmware(dev);
141 	if (err < 0)
142 		return err;
143 
144 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mt76.state);
145 
146 	return 0;
147 }
148