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 wilc_spi_init was successful */
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);
58 static int wilc_spi_configure_bus_protocol(struct wilc *wilc);
59 static int wilc_validate_chipid(struct wilc *wilc);
61 /********************************************
63 * Spi protocol Function
65 ********************************************/
67 #define CMD_DMA_WRITE 0xc1
68 #define CMD_DMA_READ 0xc2
69 #define CMD_INTERNAL_WRITE 0xc3
70 #define CMD_INTERNAL_READ 0xc4
71 #define CMD_TERMINATE 0xc5
72 #define CMD_REPEAT 0xc6
73 #define CMD_DMA_EXT_WRITE 0xc7
74 #define CMD_DMA_EXT_READ 0xc8
75 #define CMD_SINGLE_WRITE 0xc9
76 #define CMD_SINGLE_READ 0xca
77 #define CMD_RESET 0xcf
79 #define SPI_RETRY_MAX_LIMIT 10
80 #define SPI_ENABLE_VMM_RETRY_LIMIT 2
82 /* SPI response fields (section 11.1.2 in ATWILC1000 User Guide): */
83 #define RSP_START_FIELD GENMASK(7, 4)
84 #define RSP_TYPE_FIELD GENMASK(3, 0)
86 /* SPI response values for the response fields: */
87 #define RSP_START_TAG 0xc
88 #define RSP_TYPE_FIRST_PACKET 0x1
89 #define RSP_TYPE_INNER_PACKET 0x2
90 #define RSP_TYPE_LAST_PACKET 0x3
91 #define RSP_STATE_NO_ERROR 0x00
93 #define PROTOCOL_REG_PKT_SZ_MASK GENMASK(6, 4)
94 #define PROTOCOL_REG_CRC16_MASK GENMASK(3, 3)
95 #define PROTOCOL_REG_CRC7_MASK GENMASK(2, 2)
98 * The SPI data packet size may be any integer power of two in the
99 * range from 256 to 8192 bytes.
101 #define DATA_PKT_LOG_SZ_MIN 8 /* 256 B */
102 #define DATA_PKT_LOG_SZ_MAX 13 /* 8 KiB */
105 * Select the data packet size (log2 of number of bytes): Use the
106 * maximum data packet size. We only retransmit complete packets, so
107 * there is no benefit from using smaller data packets.
109 #define DATA_PKT_LOG_SZ DATA_PKT_LOG_SZ_MAX
110 #define DATA_PKT_SZ (1 << DATA_PKT_LOG_SZ)
112 #define WILC_SPI_COMMAND_STAT_SUCCESS 0
113 #define WILC_GET_RESP_HDR_START(h) (((h) >> 4) & 0xf)
115 struct wilc_spi_cmd {
121 } __packed simple_cmd;
131 } __packed dma_cmd_ext;
136 } __packed internal_w_cmd;
145 struct wilc_spi_read_rsp_data {
151 struct wilc_spi_rsp_data {
157 struct wilc_spi_special_cmd_rsp {
163 static int wilc_parse_gpios(struct wilc *wilc)
165 struct spi_device *spi = to_spi_device(wilc->dev);
166 struct wilc_spi *spi_priv = wilc->bus_data;
167 struct wilc_gpios *gpios = &spi_priv->gpios;
169 /* get ENABLE pin and deassert it (if it is defined): */
170 gpios->enable = devm_gpiod_get_optional(&spi->dev,
171 "enable", GPIOD_OUT_LOW);
172 /* get RESET pin and assert it (if it is defined): */
174 /* if enable pin exists, reset must exist as well */
175 gpios->reset = devm_gpiod_get(&spi->dev,
176 "reset", GPIOD_OUT_HIGH);
177 if (IS_ERR(gpios->reset)) {
178 dev_err(&spi->dev, "missing reset gpio.\n");
179 return PTR_ERR(gpios->reset);
182 gpios->reset = devm_gpiod_get_optional(&spi->dev,
183 "reset", GPIOD_OUT_HIGH);
188 static void wilc_wlan_power(struct wilc *wilc, bool on)
190 struct wilc_spi *spi_priv = wilc->bus_data;
191 struct wilc_gpios *gpios = &spi_priv->gpios;
195 gpiod_set_value(gpios->enable, 1);
197 /* deassert RESET: */
198 gpiod_set_value(gpios->reset, 0);
201 gpiod_set_value(gpios->reset, 1);
202 /* deassert ENABLE: */
203 gpiod_set_value(gpios->enable, 0);
207 static int wilc_bus_probe(struct spi_device *spi)
209 struct wilc_spi *spi_priv;
210 struct wilc_vif *vif;
214 spi_priv = kzalloc(sizeof(*spi_priv), GFP_KERNEL);
218 ret = wilc_cfg80211_init(&wilc, &spi->dev, WILC_HIF_SPI, &wilc_hif_spi);
222 spi_set_drvdata(spi, wilc);
223 wilc->dev = &spi->dev;
224 wilc->bus_data = spi_priv;
225 wilc->dev_irq_num = spi->irq;
227 ret = wilc_parse_gpios(wilc);
231 wilc->rtc_clk = devm_clk_get_optional(&spi->dev, "rtc");
232 if (IS_ERR(wilc->rtc_clk)) {
233 ret = PTR_ERR(wilc->rtc_clk);
236 clk_prepare_enable(wilc->rtc_clk);
238 dev_info(&spi->dev, "Selected CRC config: crc7=%s, crc16=%s\n",
239 enable_crc7 ? "on" : "off", enable_crc16 ? "on" : "off");
241 /* we need power to configure the bus protocol and to read the chip id: */
243 wilc_wlan_power(wilc, true);
245 ret = wilc_spi_configure_bus_protocol(wilc);
249 ret = wilc_validate_chipid(wilc);
253 ret = wilc_load_mac_from_nv(wilc);
255 pr_err("Can not retrieve MAC address from chip\n");
259 wilc_wlan_power(wilc, false);
260 vif = wilc_netdev_ifc_init(wilc, "wlan%d", WILC_STATION_MODE,
261 NL80211_IFTYPE_STATION, false);
269 clk_disable_unprepare(wilc->rtc_clk);
270 wilc_wlan_power(wilc, false);
272 wilc_netdev_cleanup(wilc);
278 static void wilc_bus_remove(struct spi_device *spi)
280 struct wilc *wilc = spi_get_drvdata(spi);
281 struct wilc_spi *spi_priv = wilc->bus_data;
283 clk_disable_unprepare(wilc->rtc_clk);
284 wilc_netdev_cleanup(wilc);
288 static const struct of_device_id wilc_of_match[] = {
289 { .compatible = "microchip,wilc1000", },
292 MODULE_DEVICE_TABLE(of, wilc_of_match);
294 static const struct spi_device_id wilc_spi_id[] = {
298 MODULE_DEVICE_TABLE(spi, wilc_spi_id);
300 static struct spi_driver wilc_spi_driver = {
302 .name = SPI_MODALIAS,
303 .of_match_table = wilc_of_match,
305 .id_table = wilc_spi_id,
306 .probe = wilc_bus_probe,
307 .remove = wilc_bus_remove,
309 module_spi_driver(wilc_spi_driver);
310 MODULE_DESCRIPTION("Atmel WILC1000 SPI wireless driver");
311 MODULE_LICENSE("GPL");
313 static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
315 struct spi_device *spi = to_spi_device(wilc->dev);
317 struct spi_message msg;
320 struct spi_transfer tr = {
325 .unit = SPI_DELAY_UNIT_USECS
328 char *r_buffer = kzalloc(len, GFP_KERNEL);
333 tr.rx_buf = r_buffer;
334 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
336 memset(&msg, 0, sizeof(msg));
337 spi_message_init(&msg);
338 spi_message_add_tail(&tr, &msg);
340 ret = spi_sync(spi, &msg);
342 dev_err(&spi->dev, "SPI transaction failed\n");
347 "can't write data with the following length: %d\n",
355 static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
357 struct spi_device *spi = to_spi_device(wilc->dev);
361 struct spi_message msg;
362 struct spi_transfer tr = {
367 .unit = SPI_DELAY_UNIT_USECS
371 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
376 tr.tx_buf = t_buffer;
378 memset(&msg, 0, sizeof(msg));
379 spi_message_init(&msg);
380 spi_message_add_tail(&tr, &msg);
382 ret = spi_sync(spi, &msg);
384 dev_err(&spi->dev, "SPI transaction failed\n");
388 "can't read data with the following length: %u\n",
396 static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
398 struct spi_device *spi = to_spi_device(wilc->dev);
402 struct spi_message msg;
403 struct spi_transfer tr = {
410 .unit = SPI_DELAY_UNIT_USECS
415 memset(&msg, 0, sizeof(msg));
416 spi_message_init(&msg);
417 spi_message_add_tail(&tr, &msg);
418 ret = spi_sync(spi, &msg);
420 dev_err(&spi->dev, "SPI transaction failed\n");
423 "can't read data with the following length: %u\n",
431 static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
433 struct spi_device *spi = to_spi_device(wilc->dev);
434 struct wilc_spi *spi_priv = wilc->bus_data;
437 u8 cmd, order, crc[2];
445 if (sz <= DATA_PKT_SZ) {
449 nbytes = DATA_PKT_SZ;
462 if (wilc_spi_tx(wilc, &cmd, 1)) {
464 "Failed data block cmd write, bus error...\n");
472 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
474 "Failed data block write, bus error...\n");
482 if (spi_priv->crc16_enabled) {
483 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
484 crc[0] = crc_calc >> 8;
486 if (wilc_spi_tx(wilc, crc, 2)) {
487 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
494 * No need to wait for response
503 /********************************************
505 * Spi Internal Read/Write Function
507 ********************************************/
508 static u8 wilc_get_crc7(u8 *buffer, u32 len)
510 return crc7_be(0xfe, buffer, len) | 0x01;
513 static int wilc_spi_single_read(struct wilc *wilc, u8 cmd, u32 adr, void *b,
516 struct spi_device *spi = to_spi_device(wilc->dev);
517 struct wilc_spi *spi_priv = wilc->bus_data;
519 int cmd_len, resp_len, i;
520 u16 crc_calc, crc_recv;
521 struct wilc_spi_cmd *c;
522 struct wilc_spi_rsp_data *r;
523 struct wilc_spi_read_rsp_data *r_data;
525 memset(wb, 0x0, sizeof(wb));
526 memset(rb, 0x0, sizeof(rb));
527 c = (struct wilc_spi_cmd *)wb;
529 if (cmd == CMD_SINGLE_READ) {
530 c->u.simple_cmd.addr[0] = adr >> 16;
531 c->u.simple_cmd.addr[1] = adr >> 8;
532 c->u.simple_cmd.addr[2] = adr;
533 } else if (cmd == CMD_INTERNAL_READ) {
534 c->u.simple_cmd.addr[0] = adr >> 8;
536 c->u.simple_cmd.addr[0] |= BIT(7);
537 c->u.simple_cmd.addr[1] = adr;
538 c->u.simple_cmd.addr[2] = 0x0;
540 dev_err(&spi->dev, "cmd [%x] not supported\n", cmd);
544 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
545 resp_len = sizeof(*r) + sizeof(*r_data) + WILC_SPI_RSP_HDR_EXTRA_DATA;
547 if (spi_priv->crc7_enabled) {
548 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
553 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
555 "spi buffer size too small (%d) (%d) (%zu)\n",
556 cmd_len, resp_len, ARRAY_SIZE(wb));
560 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
561 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
565 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
566 if (r->rsp_cmd_type != cmd && !clockless) {
567 if (!spi_priv->probing_crc)
569 "Failed cmd, cmd (%02x), resp (%02x)\n",
570 cmd, r->rsp_cmd_type);
574 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
575 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
580 for (i = 0; i < WILC_SPI_RSP_HDR_EXTRA_DATA; ++i)
581 if (WILC_GET_RESP_HDR_START(r->data[i]) == 0xf)
584 if (i >= WILC_SPI_RSP_HDR_EXTRA_DATA) {
585 dev_err(&spi->dev, "Error, data start missing\n");
589 r_data = (struct wilc_spi_read_rsp_data *)&r->data[i];
592 memcpy(b, r_data->data, 4);
594 if (!clockless && spi_priv->crc16_enabled) {
595 crc_recv = (r_data->crc[0] << 8) | r_data->crc[1];
596 crc_calc = crc_itu_t(0xffff, r_data->data, 4);
597 if (crc_recv != crc_calc) {
598 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
599 "(calculated 0x%04x)\n", __func__,
608 static int wilc_spi_write_cmd(struct wilc *wilc, u8 cmd, u32 adr, u32 data,
611 struct spi_device *spi = to_spi_device(wilc->dev);
612 struct wilc_spi *spi_priv = wilc->bus_data;
614 int cmd_len, resp_len;
615 struct wilc_spi_cmd *c;
616 struct wilc_spi_rsp_data *r;
618 memset(wb, 0x0, sizeof(wb));
619 memset(rb, 0x0, sizeof(rb));
620 c = (struct wilc_spi_cmd *)wb;
622 if (cmd == CMD_INTERNAL_WRITE) {
623 c->u.internal_w_cmd.addr[0] = adr >> 8;
625 c->u.internal_w_cmd.addr[0] |= BIT(7);
627 c->u.internal_w_cmd.addr[1] = adr;
628 c->u.internal_w_cmd.data = cpu_to_be32(data);
629 cmd_len = offsetof(struct wilc_spi_cmd, u.internal_w_cmd.crc);
630 if (spi_priv->crc7_enabled)
631 c->u.internal_w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
632 } else if (cmd == CMD_SINGLE_WRITE) {
633 c->u.w_cmd.addr[0] = adr >> 16;
634 c->u.w_cmd.addr[1] = adr >> 8;
635 c->u.w_cmd.addr[2] = adr;
636 c->u.w_cmd.data = cpu_to_be32(data);
637 cmd_len = offsetof(struct wilc_spi_cmd, u.w_cmd.crc);
638 if (spi_priv->crc7_enabled)
639 c->u.w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
641 dev_err(&spi->dev, "write cmd [%x] not supported\n", cmd);
645 if (spi_priv->crc7_enabled)
648 resp_len = sizeof(*r);
650 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
652 "spi buffer size too small (%d) (%d) (%zu)\n",
653 cmd_len, resp_len, ARRAY_SIZE(wb));
657 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
658 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
662 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
664 * Clockless registers operations might return unexptected responses,
665 * even if successful.
667 if (r->rsp_cmd_type != cmd && !clockless) {
669 "Failed cmd response, cmd (%02x), resp (%02x)\n",
670 cmd, r->rsp_cmd_type);
674 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
675 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
683 static int wilc_spi_dma_rw(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz)
685 struct spi_device *spi = to_spi_device(wilc->dev);
686 struct wilc_spi *spi_priv = wilc->bus_data;
687 u16 crc_recv, crc_calc;
689 int cmd_len, resp_len;
692 struct wilc_spi_cmd *c;
693 struct wilc_spi_rsp_data *r;
695 memset(wb, 0x0, sizeof(wb));
696 memset(rb, 0x0, sizeof(rb));
697 c = (struct wilc_spi_cmd *)wb;
699 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_READ) {
700 c->u.dma_cmd.addr[0] = adr >> 16;
701 c->u.dma_cmd.addr[1] = adr >> 8;
702 c->u.dma_cmd.addr[2] = adr;
703 c->u.dma_cmd.size[0] = sz >> 8;
704 c->u.dma_cmd.size[1] = sz;
705 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd.crc);
706 if (spi_priv->crc7_enabled)
707 c->u.dma_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
708 } else if (cmd == CMD_DMA_EXT_WRITE || cmd == CMD_DMA_EXT_READ) {
709 c->u.dma_cmd_ext.addr[0] = adr >> 16;
710 c->u.dma_cmd_ext.addr[1] = adr >> 8;
711 c->u.dma_cmd_ext.addr[2] = adr;
712 c->u.dma_cmd_ext.size[0] = sz >> 16;
713 c->u.dma_cmd_ext.size[1] = sz >> 8;
714 c->u.dma_cmd_ext.size[2] = sz;
715 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd_ext.crc);
716 if (spi_priv->crc7_enabled)
717 c->u.dma_cmd_ext.crc[0] = wilc_get_crc7(wb, cmd_len);
719 dev_err(&spi->dev, "dma read write cmd [%x] not supported\n",
723 if (spi_priv->crc7_enabled)
726 resp_len = sizeof(*r);
728 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
729 dev_err(&spi->dev, "spi buffer size too small (%d)(%d) (%zu)\n",
730 cmd_len, resp_len, ARRAY_SIZE(wb));
734 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
735 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
739 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
740 if (r->rsp_cmd_type != cmd) {
742 "Failed cmd response, cmd (%02x), resp (%02x)\n",
743 cmd, r->rsp_cmd_type);
747 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
748 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
753 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_EXT_WRITE)
760 nbytes = min_t(u32, sz, DATA_PKT_SZ);
763 * Data Response header
767 if (wilc_spi_rx(wilc, &rsp, 1)) {
769 "Failed resp read, bus err\n");
772 if (WILC_GET_RESP_HDR_START(rsp) == 0xf)
779 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
781 "Failed block read, bus err\n");
788 if (spi_priv->crc16_enabled) {
789 if (wilc_spi_rx(wilc, crc, 2)) {
791 "Failed block CRC read, bus err\n");
794 crc_recv = (crc[0] << 8) | crc[1];
795 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
796 if (crc_recv != crc_calc) {
797 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
798 "(calculated 0x%04x)\n", __func__,
810 static int wilc_spi_special_cmd(struct wilc *wilc, u8 cmd)
812 struct spi_device *spi = to_spi_device(wilc->dev);
813 struct wilc_spi *spi_priv = wilc->bus_data;
815 int cmd_len, resp_len = 0;
816 struct wilc_spi_cmd *c;
817 struct wilc_spi_special_cmd_rsp *r;
819 if (cmd != CMD_TERMINATE && cmd != CMD_REPEAT && cmd != CMD_RESET)
822 memset(wb, 0x0, sizeof(wb));
823 memset(rb, 0x0, sizeof(rb));
824 c = (struct wilc_spi_cmd *)wb;
827 if (cmd == CMD_RESET)
828 memset(c->u.simple_cmd.addr, 0xFF, 3);
830 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
831 resp_len = sizeof(*r);
833 if (spi_priv->crc7_enabled) {
834 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
837 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
838 dev_err(&spi->dev, "spi buffer size too small (%d) (%d) (%zu)\n",
839 cmd_len, resp_len, ARRAY_SIZE(wb));
843 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
844 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
848 r = (struct wilc_spi_special_cmd_rsp *)&rb[cmd_len];
849 if (r->rsp_cmd_type != cmd) {
850 if (!spi_priv->probing_crc)
852 "Failed cmd response, cmd (%02x), resp (%02x)\n",
853 cmd, r->rsp_cmd_type);
857 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
858 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
865 static void wilc_spi_reset_cmd_sequence(struct wilc *wl, u8 attempt, u32 addr)
867 struct spi_device *spi = to_spi_device(wl->dev);
868 struct wilc_spi *spi_priv = wl->bus_data;
870 if (!spi_priv->probing_crc)
871 dev_err(&spi->dev, "Reset and retry %d %x\n", attempt, addr);
873 usleep_range(1000, 1100);
875 usleep_range(1000, 1100);
878 static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
880 struct spi_device *spi = to_spi_device(wilc->dev);
882 u8 cmd = CMD_SINGLE_READ;
886 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
887 /* Clockless register */
888 cmd = CMD_INTERNAL_READ;
892 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
893 result = wilc_spi_single_read(wilc, cmd, addr, data, clockless);
899 /* retry is not applicable for clockless registers */
903 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
904 wilc_spi_reset_cmd_sequence(wilc, i, addr);
910 static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
912 struct spi_device *spi = to_spi_device(wilc->dev);
919 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
920 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_READ, addr,
925 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
927 wilc_spi_reset_cmd_sequence(wilc, i, addr);
933 static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
935 struct spi_device *spi = to_spi_device(wilc->dev);
939 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
940 result = wilc_spi_write_cmd(wilc, CMD_INTERNAL_WRITE, adr,
944 dev_err(&spi->dev, "Failed internal write cmd...\n");
946 wilc_spi_reset_cmd_sequence(wilc, i, adr);
952 static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
954 struct spi_device *spi = to_spi_device(wilc->dev);
955 struct wilc_spi *spi_priv = wilc->bus_data;
959 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
960 result = wilc_spi_single_read(wilc, CMD_INTERNAL_READ, adr,
966 if (!spi_priv->probing_crc)
967 dev_err(&spi->dev, "Failed internal read cmd...\n");
969 wilc_spi_reset_cmd_sequence(wilc, i, adr);
975 /********************************************
979 ********************************************/
981 static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
983 struct spi_device *spi = to_spi_device(wilc->dev);
985 u8 cmd = CMD_SINGLE_WRITE;
989 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
990 /* Clockless register */
991 cmd = CMD_INTERNAL_WRITE;
995 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
996 result = wilc_spi_write_cmd(wilc, cmd, addr, data, clockless);
1000 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
1005 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1010 static int spi_data_rsp(struct wilc *wilc, u8 cmd)
1012 struct spi_device *spi = to_spi_device(wilc->dev);
1017 * The response to data packets is two bytes long. For
1018 * efficiency's sake, wilc_spi_write() wisely ignores the
1019 * responses for all packets but the final one. The downside
1020 * of that optimization is that when the final data packet is
1021 * short, we may receive (part of) the response to the
1022 * second-to-last packet before the one for the final packet.
1023 * To handle this, we always read 4 bytes and then search for
1024 * the last byte that contains the "Response Start" code (0xc
1025 * in the top 4 bits). We then know that this byte is the
1026 * first response byte of the final data packet.
1028 result = wilc_spi_rx(wilc, rsp, sizeof(rsp));
1030 dev_err(&spi->dev, "Failed bus error...\n");
1034 for (i = sizeof(rsp) - 2; i >= 0; --i)
1035 if (FIELD_GET(RSP_START_FIELD, rsp[i]) == RSP_START_TAG)
1040 "Data packet response missing (%02x %02x %02x %02x)\n",
1041 rsp[0], rsp[1], rsp[2], rsp[3]);
1045 /* rsp[i] is the last response start byte */
1047 if (FIELD_GET(RSP_TYPE_FIELD, rsp[i]) != RSP_TYPE_LAST_PACKET
1048 || rsp[i + 1] != RSP_STATE_NO_ERROR) {
1049 dev_err(&spi->dev, "Data response error (%02x %02x)\n",
1050 rsp[i], rsp[i + 1]);
1056 static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
1058 struct spi_device *spi = to_spi_device(wilc->dev);
1063 * has to be greated than 4
1068 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
1069 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_WRITE, addr,
1073 "Failed cmd, write block (%08x)...\n", addr);
1074 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1081 result = spi_data_write(wilc, buf, size);
1083 dev_err(&spi->dev, "Failed block data write...\n");
1084 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1091 result = spi_data_rsp(wilc, CMD_DMA_EXT_WRITE);
1093 dev_err(&spi->dev, "Failed block data rsp...\n");
1094 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1102 /********************************************
1106 ********************************************/
1108 static int wilc_spi_reset(struct wilc *wilc)
1110 struct spi_device *spi = to_spi_device(wilc->dev);
1111 struct wilc_spi *spi_priv = wilc->bus_data;
1114 result = wilc_spi_special_cmd(wilc, CMD_RESET);
1115 if (result && !spi_priv->probing_crc)
1116 dev_err(&spi->dev, "Failed cmd reset\n");
1121 static bool wilc_spi_is_init(struct wilc *wilc)
1123 struct wilc_spi *spi_priv = wilc->bus_data;
1125 return spi_priv->isinit;
1128 static int wilc_spi_deinit(struct wilc *wilc)
1130 struct wilc_spi *spi_priv = wilc->bus_data;
1132 spi_priv->isinit = false;
1133 wilc_wlan_power(wilc, false);
1137 static int wilc_spi_init(struct wilc *wilc, bool resume)
1139 struct wilc_spi *spi_priv = wilc->bus_data;
1142 if (spi_priv->isinit) {
1143 /* Confirm we can read chipid register without error: */
1144 if (wilc_validate_chipid(wilc) == 0)
1148 wilc_wlan_power(wilc, true);
1150 ret = wilc_spi_configure_bus_protocol(wilc);
1152 wilc_wlan_power(wilc, false);
1156 spi_priv->isinit = true;
1161 static int wilc_spi_configure_bus_protocol(struct wilc *wilc)
1163 struct spi_device *spi = to_spi_device(wilc->dev);
1164 struct wilc_spi *spi_priv = wilc->bus_data;
1169 * Infer the CRC settings that are currently in effect. This
1170 * is necessary because we can't be sure that the chip has
1171 * been RESET (e.g, after module unload and reload).
1173 spi_priv->probing_crc = true;
1174 spi_priv->crc7_enabled = enable_crc7;
1175 spi_priv->crc16_enabled = false; /* don't check CRC16 during probing */
1176 for (i = 0; i < 2; ++i) {
1177 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1180 spi_priv->crc7_enabled = !enable_crc7;
1183 dev_err(&spi->dev, "Failed with CRC7 on and off.\n");
1187 /* set up the desired CRC configuration: */
1188 reg &= ~(PROTOCOL_REG_CRC7_MASK | PROTOCOL_REG_CRC16_MASK);
1190 reg |= PROTOCOL_REG_CRC7_MASK;
1192 reg |= PROTOCOL_REG_CRC16_MASK;
1194 /* set up the data packet size: */
1195 BUILD_BUG_ON(DATA_PKT_LOG_SZ < DATA_PKT_LOG_SZ_MIN
1196 || DATA_PKT_LOG_SZ > DATA_PKT_LOG_SZ_MAX);
1197 reg &= ~PROTOCOL_REG_PKT_SZ_MASK;
1198 reg |= FIELD_PREP(PROTOCOL_REG_PKT_SZ_MASK,
1199 DATA_PKT_LOG_SZ - DATA_PKT_LOG_SZ_MIN);
1201 /* establish the new setup: */
1202 ret = spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg);
1205 "[wilc spi %d]: Failed internal write reg\n",
1209 /* update our state to match new protocol settings: */
1210 spi_priv->crc7_enabled = enable_crc7;
1211 spi_priv->crc16_enabled = enable_crc16;
1213 /* re-read to make sure new settings are in effect: */
1214 spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1216 spi_priv->probing_crc = false;
1221 static int wilc_validate_chipid(struct wilc *wilc)
1223 struct spi_device *spi = to_spi_device(wilc->dev);
1228 * make sure can read chip id without protocol error
1230 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1232 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1235 if (!is_wilc1000(chipid)) {
1236 dev_err(&spi->dev, "Unknown chip id 0x%x\n", chipid);
1242 static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
1246 ret = spi_internal_read(wilc,
1247 WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE, size);
1248 *size = FIELD_GET(IRQ_DMA_WD_CNT_MASK, *size);
1253 static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
1255 return spi_internal_read(wilc, WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE,
1259 static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
1262 int retry = SPI_ENABLE_VMM_RETRY_LIMIT;
1266 ret = spi_internal_write(wilc,
1267 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1272 ret = spi_internal_read(wilc,
1273 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1275 if (ret || ((check & EN_VMM) == (val & EN_VMM)))
1283 static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
1285 struct spi_device *spi = to_spi_device(wilc->dev);
1289 if (nint > MAX_NUM_INT) {
1290 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
1295 * interrupt pin mux select
1297 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
1299 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1304 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
1306 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1314 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
1316 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1321 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1322 reg |= (BIT((27 + i)));
1324 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
1326 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1331 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1333 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1338 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1341 ret = wilc_spi_write_reg(wilc, WILC_INTR2_ENABLE, reg);
1343 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1352 /* Global spi HIF function table */
1353 static const struct wilc_hif_func wilc_hif_spi = {
1354 .hif_init = wilc_spi_init,
1355 .hif_deinit = wilc_spi_deinit,
1356 .hif_read_reg = wilc_spi_read_reg,
1357 .hif_write_reg = wilc_spi_write_reg,
1358 .hif_block_rx = wilc_spi_read,
1359 .hif_block_tx = wilc_spi_write,
1360 .hif_read_int = wilc_spi_read_int,
1361 .hif_clear_int_ext = wilc_spi_clear_int_ext,
1362 .hif_read_size = wilc_spi_read_size,
1363 .hif_block_tx_ext = wilc_spi_write,
1364 .hif_block_rx_ext = wilc_spi_read,
1365 .hif_sync_ext = wilc_spi_sync_ext,
1366 .hif_reset = wilc_spi_reset,
1367 .hif_is_init = wilc_spi_is_init,