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 .driver_data = (kernel_ulong_t)MT7921_FIRMWARE_WM },
22 { } /* Terminating entry */
23 };
24
mt7921s_txrx_worker(struct mt76_worker * w)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
mt7921s_unregister_device(struct mt7921_dev * dev)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
mt7921s_parse_intr(struct mt76_dev * dev,struct mt76s_intr * intr)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
mt7921s_probe(struct sdio_func * func,const struct sdio_device_id * id)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_ps = mt7921_sta_ps,
103 .sta_add = mt7921_mac_sta_add,
104 .sta_assoc = mt7921_mac_sta_assoc,
105 .sta_remove = mt7921_mac_sta_remove,
106 .update_survey = mt7921_update_channel,
107 };
108 static const struct mt76_bus_ops mt7921s_ops = {
109 .rr = mt76s_rr,
110 .rmw = mt76s_rmw,
111 .wr = mt76s_wr,
112 .write_copy = mt76s_write_copy,
113 .read_copy = mt76s_read_copy,
114 .wr_rp = mt76s_wr_rp,
115 .rd_rp = mt76s_rd_rp,
116 .type = MT76_BUS_SDIO,
117 };
118 static const struct mt7921_hif_ops mt7921_sdio_ops = {
119 .init_reset = mt7921s_init_reset,
120 .reset = mt7921s_mac_reset,
121 .mcu_init = mt7921s_mcu_init,
122 .drv_own = mt7921s_mcu_drv_pmctrl,
123 .fw_own = mt7921s_mcu_fw_pmctrl,
124 };
125
126 struct ieee80211_ops *ops;
127 struct mt7921_dev *dev;
128 struct mt76_dev *mdev;
129 u8 features;
130 int ret;
131
132 features = mt7921_check_offload_capability(&func->dev, (const char *)
133 id->driver_data);
134
135 ops = devm_kmemdup(&func->dev, &mt7921_ops, sizeof(mt7921_ops),
136 GFP_KERNEL);
137 if (!ops)
138 return -ENOMEM;
139
140 if (!(features & MT7921_FW_CAP_CNM)) {
141 ops->remain_on_channel = NULL;
142 ops->cancel_remain_on_channel = NULL;
143 ops->add_chanctx = NULL;
144 ops->remove_chanctx = NULL;
145 ops->change_chanctx = NULL;
146 ops->assign_vif_chanctx = NULL;
147 ops->unassign_vif_chanctx = NULL;
148 ops->mgd_prepare_tx = NULL;
149 ops->mgd_complete_tx = NULL;
150 }
151
152 mdev = mt76_alloc_device(&func->dev, sizeof(*dev), ops, &drv_ops);
153 if (!mdev)
154 return -ENOMEM;
155
156 dev = container_of(mdev, struct mt7921_dev, mt76);
157 dev->fw_features = features;
158 dev->hif_ops = &mt7921_sdio_ops;
159 sdio_set_drvdata(func, dev);
160
161 ret = mt76s_init(mdev, func, &mt7921s_ops);
162 if (ret < 0)
163 goto error;
164
165 ret = mt76s_hw_init(mdev, func, MT76_CONNAC2_SDIO);
166 if (ret)
167 goto error;
168
169 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
170 (mt76_rr(dev, MT_HW_REV) & 0xff);
171 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
172
173 mdev->sdio.parse_irq = mt7921s_parse_intr;
174 mdev->sdio.intr_data = devm_kmalloc(mdev->dev,
175 sizeof(struct mt7921_sdio_intr),
176 GFP_KERNEL);
177 if (!mdev->sdio.intr_data) {
178 ret = -ENOMEM;
179 goto error;
180 }
181
182 ret = mt76s_alloc_rx_queue(mdev, MT_RXQ_MAIN);
183 if (ret)
184 goto error;
185
186 ret = mt76s_alloc_rx_queue(mdev, MT_RXQ_MCU);
187 if (ret)
188 goto error;
189
190 ret = mt76s_alloc_tx(mdev);
191 if (ret)
192 goto error;
193
194 ret = mt76_worker_setup(mt76_hw(dev), &mdev->sdio.txrx_worker,
195 mt7921s_txrx_worker, "sdio-txrx");
196 if (ret)
197 goto error;
198
199 sched_set_fifo_low(mdev->sdio.txrx_worker.task);
200
201 ret = mt7921_register_device(dev);
202 if (ret)
203 goto error;
204
205 return 0;
206
207 error:
208 mt76s_deinit(&dev->mt76);
209 mt76_free_device(&dev->mt76);
210
211 return ret;
212 }
213
mt7921s_remove(struct sdio_func * func)214 static void mt7921s_remove(struct sdio_func *func)
215 {
216 struct mt7921_dev *dev = sdio_get_drvdata(func);
217
218 mt7921s_unregister_device(dev);
219 }
220
mt7921s_suspend(struct device * __dev)221 static int mt7921s_suspend(struct device *__dev)
222 {
223 struct sdio_func *func = dev_to_sdio_func(__dev);
224 struct mt7921_dev *dev = sdio_get_drvdata(func);
225 struct mt76_connac_pm *pm = &dev->pm;
226 struct mt76_dev *mdev = &dev->mt76;
227 int err;
228
229 pm->suspended = true;
230 set_bit(MT76_STATE_SUSPEND, &mdev->phy.state);
231
232 flush_work(&dev->reset_work);
233 cancel_delayed_work_sync(&pm->ps_work);
234 cancel_work_sync(&pm->wake_work);
235
236 err = mt7921_mcu_drv_pmctrl(dev);
237 if (err < 0)
238 goto restore_suspend;
239
240 /* always enable deep sleep during suspend to reduce
241 * power consumption
242 */
243 mt76_connac_mcu_set_deep_sleep(mdev, true);
244
245 mt76_txq_schedule_all(&dev->mphy);
246 mt76_worker_disable(&mdev->tx_worker);
247 mt76_worker_disable(&mdev->sdio.status_worker);
248 cancel_work_sync(&mdev->sdio.stat_work);
249 clear_bit(MT76_READING_STATS, &dev->mphy.state);
250 mt76_tx_status_check(mdev, true);
251
252 mt76_worker_schedule(&mdev->sdio.txrx_worker);
253 wait_event_timeout(dev->mt76.sdio.wait,
254 mt76s_txqs_empty(&dev->mt76), 5 * HZ);
255
256 /* It is supposed that SDIO bus is idle at the point */
257 err = mt76_connac_mcu_set_hif_suspend(mdev, true);
258 if (err)
259 goto restore_worker;
260
261 mt76_worker_disable(&mdev->sdio.txrx_worker);
262 mt76_worker_disable(&mdev->sdio.net_worker);
263
264 err = mt7921_mcu_fw_pmctrl(dev);
265 if (err)
266 goto restore_txrx_worker;
267
268 sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
269
270 return 0;
271
272 restore_txrx_worker:
273 mt76_worker_enable(&mdev->sdio.net_worker);
274 mt76_worker_enable(&mdev->sdio.txrx_worker);
275 mt76_connac_mcu_set_hif_suspend(mdev, false);
276
277 restore_worker:
278 mt76_worker_enable(&mdev->tx_worker);
279 mt76_worker_enable(&mdev->sdio.status_worker);
280
281 if (!pm->ds_enable)
282 mt76_connac_mcu_set_deep_sleep(mdev, false);
283
284 restore_suspend:
285 clear_bit(MT76_STATE_SUSPEND, &mdev->phy.state);
286 pm->suspended = false;
287
288 if (err < 0)
289 mt7921_reset(&dev->mt76);
290
291 return err;
292 }
293
mt7921s_resume(struct device * __dev)294 static int mt7921s_resume(struct device *__dev)
295 {
296 struct sdio_func *func = dev_to_sdio_func(__dev);
297 struct mt7921_dev *dev = sdio_get_drvdata(func);
298 struct mt76_connac_pm *pm = &dev->pm;
299 struct mt76_dev *mdev = &dev->mt76;
300 int err;
301
302 clear_bit(MT76_STATE_SUSPEND, &mdev->phy.state);
303
304 err = mt7921_mcu_drv_pmctrl(dev);
305 if (err < 0)
306 goto failed;
307
308 mt76_worker_enable(&mdev->tx_worker);
309 mt76_worker_enable(&mdev->sdio.txrx_worker);
310 mt76_worker_enable(&mdev->sdio.status_worker);
311 mt76_worker_enable(&mdev->sdio.net_worker);
312
313 /* restore previous ds setting */
314 if (!pm->ds_enable)
315 mt76_connac_mcu_set_deep_sleep(mdev, false);
316
317 err = mt76_connac_mcu_set_hif_suspend(mdev, false);
318 failed:
319 pm->suspended = false;
320
321 if (err < 0)
322 mt7921_reset(&dev->mt76);
323
324 return err;
325 }
326
327 MODULE_DEVICE_TABLE(sdio, mt7921s_table);
328 MODULE_FIRMWARE(MT7921_FIRMWARE_WM);
329 MODULE_FIRMWARE(MT7921_ROM_PATCH);
330
331 static DEFINE_SIMPLE_DEV_PM_OPS(mt7921s_pm_ops, mt7921s_suspend, mt7921s_resume);
332
333 static struct sdio_driver mt7921s_driver = {
334 .name = KBUILD_MODNAME,
335 .probe = mt7921s_probe,
336 .remove = mt7921s_remove,
337 .id_table = mt7921s_table,
338 .drv.pm = pm_sleep_ptr(&mt7921s_pm_ops),
339 };
340 module_sdio_driver(mt7921s_driver);
341 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
342 MODULE_LICENSE("Dual BSD/GPL");
343