1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
7 #include <linux/spi/spi.h>
9 #include "wilc_wfi_netdevice.h"
17 static struct wilc_spi g_spi;
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
96 #define DATA_PKT_SZ_256 256
97 #define DATA_PKT_SZ_512 512
98 #define DATA_PKT_SZ_1K 1024
99 #define DATA_PKT_SZ_4K (4 * 1024)
100 #define DATA_PKT_SZ_8K (8 * 1024)
101 #define DATA_PKT_SZ DATA_PKT_SZ_8K
103 #define USE_SPI_DMA 0
105 static int wilc_bus_probe(struct spi_device *spi)
109 struct gpio_desc *gpio;
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_netdev_init(&wilc, NULL, HIF_SPI, &wilc_hif_spi);
123 spi_set_drvdata(spi, wilc);
124 wilc->dev = &spi->dev;
125 wilc->gpio_irq = gpio;
130 static int wilc_bus_remove(struct spi_device *spi)
132 struct wilc *wilc = spi_get_drvdata(spi);
134 /* free the GPIO in module remove */
136 gpiod_put(wilc->gpio_irq);
137 wilc_netdev_cleanup(wilc);
141 static const struct of_device_id wilc_of_match[] = {
142 { .compatible = "microchip,wilc1000-spi", },
145 MODULE_DEVICE_TABLE(of, wilc_of_match);
147 static struct spi_driver wilc_spi_driver = {
150 .of_match_table = wilc_of_match,
152 .probe = wilc_bus_probe,
153 .remove = wilc_bus_remove,
155 module_spi_driver(wilc_spi_driver);
156 MODULE_LICENSE("GPL");
158 static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
160 struct spi_device *spi = to_spi_device(wilc->dev);
162 struct spi_message msg;
165 struct spi_transfer tr = {
170 char *r_buffer = kzalloc(len, GFP_KERNEL);
175 tr.rx_buf = r_buffer;
176 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
178 memset(&msg, 0, sizeof(msg));
179 spi_message_init(&msg);
181 msg.is_dma_mapped = USE_SPI_DMA;
182 spi_message_add_tail(&tr, &msg);
184 ret = spi_sync(spi, &msg);
186 dev_err(&spi->dev, "SPI transaction failed\n");
191 "can't write data with the following length: %d\n",
199 static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
201 struct spi_device *spi = to_spi_device(wilc->dev);
205 struct spi_message msg;
206 struct spi_transfer tr = {
212 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
217 tr.tx_buf = t_buffer;
219 memset(&msg, 0, sizeof(msg));
220 spi_message_init(&msg);
222 msg.is_dma_mapped = USE_SPI_DMA;
223 spi_message_add_tail(&tr, &msg);
225 ret = spi_sync(spi, &msg);
227 dev_err(&spi->dev, "SPI transaction failed\n");
231 "can't read data with the following length: %u\n",
239 static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
241 struct spi_device *spi = to_spi_device(wilc->dev);
245 struct spi_message msg;
246 struct spi_transfer tr = {
255 memset(&msg, 0, sizeof(msg));
256 spi_message_init(&msg);
258 msg.is_dma_mapped = USE_SPI_DMA;
260 spi_message_add_tail(&tr, &msg);
261 ret = spi_sync(spi, &msg);
263 dev_err(&spi->dev, "SPI transaction failed\n");
266 "can't read data with the following length: %u\n",
274 static int spi_cmd_complete(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz,
277 struct spi_device *spi = to_spi_device(wilc->dev);
289 case CMD_SINGLE_READ: /* single word (4 bytes) read */
290 wb[1] = (u8)(adr >> 16);
291 wb[2] = (u8)(adr >> 8);
296 case CMD_INTERNAL_READ: /* internal register read */
297 wb[1] = (u8)(adr >> 8);
326 case CMD_DMA_WRITE: /* dma write */
327 case CMD_DMA_READ: /* dma read */
328 wb[1] = (u8)(adr >> 16);
329 wb[2] = (u8)(adr >> 8);
331 wb[4] = (u8)(sz >> 8);
336 case CMD_DMA_EXT_WRITE: /* dma extended write */
337 case CMD_DMA_EXT_READ: /* dma extended read */
338 wb[1] = (u8)(adr >> 16);
339 wb[2] = (u8)(adr >> 8);
341 wb[4] = (u8)(sz >> 16);
342 wb[5] = (u8)(sz >> 8);
347 case CMD_INTERNAL_WRITE: /* internal register write */
348 wb[1] = (u8)(adr >> 8);
359 case CMD_SINGLE_WRITE: /* single word write */
360 wb[1] = (u8)(adr >> 16);
361 wb[2] = (u8)(adr >> 8);
379 wb[len - 1] = (crc7(0x7f, (const u8 *)&wb[0], len - 1)) << 1;
383 #define NUM_SKIP_BYTES (1)
384 #define NUM_RSP_BYTES (2)
385 #define NUM_DATA_HDR_BYTES (1)
386 #define NUM_DATA_BYTES (4)
387 #define NUM_CRC_BYTES (2)
388 #define NUM_DUMMY_BYTES (3)
389 if (cmd == CMD_RESET ||
390 cmd == CMD_TERMINATE ||
392 len2 = len + (NUM_SKIP_BYTES + NUM_RSP_BYTES + NUM_DUMMY_BYTES);
393 } else if (cmd == CMD_INTERNAL_READ || cmd == CMD_SINGLE_READ) {
394 int tmp = NUM_RSP_BYTES + NUM_DATA_HDR_BYTES + NUM_DATA_BYTES
397 len2 = len + tmp + NUM_CRC_BYTES;
401 len2 = len + (NUM_RSP_BYTES + NUM_DUMMY_BYTES);
403 #undef NUM_DUMMY_BYTES
405 if (len2 > ARRAY_SIZE(wb)) {
406 dev_err(&spi->dev, "spi buffer size too small (%d) (%zu)\n",
407 len2, ARRAY_SIZE(wb));
410 /* zero spi write buffers. */
411 for (wix = len; wix < len2; wix++)
415 if (wilc_spi_tx_rx(wilc, wb, rb, len2)) {
416 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
421 * Command/Control response
423 if (cmd == CMD_RESET || cmd == CMD_TERMINATE || cmd == CMD_REPEAT)
424 rix++; /* skip 1 byte */
430 "Failed cmd response, cmd (%02x), resp (%02x)\n",
440 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
445 if (cmd == CMD_INTERNAL_READ || cmd == CMD_SINGLE_READ ||
446 cmd == CMD_DMA_READ || cmd == CMD_DMA_EXT_READ) {
448 * Data Respnose header
453 * ensure there is room in buffer later
454 * to read data and crc
462 if (((rsp >> 4) & 0xf) == 0xf)
468 "Error, data read response (%02x)\n", rsp);
473 if (cmd == CMD_INTERNAL_READ || cmd == CMD_SINGLE_READ) {
477 if ((rix + 3) < len2) {
484 "buffer overrun when reading data.\n");
488 if (!g_spi.crc_off) {
492 if ((rix + 1) < len2) {
497 "buffer overrun when reading crc.\n");
501 } else if ((cmd == CMD_DMA_READ) || (cmd == CMD_DMA_EXT_READ)) {
504 /* some data may be read in response to dummy bytes. */
505 for (ix = 0; (rix < len2) && (ix < sz); )
513 if (sz <= (DATA_PKT_SZ - ix))
516 nbytes = DATA_PKT_SZ - ix;
521 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
523 "Failed block read, bus err\n");
530 if (!g_spi.crc_off && wilc_spi_rx(wilc, crc, 2)) {
532 "Failed block crc read, bus err\n");
541 * if any data in left unread,
542 * then read the rest using normal DMA code.
547 if (sz <= DATA_PKT_SZ)
550 nbytes = DATA_PKT_SZ;
553 * read data response only on the next DMA cycles not
554 * the first DMA since data response header is already
555 * handled above for the first DMA.
558 * Data Respnose header
562 if (wilc_spi_rx(wilc, &rsp, 1)) {
564 "Failed resp read, bus err\n");
568 if (((rsp >> 4) & 0xf) == 0xf)
572 if (result == N_FAIL)
578 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
580 "Failed block read, bus err\n");
588 if (!g_spi.crc_off && wilc_spi_rx(wilc, crc, 2)) {
590 "Failed block crc read, bus err\n");
602 static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
604 struct spi_device *spi = to_spi_device(wilc->dev);
607 u8 cmd, order, crc[2] = {0};
614 if (sz <= DATA_PKT_SZ) {
618 nbytes = DATA_PKT_SZ;
631 if (wilc_spi_tx(wilc, &cmd, 1)) {
633 "Failed data block cmd write, bus error...\n");
641 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
643 "Failed data block write, bus error...\n");
651 if (!g_spi.crc_off) {
652 if (wilc_spi_tx(wilc, crc, 2)) {
653 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
660 * No need to wait for response
669 /********************************************
671 * Spi Internal Read/Write Function
673 ********************************************/
675 static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
677 struct spi_device *spi = to_spi_device(wilc->dev);
681 result = spi_cmd_complete(wilc, CMD_INTERNAL_WRITE, adr, (u8 *)&dat, 4,
684 dev_err(&spi->dev, "Failed internal write cmd...\n");
689 static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
691 struct spi_device *spi = to_spi_device(wilc->dev);
694 result = spi_cmd_complete(wilc, CMD_INTERNAL_READ, adr, (u8 *)data, 4,
696 if (result != N_OK) {
697 dev_err(&spi->dev, "Failed internal read cmd...\n");
706 /********************************************
710 ********************************************/
712 static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
714 struct spi_device *spi = to_spi_device(wilc->dev);
716 u8 cmd = CMD_SINGLE_WRITE;
721 /* Clockless register */
722 cmd = CMD_INTERNAL_WRITE;
726 result = spi_cmd_complete(wilc, cmd, addr, (u8 *)&data, 4, clockless);
728 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
733 static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
735 struct spi_device *spi = to_spi_device(wilc->dev);
739 * has to be greated than 4
744 result = spi_cmd_complete(wilc, CMD_DMA_EXT_WRITE, addr, NULL, size, 0);
745 if (result != N_OK) {
747 "Failed cmd, write block (%08x)...\n", addr);
754 result = spi_data_write(wilc, buf, size);
756 dev_err(&spi->dev, "Failed block data write...\n");
761 static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
763 struct spi_device *spi = to_spi_device(wilc->dev);
765 u8 cmd = CMD_SINGLE_READ;
769 /* Clockless register */
770 cmd = CMD_INTERNAL_READ;
774 result = spi_cmd_complete(wilc, cmd, addr, (u8 *)data, 4, clockless);
775 if (result != N_OK) {
776 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
785 static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
787 struct spi_device *spi = to_spi_device(wilc->dev);
793 result = spi_cmd_complete(wilc, CMD_DMA_EXT_READ, addr, buf, size, 0);
794 if (result != N_OK) {
795 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
802 /********************************************
806 ********************************************/
808 static int _wilc_spi_deinit(struct wilc *wilc)
816 static int wilc_spi_init(struct wilc *wilc, bool resume)
818 struct spi_device *spi = to_spi_device(wilc->dev);
824 if (!wilc_spi_read_reg(wilc, 0x1000, &chipid)) {
825 dev_err(&spi->dev, "Fail cmd read chip id...\n");
831 memset(&g_spi, 0, sizeof(struct wilc_spi));
839 * TODO: We can remove the CRC trials if there is a definite
842 /* the SPI to it's initial value. */
843 if (!spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®)) {
845 * Read failed. Try with CRC off. This might happen when module
846 * is removed but chip isn't reset
850 "Failed read with CRC on, retrying with CRC off\n");
851 if (!spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®)) {
853 * Read failed with both CRC on and off,
856 dev_err(&spi->dev, "Failed internal read protocol\n");
860 if (g_spi.crc_off == 0) {
861 reg &= ~0xc; /* disable crc checking */
864 if (!spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg)) {
866 "[wilc spi %d]: Failed internal write reg\n",
874 * make sure can read back chip id correctly
876 if (!wilc_spi_read_reg(wilc, 0x1000, &chipid)) {
877 dev_err(&spi->dev, "Fail cmd read chip id...\n");
881 g_spi.has_thrpt_enh = 1;
888 static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
890 struct spi_device *spi = to_spi_device(wilc->dev);
893 if (g_spi.has_thrpt_enh) {
894 ret = spi_internal_read(wilc, 0xe840 - WILC_SPI_REG_BASE,
896 *size = *size & IRQ_DMA_WD_CNT_MASK;
901 ret = wilc_spi_read_reg(wilc, WILC_VMM_TO_HOST_SIZE,
905 "Failed read WILC_VMM_TO_HOST_SIZE ...\n");
908 tmp = (byte_cnt >> 2) & IRQ_DMA_WD_CNT_MASK;
915 static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
917 struct spi_device *spi = to_spi_device(wilc->dev);
924 int k = IRG_FLAGS_OFFSET + 5;
926 if (g_spi.has_thrpt_enh) {
927 ret = spi_internal_read(wilc, 0xe840 - WILC_SPI_REG_BASE,
931 ret = wilc_spi_read_reg(wilc, WILC_VMM_TO_HOST_SIZE, &byte_cnt);
934 "Failed read WILC_VMM_TO_HOST_SIZE ...\n");
937 tmp = (byte_cnt >> 2) & IRQ_DMA_WD_CNT_MASK;
943 wilc_spi_read_reg(wilc, 0x1a90, &irq_flags);
944 tmp |= ((irq_flags >> 27) << IRG_FLAGS_OFFSET);
946 if (g_spi.nint > 5) {
947 wilc_spi_read_reg(wilc, 0x1a94, &irq_flags);
948 tmp |= (((irq_flags >> 0) & 0x7) << k);
951 unknown_mask = ~((1ul << g_spi.nint) - 1);
953 if ((tmp >> IRG_FLAGS_OFFSET) & unknown_mask) {
955 "Unexpected interrupt(2):j=%d,tmp=%x,mask=%x\n",
956 j, tmp, unknown_mask);
968 static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
970 struct spi_device *spi = to_spi_device(wilc->dev);
975 if (g_spi.has_thrpt_enh) {
976 ret = spi_internal_write(wilc, 0xe844 - WILC_SPI_REG_BASE,
981 flags = val & (BIT(MAX_NUM_INT) - 1);
986 for (i = 0; i < g_spi.nint; i++) {
988 * No matter what you write 1 or 0,
989 * it will clear interrupt.
992 ret = wilc_spi_write_reg(wilc,
1000 "Failed wilc_spi_write_reg, set reg %x ...\n",
1004 for (i = g_spi.nint; i < MAX_NUM_INT; i++) {
1007 "Unexpected interrupt cleared %d...\n",
1014 /* select VMM table 0 */
1015 if (val & SEL_VMM_TBL0)
1017 /* select VMM table 1 */
1018 if (val & SEL_VMM_TBL1)
1021 ret = wilc_spi_write_reg(wilc, WILC_VMM_TBL_CTL, tbl_ctl);
1023 dev_err(&spi->dev, "fail write reg vmm_tbl_ctl...\n");
1029 * enable vmm transfer.
1031 ret = wilc_spi_write_reg(wilc, WILC_VMM_CORE_CTL, 1);
1033 dev_err(&spi->dev, "fail write reg vmm_core_ctl...\n");
1041 static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
1043 struct spi_device *spi = to_spi_device(wilc->dev);
1047 if (nint > MAX_NUM_INT) {
1048 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
1055 * interrupt pin mux select
1057 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
1059 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1064 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
1066 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1074 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
1076 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1081 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1082 reg |= (BIT((27 + i)));
1084 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
1086 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1091 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1093 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1098 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1101 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1103 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1112 /* Global spi HIF function table */
1113 static const struct wilc_hif_func wilc_hif_spi = {
1114 .hif_init = wilc_spi_init,
1115 .hif_deinit = _wilc_spi_deinit,
1116 .hif_read_reg = wilc_spi_read_reg,
1117 .hif_write_reg = wilc_spi_write_reg,
1118 .hif_block_rx = wilc_spi_read,
1119 .hif_block_tx = wilc_spi_write,
1120 .hif_read_int = wilc_spi_read_int,
1121 .hif_clear_int_ext = wilc_spi_clear_int_ext,
1122 .hif_read_size = wilc_spi_read_size,
1123 .hif_block_tx_ext = wilc_spi_write,
1124 .hif_block_rx_ext = wilc_spi_read,
1125 .hif_sync_ext = wilc_spi_sync_ext,