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_enabled(&spi->dev, "rtc");
232 if (IS_ERR(wilc->rtc_clk)) {
233 ret = PTR_ERR(wilc->rtc_clk);
237 dev_info(&spi->dev, "Selected CRC config: crc7=%s, crc16=%s\n",
238 enable_crc7 ? "on" : "off", enable_crc16 ? "on" : "off");
240 /* we need power to configure the bus protocol and to read the chip id: */
242 wilc_wlan_power(wilc, true);
244 ret = wilc_spi_configure_bus_protocol(wilc);
248 ret = wilc_get_chipid(wilc);
252 ret = wilc_cfg80211_register(wilc);
256 ret = wilc_load_mac_from_nv(wilc);
258 pr_err("Can not retrieve MAC address from chip\n");
259 goto unregister_wiphy;
262 wilc_wlan_power(wilc, false);
263 vif = wilc_netdev_ifc_init(wilc, "wlan%d", WILC_STATION_MODE,
264 NL80211_IFTYPE_STATION, false);
267 goto unregister_wiphy;
272 wiphy_unregister(wilc->wiphy);
274 wilc_wlan_power(wilc, false);
276 wilc_netdev_cleanup(wilc);
277 wiphy_free(wilc->wiphy);
283 static void wilc_bus_remove(struct spi_device *spi)
285 struct wilc *wilc = spi_get_drvdata(spi);
286 struct wilc_spi *spi_priv = wilc->bus_data;
288 wilc_netdev_cleanup(wilc);
289 wiphy_unregister(wilc->wiphy);
290 wiphy_free(wilc->wiphy);
294 static const struct of_device_id wilc_of_match[] = {
295 { .compatible = "microchip,wilc1000", },
298 MODULE_DEVICE_TABLE(of, wilc_of_match);
300 static const struct spi_device_id wilc_spi_id[] = {
304 MODULE_DEVICE_TABLE(spi, wilc_spi_id);
306 static struct spi_driver wilc_spi_driver = {
308 .name = SPI_MODALIAS,
309 .of_match_table = wilc_of_match,
311 .id_table = wilc_spi_id,
312 .probe = wilc_bus_probe,
313 .remove = wilc_bus_remove,
315 module_spi_driver(wilc_spi_driver);
316 MODULE_DESCRIPTION("Atmel WILC1000 SPI wireless driver");
317 MODULE_LICENSE("GPL");
319 static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
321 struct spi_device *spi = to_spi_device(wilc->dev);
323 struct spi_message msg;
326 struct spi_transfer tr = {
331 .unit = SPI_DELAY_UNIT_USECS
334 char *r_buffer = kzalloc(len, GFP_KERNEL);
339 tr.rx_buf = r_buffer;
340 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
342 memset(&msg, 0, sizeof(msg));
343 spi_message_init(&msg);
344 spi_message_add_tail(&tr, &msg);
346 ret = spi_sync(spi, &msg);
348 dev_err(&spi->dev, "SPI transaction failed\n");
353 "can't write data with the following length: %d\n",
361 static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
363 struct spi_device *spi = to_spi_device(wilc->dev);
367 struct spi_message msg;
368 struct spi_transfer tr = {
373 .unit = SPI_DELAY_UNIT_USECS
377 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
382 tr.tx_buf = t_buffer;
384 memset(&msg, 0, sizeof(msg));
385 spi_message_init(&msg);
386 spi_message_add_tail(&tr, &msg);
388 ret = spi_sync(spi, &msg);
390 dev_err(&spi->dev, "SPI transaction failed\n");
394 "can't read data with the following length: %u\n",
402 static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
404 struct spi_device *spi = to_spi_device(wilc->dev);
408 struct spi_message msg;
409 struct spi_transfer tr = {
416 .unit = SPI_DELAY_UNIT_USECS
421 memset(&msg, 0, sizeof(msg));
422 spi_message_init(&msg);
423 spi_message_add_tail(&tr, &msg);
424 ret = spi_sync(spi, &msg);
426 dev_err(&spi->dev, "SPI transaction failed\n");
429 "can't read data with the following length: %u\n",
437 static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
439 struct spi_device *spi = to_spi_device(wilc->dev);
440 struct wilc_spi *spi_priv = wilc->bus_data;
443 u8 cmd, order, crc[2];
451 if (sz <= DATA_PKT_SZ) {
455 nbytes = DATA_PKT_SZ;
468 if (wilc_spi_tx(wilc, &cmd, 1)) {
470 "Failed data block cmd write, bus error...\n");
478 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
480 "Failed data block write, bus error...\n");
488 if (spi_priv->crc16_enabled) {
489 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
490 crc[0] = crc_calc >> 8;
492 if (wilc_spi_tx(wilc, crc, 2)) {
493 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
500 * No need to wait for response
509 /********************************************
511 * Spi Internal Read/Write Function
513 ********************************************/
514 static u8 wilc_get_crc7(u8 *buffer, u32 len)
516 return crc7_be(0xfe, buffer, len) | 0x01;
519 static int wilc_spi_single_read(struct wilc *wilc, u8 cmd, u32 adr, void *b,
522 struct spi_device *spi = to_spi_device(wilc->dev);
523 struct wilc_spi *spi_priv = wilc->bus_data;
525 int cmd_len, resp_len, i;
526 u16 crc_calc, crc_recv;
527 struct wilc_spi_cmd *c;
528 struct wilc_spi_rsp_data *r;
529 struct wilc_spi_read_rsp_data *r_data;
531 memset(wb, 0x0, sizeof(wb));
532 memset(rb, 0x0, sizeof(rb));
533 c = (struct wilc_spi_cmd *)wb;
535 if (cmd == CMD_SINGLE_READ) {
536 c->u.simple_cmd.addr[0] = adr >> 16;
537 c->u.simple_cmd.addr[1] = adr >> 8;
538 c->u.simple_cmd.addr[2] = adr;
539 } else if (cmd == CMD_INTERNAL_READ) {
540 c->u.simple_cmd.addr[0] = adr >> 8;
542 c->u.simple_cmd.addr[0] |= BIT(7);
543 c->u.simple_cmd.addr[1] = adr;
544 c->u.simple_cmd.addr[2] = 0x0;
546 dev_err(&spi->dev, "cmd [%x] not supported\n", cmd);
550 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
551 resp_len = sizeof(*r) + sizeof(*r_data) + WILC_SPI_RSP_HDR_EXTRA_DATA;
553 if (spi_priv->crc7_enabled) {
554 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
559 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
561 "spi buffer size too small (%d) (%d) (%zu)\n",
562 cmd_len, resp_len, ARRAY_SIZE(wb));
566 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
567 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
571 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
572 if (r->rsp_cmd_type != cmd && !clockless) {
573 if (!spi_priv->probing_crc)
575 "Failed cmd, cmd (%02x), resp (%02x)\n",
576 cmd, r->rsp_cmd_type);
580 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
581 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
586 for (i = 0; i < WILC_SPI_RSP_HDR_EXTRA_DATA; ++i)
587 if (WILC_GET_RESP_HDR_START(r->data[i]) == 0xf)
590 if (i >= WILC_SPI_RSP_HDR_EXTRA_DATA) {
591 dev_err(&spi->dev, "Error, data start missing\n");
595 r_data = (struct wilc_spi_read_rsp_data *)&r->data[i];
598 memcpy(b, r_data->data, 4);
600 if (!clockless && spi_priv->crc16_enabled) {
601 crc_recv = (r_data->crc[0] << 8) | r_data->crc[1];
602 crc_calc = crc_itu_t(0xffff, r_data->data, 4);
603 if (crc_recv != crc_calc) {
604 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
605 "(calculated 0x%04x)\n", __func__,
614 static int wilc_spi_write_cmd(struct wilc *wilc, u8 cmd, u32 adr, u32 data,
617 struct spi_device *spi = to_spi_device(wilc->dev);
618 struct wilc_spi *spi_priv = wilc->bus_data;
620 int cmd_len, resp_len;
621 struct wilc_spi_cmd *c;
622 struct wilc_spi_rsp_data *r;
624 memset(wb, 0x0, sizeof(wb));
625 memset(rb, 0x0, sizeof(rb));
626 c = (struct wilc_spi_cmd *)wb;
628 if (cmd == CMD_INTERNAL_WRITE) {
629 c->u.internal_w_cmd.addr[0] = adr >> 8;
631 c->u.internal_w_cmd.addr[0] |= BIT(7);
633 c->u.internal_w_cmd.addr[1] = adr;
634 c->u.internal_w_cmd.data = cpu_to_be32(data);
635 cmd_len = offsetof(struct wilc_spi_cmd, u.internal_w_cmd.crc);
636 if (spi_priv->crc7_enabled)
637 c->u.internal_w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
638 } else if (cmd == CMD_SINGLE_WRITE) {
639 c->u.w_cmd.addr[0] = adr >> 16;
640 c->u.w_cmd.addr[1] = adr >> 8;
641 c->u.w_cmd.addr[2] = adr;
642 c->u.w_cmd.data = cpu_to_be32(data);
643 cmd_len = offsetof(struct wilc_spi_cmd, u.w_cmd.crc);
644 if (spi_priv->crc7_enabled)
645 c->u.w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
647 dev_err(&spi->dev, "write cmd [%x] not supported\n", cmd);
651 if (spi_priv->crc7_enabled)
654 resp_len = sizeof(*r);
656 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
658 "spi buffer size too small (%d) (%d) (%zu)\n",
659 cmd_len, resp_len, ARRAY_SIZE(wb));
663 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
664 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
668 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
670 * Clockless registers operations might return unexptected responses,
671 * even if successful.
673 if (r->rsp_cmd_type != cmd && !clockless) {
675 "Failed cmd response, cmd (%02x), resp (%02x)\n",
676 cmd, r->rsp_cmd_type);
680 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
681 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
689 static int wilc_spi_dma_rw(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz)
691 struct spi_device *spi = to_spi_device(wilc->dev);
692 struct wilc_spi *spi_priv = wilc->bus_data;
693 u16 crc_recv, crc_calc;
695 int cmd_len, resp_len;
698 struct wilc_spi_cmd *c;
699 struct wilc_spi_rsp_data *r;
701 memset(wb, 0x0, sizeof(wb));
702 memset(rb, 0x0, sizeof(rb));
703 c = (struct wilc_spi_cmd *)wb;
705 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_READ) {
706 c->u.dma_cmd.addr[0] = adr >> 16;
707 c->u.dma_cmd.addr[1] = adr >> 8;
708 c->u.dma_cmd.addr[2] = adr;
709 c->u.dma_cmd.size[0] = sz >> 8;
710 c->u.dma_cmd.size[1] = sz;
711 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd.crc);
712 if (spi_priv->crc7_enabled)
713 c->u.dma_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
714 } else if (cmd == CMD_DMA_EXT_WRITE || cmd == CMD_DMA_EXT_READ) {
715 c->u.dma_cmd_ext.addr[0] = adr >> 16;
716 c->u.dma_cmd_ext.addr[1] = adr >> 8;
717 c->u.dma_cmd_ext.addr[2] = adr;
718 c->u.dma_cmd_ext.size[0] = sz >> 16;
719 c->u.dma_cmd_ext.size[1] = sz >> 8;
720 c->u.dma_cmd_ext.size[2] = sz;
721 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd_ext.crc);
722 if (spi_priv->crc7_enabled)
723 c->u.dma_cmd_ext.crc[0] = wilc_get_crc7(wb, cmd_len);
725 dev_err(&spi->dev, "dma read write cmd [%x] not supported\n",
729 if (spi_priv->crc7_enabled)
732 resp_len = sizeof(*r);
734 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
735 dev_err(&spi->dev, "spi buffer size too small (%d)(%d) (%zu)\n",
736 cmd_len, resp_len, ARRAY_SIZE(wb));
740 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
741 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
745 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
746 if (r->rsp_cmd_type != cmd) {
748 "Failed cmd response, cmd (%02x), resp (%02x)\n",
749 cmd, r->rsp_cmd_type);
753 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
754 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
759 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_EXT_WRITE)
766 nbytes = min_t(u32, sz, DATA_PKT_SZ);
769 * Data Response header
773 if (wilc_spi_rx(wilc, &rsp, 1)) {
775 "Failed resp read, bus err\n");
778 if (WILC_GET_RESP_HDR_START(rsp) == 0xf)
785 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
787 "Failed block read, bus err\n");
794 if (spi_priv->crc16_enabled) {
795 if (wilc_spi_rx(wilc, crc, 2)) {
797 "Failed block CRC read, bus err\n");
800 crc_recv = (crc[0] << 8) | crc[1];
801 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
802 if (crc_recv != crc_calc) {
803 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
804 "(calculated 0x%04x)\n", __func__,
816 static int wilc_spi_special_cmd(struct wilc *wilc, u8 cmd)
818 struct spi_device *spi = to_spi_device(wilc->dev);
819 struct wilc_spi *spi_priv = wilc->bus_data;
821 int cmd_len, resp_len = 0;
822 struct wilc_spi_cmd *c;
823 struct wilc_spi_special_cmd_rsp *r;
825 if (cmd != CMD_TERMINATE && cmd != CMD_REPEAT && cmd != CMD_RESET)
828 memset(wb, 0x0, sizeof(wb));
829 memset(rb, 0x0, sizeof(rb));
830 c = (struct wilc_spi_cmd *)wb;
833 if (cmd == CMD_RESET)
834 memset(c->u.simple_cmd.addr, 0xFF, 3);
836 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
837 resp_len = sizeof(*r);
839 if (spi_priv->crc7_enabled) {
840 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
843 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
844 dev_err(&spi->dev, "spi buffer size too small (%d) (%d) (%zu)\n",
845 cmd_len, resp_len, ARRAY_SIZE(wb));
849 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
850 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
854 r = (struct wilc_spi_special_cmd_rsp *)&rb[cmd_len];
855 if (r->rsp_cmd_type != cmd) {
856 if (!spi_priv->probing_crc)
858 "Failed cmd response, cmd (%02x), resp (%02x)\n",
859 cmd, r->rsp_cmd_type);
863 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
864 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
871 static void wilc_spi_reset_cmd_sequence(struct wilc *wl, u8 attempt, u32 addr)
873 struct spi_device *spi = to_spi_device(wl->dev);
874 struct wilc_spi *spi_priv = wl->bus_data;
876 if (!spi_priv->probing_crc)
877 dev_err(&spi->dev, "Reset and retry %d %x\n", attempt, addr);
879 usleep_range(1000, 1100);
881 usleep_range(1000, 1100);
884 static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
886 struct spi_device *spi = to_spi_device(wilc->dev);
888 u8 cmd = CMD_SINGLE_READ;
892 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
893 /* Clockless register */
894 cmd = CMD_INTERNAL_READ;
898 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
899 result = wilc_spi_single_read(wilc, cmd, addr, data, clockless);
905 /* retry is not applicable for clockless registers */
909 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
910 wilc_spi_reset_cmd_sequence(wilc, i, addr);
916 static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
918 struct spi_device *spi = to_spi_device(wilc->dev);
925 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
926 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_READ, addr,
931 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
933 wilc_spi_reset_cmd_sequence(wilc, i, addr);
939 static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
941 struct spi_device *spi = to_spi_device(wilc->dev);
945 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
946 result = wilc_spi_write_cmd(wilc, CMD_INTERNAL_WRITE, adr,
950 dev_err(&spi->dev, "Failed internal write cmd...\n");
952 wilc_spi_reset_cmd_sequence(wilc, i, adr);
958 static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
960 struct spi_device *spi = to_spi_device(wilc->dev);
961 struct wilc_spi *spi_priv = wilc->bus_data;
965 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
966 result = wilc_spi_single_read(wilc, CMD_INTERNAL_READ, adr,
972 if (!spi_priv->probing_crc)
973 dev_err(&spi->dev, "Failed internal read cmd...\n");
975 wilc_spi_reset_cmd_sequence(wilc, i, adr);
981 /********************************************
985 ********************************************/
987 static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
989 struct spi_device *spi = to_spi_device(wilc->dev);
991 u8 cmd = CMD_SINGLE_WRITE;
995 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
996 /* Clockless register */
997 cmd = CMD_INTERNAL_WRITE;
1001 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
1002 result = wilc_spi_write_cmd(wilc, cmd, addr, data, clockless);
1006 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
1011 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1016 static int spi_data_rsp(struct wilc *wilc, u8 cmd)
1018 struct spi_device *spi = to_spi_device(wilc->dev);
1023 * The response to data packets is two bytes long. For
1024 * efficiency's sake, wilc_spi_write() wisely ignores the
1025 * responses for all packets but the final one. The downside
1026 * of that optimization is that when the final data packet is
1027 * short, we may receive (part of) the response to the
1028 * second-to-last packet before the one for the final packet.
1029 * To handle this, we always read 4 bytes and then search for
1030 * the last byte that contains the "Response Start" code (0xc
1031 * in the top 4 bits). We then know that this byte is the
1032 * first response byte of the final data packet.
1034 result = wilc_spi_rx(wilc, rsp, sizeof(rsp));
1036 dev_err(&spi->dev, "Failed bus error...\n");
1040 for (i = sizeof(rsp) - 2; i >= 0; --i)
1041 if (FIELD_GET(RSP_START_FIELD, rsp[i]) == RSP_START_TAG)
1046 "Data packet response missing (%02x %02x %02x %02x)\n",
1047 rsp[0], rsp[1], rsp[2], rsp[3]);
1051 /* rsp[i] is the last response start byte */
1053 if (FIELD_GET(RSP_TYPE_FIELD, rsp[i]) != RSP_TYPE_LAST_PACKET
1054 || rsp[i + 1] != RSP_STATE_NO_ERROR) {
1055 dev_err(&spi->dev, "Data response error (%02x %02x)\n",
1056 rsp[i], rsp[i + 1]);
1062 static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
1064 struct spi_device *spi = to_spi_device(wilc->dev);
1069 * has to be greated than 4
1074 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
1075 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_WRITE, addr,
1079 "Failed cmd, write block (%08x)...\n", addr);
1080 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1087 result = spi_data_write(wilc, buf, size);
1089 dev_err(&spi->dev, "Failed block data write...\n");
1090 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1097 result = spi_data_rsp(wilc, CMD_DMA_EXT_WRITE);
1099 dev_err(&spi->dev, "Failed block data rsp...\n");
1100 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1108 /********************************************
1112 ********************************************/
1114 static int wilc_spi_reset(struct wilc *wilc)
1116 struct spi_device *spi = to_spi_device(wilc->dev);
1117 struct wilc_spi *spi_priv = wilc->bus_data;
1120 result = wilc_spi_special_cmd(wilc, CMD_RESET);
1121 if (result && !spi_priv->probing_crc)
1122 dev_err(&spi->dev, "Failed cmd reset\n");
1127 static bool wilc_spi_is_init(struct wilc *wilc)
1129 struct wilc_spi *spi_priv = wilc->bus_data;
1131 return spi_priv->isinit;
1134 static int wilc_spi_deinit(struct wilc *wilc)
1136 struct wilc_spi *spi_priv = wilc->bus_data;
1138 spi_priv->isinit = false;
1139 wilc_wlan_power(wilc, false);
1143 static int wilc_spi_init(struct wilc *wilc, bool resume)
1145 struct wilc_spi *spi_priv = wilc->bus_data;
1148 if (spi_priv->isinit) {
1149 /* Confirm we can read chipid register without error: */
1150 if (wilc_validate_chipid(wilc) == 0)
1154 wilc_wlan_power(wilc, true);
1156 ret = wilc_spi_configure_bus_protocol(wilc);
1158 wilc_wlan_power(wilc, false);
1162 spi_priv->isinit = true;
1167 static int wilc_spi_configure_bus_protocol(struct wilc *wilc)
1169 struct spi_device *spi = to_spi_device(wilc->dev);
1170 struct wilc_spi *spi_priv = wilc->bus_data;
1175 * Infer the CRC settings that are currently in effect. This
1176 * is necessary because we can't be sure that the chip has
1177 * been RESET (e.g, after module unload and reload).
1179 spi_priv->probing_crc = true;
1180 spi_priv->crc7_enabled = enable_crc7;
1181 spi_priv->crc16_enabled = false; /* don't check CRC16 during probing */
1182 for (i = 0; i < 2; ++i) {
1183 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1186 spi_priv->crc7_enabled = !enable_crc7;
1189 dev_err(&spi->dev, "Failed with CRC7 on and off.\n");
1193 /* set up the desired CRC configuration: */
1194 reg &= ~(PROTOCOL_REG_CRC7_MASK | PROTOCOL_REG_CRC16_MASK);
1196 reg |= PROTOCOL_REG_CRC7_MASK;
1198 reg |= PROTOCOL_REG_CRC16_MASK;
1200 /* set up the data packet size: */
1201 BUILD_BUG_ON(DATA_PKT_LOG_SZ < DATA_PKT_LOG_SZ_MIN
1202 || DATA_PKT_LOG_SZ > DATA_PKT_LOG_SZ_MAX);
1203 reg &= ~PROTOCOL_REG_PKT_SZ_MASK;
1204 reg |= FIELD_PREP(PROTOCOL_REG_PKT_SZ_MASK,
1205 DATA_PKT_LOG_SZ - DATA_PKT_LOG_SZ_MIN);
1207 /* establish the new setup: */
1208 ret = spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg);
1211 "[wilc spi %d]: Failed internal write reg\n",
1215 /* update our state to match new protocol settings: */
1216 spi_priv->crc7_enabled = enable_crc7;
1217 spi_priv->crc16_enabled = enable_crc16;
1219 /* re-read to make sure new settings are in effect: */
1220 spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1222 spi_priv->probing_crc = false;
1227 static int wilc_validate_chipid(struct wilc *wilc)
1229 struct spi_device *spi = to_spi_device(wilc->dev);
1234 * make sure can read chip id without protocol error
1236 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1238 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1241 if (!is_wilc1000(chipid) && !is_wilc3000(chipid)) {
1242 dev_err(&spi->dev, "Unknown chip id 0x%x\n", chipid);
1248 static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
1252 ret = spi_internal_read(wilc,
1253 WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE, size);
1254 *size = FIELD_GET(IRQ_DMA_WD_CNT_MASK, *size);
1259 static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
1261 return spi_internal_read(wilc, WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE,
1265 static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
1268 int retry = SPI_ENABLE_VMM_RETRY_LIMIT;
1272 ret = spi_internal_write(wilc,
1273 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1278 ret = spi_internal_read(wilc,
1279 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1281 if (ret || ((check & EN_VMM) == (val & EN_VMM)))
1289 static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
1291 struct spi_device *spi = to_spi_device(wilc->dev);
1295 if (nint > MAX_NUM_INT) {
1296 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
1301 * interrupt pin mux select
1303 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
1305 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1310 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
1312 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1320 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
1322 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1327 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1328 reg |= (BIT((27 + i)));
1330 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
1332 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1337 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1339 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1344 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1347 ret = wilc_spi_write_reg(wilc, WILC_INTR2_ENABLE, reg);
1349 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1358 /* Global spi HIF function table */
1359 static const struct wilc_hif_func wilc_hif_spi = {
1360 .hif_init = wilc_spi_init,
1361 .hif_deinit = wilc_spi_deinit,
1362 .hif_read_reg = wilc_spi_read_reg,
1363 .hif_write_reg = wilc_spi_write_reg,
1364 .hif_block_rx = wilc_spi_read,
1365 .hif_block_tx = wilc_spi_write,
1366 .hif_read_int = wilc_spi_read_int,
1367 .hif_clear_int_ext = wilc_spi_clear_int_ext,
1368 .hif_read_size = wilc_spi_read_size,
1369 .hif_block_tx_ext = wilc_spi_write,
1370 .hif_block_rx_ext = wilc_spi_read,
1371 .hif_sync_ext = wilc_spi_sync_ext,
1372 .hif_reset = wilc_spi_reset,
1373 .hif_is_init = wilc_spi_is_init,