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>
10 #include <linux/mmc/sdio.h>
11 #include <linux/of_irq.h>
16 #define SDIO_MODALIAS "wilc1000_sdio"
18 #define SDIO_VENDOR_ID_WILC 0x0296
19 #define SDIO_DEVICE_ID_WILC 0x5347
21 static const struct sdio_device_id wilc_sdio_ids
[] = {
22 { SDIO_DEVICE(SDIO_VENDOR_ID_WILC
, SDIO_DEVICE_ID_WILC
) },
26 #define WILC_SDIO_BLOCK_SIZE 512
53 static const struct wilc_hif_func wilc_hif_sdio
;
55 static void wilc_sdio_interrupt(struct sdio_func
*func
)
57 sdio_release_host(func
);
58 wilc_handle_isr(sdio_get_drvdata(func
));
59 sdio_claim_host(func
);
62 static int wilc_sdio_cmd52(struct wilc
*wilc
, struct sdio_cmd52
*cmd
)
64 struct sdio_func
*func
= container_of(wilc
->dev
, struct sdio_func
, dev
);
68 sdio_claim_host(func
);
70 func
->num
= cmd
->function
;
71 if (cmd
->read_write
) { /* write */
73 sdio_writeb(func
, cmd
->data
, cmd
->address
, &ret
);
74 data
= sdio_readb(func
, cmd
->address
, &ret
);
77 sdio_writeb(func
, cmd
->data
, cmd
->address
, &ret
);
80 data
= sdio_readb(func
, cmd
->address
, &ret
);
84 sdio_release_host(func
);
87 dev_err(&func
->dev
, "%s..failed, err(%d)\n", __func__
, ret
);
91 static int wilc_sdio_cmd53(struct wilc
*wilc
, struct sdio_cmd53
*cmd
)
93 struct sdio_func
*func
= container_of(wilc
->dev
, struct sdio_func
, dev
);
96 sdio_claim_host(func
);
98 func
->num
= cmd
->function
;
99 func
->cur_blksize
= cmd
->block_size
;
101 size
= cmd
->count
* cmd
->block_size
;
105 if (cmd
->read_write
) { /* write */
106 ret
= sdio_memcpy_toio(func
, cmd
->address
,
107 (void *)cmd
->buffer
, size
);
109 ret
= sdio_memcpy_fromio(func
, (void *)cmd
->buffer
,
113 sdio_release_host(func
);
116 dev_err(&func
->dev
, "%s..failed, err(%d)\n", __func__
, ret
);
121 static int wilc_sdio_probe(struct sdio_func
*func
,
122 const struct sdio_device_id
*id
)
126 struct wilc_sdio
*sdio_priv
;
128 sdio_priv
= kzalloc(sizeof(*sdio_priv
), GFP_KERNEL
);
132 ret
= wilc_cfg80211_init(&wilc
, &func
->dev
, WILC_HIF_SDIO
,
139 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR
)) {
140 struct device_node
*np
= func
->card
->dev
.of_node
;
141 int irq_num
= of_irq_get(np
, 0);
144 wilc
->dev_irq_num
= irq_num
;
145 sdio_priv
->irq_gpio
= true;
149 sdio_set_drvdata(func
, wilc
);
150 wilc
->bus_data
= sdio_priv
;
151 wilc
->dev
= &func
->dev
;
153 wilc
->rtc_clk
= devm_clk_get(&func
->card
->dev
, "rtc");
154 if (PTR_ERR_OR_ZERO(wilc
->rtc_clk
) == -EPROBE_DEFER
)
155 return -EPROBE_DEFER
;
156 else if (!IS_ERR(wilc
->rtc_clk
))
157 clk_prepare_enable(wilc
->rtc_clk
);
159 dev_info(&func
->dev
, "Driver Initializing success\n");
163 static void wilc_sdio_remove(struct sdio_func
*func
)
165 struct wilc
*wilc
= sdio_get_drvdata(func
);
167 if (!IS_ERR(wilc
->rtc_clk
))
168 clk_disable_unprepare(wilc
->rtc_clk
);
170 wilc_netdev_cleanup(wilc
);
173 static int wilc_sdio_reset(struct wilc
*wilc
)
175 struct sdio_cmd52 cmd
;
177 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
182 cmd
.address
= SDIO_CCCR_ABORT
;
183 cmd
.data
= WILC_SDIO_CCCR_ABORT_RESET
;
184 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
186 dev_err(&func
->dev
, "Fail cmd 52, reset cmd ...\n");
192 static int wilc_sdio_suspend(struct device
*dev
)
194 struct sdio_func
*func
= dev_to_sdio_func(dev
);
195 struct wilc
*wilc
= sdio_get_drvdata(func
);
198 dev_info(dev
, "sdio suspend\n");
201 if (!IS_ERR(wilc
->rtc_clk
))
202 clk_disable_unprepare(wilc
->rtc_clk
);
204 if (wilc
->suspend_event
) {
205 host_sleep_notify(wilc
);
206 chip_allow_sleep(wilc
);
209 ret
= wilc_sdio_reset(wilc
);
211 dev_err(&func
->dev
, "Fail reset sdio\n");
214 sdio_claim_host(func
);
219 static int wilc_sdio_enable_interrupt(struct wilc
*dev
)
221 struct sdio_func
*func
= container_of(dev
->dev
, struct sdio_func
, dev
);
224 sdio_claim_host(func
);
225 ret
= sdio_claim_irq(func
, wilc_sdio_interrupt
);
226 sdio_release_host(func
);
229 dev_err(&func
->dev
, "can't claim sdio_irq, err(%d)\n", ret
);
235 static void wilc_sdio_disable_interrupt(struct wilc
*dev
)
237 struct sdio_func
*func
= container_of(dev
->dev
, struct sdio_func
, dev
);
240 sdio_claim_host(func
);
241 ret
= sdio_release_irq(func
);
243 dev_err(&func
->dev
, "can't release sdio_irq, err(%d)\n", ret
);
244 sdio_release_host(func
);
247 /********************************************
251 ********************************************/
253 static int wilc_sdio_set_func0_csa_address(struct wilc
*wilc
, u32 adr
)
255 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
256 struct sdio_cmd52 cmd
;
265 cmd
.address
= WILC_SDIO_FBR_CSA_REG
;
267 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
269 dev_err(&func
->dev
, "Failed cmd52, set %04x data...\n",
274 cmd
.address
= WILC_SDIO_FBR_CSA_REG
+ 1;
275 cmd
.data
= (u8
)(adr
>> 8);
276 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
278 dev_err(&func
->dev
, "Failed cmd52, set %04x data...\n",
283 cmd
.address
= WILC_SDIO_FBR_CSA_REG
+ 2;
284 cmd
.data
= (u8
)(adr
>> 16);
285 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
287 dev_err(&func
->dev
, "Failed cmd52, set %04x data...\n",
295 static int wilc_sdio_set_block_size(struct wilc
*wilc
, u8 func_num
,
298 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
299 struct sdio_cmd52 cmd
;
305 cmd
.address
= SDIO_FBR_BASE(func_num
) + SDIO_CCCR_BLKSIZE
;
306 cmd
.data
= (u8
)block_size
;
307 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
309 dev_err(&func
->dev
, "Failed cmd52, set %04x data...\n",
314 cmd
.address
= SDIO_FBR_BASE(func_num
) + SDIO_CCCR_BLKSIZE
+ 1;
315 cmd
.data
= (u8
)(block_size
>> 8);
316 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
318 dev_err(&func
->dev
, "Failed cmd52, set %04x data...\n",
326 /********************************************
330 ********************************************/
331 static int wilc_sdio_write_reg(struct wilc
*wilc
, u32 addr
, u32 data
)
333 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
334 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
339 if (addr
>= 0xf0 && addr
<= 0xff) { /* only vendor specific registers */
340 struct sdio_cmd52 cmd
;
347 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
350 "Failed cmd 52, read reg (%08x) ...\n", addr
);
352 struct sdio_cmd53 cmd
;
355 * set the AHB address
357 ret
= wilc_sdio_set_func0_csa_address(wilc
, addr
);
363 cmd
.address
= WILC_SDIO_FBR_DATA_REG
;
367 cmd
.buffer
= (u8
*)&data
;
368 cmd
.block_size
= sdio_priv
->block_size
;
369 ret
= wilc_sdio_cmd53(wilc
, &cmd
);
372 "Failed cmd53, write reg (%08x)...\n", addr
);
378 static int wilc_sdio_write(struct wilc
*wilc
, u32 addr
, u8
*buf
, u32 size
)
380 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
381 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
382 u32 block_size
= sdio_priv
->block_size
;
383 struct sdio_cmd53 cmd
;
384 int nblk
, nleft
, ret
;
392 cmd
.address
= WILC_SDIO_FBR_DATA_REG
;
398 cmd
.address
= WILC_SDIO_F1_DATA_REG
;
401 size
= ALIGN(size
, 4);
402 nblk
= size
/ block_size
;
403 nleft
= size
% block_size
;
410 cmd
.block_size
= block_size
;
412 ret
= wilc_sdio_set_func0_csa_address(wilc
, addr
);
416 ret
= wilc_sdio_cmd53(wilc
, &cmd
);
419 "Failed cmd53 [%x], block send...\n", addr
);
423 addr
+= nblk
* block_size
;
424 buf
+= nblk
* block_size
;
433 cmd
.block_size
= block_size
;
436 ret
= wilc_sdio_set_func0_csa_address(wilc
, addr
);
440 ret
= wilc_sdio_cmd53(wilc
, &cmd
);
443 "Failed cmd53 [%x], bytes send...\n", addr
);
451 static int wilc_sdio_read_reg(struct wilc
*wilc
, u32 addr
, u32
*data
)
453 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
454 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
457 if (addr
>= 0xf0 && addr
<= 0xff) { /* only vendor specific registers */
458 struct sdio_cmd52 cmd
;
464 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
467 "Failed cmd 52, read reg (%08x) ...\n", addr
);
472 struct sdio_cmd53 cmd
;
474 ret
= wilc_sdio_set_func0_csa_address(wilc
, addr
);
480 cmd
.address
= WILC_SDIO_FBR_DATA_REG
;
484 cmd
.buffer
= (u8
*)data
;
486 cmd
.block_size
= sdio_priv
->block_size
;
487 ret
= wilc_sdio_cmd53(wilc
, &cmd
);
490 "Failed cmd53, read reg (%08x)...\n", addr
);
499 static int wilc_sdio_read(struct wilc
*wilc
, u32 addr
, u8
*buf
, u32 size
)
501 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
502 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
503 u32 block_size
= sdio_priv
->block_size
;
504 struct sdio_cmd53 cmd
;
505 int nblk
, nleft
, ret
;
513 cmd
.address
= WILC_SDIO_FBR_DATA_REG
;
519 cmd
.address
= WILC_SDIO_F1_DATA_REG
;
522 size
= ALIGN(size
, 4);
523 nblk
= size
/ block_size
;
524 nleft
= size
% block_size
;
531 cmd
.block_size
= block_size
;
533 ret
= wilc_sdio_set_func0_csa_address(wilc
, addr
);
537 ret
= wilc_sdio_cmd53(wilc
, &cmd
);
540 "Failed cmd53 [%x], block read...\n", addr
);
544 addr
+= nblk
* block_size
;
545 buf
+= nblk
* block_size
;
546 } /* if (nblk > 0) */
554 cmd
.block_size
= block_size
;
557 ret
= wilc_sdio_set_func0_csa_address(wilc
, addr
);
561 ret
= wilc_sdio_cmd53(wilc
, &cmd
);
564 "Failed cmd53 [%x], bytes read...\n", addr
);
572 /********************************************
576 ********************************************/
578 static int wilc_sdio_deinit(struct wilc
*wilc
)
583 static int wilc_sdio_init(struct wilc
*wilc
, bool resume
)
585 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
586 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
587 struct sdio_cmd52 cmd
;
592 * function 0 csa enable
597 cmd
.address
= SDIO_FBR_BASE(func
->num
);
598 cmd
.data
= SDIO_FBR_ENABLE_CSA
;
599 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
601 dev_err(&func
->dev
, "Fail cmd 52, enable csa...\n");
606 * function 0 block size
608 ret
= wilc_sdio_set_block_size(wilc
, 0, WILC_SDIO_BLOCK_SIZE
);
610 dev_err(&func
->dev
, "Fail cmd 52, set func 0 block size...\n");
613 sdio_priv
->block_size
= WILC_SDIO_BLOCK_SIZE
;
621 cmd
.address
= SDIO_CCCR_IOEx
;
622 cmd
.data
= WILC_SDIO_CCCR_IO_EN_FUNC1
;
623 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
626 "Fail cmd 52, set IOE register...\n");
631 * make sure func 1 is up
636 cmd
.address
= SDIO_CCCR_IORx
;
640 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
643 "Fail cmd 52, get IOR register...\n");
646 if (cmd
.data
== WILC_SDIO_CCCR_IO_EN_FUNC1
)
651 dev_err(&func
->dev
, "Fail func 1 is not ready...\n");
656 * func 1 is ready, set func 1 block size
658 ret
= wilc_sdio_set_block_size(wilc
, 1, WILC_SDIO_BLOCK_SIZE
);
660 dev_err(&func
->dev
, "Fail set func 1 block size...\n");
665 * func 1 interrupt enable
670 cmd
.address
= SDIO_CCCR_IENx
;
671 cmd
.data
= WILC_SDIO_CCCR_IEN_MASTER
| WILC_SDIO_CCCR_IEN_FUNC1
;
672 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
674 dev_err(&func
->dev
, "Fail cmd 52, set IEN register...\n");
679 * make sure can read back chip id correctly
684 ret
= wilc_sdio_read_reg(wilc
, WILC_CHIPID
, &chipid
);
686 dev_err(&func
->dev
, "Fail cmd read chip id...\n");
689 dev_err(&func
->dev
, "chipid (%08x)\n", chipid
);
690 rev
= FIELD_GET(WILC_CHIP_REV_FIELD
, chipid
);
691 if (rev
> FIELD_GET(WILC_CHIP_REV_FIELD
, WILC_1000_BASE_ID_2A
))
692 sdio_priv
->has_thrpt_enh3
= 1;
694 sdio_priv
->has_thrpt_enh3
= 0;
695 dev_info(&func
->dev
, "has_thrpt_enh3 = %d...\n",
696 sdio_priv
->has_thrpt_enh3
);
702 static int wilc_sdio_read_size(struct wilc
*wilc
, u32
*size
)
705 struct sdio_cmd52 cmd
;
708 * Read DMA count in words
713 cmd
.address
= WILC_SDIO_INTERRUPT_DATA_SZ_REG
;
715 wilc_sdio_cmd52(wilc
, &cmd
);
718 cmd
.address
= WILC_SDIO_INTERRUPT_DATA_SZ_REG
+ 1;
720 wilc_sdio_cmd52(wilc
, &cmd
);
721 tmp
|= (cmd
.data
<< 8);
727 static int wilc_sdio_read_int(struct wilc
*wilc
, u32
*int_status
)
729 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
730 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
733 struct sdio_cmd52 cmd
;
735 wilc_sdio_read_size(wilc
, &tmp
);
740 if (!sdio_priv
->irq_gpio
) {
742 cmd
.address
= WILC_SDIO_EXT_IRQ_FLAG_REG
;
745 cmd
.address
= WILC_SDIO_IRQ_FLAG_REG
;
750 wilc_sdio_cmd52(wilc
, &cmd
);
751 irq_flags
= cmd
.data
;
752 tmp
|= FIELD_PREP(IRG_FLAGS_MASK
, cmd
.data
);
754 if (FIELD_GET(UNHANDLED_IRQ_MASK
, irq_flags
))
755 dev_err(&func
->dev
, "Unexpected interrupt (1) int=%lx\n",
756 FIELD_GET(UNHANDLED_IRQ_MASK
, irq_flags
));
763 static int wilc_sdio_clear_int_ext(struct wilc
*wilc
, u32 val
)
765 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
766 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
770 if (sdio_priv
->has_thrpt_enh3
) {
773 if (sdio_priv
->irq_gpio
)
774 reg
= val
& (BIT(MAX_NUM_INT
) - 1);
776 /* select VMM table 0 */
777 if (val
& SEL_VMM_TBL0
)
779 /* select VMM table 1 */
780 if (val
& SEL_VMM_TBL1
)
786 struct sdio_cmd52 cmd
;
791 cmd
.address
= WILC_SDIO_IRQ_CLEAR_FLAG_REG
;
794 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
797 "Failed cmd52, set (%02x) data (%d) ...\n",
798 cmd
.address
, __LINE__
);
804 if (sdio_priv
->irq_gpio
) {
805 /* has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
807 * Cannot clear multiple interrupts.
808 * Must clear each interrupt individually.
813 flags
= val
& (BIT(MAX_NUM_INT
) - 1);
814 for (i
= 0; i
< NUM_INT_EXT
&& flags
; i
++) {
815 if (flags
& BIT(i
)) {
816 struct sdio_cmd52 cmd
;
821 cmd
.address
= WILC_SDIO_IRQ_CLEAR_FLAG_REG
;
824 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
827 "Failed cmd52, set (%02x) data (%d) ...\n",
828 cmd
.address
, __LINE__
);
835 for (i
= NUM_INT_EXT
; i
< MAX_NUM_INT
&& flags
; i
++) {
836 if (flags
& BIT(i
)) {
838 "Unexpected interrupt cleared %d...\n",
846 /* select VMM table 0 */
847 if (val
& SEL_VMM_TBL0
)
849 /* select VMM table 1 */
850 if (val
& SEL_VMM_TBL1
)
857 struct sdio_cmd52 cmd
;
862 cmd
.address
= WILC_SDIO_VMM_TBL_CTRL_REG
;
864 ret
= wilc_sdio_cmd52(wilc
, &cmd
);
867 "Failed cmd52, set (%02x) data (%d) ...\n",
868 cmd
.address
, __LINE__
);
875 static int wilc_sdio_sync_ext(struct wilc
*wilc
, int nint
)
877 struct sdio_func
*func
= dev_to_sdio_func(wilc
->dev
);
878 struct wilc_sdio
*sdio_priv
= wilc
->bus_data
;
881 if (nint
> MAX_NUM_INT
) {
882 dev_err(&func
->dev
, "Too many interrupts (%d)...\n", nint
);
887 * Disable power sequencer
889 if (wilc_sdio_read_reg(wilc
, WILC_MISC
, ®
)) {
890 dev_err(&func
->dev
, "Failed read misc reg...\n");
895 if (wilc_sdio_write_reg(wilc
, WILC_MISC
, reg
)) {
896 dev_err(&func
->dev
, "Failed write misc reg...\n");
900 if (sdio_priv
->irq_gpio
) {
905 * interrupt pin mux select
907 ret
= wilc_sdio_read_reg(wilc
, WILC_PIN_MUX_0
, ®
);
909 dev_err(&func
->dev
, "Failed read reg (%08x)...\n",
914 ret
= wilc_sdio_write_reg(wilc
, WILC_PIN_MUX_0
, reg
);
916 dev_err(&func
->dev
, "Failed write reg (%08x)...\n",
924 ret
= wilc_sdio_read_reg(wilc
, WILC_INTR_ENABLE
, ®
);
926 dev_err(&func
->dev
, "Failed read reg (%08x)...\n",
931 for (i
= 0; (i
< 5) && (nint
> 0); i
++, nint
--)
932 reg
|= BIT((27 + i
));
933 ret
= wilc_sdio_write_reg(wilc
, WILC_INTR_ENABLE
, reg
);
935 dev_err(&func
->dev
, "Failed write reg (%08x)...\n",
940 ret
= wilc_sdio_read_reg(wilc
, WILC_INTR2_ENABLE
, ®
);
943 "Failed read reg (%08x)...\n",
948 for (i
= 0; (i
< 3) && (nint
> 0); i
++, nint
--)
951 ret
= wilc_sdio_read_reg(wilc
, WILC_INTR2_ENABLE
, ®
);
954 "Failed write reg (%08x)...\n",
963 /* Global sdio HIF function table */
964 static const struct wilc_hif_func wilc_hif_sdio
= {
965 .hif_init
= wilc_sdio_init
,
966 .hif_deinit
= wilc_sdio_deinit
,
967 .hif_read_reg
= wilc_sdio_read_reg
,
968 .hif_write_reg
= wilc_sdio_write_reg
,
969 .hif_block_rx
= wilc_sdio_read
,
970 .hif_block_tx
= wilc_sdio_write
,
971 .hif_read_int
= wilc_sdio_read_int
,
972 .hif_clear_int_ext
= wilc_sdio_clear_int_ext
,
973 .hif_read_size
= wilc_sdio_read_size
,
974 .hif_block_tx_ext
= wilc_sdio_write
,
975 .hif_block_rx_ext
= wilc_sdio_read
,
976 .hif_sync_ext
= wilc_sdio_sync_ext
,
977 .enable_interrupt
= wilc_sdio_enable_interrupt
,
978 .disable_interrupt
= wilc_sdio_disable_interrupt
,
981 static int wilc_sdio_resume(struct device
*dev
)
983 struct sdio_func
*func
= dev_to_sdio_func(dev
);
984 struct wilc
*wilc
= sdio_get_drvdata(func
);
986 dev_info(dev
, "sdio resume\n");
987 sdio_release_host(func
);
989 wilc_sdio_init(wilc
, true);
991 if (wilc
->suspend_event
)
992 host_wakeup_notify(wilc
);
994 chip_allow_sleep(wilc
);
999 static const struct of_device_id wilc_of_match
[] = {
1000 { .compatible
= "microchip,wilc1000", },
1003 MODULE_DEVICE_TABLE(of
, wilc_of_match
);
1005 static const struct dev_pm_ops wilc_sdio_pm_ops
= {
1006 .suspend
= wilc_sdio_suspend
,
1007 .resume
= wilc_sdio_resume
,
1010 static struct sdio_driver wilc_sdio_driver
= {
1011 .name
= SDIO_MODALIAS
,
1012 .id_table
= wilc_sdio_ids
,
1013 .probe
= wilc_sdio_probe
,
1014 .remove
= wilc_sdio_remove
,
1016 .pm
= &wilc_sdio_pm_ops
,
1017 .of_match_table
= wilc_of_match
,
1020 module_driver(wilc_sdio_driver
,
1021 sdio_register_driver
,
1022 sdio_unregister_driver
);
1023 MODULE_LICENSE("GPL");