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