xref: /linux/drivers/net/wireless/mediatek/mt76/mt7615/sdio.c (revision 722ecdbce68a87de2d9296f91308f44ea900a039)
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 
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 
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 
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 	sdio_claim_host(sdio->func);
60 	err = sdio_readsb(sdio->func, irq_data, MCR_WHISR, sizeof(*irq_data));
61 	sdio_release_host(sdio->func);
62 
63 	if (err)
64 		return err;
65 
66 	intr->isr = irq_data->isr;
67 	intr->rec_mb = irq_data->rec_mb;
68 	intr->tx.wtqcr = irq_data->tx.wtqcr;
69 	intr->rx.num = irq_data->rx.num;
70 	for (i = 0; i < 2 ; i++)
71 		intr->rx.len[i] = irq_data->rx.len[i];
72 
73 	return 0;
74 }
75 
76 static int mt7663s_probe(struct sdio_func *func,
77 			 const struct sdio_device_id *id)
78 {
79 	static const struct mt76_driver_ops drv_ops = {
80 		.txwi_size = MT_USB_TXD_SIZE,
81 		.drv_flags = MT_DRV_RX_DMA_HDR,
82 		.tx_prepare_skb = mt7663_usb_sdio_tx_prepare_skb,
83 		.tx_complete_skb = mt7663_usb_sdio_tx_complete_skb,
84 		.tx_status_data = mt7663_usb_sdio_tx_status_data,
85 		.rx_skb = mt7615_queue_rx_skb,
86 		.sta_ps = mt7615_sta_ps,
87 		.sta_add = mt7615_mac_sta_add,
88 		.sta_remove = mt7615_mac_sta_remove,
89 		.update_survey = mt7615_update_channel,
90 	};
91 	static const struct mt76_bus_ops mt7663s_ops = {
92 		.rr = mt76s_rr,
93 		.rmw = mt76s_rmw,
94 		.wr = mt76s_wr,
95 		.write_copy = mt76s_write_copy,
96 		.read_copy = mt76s_read_copy,
97 		.wr_rp = mt76s_wr_rp,
98 		.rd_rp = mt76s_rd_rp,
99 		.type = MT76_BUS_SDIO,
100 	};
101 	struct ieee80211_ops *ops;
102 	struct mt7615_dev *dev;
103 	struct mt76_dev *mdev;
104 	int ret;
105 
106 	ops = devm_kmemdup(&func->dev, &mt7615_ops, sizeof(mt7615_ops),
107 			   GFP_KERNEL);
108 	if (!ops)
109 		return -ENOMEM;
110 
111 	mdev = mt76_alloc_device(&func->dev, sizeof(*dev), ops, &drv_ops);
112 	if (!mdev)
113 		return -ENOMEM;
114 
115 	dev = container_of(mdev, struct mt7615_dev, mt76);
116 
117 	INIT_WORK(&dev->mcu_work, mt7663s_init_work);
118 	dev->reg_map = mt7663_usb_sdio_reg_map;
119 	dev->ops = ops;
120 	sdio_set_drvdata(func, dev);
121 
122 	ret = mt76s_init(mdev, func, &mt7663s_ops);
123 	if (ret < 0)
124 		goto error;
125 
126 	ret = mt76s_hw_init(mdev, func, MT76_CONNAC_SDIO);
127 	if (ret)
128 		goto error;
129 
130 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
131 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
132 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
133 
134 	mdev->sdio.parse_irq = mt7663s_parse_intr;
135 	mdev->sdio.intr_data = devm_kmalloc(mdev->dev,
136 					    sizeof(struct mt7663s_intr),
137 					    GFP_KERNEL);
138 	if (!mdev->sdio.intr_data) {
139 		ret = -ENOMEM;
140 		goto error;
141 	}
142 
143 	ret = mt76s_alloc_rx_queue(mdev, MT_RXQ_MAIN);
144 	if (ret)
145 		goto error;
146 
147 	ret = mt76s_alloc_tx(mdev);
148 	if (ret)
149 		goto error;
150 
151 	ret = mt76_worker_setup(mt76_hw(dev), &mdev->sdio.txrx_worker,
152 				mt7663s_txrx_worker, "sdio-txrx");
153 	if (ret)
154 		goto error;
155 
156 	sched_set_fifo_low(mdev->sdio.txrx_worker.task);
157 
158 	ret = mt7663_usb_sdio_register_device(dev);
159 	if (ret)
160 		goto error;
161 
162 	return 0;
163 
164 error:
165 	mt76s_deinit(&dev->mt76);
166 	mt76_free_device(&dev->mt76);
167 
168 	return ret;
169 }
170 
171 static void mt7663s_remove(struct sdio_func *func)
172 {
173 	struct mt7615_dev *dev = sdio_get_drvdata(func);
174 
175 	if (!test_and_clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
176 		return;
177 
178 	ieee80211_unregister_hw(dev->mt76.hw);
179 	mt76s_deinit(&dev->mt76);
180 	mt76_free_device(&dev->mt76);
181 }
182 
183 #ifdef CONFIG_PM
184 static int mt7663s_suspend(struct device *dev)
185 {
186 	struct sdio_func *func = dev_to_sdio_func(dev);
187 	struct mt7615_dev *mdev = sdio_get_drvdata(func);
188 	int err;
189 
190 	if (!test_bit(MT76_STATE_SUSPEND, &mdev->mphy.state) &&
191 	    mt7615_firmware_offload(mdev)) {
192 		int err;
193 
194 		err = mt76_connac_mcu_set_hif_suspend(&mdev->mt76, true);
195 		if (err < 0)
196 			return err;
197 	}
198 
199 	sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
200 
201 	err = mt7615_mcu_set_fw_ctrl(mdev);
202 	if (err)
203 		return err;
204 
205 	mt76_worker_disable(&mdev->mt76.sdio.txrx_worker);
206 	mt76_worker_disable(&mdev->mt76.sdio.status_worker);
207 	mt76_worker_disable(&mdev->mt76.sdio.net_worker);
208 
209 	cancel_work_sync(&mdev->mt76.sdio.stat_work);
210 	clear_bit(MT76_READING_STATS, &mdev->mphy.state);
211 
212 	mt76_tx_status_check(&mdev->mt76, true);
213 
214 	return 0;
215 }
216 
217 static int mt7663s_resume(struct device *dev)
218 {
219 	struct sdio_func *func = dev_to_sdio_func(dev);
220 	struct mt7615_dev *mdev = sdio_get_drvdata(func);
221 	int err;
222 
223 	mt76_worker_enable(&mdev->mt76.sdio.txrx_worker);
224 	mt76_worker_enable(&mdev->mt76.sdio.status_worker);
225 	mt76_worker_enable(&mdev->mt76.sdio.net_worker);
226 
227 	err = mt7615_mcu_set_drv_ctrl(mdev);
228 	if (err)
229 		return err;
230 
231 	if (!test_bit(MT76_STATE_SUSPEND, &mdev->mphy.state) &&
232 	    mt7615_firmware_offload(mdev))
233 		err = mt76_connac_mcu_set_hif_suspend(&mdev->mt76, false);
234 
235 	return err;
236 }
237 
238 static const struct dev_pm_ops mt7663s_pm_ops = {
239 	.suspend = mt7663s_suspend,
240 	.resume = mt7663s_resume,
241 };
242 #endif
243 
244 MODULE_DEVICE_TABLE(sdio, mt7663s_table);
245 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9);
246 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH);
247 MODULE_FIRMWARE(MT7663_FIRMWARE_N9);
248 MODULE_FIRMWARE(MT7663_ROM_PATCH);
249 
250 static struct sdio_driver mt7663s_driver = {
251 	.name		= KBUILD_MODNAME,
252 	.probe		= mt7663s_probe,
253 	.remove		= mt7663s_remove,
254 	.id_table	= mt7663s_table,
255 #ifdef CONFIG_PM
256 	.drv = {
257 		.pm = &mt7663s_pm_ops,
258 	}
259 #endif
260 };
261 module_sdio_driver(mt7663s_driver);
262 
263 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
264 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
265 MODULE_LICENSE("Dual BSD/GPL");
266