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;
43 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data);
44 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data);
45 static int sdio_init(struct wilc *wilc, bool resume);
47 static void wilc_sdio_interrupt(struct sdio_func *func)
49 sdio_release_host(func);
50 wilc_handle_isr(sdio_get_drvdata(func));
51 sdio_claim_host(func);
54 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
56 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
60 sdio_claim_host(func);
62 func->num = cmd->function;
63 if (cmd->read_write) { /* write */
65 sdio_writeb(func, cmd->data, cmd->address, &ret);
66 data = sdio_readb(func, cmd->address, &ret);
69 sdio_writeb(func, cmd->data, cmd->address, &ret);
72 data = sdio_readb(func, cmd->address, &ret);
76 sdio_release_host(func);
79 dev_err(&func->dev, "wilc_sdio_cmd52..failed, err(%d)\n", ret);
84 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
86 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
89 sdio_claim_host(func);
91 func->num = cmd->function;
92 func->cur_blksize = cmd->block_size;
94 size = cmd->count * cmd->block_size;
98 if (cmd->read_write) { /* write */
99 ret = sdio_memcpy_toio(func, cmd->address,
100 (void *)cmd->buffer, size);
102 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
106 sdio_release_host(func);
109 dev_err(&func->dev, "wilc_sdio_cmd53..failed, err(%d)\n", ret);
114 static int linux_sdio_probe(struct sdio_func *func,
115 const struct sdio_device_id *id)
121 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
122 gpio = of_get_gpio(func->dev.of_node, 0);
127 dev_dbg(&func->dev, "Initializing netdev\n");
128 ret = wilc_netdev_init(&wilc, &func->dev, HIF_SDIO, gpio,
131 dev_err(&func->dev, "Couldn't initialize netdev\n");
134 sdio_set_drvdata(func, wilc);
135 wilc->dev = &func->dev;
137 dev_info(&func->dev, "Driver Initializing success\n");
141 static void linux_sdio_remove(struct sdio_func *func)
143 wilc_netdev_cleanup(sdio_get_drvdata(func));
146 static int sdio_reset(struct wilc *wilc)
148 struct sdio_cmd52 cmd;
150 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
157 ret = wilc_sdio_cmd52(wilc, &cmd);
159 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
165 static int wilc_sdio_suspend(struct device *dev)
167 struct sdio_func *func = dev_to_sdio_func(dev);
168 struct wilc *wilc = sdio_get_drvdata(func);
171 dev_info(dev, "sdio suspend\n");
174 if (!wilc->suspend_event) {
175 wilc_chip_sleep_manually(wilc);
177 host_sleep_notify(wilc);
178 chip_allow_sleep(wilc);
181 ret = sdio_reset(wilc);
183 dev_err(&func->dev, "Fail reset sdio\n");
186 sdio_claim_host(func);
191 static int wilc_sdio_resume(struct device *dev)
193 struct sdio_func *func = dev_to_sdio_func(dev);
194 struct wilc *wilc = sdio_get_drvdata(func);
196 dev_info(dev, "sdio resume\n");
197 sdio_release_host(func);
199 sdio_init(wilc, true);
201 if (wilc->suspend_event)
202 host_wakeup_notify(wilc);
204 chip_allow_sleep(wilc);
209 static const struct dev_pm_ops wilc_sdio_pm_ops = {
210 .suspend = wilc_sdio_suspend,
211 .resume = wilc_sdio_resume,
214 static struct sdio_driver wilc1000_sdio_driver = {
215 .name = SDIO_MODALIAS,
216 .id_table = wilc_sdio_ids,
217 .probe = linux_sdio_probe,
218 .remove = linux_sdio_remove,
220 .pm = &wilc_sdio_pm_ops,
223 module_driver(wilc1000_sdio_driver,
224 sdio_register_driver,
225 sdio_unregister_driver);
226 MODULE_LICENSE("GPL");
228 static int wilc_sdio_enable_interrupt(struct wilc *dev)
230 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
233 sdio_claim_host(func);
234 ret = sdio_claim_irq(func, wilc_sdio_interrupt);
235 sdio_release_host(func);
238 dev_err(&func->dev, "can't claim sdio_irq, err(%d)\n", ret);
244 static void wilc_sdio_disable_interrupt(struct wilc *dev)
246 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
249 dev_dbg(&func->dev, "wilc_sdio_disable_interrupt IN\n");
251 sdio_claim_host(func);
252 ret = sdio_release_irq(func);
254 dev_err(&func->dev, "can't release sdio_irq, err(%d)\n", ret);
255 sdio_release_host(func);
257 dev_info(&func->dev, "wilc_sdio_disable_interrupt OUT\n");
260 /********************************************
264 ********************************************/
266 static int sdio_set_func0_csa_address(struct wilc *wilc, u32 adr)
268 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
269 struct sdio_cmd52 cmd;
280 ret = wilc_sdio_cmd52(wilc, &cmd);
282 dev_err(&func->dev, "Failed cmd52, set 0x10c data...\n");
287 cmd.data = (u8)(adr >> 8);
288 ret = wilc_sdio_cmd52(wilc, &cmd);
290 dev_err(&func->dev, "Failed cmd52, set 0x10d data...\n");
295 cmd.data = (u8)(adr >> 16);
296 ret = wilc_sdio_cmd52(wilc, &cmd);
298 dev_err(&func->dev, "Failed cmd52, set 0x10e data...\n");
307 static int sdio_set_func0_block_size(struct wilc *wilc, u32 block_size)
309 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
310 struct sdio_cmd52 cmd;
317 cmd.data = (u8)block_size;
318 ret = wilc_sdio_cmd52(wilc, &cmd);
320 dev_err(&func->dev, "Failed cmd52, set 0x10 data...\n");
325 cmd.data = (u8)(block_size >> 8);
326 ret = wilc_sdio_cmd52(wilc, &cmd);
328 dev_err(&func->dev, "Failed cmd52, set 0x11 data...\n");
337 /********************************************
341 ********************************************/
343 static int sdio_set_func1_block_size(struct wilc *wilc, u32 block_size)
345 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
346 struct sdio_cmd52 cmd;
353 cmd.data = (u8)block_size;
354 ret = wilc_sdio_cmd52(wilc, &cmd);
356 dev_err(&func->dev, "Failed cmd52, set 0x110 data...\n");
360 cmd.data = (u8)(block_size >> 8);
361 ret = wilc_sdio_cmd52(wilc, &cmd);
363 dev_err(&func->dev, "Failed cmd52, set 0x111 data...\n");
372 /********************************************
376 ********************************************/
377 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
379 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
382 data = cpu_to_le32(data);
384 if ((addr >= 0xf0) && (addr <= 0xff)) {
385 struct sdio_cmd52 cmd;
392 ret = wilc_sdio_cmd52(wilc, &cmd);
395 "Failed cmd 52, read reg (%08x) ...\n", addr);
399 struct sdio_cmd53 cmd;
402 * set the AHB address
404 if (!sdio_set_func0_csa_address(wilc, addr))
413 cmd.buffer = (u8 *)&data;
414 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
415 ret = wilc_sdio_cmd53(wilc, &cmd);
418 "Failed cmd53, write reg (%08x)...\n", addr);
430 static int sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
432 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
433 u32 block_size = g_sdio.block_size;
434 struct sdio_cmd53 cmd;
435 int nblk, nleft, ret;
440 * has to be word aligned...
454 * has to be word aligned...
468 nblk = size / block_size;
469 nleft = size % block_size;
476 cmd.block_size = block_size;
478 if (!sdio_set_func0_csa_address(wilc, addr))
481 ret = wilc_sdio_cmd53(wilc, &cmd);
484 "Failed cmd53 [%x], block send...\n", addr);
488 addr += nblk * block_size;
489 buf += nblk * block_size;
498 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
501 if (!sdio_set_func0_csa_address(wilc, addr))
504 ret = wilc_sdio_cmd53(wilc, &cmd);
507 "Failed cmd53 [%x], bytes send...\n", addr);
519 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
521 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
524 if ((addr >= 0xf0) && (addr <= 0xff)) {
525 struct sdio_cmd52 cmd;
531 ret = wilc_sdio_cmd52(wilc, &cmd);
534 "Failed cmd 52, read reg (%08x) ...\n", addr);
539 struct sdio_cmd53 cmd;
541 if (!sdio_set_func0_csa_address(wilc, addr))
550 cmd.buffer = (u8 *)data;
552 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
553 ret = wilc_sdio_cmd53(wilc, &cmd);
556 "Failed cmd53, read reg (%08x)...\n", addr);
561 *data = cpu_to_le32(*data);
570 static int sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
572 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
573 u32 block_size = g_sdio.block_size;
574 struct sdio_cmd53 cmd;
575 int nblk, nleft, ret;
580 * has to be word aligned...
594 * has to be word aligned...
608 nblk = size / block_size;
609 nleft = size % block_size;
616 cmd.block_size = block_size;
618 if (!sdio_set_func0_csa_address(wilc, addr))
621 ret = wilc_sdio_cmd53(wilc, &cmd);
624 "Failed cmd53 [%x], block read...\n", addr);
628 addr += nblk * block_size;
629 buf += nblk * block_size;
630 } /* if (nblk > 0) */
638 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
641 if (!sdio_set_func0_csa_address(wilc, addr))
644 ret = wilc_sdio_cmd53(wilc, &cmd);
647 "Failed cmd53 [%x], bytes read...\n", addr);
659 /********************************************
663 ********************************************/
665 static int sdio_deinit(struct wilc *wilc)
670 static int sdio_init(struct wilc *wilc, bool resume)
672 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
673 struct sdio_cmd52 cmd;
678 memset(&g_sdio, 0, sizeof(struct wilc_sdio));
679 g_sdio.irq_gpio = wilc->dev_irq_num;
683 * function 0 csa enable
690 ret = wilc_sdio_cmd52(wilc, &cmd);
692 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
697 * function 0 block size
699 if (!sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
700 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
703 g_sdio.block_size = WILC_SDIO_BLOCK_SIZE;
713 ret = wilc_sdio_cmd52(wilc, &cmd);
716 "Fail cmd 52, set IOE register...\n");
721 * make sure func 1 is up
730 ret = wilc_sdio_cmd52(wilc, &cmd);
733 "Fail cmd 52, get IOR register...\n");
741 dev_err(&func->dev, "Fail func 1 is not ready...\n");
746 * func 1 is ready, set func 1 block size
748 if (!sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
749 dev_err(&func->dev, "Fail set func 1 block size...\n");
754 * func 1 interrupt enable
761 ret = wilc_sdio_cmd52(wilc, &cmd);
763 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
768 * make sure can read back chip id correctly
771 if (!sdio_read_reg(wilc, 0x1000, &chipid)) {
772 dev_err(&func->dev, "Fail cmd read chip id...\n");
775 dev_err(&func->dev, "chipid (%08x)\n", chipid);
776 if ((chipid & 0xfff) > 0x2a0)
777 g_sdio.has_thrpt_enh3 = 1;
779 g_sdio.has_thrpt_enh3 = 0;
780 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
781 g_sdio.has_thrpt_enh3);
791 static int sdio_read_size(struct wilc *wilc, u32 *size)
794 struct sdio_cmd52 cmd;
797 * Read DMA count in words
804 wilc_sdio_cmd52(wilc, &cmd);
807 /* cmd.read_write = 0; */
808 /* cmd.function = 0; */
812 wilc_sdio_cmd52(wilc, &cmd);
813 tmp |= (cmd.data << 8);
819 static int sdio_read_int(struct wilc *wilc, u32 *int_status)
821 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
823 struct sdio_cmd52 cmd;
825 sdio_read_size(wilc, &tmp);
830 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 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,