1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
8 #include <linux/mmc/sdio_func.h>
9 #include <linux/mmc/host.h>
14 #define SDIO_MODALIAS "wilc1000_sdio"
16 #define SDIO_VENDOR_ID_WILC 0x0296
17 #define SDIO_DEVICE_ID_WILC 0x5347
19 static const struct sdio_device_id wilc_sdio_ids[] = {
20 { SDIO_DEVICE(SDIO_VENDOR_ID_WILC, SDIO_DEVICE_ID_WILC) },
24 #define WILC_SDIO_BLOCK_SIZE 512
30 /* Max num interrupts allowed in registers 0xf7, 0xf8 */
31 #define MAX_NUN_INT_THRPT_ENH2 (5)
54 static const struct wilc_hif_func wilc_hif_sdio;
56 static void wilc_sdio_interrupt(struct sdio_func *func)
58 sdio_release_host(func);
59 wilc_handle_isr(sdio_get_drvdata(func));
60 sdio_claim_host(func);
63 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
65 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
69 sdio_claim_host(func);
71 func->num = cmd->function;
72 if (cmd->read_write) { /* write */
74 sdio_writeb(func, cmd->data, cmd->address, &ret);
75 data = sdio_readb(func, cmd->address, &ret);
78 sdio_writeb(func, cmd->data, cmd->address, &ret);
81 data = sdio_readb(func, cmd->address, &ret);
85 sdio_release_host(func);
88 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
92 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
94 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
97 sdio_claim_host(func);
99 func->num = cmd->function;
100 func->cur_blksize = cmd->block_size;
102 size = cmd->count * cmd->block_size;
106 if (cmd->read_write) { /* write */
107 ret = sdio_memcpy_toio(func, cmd->address,
108 (void *)cmd->buffer, size);
110 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
114 sdio_release_host(func);
117 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
122 static int wilc_sdio_probe(struct sdio_func *func,
123 const struct sdio_device_id *id)
127 struct gpio_desc *gpio = NULL;
128 struct wilc_sdio *sdio_priv;
130 sdio_priv = kzalloc(sizeof(*sdio_priv), GFP_KERNEL);
134 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
135 gpio = gpiod_get(&func->dev, "irq", GPIOD_IN);
137 /* get the GPIO descriptor from hardcode GPIO number */
138 gpio = gpio_to_desc(GPIO_NUM);
140 dev_err(&func->dev, "failed to get irq gpio\n");
144 ret = wilc_cfg80211_init(&wilc, &func->dev, WILC_HIF_SDIO,
150 sdio_set_drvdata(func, wilc);
151 wilc->bus_data = sdio_priv;
152 wilc->dev = &func->dev;
153 wilc->gpio_irq = gpio;
155 wilc->rtc_clk = devm_clk_get(&func->card->dev, "rtc_clk");
156 if (PTR_ERR_OR_ZERO(wilc->rtc_clk) == -EPROBE_DEFER)
157 return -EPROBE_DEFER;
158 else if (!IS_ERR(wilc->rtc_clk))
159 clk_prepare_enable(wilc->rtc_clk);
161 dev_info(&func->dev, "Driver Initializing success\n");
165 static void wilc_sdio_remove(struct sdio_func *func)
167 struct wilc *wilc = sdio_get_drvdata(func);
169 /* free the GPIO in module remove */
171 gpiod_put(wilc->gpio_irq);
173 if (!IS_ERR(wilc->rtc_clk))
174 clk_disable_unprepare(wilc->rtc_clk);
176 wilc_netdev_cleanup(wilc);
179 static int wilc_sdio_reset(struct wilc *wilc)
181 struct sdio_cmd52 cmd;
183 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
190 ret = wilc_sdio_cmd52(wilc, &cmd);
192 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
198 static int wilc_sdio_suspend(struct device *dev)
200 struct sdio_func *func = dev_to_sdio_func(dev);
201 struct wilc *wilc = sdio_get_drvdata(func);
204 dev_info(dev, "sdio suspend\n");
207 if (!IS_ERR(wilc->rtc_clk))
208 clk_disable_unprepare(wilc->rtc_clk);
210 if (wilc->suspend_event) {
211 host_sleep_notify(wilc);
212 chip_allow_sleep(wilc);
215 ret = wilc_sdio_reset(wilc);
217 dev_err(&func->dev, "Fail reset sdio\n");
220 sdio_claim_host(func);
225 static int wilc_sdio_enable_interrupt(struct wilc *dev)
227 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
230 sdio_claim_host(func);
231 ret = sdio_claim_irq(func, wilc_sdio_interrupt);
232 sdio_release_host(func);
235 dev_err(&func->dev, "can't claim sdio_irq, err(%d)\n", ret);
241 static void wilc_sdio_disable_interrupt(struct wilc *dev)
243 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
246 sdio_claim_host(func);
247 ret = sdio_release_irq(func);
249 dev_err(&func->dev, "can't release sdio_irq, err(%d)\n", ret);
250 sdio_release_host(func);
253 /********************************************
257 ********************************************/
259 static int wilc_sdio_set_func0_csa_address(struct wilc *wilc, u32 adr)
261 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
262 struct sdio_cmd52 cmd;
273 ret = wilc_sdio_cmd52(wilc, &cmd);
275 dev_err(&func->dev, "Failed cmd52, set 0x10c data...\n");
280 cmd.data = (u8)(adr >> 8);
281 ret = wilc_sdio_cmd52(wilc, &cmd);
283 dev_err(&func->dev, "Failed cmd52, set 0x10d data...\n");
288 cmd.data = (u8)(adr >> 16);
289 ret = wilc_sdio_cmd52(wilc, &cmd);
291 dev_err(&func->dev, "Failed cmd52, set 0x10e data...\n");
300 static int wilc_sdio_set_func0_block_size(struct wilc *wilc, u32 block_size)
302 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
303 struct sdio_cmd52 cmd;
310 cmd.data = (u8)block_size;
311 ret = wilc_sdio_cmd52(wilc, &cmd);
313 dev_err(&func->dev, "Failed cmd52, set 0x10 data...\n");
318 cmd.data = (u8)(block_size >> 8);
319 ret = wilc_sdio_cmd52(wilc, &cmd);
321 dev_err(&func->dev, "Failed cmd52, set 0x11 data...\n");
330 /********************************************
334 ********************************************/
336 static int wilc_sdio_set_func1_block_size(struct wilc *wilc, u32 block_size)
338 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
339 struct sdio_cmd52 cmd;
346 cmd.data = (u8)block_size;
347 ret = wilc_sdio_cmd52(wilc, &cmd);
349 dev_err(&func->dev, "Failed cmd52, set 0x110 data...\n");
353 cmd.data = (u8)(block_size >> 8);
354 ret = wilc_sdio_cmd52(wilc, &cmd);
356 dev_err(&func->dev, "Failed cmd52, set 0x111 data...\n");
365 /********************************************
369 ********************************************/
370 static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
372 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
373 struct wilc_sdio *sdio_priv = wilc->bus_data;
378 if (addr >= 0xf0 && addr <= 0xff) {
379 struct sdio_cmd52 cmd;
386 ret = wilc_sdio_cmd52(wilc, &cmd);
389 "Failed cmd 52, read reg (%08x) ...\n", addr);
393 struct sdio_cmd53 cmd;
396 * set the AHB address
398 if (!wilc_sdio_set_func0_csa_address(wilc, addr))
407 cmd.buffer = (u8 *)&data;
408 cmd.block_size = sdio_priv->block_size;
409 ret = wilc_sdio_cmd53(wilc, &cmd);
412 "Failed cmd53, write reg (%08x)...\n", addr);
424 static int wilc_sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
426 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
427 struct wilc_sdio *sdio_priv = wilc->bus_data;
428 u32 block_size = sdio_priv->block_size;
429 struct sdio_cmd53 cmd;
430 int nblk, nleft, ret;
435 * has to be word aligned...
449 * has to be word aligned...
463 nblk = size / block_size;
464 nleft = size % block_size;
471 cmd.block_size = block_size;
473 if (!wilc_sdio_set_func0_csa_address(wilc, addr))
476 ret = wilc_sdio_cmd53(wilc, &cmd);
479 "Failed cmd53 [%x], block send...\n", addr);
483 addr += nblk * block_size;
484 buf += nblk * block_size;
493 cmd.block_size = block_size;
496 if (!wilc_sdio_set_func0_csa_address(wilc, addr))
499 ret = wilc_sdio_cmd53(wilc, &cmd);
502 "Failed cmd53 [%x], bytes send...\n", addr);
514 static int wilc_sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
516 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
517 struct wilc_sdio *sdio_priv = wilc->bus_data;
520 if (addr >= 0xf0 && addr <= 0xff) {
521 struct sdio_cmd52 cmd;
527 ret = wilc_sdio_cmd52(wilc, &cmd);
530 "Failed cmd 52, read reg (%08x) ...\n", addr);
535 struct sdio_cmd53 cmd;
537 if (!wilc_sdio_set_func0_csa_address(wilc, addr))
546 cmd.buffer = (u8 *)data;
548 cmd.block_size = sdio_priv->block_size;
549 ret = wilc_sdio_cmd53(wilc, &cmd);
552 "Failed cmd53, read reg (%08x)...\n", addr);
566 static int wilc_sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
568 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
569 struct wilc_sdio *sdio_priv = wilc->bus_data;
570 u32 block_size = sdio_priv->block_size;
571 struct sdio_cmd53 cmd;
572 int nblk, nleft, ret;
577 * has to be word aligned...
591 * has to be word aligned...
605 nblk = size / block_size;
606 nleft = size % block_size;
613 cmd.block_size = block_size;
615 if (!wilc_sdio_set_func0_csa_address(wilc, addr))
618 ret = wilc_sdio_cmd53(wilc, &cmd);
621 "Failed cmd53 [%x], block read...\n", addr);
625 addr += nblk * block_size;
626 buf += nblk * block_size;
627 } /* if (nblk > 0) */
635 cmd.block_size = block_size;
638 if (!wilc_sdio_set_func0_csa_address(wilc, addr))
641 ret = wilc_sdio_cmd53(wilc, &cmd);
644 "Failed cmd53 [%x], bytes read...\n", addr);
656 /********************************************
660 ********************************************/
662 static int wilc_sdio_deinit(struct wilc *wilc)
667 static int wilc_sdio_init(struct wilc *wilc, bool resume)
669 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
670 struct wilc_sdio *sdio_priv = wilc->bus_data;
671 struct sdio_cmd52 cmd;
676 sdio_priv->irq_gpio = wilc->dev_irq_num;
679 * function 0 csa enable
686 ret = wilc_sdio_cmd52(wilc, &cmd);
688 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
693 * function 0 block size
695 if (!wilc_sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
696 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
699 sdio_priv->block_size = WILC_SDIO_BLOCK_SIZE;
709 ret = wilc_sdio_cmd52(wilc, &cmd);
712 "Fail cmd 52, set IOE register...\n");
717 * make sure func 1 is up
726 ret = wilc_sdio_cmd52(wilc, &cmd);
729 "Fail cmd 52, get IOR register...\n");
737 dev_err(&func->dev, "Fail func 1 is not ready...\n");
742 * func 1 is ready, set func 1 block size
744 if (!wilc_sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
745 dev_err(&func->dev, "Fail set func 1 block size...\n");
750 * func 1 interrupt enable
757 ret = wilc_sdio_cmd52(wilc, &cmd);
759 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
764 * make sure can read back chip id correctly
767 if (!wilc_sdio_read_reg(wilc, 0x1000, &chipid)) {
768 dev_err(&func->dev, "Fail cmd read chip id...\n");
771 dev_err(&func->dev, "chipid (%08x)\n", chipid);
772 if ((chipid & 0xfff) > 0x2a0)
773 sdio_priv->has_thrpt_enh3 = 1;
775 sdio_priv->has_thrpt_enh3 = 0;
776 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
777 sdio_priv->has_thrpt_enh3);
787 static int wilc_sdio_read_size(struct wilc *wilc, u32 *size)
790 struct sdio_cmd52 cmd;
793 * Read DMA count in words
800 wilc_sdio_cmd52(wilc, &cmd);
805 wilc_sdio_cmd52(wilc, &cmd);
806 tmp |= (cmd.data << 8);
812 static int wilc_sdio_read_int(struct wilc *wilc, u32 *int_status)
814 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
815 struct wilc_sdio *sdio_priv = wilc->bus_data;
817 struct sdio_cmd52 cmd;
819 wilc_sdio_read_size(wilc, &tmp);
824 if (!sdio_priv->irq_gpio) {
831 wilc_sdio_cmd52(wilc, &cmd);
833 if (cmd.data & BIT(0))
835 if (cmd.data & BIT(2))
837 if (cmd.data & BIT(3))
839 if (cmd.data & BIT(4))
841 if (cmd.data & BIT(5))
843 if (cmd.data & BIT(6))
845 for (i = sdio_priv->nint; i < MAX_NUM_INT; i++) {
846 if ((tmp >> (IRG_FLAGS_OFFSET + i)) & 0x1) {
848 "Unexpected interrupt (1) : tmp=%x, data=%x\n",
861 wilc_sdio_cmd52(wilc, &cmd);
862 irq_flags = cmd.data & 0x1f;
863 tmp |= ((irq_flags >> 0) << IRG_FLAGS_OFFSET);
871 static int wilc_sdio_clear_int_ext(struct wilc *wilc, u32 val)
873 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
874 struct wilc_sdio *sdio_priv = wilc->bus_data;
878 if (sdio_priv->has_thrpt_enh3) {
881 if (sdio_priv->irq_gpio) {
884 flags = val & (BIT(MAX_NUN_INT_THRPT_ENH2) - 1);
889 /* select VMM table 0 */
890 if (val & SEL_VMM_TBL0)
892 /* select VMM table 1 */
893 if (val & SEL_VMM_TBL1)
899 struct sdio_cmd52 cmd;
907 ret = wilc_sdio_cmd52(wilc, &cmd);
910 "Failed cmd52, set 0xf8 data (%d) ...\n",
917 if (sdio_priv->irq_gpio) {
918 /* has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
920 * Cannot clear multiple interrupts.
921 * Must clear each interrupt individually.
925 flags = val & (BIT(MAX_NUM_INT) - 1);
930 for (i = 0; i < sdio_priv->nint; i++) {
932 struct sdio_cmd52 cmd;
940 ret = wilc_sdio_cmd52(wilc, &cmd);
943 "Failed cmd52, set 0xf8 data (%d) ...\n",
954 for (i = sdio_priv->nint; i < MAX_NUM_INT; i++) {
957 "Unexpected interrupt cleared %d...\n",
965 /* select VMM table 0 */
966 if (val & SEL_VMM_TBL0)
968 /* select VMM table 1 */
969 if (val & SEL_VMM_TBL1)
976 struct sdio_cmd52 cmd;
983 ret = wilc_sdio_cmd52(wilc, &cmd);
986 "Failed cmd52, set 0xf6 data (%d) ...\n",
996 static int wilc_sdio_sync_ext(struct wilc *wilc, int nint)
998 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
999 struct wilc_sdio *sdio_priv = wilc->bus_data;
1002 if (nint > MAX_NUM_INT) {
1003 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
1006 if (nint > MAX_NUN_INT_THRPT_ENH2) {
1008 "Cannot support more than 5 interrupts when has_thrpt_enh2=1.\n");
1012 sdio_priv->nint = nint;
1015 * Disable power sequencer
1017 if (!wilc_sdio_read_reg(wilc, WILC_MISC, ®)) {
1018 dev_err(&func->dev, "Failed read misc reg...\n");
1023 if (!wilc_sdio_write_reg(wilc, WILC_MISC, reg)) {
1024 dev_err(&func->dev, "Failed write misc reg...\n");
1028 if (sdio_priv->irq_gpio) {
1033 * interrupt pin mux select
1035 ret = wilc_sdio_read_reg(wilc, WILC_PIN_MUX_0, ®);
1037 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1042 ret = wilc_sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
1044 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1052 ret = wilc_sdio_read_reg(wilc, WILC_INTR_ENABLE, ®);
1054 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1059 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1060 reg |= BIT((27 + i));
1061 ret = wilc_sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
1063 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1068 ret = wilc_sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1071 "Failed read reg (%08x)...\n",
1076 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1079 ret = wilc_sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1082 "Failed write reg (%08x)...\n",
1091 /* Global sdio HIF function table */
1092 static const struct wilc_hif_func wilc_hif_sdio = {
1093 .hif_init = wilc_sdio_init,
1094 .hif_deinit = wilc_sdio_deinit,
1095 .hif_read_reg = wilc_sdio_read_reg,
1096 .hif_write_reg = wilc_sdio_write_reg,
1097 .hif_block_rx = wilc_sdio_read,
1098 .hif_block_tx = wilc_sdio_write,
1099 .hif_read_int = wilc_sdio_read_int,
1100 .hif_clear_int_ext = wilc_sdio_clear_int_ext,
1101 .hif_read_size = wilc_sdio_read_size,
1102 .hif_block_tx_ext = wilc_sdio_write,
1103 .hif_block_rx_ext = wilc_sdio_read,
1104 .hif_sync_ext = wilc_sdio_sync_ext,
1105 .enable_interrupt = wilc_sdio_enable_interrupt,
1106 .disable_interrupt = wilc_sdio_disable_interrupt,
1109 static int wilc_sdio_resume(struct device *dev)
1111 struct sdio_func *func = dev_to_sdio_func(dev);
1112 struct wilc *wilc = sdio_get_drvdata(func);
1114 dev_info(dev, "sdio resume\n");
1115 sdio_release_host(func);
1117 wilc_sdio_init(wilc, true);
1119 if (wilc->suspend_event)
1120 host_wakeup_notify(wilc);
1122 chip_allow_sleep(wilc);
1127 static const struct of_device_id wilc_of_match[] = {
1128 { .compatible = "microchip,wilc1000-sdio", },
1131 MODULE_DEVICE_TABLE(of, wilc_of_match);
1133 static const struct dev_pm_ops wilc_sdio_pm_ops = {
1134 .suspend = wilc_sdio_suspend,
1135 .resume = wilc_sdio_resume,
1138 static struct sdio_driver wilc_sdio_driver = {
1139 .name = SDIO_MODALIAS,
1140 .id_table = wilc_sdio_ids,
1141 .probe = wilc_sdio_probe,
1142 .remove = wilc_sdio_remove,
1144 .pm = &wilc_sdio_pm_ops,
1145 .of_match_table = wilc_of_match,
1148 module_driver(wilc_sdio_driver,
1149 sdio_register_driver,
1150 sdio_unregister_driver);
1151 MODULE_LICENSE("GPL");