1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
8 #include <linux/spi/spi.h>
9 #include <linux/crc7.h>
10 #include <linux/crc-itu-t.h>
11 #include <linux/gpio/consumer.h>
16 #define SPI_MODALIAS "wilc1000_spi"
18 static bool enable_crc7; /* protect SPI commands with CRC7 */
19 module_param(enable_crc7, bool, 0644);
20 MODULE_PARM_DESC(enable_crc7,
21 "Enable CRC7 checksum to protect command transfers\n"
22 "\t\t\tagainst corruption during the SPI transfer.\n"
23 "\t\t\tCommand transfers are short and the CPU-cycle cost\n"
24 "\t\t\tof enabling this is small.");
26 static bool enable_crc16; /* protect SPI data with CRC16 */
27 module_param(enable_crc16, bool, 0644);
28 MODULE_PARM_DESC(enable_crc16,
29 "Enable CRC16 checksum to protect data transfers\n"
30 "\t\t\tagainst corruption during the SPI transfer.\n"
31 "\t\t\tData transfers can be large and the CPU-cycle cost\n"
32 "\t\t\tof enabling this may be substantial.");
35 * For CMD_SINGLE_READ and CMD_INTERNAL_READ, WILC may insert one or
36 * more zero bytes between the command response and the DATA Start tag
37 * (0xf3). This behavior appears to be undocumented in "ATWILC1000
38 * USER GUIDE" (https://tinyurl.com/4hhshdts) but we have observed 1-4
39 * zero bytes when the SPI bus operates at 48MHz and none when it
42 #define WILC_SPI_RSP_HDR_EXTRA_DATA 8
45 bool isinit; /* true if SPI protocol has been configured */
46 bool probing_crc; /* true if we're probing chip's CRC config */
47 bool crc7_enabled; /* true if crc7 is currently enabled */
48 bool crc16_enabled; /* true if crc16 is currently enabled */
50 struct gpio_desc *enable; /* ENABLE GPIO or NULL */
51 struct gpio_desc *reset; /* RESET GPIO or NULL */
55 static const struct wilc_hif_func wilc_hif_spi;
57 static int wilc_spi_reset(struct wilc *wilc);
59 /********************************************
61 * Spi protocol Function
63 ********************************************/
65 #define CMD_DMA_WRITE 0xc1
66 #define CMD_DMA_READ 0xc2
67 #define CMD_INTERNAL_WRITE 0xc3
68 #define CMD_INTERNAL_READ 0xc4
69 #define CMD_TERMINATE 0xc5
70 #define CMD_REPEAT 0xc6
71 #define CMD_DMA_EXT_WRITE 0xc7
72 #define CMD_DMA_EXT_READ 0xc8
73 #define CMD_SINGLE_WRITE 0xc9
74 #define CMD_SINGLE_READ 0xca
75 #define CMD_RESET 0xcf
77 #define SPI_RETRY_MAX_LIMIT 10
78 #define SPI_ENABLE_VMM_RETRY_LIMIT 2
80 /* SPI response fields (section 11.1.2 in ATWILC1000 User Guide): */
81 #define RSP_START_FIELD GENMASK(7, 4)
82 #define RSP_TYPE_FIELD GENMASK(3, 0)
84 /* SPI response values for the response fields: */
85 #define RSP_START_TAG 0xc
86 #define RSP_TYPE_FIRST_PACKET 0x1
87 #define RSP_TYPE_INNER_PACKET 0x2
88 #define RSP_TYPE_LAST_PACKET 0x3
89 #define RSP_STATE_NO_ERROR 0x00
91 #define PROTOCOL_REG_PKT_SZ_MASK GENMASK(6, 4)
92 #define PROTOCOL_REG_CRC16_MASK GENMASK(3, 3)
93 #define PROTOCOL_REG_CRC7_MASK GENMASK(2, 2)
96 * The SPI data packet size may be any integer power of two in the
97 * range from 256 to 8192 bytes.
99 #define DATA_PKT_LOG_SZ_MIN 8 /* 256 B */
100 #define DATA_PKT_LOG_SZ_MAX 13 /* 8 KiB */
103 * Select the data packet size (log2 of number of bytes): Use the
104 * maximum data packet size. We only retransmit complete packets, so
105 * there is no benefit from using smaller data packets.
107 #define DATA_PKT_LOG_SZ DATA_PKT_LOG_SZ_MAX
108 #define DATA_PKT_SZ (1 << DATA_PKT_LOG_SZ)
110 #define WILC_SPI_COMMAND_STAT_SUCCESS 0
111 #define WILC_GET_RESP_HDR_START(h) (((h) >> 4) & 0xf)
113 struct wilc_spi_cmd {
119 } __packed simple_cmd;
129 } __packed dma_cmd_ext;
134 } __packed internal_w_cmd;
143 struct wilc_spi_read_rsp_data {
149 struct wilc_spi_rsp_data {
155 struct wilc_spi_special_cmd_rsp {
161 static int wilc_parse_gpios(struct wilc *wilc)
163 struct spi_device *spi = to_spi_device(wilc->dev);
164 struct wilc_spi *spi_priv = wilc->bus_data;
165 struct wilc_gpios *gpios = &spi_priv->gpios;
167 /* get ENABLE pin and deassert it (if it is defined): */
168 gpios->enable = devm_gpiod_get_optional(&spi->dev,
169 "enable", GPIOD_OUT_LOW);
170 /* get RESET pin and assert it (if it is defined): */
172 /* if enable pin exists, reset must exist as well */
173 gpios->reset = devm_gpiod_get(&spi->dev,
174 "reset", GPIOD_OUT_HIGH);
175 if (IS_ERR(gpios->reset)) {
176 dev_err(&spi->dev, "missing reset gpio.\n");
177 return PTR_ERR(gpios->reset);
180 gpios->reset = devm_gpiod_get_optional(&spi->dev,
181 "reset", GPIOD_OUT_HIGH);
186 static void wilc_wlan_power(struct wilc *wilc, bool on)
188 struct wilc_spi *spi_priv = wilc->bus_data;
189 struct wilc_gpios *gpios = &spi_priv->gpios;
193 gpiod_set_value(gpios->enable, 1);
195 /* deassert RESET: */
196 gpiod_set_value(gpios->reset, 0);
199 gpiod_set_value(gpios->reset, 1);
200 /* deassert ENABLE: */
201 gpiod_set_value(gpios->enable, 0);
205 static int wilc_bus_probe(struct spi_device *spi)
209 struct wilc_spi *spi_priv;
211 spi_priv = kzalloc(sizeof(*spi_priv), GFP_KERNEL);
215 ret = wilc_cfg80211_init(&wilc, &spi->dev, WILC_HIF_SPI, &wilc_hif_spi);
219 spi_set_drvdata(spi, wilc);
220 wilc->dev = &spi->dev;
221 wilc->bus_data = spi_priv;
222 wilc->dev_irq_num = spi->irq;
224 ret = wilc_parse_gpios(wilc);
228 wilc->rtc_clk = devm_clk_get_optional(&spi->dev, "rtc");
229 if (IS_ERR(wilc->rtc_clk)) {
230 ret = PTR_ERR(wilc->rtc_clk);
233 clk_prepare_enable(wilc->rtc_clk);
238 wilc_netdev_cleanup(wilc);
244 static void wilc_bus_remove(struct spi_device *spi)
246 struct wilc *wilc = spi_get_drvdata(spi);
247 struct wilc_spi *spi_priv = wilc->bus_data;
249 clk_disable_unprepare(wilc->rtc_clk);
250 wilc_netdev_cleanup(wilc);
254 static const struct of_device_id wilc_of_match[] = {
255 { .compatible = "microchip,wilc1000", },
258 MODULE_DEVICE_TABLE(of, wilc_of_match);
260 static const struct spi_device_id wilc_spi_id[] = {
264 MODULE_DEVICE_TABLE(spi, wilc_spi_id);
266 static struct spi_driver wilc_spi_driver = {
268 .name = SPI_MODALIAS,
269 .of_match_table = wilc_of_match,
271 .id_table = wilc_spi_id,
272 .probe = wilc_bus_probe,
273 .remove = wilc_bus_remove,
275 module_spi_driver(wilc_spi_driver);
276 MODULE_DESCRIPTION("Atmel WILC1000 SPI wireless driver");
277 MODULE_LICENSE("GPL");
279 static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
281 struct spi_device *spi = to_spi_device(wilc->dev);
283 struct spi_message msg;
286 struct spi_transfer tr = {
291 .unit = SPI_DELAY_UNIT_USECS
294 char *r_buffer = kzalloc(len, GFP_KERNEL);
299 tr.rx_buf = r_buffer;
300 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
302 memset(&msg, 0, sizeof(msg));
303 spi_message_init(&msg);
305 spi_message_add_tail(&tr, &msg);
307 ret = spi_sync(spi, &msg);
309 dev_err(&spi->dev, "SPI transaction failed\n");
314 "can't write data with the following length: %d\n",
322 static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
324 struct spi_device *spi = to_spi_device(wilc->dev);
328 struct spi_message msg;
329 struct spi_transfer tr = {
334 .unit = SPI_DELAY_UNIT_USECS
338 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
343 tr.tx_buf = t_buffer;
345 memset(&msg, 0, sizeof(msg));
346 spi_message_init(&msg);
348 spi_message_add_tail(&tr, &msg);
350 ret = spi_sync(spi, &msg);
352 dev_err(&spi->dev, "SPI transaction failed\n");
356 "can't read data with the following length: %u\n",
364 static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
366 struct spi_device *spi = to_spi_device(wilc->dev);
370 struct spi_message msg;
371 struct spi_transfer tr = {
378 .unit = SPI_DELAY_UNIT_USECS
383 memset(&msg, 0, sizeof(msg));
384 spi_message_init(&msg);
387 spi_message_add_tail(&tr, &msg);
388 ret = spi_sync(spi, &msg);
390 dev_err(&spi->dev, "SPI transaction failed\n");
393 "can't read data with the following length: %u\n",
401 static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
403 struct spi_device *spi = to_spi_device(wilc->dev);
404 struct wilc_spi *spi_priv = wilc->bus_data;
407 u8 cmd, order, crc[2];
415 if (sz <= DATA_PKT_SZ) {
419 nbytes = DATA_PKT_SZ;
432 if (wilc_spi_tx(wilc, &cmd, 1)) {
434 "Failed data block cmd write, bus error...\n");
442 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
444 "Failed data block write, bus error...\n");
452 if (spi_priv->crc16_enabled) {
453 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
454 crc[0] = crc_calc >> 8;
456 if (wilc_spi_tx(wilc, crc, 2)) {
457 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
464 * No need to wait for response
473 /********************************************
475 * Spi Internal Read/Write Function
477 ********************************************/
478 static u8 wilc_get_crc7(u8 *buffer, u32 len)
480 return crc7_be(0xfe, buffer, len);
483 static int wilc_spi_single_read(struct wilc *wilc, u8 cmd, u32 adr, void *b,
486 struct spi_device *spi = to_spi_device(wilc->dev);
487 struct wilc_spi *spi_priv = wilc->bus_data;
489 int cmd_len, resp_len, i;
490 u16 crc_calc, crc_recv;
491 struct wilc_spi_cmd *c;
492 struct wilc_spi_rsp_data *r;
493 struct wilc_spi_read_rsp_data *r_data;
495 memset(wb, 0x0, sizeof(wb));
496 memset(rb, 0x0, sizeof(rb));
497 c = (struct wilc_spi_cmd *)wb;
499 if (cmd == CMD_SINGLE_READ) {
500 c->u.simple_cmd.addr[0] = adr >> 16;
501 c->u.simple_cmd.addr[1] = adr >> 8;
502 c->u.simple_cmd.addr[2] = adr;
503 } else if (cmd == CMD_INTERNAL_READ) {
504 c->u.simple_cmd.addr[0] = adr >> 8;
506 c->u.simple_cmd.addr[0] |= BIT(7);
507 c->u.simple_cmd.addr[1] = adr;
508 c->u.simple_cmd.addr[2] = 0x0;
510 dev_err(&spi->dev, "cmd [%x] not supported\n", cmd);
514 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
515 resp_len = sizeof(*r) + sizeof(*r_data) + WILC_SPI_RSP_HDR_EXTRA_DATA;
517 if (spi_priv->crc7_enabled) {
518 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
523 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
525 "spi buffer size too small (%d) (%d) (%zu)\n",
526 cmd_len, resp_len, ARRAY_SIZE(wb));
530 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
531 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
535 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
536 if (r->rsp_cmd_type != cmd && !clockless) {
537 if (!spi_priv->probing_crc)
539 "Failed cmd, cmd (%02x), resp (%02x)\n",
540 cmd, r->rsp_cmd_type);
544 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
545 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
550 for (i = 0; i < WILC_SPI_RSP_HDR_EXTRA_DATA; ++i)
551 if (WILC_GET_RESP_HDR_START(r->data[i]) == 0xf)
554 if (i >= WILC_SPI_RSP_HDR_EXTRA_DATA) {
555 dev_err(&spi->dev, "Error, data start missing\n");
559 r_data = (struct wilc_spi_read_rsp_data *)&r->data[i];
562 memcpy(b, r_data->data, 4);
564 if (!clockless && spi_priv->crc16_enabled) {
565 crc_recv = (r_data->crc[0] << 8) | r_data->crc[1];
566 crc_calc = crc_itu_t(0xffff, r_data->data, 4);
567 if (crc_recv != crc_calc) {
568 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
569 "(calculated 0x%04x)\n", __func__,
578 static int wilc_spi_write_cmd(struct wilc *wilc, u8 cmd, u32 adr, u32 data,
581 struct spi_device *spi = to_spi_device(wilc->dev);
582 struct wilc_spi *spi_priv = wilc->bus_data;
584 int cmd_len, resp_len;
585 struct wilc_spi_cmd *c;
586 struct wilc_spi_rsp_data *r;
588 memset(wb, 0x0, sizeof(wb));
589 memset(rb, 0x0, sizeof(rb));
590 c = (struct wilc_spi_cmd *)wb;
592 if (cmd == CMD_INTERNAL_WRITE) {
593 c->u.internal_w_cmd.addr[0] = adr >> 8;
595 c->u.internal_w_cmd.addr[0] |= BIT(7);
597 c->u.internal_w_cmd.addr[1] = adr;
598 c->u.internal_w_cmd.data = cpu_to_be32(data);
599 cmd_len = offsetof(struct wilc_spi_cmd, u.internal_w_cmd.crc);
600 if (spi_priv->crc7_enabled)
601 c->u.internal_w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
602 } else if (cmd == CMD_SINGLE_WRITE) {
603 c->u.w_cmd.addr[0] = adr >> 16;
604 c->u.w_cmd.addr[1] = adr >> 8;
605 c->u.w_cmd.addr[2] = adr;
606 c->u.w_cmd.data = cpu_to_be32(data);
607 cmd_len = offsetof(struct wilc_spi_cmd, u.w_cmd.crc);
608 if (spi_priv->crc7_enabled)
609 c->u.w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
611 dev_err(&spi->dev, "write cmd [%x] not supported\n", cmd);
615 if (spi_priv->crc7_enabled)
618 resp_len = sizeof(*r);
620 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
622 "spi buffer size too small (%d) (%d) (%zu)\n",
623 cmd_len, resp_len, ARRAY_SIZE(wb));
627 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
628 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
632 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
634 * Clockless registers operations might return unexptected responses,
635 * even if successful.
637 if (r->rsp_cmd_type != cmd && !clockless) {
639 "Failed cmd response, cmd (%02x), resp (%02x)\n",
640 cmd, r->rsp_cmd_type);
644 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
645 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
653 static int wilc_spi_dma_rw(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz)
655 struct spi_device *spi = to_spi_device(wilc->dev);
656 struct wilc_spi *spi_priv = wilc->bus_data;
657 u16 crc_recv, crc_calc;
659 int cmd_len, resp_len;
662 struct wilc_spi_cmd *c;
663 struct wilc_spi_rsp_data *r;
665 memset(wb, 0x0, sizeof(wb));
666 memset(rb, 0x0, sizeof(rb));
667 c = (struct wilc_spi_cmd *)wb;
669 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_READ) {
670 c->u.dma_cmd.addr[0] = adr >> 16;
671 c->u.dma_cmd.addr[1] = adr >> 8;
672 c->u.dma_cmd.addr[2] = adr;
673 c->u.dma_cmd.size[0] = sz >> 8;
674 c->u.dma_cmd.size[1] = sz;
675 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd.crc);
676 if (spi_priv->crc7_enabled)
677 c->u.dma_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
678 } else if (cmd == CMD_DMA_EXT_WRITE || cmd == CMD_DMA_EXT_READ) {
679 c->u.dma_cmd_ext.addr[0] = adr >> 16;
680 c->u.dma_cmd_ext.addr[1] = adr >> 8;
681 c->u.dma_cmd_ext.addr[2] = adr;
682 c->u.dma_cmd_ext.size[0] = sz >> 16;
683 c->u.dma_cmd_ext.size[1] = sz >> 8;
684 c->u.dma_cmd_ext.size[2] = sz;
685 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd_ext.crc);
686 if (spi_priv->crc7_enabled)
687 c->u.dma_cmd_ext.crc[0] = wilc_get_crc7(wb, cmd_len);
689 dev_err(&spi->dev, "dma read write cmd [%x] not supported\n",
693 if (spi_priv->crc7_enabled)
696 resp_len = sizeof(*r);
698 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
699 dev_err(&spi->dev, "spi buffer size too small (%d)(%d) (%zu)\n",
700 cmd_len, resp_len, ARRAY_SIZE(wb));
704 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
705 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
709 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
710 if (r->rsp_cmd_type != cmd) {
712 "Failed cmd response, cmd (%02x), resp (%02x)\n",
713 cmd, r->rsp_cmd_type);
717 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
718 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
723 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_EXT_WRITE)
730 nbytes = min_t(u32, sz, DATA_PKT_SZ);
733 * Data Response header
737 if (wilc_spi_rx(wilc, &rsp, 1)) {
739 "Failed resp read, bus err\n");
742 if (WILC_GET_RESP_HDR_START(rsp) == 0xf)
749 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
751 "Failed block read, bus err\n");
758 if (spi_priv->crc16_enabled) {
759 if (wilc_spi_rx(wilc, crc, 2)) {
761 "Failed block CRC read, bus err\n");
764 crc_recv = (crc[0] << 8) | crc[1];
765 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
766 if (crc_recv != crc_calc) {
767 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
768 "(calculated 0x%04x)\n", __func__,
780 static int wilc_spi_special_cmd(struct wilc *wilc, u8 cmd)
782 struct spi_device *spi = to_spi_device(wilc->dev);
783 struct wilc_spi *spi_priv = wilc->bus_data;
785 int cmd_len, resp_len = 0;
786 struct wilc_spi_cmd *c;
787 struct wilc_spi_special_cmd_rsp *r;
789 if (cmd != CMD_TERMINATE && cmd != CMD_REPEAT && cmd != CMD_RESET)
792 memset(wb, 0x0, sizeof(wb));
793 memset(rb, 0x0, sizeof(rb));
794 c = (struct wilc_spi_cmd *)wb;
797 if (cmd == CMD_RESET)
798 memset(c->u.simple_cmd.addr, 0xFF, 3);
800 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
801 resp_len = sizeof(*r);
803 if (spi_priv->crc7_enabled) {
804 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
807 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
808 dev_err(&spi->dev, "spi buffer size too small (%d) (%d) (%zu)\n",
809 cmd_len, resp_len, ARRAY_SIZE(wb));
813 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
814 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
818 r = (struct wilc_spi_special_cmd_rsp *)&rb[cmd_len];
819 if (r->rsp_cmd_type != cmd) {
820 if (!spi_priv->probing_crc)
822 "Failed cmd response, cmd (%02x), resp (%02x)\n",
823 cmd, r->rsp_cmd_type);
827 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
828 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
835 static void wilc_spi_reset_cmd_sequence(struct wilc *wl, u8 attempt, u32 addr)
837 struct spi_device *spi = to_spi_device(wl->dev);
838 struct wilc_spi *spi_priv = wl->bus_data;
840 if (!spi_priv->probing_crc)
841 dev_err(&spi->dev, "Reset and retry %d %x\n", attempt, addr);
843 usleep_range(1000, 1100);
845 usleep_range(1000, 1100);
848 static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
850 struct spi_device *spi = to_spi_device(wilc->dev);
852 u8 cmd = CMD_SINGLE_READ;
856 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
857 /* Clockless register */
858 cmd = CMD_INTERNAL_READ;
862 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
863 result = wilc_spi_single_read(wilc, cmd, addr, data, clockless);
869 /* retry is not applicable for clockless registers */
873 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
874 wilc_spi_reset_cmd_sequence(wilc, i, addr);
880 static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
882 struct spi_device *spi = to_spi_device(wilc->dev);
889 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
890 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_READ, addr,
895 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
897 wilc_spi_reset_cmd_sequence(wilc, i, addr);
903 static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
905 struct spi_device *spi = to_spi_device(wilc->dev);
909 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
910 result = wilc_spi_write_cmd(wilc, CMD_INTERNAL_WRITE, adr,
914 dev_err(&spi->dev, "Failed internal write cmd...\n");
916 wilc_spi_reset_cmd_sequence(wilc, i, adr);
922 static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
924 struct spi_device *spi = to_spi_device(wilc->dev);
925 struct wilc_spi *spi_priv = wilc->bus_data;
929 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
930 result = wilc_spi_single_read(wilc, CMD_INTERNAL_READ, adr,
936 if (!spi_priv->probing_crc)
937 dev_err(&spi->dev, "Failed internal read cmd...\n");
939 wilc_spi_reset_cmd_sequence(wilc, i, adr);
945 /********************************************
949 ********************************************/
951 static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
953 struct spi_device *spi = to_spi_device(wilc->dev);
955 u8 cmd = CMD_SINGLE_WRITE;
959 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
960 /* Clockless register */
961 cmd = CMD_INTERNAL_WRITE;
965 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
966 result = wilc_spi_write_cmd(wilc, cmd, addr, data, clockless);
970 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
975 wilc_spi_reset_cmd_sequence(wilc, i, addr);
980 static int spi_data_rsp(struct wilc *wilc, u8 cmd)
982 struct spi_device *spi = to_spi_device(wilc->dev);
987 * The response to data packets is two bytes long. For
988 * efficiency's sake, wilc_spi_write() wisely ignores the
989 * responses for all packets but the final one. The downside
990 * of that optimization is that when the final data packet is
991 * short, we may receive (part of) the response to the
992 * second-to-last packet before the one for the final packet.
993 * To handle this, we always read 4 bytes and then search for
994 * the last byte that contains the "Response Start" code (0xc
995 * in the top 4 bits). We then know that this byte is the
996 * first response byte of the final data packet.
998 result = wilc_spi_rx(wilc, rsp, sizeof(rsp));
1000 dev_err(&spi->dev, "Failed bus error...\n");
1004 for (i = sizeof(rsp) - 2; i >= 0; --i)
1005 if (FIELD_GET(RSP_START_FIELD, rsp[i]) == RSP_START_TAG)
1010 "Data packet response missing (%02x %02x %02x %02x)\n",
1011 rsp[0], rsp[1], rsp[2], rsp[3]);
1015 /* rsp[i] is the last response start byte */
1017 if (FIELD_GET(RSP_TYPE_FIELD, rsp[i]) != RSP_TYPE_LAST_PACKET
1018 || rsp[i + 1] != RSP_STATE_NO_ERROR) {
1019 dev_err(&spi->dev, "Data response error (%02x %02x)\n",
1020 rsp[i], rsp[i + 1]);
1026 static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
1028 struct spi_device *spi = to_spi_device(wilc->dev);
1033 * has to be greated than 4
1038 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
1039 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_WRITE, addr,
1043 "Failed cmd, write block (%08x)...\n", addr);
1044 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1051 result = spi_data_write(wilc, buf, size);
1053 dev_err(&spi->dev, "Failed block data write...\n");
1054 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1061 result = spi_data_rsp(wilc, CMD_DMA_EXT_WRITE);
1063 dev_err(&spi->dev, "Failed block data rsp...\n");
1064 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1072 /********************************************
1076 ********************************************/
1078 static int wilc_spi_reset(struct wilc *wilc)
1080 struct spi_device *spi = to_spi_device(wilc->dev);
1081 struct wilc_spi *spi_priv = wilc->bus_data;
1084 result = wilc_spi_special_cmd(wilc, CMD_RESET);
1085 if (result && !spi_priv->probing_crc)
1086 dev_err(&spi->dev, "Failed cmd reset\n");
1091 static bool wilc_spi_is_init(struct wilc *wilc)
1093 struct wilc_spi *spi_priv = wilc->bus_data;
1095 return spi_priv->isinit;
1098 static int wilc_spi_deinit(struct wilc *wilc)
1100 struct wilc_spi *spi_priv = wilc->bus_data;
1102 spi_priv->isinit = false;
1103 wilc_wlan_power(wilc, false);
1107 static int wilc_spi_init(struct wilc *wilc, bool resume)
1109 struct spi_device *spi = to_spi_device(wilc->dev);
1110 struct wilc_spi *spi_priv = wilc->bus_data;
1115 if (spi_priv->isinit) {
1116 /* Confirm we can read chipid register without error: */
1117 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1121 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1124 wilc_wlan_power(wilc, true);
1127 * configure protocol
1131 * Infer the CRC settings that are currently in effect. This
1132 * is necessary because we can't be sure that the chip has
1133 * been RESET (e.g, after module unload and reload).
1135 spi_priv->probing_crc = true;
1136 spi_priv->crc7_enabled = enable_crc7;
1137 spi_priv->crc16_enabled = false; /* don't check CRC16 during probing */
1138 for (i = 0; i < 2; ++i) {
1139 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1142 spi_priv->crc7_enabled = !enable_crc7;
1145 dev_err(&spi->dev, "Failed with CRC7 on and off.\n");
1149 /* set up the desired CRC configuration: */
1150 reg &= ~(PROTOCOL_REG_CRC7_MASK | PROTOCOL_REG_CRC16_MASK);
1152 reg |= PROTOCOL_REG_CRC7_MASK;
1154 reg |= PROTOCOL_REG_CRC16_MASK;
1156 /* set up the data packet size: */
1157 BUILD_BUG_ON(DATA_PKT_LOG_SZ < DATA_PKT_LOG_SZ_MIN
1158 || DATA_PKT_LOG_SZ > DATA_PKT_LOG_SZ_MAX);
1159 reg &= ~PROTOCOL_REG_PKT_SZ_MASK;
1160 reg |= FIELD_PREP(PROTOCOL_REG_PKT_SZ_MASK,
1161 DATA_PKT_LOG_SZ - DATA_PKT_LOG_SZ_MIN);
1163 /* establish the new setup: */
1164 ret = spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg);
1167 "[wilc spi %d]: Failed internal write reg\n",
1171 /* update our state to match new protocol settings: */
1172 spi_priv->crc7_enabled = enable_crc7;
1173 spi_priv->crc16_enabled = enable_crc16;
1175 /* re-read to make sure new settings are in effect: */
1176 spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1178 spi_priv->probing_crc = false;
1181 * make sure can read chip id without protocol error
1183 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1185 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1189 spi_priv->isinit = true;
1194 static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
1198 ret = spi_internal_read(wilc,
1199 WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE, size);
1200 *size = FIELD_GET(IRQ_DMA_WD_CNT_MASK, *size);
1205 static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
1207 return spi_internal_read(wilc, WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE,
1211 static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
1214 int retry = SPI_ENABLE_VMM_RETRY_LIMIT;
1218 ret = spi_internal_write(wilc,
1219 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1224 ret = spi_internal_read(wilc,
1225 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1227 if (ret || ((check & EN_VMM) == (val & EN_VMM)))
1235 static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
1237 struct spi_device *spi = to_spi_device(wilc->dev);
1241 if (nint > MAX_NUM_INT) {
1242 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
1247 * interrupt pin mux select
1249 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
1251 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1256 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
1258 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1266 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
1268 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1273 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1274 reg |= (BIT((27 + i)));
1276 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
1278 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1283 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1285 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1290 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1293 ret = wilc_spi_write_reg(wilc, WILC_INTR2_ENABLE, reg);
1295 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1304 /* Global spi HIF function table */
1305 static const struct wilc_hif_func wilc_hif_spi = {
1306 .hif_init = wilc_spi_init,
1307 .hif_deinit = wilc_spi_deinit,
1308 .hif_read_reg = wilc_spi_read_reg,
1309 .hif_write_reg = wilc_spi_write_reg,
1310 .hif_block_rx = wilc_spi_read,
1311 .hif_block_tx = wilc_spi_write,
1312 .hif_read_int = wilc_spi_read_int,
1313 .hif_clear_int_ext = wilc_spi_clear_int_ext,
1314 .hif_read_size = wilc_spi_read_size,
1315 .hif_block_tx_ext = wilc_spi_write,
1316 .hif_block_rx_ext = wilc_spi_read,
1317 .hif_sync_ext = wilc_spi_sync_ext,
1318 .hif_reset = wilc_spi_reset,
1319 .hif_is_init = wilc_spi_is_init,