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 		.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 	};
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 static int mt7663s_suspend(struct device *dev)
184 {
185 	struct sdio_func *func = dev_to_sdio_func(dev);
186 	struct mt7615_dev *mdev = sdio_get_drvdata(func);
187 	int err;
188 
189 	if (!test_bit(MT76_STATE_SUSPEND, &mdev->mphy.state) &&
190 	    mt7615_firmware_offload(mdev)) {
191 		int err;
192 
193 		err = mt76_connac_mcu_set_hif_suspend(&mdev->mt76, true);
194 		if (err < 0)
195 			return err;
196 	}
197 
198 	sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
199 
200 	err = mt7615_mcu_set_fw_ctrl(mdev);
201 	if (err)
202 		return err;
203 
204 	mt76_worker_disable(&mdev->mt76.sdio.txrx_worker);
205 	mt76_worker_disable(&mdev->mt76.sdio.status_worker);
206 	mt76_worker_disable(&mdev->mt76.sdio.net_worker);
207 	mt76_worker_disable(&mdev->mt76.sdio.stat_worker);
208 
209 	clear_bit(MT76_READING_STATS, &mdev->mphy.state);
210 
211 	mt76_tx_status_check(&mdev->mt76, true);
212 
213 	return 0;
214 }
215 
216 static int mt7663s_resume(struct device *dev)
217 {
218 	struct sdio_func *func = dev_to_sdio_func(dev);
219 	struct mt7615_dev *mdev = sdio_get_drvdata(func);
220 	int err;
221 
222 	mt76_worker_enable(&mdev->mt76.sdio.txrx_worker);
223 	mt76_worker_enable(&mdev->mt76.sdio.status_worker);
224 	mt76_worker_enable(&mdev->mt76.sdio.net_worker);
225 
226 	err = mt7615_mcu_set_drv_ctrl(mdev);
227 	if (err)
228 		return err;
229 
230 	if (!test_bit(MT76_STATE_SUSPEND, &mdev->mphy.state) &&
231 	    mt7615_firmware_offload(mdev))
232 		err = mt76_connac_mcu_set_hif_suspend(&mdev->mt76, false);
233 
234 	return err;
235 }
236 
237 MODULE_DEVICE_TABLE(sdio, mt7663s_table);
238 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9);
239 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH);
240 MODULE_FIRMWARE(MT7663_FIRMWARE_N9);
241 MODULE_FIRMWARE(MT7663_ROM_PATCH);
242 
243 static DEFINE_SIMPLE_DEV_PM_OPS(mt7663s_pm_ops, mt7663s_suspend, mt7663s_resume);
244 
245 static struct sdio_driver mt7663s_driver = {
246 	.name		= KBUILD_MODNAME,
247 	.probe		= mt7663s_probe,
248 	.remove		= mt7663s_remove,
249 	.id_table	= mt7663s_table,
250 	.drv.pm		= pm_sleep_ptr(&mt7663s_pm_ops),
251 };
252 module_sdio_driver(mt7663s_driver);
253 
254 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
255 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
256 MODULE_LICENSE("Dual BSD/GPL");
257