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>
18 static const struct wilc_hif_func wilc_hif_spi;
20 /********************************************
22 * Spi protocol Function
24 ********************************************/
26 #define CMD_DMA_WRITE 0xc1
27 #define CMD_DMA_READ 0xc2
28 #define CMD_INTERNAL_WRITE 0xc3
29 #define CMD_INTERNAL_READ 0xc4
30 #define CMD_TERMINATE 0xc5
31 #define CMD_REPEAT 0xc6
32 #define CMD_DMA_EXT_WRITE 0xc7
33 #define CMD_DMA_EXT_READ 0xc8
34 #define CMD_SINGLE_WRITE 0xc9
35 #define CMD_SINGLE_READ 0xca
36 #define CMD_RESET 0xcf
38 #define SPI_ENABLE_VMM_RETRY_LIMIT 2
39 #define DATA_PKT_SZ_256 256
40 #define DATA_PKT_SZ_512 512
41 #define DATA_PKT_SZ_1K 1024
42 #define DATA_PKT_SZ_4K (4 * 1024)
43 #define DATA_PKT_SZ_8K (8 * 1024)
44 #define DATA_PKT_SZ DATA_PKT_SZ_8K
48 #define WILC_SPI_COMMAND_STAT_SUCCESS 0
49 #define WILC_GET_RESP_HDR_START(h) (((h) >> 4) & 0xf)
57 } __packed simple_cmd;
67 } __packed dma_cmd_ext;
72 } __packed internal_w_cmd;
81 struct wilc_spi_read_rsp_data {
89 struct wilc_spi_rsp_data {
94 static int wilc_bus_probe(struct spi_device *spi)
98 struct wilc_spi *spi_priv;
100 spi_priv = kzalloc(sizeof(*spi_priv), GFP_KERNEL);
104 ret = wilc_cfg80211_init(&wilc, &spi->dev, WILC_HIF_SPI, &wilc_hif_spi);
110 spi_set_drvdata(spi, wilc);
111 wilc->dev = &spi->dev;
112 wilc->bus_data = spi_priv;
113 wilc->dev_irq_num = spi->irq;
115 wilc->rtc_clk = devm_clk_get(&spi->dev, "rtc_clk");
116 if (PTR_ERR_OR_ZERO(wilc->rtc_clk) == -EPROBE_DEFER) {
118 return -EPROBE_DEFER;
119 } else if (!IS_ERR(wilc->rtc_clk))
120 clk_prepare_enable(wilc->rtc_clk);
125 static int wilc_bus_remove(struct spi_device *spi)
127 struct wilc *wilc = spi_get_drvdata(spi);
129 if (!IS_ERR(wilc->rtc_clk))
130 clk_disable_unprepare(wilc->rtc_clk);
132 wilc_netdev_cleanup(wilc);
136 static const struct of_device_id wilc_of_match[] = {
137 { .compatible = "microchip,wilc1000", },
140 MODULE_DEVICE_TABLE(of, wilc_of_match);
142 static struct spi_driver wilc_spi_driver = {
145 .of_match_table = wilc_of_match,
147 .probe = wilc_bus_probe,
148 .remove = wilc_bus_remove,
150 module_spi_driver(wilc_spi_driver);
151 MODULE_LICENSE("GPL");
153 static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
155 struct spi_device *spi = to_spi_device(wilc->dev);
157 struct spi_message msg;
160 struct spi_transfer tr = {
165 .unit = SPI_DELAY_UNIT_USECS
168 char *r_buffer = kzalloc(len, GFP_KERNEL);
173 tr.rx_buf = r_buffer;
174 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
176 memset(&msg, 0, sizeof(msg));
177 spi_message_init(&msg);
179 msg.is_dma_mapped = USE_SPI_DMA;
180 spi_message_add_tail(&tr, &msg);
182 ret = spi_sync(spi, &msg);
184 dev_err(&spi->dev, "SPI transaction failed\n");
189 "can't write data with the following length: %d\n",
197 static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
199 struct spi_device *spi = to_spi_device(wilc->dev);
203 struct spi_message msg;
204 struct spi_transfer tr = {
209 .unit = SPI_DELAY_UNIT_USECS
213 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
218 tr.tx_buf = t_buffer;
220 memset(&msg, 0, sizeof(msg));
221 spi_message_init(&msg);
223 msg.is_dma_mapped = USE_SPI_DMA;
224 spi_message_add_tail(&tr, &msg);
226 ret = spi_sync(spi, &msg);
228 dev_err(&spi->dev, "SPI transaction failed\n");
232 "can't read data with the following length: %u\n",
240 static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
242 struct spi_device *spi = to_spi_device(wilc->dev);
246 struct spi_message msg;
247 struct spi_transfer tr = {
254 .unit = SPI_DELAY_UNIT_USECS
259 memset(&msg, 0, sizeof(msg));
260 spi_message_init(&msg);
262 msg.is_dma_mapped = USE_SPI_DMA;
264 spi_message_add_tail(&tr, &msg);
265 ret = spi_sync(spi, &msg);
267 dev_err(&spi->dev, "SPI transaction failed\n");
270 "can't read data with the following length: %u\n",
278 static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
280 struct spi_device *spi = to_spi_device(wilc->dev);
281 struct wilc_spi *spi_priv = wilc->bus_data;
284 u8 cmd, order, crc[2] = {0};
291 if (sz <= DATA_PKT_SZ) {
295 nbytes = DATA_PKT_SZ;
308 if (wilc_spi_tx(wilc, &cmd, 1)) {
310 "Failed data block cmd write, bus error...\n");
318 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
320 "Failed data block write, bus error...\n");
328 if (!spi_priv->crc_off) {
329 if (wilc_spi_tx(wilc, crc, 2)) {
330 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
337 * No need to wait for response
346 /********************************************
348 * Spi Internal Read/Write Function
350 ********************************************/
351 static u8 wilc_get_crc7(u8 *buffer, u32 len)
353 return crc7_be(0xfe, buffer, len);
356 static int wilc_spi_single_read(struct wilc *wilc, u8 cmd, u32 adr, void *b,
359 struct spi_device *spi = to_spi_device(wilc->dev);
360 struct wilc_spi *spi_priv = wilc->bus_data;
362 int cmd_len, resp_len;
364 struct wilc_spi_cmd *c;
365 struct wilc_spi_read_rsp_data *r;
367 memset(wb, 0x0, sizeof(wb));
368 memset(rb, 0x0, sizeof(rb));
369 c = (struct wilc_spi_cmd *)wb;
371 if (cmd == CMD_SINGLE_READ) {
372 c->u.simple_cmd.addr[0] = adr >> 16;
373 c->u.simple_cmd.addr[1] = adr >> 8;
374 c->u.simple_cmd.addr[2] = adr;
375 } else if (cmd == CMD_INTERNAL_READ) {
376 c->u.simple_cmd.addr[0] = adr >> 8;
378 c->u.simple_cmd.addr[0] |= BIT(7);
379 c->u.simple_cmd.addr[1] = adr;
380 c->u.simple_cmd.addr[2] = 0x0;
382 dev_err(&spi->dev, "cmd [%x] not supported\n", cmd);
386 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
387 resp_len = sizeof(*r);
388 if (!spi_priv->crc_off) {
389 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
394 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
396 "spi buffer size too small (%d) (%d) (%zu)\n",
397 cmd_len, resp_len, ARRAY_SIZE(wb));
401 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
402 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
406 r = (struct wilc_spi_read_rsp_data *)&rb[cmd_len];
407 if (r->rsp_cmd_type != cmd) {
409 "Failed cmd response, cmd (%02x), resp (%02x)\n",
410 cmd, r->rsp_cmd_type);
414 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
415 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
420 if (WILC_GET_RESP_HDR_START(r->resp_header) != 0xf) {
421 dev_err(&spi->dev, "Error, data read response (%02x)\n",
427 memcpy(b, r->resp_data, 4);
429 if (!spi_priv->crc_off)
430 memcpy(crc, r->crc, 2);
435 static int wilc_spi_write_cmd(struct wilc *wilc, u8 cmd, u32 adr, u32 data,
438 struct spi_device *spi = to_spi_device(wilc->dev);
439 struct wilc_spi *spi_priv = wilc->bus_data;
441 int cmd_len, resp_len;
442 struct wilc_spi_cmd *c;
443 struct wilc_spi_rsp_data *r;
445 memset(wb, 0x0, sizeof(wb));
446 memset(rb, 0x0, sizeof(rb));
447 c = (struct wilc_spi_cmd *)wb;
449 if (cmd == CMD_INTERNAL_WRITE) {
450 c->u.internal_w_cmd.addr[0] = adr >> 8;
452 c->u.internal_w_cmd.addr[0] |= BIT(7);
454 c->u.internal_w_cmd.addr[1] = adr;
455 c->u.internal_w_cmd.data = cpu_to_be32(data);
456 cmd_len = offsetof(struct wilc_spi_cmd, u.internal_w_cmd.crc);
457 if (!spi_priv->crc_off)
458 c->u.internal_w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
459 } else if (cmd == CMD_SINGLE_WRITE) {
460 c->u.w_cmd.addr[0] = adr >> 16;
461 c->u.w_cmd.addr[1] = adr >> 8;
462 c->u.w_cmd.addr[2] = adr;
463 c->u.w_cmd.data = cpu_to_be32(data);
464 cmd_len = offsetof(struct wilc_spi_cmd, u.w_cmd.crc);
465 if (!spi_priv->crc_off)
466 c->u.w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
468 dev_err(&spi->dev, "write cmd [%x] not supported\n", cmd);
472 if (!spi_priv->crc_off)
475 resp_len = sizeof(*r);
477 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
479 "spi buffer size too small (%d) (%d) (%zu)\n",
480 cmd_len, resp_len, ARRAY_SIZE(wb));
484 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
485 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
489 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
490 if (r->rsp_cmd_type != cmd) {
492 "Failed cmd response, cmd (%02x), resp (%02x)\n",
493 cmd, r->rsp_cmd_type);
497 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
498 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
506 static int wilc_spi_dma_rw(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz)
508 struct spi_device *spi = to_spi_device(wilc->dev);
509 struct wilc_spi *spi_priv = wilc->bus_data;
511 int cmd_len, resp_len;
514 struct wilc_spi_cmd *c;
515 struct wilc_spi_rsp_data *r;
517 memset(wb, 0x0, sizeof(wb));
518 memset(rb, 0x0, sizeof(rb));
519 c = (struct wilc_spi_cmd *)wb;
521 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_READ) {
522 c->u.dma_cmd.addr[0] = adr >> 16;
523 c->u.dma_cmd.addr[1] = adr >> 8;
524 c->u.dma_cmd.addr[2] = adr;
525 c->u.dma_cmd.size[0] = sz >> 8;
526 c->u.dma_cmd.size[1] = sz;
527 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd.crc);
528 if (!spi_priv->crc_off)
529 c->u.dma_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
530 } else if (cmd == CMD_DMA_EXT_WRITE || cmd == CMD_DMA_EXT_READ) {
531 c->u.dma_cmd_ext.addr[0] = adr >> 16;
532 c->u.dma_cmd_ext.addr[1] = adr >> 8;
533 c->u.dma_cmd_ext.addr[2] = adr;
534 c->u.dma_cmd_ext.size[0] = sz >> 16;
535 c->u.dma_cmd_ext.size[1] = sz >> 8;
536 c->u.dma_cmd_ext.size[2] = sz;
537 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd_ext.crc);
538 if (!spi_priv->crc_off)
539 c->u.dma_cmd_ext.crc[0] = wilc_get_crc7(wb, cmd_len);
541 dev_err(&spi->dev, "dma read write cmd [%x] not supported\n",
545 if (!spi_priv->crc_off)
548 resp_len = sizeof(*r);
550 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
551 dev_err(&spi->dev, "spi buffer size too small (%d)(%d) (%zu)\n",
552 cmd_len, resp_len, ARRAY_SIZE(wb));
556 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
557 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
561 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
562 if (r->rsp_cmd_type != cmd) {
564 "Failed cmd response, cmd (%02x), resp (%02x)\n",
565 cmd, r->rsp_cmd_type);
569 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
570 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
575 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_EXT_WRITE)
582 if (sz <= DATA_PKT_SZ)
585 nbytes = DATA_PKT_SZ;
588 * Data Response header
592 if (wilc_spi_rx(wilc, &rsp, 1)) {
594 "Failed resp read, bus err\n");
597 if (WILC_GET_RESP_HDR_START(rsp) == 0xf)
604 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
606 "Failed block read, bus err\n");
613 if (!spi_priv->crc_off && wilc_spi_rx(wilc, crc, 2)) {
615 "Failed block crc read, bus err\n");
625 static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
627 struct spi_device *spi = to_spi_device(wilc->dev);
629 u8 cmd = CMD_SINGLE_READ;
632 if (addr < WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
633 /* Clockless register */
634 cmd = CMD_INTERNAL_READ;
638 result = wilc_spi_single_read(wilc, cmd, addr, data, clockless);
640 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
649 static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
651 struct spi_device *spi = to_spi_device(wilc->dev);
657 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_READ, addr, buf, size);
659 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
666 static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
668 struct spi_device *spi = to_spi_device(wilc->dev);
671 result = wilc_spi_write_cmd(wilc, CMD_INTERNAL_WRITE, adr, dat, 0);
673 dev_err(&spi->dev, "Failed internal write cmd...\n");
680 static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
682 struct spi_device *spi = to_spi_device(wilc->dev);
685 result = wilc_spi_single_read(wilc, CMD_INTERNAL_READ, adr, data, 0);
687 dev_err(&spi->dev, "Failed internal read cmd...\n");
696 /********************************************
700 ********************************************/
702 static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
704 struct spi_device *spi = to_spi_device(wilc->dev);
706 u8 cmd = CMD_SINGLE_WRITE;
709 if (addr < WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
710 /* Clockless register */
711 cmd = CMD_INTERNAL_WRITE;
715 result = wilc_spi_write_cmd(wilc, cmd, addr, data, clockless);
717 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
724 static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
726 struct spi_device *spi = to_spi_device(wilc->dev);
730 * has to be greated than 4
735 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_WRITE, addr, NULL, size);
738 "Failed cmd, write block (%08x)...\n", addr);
745 result = spi_data_write(wilc, buf, size);
747 dev_err(&spi->dev, "Failed block data write...\n");
754 /********************************************
758 ********************************************/
760 static int wilc_spi_deinit(struct wilc *wilc)
768 static int wilc_spi_init(struct wilc *wilc, bool resume)
770 struct spi_device *spi = to_spi_device(wilc->dev);
771 struct wilc_spi *spi_priv = wilc->bus_data;
778 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
780 dev_err(&spi->dev, "Fail cmd read chip id...\n");
790 * TODO: We can remove the CRC trials if there is a definite
793 /* the SPI to it's initial value. */
794 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
797 * Read failed. Try with CRC off. This might happen when module
798 * is removed but chip isn't reset
800 spi_priv->crc_off = 1;
802 "Failed read with CRC on, retrying with CRC off\n");
803 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
806 * Read failed with both CRC on and off,
809 dev_err(&spi->dev, "Failed internal read protocol\n");
813 if (spi_priv->crc_off == 0) {
814 reg &= ~0xc; /* disable crc checking */
817 ret = spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg);
820 "[wilc spi %d]: Failed internal write reg\n",
824 spi_priv->crc_off = 1;
828 * make sure can read back chip id correctly
830 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
832 dev_err(&spi->dev, "Fail cmd read chip id...\n");
841 static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
845 ret = spi_internal_read(wilc,
846 WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE, size);
847 *size = FIELD_GET(IRQ_DMA_WD_CNT_MASK, *size);
852 static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
854 return spi_internal_read(wilc, WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE,
858 static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
861 int retry = SPI_ENABLE_VMM_RETRY_LIMIT;
865 ret = spi_internal_write(wilc,
866 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
871 ret = spi_internal_read(wilc,
872 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
874 if (ret || ((check & EN_VMM) == (val & EN_VMM)))
882 static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
884 struct spi_device *spi = to_spi_device(wilc->dev);
888 if (nint > MAX_NUM_INT) {
889 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
894 * interrupt pin mux select
896 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
898 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
903 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
905 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
913 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
915 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
920 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
921 reg |= (BIT((27 + i)));
923 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
925 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
930 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
932 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
937 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
940 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
942 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
951 /* Global spi HIF function table */
952 static const struct wilc_hif_func wilc_hif_spi = {
953 .hif_init = wilc_spi_init,
954 .hif_deinit = wilc_spi_deinit,
955 .hif_read_reg = wilc_spi_read_reg,
956 .hif_write_reg = wilc_spi_write_reg,
957 .hif_block_rx = wilc_spi_read,
958 .hif_block_tx = wilc_spi_write,
959 .hif_read_int = wilc_spi_read_int,
960 .hif_clear_int_ext = wilc_spi_clear_int_ext,
961 .hif_read_size = wilc_spi_read_size,
962 .hif_block_tx_ext = wilc_spi_write,
963 .hif_block_rx_ext = wilc_spi_read,
964 .hif_sync_ext = wilc_spi_sync_ext,