1 /* ////////////////////////////////////////////////////////////////////////// */
3 /* Copyright (c) Atmel Corporation. All rights reserved. */
5 /* Module Name: wilc_sdio.c */
8 /* //////////////////////////////////////////////////////////////////////////// */
10 #include <linux/string.h>
11 #include "wilc_wlan_if.h"
12 #include "wilc_wlan.h"
13 #include "wilc_wfi_netdevice.h"
14 #include <linux/mmc/sdio_func.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/sdio_ids.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/host.h>
19 #include <linux/of_gpio.h>
21 #define SDIO_MODALIAS "wilc1000_sdio"
23 #define SDIO_VENDOR_ID_WILC 0x0296
24 #define SDIO_DEVICE_ID_WILC 0x5347
26 static const struct sdio_device_id wilc_sdio_ids[] = {
27 { SDIO_DEVICE(SDIO_VENDOR_ID_WILC, SDIO_DEVICE_ID_WILC) },
31 #define WILC_SDIO_BLOCK_SIZE 512
37 #define MAX_NUN_INT_THRPT_ENH2 (5) /* Max num interrupts allowed in registers 0xf7, 0xf8 */
41 static struct wilc_sdio g_sdio;
42 static const struct wilc_hif_func wilc_hif_sdio;
44 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data);
45 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data);
46 static int sdio_init(struct wilc *wilc, bool resume);
48 static void wilc_sdio_interrupt(struct sdio_func *func)
50 sdio_release_host(func);
51 wilc_handle_isr(sdio_get_drvdata(func));
52 sdio_claim_host(func);
55 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
57 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
61 sdio_claim_host(func);
63 func->num = cmd->function;
64 if (cmd->read_write) { /* write */
66 sdio_writeb(func, cmd->data, cmd->address, &ret);
67 data = sdio_readb(func, cmd->address, &ret);
70 sdio_writeb(func, cmd->data, cmd->address, &ret);
73 data = sdio_readb(func, cmd->address, &ret);
77 sdio_release_host(func);
80 dev_err(&func->dev, "wilc_sdio_cmd52..failed, err(%d)\n", ret);
85 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
87 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
90 sdio_claim_host(func);
92 func->num = cmd->function;
93 func->cur_blksize = cmd->block_size;
95 size = cmd->count * cmd->block_size;
99 if (cmd->read_write) { /* write */
100 ret = sdio_memcpy_toio(func, cmd->address,
101 (void *)cmd->buffer, size);
103 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
107 sdio_release_host(func);
110 dev_err(&func->dev, "wilc_sdio_cmd53..failed, err(%d)\n", ret);
115 static int linux_sdio_probe(struct sdio_func *func,
116 const struct sdio_device_id *id)
122 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
123 gpio = of_get_gpio(func->dev.of_node, 0);
128 dev_dbg(&func->dev, "Initializing netdev\n");
129 ret = wilc_netdev_init(&wilc, &func->dev, HIF_SDIO, gpio,
132 dev_err(&func->dev, "Couldn't initialize netdev\n");
135 sdio_set_drvdata(func, wilc);
136 wilc->dev = &func->dev;
138 dev_info(&func->dev, "Driver Initializing success\n");
142 static void linux_sdio_remove(struct sdio_func *func)
144 wilc_netdev_cleanup(sdio_get_drvdata(func));
147 static int sdio_reset(struct wilc *wilc)
149 struct sdio_cmd52 cmd;
151 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
158 ret = wilc_sdio_cmd52(wilc, &cmd);
160 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
166 static int wilc_sdio_suspend(struct device *dev)
168 struct sdio_func *func = dev_to_sdio_func(dev);
169 struct wilc *wilc = sdio_get_drvdata(func);
172 dev_info(dev, "sdio suspend\n");
175 if (!wilc->suspend_event) {
176 wilc_chip_sleep_manually(wilc);
178 host_sleep_notify(wilc);
179 chip_allow_sleep(wilc);
182 ret = sdio_reset(wilc);
184 dev_err(&func->dev, "Fail reset sdio\n");
187 sdio_claim_host(func);
192 static int wilc_sdio_resume(struct device *dev)
194 struct sdio_func *func = dev_to_sdio_func(dev);
195 struct wilc *wilc = sdio_get_drvdata(func);
197 dev_info(dev, "sdio resume\n");
198 sdio_release_host(func);
200 sdio_init(wilc, true);
202 if (wilc->suspend_event)
203 host_wakeup_notify(wilc);
205 chip_allow_sleep(wilc);
210 static const struct dev_pm_ops wilc_sdio_pm_ops = {
211 .suspend = wilc_sdio_suspend,
212 .resume = wilc_sdio_resume,
215 static struct sdio_driver wilc1000_sdio_driver = {
216 .name = SDIO_MODALIAS,
217 .id_table = wilc_sdio_ids,
218 .probe = linux_sdio_probe,
219 .remove = linux_sdio_remove,
221 .pm = &wilc_sdio_pm_ops,
224 module_driver(wilc1000_sdio_driver,
225 sdio_register_driver,
226 sdio_unregister_driver);
227 MODULE_LICENSE("GPL");
229 static int wilc_sdio_enable_interrupt(struct wilc *dev)
231 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
234 sdio_claim_host(func);
235 ret = sdio_claim_irq(func, wilc_sdio_interrupt);
236 sdio_release_host(func);
239 dev_err(&func->dev, "can't claim sdio_irq, err(%d)\n", ret);
245 static void wilc_sdio_disable_interrupt(struct wilc *dev)
247 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
250 dev_dbg(&func->dev, "wilc_sdio_disable_interrupt IN\n");
252 sdio_claim_host(func);
253 ret = sdio_release_irq(func);
255 dev_err(&func->dev, "can't release sdio_irq, err(%d)\n", ret);
256 sdio_release_host(func);
258 dev_info(&func->dev, "wilc_sdio_disable_interrupt OUT\n");
261 /********************************************
265 ********************************************/
267 static int sdio_set_func0_csa_address(struct wilc *wilc, u32 adr)
269 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
270 struct sdio_cmd52 cmd;
281 ret = wilc_sdio_cmd52(wilc, &cmd);
283 dev_err(&func->dev, "Failed cmd52, set 0x10c data...\n");
288 cmd.data = (u8)(adr >> 8);
289 ret = wilc_sdio_cmd52(wilc, &cmd);
291 dev_err(&func->dev, "Failed cmd52, set 0x10d data...\n");
296 cmd.data = (u8)(adr >> 16);
297 ret = wilc_sdio_cmd52(wilc, &cmd);
299 dev_err(&func->dev, "Failed cmd52, set 0x10e data...\n");
308 static int sdio_set_func0_block_size(struct wilc *wilc, u32 block_size)
310 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
311 struct sdio_cmd52 cmd;
318 cmd.data = (u8)block_size;
319 ret = wilc_sdio_cmd52(wilc, &cmd);
321 dev_err(&func->dev, "Failed cmd52, set 0x10 data...\n");
326 cmd.data = (u8)(block_size >> 8);
327 ret = wilc_sdio_cmd52(wilc, &cmd);
329 dev_err(&func->dev, "Failed cmd52, set 0x11 data...\n");
338 /********************************************
342 ********************************************/
344 static int sdio_set_func1_block_size(struct wilc *wilc, u32 block_size)
346 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
347 struct sdio_cmd52 cmd;
354 cmd.data = (u8)block_size;
355 ret = wilc_sdio_cmd52(wilc, &cmd);
357 dev_err(&func->dev, "Failed cmd52, set 0x110 data...\n");
361 cmd.data = (u8)(block_size >> 8);
362 ret = wilc_sdio_cmd52(wilc, &cmd);
364 dev_err(&func->dev, "Failed cmd52, set 0x111 data...\n");
373 /********************************************
377 ********************************************/
378 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
380 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
383 data = cpu_to_le32(data);
385 if ((addr >= 0xf0) && (addr <= 0xff)) {
386 struct sdio_cmd52 cmd;
393 ret = wilc_sdio_cmd52(wilc, &cmd);
396 "Failed cmd 52, read reg (%08x) ...\n", addr);
400 struct sdio_cmd53 cmd;
403 * set the AHB address
405 if (!sdio_set_func0_csa_address(wilc, addr))
414 cmd.buffer = (u8 *)&data;
415 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
416 ret = wilc_sdio_cmd53(wilc, &cmd);
419 "Failed cmd53, write reg (%08x)...\n", addr);
431 static int sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
433 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
434 u32 block_size = g_sdio.block_size;
435 struct sdio_cmd53 cmd;
436 int nblk, nleft, ret;
441 * has to be word aligned...
455 * has to be word aligned...
469 nblk = size / block_size;
470 nleft = size % block_size;
477 cmd.block_size = block_size;
479 if (!sdio_set_func0_csa_address(wilc, addr))
482 ret = wilc_sdio_cmd53(wilc, &cmd);
485 "Failed cmd53 [%x], block send...\n", addr);
489 addr += nblk * block_size;
490 buf += nblk * block_size;
499 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
502 if (!sdio_set_func0_csa_address(wilc, addr))
505 ret = wilc_sdio_cmd53(wilc, &cmd);
508 "Failed cmd53 [%x], bytes send...\n", addr);
520 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
522 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
525 if ((addr >= 0xf0) && (addr <= 0xff)) {
526 struct sdio_cmd52 cmd;
532 ret = wilc_sdio_cmd52(wilc, &cmd);
535 "Failed cmd 52, read reg (%08x) ...\n", addr);
540 struct sdio_cmd53 cmd;
542 if (!sdio_set_func0_csa_address(wilc, addr))
551 cmd.buffer = (u8 *)data;
553 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
554 ret = wilc_sdio_cmd53(wilc, &cmd);
557 "Failed cmd53, read reg (%08x)...\n", addr);
562 *data = cpu_to_le32(*data);
571 static int sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
573 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
574 u32 block_size = g_sdio.block_size;
575 struct sdio_cmd53 cmd;
576 int nblk, nleft, ret;
581 * has to be word aligned...
595 * has to be word aligned...
609 nblk = size / block_size;
610 nleft = size % block_size;
617 cmd.block_size = block_size;
619 if (!sdio_set_func0_csa_address(wilc, addr))
622 ret = wilc_sdio_cmd53(wilc, &cmd);
625 "Failed cmd53 [%x], block read...\n", addr);
629 addr += nblk * block_size;
630 buf += nblk * block_size;
631 } /* if (nblk > 0) */
639 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
642 if (!sdio_set_func0_csa_address(wilc, addr))
645 ret = wilc_sdio_cmd53(wilc, &cmd);
648 "Failed cmd53 [%x], bytes read...\n", addr);
660 /********************************************
664 ********************************************/
666 static int sdio_deinit(struct wilc *wilc)
671 static int sdio_init(struct wilc *wilc, bool resume)
673 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
674 struct sdio_cmd52 cmd;
679 memset(&g_sdio, 0, sizeof(struct wilc_sdio));
680 g_sdio.irq_gpio = wilc->dev_irq_num;
684 * function 0 csa enable
691 ret = wilc_sdio_cmd52(wilc, &cmd);
693 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
698 * function 0 block size
700 if (!sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
701 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
704 g_sdio.block_size = WILC_SDIO_BLOCK_SIZE;
714 ret = wilc_sdio_cmd52(wilc, &cmd);
717 "Fail cmd 52, set IOE register...\n");
722 * make sure func 1 is up
731 ret = wilc_sdio_cmd52(wilc, &cmd);
734 "Fail cmd 52, get IOR register...\n");
742 dev_err(&func->dev, "Fail func 1 is not ready...\n");
747 * func 1 is ready, set func 1 block size
749 if (!sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
750 dev_err(&func->dev, "Fail set func 1 block size...\n");
755 * func 1 interrupt enable
762 ret = wilc_sdio_cmd52(wilc, &cmd);
764 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
769 * make sure can read back chip id correctly
772 if (!sdio_read_reg(wilc, 0x1000, &chipid)) {
773 dev_err(&func->dev, "Fail cmd read chip id...\n");
776 dev_err(&func->dev, "chipid (%08x)\n", chipid);
777 if ((chipid & 0xfff) > 0x2a0)
778 g_sdio.has_thrpt_enh3 = 1;
780 g_sdio.has_thrpt_enh3 = 0;
781 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
782 g_sdio.has_thrpt_enh3);
792 static int sdio_read_size(struct wilc *wilc, u32 *size)
795 struct sdio_cmd52 cmd;
798 * Read DMA count in words
805 wilc_sdio_cmd52(wilc, &cmd);
808 /* cmd.read_write = 0; */
809 /* cmd.function = 0; */
813 wilc_sdio_cmd52(wilc, &cmd);
814 tmp |= (cmd.data << 8);
820 static int sdio_read_int(struct wilc *wilc, u32 *int_status)
822 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
824 struct sdio_cmd52 cmd;
826 sdio_read_size(wilc, &tmp);
831 if (!g_sdio.irq_gpio) {
837 wilc_sdio_cmd52(wilc, &cmd);
839 if (cmd.data & BIT(0))
841 if (cmd.data & BIT(2))
843 if (cmd.data & BIT(3))
845 if (cmd.data & BIT(4))
847 if (cmd.data & BIT(5))
849 if (cmd.data & BIT(6))
851 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
852 if ((tmp >> (IRG_FLAGS_OFFSET + i)) & 0x1) {
854 "Unexpected interrupt (1) : tmp=%x, data=%x\n",
867 wilc_sdio_cmd52(wilc, &cmd);
868 irq_flags = cmd.data & 0x1f;
869 tmp |= ((irq_flags >> 0) << IRG_FLAGS_OFFSET);
877 static int sdio_clear_int_ext(struct wilc *wilc, u32 val)
879 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
882 if (g_sdio.has_thrpt_enh3) {
885 if (g_sdio.irq_gpio) {
888 flags = val & (BIT(MAX_NUN_INT_THRPT_ENH2) - 1);
893 /* select VMM table 0 */
894 if ((val & SEL_VMM_TBL0) == SEL_VMM_TBL0)
896 /* select VMM table 1 */
897 if ((val & SEL_VMM_TBL1) == SEL_VMM_TBL1)
900 if ((val & EN_VMM) == EN_VMM)
903 struct sdio_cmd52 cmd;
911 ret = wilc_sdio_cmd52(wilc, &cmd);
914 "Failed cmd52, set 0xf8 data (%d) ...\n",
921 if (g_sdio.irq_gpio) {
922 /* see below. has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
923 /* Cannot clear multiple interrupts. Must clear each interrupt individually */
926 flags = val & (BIT(MAX_NUM_INT) - 1);
931 for (i = 0; i < g_sdio.nint; i++) {
933 struct sdio_cmd52 cmd;
941 ret = wilc_sdio_cmd52(wilc, &cmd);
944 "Failed cmd52, set 0xf8 data (%d) ...\n",
956 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
959 "Unexpected interrupt cleared %d...\n",
970 /* select VMM table 0 */
971 if ((val & SEL_VMM_TBL0) == SEL_VMM_TBL0)
973 /* select VMM table 1 */
974 if ((val & SEL_VMM_TBL1) == SEL_VMM_TBL1)
977 if ((val & EN_VMM) == EN_VMM)
981 struct sdio_cmd52 cmd;
988 ret = wilc_sdio_cmd52(wilc, &cmd);
991 "Failed cmd52, set 0xf6 data (%d) ...\n",
1004 static int sdio_sync_ext(struct wilc *wilc, int nint)
1006 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
1009 if (nint > MAX_NUM_INT) {
1010 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
1013 if (nint > MAX_NUN_INT_THRPT_ENH2) {
1015 "Cannot support more than 5 interrupts when has_thrpt_enh2=1.\n");
1022 * Disable power sequencer
1024 if (!sdio_read_reg(wilc, WILC_MISC, ®)) {
1025 dev_err(&func->dev, "Failed read misc reg...\n");
1030 if (!sdio_write_reg(wilc, WILC_MISC, reg)) {
1031 dev_err(&func->dev, "Failed write misc reg...\n");
1035 if (g_sdio.irq_gpio) {
1040 * interrupt pin mux select
1042 ret = sdio_read_reg(wilc, WILC_PIN_MUX_0, ®);
1044 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1049 ret = sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
1051 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1059 ret = sdio_read_reg(wilc, WILC_INTR_ENABLE, ®);
1061 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1066 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1067 reg |= BIT((27 + i));
1068 ret = sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
1070 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1075 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1078 "Failed read reg (%08x)...\n",
1083 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1086 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1089 "Failed write reg (%08x)...\n",
1098 /********************************************
1100 * Global sdio HIF function table
1102 ********************************************/
1104 static const struct wilc_hif_func wilc_hif_sdio = {
1105 .hif_init = sdio_init,
1106 .hif_deinit = sdio_deinit,
1107 .hif_read_reg = sdio_read_reg,
1108 .hif_write_reg = sdio_write_reg,
1109 .hif_block_rx = sdio_read,
1110 .hif_block_tx = sdio_write,
1111 .hif_read_int = sdio_read_int,
1112 .hif_clear_int_ext = sdio_clear_int_ext,
1113 .hif_read_size = sdio_read_size,
1114 .hif_block_tx_ext = sdio_write,
1115 .hif_block_rx_ext = sdio_read,
1116 .hif_sync_ext = sdio_sync_ext,
1117 .enable_interrupt = wilc_sdio_enable_interrupt,
1118 .disable_interrupt = wilc_sdio_disable_interrupt,