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
mt7663s_txrx_worker(struct mt76_worker * w)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
mt7663s_init_work(struct work_struct * work)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
mt7663s_parse_intr(struct mt76_dev * dev,struct mt76s_intr * intr)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
mt7663s_probe(struct sdio_func * func,const struct sdio_device_id * id)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 .rx_check = mt7615_rx_check,
87 .sta_add = mt7615_mac_sta_add,
88 .sta_remove = mt7615_mac_sta_remove,
89 .update_survey = mt7615_update_channel,
90 .set_channel = mt7615_set_channel,
91 };
92 static const struct mt76_bus_ops mt7663s_ops = {
93 .rr = mt76s_rr,
94 .rmw = mt76s_rmw,
95 .wr = mt76s_wr,
96 .write_copy = mt76s_write_copy,
97 .read_copy = mt76s_read_copy,
98 .wr_rp = mt76s_wr_rp,
99 .rd_rp = mt76s_rd_rp,
100 .type = MT76_BUS_SDIO,
101 };
102 struct ieee80211_ops *ops;
103 struct mt7615_dev *dev;
104 struct mt76_dev *mdev;
105 int ret;
106
107 ops = devm_kmemdup(&func->dev, &mt7615_ops, sizeof(mt7615_ops),
108 GFP_KERNEL);
109 if (!ops)
110 return -ENOMEM;
111
112 mdev = mt76_alloc_device(&func->dev, sizeof(*dev), ops, &drv_ops);
113 if (!mdev)
114 return -ENOMEM;
115
116 dev = container_of(mdev, struct mt7615_dev, mt76);
117
118 INIT_WORK(&dev->mcu_work, mt7663s_init_work);
119 dev->reg_map = mt7663_usb_sdio_reg_map;
120 dev->ops = ops;
121 sdio_set_drvdata(func, dev);
122
123 ret = mt76s_init(mdev, func, &mt7663s_ops);
124 if (ret < 0)
125 goto error;
126
127 ret = mt76s_hw_init(mdev, func, MT76_CONNAC_SDIO);
128 if (ret)
129 goto error;
130
131 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
132 (mt76_rr(dev, MT_HW_REV) & 0xff);
133 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
134
135 mdev->sdio.parse_irq = mt7663s_parse_intr;
136 mdev->sdio.intr_data = devm_kmalloc(mdev->dev,
137 sizeof(struct mt7663s_intr),
138 GFP_KERNEL);
139 if (!mdev->sdio.intr_data) {
140 ret = -ENOMEM;
141 goto error;
142 }
143
144 ret = mt76s_alloc_rx_queue(mdev, MT_RXQ_MAIN);
145 if (ret)
146 goto error;
147
148 ret = mt76s_alloc_tx(mdev);
149 if (ret)
150 goto error;
151
152 ret = mt76_worker_setup(mt76_hw(dev), &mdev->sdio.txrx_worker,
153 mt7663s_txrx_worker, "sdio-txrx");
154 if (ret)
155 goto error;
156
157 sched_set_fifo_low(mdev->sdio.txrx_worker.task);
158
159 ret = mt7663_usb_sdio_register_device(dev);
160 if (ret)
161 goto error;
162
163 return 0;
164
165 error:
166 mt76s_deinit(&dev->mt76);
167 mt76_free_device(&dev->mt76);
168
169 return ret;
170 }
171
mt7663s_remove(struct sdio_func * func)172 static void mt7663s_remove(struct sdio_func *func)
173 {
174 struct mt7615_dev *dev = sdio_get_drvdata(func);
175
176 if (!test_and_clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
177 return;
178
179 ieee80211_unregister_hw(dev->mt76.hw);
180 mt76s_deinit(&dev->mt76);
181 mt76_free_device(&dev->mt76);
182 }
183
mt7663s_suspend(struct device * dev)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 mt76_worker_disable(&mdev->mt76.sdio.stat_worker);
209
210 clear_bit(MT76_READING_STATS, &mdev->mphy.state);
211
212 mt76_tx_status_check(&mdev->mt76, true);
213
214 return 0;
215 }
216
mt7663s_resume(struct device * dev)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 MODULE_DEVICE_TABLE(sdio, mt7663s_table);
239 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9);
240 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH);
241 MODULE_FIRMWARE(MT7663_FIRMWARE_N9);
242 MODULE_FIRMWARE(MT7663_ROM_PATCH);
243
244 static DEFINE_SIMPLE_DEV_PM_OPS(mt7663s_pm_ops, mt7663s_suspend, mt7663s_resume);
245
246 static struct sdio_driver mt7663s_driver = {
247 .name = KBUILD_MODNAME,
248 .probe = mt7663s_probe,
249 .remove = mt7663s_remove,
250 .id_table = mt7663s_table,
251 .drv.pm = pm_sleep_ptr(&mt7663s_pm_ops),
252 };
253 module_sdio_driver(mt7663s_driver);
254
255 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
256 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
257 MODULE_DESCRIPTION("MediaTek MT7663S (SDIO) wireless driver");
258 MODULE_LICENSE("Dual BSD/GPL");
259