2 * Copyright (c) Atmel Corporation. All rights reserved.
4 * Module Name: wilc_sdio.c
7 #include <linux/string.h>
8 #include "wilc_wlan_if.h"
10 #include "wilc_wfi_netdevice.h"
11 #include <linux/mmc/sdio_func.h>
12 #include <linux/mmc/card.h>
13 #include <linux/mmc/sdio_ids.h>
14 #include <linux/mmc/sdio.h>
15 #include <linux/mmc/host.h>
16 #include <linux/of_gpio.h>
18 #define SDIO_MODALIAS "wilc1000_sdio"
20 #define SDIO_VENDOR_ID_WILC 0x0296
21 #define SDIO_DEVICE_ID_WILC 0x5347
23 static const struct sdio_device_id wilc_sdio_ids[] = {
24 { SDIO_DEVICE(SDIO_VENDOR_ID_WILC, SDIO_DEVICE_ID_WILC) },
28 #define WILC_SDIO_BLOCK_SIZE 512
34 #define MAX_NUN_INT_THRPT_ENH2 (5) /* Max num interrupts allowed in registers 0xf7, 0xf8 */
38 static struct wilc_sdio g_sdio;
39 static const struct wilc_hif_func wilc_hif_sdio;
41 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data);
42 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data);
43 static int sdio_init(struct wilc *wilc, bool resume);
45 static void wilc_sdio_interrupt(struct sdio_func *func)
47 sdio_release_host(func);
48 wilc_handle_isr(sdio_get_drvdata(func));
49 sdio_claim_host(func);
52 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
54 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
58 sdio_claim_host(func);
60 func->num = cmd->function;
61 if (cmd->read_write) { /* write */
63 sdio_writeb(func, cmd->data, cmd->address, &ret);
64 data = sdio_readb(func, cmd->address, &ret);
67 sdio_writeb(func, cmd->data, cmd->address, &ret);
70 data = sdio_readb(func, cmd->address, &ret);
74 sdio_release_host(func);
77 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
81 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
83 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
86 sdio_claim_host(func);
88 func->num = cmd->function;
89 func->cur_blksize = cmd->block_size;
91 size = cmd->count * cmd->block_size;
95 if (cmd->read_write) { /* write */
96 ret = sdio_memcpy_toio(func, cmd->address,
97 (void *)cmd->buffer, size);
99 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
103 sdio_release_host(func);
106 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
111 static int linux_sdio_probe(struct sdio_func *func,
112 const struct sdio_device_id *id)
118 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
119 gpio = of_get_gpio(func->dev.of_node, 0);
124 dev_dbg(&func->dev, "Initializing netdev\n");
125 ret = wilc_netdev_init(&wilc, &func->dev, HIF_SDIO, gpio,
128 dev_err(&func->dev, "Couldn't initialize netdev\n");
131 sdio_set_drvdata(func, wilc);
132 wilc->dev = &func->dev;
134 dev_info(&func->dev, "Driver Initializing success\n");
138 static void linux_sdio_remove(struct sdio_func *func)
140 wilc_netdev_cleanup(sdio_get_drvdata(func));
143 static int sdio_reset(struct wilc *wilc)
145 struct sdio_cmd52 cmd;
147 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
154 ret = wilc_sdio_cmd52(wilc, &cmd);
156 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
162 static int wilc_sdio_suspend(struct device *dev)
164 struct sdio_func *func = dev_to_sdio_func(dev);
165 struct wilc *wilc = sdio_get_drvdata(func);
168 dev_info(dev, "sdio suspend\n");
171 if (!wilc->suspend_event) {
172 wilc_chip_sleep_manually(wilc);
174 host_sleep_notify(wilc);
175 chip_allow_sleep(wilc);
178 ret = sdio_reset(wilc);
180 dev_err(&func->dev, "Fail reset sdio\n");
183 sdio_claim_host(func);
188 static int wilc_sdio_resume(struct device *dev)
190 struct sdio_func *func = dev_to_sdio_func(dev);
191 struct wilc *wilc = sdio_get_drvdata(func);
193 dev_info(dev, "sdio resume\n");
194 sdio_release_host(func);
196 sdio_init(wilc, true);
198 if (wilc->suspend_event)
199 host_wakeup_notify(wilc);
201 chip_allow_sleep(wilc);
206 static const struct dev_pm_ops wilc_sdio_pm_ops = {
207 .suspend = wilc_sdio_suspend,
208 .resume = wilc_sdio_resume,
211 static struct sdio_driver wilc1000_sdio_driver = {
212 .name = SDIO_MODALIAS,
213 .id_table = wilc_sdio_ids,
214 .probe = linux_sdio_probe,
215 .remove = linux_sdio_remove,
217 .pm = &wilc_sdio_pm_ops,
220 module_driver(wilc1000_sdio_driver,
221 sdio_register_driver,
222 sdio_unregister_driver);
223 MODULE_LICENSE("GPL");
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 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 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 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 sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
372 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
375 data = cpu_to_le32(data);
377 if ((addr >= 0xf0) && (addr <= 0xff)) {
378 struct sdio_cmd52 cmd;
385 ret = wilc_sdio_cmd52(wilc, &cmd);
388 "Failed cmd 52, read reg (%08x) ...\n", addr);
392 struct sdio_cmd53 cmd;
395 * set the AHB address
397 if (!sdio_set_func0_csa_address(wilc, addr))
406 cmd.buffer = (u8 *)&data;
407 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
408 ret = wilc_sdio_cmd53(wilc, &cmd);
411 "Failed cmd53, write reg (%08x)...\n", addr);
423 static int sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
425 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
426 u32 block_size = g_sdio.block_size;
427 struct sdio_cmd53 cmd;
428 int nblk, nleft, ret;
433 * has to be word aligned...
447 * has to be word aligned...
461 nblk = size / block_size;
462 nleft = size % block_size;
469 cmd.block_size = block_size;
471 if (!sdio_set_func0_csa_address(wilc, addr))
474 ret = wilc_sdio_cmd53(wilc, &cmd);
477 "Failed cmd53 [%x], block send...\n", addr);
481 addr += nblk * block_size;
482 buf += nblk * block_size;
491 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
494 if (!sdio_set_func0_csa_address(wilc, addr))
497 ret = wilc_sdio_cmd53(wilc, &cmd);
500 "Failed cmd53 [%x], bytes send...\n", addr);
512 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
514 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
517 if ((addr >= 0xf0) && (addr <= 0xff)) {
518 struct sdio_cmd52 cmd;
524 ret = wilc_sdio_cmd52(wilc, &cmd);
527 "Failed cmd 52, read reg (%08x) ...\n", addr);
532 struct sdio_cmd53 cmd;
534 if (!sdio_set_func0_csa_address(wilc, addr))
543 cmd.buffer = (u8 *)data;
545 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
546 ret = wilc_sdio_cmd53(wilc, &cmd);
549 "Failed cmd53, read reg (%08x)...\n", addr);
554 *data = cpu_to_le32(*data);
563 static int sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
565 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
566 u32 block_size = g_sdio.block_size;
567 struct sdio_cmd53 cmd;
568 int nblk, nleft, ret;
573 * has to be word aligned...
587 * has to be word aligned...
601 nblk = size / block_size;
602 nleft = size % block_size;
609 cmd.block_size = block_size;
611 if (!sdio_set_func0_csa_address(wilc, addr))
614 ret = wilc_sdio_cmd53(wilc, &cmd);
617 "Failed cmd53 [%x], block read...\n", addr);
621 addr += nblk * block_size;
622 buf += nblk * block_size;
623 } /* if (nblk > 0) */
631 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
634 if (!sdio_set_func0_csa_address(wilc, addr))
637 ret = wilc_sdio_cmd53(wilc, &cmd);
640 "Failed cmd53 [%x], bytes read...\n", addr);
652 /********************************************
656 ********************************************/
658 static int sdio_deinit(struct wilc *wilc)
663 static int sdio_init(struct wilc *wilc, bool resume)
665 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
666 struct sdio_cmd52 cmd;
671 memset(&g_sdio, 0, sizeof(struct wilc_sdio));
672 g_sdio.irq_gpio = wilc->dev_irq_num;
676 * function 0 csa enable
683 ret = wilc_sdio_cmd52(wilc, &cmd);
685 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
690 * function 0 block size
692 if (!sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
693 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
696 g_sdio.block_size = WILC_SDIO_BLOCK_SIZE;
706 ret = wilc_sdio_cmd52(wilc, &cmd);
709 "Fail cmd 52, set IOE register...\n");
714 * make sure func 1 is up
723 ret = wilc_sdio_cmd52(wilc, &cmd);
726 "Fail cmd 52, get IOR register...\n");
734 dev_err(&func->dev, "Fail func 1 is not ready...\n");
739 * func 1 is ready, set func 1 block size
741 if (!sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
742 dev_err(&func->dev, "Fail set func 1 block size...\n");
747 * func 1 interrupt enable
754 ret = wilc_sdio_cmd52(wilc, &cmd);
756 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
761 * make sure can read back chip id correctly
764 if (!sdio_read_reg(wilc, 0x1000, &chipid)) {
765 dev_err(&func->dev, "Fail cmd read chip id...\n");
768 dev_err(&func->dev, "chipid (%08x)\n", chipid);
769 if ((chipid & 0xfff) > 0x2a0)
770 g_sdio.has_thrpt_enh3 = 1;
772 g_sdio.has_thrpt_enh3 = 0;
773 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
774 g_sdio.has_thrpt_enh3);
784 static int sdio_read_size(struct wilc *wilc, u32 *size)
787 struct sdio_cmd52 cmd;
790 * Read DMA count in words
797 wilc_sdio_cmd52(wilc, &cmd);
800 /* cmd.read_write = 0; */
801 /* cmd.function = 0; */
805 wilc_sdio_cmd52(wilc, &cmd);
806 tmp |= (cmd.data << 8);
812 static int sdio_read_int(struct wilc *wilc, u32 *int_status)
814 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
816 struct sdio_cmd52 cmd;
818 sdio_read_size(wilc, &tmp);
823 if (!g_sdio.irq_gpio) {
830 wilc_sdio_cmd52(wilc, &cmd);
832 if (cmd.data & BIT(0))
834 if (cmd.data & BIT(2))
836 if (cmd.data & BIT(3))
838 if (cmd.data & BIT(4))
840 if (cmd.data & BIT(5))
842 if (cmd.data & BIT(6))
844 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
845 if ((tmp >> (IRG_FLAGS_OFFSET + i)) & 0x1) {
847 "Unexpected interrupt (1) : tmp=%x, data=%x\n",
860 wilc_sdio_cmd52(wilc, &cmd);
861 irq_flags = cmd.data & 0x1f;
862 tmp |= ((irq_flags >> 0) << IRG_FLAGS_OFFSET);
870 static int sdio_clear_int_ext(struct wilc *wilc, u32 val)
872 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
875 if (g_sdio.has_thrpt_enh3) {
878 if (g_sdio.irq_gpio) {
881 flags = val & (BIT(MAX_NUN_INT_THRPT_ENH2) - 1);
886 /* select VMM table 0 */
887 if ((val & SEL_VMM_TBL0) == SEL_VMM_TBL0)
889 /* select VMM table 1 */
890 if ((val & SEL_VMM_TBL1) == SEL_VMM_TBL1)
893 if ((val & EN_VMM) == EN_VMM)
896 struct sdio_cmd52 cmd;
904 ret = wilc_sdio_cmd52(wilc, &cmd);
907 "Failed cmd52, set 0xf8 data (%d) ...\n",
913 if (g_sdio.irq_gpio) {
914 /* see below. has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
915 /* Cannot clear multiple interrupts. Must clear each interrupt individually */
918 flags = val & (BIT(MAX_NUM_INT) - 1);
923 for (i = 0; i < g_sdio.nint; i++) {
925 struct sdio_cmd52 cmd;
933 ret = wilc_sdio_cmd52(wilc, &cmd);
936 "Failed cmd52, set 0xf8 data (%d) ...\n",
947 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
950 "Unexpected interrupt cleared %d...\n",
961 /* select VMM table 0 */
962 if ((val & SEL_VMM_TBL0) == SEL_VMM_TBL0)
964 /* select VMM table 1 */
965 if ((val & SEL_VMM_TBL1) == SEL_VMM_TBL1)
968 if ((val & EN_VMM) == EN_VMM)
972 struct sdio_cmd52 cmd;
979 ret = wilc_sdio_cmd52(wilc, &cmd);
982 "Failed cmd52, set 0xf6 data (%d) ...\n",
995 static int sdio_sync_ext(struct wilc *wilc, int nint)
997 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
1000 if (nint > MAX_NUM_INT) {
1001 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
1004 if (nint > MAX_NUN_INT_THRPT_ENH2) {
1006 "Cannot support more than 5 interrupts when has_thrpt_enh2=1.\n");
1013 * Disable power sequencer
1015 if (!sdio_read_reg(wilc, WILC_MISC, ®)) {
1016 dev_err(&func->dev, "Failed read misc reg...\n");
1021 if (!sdio_write_reg(wilc, WILC_MISC, reg)) {
1022 dev_err(&func->dev, "Failed write misc reg...\n");
1026 if (g_sdio.irq_gpio) {
1031 * interrupt pin mux select
1033 ret = sdio_read_reg(wilc, WILC_PIN_MUX_0, ®);
1035 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1040 ret = sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
1042 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1050 ret = sdio_read_reg(wilc, WILC_INTR_ENABLE, ®);
1052 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1057 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1058 reg |= BIT((27 + i));
1059 ret = sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
1061 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1066 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1069 "Failed read reg (%08x)...\n",
1074 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1077 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1080 "Failed write reg (%08x)...\n",
1089 /********************************************
1091 * Global sdio HIF function table
1093 ********************************************/
1095 static const struct wilc_hif_func wilc_hif_sdio = {
1096 .hif_init = sdio_init,
1097 .hif_deinit = sdio_deinit,
1098 .hif_read_reg = sdio_read_reg,
1099 .hif_write_reg = sdio_write_reg,
1100 .hif_block_rx = sdio_read,
1101 .hif_block_tx = sdio_write,
1102 .hif_read_int = sdio_read_int,
1103 .hif_clear_int_ext = sdio_clear_int_ext,
1104 .hif_read_size = sdio_read_size,
1105 .hif_block_tx_ext = sdio_write,
1106 .hif_block_rx_ext = sdio_read,
1107 .hif_sync_ext = sdio_sync_ext,
1108 .enable_interrupt = wilc_sdio_enable_interrupt,
1109 .disable_interrupt = wilc_sdio_disable_interrupt,