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