1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc.
3  *
4  * Author: Felix Fietkau <nbd@nbd.name>
5  *	   Lorenzo Bianconi <lorenzo@kernel.org>
6  *	   Sean Wang <sean.wang@mediatek.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/iopoll.h>
11 #include <linux/module.h>
12 
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/sdio_ids.h>
15 #include <linux/mmc/sdio_func.h>
16 
17 #include "../sdio.h"
18 #include "mt7615.h"
19 #include "mac.h"
20 #include "mcu.h"
21 
22 static const struct sdio_device_id mt7663s_table[] = {
23 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, 0x7603) },
24 	{ }	/* Terminating entry */
25 };
26 
mt7663s_txrx_worker(struct mt76_worker * w)27 static void mt7663s_txrx_worker(struct mt76_worker *w)
28 {
29 	struct mt76_sdio *sdio = container_of(w, struct mt76_sdio,
30 					      txrx_worker);
31 	struct mt76_dev *mdev = container_of(sdio, struct mt76_dev, sdio);
32 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
33 
34 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
35 		queue_work(mdev->wq, &dev->pm.wake_work);
36 		return;
37 	}
38 	mt76s_txrx_worker(sdio);
39 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
40 }
41 
mt7663s_init_work(struct work_struct * work)42 static void mt7663s_init_work(struct work_struct *work)
43 {
44 	struct mt7615_dev *dev;
45 
46 	dev = container_of(work, struct mt7615_dev, mcu_work);
47 	if (mt7663s_mcu_init(dev))
48 		return;
49 
50 	mt7615_init_work(dev);
51 }
52 
mt7663s_parse_intr(struct mt76_dev * dev,struct mt76s_intr * intr)53 static int mt7663s_parse_intr(struct mt76_dev *dev, struct mt76s_intr *intr)
54 {
55 	struct mt76_sdio *sdio = &dev->sdio;
56 	struct mt7663s_intr *irq_data = sdio->intr_data;
57 	int i, err;
58 
59 	err = sdio_readsb(sdio->func, irq_data, MCR_WHISR, sizeof(*irq_data));
60 	if (err)
61 		return err;
62 
63 	intr->isr = irq_data->isr;
64 	intr->rec_mb = irq_data->rec_mb;
65 	intr->tx.wtqcr = irq_data->tx.wtqcr;
66 	intr->rx.num = irq_data->rx.num;
67 	for (i = 0; i < 2 ; i++)
68 		intr->rx.len[i] = irq_data->rx.len[i];
69 
70 	return 0;
71 }
72 
mt7663s_probe(struct sdio_func * func,const struct sdio_device_id * id)73 static int mt7663s_probe(struct sdio_func *func,
74 			 const struct sdio_device_id *id)
75 {
76 	static const struct mt76_driver_ops drv_ops = {
77 		.txwi_size = MT_USB_TXD_SIZE,
78 		.drv_flags = MT_DRV_RX_DMA_HDR,
79 		.tx_prepare_skb = mt7663_usb_sdio_tx_prepare_skb,
80 		.tx_complete_skb = mt7663_usb_sdio_tx_complete_skb,
81 		.tx_status_data = mt7663_usb_sdio_tx_status_data,
82 		.rx_skb = mt7615_queue_rx_skb,
83 		.sta_ps = mt7615_sta_ps,
84 		.sta_add = mt7615_mac_sta_add,
85 		.sta_remove = mt7615_mac_sta_remove,
86 		.update_survey = mt7615_update_channel,
87 	};
88 	static const struct mt76_bus_ops mt7663s_ops = {
89 		.rr = mt76s_rr,
90 		.rmw = mt76s_rmw,
91 		.wr = mt76s_wr,
92 		.write_copy = mt76s_write_copy,
93 		.read_copy = mt76s_read_copy,
94 		.wr_rp = mt76s_wr_rp,
95 		.rd_rp = mt76s_rd_rp,
96 		.type = MT76_BUS_SDIO,
97 	};
98 	struct ieee80211_ops *ops;
99 	struct mt7615_dev *dev;
100 	struct mt76_dev *mdev;
101 	int i, ret;
102 
103 	ops = devm_kmemdup(&func->dev, &mt7615_ops, sizeof(mt7615_ops),
104 			   GFP_KERNEL);
105 	if (!ops)
106 		return -ENOMEM;
107 
108 	mdev = mt76_alloc_device(&func->dev, sizeof(*dev), ops, &drv_ops);
109 	if (!mdev)
110 		return -ENOMEM;
111 
112 	dev = container_of(mdev, struct mt7615_dev, mt76);
113 
114 	INIT_WORK(&dev->mcu_work, mt7663s_init_work);
115 	dev->reg_map = mt7663_usb_sdio_reg_map;
116 	dev->ops = ops;
117 	sdio_set_drvdata(func, dev);
118 
119 	ret = mt76s_init(mdev, func, &mt7663s_ops);
120 	if (ret < 0)
121 		goto error;
122 
123 	ret = mt76s_hw_init(mdev, func, MT76_CONNAC_SDIO);
124 	if (ret)
125 		goto error;
126 
127 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
128 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
129 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
130 
131 	mdev->sdio.parse_irq = mt7663s_parse_intr;
132 	mdev->sdio.intr_data = devm_kmalloc(mdev->dev,
133 					    sizeof(struct mt7663s_intr),
134 					    GFP_KERNEL);
135 	if (!mdev->sdio.intr_data) {
136 		ret = -ENOMEM;
137 		goto error;
138 	}
139 
140 	for (i = 0; i < ARRAY_SIZE(mdev->sdio.xmit_buf); i++) {
141 		mdev->sdio.xmit_buf[i] = devm_kmalloc(mdev->dev,
142 						      MT76S_XMIT_BUF_SZ,
143 						      GFP_KERNEL);
144 		if (!mdev->sdio.xmit_buf[i]) {
145 			ret = -ENOMEM;
146 			goto error;
147 		}
148 	}
149 
150 	ret = mt76s_alloc_rx_queue(mdev, MT_RXQ_MAIN);
151 	if (ret)
152 		goto error;
153 
154 	ret = mt76s_alloc_tx(mdev);
155 	if (ret)
156 		goto error;
157 
158 	ret = mt76_worker_setup(mt76_hw(dev), &mdev->sdio.txrx_worker,
159 				mt7663s_txrx_worker, "sdio-txrx");
160 	if (ret)
161 		goto error;
162 
163 	sched_set_fifo_low(mdev->sdio.txrx_worker.task);
164 
165 	ret = mt7663_usb_sdio_register_device(dev);
166 	if (ret)
167 		goto error;
168 
169 	return 0;
170 
171 error:
172 	mt76s_deinit(&dev->mt76);
173 	mt76_free_device(&dev->mt76);
174 
175 	return ret;
176 }
177 
mt7663s_remove(struct sdio_func * func)178 static void mt7663s_remove(struct sdio_func *func)
179 {
180 	struct mt7615_dev *dev = sdio_get_drvdata(func);
181 
182 	if (!test_and_clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
183 		return;
184 
185 	ieee80211_unregister_hw(dev->mt76.hw);
186 	mt76s_deinit(&dev->mt76);
187 	mt76_free_device(&dev->mt76);
188 }
189 
190 #ifdef CONFIG_PM
mt7663s_suspend(struct device * dev)191 static int mt7663s_suspend(struct device *dev)
192 {
193 	struct sdio_func *func = dev_to_sdio_func(dev);
194 	struct mt7615_dev *mdev = sdio_get_drvdata(func);
195 	int err;
196 
197 	if (!test_bit(MT76_STATE_SUSPEND, &mdev->mphy.state) &&
198 	    mt7615_firmware_offload(mdev)) {
199 		int err;
200 
201 		err = mt76_connac_mcu_set_hif_suspend(&mdev->mt76, true);
202 		if (err < 0)
203 			return err;
204 	}
205 
206 	sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
207 
208 	err = mt7615_mcu_set_fw_ctrl(mdev);
209 	if (err)
210 		return err;
211 
212 	mt76_worker_disable(&mdev->mt76.sdio.txrx_worker);
213 	mt76_worker_disable(&mdev->mt76.sdio.status_worker);
214 	mt76_worker_disable(&mdev->mt76.sdio.net_worker);
215 
216 	cancel_work_sync(&mdev->mt76.sdio.stat_work);
217 	clear_bit(MT76_READING_STATS, &mdev->mphy.state);
218 
219 	mt76_tx_status_check(&mdev->mt76, true);
220 
221 	return 0;
222 }
223 
mt7663s_resume(struct device * dev)224 static int mt7663s_resume(struct device *dev)
225 {
226 	struct sdio_func *func = dev_to_sdio_func(dev);
227 	struct mt7615_dev *mdev = sdio_get_drvdata(func);
228 	int err;
229 
230 	mt76_worker_enable(&mdev->mt76.sdio.txrx_worker);
231 	mt76_worker_enable(&mdev->mt76.sdio.status_worker);
232 	mt76_worker_enable(&mdev->mt76.sdio.net_worker);
233 
234 	err = mt7615_mcu_set_drv_ctrl(mdev);
235 	if (err)
236 		return err;
237 
238 	if (!test_bit(MT76_STATE_SUSPEND, &mdev->mphy.state) &&
239 	    mt7615_firmware_offload(mdev))
240 		err = mt76_connac_mcu_set_hif_suspend(&mdev->mt76, false);
241 
242 	return err;
243 }
244 
245 static const struct dev_pm_ops mt7663s_pm_ops = {
246 	.suspend = mt7663s_suspend,
247 	.resume = mt7663s_resume,
248 };
249 #endif
250 
251 MODULE_DEVICE_TABLE(sdio, mt7663s_table);
252 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9);
253 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH);
254 MODULE_FIRMWARE(MT7663_FIRMWARE_N9);
255 MODULE_FIRMWARE(MT7663_ROM_PATCH);
256 
257 static struct sdio_driver mt7663s_driver = {
258 	.name		= KBUILD_MODNAME,
259 	.probe		= mt7663s_probe,
260 	.remove		= mt7663s_remove,
261 	.id_table	= mt7663s_table,
262 #ifdef CONFIG_PM
263 	.drv = {
264 		.pm = &mt7663s_pm_ops,
265 	}
266 #endif
267 };
268 module_sdio_driver(mt7663s_driver);
269 
270 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
271 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
272 MODULE_LICENSE("Dual BSD/GPL");
273