1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
7 #include <linux/mmc/sdio_func.h>
8 #include <linux/mmc/host.h>
10 #include "wilc_wfi_netdevice.h"
12 #define SDIO_MODALIAS "wilc1000_sdio"
14 #define SDIO_VENDOR_ID_WILC 0x0296
15 #define SDIO_DEVICE_ID_WILC 0x5347
17 static const struct sdio_device_id wilc_sdio_ids[] = {
18 { SDIO_DEVICE(SDIO_VENDOR_ID_WILC, SDIO_DEVICE_ID_WILC) },
21 MODULE_DEVICE_TABLE(sdio, wilc_sdio_ids);
23 #define WILC_SDIO_BLOCK_SIZE 512
29 /* Max num interrupts allowed in registers 0xf7, 0xf8 */
30 #define MAX_NUN_INT_THRPT_ENH2 (5)
34 static struct wilc_sdio g_sdio;
35 static const struct wilc_hif_func wilc_hif_sdio;
37 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data);
38 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data);
39 static int sdio_init(struct wilc *wilc, bool resume);
41 static void wilc_sdio_interrupt(struct sdio_func *func)
43 sdio_release_host(func);
44 wilc_handle_isr(sdio_get_drvdata(func));
45 sdio_claim_host(func);
48 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
50 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
54 sdio_claim_host(func);
56 func->num = cmd->function;
57 if (cmd->read_write) { /* write */
59 sdio_writeb(func, cmd->data, cmd->address, &ret);
60 data = sdio_readb(func, cmd->address, &ret);
63 sdio_writeb(func, cmd->data, cmd->address, &ret);
66 data = sdio_readb(func, cmd->address, &ret);
70 sdio_release_host(func);
73 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
77 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
79 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
82 sdio_claim_host(func);
84 func->num = cmd->function;
85 func->cur_blksize = cmd->block_size;
87 size = cmd->count * cmd->block_size;
91 if (cmd->read_write) { /* write */
92 ret = sdio_memcpy_toio(func, cmd->address,
93 (void *)cmd->buffer, size);
95 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
99 sdio_release_host(func);
102 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
107 static int linux_sdio_probe(struct sdio_func *func,
108 const struct sdio_device_id *id)
112 struct gpio_desc *gpio = NULL;
114 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
115 gpio = gpiod_get(&func->dev, "irq", GPIOD_IN);
117 /* get the GPIO descriptor from hardcode GPIO number */
118 gpio = gpio_to_desc(GPIO_NUM);
120 dev_err(&func->dev, "failed to get irq gpio\n");
124 dev_dbg(&func->dev, "Initializing netdev\n");
125 ret = wilc_netdev_init(&wilc, &func->dev, HIF_SDIO, &wilc_hif_sdio);
127 dev_err(&func->dev, "Couldn't initialize netdev\n");
130 sdio_set_drvdata(func, wilc);
131 wilc->dev = &func->dev;
132 wilc->gpio_irq = gpio;
134 dev_info(&func->dev, "Driver Initializing success\n");
138 static void linux_sdio_remove(struct sdio_func *func)
140 struct wilc *wilc = sdio_get_drvdata(func);
142 /* free the GPIO in module remove */
144 gpiod_put(wilc->gpio_irq);
145 wilc_netdev_cleanup(wilc);
148 static int sdio_reset(struct wilc *wilc)
150 struct sdio_cmd52 cmd;
152 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
159 ret = wilc_sdio_cmd52(wilc, &cmd);
161 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
167 static int wilc_sdio_suspend(struct device *dev)
169 struct sdio_func *func = dev_to_sdio_func(dev);
170 struct wilc *wilc = sdio_get_drvdata(func);
173 dev_info(dev, "sdio suspend\n");
176 if (!wilc->suspend_event) {
177 wilc_chip_sleep_manually(wilc);
179 host_sleep_notify(wilc);
180 chip_allow_sleep(wilc);
183 ret = sdio_reset(wilc);
185 dev_err(&func->dev, "Fail reset sdio\n");
188 sdio_claim_host(func);
193 static int wilc_sdio_resume(struct device *dev)
195 struct sdio_func *func = dev_to_sdio_func(dev);
196 struct wilc *wilc = sdio_get_drvdata(func);
198 dev_info(dev, "sdio resume\n");
199 sdio_release_host(func);
201 sdio_init(wilc, true);
203 if (wilc->suspend_event)
204 host_wakeup_notify(wilc);
206 chip_allow_sleep(wilc);
211 static const struct of_device_id wilc_of_match[] = {
212 { .compatible = "microchip,wilc1000-sdio", },
215 MODULE_DEVICE_TABLE(of, wilc_of_match);
217 static const struct dev_pm_ops wilc_sdio_pm_ops = {
218 .suspend = wilc_sdio_suspend,
219 .resume = wilc_sdio_resume,
222 static struct sdio_driver wilc_sdio_driver = {
223 .name = SDIO_MODALIAS,
224 .id_table = wilc_sdio_ids,
225 .probe = linux_sdio_probe,
226 .remove = linux_sdio_remove,
228 .pm = &wilc_sdio_pm_ops,
229 .of_match_table = wilc_of_match,
232 module_driver(wilc_sdio_driver,
233 sdio_register_driver,
234 sdio_unregister_driver);
235 MODULE_LICENSE("GPL");
237 static int wilc_sdio_enable_interrupt(struct wilc *dev)
239 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
242 sdio_claim_host(func);
243 ret = sdio_claim_irq(func, wilc_sdio_interrupt);
244 sdio_release_host(func);
247 dev_err(&func->dev, "can't claim sdio_irq, err(%d)\n", ret);
253 static void wilc_sdio_disable_interrupt(struct wilc *dev)
255 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
258 sdio_claim_host(func);
259 ret = sdio_release_irq(func);
261 dev_err(&func->dev, "can't release sdio_irq, err(%d)\n", ret);
262 sdio_release_host(func);
265 /********************************************
269 ********************************************/
271 static int sdio_set_func0_csa_address(struct wilc *wilc, u32 adr)
273 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
274 struct sdio_cmd52 cmd;
285 ret = wilc_sdio_cmd52(wilc, &cmd);
287 dev_err(&func->dev, "Failed cmd52, set 0x10c data...\n");
292 cmd.data = (u8)(adr >> 8);
293 ret = wilc_sdio_cmd52(wilc, &cmd);
295 dev_err(&func->dev, "Failed cmd52, set 0x10d data...\n");
300 cmd.data = (u8)(adr >> 16);
301 ret = wilc_sdio_cmd52(wilc, &cmd);
303 dev_err(&func->dev, "Failed cmd52, set 0x10e data...\n");
312 static int sdio_set_func0_block_size(struct wilc *wilc, u32 block_size)
314 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
315 struct sdio_cmd52 cmd;
322 cmd.data = (u8)block_size;
323 ret = wilc_sdio_cmd52(wilc, &cmd);
325 dev_err(&func->dev, "Failed cmd52, set 0x10 data...\n");
330 cmd.data = (u8)(block_size >> 8);
331 ret = wilc_sdio_cmd52(wilc, &cmd);
333 dev_err(&func->dev, "Failed cmd52, set 0x11 data...\n");
342 /********************************************
346 ********************************************/
348 static int sdio_set_func1_block_size(struct wilc *wilc, u32 block_size)
350 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
351 struct sdio_cmd52 cmd;
358 cmd.data = (u8)block_size;
359 ret = wilc_sdio_cmd52(wilc, &cmd);
361 dev_err(&func->dev, "Failed cmd52, set 0x110 data...\n");
365 cmd.data = (u8)(block_size >> 8);
366 ret = wilc_sdio_cmd52(wilc, &cmd);
368 dev_err(&func->dev, "Failed cmd52, set 0x111 data...\n");
377 /********************************************
381 ********************************************/
382 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
384 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
389 if (addr >= 0xf0 && addr <= 0xff) {
390 struct sdio_cmd52 cmd;
397 ret = wilc_sdio_cmd52(wilc, &cmd);
400 "Failed cmd 52, read reg (%08x) ...\n", addr);
404 struct sdio_cmd53 cmd;
407 * set the AHB address
409 if (!sdio_set_func0_csa_address(wilc, addr))
418 cmd.buffer = (u8 *)&data;
419 cmd.block_size = g_sdio.block_size;
420 ret = wilc_sdio_cmd53(wilc, &cmd);
423 "Failed cmd53, write reg (%08x)...\n", addr);
435 static int sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
437 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
438 u32 block_size = g_sdio.block_size;
439 struct sdio_cmd53 cmd;
440 int nblk, nleft, ret;
445 * has to be word aligned...
459 * has to be word aligned...
473 nblk = size / block_size;
474 nleft = size % block_size;
481 cmd.block_size = block_size;
483 if (!sdio_set_func0_csa_address(wilc, addr))
486 ret = wilc_sdio_cmd53(wilc, &cmd);
489 "Failed cmd53 [%x], block send...\n", addr);
493 addr += nblk * block_size;
494 buf += nblk * block_size;
503 cmd.block_size = block_size;
506 if (!sdio_set_func0_csa_address(wilc, addr))
509 ret = wilc_sdio_cmd53(wilc, &cmd);
512 "Failed cmd53 [%x], bytes send...\n", addr);
524 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
526 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
529 if (addr >= 0xf0 && addr <= 0xff) {
530 struct sdio_cmd52 cmd;
536 ret = wilc_sdio_cmd52(wilc, &cmd);
539 "Failed cmd 52, read reg (%08x) ...\n", addr);
544 struct sdio_cmd53 cmd;
546 if (!sdio_set_func0_csa_address(wilc, addr))
555 cmd.buffer = (u8 *)data;
557 cmd.block_size = g_sdio.block_size;
558 ret = wilc_sdio_cmd53(wilc, &cmd);
561 "Failed cmd53, read reg (%08x)...\n", addr);
575 static int sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
577 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
578 u32 block_size = g_sdio.block_size;
579 struct sdio_cmd53 cmd;
580 int nblk, nleft, ret;
585 * has to be word aligned...
599 * has to be word aligned...
613 nblk = size / block_size;
614 nleft = size % block_size;
621 cmd.block_size = block_size;
623 if (!sdio_set_func0_csa_address(wilc, addr))
626 ret = wilc_sdio_cmd53(wilc, &cmd);
629 "Failed cmd53 [%x], block read...\n", addr);
633 addr += nblk * block_size;
634 buf += nblk * block_size;
635 } /* if (nblk > 0) */
643 cmd.block_size = block_size;
646 if (!sdio_set_func0_csa_address(wilc, addr))
649 ret = wilc_sdio_cmd53(wilc, &cmd);
652 "Failed cmd53 [%x], bytes read...\n", addr);
664 /********************************************
668 ********************************************/
670 static int sdio_deinit(struct wilc *wilc)
675 static int sdio_init(struct wilc *wilc, bool resume)
677 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
678 struct sdio_cmd52 cmd;
683 memset(&g_sdio, 0, sizeof(struct wilc_sdio));
684 g_sdio.irq_gpio = wilc->dev_irq_num;
688 * function 0 csa enable
695 ret = wilc_sdio_cmd52(wilc, &cmd);
697 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
702 * function 0 block size
704 if (!sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
705 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
708 g_sdio.block_size = WILC_SDIO_BLOCK_SIZE;
718 ret = wilc_sdio_cmd52(wilc, &cmd);
721 "Fail cmd 52, set IOE register...\n");
726 * make sure func 1 is up
735 ret = wilc_sdio_cmd52(wilc, &cmd);
738 "Fail cmd 52, get IOR register...\n");
746 dev_err(&func->dev, "Fail func 1 is not ready...\n");
751 * func 1 is ready, set func 1 block size
753 if (!sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
754 dev_err(&func->dev, "Fail set func 1 block size...\n");
759 * func 1 interrupt enable
766 ret = wilc_sdio_cmd52(wilc, &cmd);
768 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
773 * make sure can read back chip id correctly
776 if (!sdio_read_reg(wilc, 0x1000, &chipid)) {
777 dev_err(&func->dev, "Fail cmd read chip id...\n");
780 dev_err(&func->dev, "chipid (%08x)\n", chipid);
781 if ((chipid & 0xfff) > 0x2a0)
782 g_sdio.has_thrpt_enh3 = 1;
784 g_sdio.has_thrpt_enh3 = 0;
785 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
786 g_sdio.has_thrpt_enh3);
796 static int sdio_read_size(struct wilc *wilc, u32 *size)
799 struct sdio_cmd52 cmd;
802 * Read DMA count in words
809 wilc_sdio_cmd52(wilc, &cmd);
814 wilc_sdio_cmd52(wilc, &cmd);
815 tmp |= (cmd.data << 8);
821 static int sdio_read_int(struct wilc *wilc, u32 *int_status)
823 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
825 struct sdio_cmd52 cmd;
827 sdio_read_size(wilc, &tmp);
832 if (!g_sdio.irq_gpio) {
839 wilc_sdio_cmd52(wilc, &cmd);
841 if (cmd.data & BIT(0))
843 if (cmd.data & BIT(2))
845 if (cmd.data & BIT(3))
847 if (cmd.data & BIT(4))
849 if (cmd.data & BIT(5))
851 if (cmd.data & BIT(6))
853 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
854 if ((tmp >> (IRG_FLAGS_OFFSET + i)) & 0x1) {
856 "Unexpected interrupt (1) : tmp=%x, data=%x\n",
869 wilc_sdio_cmd52(wilc, &cmd);
870 irq_flags = cmd.data & 0x1f;
871 tmp |= ((irq_flags >> 0) << IRG_FLAGS_OFFSET);
879 static int sdio_clear_int_ext(struct wilc *wilc, u32 val)
881 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
885 if (g_sdio.has_thrpt_enh3) {
888 if (g_sdio.irq_gpio) {
891 flags = val & (BIT(MAX_NUN_INT_THRPT_ENH2) - 1);
896 /* select VMM table 0 */
897 if (val & SEL_VMM_TBL0)
899 /* select VMM table 1 */
900 if (val & SEL_VMM_TBL1)
906 struct sdio_cmd52 cmd;
914 ret = wilc_sdio_cmd52(wilc, &cmd);
917 "Failed cmd52, set 0xf8 data (%d) ...\n",
924 if (g_sdio.irq_gpio) {
925 /* has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
927 * Cannot clear multiple interrupts.
928 * Must clear each interrupt individually.
932 flags = val & (BIT(MAX_NUM_INT) - 1);
937 for (i = 0; i < g_sdio.nint; i++) {
939 struct sdio_cmd52 cmd;
947 ret = wilc_sdio_cmd52(wilc, &cmd);
950 "Failed cmd52, set 0xf8 data (%d) ...\n",
961 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
964 "Unexpected interrupt cleared %d...\n",
972 /* select VMM table 0 */
973 if (val & SEL_VMM_TBL0)
975 /* select VMM table 1 */
976 if (val & SEL_VMM_TBL1)
983 struct sdio_cmd52 cmd;
990 ret = wilc_sdio_cmd52(wilc, &cmd);
993 "Failed cmd52, set 0xf6 data (%d) ...\n",
1003 static int sdio_sync_ext(struct wilc *wilc, int nint)
1005 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
1008 if (nint > MAX_NUM_INT) {
1009 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
1012 if (nint > MAX_NUN_INT_THRPT_ENH2) {
1014 "Cannot support more than 5 interrupts when has_thrpt_enh2=1.\n");
1021 * Disable power sequencer
1023 if (!sdio_read_reg(wilc, WILC_MISC, ®)) {
1024 dev_err(&func->dev, "Failed read misc reg...\n");
1029 if (!sdio_write_reg(wilc, WILC_MISC, reg)) {
1030 dev_err(&func->dev, "Failed write misc reg...\n");
1034 if (g_sdio.irq_gpio) {
1039 * interrupt pin mux select
1041 ret = sdio_read_reg(wilc, WILC_PIN_MUX_0, ®);
1043 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1048 ret = sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
1050 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1058 ret = sdio_read_reg(wilc, WILC_INTR_ENABLE, ®);
1060 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1065 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1066 reg |= BIT((27 + i));
1067 ret = sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
1069 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1074 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1077 "Failed read reg (%08x)...\n",
1082 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1085 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1088 "Failed write reg (%08x)...\n",
1097 /* Global sdio HIF function table */
1098 static const struct wilc_hif_func wilc_hif_sdio = {
1099 .hif_init = sdio_init,
1100 .hif_deinit = sdio_deinit,
1101 .hif_read_reg = sdio_read_reg,
1102 .hif_write_reg = sdio_write_reg,
1103 .hif_block_rx = sdio_read,
1104 .hif_block_tx = sdio_write,
1105 .hif_read_int = sdio_read_int,
1106 .hif_clear_int_ext = sdio_clear_int_ext,
1107 .hif_read_size = sdio_read_size,
1108 .hif_block_tx_ext = sdio_write,
1109 .hif_block_rx_ext = sdio_read,
1110 .hif_sync_ext = sdio_sync_ext,
1111 .enable_interrupt = wilc_sdio_enable_interrupt,
1112 .disable_interrupt = wilc_sdio_disable_interrupt,