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) },
22 #define WILC_SDIO_BLOCK_SIZE 512
28 /* Max num interrupts allowed in registers 0xf7, 0xf8 */
29 #define MAX_NUN_INT_THRPT_ENH2 (5)
33 static struct wilc_sdio g_sdio;
34 static const struct wilc_hif_func wilc_hif_sdio;
36 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data);
37 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data);
38 static int sdio_init(struct wilc *wilc, bool resume);
40 static void wilc_sdio_interrupt(struct sdio_func *func)
42 sdio_release_host(func);
43 wilc_handle_isr(sdio_get_drvdata(func));
44 sdio_claim_host(func);
47 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
49 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
53 sdio_claim_host(func);
55 func->num = cmd->function;
56 if (cmd->read_write) { /* write */
58 sdio_writeb(func, cmd->data, cmd->address, &ret);
59 data = sdio_readb(func, cmd->address, &ret);
62 sdio_writeb(func, cmd->data, cmd->address, &ret);
65 data = sdio_readb(func, cmd->address, &ret);
69 sdio_release_host(func);
72 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
76 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
78 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
81 sdio_claim_host(func);
83 func->num = cmd->function;
84 func->cur_blksize = cmd->block_size;
86 size = cmd->count * cmd->block_size;
90 if (cmd->read_write) { /* write */
91 ret = sdio_memcpy_toio(func, cmd->address,
92 (void *)cmd->buffer, size);
94 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
98 sdio_release_host(func);
101 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
106 static int linux_sdio_probe(struct sdio_func *func,
107 const struct sdio_device_id *id)
111 struct gpio_desc *gpio = NULL;
113 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
114 gpio = gpiod_get(&func->dev, "irq", GPIOD_IN);
116 /* get the GPIO descriptor from hardcode GPIO number */
117 gpio = gpio_to_desc(GPIO_NUM);
119 dev_err(&func->dev, "failed to get irq gpio\n");
123 dev_dbg(&func->dev, "Initializing netdev\n");
124 ret = wilc_netdev_init(&wilc, &func->dev, HIF_SDIO, &wilc_hif_sdio);
126 dev_err(&func->dev, "Couldn't initialize netdev\n");
129 sdio_set_drvdata(func, wilc);
130 wilc->dev = &func->dev;
131 wilc->gpio_irq = gpio;
133 dev_info(&func->dev, "Driver Initializing success\n");
137 static void linux_sdio_remove(struct sdio_func *func)
139 struct wilc *wilc = sdio_get_drvdata(func);
141 /* free the GPIO in module remove */
143 gpiod_put(wilc->gpio_irq);
144 wilc_netdev_cleanup(wilc);
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 of_device_id wilc_of_match[] = {
211 { .compatible = "microchip,wilc1000-sdio", },
214 MODULE_DEVICE_TABLE(of, wilc_of_match);
216 static const struct dev_pm_ops wilc_sdio_pm_ops = {
217 .suspend = wilc_sdio_suspend,
218 .resume = wilc_sdio_resume,
221 static struct sdio_driver wilc_sdio_driver = {
222 .name = SDIO_MODALIAS,
223 .id_table = wilc_sdio_ids,
224 .probe = linux_sdio_probe,
225 .remove = linux_sdio_remove,
227 .pm = &wilc_sdio_pm_ops,
228 .of_match_table = wilc_of_match,
231 module_driver(wilc_sdio_driver,
232 sdio_register_driver,
233 sdio_unregister_driver);
234 MODULE_LICENSE("GPL");
236 static int wilc_sdio_enable_interrupt(struct wilc *dev)
238 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
241 sdio_claim_host(func);
242 ret = sdio_claim_irq(func, wilc_sdio_interrupt);
243 sdio_release_host(func);
246 dev_err(&func->dev, "can't claim sdio_irq, err(%d)\n", ret);
252 static void wilc_sdio_disable_interrupt(struct wilc *dev)
254 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
257 sdio_claim_host(func);
258 ret = sdio_release_irq(func);
260 dev_err(&func->dev, "can't release sdio_irq, err(%d)\n", ret);
261 sdio_release_host(func);
264 /********************************************
268 ********************************************/
270 static int sdio_set_func0_csa_address(struct wilc *wilc, u32 adr)
272 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
273 struct sdio_cmd52 cmd;
284 ret = wilc_sdio_cmd52(wilc, &cmd);
286 dev_err(&func->dev, "Failed cmd52, set 0x10c data...\n");
291 cmd.data = (u8)(adr >> 8);
292 ret = wilc_sdio_cmd52(wilc, &cmd);
294 dev_err(&func->dev, "Failed cmd52, set 0x10d data...\n");
299 cmd.data = (u8)(adr >> 16);
300 ret = wilc_sdio_cmd52(wilc, &cmd);
302 dev_err(&func->dev, "Failed cmd52, set 0x10e data...\n");
311 static int sdio_set_func0_block_size(struct wilc *wilc, u32 block_size)
313 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
314 struct sdio_cmd52 cmd;
321 cmd.data = (u8)block_size;
322 ret = wilc_sdio_cmd52(wilc, &cmd);
324 dev_err(&func->dev, "Failed cmd52, set 0x10 data...\n");
329 cmd.data = (u8)(block_size >> 8);
330 ret = wilc_sdio_cmd52(wilc, &cmd);
332 dev_err(&func->dev, "Failed cmd52, set 0x11 data...\n");
341 /********************************************
345 ********************************************/
347 static int sdio_set_func1_block_size(struct wilc *wilc, u32 block_size)
349 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
350 struct sdio_cmd52 cmd;
357 cmd.data = (u8)block_size;
358 ret = wilc_sdio_cmd52(wilc, &cmd);
360 dev_err(&func->dev, "Failed cmd52, set 0x110 data...\n");
364 cmd.data = (u8)(block_size >> 8);
365 ret = wilc_sdio_cmd52(wilc, &cmd);
367 dev_err(&func->dev, "Failed cmd52, set 0x111 data...\n");
376 /********************************************
380 ********************************************/
381 static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
383 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
388 if (addr >= 0xf0 && addr <= 0xff) {
389 struct sdio_cmd52 cmd;
396 ret = wilc_sdio_cmd52(wilc, &cmd);
399 "Failed cmd 52, read reg (%08x) ...\n", addr);
403 struct sdio_cmd53 cmd;
406 * set the AHB address
408 if (!sdio_set_func0_csa_address(wilc, addr))
417 cmd.buffer = (u8 *)&data;
418 cmd.block_size = g_sdio.block_size;
419 ret = wilc_sdio_cmd53(wilc, &cmd);
422 "Failed cmd53, write reg (%08x)...\n", addr);
434 static int sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
436 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
437 u32 block_size = g_sdio.block_size;
438 struct sdio_cmd53 cmd;
439 int nblk, nleft, ret;
444 * has to be word aligned...
458 * has to be word aligned...
472 nblk = size / block_size;
473 nleft = size % block_size;
480 cmd.block_size = block_size;
482 if (!sdio_set_func0_csa_address(wilc, addr))
485 ret = wilc_sdio_cmd53(wilc, &cmd);
488 "Failed cmd53 [%x], block send...\n", addr);
492 addr += nblk * block_size;
493 buf += nblk * block_size;
502 cmd.block_size = block_size;
505 if (!sdio_set_func0_csa_address(wilc, addr))
508 ret = wilc_sdio_cmd53(wilc, &cmd);
511 "Failed cmd53 [%x], bytes send...\n", addr);
523 static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
525 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
528 if (addr >= 0xf0 && addr <= 0xff) {
529 struct sdio_cmd52 cmd;
535 ret = wilc_sdio_cmd52(wilc, &cmd);
538 "Failed cmd 52, read reg (%08x) ...\n", addr);
543 struct sdio_cmd53 cmd;
545 if (!sdio_set_func0_csa_address(wilc, addr))
554 cmd.buffer = (u8 *)data;
556 cmd.block_size = g_sdio.block_size;
557 ret = wilc_sdio_cmd53(wilc, &cmd);
560 "Failed cmd53, read reg (%08x)...\n", addr);
574 static int sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
576 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
577 u32 block_size = g_sdio.block_size;
578 struct sdio_cmd53 cmd;
579 int nblk, nleft, ret;
584 * has to be word aligned...
598 * has to be word aligned...
612 nblk = size / block_size;
613 nleft = size % block_size;
620 cmd.block_size = block_size;
622 if (!sdio_set_func0_csa_address(wilc, addr))
625 ret = wilc_sdio_cmd53(wilc, &cmd);
628 "Failed cmd53 [%x], block read...\n", addr);
632 addr += nblk * block_size;
633 buf += nblk * block_size;
634 } /* if (nblk > 0) */
642 cmd.block_size = block_size;
645 if (!sdio_set_func0_csa_address(wilc, addr))
648 ret = wilc_sdio_cmd53(wilc, &cmd);
651 "Failed cmd53 [%x], bytes read...\n", addr);
663 /********************************************
667 ********************************************/
669 static int sdio_deinit(struct wilc *wilc)
674 static int sdio_init(struct wilc *wilc, bool resume)
676 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
677 struct sdio_cmd52 cmd;
682 memset(&g_sdio, 0, sizeof(struct wilc_sdio));
683 g_sdio.irq_gpio = wilc->dev_irq_num;
687 * function 0 csa enable
694 ret = wilc_sdio_cmd52(wilc, &cmd);
696 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
701 * function 0 block size
703 if (!sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
704 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
707 g_sdio.block_size = WILC_SDIO_BLOCK_SIZE;
717 ret = wilc_sdio_cmd52(wilc, &cmd);
720 "Fail cmd 52, set IOE register...\n");
725 * make sure func 1 is up
734 ret = wilc_sdio_cmd52(wilc, &cmd);
737 "Fail cmd 52, get IOR register...\n");
745 dev_err(&func->dev, "Fail func 1 is not ready...\n");
750 * func 1 is ready, set func 1 block size
752 if (!sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
753 dev_err(&func->dev, "Fail set func 1 block size...\n");
758 * func 1 interrupt enable
765 ret = wilc_sdio_cmd52(wilc, &cmd);
767 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
772 * make sure can read back chip id correctly
775 if (!sdio_read_reg(wilc, 0x1000, &chipid)) {
776 dev_err(&func->dev, "Fail cmd read chip id...\n");
779 dev_err(&func->dev, "chipid (%08x)\n", chipid);
780 if ((chipid & 0xfff) > 0x2a0)
781 g_sdio.has_thrpt_enh3 = 1;
783 g_sdio.has_thrpt_enh3 = 0;
784 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
785 g_sdio.has_thrpt_enh3);
795 static int sdio_read_size(struct wilc *wilc, u32 *size)
798 struct sdio_cmd52 cmd;
801 * Read DMA count in words
808 wilc_sdio_cmd52(wilc, &cmd);
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) {
838 wilc_sdio_cmd52(wilc, &cmd);
840 if (cmd.data & BIT(0))
842 if (cmd.data & BIT(2))
844 if (cmd.data & BIT(3))
846 if (cmd.data & BIT(4))
848 if (cmd.data & BIT(5))
850 if (cmd.data & BIT(6))
852 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
853 if ((tmp >> (IRG_FLAGS_OFFSET + i)) & 0x1) {
855 "Unexpected interrupt (1) : tmp=%x, data=%x\n",
868 wilc_sdio_cmd52(wilc, &cmd);
869 irq_flags = cmd.data & 0x1f;
870 tmp |= ((irq_flags >> 0) << IRG_FLAGS_OFFSET);
878 static int sdio_clear_int_ext(struct wilc *wilc, u32 val)
880 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
884 if (g_sdio.has_thrpt_enh3) {
887 if (g_sdio.irq_gpio) {
890 flags = val & (BIT(MAX_NUN_INT_THRPT_ENH2) - 1);
895 /* select VMM table 0 */
896 if (val & SEL_VMM_TBL0)
898 /* select VMM table 1 */
899 if (val & SEL_VMM_TBL1)
905 struct sdio_cmd52 cmd;
913 ret = wilc_sdio_cmd52(wilc, &cmd);
916 "Failed cmd52, set 0xf8 data (%d) ...\n",
923 if (g_sdio.irq_gpio) {
924 /* has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
926 * Cannot clear multiple interrupts.
927 * Must clear each interrupt individually.
931 flags = val & (BIT(MAX_NUM_INT) - 1);
936 for (i = 0; i < g_sdio.nint; i++) {
938 struct sdio_cmd52 cmd;
946 ret = wilc_sdio_cmd52(wilc, &cmd);
949 "Failed cmd52, set 0xf8 data (%d) ...\n",
960 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
963 "Unexpected interrupt cleared %d...\n",
971 /* select VMM table 0 */
972 if (val & SEL_VMM_TBL0)
974 /* select VMM table 1 */
975 if (val & SEL_VMM_TBL1)
982 struct sdio_cmd52 cmd;
989 ret = wilc_sdio_cmd52(wilc, &cmd);
992 "Failed cmd52, set 0xf6 data (%d) ...\n",
1002 static int sdio_sync_ext(struct wilc *wilc, int nint)
1004 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
1007 if (nint > MAX_NUM_INT) {
1008 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
1011 if (nint > MAX_NUN_INT_THRPT_ENH2) {
1013 "Cannot support more than 5 interrupts when has_thrpt_enh2=1.\n");
1020 * Disable power sequencer
1022 if (!sdio_read_reg(wilc, WILC_MISC, ®)) {
1023 dev_err(&func->dev, "Failed read misc reg...\n");
1028 if (!sdio_write_reg(wilc, WILC_MISC, reg)) {
1029 dev_err(&func->dev, "Failed write misc reg...\n");
1033 if (g_sdio.irq_gpio) {
1038 * interrupt pin mux select
1040 ret = sdio_read_reg(wilc, WILC_PIN_MUX_0, ®);
1042 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1047 ret = sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
1049 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1057 ret = sdio_read_reg(wilc, WILC_INTR_ENABLE, ®);
1059 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1064 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1065 reg |= BIT((27 + i));
1066 ret = sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
1068 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1073 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1076 "Failed read reg (%08x)...\n",
1081 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1084 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1087 "Failed write reg (%08x)...\n",
1096 /* Global sdio HIF function table */
1097 static const struct wilc_hif_func wilc_hif_sdio = {
1098 .hif_init = sdio_init,
1099 .hif_deinit = sdio_deinit,
1100 .hif_read_reg = sdio_read_reg,
1101 .hif_write_reg = sdio_write_reg,
1102 .hif_block_rx = sdio_read,
1103 .hif_block_tx = sdio_write,
1104 .hif_read_int = sdio_read_int,
1105 .hif_clear_int_ext = sdio_clear_int_ext,
1106 .hif_read_size = sdio_read_size,
1107 .hif_block_tx_ext = sdio_write,
1108 .hif_block_rx_ext = sdio_read,
1109 .hif_sync_ext = sdio_sync_ext,
1110 .enable_interrupt = wilc_sdio_enable_interrupt,
1111 .disable_interrupt = wilc_sdio_disable_interrupt,