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);
196 gpiod_set_value(gpios->reset, 1);
198 /* deassert RESET: */
199 gpiod_set_value(gpios->reset, 0);
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_LICENSE("GPL");
278 static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
280 struct spi_device *spi = to_spi_device(wilc->dev);
282 struct spi_message msg;
285 struct spi_transfer tr = {
290 .unit = SPI_DELAY_UNIT_USECS
293 char *r_buffer = kzalloc(len, GFP_KERNEL);
298 tr.rx_buf = r_buffer;
299 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
301 memset(&msg, 0, sizeof(msg));
302 spi_message_init(&msg);
304 spi_message_add_tail(&tr, &msg);
306 ret = spi_sync(spi, &msg);
308 dev_err(&spi->dev, "SPI transaction failed\n");
313 "can't write data with the following length: %d\n",
321 static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
323 struct spi_device *spi = to_spi_device(wilc->dev);
327 struct spi_message msg;
328 struct spi_transfer tr = {
333 .unit = SPI_DELAY_UNIT_USECS
337 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
342 tr.tx_buf = t_buffer;
344 memset(&msg, 0, sizeof(msg));
345 spi_message_init(&msg);
347 spi_message_add_tail(&tr, &msg);
349 ret = spi_sync(spi, &msg);
351 dev_err(&spi->dev, "SPI transaction failed\n");
355 "can't read data with the following length: %u\n",
363 static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
365 struct spi_device *spi = to_spi_device(wilc->dev);
369 struct spi_message msg;
370 struct spi_transfer tr = {
377 .unit = SPI_DELAY_UNIT_USECS
382 memset(&msg, 0, sizeof(msg));
383 spi_message_init(&msg);
386 spi_message_add_tail(&tr, &msg);
387 ret = spi_sync(spi, &msg);
389 dev_err(&spi->dev, "SPI transaction failed\n");
392 "can't read data with the following length: %u\n",
400 static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
402 struct spi_device *spi = to_spi_device(wilc->dev);
403 struct wilc_spi *spi_priv = wilc->bus_data;
406 u8 cmd, order, crc[2];
414 if (sz <= DATA_PKT_SZ) {
418 nbytes = DATA_PKT_SZ;
431 if (wilc_spi_tx(wilc, &cmd, 1)) {
433 "Failed data block cmd write, bus error...\n");
441 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
443 "Failed data block write, bus error...\n");
451 if (spi_priv->crc16_enabled) {
452 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
453 crc[0] = crc_calc >> 8;
455 if (wilc_spi_tx(wilc, crc, 2)) {
456 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
463 * No need to wait for response
472 /********************************************
474 * Spi Internal Read/Write Function
476 ********************************************/
477 static u8 wilc_get_crc7(u8 *buffer, u32 len)
479 return crc7_be(0xfe, buffer, len);
482 static int wilc_spi_single_read(struct wilc *wilc, u8 cmd, u32 adr, void *b,
485 struct spi_device *spi = to_spi_device(wilc->dev);
486 struct wilc_spi *spi_priv = wilc->bus_data;
488 int cmd_len, resp_len, i;
489 u16 crc_calc, crc_recv;
490 struct wilc_spi_cmd *c;
491 struct wilc_spi_rsp_data *r;
492 struct wilc_spi_read_rsp_data *r_data;
494 memset(wb, 0x0, sizeof(wb));
495 memset(rb, 0x0, sizeof(rb));
496 c = (struct wilc_spi_cmd *)wb;
498 if (cmd == CMD_SINGLE_READ) {
499 c->u.simple_cmd.addr[0] = adr >> 16;
500 c->u.simple_cmd.addr[1] = adr >> 8;
501 c->u.simple_cmd.addr[2] = adr;
502 } else if (cmd == CMD_INTERNAL_READ) {
503 c->u.simple_cmd.addr[0] = adr >> 8;
505 c->u.simple_cmd.addr[0] |= BIT(7);
506 c->u.simple_cmd.addr[1] = adr;
507 c->u.simple_cmd.addr[2] = 0x0;
509 dev_err(&spi->dev, "cmd [%x] not supported\n", cmd);
513 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
514 resp_len = sizeof(*r) + sizeof(*r_data) + WILC_SPI_RSP_HDR_EXTRA_DATA;
516 if (spi_priv->crc7_enabled) {
517 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
522 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
524 "spi buffer size too small (%d) (%d) (%zu)\n",
525 cmd_len, resp_len, ARRAY_SIZE(wb));
529 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
530 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
534 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
535 if (r->rsp_cmd_type != cmd && !clockless) {
536 if (!spi_priv->probing_crc)
538 "Failed cmd, cmd (%02x), resp (%02x)\n",
539 cmd, r->rsp_cmd_type);
543 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
544 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
549 for (i = 0; i < WILC_SPI_RSP_HDR_EXTRA_DATA; ++i)
550 if (WILC_GET_RESP_HDR_START(r->data[i]) == 0xf)
553 if (i >= WILC_SPI_RSP_HDR_EXTRA_DATA) {
554 dev_err(&spi->dev, "Error, data start missing\n");
558 r_data = (struct wilc_spi_read_rsp_data *)&r->data[i];
561 memcpy(b, r_data->data, 4);
563 if (!clockless && spi_priv->crc16_enabled) {
564 crc_recv = (r_data->crc[0] << 8) | r_data->crc[1];
565 crc_calc = crc_itu_t(0xffff, r_data->data, 4);
566 if (crc_recv != crc_calc) {
567 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
568 "(calculated 0x%04x)\n", __func__,
577 static int wilc_spi_write_cmd(struct wilc *wilc, u8 cmd, u32 adr, u32 data,
580 struct spi_device *spi = to_spi_device(wilc->dev);
581 struct wilc_spi *spi_priv = wilc->bus_data;
583 int cmd_len, resp_len;
584 struct wilc_spi_cmd *c;
585 struct wilc_spi_rsp_data *r;
587 memset(wb, 0x0, sizeof(wb));
588 memset(rb, 0x0, sizeof(rb));
589 c = (struct wilc_spi_cmd *)wb;
591 if (cmd == CMD_INTERNAL_WRITE) {
592 c->u.internal_w_cmd.addr[0] = adr >> 8;
594 c->u.internal_w_cmd.addr[0] |= BIT(7);
596 c->u.internal_w_cmd.addr[1] = adr;
597 c->u.internal_w_cmd.data = cpu_to_be32(data);
598 cmd_len = offsetof(struct wilc_spi_cmd, u.internal_w_cmd.crc);
599 if (spi_priv->crc7_enabled)
600 c->u.internal_w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
601 } else if (cmd == CMD_SINGLE_WRITE) {
602 c->u.w_cmd.addr[0] = adr >> 16;
603 c->u.w_cmd.addr[1] = adr >> 8;
604 c->u.w_cmd.addr[2] = adr;
605 c->u.w_cmd.data = cpu_to_be32(data);
606 cmd_len = offsetof(struct wilc_spi_cmd, u.w_cmd.crc);
607 if (spi_priv->crc7_enabled)
608 c->u.w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
610 dev_err(&spi->dev, "write cmd [%x] not supported\n", cmd);
614 if (spi_priv->crc7_enabled)
617 resp_len = sizeof(*r);
619 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
621 "spi buffer size too small (%d) (%d) (%zu)\n",
622 cmd_len, resp_len, ARRAY_SIZE(wb));
626 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
627 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
631 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
633 * Clockless registers operations might return unexptected responses,
634 * even if successful.
636 if (r->rsp_cmd_type != cmd && !clockless) {
638 "Failed cmd response, cmd (%02x), resp (%02x)\n",
639 cmd, r->rsp_cmd_type);
643 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
644 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
652 static int wilc_spi_dma_rw(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz)
654 struct spi_device *spi = to_spi_device(wilc->dev);
655 struct wilc_spi *spi_priv = wilc->bus_data;
656 u16 crc_recv, crc_calc;
658 int cmd_len, resp_len;
661 struct wilc_spi_cmd *c;
662 struct wilc_spi_rsp_data *r;
664 memset(wb, 0x0, sizeof(wb));
665 memset(rb, 0x0, sizeof(rb));
666 c = (struct wilc_spi_cmd *)wb;
668 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_READ) {
669 c->u.dma_cmd.addr[0] = adr >> 16;
670 c->u.dma_cmd.addr[1] = adr >> 8;
671 c->u.dma_cmd.addr[2] = adr;
672 c->u.dma_cmd.size[0] = sz >> 8;
673 c->u.dma_cmd.size[1] = sz;
674 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd.crc);
675 if (spi_priv->crc7_enabled)
676 c->u.dma_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
677 } else if (cmd == CMD_DMA_EXT_WRITE || cmd == CMD_DMA_EXT_READ) {
678 c->u.dma_cmd_ext.addr[0] = adr >> 16;
679 c->u.dma_cmd_ext.addr[1] = adr >> 8;
680 c->u.dma_cmd_ext.addr[2] = adr;
681 c->u.dma_cmd_ext.size[0] = sz >> 16;
682 c->u.dma_cmd_ext.size[1] = sz >> 8;
683 c->u.dma_cmd_ext.size[2] = sz;
684 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd_ext.crc);
685 if (spi_priv->crc7_enabled)
686 c->u.dma_cmd_ext.crc[0] = wilc_get_crc7(wb, cmd_len);
688 dev_err(&spi->dev, "dma read write cmd [%x] not supported\n",
692 if (spi_priv->crc7_enabled)
695 resp_len = sizeof(*r);
697 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
698 dev_err(&spi->dev, "spi buffer size too small (%d)(%d) (%zu)\n",
699 cmd_len, resp_len, ARRAY_SIZE(wb));
703 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
704 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
708 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
709 if (r->rsp_cmd_type != cmd) {
711 "Failed cmd response, cmd (%02x), resp (%02x)\n",
712 cmd, r->rsp_cmd_type);
716 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
717 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
722 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_EXT_WRITE)
729 nbytes = min_t(u32, sz, DATA_PKT_SZ);
732 * Data Response header
736 if (wilc_spi_rx(wilc, &rsp, 1)) {
738 "Failed resp read, bus err\n");
741 if (WILC_GET_RESP_HDR_START(rsp) == 0xf)
748 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
750 "Failed block read, bus err\n");
757 if (spi_priv->crc16_enabled) {
758 if (wilc_spi_rx(wilc, crc, 2)) {
760 "Failed block CRC read, bus err\n");
763 crc_recv = (crc[0] << 8) | crc[1];
764 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
765 if (crc_recv != crc_calc) {
766 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
767 "(calculated 0x%04x)\n", __func__,
779 static int wilc_spi_special_cmd(struct wilc *wilc, u8 cmd)
781 struct spi_device *spi = to_spi_device(wilc->dev);
782 struct wilc_spi *spi_priv = wilc->bus_data;
784 int cmd_len, resp_len = 0;
785 struct wilc_spi_cmd *c;
786 struct wilc_spi_special_cmd_rsp *r;
788 if (cmd != CMD_TERMINATE && cmd != CMD_REPEAT && cmd != CMD_RESET)
791 memset(wb, 0x0, sizeof(wb));
792 memset(rb, 0x0, sizeof(rb));
793 c = (struct wilc_spi_cmd *)wb;
796 if (cmd == CMD_RESET)
797 memset(c->u.simple_cmd.addr, 0xFF, 3);
799 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
800 resp_len = sizeof(*r);
802 if (spi_priv->crc7_enabled) {
803 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
806 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
807 dev_err(&spi->dev, "spi buffer size too small (%d) (%d) (%zu)\n",
808 cmd_len, resp_len, ARRAY_SIZE(wb));
812 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
813 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
817 r = (struct wilc_spi_special_cmd_rsp *)&rb[cmd_len];
818 if (r->rsp_cmd_type != cmd) {
819 if (!spi_priv->probing_crc)
821 "Failed cmd response, cmd (%02x), resp (%02x)\n",
822 cmd, r->rsp_cmd_type);
826 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
827 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
834 static void wilc_spi_reset_cmd_sequence(struct wilc *wl, u8 attempt, u32 addr)
836 struct spi_device *spi = to_spi_device(wl->dev);
837 struct wilc_spi *spi_priv = wl->bus_data;
839 if (!spi_priv->probing_crc)
840 dev_err(&spi->dev, "Reset and retry %d %x\n", attempt, addr);
842 usleep_range(1000, 1100);
844 usleep_range(1000, 1100);
847 static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
849 struct spi_device *spi = to_spi_device(wilc->dev);
851 u8 cmd = CMD_SINGLE_READ;
855 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
856 /* Clockless register */
857 cmd = CMD_INTERNAL_READ;
861 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
862 result = wilc_spi_single_read(wilc, cmd, addr, data, clockless);
868 /* retry is not applicable for clockless registers */
872 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
873 wilc_spi_reset_cmd_sequence(wilc, i, addr);
879 static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
881 struct spi_device *spi = to_spi_device(wilc->dev);
888 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
889 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_READ, addr,
894 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
896 wilc_spi_reset_cmd_sequence(wilc, i, addr);
902 static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
904 struct spi_device *spi = to_spi_device(wilc->dev);
908 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
909 result = wilc_spi_write_cmd(wilc, CMD_INTERNAL_WRITE, adr,
913 dev_err(&spi->dev, "Failed internal write cmd...\n");
915 wilc_spi_reset_cmd_sequence(wilc, i, adr);
921 static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
923 struct spi_device *spi = to_spi_device(wilc->dev);
924 struct wilc_spi *spi_priv = wilc->bus_data;
928 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
929 result = wilc_spi_single_read(wilc, CMD_INTERNAL_READ, adr,
935 if (!spi_priv->probing_crc)
936 dev_err(&spi->dev, "Failed internal read cmd...\n");
938 wilc_spi_reset_cmd_sequence(wilc, i, adr);
944 /********************************************
948 ********************************************/
950 static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
952 struct spi_device *spi = to_spi_device(wilc->dev);
954 u8 cmd = CMD_SINGLE_WRITE;
958 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
959 /* Clockless register */
960 cmd = CMD_INTERNAL_WRITE;
964 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
965 result = wilc_spi_write_cmd(wilc, cmd, addr, data, clockless);
969 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
974 wilc_spi_reset_cmd_sequence(wilc, i, addr);
979 static int spi_data_rsp(struct wilc *wilc, u8 cmd)
981 struct spi_device *spi = to_spi_device(wilc->dev);
986 * The response to data packets is two bytes long. For
987 * efficiency's sake, wilc_spi_write() wisely ignores the
988 * responses for all packets but the final one. The downside
989 * of that optimization is that when the final data packet is
990 * short, we may receive (part of) the response to the
991 * second-to-last packet before the one for the final packet.
992 * To handle this, we always read 4 bytes and then search for
993 * the last byte that contains the "Response Start" code (0xc
994 * in the top 4 bits). We then know that this byte is the
995 * first response byte of the final data packet.
997 result = wilc_spi_rx(wilc, rsp, sizeof(rsp));
999 dev_err(&spi->dev, "Failed bus error...\n");
1003 for (i = sizeof(rsp) - 2; i >= 0; --i)
1004 if (FIELD_GET(RSP_START_FIELD, rsp[i]) == RSP_START_TAG)
1009 "Data packet response missing (%02x %02x %02x %02x)\n",
1010 rsp[0], rsp[1], rsp[2], rsp[3]);
1014 /* rsp[i] is the last response start byte */
1016 if (FIELD_GET(RSP_TYPE_FIELD, rsp[i]) != RSP_TYPE_LAST_PACKET
1017 || rsp[i + 1] != RSP_STATE_NO_ERROR) {
1018 dev_err(&spi->dev, "Data response error (%02x %02x)\n",
1019 rsp[i], rsp[i + 1]);
1025 static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
1027 struct spi_device *spi = to_spi_device(wilc->dev);
1032 * has to be greated than 4
1037 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
1038 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_WRITE, addr,
1042 "Failed cmd, write block (%08x)...\n", addr);
1043 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1050 result = spi_data_write(wilc, buf, size);
1052 dev_err(&spi->dev, "Failed block data write...\n");
1053 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1060 result = spi_data_rsp(wilc, CMD_DMA_EXT_WRITE);
1062 dev_err(&spi->dev, "Failed block data rsp...\n");
1063 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1071 /********************************************
1075 ********************************************/
1077 static int wilc_spi_reset(struct wilc *wilc)
1079 struct spi_device *spi = to_spi_device(wilc->dev);
1080 struct wilc_spi *spi_priv = wilc->bus_data;
1083 result = wilc_spi_special_cmd(wilc, CMD_RESET);
1084 if (result && !spi_priv->probing_crc)
1085 dev_err(&spi->dev, "Failed cmd reset\n");
1090 static bool wilc_spi_is_init(struct wilc *wilc)
1092 struct wilc_spi *spi_priv = wilc->bus_data;
1094 return spi_priv->isinit;
1097 static int wilc_spi_deinit(struct wilc *wilc)
1099 struct wilc_spi *spi_priv = wilc->bus_data;
1101 spi_priv->isinit = false;
1102 wilc_wlan_power(wilc, false);
1106 static int wilc_spi_init(struct wilc *wilc, bool resume)
1108 struct spi_device *spi = to_spi_device(wilc->dev);
1109 struct wilc_spi *spi_priv = wilc->bus_data;
1114 if (spi_priv->isinit) {
1115 /* Confirm we can read chipid register without error: */
1116 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1120 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1123 wilc_wlan_power(wilc, true);
1126 * configure protocol
1130 * Infer the CRC settings that are currently in effect. This
1131 * is necessary because we can't be sure that the chip has
1132 * been RESET (e.g, after module unload and reload).
1134 spi_priv->probing_crc = true;
1135 spi_priv->crc7_enabled = enable_crc7;
1136 spi_priv->crc16_enabled = false; /* don't check CRC16 during probing */
1137 for (i = 0; i < 2; ++i) {
1138 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1141 spi_priv->crc7_enabled = !enable_crc7;
1144 dev_err(&spi->dev, "Failed with CRC7 on and off.\n");
1148 /* set up the desired CRC configuration: */
1149 reg &= ~(PROTOCOL_REG_CRC7_MASK | PROTOCOL_REG_CRC16_MASK);
1151 reg |= PROTOCOL_REG_CRC7_MASK;
1153 reg |= PROTOCOL_REG_CRC16_MASK;
1155 /* set up the data packet size: */
1156 BUILD_BUG_ON(DATA_PKT_LOG_SZ < DATA_PKT_LOG_SZ_MIN
1157 || DATA_PKT_LOG_SZ > DATA_PKT_LOG_SZ_MAX);
1158 reg &= ~PROTOCOL_REG_PKT_SZ_MASK;
1159 reg |= FIELD_PREP(PROTOCOL_REG_PKT_SZ_MASK,
1160 DATA_PKT_LOG_SZ - DATA_PKT_LOG_SZ_MIN);
1162 /* establish the new setup: */
1163 ret = spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg);
1166 "[wilc spi %d]: Failed internal write reg\n",
1170 /* update our state to match new protocol settings: */
1171 spi_priv->crc7_enabled = enable_crc7;
1172 spi_priv->crc16_enabled = enable_crc16;
1174 /* re-read to make sure new settings are in effect: */
1175 spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1177 spi_priv->probing_crc = false;
1180 * make sure can read chip id without protocol error
1182 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1184 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1188 spi_priv->isinit = true;
1193 static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
1197 ret = spi_internal_read(wilc,
1198 WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE, size);
1199 *size = FIELD_GET(IRQ_DMA_WD_CNT_MASK, *size);
1204 static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
1206 return spi_internal_read(wilc, WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE,
1210 static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
1213 int retry = SPI_ENABLE_VMM_RETRY_LIMIT;
1217 ret = spi_internal_write(wilc,
1218 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1223 ret = spi_internal_read(wilc,
1224 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1226 if (ret || ((check & EN_VMM) == (val & EN_VMM)))
1234 static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
1236 struct spi_device *spi = to_spi_device(wilc->dev);
1240 if (nint > MAX_NUM_INT) {
1241 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
1246 * interrupt pin mux select
1248 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
1250 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1255 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
1257 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1265 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
1267 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1272 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1273 reg |= (BIT((27 + i)));
1275 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
1277 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1282 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1284 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1289 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1292 ret = wilc_spi_write_reg(wilc, WILC_INTR2_ENABLE, reg);
1294 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1303 /* Global spi HIF function table */
1304 static const struct wilc_hif_func wilc_hif_spi = {
1305 .hif_init = wilc_spi_init,
1306 .hif_deinit = wilc_spi_deinit,
1307 .hif_read_reg = wilc_spi_read_reg,
1308 .hif_write_reg = wilc_spi_write_reg,
1309 .hif_block_rx = wilc_spi_read,
1310 .hif_block_tx = wilc_spi_write,
1311 .hif_read_int = wilc_spi_read_int,
1312 .hif_clear_int_ext = wilc_spi_clear_int_ext,
1313 .hif_read_size = wilc_spi_read_size,
1314 .hif_block_tx_ext = wilc_spi_write,
1315 .hif_block_rx_ext = wilc_spi_read,
1316 .hif_sync_ext = wilc_spi_sync_ext,
1317 .hif_reset = wilc_spi_reset,
1318 .hif_is_init = wilc_spi_is_init,