1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
8 #include <linux/spi/spi.h>
18 static const struct wilc_hif_func wilc_hif_spi
;
20 /********************************************
24 ********************************************/
26 static const u8 crc7_syndrome_table
[256] = {
27 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f,
28 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77,
29 0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26,
30 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e,
31 0x32, 0x3b, 0x20, 0x29, 0x16, 0x1f, 0x04, 0x0d,
32 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45,
33 0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14,
34 0x63, 0x6a, 0x71, 0x78, 0x47, 0x4e, 0x55, 0x5c,
35 0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b,
36 0x2c, 0x25, 0x3e, 0x37, 0x08, 0x01, 0x1a, 0x13,
37 0x7d, 0x74, 0x6f, 0x66, 0x59, 0x50, 0x4b, 0x42,
38 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03, 0x0a,
39 0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69,
40 0x1e, 0x17, 0x0c, 0x05, 0x3a, 0x33, 0x28, 0x21,
41 0x4f, 0x46, 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70,
42 0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38,
43 0x41, 0x48, 0x53, 0x5a, 0x65, 0x6c, 0x77, 0x7e,
44 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36,
45 0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67,
46 0x10, 0x19, 0x02, 0x0b, 0x34, 0x3d, 0x26, 0x2f,
47 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
48 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04,
49 0x6a, 0x63, 0x78, 0x71, 0x4e, 0x47, 0x5c, 0x55,
50 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f, 0x14, 0x1d,
51 0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a,
52 0x6d, 0x64, 0x7f, 0x76, 0x49, 0x40, 0x5b, 0x52,
53 0x3c, 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03,
54 0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b,
55 0x17, 0x1e, 0x05, 0x0c, 0x33, 0x3a, 0x21, 0x28,
56 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60,
57 0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38, 0x31,
58 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79
61 static u8
crc7_byte(u8 crc
, u8 data
)
63 return crc7_syndrome_table
[(crc
<< 1) ^ data
];
66 static u8
crc7(u8 crc
, const u8
*buffer
, u32 len
)
69 crc
= crc7_byte(crc
, *buffer
++);
73 /********************************************
75 * Spi protocol Function
77 ********************************************/
79 #define CMD_DMA_WRITE 0xc1
80 #define CMD_DMA_READ 0xc2
81 #define CMD_INTERNAL_WRITE 0xc3
82 #define CMD_INTERNAL_READ 0xc4
83 #define CMD_TERMINATE 0xc5
84 #define CMD_REPEAT 0xc6
85 #define CMD_DMA_EXT_WRITE 0xc7
86 #define CMD_DMA_EXT_READ 0xc8
87 #define CMD_SINGLE_WRITE 0xc9
88 #define CMD_SINGLE_READ 0xca
89 #define CMD_RESET 0xcf
91 #define DATA_PKT_SZ_256 256
92 #define DATA_PKT_SZ_512 512
93 #define DATA_PKT_SZ_1K 1024
94 #define DATA_PKT_SZ_4K (4 * 1024)
95 #define DATA_PKT_SZ_8K (8 * 1024)
96 #define DATA_PKT_SZ DATA_PKT_SZ_8K
100 static int wilc_bus_probe(struct spi_device
*spi
)
104 struct gpio_desc
*gpio
;
105 struct wilc_spi
*spi_priv
;
107 spi_priv
= kzalloc(sizeof(*spi_priv
), GFP_KERNEL
);
111 gpio
= gpiod_get(&spi
->dev
, "irq", GPIOD_IN
);
113 /* get the GPIO descriptor from hardcode GPIO number */
114 gpio
= gpio_to_desc(GPIO_NUM
);
116 dev_err(&spi
->dev
, "failed to get the irq gpio\n");
119 ret
= wilc_cfg80211_init(&wilc
, &spi
->dev
, WILC_HIF_SPI
, &wilc_hif_spi
);
125 spi_set_drvdata(spi
, wilc
);
126 wilc
->dev
= &spi
->dev
;
127 wilc
->bus_data
= spi_priv
;
128 wilc
->gpio_irq
= gpio
;
130 wilc
->rtc_clk
= devm_clk_get(&spi
->dev
, "rtc_clk");
131 if (PTR_ERR_OR_ZERO(wilc
->rtc_clk
) == -EPROBE_DEFER
)
132 return -EPROBE_DEFER
;
133 else if (!IS_ERR(wilc
->rtc_clk
))
134 clk_prepare_enable(wilc
->rtc_clk
);
139 static int wilc_bus_remove(struct spi_device
*spi
)
141 struct wilc
*wilc
= spi_get_drvdata(spi
);
143 /* free the GPIO in module remove */
145 gpiod_put(wilc
->gpio_irq
);
147 if (!IS_ERR(wilc
->rtc_clk
))
148 clk_disable_unprepare(wilc
->rtc_clk
);
150 wilc_netdev_cleanup(wilc
);
154 static const struct of_device_id wilc_of_match
[] = {
155 { .compatible
= "microchip,wilc1000-spi", },
158 MODULE_DEVICE_TABLE(of
, wilc_of_match
);
160 static struct spi_driver wilc_spi_driver
= {
163 .of_match_table
= wilc_of_match
,
165 .probe
= wilc_bus_probe
,
166 .remove
= wilc_bus_remove
,
168 module_spi_driver(wilc_spi_driver
);
169 MODULE_LICENSE("GPL");
171 static int wilc_spi_tx(struct wilc
*wilc
, u8
*b
, u32 len
)
173 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
175 struct spi_message msg
;
178 struct spi_transfer tr
= {
183 char *r_buffer
= kzalloc(len
, GFP_KERNEL
);
188 tr
.rx_buf
= r_buffer
;
189 dev_dbg(&spi
->dev
, "Request writing %d bytes\n", len
);
191 memset(&msg
, 0, sizeof(msg
));
192 spi_message_init(&msg
);
194 msg
.is_dma_mapped
= USE_SPI_DMA
;
195 spi_message_add_tail(&tr
, &msg
);
197 ret
= spi_sync(spi
, &msg
);
199 dev_err(&spi
->dev
, "SPI transaction failed\n");
204 "can't write data with the following length: %d\n",
212 static int wilc_spi_rx(struct wilc
*wilc
, u8
*rb
, u32 rlen
)
214 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
218 struct spi_message msg
;
219 struct spi_transfer tr
= {
225 char *t_buffer
= kzalloc(rlen
, GFP_KERNEL
);
230 tr
.tx_buf
= t_buffer
;
232 memset(&msg
, 0, sizeof(msg
));
233 spi_message_init(&msg
);
235 msg
.is_dma_mapped
= USE_SPI_DMA
;
236 spi_message_add_tail(&tr
, &msg
);
238 ret
= spi_sync(spi
, &msg
);
240 dev_err(&spi
->dev
, "SPI transaction failed\n");
244 "can't read data with the following length: %u\n",
252 static int wilc_spi_tx_rx(struct wilc
*wilc
, u8
*wb
, u8
*rb
, u32 rlen
)
254 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
258 struct spi_message msg
;
259 struct spi_transfer tr
= {
268 memset(&msg
, 0, sizeof(msg
));
269 spi_message_init(&msg
);
271 msg
.is_dma_mapped
= USE_SPI_DMA
;
273 spi_message_add_tail(&tr
, &msg
);
274 ret
= spi_sync(spi
, &msg
);
276 dev_err(&spi
->dev
, "SPI transaction failed\n");
279 "can't read data with the following length: %u\n",
287 static int spi_cmd_complete(struct wilc
*wilc
, u8 cmd
, u32 adr
, u8
*b
, u32 sz
,
290 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
291 struct wilc_spi
*spi_priv
= wilc
->bus_data
;
303 case CMD_SINGLE_READ
: /* single word (4 bytes) read */
304 wb
[1] = (u8
)(adr
>> 16);
305 wb
[2] = (u8
)(adr
>> 8);
310 case CMD_INTERNAL_READ
: /* internal register read */
311 wb
[1] = (u8
)(adr
>> 8);
340 case CMD_DMA_WRITE
: /* dma write */
341 case CMD_DMA_READ
: /* dma read */
342 wb
[1] = (u8
)(adr
>> 16);
343 wb
[2] = (u8
)(adr
>> 8);
345 wb
[4] = (u8
)(sz
>> 8);
350 case CMD_DMA_EXT_WRITE
: /* dma extended write */
351 case CMD_DMA_EXT_READ
: /* dma extended read */
352 wb
[1] = (u8
)(adr
>> 16);
353 wb
[2] = (u8
)(adr
>> 8);
355 wb
[4] = (u8
)(sz
>> 16);
356 wb
[5] = (u8
)(sz
>> 8);
361 case CMD_INTERNAL_WRITE
: /* internal register write */
362 wb
[1] = (u8
)(adr
>> 8);
373 case CMD_SINGLE_WRITE
: /* single word write */
374 wb
[1] = (u8
)(adr
>> 16);
375 wb
[2] = (u8
)(adr
>> 8);
392 if (!spi_priv
->crc_off
)
393 wb
[len
- 1] = (crc7(0x7f, (const u8
*)&wb
[0], len
- 1)) << 1;
397 #define NUM_SKIP_BYTES (1)
398 #define NUM_RSP_BYTES (2)
399 #define NUM_DATA_HDR_BYTES (1)
400 #define NUM_DATA_BYTES (4)
401 #define NUM_CRC_BYTES (2)
402 #define NUM_DUMMY_BYTES (3)
403 if (cmd
== CMD_RESET
||
404 cmd
== CMD_TERMINATE
||
406 len2
= len
+ (NUM_SKIP_BYTES
+ NUM_RSP_BYTES
+ NUM_DUMMY_BYTES
);
407 } else if (cmd
== CMD_INTERNAL_READ
|| cmd
== CMD_SINGLE_READ
) {
408 int tmp
= NUM_RSP_BYTES
+ NUM_DATA_HDR_BYTES
+ NUM_DATA_BYTES
410 if (!spi_priv
->crc_off
)
411 len2
= len
+ tmp
+ NUM_CRC_BYTES
;
415 len2
= len
+ (NUM_RSP_BYTES
+ NUM_DUMMY_BYTES
);
417 #undef NUM_DUMMY_BYTES
419 if (len2
> ARRAY_SIZE(wb
)) {
420 dev_err(&spi
->dev
, "spi buffer size too small (%d) (%zu)\n",
421 len2
, ARRAY_SIZE(wb
));
424 /* zero spi write buffers. */
425 for (wix
= len
; wix
< len2
; wix
++)
429 if (wilc_spi_tx_rx(wilc
, wb
, rb
, len2
)) {
430 dev_err(&spi
->dev
, "Failed cmd write, bus error...\n");
435 * Command/Control response
437 if (cmd
== CMD_RESET
|| cmd
== CMD_TERMINATE
|| cmd
== CMD_REPEAT
)
438 rix
++; /* skip 1 byte */
444 "Failed cmd response, cmd (%02x), resp (%02x)\n",
454 dev_err(&spi
->dev
, "Failed cmd state response state (%02x)\n",
459 if (cmd
== CMD_INTERNAL_READ
|| cmd
== CMD_SINGLE_READ
||
460 cmd
== CMD_DMA_READ
|| cmd
== CMD_DMA_EXT_READ
) {
462 * Data Respnose header
467 * ensure there is room in buffer later
468 * to read data and crc
476 if (((rsp
>> 4) & 0xf) == 0xf)
482 "Error, data read response (%02x)\n", rsp
);
487 if (cmd
== CMD_INTERNAL_READ
|| cmd
== CMD_SINGLE_READ
) {
491 if ((rix
+ 3) < len2
) {
498 "buffer overrun when reading data.\n");
502 if (!spi_priv
->crc_off
) {
506 if ((rix
+ 1) < len2
) {
511 "buffer overrun when reading crc.\n");
515 } else if ((cmd
== CMD_DMA_READ
) || (cmd
== CMD_DMA_EXT_READ
)) {
518 /* some data may be read in response to dummy bytes. */
519 for (ix
= 0; (rix
< len2
) && (ix
< sz
); )
527 if (sz
<= (DATA_PKT_SZ
- ix
))
530 nbytes
= DATA_PKT_SZ
- ix
;
535 if (wilc_spi_rx(wilc
, &b
[ix
], nbytes
)) {
537 "Failed block read, bus err\n");
544 if (!spi_priv
->crc_off
&& wilc_spi_rx(wilc
, crc
, 2)) {
546 "Failed block crc read, bus err\n");
555 * if any data in left unread,
556 * then read the rest using normal DMA code.
561 if (sz
<= DATA_PKT_SZ
)
564 nbytes
= DATA_PKT_SZ
;
567 * read data response only on the next DMA cycles not
568 * the first DMA since data response header is already
569 * handled above for the first DMA.
572 * Data Respnose header
576 if (wilc_spi_rx(wilc
, &rsp
, 1)) {
578 "Failed resp read, bus err\n");
582 if (((rsp
>> 4) & 0xf) == 0xf)
592 if (wilc_spi_rx(wilc
, &b
[ix
], nbytes
)) {
594 "Failed block read, bus err\n");
602 if (!spi_priv
->crc_off
&& wilc_spi_rx(wilc
, crc
, 2)) {
604 "Failed block crc read, bus err\n");
616 static int spi_data_write(struct wilc
*wilc
, u8
*b
, u32 sz
)
618 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
619 struct wilc_spi
*spi_priv
= wilc
->bus_data
;
622 u8 cmd
, order
, crc
[2] = {0};
629 if (sz
<= DATA_PKT_SZ
) {
633 nbytes
= DATA_PKT_SZ
;
646 if (wilc_spi_tx(wilc
, &cmd
, 1)) {
648 "Failed data block cmd write, bus error...\n");
656 if (wilc_spi_tx(wilc
, &b
[ix
], nbytes
)) {
658 "Failed data block write, bus error...\n");
666 if (!spi_priv
->crc_off
) {
667 if (wilc_spi_tx(wilc
, crc
, 2)) {
668 dev_err(&spi
->dev
, "Failed data block crc write, bus error...\n");
675 * No need to wait for response
684 /********************************************
686 * Spi Internal Read/Write Function
688 ********************************************/
690 static int spi_internal_write(struct wilc
*wilc
, u32 adr
, u32 dat
)
692 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
696 result
= spi_cmd_complete(wilc
, CMD_INTERNAL_WRITE
, adr
, (u8
*)&dat
, 4,
699 dev_err(&spi
->dev
, "Failed internal write cmd...\n");
704 static int spi_internal_read(struct wilc
*wilc
, u32 adr
, u32
*data
)
706 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
709 result
= spi_cmd_complete(wilc
, CMD_INTERNAL_READ
, adr
, (u8
*)data
, 4,
712 dev_err(&spi
->dev
, "Failed internal read cmd...\n");
721 /********************************************
725 ********************************************/
727 static int wilc_spi_write_reg(struct wilc
*wilc
, u32 addr
, u32 data
)
729 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
731 u8 cmd
= CMD_SINGLE_WRITE
;
736 /* Clockless register */
737 cmd
= CMD_INTERNAL_WRITE
;
741 result
= spi_cmd_complete(wilc
, cmd
, addr
, (u8
*)&data
, 4, clockless
);
743 dev_err(&spi
->dev
, "Failed cmd, write reg (%08x)...\n", addr
);
748 static int wilc_spi_write(struct wilc
*wilc
, u32 addr
, u8
*buf
, u32 size
)
750 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
754 * has to be greated than 4
759 result
= spi_cmd_complete(wilc
, CMD_DMA_EXT_WRITE
, addr
, NULL
, size
, 0);
762 "Failed cmd, write block (%08x)...\n", addr
);
769 result
= spi_data_write(wilc
, buf
, size
);
771 dev_err(&spi
->dev
, "Failed block data write...\n");
776 static int wilc_spi_read_reg(struct wilc
*wilc
, u32 addr
, u32
*data
)
778 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
780 u8 cmd
= CMD_SINGLE_READ
;
784 /* Clockless register */
785 cmd
= CMD_INTERNAL_READ
;
789 result
= spi_cmd_complete(wilc
, cmd
, addr
, (u8
*)data
, 4, clockless
);
791 dev_err(&spi
->dev
, "Failed cmd, read reg (%08x)...\n", addr
);
800 static int wilc_spi_read(struct wilc
*wilc
, u32 addr
, u8
*buf
, u32 size
)
802 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
808 result
= spi_cmd_complete(wilc
, CMD_DMA_EXT_READ
, addr
, buf
, size
, 0);
810 dev_err(&spi
->dev
, "Failed cmd, read block (%08x)...\n", addr
);
815 /********************************************
819 ********************************************/
821 static int wilc_spi_deinit(struct wilc
*wilc
)
829 static int wilc_spi_init(struct wilc
*wilc
, bool resume
)
831 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
832 struct wilc_spi
*spi_priv
= wilc
->bus_data
;
839 ret
= wilc_spi_read_reg(wilc
, 0x1000, &chipid
);
841 dev_err(&spi
->dev
, "Fail cmd read chip id...\n");
851 * TODO: We can remove the CRC trials if there is a definite
854 /* the SPI to it's initial value. */
855 ret
= spi_internal_read(wilc
, WILC_SPI_PROTOCOL_OFFSET
, ®
);
858 * Read failed. Try with CRC off. This might happen when module
859 * is removed but chip isn't reset
861 spi_priv
->crc_off
= 1;
863 "Failed read with CRC on, retrying with CRC off\n");
864 ret
= spi_internal_read(wilc
, WILC_SPI_PROTOCOL_OFFSET
, ®
);
867 * Read failed with both CRC on and off,
870 dev_err(&spi
->dev
, "Failed internal read protocol\n");
874 if (spi_priv
->crc_off
== 0) {
875 reg
&= ~0xc; /* disable crc checking */
878 ret
= spi_internal_write(wilc
, WILC_SPI_PROTOCOL_OFFSET
, reg
);
881 "[wilc spi %d]: Failed internal write reg\n",
885 spi_priv
->crc_off
= 1;
889 * make sure can read back chip id correctly
891 ret
= wilc_spi_read_reg(wilc
, 0x1000, &chipid
);
893 dev_err(&spi
->dev
, "Fail cmd read chip id...\n");
902 static int wilc_spi_read_size(struct wilc
*wilc
, u32
*size
)
906 ret
= spi_internal_read(wilc
, 0xe840 - WILC_SPI_REG_BASE
, size
);
907 *size
= *size
& IRQ_DMA_WD_CNT_MASK
;
912 static int wilc_spi_read_int(struct wilc
*wilc
, u32
*int_status
)
914 return spi_internal_read(wilc
, 0xe840 - WILC_SPI_REG_BASE
, int_status
);
917 static int wilc_spi_clear_int_ext(struct wilc
*wilc
, u32 val
)
919 return spi_internal_write(wilc
, 0xe844 - WILC_SPI_REG_BASE
, val
);
922 static int wilc_spi_sync_ext(struct wilc
*wilc
, int nint
)
924 struct spi_device
*spi
= to_spi_device(wilc
->dev
);
925 struct wilc_spi
*spi_priv
= wilc
->bus_data
;
929 if (nint
> MAX_NUM_INT
) {
930 dev_err(&spi
->dev
, "Too many interrupts (%d)...\n", nint
);
934 spi_priv
->nint
= nint
;
937 * interrupt pin mux select
939 ret
= wilc_spi_read_reg(wilc
, WILC_PIN_MUX_0
, ®
);
941 dev_err(&spi
->dev
, "Failed read reg (%08x)...\n",
946 ret
= wilc_spi_write_reg(wilc
, WILC_PIN_MUX_0
, reg
);
948 dev_err(&spi
->dev
, "Failed write reg (%08x)...\n",
956 ret
= wilc_spi_read_reg(wilc
, WILC_INTR_ENABLE
, ®
);
958 dev_err(&spi
->dev
, "Failed read reg (%08x)...\n",
963 for (i
= 0; (i
< 5) && (nint
> 0); i
++, nint
--)
964 reg
|= (BIT((27 + i
)));
966 ret
= wilc_spi_write_reg(wilc
, WILC_INTR_ENABLE
, reg
);
968 dev_err(&spi
->dev
, "Failed write reg (%08x)...\n",
973 ret
= wilc_spi_read_reg(wilc
, WILC_INTR2_ENABLE
, ®
);
975 dev_err(&spi
->dev
, "Failed read reg (%08x)...\n",
980 for (i
= 0; (i
< 3) && (nint
> 0); i
++, nint
--)
983 ret
= wilc_spi_read_reg(wilc
, WILC_INTR2_ENABLE
, ®
);
985 dev_err(&spi
->dev
, "Failed write reg (%08x)...\n",
994 /* Global spi HIF function table */
995 static const struct wilc_hif_func wilc_hif_spi
= {
996 .hif_init
= wilc_spi_init
,
997 .hif_deinit
= wilc_spi_deinit
,
998 .hif_read_reg
= wilc_spi_read_reg
,
999 .hif_write_reg
= wilc_spi_write_reg
,
1000 .hif_block_rx
= wilc_spi_read
,
1001 .hif_block_tx
= wilc_spi_write
,
1002 .hif_read_int
= wilc_spi_read_int
,
1003 .hif_clear_int_ext
= wilc_spi_clear_int_ext
,
1004 .hif_read_size
= wilc_spi_read_size
,
1005 .hif_block_tx_ext
= wilc_spi_write
,
1006 .hif_block_rx_ext
= wilc_spi_read
,
1007 .hif_sync_ext
= wilc_spi_sync_ext
,