1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
8 #include <linux/mmc/sdio_func.h>
9 #include <linux/mmc/host.h>
14 #define SDIO_MODALIAS "wilc1000_sdio"
16 #define SDIO_VENDOR_ID_WILC 0x0296
17 #define SDIO_DEVICE_ID_WILC 0x5347
19 static const struct sdio_device_id wilc_sdio_ids[] = {
20 { SDIO_DEVICE(SDIO_VENDOR_ID_WILC, SDIO_DEVICE_ID_WILC) },
24 #define WILC_SDIO_BLOCK_SIZE 512
30 /* Max num interrupts allowed in registers 0xf7, 0xf8 */
31 #define MAX_NUN_INT_THRPT_ENH2 (5)
54 static const struct wilc_hif_func wilc_hif_sdio;
56 static void wilc_sdio_interrupt(struct sdio_func *func)
58 sdio_release_host(func);
59 wilc_handle_isr(sdio_get_drvdata(func));
60 sdio_claim_host(func);
63 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
65 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
69 sdio_claim_host(func);
71 func->num = cmd->function;
72 if (cmd->read_write) { /* write */
74 sdio_writeb(func, cmd->data, cmd->address, &ret);
75 data = sdio_readb(func, cmd->address, &ret);
78 sdio_writeb(func, cmd->data, cmd->address, &ret);
81 data = sdio_readb(func, cmd->address, &ret);
85 sdio_release_host(func);
88 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
92 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
94 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
97 sdio_claim_host(func);
99 func->num = cmd->function;
100 func->cur_blksize = cmd->block_size;
102 size = cmd->count * cmd->block_size;
106 if (cmd->read_write) { /* write */
107 ret = sdio_memcpy_toio(func, cmd->address,
108 (void *)cmd->buffer, size);
110 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
114 sdio_release_host(func);
117 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
122 static int wilc_sdio_probe(struct sdio_func *func,
123 const struct sdio_device_id *id)
127 struct gpio_desc *gpio = NULL;
128 struct wilc_sdio *sdio_priv;
130 sdio_priv = kzalloc(sizeof(*sdio_priv), GFP_KERNEL);
134 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
135 gpio = gpiod_get(&func->dev, "irq", GPIOD_IN);
137 /* get the GPIO descriptor from hardcode GPIO number */
138 gpio = gpio_to_desc(GPIO_NUM);
140 dev_err(&func->dev, "failed to get irq gpio\n");
144 ret = wilc_cfg80211_init(&wilc, &func->dev, WILC_HIF_SDIO,
150 sdio_set_drvdata(func, wilc);
151 wilc->bus_data = sdio_priv;
152 wilc->dev = &func->dev;
153 wilc->gpio_irq = gpio;
155 wilc->rtc_clk = devm_clk_get(&func->card->dev, "rtc_clk");
156 if (PTR_ERR_OR_ZERO(wilc->rtc_clk) == -EPROBE_DEFER)
157 return -EPROBE_DEFER;
158 else if (!IS_ERR(wilc->rtc_clk))
159 clk_prepare_enable(wilc->rtc_clk);
161 dev_info(&func->dev, "Driver Initializing success\n");
165 static void wilc_sdio_remove(struct sdio_func *func)
167 struct wilc *wilc = sdio_get_drvdata(func);
169 /* free the GPIO in module remove */
171 gpiod_put(wilc->gpio_irq);
173 if (!IS_ERR(wilc->rtc_clk))
174 clk_disable_unprepare(wilc->rtc_clk);
176 wilc_netdev_cleanup(wilc);
179 static int wilc_sdio_reset(struct wilc *wilc)
181 struct sdio_cmd52 cmd;
183 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
190 ret = wilc_sdio_cmd52(wilc, &cmd);
192 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
198 static int wilc_sdio_suspend(struct device *dev)
200 struct sdio_func *func = dev_to_sdio_func(dev);
201 struct wilc *wilc = sdio_get_drvdata(func);
204 dev_info(dev, "sdio suspend\n");
207 if (!IS_ERR(wilc->rtc_clk))
208 clk_disable_unprepare(wilc->rtc_clk);
210 if (wilc->suspend_event) {
211 host_sleep_notify(wilc);
212 chip_allow_sleep(wilc);
215 ret = wilc_sdio_reset(wilc);
217 dev_err(&func->dev, "Fail reset sdio\n");
220 sdio_claim_host(func);
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 wilc_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");
298 static int wilc_sdio_set_func0_block_size(struct wilc *wilc, u32 block_size)
300 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
301 struct sdio_cmd52 cmd;
308 cmd.data = (u8)block_size;
309 ret = wilc_sdio_cmd52(wilc, &cmd);
311 dev_err(&func->dev, "Failed cmd52, set 0x10 data...\n");
316 cmd.data = (u8)(block_size >> 8);
317 ret = wilc_sdio_cmd52(wilc, &cmd);
319 dev_err(&func->dev, "Failed cmd52, set 0x11 data...\n");
326 /********************************************
330 ********************************************/
332 static int wilc_sdio_set_func1_block_size(struct wilc *wilc, u32 block_size)
334 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
335 struct sdio_cmd52 cmd;
342 cmd.data = (u8)block_size;
343 ret = wilc_sdio_cmd52(wilc, &cmd);
345 dev_err(&func->dev, "Failed cmd52, set 0x110 data...\n");
349 cmd.data = (u8)(block_size >> 8);
350 ret = wilc_sdio_cmd52(wilc, &cmd);
352 dev_err(&func->dev, "Failed cmd52, set 0x111 data...\n");
359 /********************************************
363 ********************************************/
364 static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
366 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
367 struct wilc_sdio *sdio_priv = wilc->bus_data;
372 if (addr >= 0xf0 && addr <= 0xff) {
373 struct sdio_cmd52 cmd;
380 ret = wilc_sdio_cmd52(wilc, &cmd);
383 "Failed cmd 52, read reg (%08x) ...\n", addr);
385 struct sdio_cmd53 cmd;
388 * set the AHB address
390 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
400 cmd.buffer = (u8 *)&data;
401 cmd.block_size = sdio_priv->block_size;
402 ret = wilc_sdio_cmd53(wilc, &cmd);
405 "Failed cmd53, write reg (%08x)...\n", addr);
411 static int wilc_sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
413 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
414 struct wilc_sdio *sdio_priv = wilc->bus_data;
415 u32 block_size = sdio_priv->block_size;
416 struct sdio_cmd53 cmd;
417 int nblk, nleft, ret;
422 * has to be word aligned...
436 * has to be word aligned...
450 nblk = size / block_size;
451 nleft = size % block_size;
458 cmd.block_size = block_size;
460 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
464 ret = wilc_sdio_cmd53(wilc, &cmd);
467 "Failed cmd53 [%x], block send...\n", addr);
471 addr += nblk * block_size;
472 buf += nblk * block_size;
481 cmd.block_size = block_size;
484 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
488 ret = wilc_sdio_cmd53(wilc, &cmd);
491 "Failed cmd53 [%x], bytes send...\n", addr);
499 static int wilc_sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
501 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
502 struct wilc_sdio *sdio_priv = wilc->bus_data;
505 if (addr >= 0xf0 && addr <= 0xff) {
506 struct sdio_cmd52 cmd;
512 ret = wilc_sdio_cmd52(wilc, &cmd);
515 "Failed cmd 52, read reg (%08x) ...\n", addr);
520 struct sdio_cmd53 cmd;
522 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
532 cmd.buffer = (u8 *)data;
534 cmd.block_size = sdio_priv->block_size;
535 ret = wilc_sdio_cmd53(wilc, &cmd);
538 "Failed cmd53, read reg (%08x)...\n", addr);
547 static int wilc_sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
549 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
550 struct wilc_sdio *sdio_priv = wilc->bus_data;
551 u32 block_size = sdio_priv->block_size;
552 struct sdio_cmd53 cmd;
553 int nblk, nleft, ret;
558 * has to be word aligned...
572 * has to be word aligned...
586 nblk = size / block_size;
587 nleft = size % block_size;
594 cmd.block_size = block_size;
596 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
600 ret = wilc_sdio_cmd53(wilc, &cmd);
603 "Failed cmd53 [%x], block read...\n", addr);
607 addr += nblk * block_size;
608 buf += nblk * block_size;
609 } /* if (nblk > 0) */
617 cmd.block_size = block_size;
620 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
624 ret = wilc_sdio_cmd53(wilc, &cmd);
627 "Failed cmd53 [%x], bytes read...\n", addr);
635 /********************************************
639 ********************************************/
641 static int wilc_sdio_deinit(struct wilc *wilc)
646 static int wilc_sdio_init(struct wilc *wilc, bool resume)
648 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
649 struct wilc_sdio *sdio_priv = wilc->bus_data;
650 struct sdio_cmd52 cmd;
655 sdio_priv->irq_gpio = wilc->dev_irq_num;
658 * function 0 csa enable
665 ret = wilc_sdio_cmd52(wilc, &cmd);
667 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
672 * function 0 block size
674 ret = wilc_sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE);
676 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
679 sdio_priv->block_size = WILC_SDIO_BLOCK_SIZE;
689 ret = wilc_sdio_cmd52(wilc, &cmd);
692 "Fail cmd 52, set IOE register...\n");
697 * make sure func 1 is up
706 ret = wilc_sdio_cmd52(wilc, &cmd);
709 "Fail cmd 52, get IOR register...\n");
717 dev_err(&func->dev, "Fail func 1 is not ready...\n");
722 * func 1 is ready, set func 1 block size
724 ret = wilc_sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE);
726 dev_err(&func->dev, "Fail set func 1 block size...\n");
731 * func 1 interrupt enable
738 ret = wilc_sdio_cmd52(wilc, &cmd);
740 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
745 * make sure can read back chip id correctly
748 ret = wilc_sdio_read_reg(wilc, 0x1000, &chipid);
750 dev_err(&func->dev, "Fail cmd read chip id...\n");
753 dev_err(&func->dev, "chipid (%08x)\n", chipid);
754 if ((chipid & 0xfff) > 0x2a0)
755 sdio_priv->has_thrpt_enh3 = 1;
757 sdio_priv->has_thrpt_enh3 = 0;
758 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
759 sdio_priv->has_thrpt_enh3);
765 static int wilc_sdio_read_size(struct wilc *wilc, u32 *size)
768 struct sdio_cmd52 cmd;
771 * Read DMA count in words
778 wilc_sdio_cmd52(wilc, &cmd);
783 wilc_sdio_cmd52(wilc, &cmd);
784 tmp |= (cmd.data << 8);
790 static int wilc_sdio_read_int(struct wilc *wilc, u32 *int_status)
792 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
793 struct wilc_sdio *sdio_priv = wilc->bus_data;
795 struct sdio_cmd52 cmd;
797 wilc_sdio_read_size(wilc, &tmp);
802 if (!sdio_priv->irq_gpio) {
809 wilc_sdio_cmd52(wilc, &cmd);
811 if (cmd.data & BIT(0))
813 if (cmd.data & BIT(2))
815 if (cmd.data & BIT(3))
817 if (cmd.data & BIT(4))
819 if (cmd.data & BIT(5))
821 if (cmd.data & BIT(6))
823 for (i = sdio_priv->nint; i < MAX_NUM_INT; i++) {
824 if ((tmp >> (IRG_FLAGS_OFFSET + i)) & 0x1) {
826 "Unexpected interrupt (1) : tmp=%x, data=%x\n",
839 wilc_sdio_cmd52(wilc, &cmd);
840 irq_flags = cmd.data & 0x1f;
841 tmp |= ((irq_flags >> 0) << IRG_FLAGS_OFFSET);
849 static int wilc_sdio_clear_int_ext(struct wilc *wilc, u32 val)
851 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
852 struct wilc_sdio *sdio_priv = wilc->bus_data;
856 if (sdio_priv->has_thrpt_enh3) {
859 if (sdio_priv->irq_gpio) {
862 flags = val & (BIT(MAX_NUN_INT_THRPT_ENH2) - 1);
867 /* select VMM table 0 */
868 if (val & SEL_VMM_TBL0)
870 /* select VMM table 1 */
871 if (val & SEL_VMM_TBL1)
877 struct sdio_cmd52 cmd;
885 ret = wilc_sdio_cmd52(wilc, &cmd);
888 "Failed cmd52, set 0xf8 data (%d) ...\n",
895 if (sdio_priv->irq_gpio) {
896 /* has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
898 * Cannot clear multiple interrupts.
899 * Must clear each interrupt individually.
903 flags = val & (BIT(MAX_NUM_INT) - 1);
907 for (i = 0; i < sdio_priv->nint; i++) {
909 struct sdio_cmd52 cmd;
917 ret = wilc_sdio_cmd52(wilc, &cmd);
920 "Failed cmd52, set 0xf8 data (%d) ...\n",
928 for (i = sdio_priv->nint; i < MAX_NUM_INT; i++) {
931 "Unexpected interrupt cleared %d...\n",
939 /* select VMM table 0 */
940 if (val & SEL_VMM_TBL0)
942 /* select VMM table 1 */
943 if (val & SEL_VMM_TBL1)
950 struct sdio_cmd52 cmd;
957 ret = wilc_sdio_cmd52(wilc, &cmd);
960 "Failed cmd52, set 0xf6 data (%d) ...\n",
968 static int wilc_sdio_sync_ext(struct wilc *wilc, int nint)
970 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
971 struct wilc_sdio *sdio_priv = wilc->bus_data;
974 if (nint > MAX_NUM_INT) {
975 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
978 if (nint > MAX_NUN_INT_THRPT_ENH2) {
980 "Cannot support more than 5 interrupts when has_thrpt_enh2=1.\n");
984 sdio_priv->nint = nint;
987 * Disable power sequencer
989 if (wilc_sdio_read_reg(wilc, WILC_MISC, ®)) {
990 dev_err(&func->dev, "Failed read misc reg...\n");
995 if (wilc_sdio_write_reg(wilc, WILC_MISC, reg)) {
996 dev_err(&func->dev, "Failed write misc reg...\n");
1000 if (sdio_priv->irq_gpio) {
1005 * interrupt pin mux select
1007 ret = wilc_sdio_read_reg(wilc, WILC_PIN_MUX_0, ®);
1009 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1014 ret = wilc_sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
1016 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1024 ret = wilc_sdio_read_reg(wilc, WILC_INTR_ENABLE, ®);
1026 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1031 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1032 reg |= BIT((27 + i));
1033 ret = wilc_sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
1035 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1040 ret = wilc_sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1043 "Failed read reg (%08x)...\n",
1048 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1051 ret = wilc_sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1054 "Failed write reg (%08x)...\n",
1063 /* Global sdio HIF function table */
1064 static const struct wilc_hif_func wilc_hif_sdio = {
1065 .hif_init = wilc_sdio_init,
1066 .hif_deinit = wilc_sdio_deinit,
1067 .hif_read_reg = wilc_sdio_read_reg,
1068 .hif_write_reg = wilc_sdio_write_reg,
1069 .hif_block_rx = wilc_sdio_read,
1070 .hif_block_tx = wilc_sdio_write,
1071 .hif_read_int = wilc_sdio_read_int,
1072 .hif_clear_int_ext = wilc_sdio_clear_int_ext,
1073 .hif_read_size = wilc_sdio_read_size,
1074 .hif_block_tx_ext = wilc_sdio_write,
1075 .hif_block_rx_ext = wilc_sdio_read,
1076 .hif_sync_ext = wilc_sdio_sync_ext,
1077 .enable_interrupt = wilc_sdio_enable_interrupt,
1078 .disable_interrupt = wilc_sdio_disable_interrupt,
1081 static int wilc_sdio_resume(struct device *dev)
1083 struct sdio_func *func = dev_to_sdio_func(dev);
1084 struct wilc *wilc = sdio_get_drvdata(func);
1086 dev_info(dev, "sdio resume\n");
1087 sdio_release_host(func);
1089 wilc_sdio_init(wilc, true);
1091 if (wilc->suspend_event)
1092 host_wakeup_notify(wilc);
1094 chip_allow_sleep(wilc);
1099 static const struct of_device_id wilc_of_match[] = {
1100 { .compatible = "microchip,wilc1000-sdio", },
1103 MODULE_DEVICE_TABLE(of, wilc_of_match);
1105 static const struct dev_pm_ops wilc_sdio_pm_ops = {
1106 .suspend = wilc_sdio_suspend,
1107 .resume = wilc_sdio_resume,
1110 static struct sdio_driver wilc_sdio_driver = {
1111 .name = SDIO_MODALIAS,
1112 .id_table = wilc_sdio_ids,
1113 .probe = wilc_sdio_probe,
1114 .remove = wilc_sdio_remove,
1116 .pm = &wilc_sdio_pm_ops,
1117 .of_match_table = wilc_of_match,
1120 module_driver(wilc_sdio_driver,
1121 sdio_register_driver,
1122 sdio_unregister_driver);
1123 MODULE_LICENSE("GPL");