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