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/sdio_ids.h>
10 #include <linux/mmc/host.h>
11 #include <linux/mmc/sdio.h>
12 #include <linux/of_irq.h>
17 #define SDIO_MODALIAS "wilc1000_sdio"
19 static const struct sdio_device_id wilc_sdio_ids[] = {
20 { SDIO_DEVICE(SDIO_VENDOR_ID_MICROCHIP_WILC, SDIO_DEVICE_ID_MICROCHIP_WILC1000) },
24 #define WILC_SDIO_BLOCK_SIZE 512
51 static const struct wilc_hif_func wilc_hif_sdio;
53 static void wilc_sdio_interrupt(struct sdio_func *func)
55 sdio_release_host(func);
56 wilc_handle_isr(sdio_get_drvdata(func));
57 sdio_claim_host(func);
60 static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
62 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
66 sdio_claim_host(func);
68 func->num = cmd->function;
69 if (cmd->read_write) { /* write */
71 sdio_writeb(func, cmd->data, cmd->address, &ret);
72 data = sdio_readb(func, cmd->address, &ret);
75 sdio_writeb(func, cmd->data, cmd->address, &ret);
78 data = sdio_readb(func, cmd->address, &ret);
82 sdio_release_host(func);
85 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
89 static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
91 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
94 sdio_claim_host(func);
96 func->num = cmd->function;
97 func->cur_blksize = cmd->block_size;
99 size = cmd->count * cmd->block_size;
103 if (cmd->read_write) { /* write */
104 ret = sdio_memcpy_toio(func, cmd->address,
105 (void *)cmd->buffer, size);
107 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
111 sdio_release_host(func);
114 dev_err(&func->dev, "%s..failed, err(%d)\n", __func__, ret);
119 static int wilc_sdio_probe(struct sdio_func *func,
120 const struct sdio_device_id *id)
124 struct wilc_sdio *sdio_priv;
126 sdio_priv = kzalloc(sizeof(*sdio_priv), GFP_KERNEL);
130 ret = wilc_cfg80211_init(&wilc, &func->dev, WILC_HIF_SDIO,
137 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
138 struct device_node *np = func->card->dev.of_node;
139 int irq_num = of_irq_get(np, 0);
142 wilc->dev_irq_num = irq_num;
143 sdio_priv->irq_gpio = true;
147 sdio_set_drvdata(func, wilc);
148 wilc->bus_data = sdio_priv;
149 wilc->dev = &func->dev;
151 wilc->rtc_clk = devm_clk_get(&func->card->dev, "rtc");
152 if (PTR_ERR_OR_ZERO(wilc->rtc_clk) == -EPROBE_DEFER) {
154 return -EPROBE_DEFER;
155 } else if (!IS_ERR(wilc->rtc_clk))
156 clk_prepare_enable(wilc->rtc_clk);
158 dev_info(&func->dev, "Driver Initializing success\n");
162 static void wilc_sdio_remove(struct sdio_func *func)
164 struct wilc *wilc = sdio_get_drvdata(func);
166 if (!IS_ERR(wilc->rtc_clk))
167 clk_disable_unprepare(wilc->rtc_clk);
169 wilc_netdev_cleanup(wilc);
172 static int wilc_sdio_reset(struct wilc *wilc)
174 struct sdio_cmd52 cmd;
176 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
181 cmd.address = SDIO_CCCR_ABORT;
182 cmd.data = WILC_SDIO_CCCR_ABORT_RESET;
183 ret = wilc_sdio_cmd52(wilc, &cmd);
185 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
191 static int wilc_sdio_suspend(struct device *dev)
193 struct sdio_func *func = dev_to_sdio_func(dev);
194 struct wilc *wilc = sdio_get_drvdata(func);
197 dev_info(dev, "sdio suspend\n");
200 if (!IS_ERR(wilc->rtc_clk))
201 clk_disable_unprepare(wilc->rtc_clk);
203 if (wilc->suspend_event) {
204 host_sleep_notify(wilc);
205 chip_allow_sleep(wilc);
208 ret = wilc_sdio_reset(wilc);
210 dev_err(&func->dev, "Fail reset sdio\n");
213 sdio_claim_host(func);
218 static int wilc_sdio_enable_interrupt(struct wilc *dev)
220 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
223 sdio_claim_host(func);
224 ret = sdio_claim_irq(func, wilc_sdio_interrupt);
225 sdio_release_host(func);
228 dev_err(&func->dev, "can't claim sdio_irq, err(%d)\n", ret);
234 static void wilc_sdio_disable_interrupt(struct wilc *dev)
236 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
239 sdio_claim_host(func);
240 ret = sdio_release_irq(func);
242 dev_err(&func->dev, "can't release sdio_irq, err(%d)\n", ret);
243 sdio_release_host(func);
246 /********************************************
250 ********************************************/
252 static int wilc_sdio_set_func0_csa_address(struct wilc *wilc, u32 adr)
254 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
255 struct sdio_cmd52 cmd;
264 cmd.address = WILC_SDIO_FBR_CSA_REG;
266 ret = wilc_sdio_cmd52(wilc, &cmd);
268 dev_err(&func->dev, "Failed cmd52, set %04x data...\n",
273 cmd.address = WILC_SDIO_FBR_CSA_REG + 1;
274 cmd.data = (u8)(adr >> 8);
275 ret = wilc_sdio_cmd52(wilc, &cmd);
277 dev_err(&func->dev, "Failed cmd52, set %04x data...\n",
282 cmd.address = WILC_SDIO_FBR_CSA_REG + 2;
283 cmd.data = (u8)(adr >> 16);
284 ret = wilc_sdio_cmd52(wilc, &cmd);
286 dev_err(&func->dev, "Failed cmd52, set %04x data...\n",
294 static int wilc_sdio_set_block_size(struct wilc *wilc, u8 func_num,
297 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
298 struct sdio_cmd52 cmd;
304 cmd.address = SDIO_FBR_BASE(func_num) + SDIO_CCCR_BLKSIZE;
305 cmd.data = (u8)block_size;
306 ret = wilc_sdio_cmd52(wilc, &cmd);
308 dev_err(&func->dev, "Failed cmd52, set %04x data...\n",
313 cmd.address = SDIO_FBR_BASE(func_num) + SDIO_CCCR_BLKSIZE + 1;
314 cmd.data = (u8)(block_size >> 8);
315 ret = wilc_sdio_cmd52(wilc, &cmd);
317 dev_err(&func->dev, "Failed cmd52, set %04x data...\n",
325 /********************************************
329 ********************************************/
330 static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
332 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
333 struct wilc_sdio *sdio_priv = wilc->bus_data;
338 if (addr >= 0xf0 && addr <= 0xff) { /* only vendor specific registers */
339 struct sdio_cmd52 cmd;
346 ret = wilc_sdio_cmd52(wilc, &cmd);
349 "Failed cmd 52, read reg (%08x) ...\n", addr);
351 struct sdio_cmd53 cmd;
354 * set the AHB address
356 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
362 cmd.address = WILC_SDIO_FBR_DATA_REG;
366 cmd.buffer = (u8 *)&data;
367 cmd.block_size = sdio_priv->block_size;
368 ret = wilc_sdio_cmd53(wilc, &cmd);
371 "Failed cmd53, write reg (%08x)...\n", addr);
377 static int wilc_sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
379 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
380 struct wilc_sdio *sdio_priv = wilc->bus_data;
381 u32 block_size = sdio_priv->block_size;
382 struct sdio_cmd53 cmd;
383 int nblk, nleft, ret;
391 cmd.address = WILC_SDIO_FBR_DATA_REG;
397 cmd.address = WILC_SDIO_F1_DATA_REG;
400 size = ALIGN(size, 4);
401 nblk = size / block_size;
402 nleft = size % block_size;
409 cmd.block_size = block_size;
411 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
415 ret = wilc_sdio_cmd53(wilc, &cmd);
418 "Failed cmd53 [%x], block send...\n", addr);
422 addr += nblk * block_size;
423 buf += nblk * block_size;
432 cmd.block_size = block_size;
435 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
439 ret = wilc_sdio_cmd53(wilc, &cmd);
442 "Failed cmd53 [%x], bytes send...\n", addr);
450 static int wilc_sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
452 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
453 struct wilc_sdio *sdio_priv = wilc->bus_data;
456 if (addr >= 0xf0 && addr <= 0xff) { /* only vendor specific registers */
457 struct sdio_cmd52 cmd;
463 ret = wilc_sdio_cmd52(wilc, &cmd);
466 "Failed cmd 52, read reg (%08x) ...\n", addr);
471 struct sdio_cmd53 cmd;
473 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
479 cmd.address = WILC_SDIO_FBR_DATA_REG;
483 cmd.buffer = (u8 *)data;
485 cmd.block_size = sdio_priv->block_size;
486 ret = wilc_sdio_cmd53(wilc, &cmd);
489 "Failed cmd53, read reg (%08x)...\n", addr);
498 static int wilc_sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
500 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
501 struct wilc_sdio *sdio_priv = wilc->bus_data;
502 u32 block_size = sdio_priv->block_size;
503 struct sdio_cmd53 cmd;
504 int nblk, nleft, ret;
512 cmd.address = WILC_SDIO_FBR_DATA_REG;
518 cmd.address = WILC_SDIO_F1_DATA_REG;
521 size = ALIGN(size, 4);
522 nblk = size / block_size;
523 nleft = size % block_size;
530 cmd.block_size = block_size;
532 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
536 ret = wilc_sdio_cmd53(wilc, &cmd);
539 "Failed cmd53 [%x], block read...\n", addr);
543 addr += nblk * block_size;
544 buf += nblk * block_size;
545 } /* if (nblk > 0) */
553 cmd.block_size = block_size;
556 ret = wilc_sdio_set_func0_csa_address(wilc, addr);
560 ret = wilc_sdio_cmd53(wilc, &cmd);
563 "Failed cmd53 [%x], bytes read...\n", addr);
571 /********************************************
575 ********************************************/
577 static int wilc_sdio_deinit(struct wilc *wilc)
582 static int wilc_sdio_init(struct wilc *wilc, bool resume)
584 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
585 struct wilc_sdio *sdio_priv = wilc->bus_data;
586 struct sdio_cmd52 cmd;
591 * function 0 csa enable
596 cmd.address = SDIO_FBR_BASE(func->num);
597 cmd.data = SDIO_FBR_ENABLE_CSA;
598 ret = wilc_sdio_cmd52(wilc, &cmd);
600 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
605 * function 0 block size
607 ret = wilc_sdio_set_block_size(wilc, 0, WILC_SDIO_BLOCK_SIZE);
609 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
612 sdio_priv->block_size = WILC_SDIO_BLOCK_SIZE;
620 cmd.address = SDIO_CCCR_IOEx;
621 cmd.data = WILC_SDIO_CCCR_IO_EN_FUNC1;
622 ret = wilc_sdio_cmd52(wilc, &cmd);
625 "Fail cmd 52, set IOE register...\n");
630 * make sure func 1 is up
635 cmd.address = SDIO_CCCR_IORx;
639 ret = wilc_sdio_cmd52(wilc, &cmd);
642 "Fail cmd 52, get IOR register...\n");
645 if (cmd.data == WILC_SDIO_CCCR_IO_EN_FUNC1)
650 dev_err(&func->dev, "Fail func 1 is not ready...\n");
655 * func 1 is ready, set func 1 block size
657 ret = wilc_sdio_set_block_size(wilc, 1, WILC_SDIO_BLOCK_SIZE);
659 dev_err(&func->dev, "Fail set func 1 block size...\n");
664 * func 1 interrupt enable
669 cmd.address = SDIO_CCCR_IENx;
670 cmd.data = WILC_SDIO_CCCR_IEN_MASTER | WILC_SDIO_CCCR_IEN_FUNC1;
671 ret = wilc_sdio_cmd52(wilc, &cmd);
673 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
678 * make sure can read back chip id correctly
683 ret = wilc_sdio_read_reg(wilc, WILC_CHIPID, &chipid);
685 dev_err(&func->dev, "Fail cmd read chip id...\n");
688 dev_err(&func->dev, "chipid (%08x)\n", chipid);
689 rev = FIELD_GET(WILC_CHIP_REV_FIELD, chipid);
690 if (rev > FIELD_GET(WILC_CHIP_REV_FIELD, WILC_1000_BASE_ID_2A))
691 sdio_priv->has_thrpt_enh3 = 1;
693 sdio_priv->has_thrpt_enh3 = 0;
694 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
695 sdio_priv->has_thrpt_enh3);
701 static int wilc_sdio_read_size(struct wilc *wilc, u32 *size)
704 struct sdio_cmd52 cmd;
707 * Read DMA count in words
712 cmd.address = WILC_SDIO_INTERRUPT_DATA_SZ_REG;
714 wilc_sdio_cmd52(wilc, &cmd);
717 cmd.address = WILC_SDIO_INTERRUPT_DATA_SZ_REG + 1;
719 wilc_sdio_cmd52(wilc, &cmd);
720 tmp |= (cmd.data << 8);
726 static int wilc_sdio_read_int(struct wilc *wilc, u32 *int_status)
728 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
729 struct wilc_sdio *sdio_priv = wilc->bus_data;
732 struct sdio_cmd52 cmd;
734 wilc_sdio_read_size(wilc, &tmp);
739 if (!sdio_priv->irq_gpio) {
741 cmd.address = WILC_SDIO_EXT_IRQ_FLAG_REG;
744 cmd.address = WILC_SDIO_IRQ_FLAG_REG;
749 wilc_sdio_cmd52(wilc, &cmd);
750 irq_flags = cmd.data;
751 tmp |= FIELD_PREP(IRG_FLAGS_MASK, cmd.data);
753 if (FIELD_GET(UNHANDLED_IRQ_MASK, irq_flags))
754 dev_err(&func->dev, "Unexpected interrupt (1) int=%lx\n",
755 FIELD_GET(UNHANDLED_IRQ_MASK, irq_flags));
762 static int wilc_sdio_clear_int_ext(struct wilc *wilc, u32 val)
764 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
765 struct wilc_sdio *sdio_priv = wilc->bus_data;
769 if (sdio_priv->has_thrpt_enh3) {
772 if (sdio_priv->irq_gpio)
773 reg = val & (BIT(MAX_NUM_INT) - 1);
775 /* select VMM table 0 */
776 if (val & SEL_VMM_TBL0)
778 /* select VMM table 1 */
779 if (val & SEL_VMM_TBL1)
785 struct sdio_cmd52 cmd;
790 cmd.address = WILC_SDIO_IRQ_CLEAR_FLAG_REG;
793 ret = wilc_sdio_cmd52(wilc, &cmd);
796 "Failed cmd52, set (%02x) data (%d) ...\n",
797 cmd.address, __LINE__);
803 if (sdio_priv->irq_gpio) {
804 /* has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
806 * Cannot clear multiple interrupts.
807 * Must clear each interrupt individually.
812 flags = val & (BIT(MAX_NUM_INT) - 1);
813 for (i = 0; i < NUM_INT_EXT && flags; i++) {
814 if (flags & BIT(i)) {
815 struct sdio_cmd52 cmd;
820 cmd.address = WILC_SDIO_IRQ_CLEAR_FLAG_REG;
823 ret = wilc_sdio_cmd52(wilc, &cmd);
826 "Failed cmd52, set (%02x) data (%d) ...\n",
827 cmd.address, __LINE__);
834 for (i = NUM_INT_EXT; i < MAX_NUM_INT && flags; i++) {
835 if (flags & BIT(i)) {
837 "Unexpected interrupt cleared %d...\n",
845 /* select VMM table 0 */
846 if (val & SEL_VMM_TBL0)
848 /* select VMM table 1 */
849 if (val & SEL_VMM_TBL1)
856 struct sdio_cmd52 cmd;
861 cmd.address = WILC_SDIO_VMM_TBL_CTRL_REG;
863 ret = wilc_sdio_cmd52(wilc, &cmd);
866 "Failed cmd52, set (%02x) data (%d) ...\n",
867 cmd.address, __LINE__);
874 static int wilc_sdio_sync_ext(struct wilc *wilc, int nint)
876 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
877 struct wilc_sdio *sdio_priv = wilc->bus_data;
880 if (nint > MAX_NUM_INT) {
881 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
886 * Disable power sequencer
888 if (wilc_sdio_read_reg(wilc, WILC_MISC, ®)) {
889 dev_err(&func->dev, "Failed read misc reg...\n");
894 if (wilc_sdio_write_reg(wilc, WILC_MISC, reg)) {
895 dev_err(&func->dev, "Failed write misc reg...\n");
899 if (sdio_priv->irq_gpio) {
904 * interrupt pin mux select
906 ret = wilc_sdio_read_reg(wilc, WILC_PIN_MUX_0, ®);
908 dev_err(&func->dev, "Failed read reg (%08x)...\n",
913 ret = wilc_sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
915 dev_err(&func->dev, "Failed write reg (%08x)...\n",
923 ret = wilc_sdio_read_reg(wilc, WILC_INTR_ENABLE, ®);
925 dev_err(&func->dev, "Failed read reg (%08x)...\n",
930 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
931 reg |= BIT((27 + i));
932 ret = wilc_sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
934 dev_err(&func->dev, "Failed write reg (%08x)...\n",
939 ret = wilc_sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
942 "Failed read reg (%08x)...\n",
947 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
950 ret = wilc_sdio_read_reg(wilc, WILC_INTR2_ENABLE, ®);
953 "Failed write reg (%08x)...\n",
962 /* Global sdio HIF function table */
963 static const struct wilc_hif_func wilc_hif_sdio = {
964 .hif_init = wilc_sdio_init,
965 .hif_deinit = wilc_sdio_deinit,
966 .hif_read_reg = wilc_sdio_read_reg,
967 .hif_write_reg = wilc_sdio_write_reg,
968 .hif_block_rx = wilc_sdio_read,
969 .hif_block_tx = wilc_sdio_write,
970 .hif_read_int = wilc_sdio_read_int,
971 .hif_clear_int_ext = wilc_sdio_clear_int_ext,
972 .hif_read_size = wilc_sdio_read_size,
973 .hif_block_tx_ext = wilc_sdio_write,
974 .hif_block_rx_ext = wilc_sdio_read,
975 .hif_sync_ext = wilc_sdio_sync_ext,
976 .enable_interrupt = wilc_sdio_enable_interrupt,
977 .disable_interrupt = wilc_sdio_disable_interrupt,
980 static int wilc_sdio_resume(struct device *dev)
982 struct sdio_func *func = dev_to_sdio_func(dev);
983 struct wilc *wilc = sdio_get_drvdata(func);
985 dev_info(dev, "sdio resume\n");
986 sdio_release_host(func);
988 wilc_sdio_init(wilc, true);
990 if (wilc->suspend_event)
991 host_wakeup_notify(wilc);
993 chip_allow_sleep(wilc);
998 static const struct of_device_id wilc_of_match[] = {
999 { .compatible = "microchip,wilc1000", },
1002 MODULE_DEVICE_TABLE(of, wilc_of_match);
1004 static const struct dev_pm_ops wilc_sdio_pm_ops = {
1005 .suspend = wilc_sdio_suspend,
1006 .resume = wilc_sdio_resume,
1009 static struct sdio_driver wilc_sdio_driver = {
1010 .name = SDIO_MODALIAS,
1011 .id_table = wilc_sdio_ids,
1012 .probe = wilc_sdio_probe,
1013 .remove = wilc_sdio_remove,
1015 .pm = &wilc_sdio_pm_ops,
1016 .of_match_table = wilc_of_match,
1019 module_driver(wilc_sdio_driver,
1020 sdio_register_driver,
1021 sdio_unregister_driver);
1022 MODULE_LICENSE("GPL");