1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2021 MediaTek Inc.
3  *
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/iopoll.h>
8 #include <linux/module.h>
9 
10 #include <linux/mmc/host.h>
11 #include <linux/mmc/sdio_ids.h>
12 #include <linux/mmc/sdio_func.h>
13 
14 #include "mt7921.h"
15 #include "../sdio.h"
16 #include "mac.h"
17 #include "mcu.h"
18 
19 static const struct sdio_device_id mt7921s_table[] = {
20 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, 0x7901) },
21 	{ }	/* Terminating entry */
22 };
23 
24 static void mt7921s_txrx_worker(struct mt76_worker *w)
25 {
26 	struct mt76_sdio *sdio = container_of(w, struct mt76_sdio,
27 					      txrx_worker);
28 	struct mt76_dev *mdev = container_of(sdio, struct mt76_dev, sdio);
29 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
30 
31 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
32 		queue_work(mdev->wq, &dev->pm.wake_work);
33 		return;
34 	}
35 
36 	mt76s_txrx_worker(sdio);
37 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
38 }
39 
40 static void mt7921s_unregister_device(struct mt7921_dev *dev)
41 {
42 	struct mt76_connac_pm *pm = &dev->pm;
43 
44 	cancel_work_sync(&dev->init_work);
45 	mt76_unregister_device(&dev->mt76);
46 	cancel_delayed_work_sync(&pm->ps_work);
47 	cancel_work_sync(&pm->wake_work);
48 
49 	mt76s_deinit(&dev->mt76);
50 	mt7921s_wfsys_reset(dev);
51 	skb_queue_purge(&dev->mt76.mcu.res_q);
52 
53 	mt76_free_device(&dev->mt76);
54 }
55 
56 static int mt7921s_parse_intr(struct mt76_dev *dev, struct mt76s_intr *intr)
57 {
58 	struct mt76_sdio *sdio = &dev->sdio;
59 	struct mt7921_sdio_intr *irq_data = sdio->intr_data;
60 	int i, err;
61 
62 	sdio_claim_host(sdio->func);
63 	err = sdio_readsb(sdio->func, irq_data, MCR_WHISR, sizeof(*irq_data));
64 	sdio_release_host(sdio->func);
65 
66 	if (err < 0)
67 		return err;
68 
69 	if (irq_data->rx.num[0] > 16 ||
70 	    irq_data->rx.num[1] > 128)
71 		return -EINVAL;
72 
73 	intr->isr = irq_data->isr;
74 	intr->rec_mb = irq_data->rec_mb;
75 	intr->tx.wtqcr = irq_data->tx.wtqcr;
76 	intr->rx.num = irq_data->rx.num;
77 	for (i = 0; i < 2 ; i++) {
78 		if (!i)
79 			intr->rx.len[0] = irq_data->rx.len0;
80 		else
81 			intr->rx.len[1] = irq_data->rx.len1;
82 	}
83 
84 	return 0;
85 }
86 
87 static int mt7921s_probe(struct sdio_func *func,
88 			 const struct sdio_device_id *id)
89 {
90 	static const struct mt76_driver_ops drv_ops = {
91 		.txwi_size = MT_SDIO_TXD_SIZE,
92 		.survey_flags = SURVEY_INFO_TIME_TX |
93 				SURVEY_INFO_TIME_RX |
94 				SURVEY_INFO_TIME_BSS_RX,
95 		.tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb,
96 		.tx_complete_skb = mt7921_usb_sdio_tx_complete_skb,
97 		.tx_status_data = mt7921_usb_sdio_tx_status_data,
98 		.rx_skb = mt7921_queue_rx_skb,
99 		.rx_check = mt7921_rx_check,
100 		.sta_ps = mt7921_sta_ps,
101 		.sta_add = mt7921_mac_sta_add,
102 		.sta_assoc = mt7921_mac_sta_assoc,
103 		.sta_remove = mt7921_mac_sta_remove,
104 		.update_survey = mt7921_update_channel,
105 	};
106 	static const struct mt76_bus_ops mt7921s_ops = {
107 		.rr = mt76s_rr,
108 		.rmw = mt76s_rmw,
109 		.wr = mt76s_wr,
110 		.write_copy = mt76s_write_copy,
111 		.read_copy = mt76s_read_copy,
112 		.wr_rp = mt76s_wr_rp,
113 		.rd_rp = mt76s_rd_rp,
114 		.type = MT76_BUS_SDIO,
115 	};
116 	static const struct mt7921_hif_ops mt7921_sdio_ops = {
117 		.init_reset = mt7921s_init_reset,
118 		.reset = mt7921s_mac_reset,
119 		.mcu_init = mt7921s_mcu_init,
120 		.drv_own = mt7921s_mcu_drv_pmctrl,
121 		.fw_own = mt7921s_mcu_fw_pmctrl,
122 	};
123 
124 	struct mt7921_dev *dev;
125 	struct mt76_dev *mdev;
126 	int ret;
127 
128 	mdev = mt76_alloc_device(&func->dev, sizeof(*dev), &mt7921_ops,
129 				 &drv_ops);
130 	if (!mdev)
131 		return -ENOMEM;
132 
133 	dev = container_of(mdev, struct mt7921_dev, mt76);
134 	dev->hif_ops = &mt7921_sdio_ops;
135 
136 	sdio_set_drvdata(func, dev);
137 
138 	ret = mt76s_init(mdev, func, &mt7921s_ops);
139 	if (ret < 0)
140 		goto error;
141 
142 	ret = mt76s_hw_init(mdev, func, MT76_CONNAC2_SDIO);
143 	if (ret)
144 		goto error;
145 
146 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
147 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
148 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
149 
150 	mdev->sdio.parse_irq = mt7921s_parse_intr;
151 	mdev->sdio.intr_data = devm_kmalloc(mdev->dev,
152 					    sizeof(struct mt7921_sdio_intr),
153 					    GFP_KERNEL);
154 	if (!mdev->sdio.intr_data) {
155 		ret = -ENOMEM;
156 		goto error;
157 	}
158 
159 	ret = mt76s_alloc_rx_queue(mdev, MT_RXQ_MAIN);
160 	if (ret)
161 		goto error;
162 
163 	ret = mt76s_alloc_rx_queue(mdev, MT_RXQ_MCU);
164 	if (ret)
165 		goto error;
166 
167 	ret = mt76s_alloc_tx(mdev);
168 	if (ret)
169 		goto error;
170 
171 	ret = mt76_worker_setup(mt76_hw(dev), &mdev->sdio.txrx_worker,
172 				mt7921s_txrx_worker, "sdio-txrx");
173 	if (ret)
174 		goto error;
175 
176 	sched_set_fifo_low(mdev->sdio.txrx_worker.task);
177 
178 	ret = mt7921_register_device(dev);
179 	if (ret)
180 		goto error;
181 
182 	return 0;
183 
184 error:
185 	mt76s_deinit(&dev->mt76);
186 	mt76_free_device(&dev->mt76);
187 
188 	return ret;
189 }
190 
191 static void mt7921s_remove(struct sdio_func *func)
192 {
193 	struct mt7921_dev *dev = sdio_get_drvdata(func);
194 
195 	mt7921s_unregister_device(dev);
196 }
197 
198 static int mt7921s_suspend(struct device *__dev)
199 {
200 	struct sdio_func *func = dev_to_sdio_func(__dev);
201 	struct mt7921_dev *dev = sdio_get_drvdata(func);
202 	struct mt76_connac_pm *pm = &dev->pm;
203 	struct mt76_dev *mdev = &dev->mt76;
204 	int err;
205 
206 	pm->suspended = true;
207 	set_bit(MT76_STATE_SUSPEND, &mdev->phy.state);
208 
209 	flush_work(&dev->reset_work);
210 	cancel_delayed_work_sync(&pm->ps_work);
211 	cancel_work_sync(&pm->wake_work);
212 
213 	err = mt7921_mcu_drv_pmctrl(dev);
214 	if (err < 0)
215 		goto restore_suspend;
216 
217 	/* always enable deep sleep during suspend to reduce
218 	 * power consumption
219 	 */
220 	mt76_connac_mcu_set_deep_sleep(mdev, true);
221 
222 	mt76_txq_schedule_all(&dev->mphy);
223 	mt76_worker_disable(&mdev->tx_worker);
224 	mt76_worker_disable(&mdev->sdio.status_worker);
225 	cancel_work_sync(&mdev->sdio.stat_work);
226 	clear_bit(MT76_READING_STATS, &dev->mphy.state);
227 	mt76_tx_status_check(mdev, true);
228 
229 	mt76_worker_schedule(&mdev->sdio.txrx_worker);
230 	wait_event_timeout(dev->mt76.sdio.wait,
231 			   mt76s_txqs_empty(&dev->mt76), 5 * HZ);
232 
233 	/* It is supposed that SDIO bus is idle at the point */
234 	err = mt76_connac_mcu_set_hif_suspend(mdev, true);
235 	if (err)
236 		goto restore_worker;
237 
238 	mt76_worker_disable(&mdev->sdio.txrx_worker);
239 	mt76_worker_disable(&mdev->sdio.net_worker);
240 
241 	err = mt7921_mcu_fw_pmctrl(dev);
242 	if (err)
243 		goto restore_txrx_worker;
244 
245 	sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
246 
247 	return 0;
248 
249 restore_txrx_worker:
250 	mt76_worker_enable(&mdev->sdio.net_worker);
251 	mt76_worker_enable(&mdev->sdio.txrx_worker);
252 	mt76_connac_mcu_set_hif_suspend(mdev, false);
253 
254 restore_worker:
255 	mt76_worker_enable(&mdev->tx_worker);
256 	mt76_worker_enable(&mdev->sdio.status_worker);
257 
258 	if (!pm->ds_enable)
259 		mt76_connac_mcu_set_deep_sleep(mdev, false);
260 
261 restore_suspend:
262 	clear_bit(MT76_STATE_SUSPEND, &mdev->phy.state);
263 	pm->suspended = false;
264 
265 	if (err < 0)
266 		mt7921_reset(&dev->mt76);
267 
268 	return err;
269 }
270 
271 static int mt7921s_resume(struct device *__dev)
272 {
273 	struct sdio_func *func = dev_to_sdio_func(__dev);
274 	struct mt7921_dev *dev = sdio_get_drvdata(func);
275 	struct mt76_connac_pm *pm = &dev->pm;
276 	struct mt76_dev *mdev = &dev->mt76;
277 	int err;
278 
279 	clear_bit(MT76_STATE_SUSPEND, &mdev->phy.state);
280 
281 	err = mt7921_mcu_drv_pmctrl(dev);
282 	if (err < 0)
283 		goto failed;
284 
285 	mt76_worker_enable(&mdev->tx_worker);
286 	mt76_worker_enable(&mdev->sdio.txrx_worker);
287 	mt76_worker_enable(&mdev->sdio.status_worker);
288 	mt76_worker_enable(&mdev->sdio.net_worker);
289 
290 	/* restore previous ds setting */
291 	if (!pm->ds_enable)
292 		mt76_connac_mcu_set_deep_sleep(mdev, false);
293 
294 	err = mt76_connac_mcu_set_hif_suspend(mdev, false);
295 failed:
296 	pm->suspended = false;
297 
298 	if (err < 0)
299 		mt7921_reset(&dev->mt76);
300 
301 	return err;
302 }
303 
304 MODULE_DEVICE_TABLE(sdio, mt7921s_table);
305 MODULE_FIRMWARE(MT7921_FIRMWARE_WM);
306 MODULE_FIRMWARE(MT7921_ROM_PATCH);
307 
308 static DEFINE_SIMPLE_DEV_PM_OPS(mt7921s_pm_ops, mt7921s_suspend, mt7921s_resume);
309 
310 static struct sdio_driver mt7921s_driver = {
311 	.name		= KBUILD_MODNAME,
312 	.probe		= mt7921s_probe,
313 	.remove		= mt7921s_remove,
314 	.id_table	= mt7921s_table,
315 	.drv.pm		= pm_sleep_ptr(&mt7921s_pm_ops),
316 };
317 module_sdio_driver(mt7921s_driver);
318 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
319 MODULE_LICENSE("Dual BSD/GPL");
320