Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4 * All rights reserved.
5 */
6
7#include <linux/clk.h>
8#include <linux/spi/spi.h>
9#include <linux/crc7.h>
10#include <linux/crc-itu-t.h>
11#include <linux/gpio/consumer.h>
12
13#include "netdev.h"
14#include "cfg80211.h"
15
16#define SPI_MODALIAS "wilc1000_spi"
17
18static bool enable_crc7; /* protect SPI commands with CRC7 */
19module_param(enable_crc7, bool, 0644);
20MODULE_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.");
25
26static bool enable_crc16; /* protect SPI data with CRC16 */
27module_param(enable_crc16, bool, 0644);
28MODULE_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.");
33
34/*
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
40 * operates at 1MHz.
41 */
42#define WILC_SPI_RSP_HDR_EXTRA_DATA 8
43
44struct wilc_spi {
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 */
49 struct wilc_gpios {
50 struct gpio_desc *enable; /* ENABLE GPIO or NULL */
51 struct gpio_desc *reset; /* RESET GPIO or NULL */
52 } gpios;
53};
54
55static const struct wilc_hif_func wilc_hif_spi;
56
57static int wilc_spi_reset(struct wilc *wilc);
58
59/********************************************
60 *
61 * Spi protocol Function
62 *
63 ********************************************/
64
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
76
77#define SPI_RETRY_MAX_LIMIT 10
78#define SPI_ENABLE_VMM_RETRY_LIMIT 2
79
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)
83
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
90
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)
94
95/*
96 * The SPI data packet size may be any integer power of two in the
97 * range from 256 to 8192 bytes.
98 */
99#define DATA_PKT_LOG_SZ_MIN 8 /* 256 B */
100#define DATA_PKT_LOG_SZ_MAX 13 /* 8 KiB */
101
102/*
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.
106 */
107#define DATA_PKT_LOG_SZ DATA_PKT_LOG_SZ_MAX
108#define DATA_PKT_SZ (1 << DATA_PKT_LOG_SZ)
109
110#define WILC_SPI_COMMAND_STAT_SUCCESS 0
111#define WILC_GET_RESP_HDR_START(h) (((h) >> 4) & 0xf)
112
113struct wilc_spi_cmd {
114 u8 cmd_type;
115 union {
116 struct {
117 u8 addr[3];
118 u8 crc[];
119 } __packed simple_cmd;
120 struct {
121 u8 addr[3];
122 u8 size[2];
123 u8 crc[];
124 } __packed dma_cmd;
125 struct {
126 u8 addr[3];
127 u8 size[3];
128 u8 crc[];
129 } __packed dma_cmd_ext;
130 struct {
131 u8 addr[2];
132 __be32 data;
133 u8 crc[];
134 } __packed internal_w_cmd;
135 struct {
136 u8 addr[3];
137 __be32 data;
138 u8 crc[];
139 } __packed w_cmd;
140 } u;
141} __packed;
142
143struct wilc_spi_read_rsp_data {
144 u8 header;
145 u8 data[4];
146 u8 crc[];
147} __packed;
148
149struct wilc_spi_rsp_data {
150 u8 rsp_cmd_type;
151 u8 status;
152 u8 data[];
153} __packed;
154
155struct wilc_spi_special_cmd_rsp {
156 u8 skip_byte;
157 u8 rsp_cmd_type;
158 u8 status;
159} __packed;
160
161static int wilc_parse_gpios(struct wilc *wilc)
162{
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;
166
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): */
171 if (gpios->enable) {
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);
178 }
179 } else {
180 gpios->reset = devm_gpiod_get_optional(&spi->dev,
181 "reset", GPIOD_OUT_HIGH);
182 }
183 return 0;
184}
185
186static void wilc_wlan_power(struct wilc *wilc, bool on)
187{
188 struct wilc_spi *spi_priv = wilc->bus_data;
189 struct wilc_gpios *gpios = &spi_priv->gpios;
190
191 if (on) {
192 /* assert ENABLE: */
193 gpiod_set_value(gpios->enable, 1);
194 mdelay(5);
195 /* assert RESET: */
196 gpiod_set_value(gpios->reset, 1);
197 } else {
198 /* deassert RESET: */
199 gpiod_set_value(gpios->reset, 0);
200 /* deassert ENABLE: */
201 gpiod_set_value(gpios->enable, 0);
202 }
203}
204
205static int wilc_bus_probe(struct spi_device *spi)
206{
207 int ret;
208 struct wilc *wilc;
209 struct wilc_spi *spi_priv;
210
211 spi_priv = kzalloc(sizeof(*spi_priv), GFP_KERNEL);
212 if (!spi_priv)
213 return -ENOMEM;
214
215 ret = wilc_cfg80211_init(&wilc, &spi->dev, WILC_HIF_SPI, &wilc_hif_spi);
216 if (ret)
217 goto free;
218
219 spi_set_drvdata(spi, wilc);
220 wilc->dev = &spi->dev;
221 wilc->bus_data = spi_priv;
222 wilc->dev_irq_num = spi->irq;
223
224 ret = wilc_parse_gpios(wilc);
225 if (ret < 0)
226 goto netdev_cleanup;
227
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);
231 goto netdev_cleanup;
232 }
233 clk_prepare_enable(wilc->rtc_clk);
234
235 return 0;
236
237netdev_cleanup:
238 wilc_netdev_cleanup(wilc);
239free:
240 kfree(spi_priv);
241 return ret;
242}
243
244static void wilc_bus_remove(struct spi_device *spi)
245{
246 struct wilc *wilc = spi_get_drvdata(spi);
247 struct wilc_spi *spi_priv = wilc->bus_data;
248
249 clk_disable_unprepare(wilc->rtc_clk);
250 wilc_netdev_cleanup(wilc);
251 kfree(spi_priv);
252}
253
254static const struct of_device_id wilc_of_match[] = {
255 { .compatible = "microchip,wilc1000", },
256 { /* sentinel */ }
257};
258MODULE_DEVICE_TABLE(of, wilc_of_match);
259
260static const struct spi_device_id wilc_spi_id[] = {
261 { "wilc1000", 0 },
262 { /* sentinel */ }
263};
264MODULE_DEVICE_TABLE(spi, wilc_spi_id);
265
266static struct spi_driver wilc_spi_driver = {
267 .driver = {
268 .name = SPI_MODALIAS,
269 .of_match_table = wilc_of_match,
270 },
271 .id_table = wilc_spi_id,
272 .probe = wilc_bus_probe,
273 .remove = wilc_bus_remove,
274};
275module_spi_driver(wilc_spi_driver);
276MODULE_DESCRIPTION("Atmel WILC1000 SPI wireless driver");
277MODULE_LICENSE("GPL");
278
279static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
280{
281 struct spi_device *spi = to_spi_device(wilc->dev);
282 int ret;
283 struct spi_message msg;
284
285 if (len > 0 && b) {
286 struct spi_transfer tr = {
287 .tx_buf = b,
288 .len = len,
289 .delay = {
290 .value = 0,
291 .unit = SPI_DELAY_UNIT_USECS
292 },
293 };
294 char *r_buffer = kzalloc(len, GFP_KERNEL);
295
296 if (!r_buffer)
297 return -ENOMEM;
298
299 tr.rx_buf = r_buffer;
300 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
301
302 memset(&msg, 0, sizeof(msg));
303 spi_message_init(&msg);
304 msg.spi = spi;
305 spi_message_add_tail(&tr, &msg);
306
307 ret = spi_sync(spi, &msg);
308 if (ret < 0)
309 dev_err(&spi->dev, "SPI transaction failed\n");
310
311 kfree(r_buffer);
312 } else {
313 dev_err(&spi->dev,
314 "can't write data with the following length: %d\n",
315 len);
316 ret = -EINVAL;
317 }
318
319 return ret;
320}
321
322static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
323{
324 struct spi_device *spi = to_spi_device(wilc->dev);
325 int ret;
326
327 if (rlen > 0) {
328 struct spi_message msg;
329 struct spi_transfer tr = {
330 .rx_buf = rb,
331 .len = rlen,
332 .delay = {
333 .value = 0,
334 .unit = SPI_DELAY_UNIT_USECS
335 },
336
337 };
338 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
339
340 if (!t_buffer)
341 return -ENOMEM;
342
343 tr.tx_buf = t_buffer;
344
345 memset(&msg, 0, sizeof(msg));
346 spi_message_init(&msg);
347 msg.spi = spi;
348 spi_message_add_tail(&tr, &msg);
349
350 ret = spi_sync(spi, &msg);
351 if (ret < 0)
352 dev_err(&spi->dev, "SPI transaction failed\n");
353 kfree(t_buffer);
354 } else {
355 dev_err(&spi->dev,
356 "can't read data with the following length: %u\n",
357 rlen);
358 ret = -EINVAL;
359 }
360
361 return ret;
362}
363
364static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
365{
366 struct spi_device *spi = to_spi_device(wilc->dev);
367 int ret;
368
369 if (rlen > 0) {
370 struct spi_message msg;
371 struct spi_transfer tr = {
372 .rx_buf = rb,
373 .tx_buf = wb,
374 .len = rlen,
375 .bits_per_word = 8,
376 .delay = {
377 .value = 0,
378 .unit = SPI_DELAY_UNIT_USECS
379 },
380
381 };
382
383 memset(&msg, 0, sizeof(msg));
384 spi_message_init(&msg);
385 msg.spi = spi;
386
387 spi_message_add_tail(&tr, &msg);
388 ret = spi_sync(spi, &msg);
389 if (ret < 0)
390 dev_err(&spi->dev, "SPI transaction failed\n");
391 } else {
392 dev_err(&spi->dev,
393 "can't read data with the following length: %u\n",
394 rlen);
395 ret = -EINVAL;
396 }
397
398 return ret;
399}
400
401static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
402{
403 struct spi_device *spi = to_spi_device(wilc->dev);
404 struct wilc_spi *spi_priv = wilc->bus_data;
405 int ix, nbytes;
406 int result = 0;
407 u8 cmd, order, crc[2];
408 u16 crc_calc;
409
410 /*
411 * Data
412 */
413 ix = 0;
414 do {
415 if (sz <= DATA_PKT_SZ) {
416 nbytes = sz;
417 order = 0x3;
418 } else {
419 nbytes = DATA_PKT_SZ;
420 if (ix == 0)
421 order = 0x1;
422 else
423 order = 0x02;
424 }
425
426 /*
427 * Write command
428 */
429 cmd = 0xf0;
430 cmd |= order;
431
432 if (wilc_spi_tx(wilc, &cmd, 1)) {
433 dev_err(&spi->dev,
434 "Failed data block cmd write, bus error...\n");
435 result = -EINVAL;
436 break;
437 }
438
439 /*
440 * Write data
441 */
442 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
443 dev_err(&spi->dev,
444 "Failed data block write, bus error...\n");
445 result = -EINVAL;
446 break;
447 }
448
449 /*
450 * Write CRC
451 */
452 if (spi_priv->crc16_enabled) {
453 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
454 crc[0] = crc_calc >> 8;
455 crc[1] = crc_calc;
456 if (wilc_spi_tx(wilc, crc, 2)) {
457 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
458 result = -EINVAL;
459 break;
460 }
461 }
462
463 /*
464 * No need to wait for response
465 */
466 ix += nbytes;
467 sz -= nbytes;
468 } while (sz);
469
470 return result;
471}
472
473/********************************************
474 *
475 * Spi Internal Read/Write Function
476 *
477 ********************************************/
478static u8 wilc_get_crc7(u8 *buffer, u32 len)
479{
480 return crc7_be(0xfe, buffer, len);
481}
482
483static int wilc_spi_single_read(struct wilc *wilc, u8 cmd, u32 adr, void *b,
484 u8 clockless)
485{
486 struct spi_device *spi = to_spi_device(wilc->dev);
487 struct wilc_spi *spi_priv = wilc->bus_data;
488 u8 wb[32], rb[32];
489 int cmd_len, resp_len, i;
490 u16 crc_calc, crc_recv;
491 struct wilc_spi_cmd *c;
492 struct wilc_spi_rsp_data *r;
493 struct wilc_spi_read_rsp_data *r_data;
494
495 memset(wb, 0x0, sizeof(wb));
496 memset(rb, 0x0, sizeof(rb));
497 c = (struct wilc_spi_cmd *)wb;
498 c->cmd_type = cmd;
499 if (cmd == CMD_SINGLE_READ) {
500 c->u.simple_cmd.addr[0] = adr >> 16;
501 c->u.simple_cmd.addr[1] = adr >> 8;
502 c->u.simple_cmd.addr[2] = adr;
503 } else if (cmd == CMD_INTERNAL_READ) {
504 c->u.simple_cmd.addr[0] = adr >> 8;
505 if (clockless == 1)
506 c->u.simple_cmd.addr[0] |= BIT(7);
507 c->u.simple_cmd.addr[1] = adr;
508 c->u.simple_cmd.addr[2] = 0x0;
509 } else {
510 dev_err(&spi->dev, "cmd [%x] not supported\n", cmd);
511 return -EINVAL;
512 }
513
514 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
515 resp_len = sizeof(*r) + sizeof(*r_data) + WILC_SPI_RSP_HDR_EXTRA_DATA;
516
517 if (spi_priv->crc7_enabled) {
518 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
519 cmd_len += 1;
520 resp_len += 2;
521 }
522
523 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
524 dev_err(&spi->dev,
525 "spi buffer size too small (%d) (%d) (%zu)\n",
526 cmd_len, resp_len, ARRAY_SIZE(wb));
527 return -EINVAL;
528 }
529
530 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
531 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
532 return -EINVAL;
533 }
534
535 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
536 if (r->rsp_cmd_type != cmd && !clockless) {
537 if (!spi_priv->probing_crc)
538 dev_err(&spi->dev,
539 "Failed cmd, cmd (%02x), resp (%02x)\n",
540 cmd, r->rsp_cmd_type);
541 return -EINVAL;
542 }
543
544 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
545 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
546 r->status);
547 return -EINVAL;
548 }
549
550 for (i = 0; i < WILC_SPI_RSP_HDR_EXTRA_DATA; ++i)
551 if (WILC_GET_RESP_HDR_START(r->data[i]) == 0xf)
552 break;
553
554 if (i >= WILC_SPI_RSP_HDR_EXTRA_DATA) {
555 dev_err(&spi->dev, "Error, data start missing\n");
556 return -EINVAL;
557 }
558
559 r_data = (struct wilc_spi_read_rsp_data *)&r->data[i];
560
561 if (b)
562 memcpy(b, r_data->data, 4);
563
564 if (!clockless && spi_priv->crc16_enabled) {
565 crc_recv = (r_data->crc[0] << 8) | r_data->crc[1];
566 crc_calc = crc_itu_t(0xffff, r_data->data, 4);
567 if (crc_recv != crc_calc) {
568 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
569 "(calculated 0x%04x)\n", __func__,
570 crc_recv, crc_calc);
571 return -EINVAL;
572 }
573 }
574
575 return 0;
576}
577
578static int wilc_spi_write_cmd(struct wilc *wilc, u8 cmd, u32 adr, u32 data,
579 u8 clockless)
580{
581 struct spi_device *spi = to_spi_device(wilc->dev);
582 struct wilc_spi *spi_priv = wilc->bus_data;
583 u8 wb[32], rb[32];
584 int cmd_len, resp_len;
585 struct wilc_spi_cmd *c;
586 struct wilc_spi_rsp_data *r;
587
588 memset(wb, 0x0, sizeof(wb));
589 memset(rb, 0x0, sizeof(rb));
590 c = (struct wilc_spi_cmd *)wb;
591 c->cmd_type = cmd;
592 if (cmd == CMD_INTERNAL_WRITE) {
593 c->u.internal_w_cmd.addr[0] = adr >> 8;
594 if (clockless == 1)
595 c->u.internal_w_cmd.addr[0] |= BIT(7);
596
597 c->u.internal_w_cmd.addr[1] = adr;
598 c->u.internal_w_cmd.data = cpu_to_be32(data);
599 cmd_len = offsetof(struct wilc_spi_cmd, u.internal_w_cmd.crc);
600 if (spi_priv->crc7_enabled)
601 c->u.internal_w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
602 } else if (cmd == CMD_SINGLE_WRITE) {
603 c->u.w_cmd.addr[0] = adr >> 16;
604 c->u.w_cmd.addr[1] = adr >> 8;
605 c->u.w_cmd.addr[2] = adr;
606 c->u.w_cmd.data = cpu_to_be32(data);
607 cmd_len = offsetof(struct wilc_spi_cmd, u.w_cmd.crc);
608 if (spi_priv->crc7_enabled)
609 c->u.w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
610 } else {
611 dev_err(&spi->dev, "write cmd [%x] not supported\n", cmd);
612 return -EINVAL;
613 }
614
615 if (spi_priv->crc7_enabled)
616 cmd_len += 1;
617
618 resp_len = sizeof(*r);
619
620 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
621 dev_err(&spi->dev,
622 "spi buffer size too small (%d) (%d) (%zu)\n",
623 cmd_len, resp_len, ARRAY_SIZE(wb));
624 return -EINVAL;
625 }
626
627 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
628 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
629 return -EINVAL;
630 }
631
632 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
633 /*
634 * Clockless registers operations might return unexptected responses,
635 * even if successful.
636 */
637 if (r->rsp_cmd_type != cmd && !clockless) {
638 dev_err(&spi->dev,
639 "Failed cmd response, cmd (%02x), resp (%02x)\n",
640 cmd, r->rsp_cmd_type);
641 return -EINVAL;
642 }
643
644 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
645 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
646 r->status);
647 return -EINVAL;
648 }
649
650 return 0;
651}
652
653static int wilc_spi_dma_rw(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz)
654{
655 struct spi_device *spi = to_spi_device(wilc->dev);
656 struct wilc_spi *spi_priv = wilc->bus_data;
657 u16 crc_recv, crc_calc;
658 u8 wb[32], rb[32];
659 int cmd_len, resp_len;
660 int retry, ix = 0;
661 u8 crc[2];
662 struct wilc_spi_cmd *c;
663 struct wilc_spi_rsp_data *r;
664
665 memset(wb, 0x0, sizeof(wb));
666 memset(rb, 0x0, sizeof(rb));
667 c = (struct wilc_spi_cmd *)wb;
668 c->cmd_type = cmd;
669 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_READ) {
670 c->u.dma_cmd.addr[0] = adr >> 16;
671 c->u.dma_cmd.addr[1] = adr >> 8;
672 c->u.dma_cmd.addr[2] = adr;
673 c->u.dma_cmd.size[0] = sz >> 8;
674 c->u.dma_cmd.size[1] = sz;
675 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd.crc);
676 if (spi_priv->crc7_enabled)
677 c->u.dma_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
678 } else if (cmd == CMD_DMA_EXT_WRITE || cmd == CMD_DMA_EXT_READ) {
679 c->u.dma_cmd_ext.addr[0] = adr >> 16;
680 c->u.dma_cmd_ext.addr[1] = adr >> 8;
681 c->u.dma_cmd_ext.addr[2] = adr;
682 c->u.dma_cmd_ext.size[0] = sz >> 16;
683 c->u.dma_cmd_ext.size[1] = sz >> 8;
684 c->u.dma_cmd_ext.size[2] = sz;
685 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd_ext.crc);
686 if (spi_priv->crc7_enabled)
687 c->u.dma_cmd_ext.crc[0] = wilc_get_crc7(wb, cmd_len);
688 } else {
689 dev_err(&spi->dev, "dma read write cmd [%x] not supported\n",
690 cmd);
691 return -EINVAL;
692 }
693 if (spi_priv->crc7_enabled)
694 cmd_len += 1;
695
696 resp_len = sizeof(*r);
697
698 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
699 dev_err(&spi->dev, "spi buffer size too small (%d)(%d) (%zu)\n",
700 cmd_len, resp_len, ARRAY_SIZE(wb));
701 return -EINVAL;
702 }
703
704 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
705 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
706 return -EINVAL;
707 }
708
709 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
710 if (r->rsp_cmd_type != cmd) {
711 dev_err(&spi->dev,
712 "Failed cmd response, cmd (%02x), resp (%02x)\n",
713 cmd, r->rsp_cmd_type);
714 return -EINVAL;
715 }
716
717 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
718 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
719 r->status);
720 return -EINVAL;
721 }
722
723 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_EXT_WRITE)
724 return 0;
725
726 while (sz > 0) {
727 int nbytes;
728 u8 rsp;
729
730 nbytes = min_t(u32, sz, DATA_PKT_SZ);
731
732 /*
733 * Data Response header
734 */
735 retry = 100;
736 do {
737 if (wilc_spi_rx(wilc, &rsp, 1)) {
738 dev_err(&spi->dev,
739 "Failed resp read, bus err\n");
740 return -EINVAL;
741 }
742 if (WILC_GET_RESP_HDR_START(rsp) == 0xf)
743 break;
744 } while (retry--);
745
746 /*
747 * Read bytes
748 */
749 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
750 dev_err(&spi->dev,
751 "Failed block read, bus err\n");
752 return -EINVAL;
753 }
754
755 /*
756 * Read CRC
757 */
758 if (spi_priv->crc16_enabled) {
759 if (wilc_spi_rx(wilc, crc, 2)) {
760 dev_err(&spi->dev,
761 "Failed block CRC read, bus err\n");
762 return -EINVAL;
763 }
764 crc_recv = (crc[0] << 8) | crc[1];
765 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
766 if (crc_recv != crc_calc) {
767 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
768 "(calculated 0x%04x)\n", __func__,
769 crc_recv, crc_calc);
770 return -EINVAL;
771 }
772 }
773
774 ix += nbytes;
775 sz -= nbytes;
776 }
777 return 0;
778}
779
780static int wilc_spi_special_cmd(struct wilc *wilc, u8 cmd)
781{
782 struct spi_device *spi = to_spi_device(wilc->dev);
783 struct wilc_spi *spi_priv = wilc->bus_data;
784 u8 wb[32], rb[32];
785 int cmd_len, resp_len = 0;
786 struct wilc_spi_cmd *c;
787 struct wilc_spi_special_cmd_rsp *r;
788
789 if (cmd != CMD_TERMINATE && cmd != CMD_REPEAT && cmd != CMD_RESET)
790 return -EINVAL;
791
792 memset(wb, 0x0, sizeof(wb));
793 memset(rb, 0x0, sizeof(rb));
794 c = (struct wilc_spi_cmd *)wb;
795 c->cmd_type = cmd;
796
797 if (cmd == CMD_RESET)
798 memset(c->u.simple_cmd.addr, 0xFF, 3);
799
800 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
801 resp_len = sizeof(*r);
802
803 if (spi_priv->crc7_enabled) {
804 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
805 cmd_len += 1;
806 }
807 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
808 dev_err(&spi->dev, "spi buffer size too small (%d) (%d) (%zu)\n",
809 cmd_len, resp_len, ARRAY_SIZE(wb));
810 return -EINVAL;
811 }
812
813 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
814 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
815 return -EINVAL;
816 }
817
818 r = (struct wilc_spi_special_cmd_rsp *)&rb[cmd_len];
819 if (r->rsp_cmd_type != cmd) {
820 if (!spi_priv->probing_crc)
821 dev_err(&spi->dev,
822 "Failed cmd response, cmd (%02x), resp (%02x)\n",
823 cmd, r->rsp_cmd_type);
824 return -EINVAL;
825 }
826
827 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
828 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
829 r->status);
830 return -EINVAL;
831 }
832 return 0;
833}
834
835static void wilc_spi_reset_cmd_sequence(struct wilc *wl, u8 attempt, u32 addr)
836{
837 struct spi_device *spi = to_spi_device(wl->dev);
838 struct wilc_spi *spi_priv = wl->bus_data;
839
840 if (!spi_priv->probing_crc)
841 dev_err(&spi->dev, "Reset and retry %d %x\n", attempt, addr);
842
843 usleep_range(1000, 1100);
844 wilc_spi_reset(wl);
845 usleep_range(1000, 1100);
846}
847
848static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
849{
850 struct spi_device *spi = to_spi_device(wilc->dev);
851 int result;
852 u8 cmd = CMD_SINGLE_READ;
853 u8 clockless = 0;
854 u8 i;
855
856 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
857 /* Clockless register */
858 cmd = CMD_INTERNAL_READ;
859 clockless = 1;
860 }
861
862 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
863 result = wilc_spi_single_read(wilc, cmd, addr, data, clockless);
864 if (!result) {
865 le32_to_cpus(data);
866 return 0;
867 }
868
869 /* retry is not applicable for clockless registers */
870 if (clockless)
871 break;
872
873 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
874 wilc_spi_reset_cmd_sequence(wilc, i, addr);
875 }
876
877 return result;
878}
879
880static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
881{
882 struct spi_device *spi = to_spi_device(wilc->dev);
883 int result;
884 u8 i;
885
886 if (size <= 4)
887 return -EINVAL;
888
889 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
890 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_READ, addr,
891 buf, size);
892 if (!result)
893 return 0;
894
895 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
896
897 wilc_spi_reset_cmd_sequence(wilc, i, addr);
898 }
899
900 return result;
901}
902
903static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
904{
905 struct spi_device *spi = to_spi_device(wilc->dev);
906 int result;
907 u8 i;
908
909 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
910 result = wilc_spi_write_cmd(wilc, CMD_INTERNAL_WRITE, adr,
911 dat, 0);
912 if (!result)
913 return 0;
914 dev_err(&spi->dev, "Failed internal write cmd...\n");
915
916 wilc_spi_reset_cmd_sequence(wilc, i, adr);
917 }
918
919 return result;
920}
921
922static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
923{
924 struct spi_device *spi = to_spi_device(wilc->dev);
925 struct wilc_spi *spi_priv = wilc->bus_data;
926 int result;
927 u8 i;
928
929 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
930 result = wilc_spi_single_read(wilc, CMD_INTERNAL_READ, adr,
931 data, 0);
932 if (!result) {
933 le32_to_cpus(data);
934 return 0;
935 }
936 if (!spi_priv->probing_crc)
937 dev_err(&spi->dev, "Failed internal read cmd...\n");
938
939 wilc_spi_reset_cmd_sequence(wilc, i, adr);
940 }
941
942 return result;
943}
944
945/********************************************
946 *
947 * Spi interfaces
948 *
949 ********************************************/
950
951static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
952{
953 struct spi_device *spi = to_spi_device(wilc->dev);
954 int result;
955 u8 cmd = CMD_SINGLE_WRITE;
956 u8 clockless = 0;
957 u8 i;
958
959 if (addr <= WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
960 /* Clockless register */
961 cmd = CMD_INTERNAL_WRITE;
962 clockless = 1;
963 }
964
965 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
966 result = wilc_spi_write_cmd(wilc, cmd, addr, data, clockless);
967 if (!result)
968 return 0;
969
970 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
971
972 if (clockless)
973 break;
974
975 wilc_spi_reset_cmd_sequence(wilc, i, addr);
976 }
977 return result;
978}
979
980static int spi_data_rsp(struct wilc *wilc, u8 cmd)
981{
982 struct spi_device *spi = to_spi_device(wilc->dev);
983 int result, i;
984 u8 rsp[4];
985
986 /*
987 * The response to data packets is two bytes long. For
988 * efficiency's sake, wilc_spi_write() wisely ignores the
989 * responses for all packets but the final one. The downside
990 * of that optimization is that when the final data packet is
991 * short, we may receive (part of) the response to the
992 * second-to-last packet before the one for the final packet.
993 * To handle this, we always read 4 bytes and then search for
994 * the last byte that contains the "Response Start" code (0xc
995 * in the top 4 bits). We then know that this byte is the
996 * first response byte of the final data packet.
997 */
998 result = wilc_spi_rx(wilc, rsp, sizeof(rsp));
999 if (result) {
1000 dev_err(&spi->dev, "Failed bus error...\n");
1001 return result;
1002 }
1003
1004 for (i = sizeof(rsp) - 2; i >= 0; --i)
1005 if (FIELD_GET(RSP_START_FIELD, rsp[i]) == RSP_START_TAG)
1006 break;
1007
1008 if (i < 0) {
1009 dev_err(&spi->dev,
1010 "Data packet response missing (%02x %02x %02x %02x)\n",
1011 rsp[0], rsp[1], rsp[2], rsp[3]);
1012 return -1;
1013 }
1014
1015 /* rsp[i] is the last response start byte */
1016
1017 if (FIELD_GET(RSP_TYPE_FIELD, rsp[i]) != RSP_TYPE_LAST_PACKET
1018 || rsp[i + 1] != RSP_STATE_NO_ERROR) {
1019 dev_err(&spi->dev, "Data response error (%02x %02x)\n",
1020 rsp[i], rsp[i + 1]);
1021 return -1;
1022 }
1023 return 0;
1024}
1025
1026static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
1027{
1028 struct spi_device *spi = to_spi_device(wilc->dev);
1029 int result;
1030 u8 i;
1031
1032 /*
1033 * has to be greated than 4
1034 */
1035 if (size <= 4)
1036 return -EINVAL;
1037
1038 for (i = 0; i < SPI_RETRY_MAX_LIMIT; i++) {
1039 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_WRITE, addr,
1040 NULL, size);
1041 if (result) {
1042 dev_err(&spi->dev,
1043 "Failed cmd, write block (%08x)...\n", addr);
1044 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1045 continue;
1046 }
1047
1048 /*
1049 * Data
1050 */
1051 result = spi_data_write(wilc, buf, size);
1052 if (result) {
1053 dev_err(&spi->dev, "Failed block data write...\n");
1054 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1055 continue;
1056 }
1057
1058 /*
1059 * Data response
1060 */
1061 result = spi_data_rsp(wilc, CMD_DMA_EXT_WRITE);
1062 if (result) {
1063 dev_err(&spi->dev, "Failed block data rsp...\n");
1064 wilc_spi_reset_cmd_sequence(wilc, i, addr);
1065 continue;
1066 }
1067 break;
1068 }
1069 return result;
1070}
1071
1072/********************************************
1073 *
1074 * Bus interfaces
1075 *
1076 ********************************************/
1077
1078static int wilc_spi_reset(struct wilc *wilc)
1079{
1080 struct spi_device *spi = to_spi_device(wilc->dev);
1081 struct wilc_spi *spi_priv = wilc->bus_data;
1082 int result;
1083
1084 result = wilc_spi_special_cmd(wilc, CMD_RESET);
1085 if (result && !spi_priv->probing_crc)
1086 dev_err(&spi->dev, "Failed cmd reset\n");
1087
1088 return result;
1089}
1090
1091static bool wilc_spi_is_init(struct wilc *wilc)
1092{
1093 struct wilc_spi *spi_priv = wilc->bus_data;
1094
1095 return spi_priv->isinit;
1096}
1097
1098static int wilc_spi_deinit(struct wilc *wilc)
1099{
1100 struct wilc_spi *spi_priv = wilc->bus_data;
1101
1102 spi_priv->isinit = false;
1103 wilc_wlan_power(wilc, false);
1104 return 0;
1105}
1106
1107static int wilc_spi_init(struct wilc *wilc, bool resume)
1108{
1109 struct spi_device *spi = to_spi_device(wilc->dev);
1110 struct wilc_spi *spi_priv = wilc->bus_data;
1111 u32 reg;
1112 u32 chipid;
1113 int ret, i;
1114
1115 if (spi_priv->isinit) {
1116 /* Confirm we can read chipid register without error: */
1117 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1118 if (ret == 0)
1119 return 0;
1120
1121 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1122 }
1123
1124 wilc_wlan_power(wilc, true);
1125
1126 /*
1127 * configure protocol
1128 */
1129
1130 /*
1131 * Infer the CRC settings that are currently in effect. This
1132 * is necessary because we can't be sure that the chip has
1133 * been RESET (e.g, after module unload and reload).
1134 */
1135 spi_priv->probing_crc = true;
1136 spi_priv->crc7_enabled = enable_crc7;
1137 spi_priv->crc16_enabled = false; /* don't check CRC16 during probing */
1138 for (i = 0; i < 2; ++i) {
1139 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1140 if (ret == 0)
1141 break;
1142 spi_priv->crc7_enabled = !enable_crc7;
1143 }
1144 if (ret) {
1145 dev_err(&spi->dev, "Failed with CRC7 on and off.\n");
1146 return ret;
1147 }
1148
1149 /* set up the desired CRC configuration: */
1150 reg &= ~(PROTOCOL_REG_CRC7_MASK | PROTOCOL_REG_CRC16_MASK);
1151 if (enable_crc7)
1152 reg |= PROTOCOL_REG_CRC7_MASK;
1153 if (enable_crc16)
1154 reg |= PROTOCOL_REG_CRC16_MASK;
1155
1156 /* set up the data packet size: */
1157 BUILD_BUG_ON(DATA_PKT_LOG_SZ < DATA_PKT_LOG_SZ_MIN
1158 || DATA_PKT_LOG_SZ > DATA_PKT_LOG_SZ_MAX);
1159 reg &= ~PROTOCOL_REG_PKT_SZ_MASK;
1160 reg |= FIELD_PREP(PROTOCOL_REG_PKT_SZ_MASK,
1161 DATA_PKT_LOG_SZ - DATA_PKT_LOG_SZ_MIN);
1162
1163 /* establish the new setup: */
1164 ret = spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg);
1165 if (ret) {
1166 dev_err(&spi->dev,
1167 "[wilc spi %d]: Failed internal write reg\n",
1168 __LINE__);
1169 return ret;
1170 }
1171 /* update our state to match new protocol settings: */
1172 spi_priv->crc7_enabled = enable_crc7;
1173 spi_priv->crc16_enabled = enable_crc16;
1174
1175 /* re-read to make sure new settings are in effect: */
1176 spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1177
1178 spi_priv->probing_crc = false;
1179
1180 /*
1181 * make sure can read chip id without protocol error
1182 */
1183 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1184 if (ret) {
1185 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1186 return ret;
1187 }
1188
1189 spi_priv->isinit = true;
1190
1191 return 0;
1192}
1193
1194static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
1195{
1196 int ret;
1197
1198 ret = spi_internal_read(wilc,
1199 WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE, size);
1200 *size = FIELD_GET(IRQ_DMA_WD_CNT_MASK, *size);
1201
1202 return ret;
1203}
1204
1205static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
1206{
1207 return spi_internal_read(wilc, WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE,
1208 int_status);
1209}
1210
1211static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
1212{
1213 int ret;
1214 int retry = SPI_ENABLE_VMM_RETRY_LIMIT;
1215 u32 check;
1216
1217 while (retry) {
1218 ret = spi_internal_write(wilc,
1219 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1220 val);
1221 if (ret)
1222 break;
1223
1224 ret = spi_internal_read(wilc,
1225 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1226 &check);
1227 if (ret || ((check & EN_VMM) == (val & EN_VMM)))
1228 break;
1229
1230 retry--;
1231 }
1232 return ret;
1233}
1234
1235static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
1236{
1237 struct spi_device *spi = to_spi_device(wilc->dev);
1238 u32 reg;
1239 int ret, i;
1240
1241 if (nint > MAX_NUM_INT) {
1242 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
1243 return -EINVAL;
1244 }
1245
1246 /*
1247 * interrupt pin mux select
1248 */
1249 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
1250 if (ret) {
1251 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1252 WILC_PIN_MUX_0);
1253 return ret;
1254 }
1255 reg |= BIT(8);
1256 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
1257 if (ret) {
1258 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1259 WILC_PIN_MUX_0);
1260 return ret;
1261 }
1262
1263 /*
1264 * interrupt enable
1265 */
1266 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
1267 if (ret) {
1268 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1269 WILC_INTR_ENABLE);
1270 return ret;
1271 }
1272
1273 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1274 reg |= (BIT((27 + i)));
1275
1276 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
1277 if (ret) {
1278 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1279 WILC_INTR_ENABLE);
1280 return ret;
1281 }
1282 if (nint) {
1283 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1284 if (ret) {
1285 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1286 WILC_INTR2_ENABLE);
1287 return ret;
1288 }
1289
1290 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1291 reg |= BIT(i);
1292
1293 ret = wilc_spi_write_reg(wilc, WILC_INTR2_ENABLE, reg);
1294 if (ret) {
1295 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1296 WILC_INTR2_ENABLE);
1297 return ret;
1298 }
1299 }
1300
1301 return 0;
1302}
1303
1304/* Global spi HIF function table */
1305static const struct wilc_hif_func wilc_hif_spi = {
1306 .hif_init = wilc_spi_init,
1307 .hif_deinit = wilc_spi_deinit,
1308 .hif_read_reg = wilc_spi_read_reg,
1309 .hif_write_reg = wilc_spi_write_reg,
1310 .hif_block_rx = wilc_spi_read,
1311 .hif_block_tx = wilc_spi_write,
1312 .hif_read_int = wilc_spi_read_int,
1313 .hif_clear_int_ext = wilc_spi_clear_int_ext,
1314 .hif_read_size = wilc_spi_read_size,
1315 .hif_block_tx_ext = wilc_spi_write,
1316 .hif_block_rx_ext = wilc_spi_read,
1317 .hif_sync_ext = wilc_spi_sync_ext,
1318 .hif_reset = wilc_spi_reset,
1319 .hif_is_init = wilc_spi_is_init,
1320};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4 * All rights reserved.
5 */
6
7#include <linux/clk.h>
8#include <linux/spi/spi.h>
9#include <linux/crc7.h>
10#include <linux/crc-itu-t.h>
11#include <linux/gpio/consumer.h>
12
13#include "netdev.h"
14#include "cfg80211.h"
15
16#define SPI_MODALIAS "wilc1000_spi"
17
18static bool enable_crc7; /* protect SPI commands with CRC7 */
19module_param(enable_crc7, bool, 0644);
20MODULE_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.");
25
26static bool enable_crc16; /* protect SPI data with CRC16 */
27module_param(enable_crc16, bool, 0644);
28MODULE_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.");
33
34/*
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
40 * operates at 1MHz.
41 */
42#define WILC_SPI_RSP_HDR_EXTRA_DATA 8
43
44struct wilc_spi {
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 */
49 struct wilc_gpios {
50 struct gpio_desc *enable; /* ENABLE GPIO or NULL */
51 struct gpio_desc *reset; /* RESET GPIO or NULL */
52 } gpios;
53};
54
55static const struct wilc_hif_func wilc_hif_spi;
56
57static int wilc_spi_reset(struct wilc *wilc);
58
59/********************************************
60 *
61 * Spi protocol Function
62 *
63 ********************************************/
64
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
76
77#define SPI_ENABLE_VMM_RETRY_LIMIT 2
78
79/* SPI response fields (section 11.1.2 in ATWILC1000 User Guide): */
80#define RSP_START_FIELD GENMASK(7, 4)
81#define RSP_TYPE_FIELD GENMASK(3, 0)
82
83/* SPI response values for the response fields: */
84#define RSP_START_TAG 0xc
85#define RSP_TYPE_FIRST_PACKET 0x1
86#define RSP_TYPE_INNER_PACKET 0x2
87#define RSP_TYPE_LAST_PACKET 0x3
88#define RSP_STATE_NO_ERROR 0x00
89
90#define PROTOCOL_REG_PKT_SZ_MASK GENMASK(6, 4)
91#define PROTOCOL_REG_CRC16_MASK GENMASK(3, 3)
92#define PROTOCOL_REG_CRC7_MASK GENMASK(2, 2)
93
94/*
95 * The SPI data packet size may be any integer power of two in the
96 * range from 256 to 8192 bytes.
97 */
98#define DATA_PKT_LOG_SZ_MIN 8 /* 256 B */
99#define DATA_PKT_LOG_SZ_MAX 13 /* 8 KiB */
100
101/*
102 * Select the data packet size (log2 of number of bytes): Use the
103 * maximum data packet size. We only retransmit complete packets, so
104 * there is no benefit from using smaller data packets.
105 */
106#define DATA_PKT_LOG_SZ DATA_PKT_LOG_SZ_MAX
107#define DATA_PKT_SZ (1 << DATA_PKT_LOG_SZ)
108
109#define WILC_SPI_COMMAND_STAT_SUCCESS 0
110#define WILC_GET_RESP_HDR_START(h) (((h) >> 4) & 0xf)
111
112struct wilc_spi_cmd {
113 u8 cmd_type;
114 union {
115 struct {
116 u8 addr[3];
117 u8 crc[];
118 } __packed simple_cmd;
119 struct {
120 u8 addr[3];
121 u8 size[2];
122 u8 crc[];
123 } __packed dma_cmd;
124 struct {
125 u8 addr[3];
126 u8 size[3];
127 u8 crc[];
128 } __packed dma_cmd_ext;
129 struct {
130 u8 addr[2];
131 __be32 data;
132 u8 crc[];
133 } __packed internal_w_cmd;
134 struct {
135 u8 addr[3];
136 __be32 data;
137 u8 crc[];
138 } __packed w_cmd;
139 } u;
140} __packed;
141
142struct wilc_spi_read_rsp_data {
143 u8 header;
144 u8 data[4];
145 u8 crc[];
146} __packed;
147
148struct wilc_spi_rsp_data {
149 u8 rsp_cmd_type;
150 u8 status;
151 u8 data[];
152} __packed;
153
154struct wilc_spi_special_cmd_rsp {
155 u8 skip_byte;
156 u8 rsp_cmd_type;
157 u8 status;
158} __packed;
159
160static int wilc_parse_gpios(struct wilc *wilc)
161{
162 struct spi_device *spi = to_spi_device(wilc->dev);
163 struct wilc_spi *spi_priv = wilc->bus_data;
164 struct wilc_gpios *gpios = &spi_priv->gpios;
165
166 /* get ENABLE pin and deassert it (if it is defined): */
167 gpios->enable = devm_gpiod_get_optional(&spi->dev,
168 "enable", GPIOD_OUT_LOW);
169 /* get RESET pin and assert it (if it is defined): */
170 if (gpios->enable) {
171 /* if enable pin exists, reset must exist as well */
172 gpios->reset = devm_gpiod_get(&spi->dev,
173 "reset", GPIOD_OUT_HIGH);
174 if (IS_ERR(gpios->reset)) {
175 dev_err(&spi->dev, "missing reset gpio.\n");
176 return PTR_ERR(gpios->reset);
177 }
178 } else {
179 gpios->reset = devm_gpiod_get_optional(&spi->dev,
180 "reset", GPIOD_OUT_HIGH);
181 }
182 return 0;
183}
184
185static void wilc_wlan_power(struct wilc *wilc, bool on)
186{
187 struct wilc_spi *spi_priv = wilc->bus_data;
188 struct wilc_gpios *gpios = &spi_priv->gpios;
189
190 if (on) {
191 /* assert ENABLE: */
192 gpiod_set_value(gpios->enable, 1);
193 mdelay(5);
194 /* assert RESET: */
195 gpiod_set_value(gpios->reset, 1);
196 } else {
197 /* deassert RESET: */
198 gpiod_set_value(gpios->reset, 0);
199 /* deassert ENABLE: */
200 gpiod_set_value(gpios->enable, 0);
201 }
202}
203
204static int wilc_bus_probe(struct spi_device *spi)
205{
206 int ret;
207 struct wilc *wilc;
208 struct wilc_spi *spi_priv;
209
210 spi_priv = kzalloc(sizeof(*spi_priv), GFP_KERNEL);
211 if (!spi_priv)
212 return -ENOMEM;
213
214 ret = wilc_cfg80211_init(&wilc, &spi->dev, WILC_HIF_SPI, &wilc_hif_spi);
215 if (ret)
216 goto free;
217
218 spi_set_drvdata(spi, wilc);
219 wilc->dev = &spi->dev;
220 wilc->bus_data = spi_priv;
221 wilc->dev_irq_num = spi->irq;
222
223 ret = wilc_parse_gpios(wilc);
224 if (ret < 0)
225 goto netdev_cleanup;
226
227 wilc->rtc_clk = devm_clk_get_optional(&spi->dev, "rtc");
228 if (IS_ERR(wilc->rtc_clk)) {
229 ret = PTR_ERR(wilc->rtc_clk);
230 goto netdev_cleanup;
231 }
232 clk_prepare_enable(wilc->rtc_clk);
233
234 return 0;
235
236netdev_cleanup:
237 wilc_netdev_cleanup(wilc);
238free:
239 kfree(spi_priv);
240 return ret;
241}
242
243static void wilc_bus_remove(struct spi_device *spi)
244{
245 struct wilc *wilc = spi_get_drvdata(spi);
246 struct wilc_spi *spi_priv = wilc->bus_data;
247
248 clk_disable_unprepare(wilc->rtc_clk);
249 wilc_netdev_cleanup(wilc);
250 kfree(spi_priv);
251}
252
253static const struct of_device_id wilc_of_match[] = {
254 { .compatible = "microchip,wilc1000", },
255 { /* sentinel */ }
256};
257MODULE_DEVICE_TABLE(of, wilc_of_match);
258
259static const struct spi_device_id wilc_spi_id[] = {
260 { "wilc1000", 0 },
261 { /* sentinel */ }
262};
263MODULE_DEVICE_TABLE(spi, wilc_spi_id);
264
265static struct spi_driver wilc_spi_driver = {
266 .driver = {
267 .name = SPI_MODALIAS,
268 .of_match_table = wilc_of_match,
269 },
270 .id_table = wilc_spi_id,
271 .probe = wilc_bus_probe,
272 .remove = wilc_bus_remove,
273};
274module_spi_driver(wilc_spi_driver);
275MODULE_LICENSE("GPL");
276
277static int wilc_spi_tx(struct wilc *wilc, u8 *b, u32 len)
278{
279 struct spi_device *spi = to_spi_device(wilc->dev);
280 int ret;
281 struct spi_message msg;
282
283 if (len > 0 && b) {
284 struct spi_transfer tr = {
285 .tx_buf = b,
286 .len = len,
287 .delay = {
288 .value = 0,
289 .unit = SPI_DELAY_UNIT_USECS
290 },
291 };
292 char *r_buffer = kzalloc(len, GFP_KERNEL);
293
294 if (!r_buffer)
295 return -ENOMEM;
296
297 tr.rx_buf = r_buffer;
298 dev_dbg(&spi->dev, "Request writing %d bytes\n", len);
299
300 memset(&msg, 0, sizeof(msg));
301 spi_message_init(&msg);
302 msg.spi = spi;
303 spi_message_add_tail(&tr, &msg);
304
305 ret = spi_sync(spi, &msg);
306 if (ret < 0)
307 dev_err(&spi->dev, "SPI transaction failed\n");
308
309 kfree(r_buffer);
310 } else {
311 dev_err(&spi->dev,
312 "can't write data with the following length: %d\n",
313 len);
314 ret = -EINVAL;
315 }
316
317 return ret;
318}
319
320static int wilc_spi_rx(struct wilc *wilc, u8 *rb, u32 rlen)
321{
322 struct spi_device *spi = to_spi_device(wilc->dev);
323 int ret;
324
325 if (rlen > 0) {
326 struct spi_message msg;
327 struct spi_transfer tr = {
328 .rx_buf = rb,
329 .len = rlen,
330 .delay = {
331 .value = 0,
332 .unit = SPI_DELAY_UNIT_USECS
333 },
334
335 };
336 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
337
338 if (!t_buffer)
339 return -ENOMEM;
340
341 tr.tx_buf = t_buffer;
342
343 memset(&msg, 0, sizeof(msg));
344 spi_message_init(&msg);
345 msg.spi = spi;
346 spi_message_add_tail(&tr, &msg);
347
348 ret = spi_sync(spi, &msg);
349 if (ret < 0)
350 dev_err(&spi->dev, "SPI transaction failed\n");
351 kfree(t_buffer);
352 } else {
353 dev_err(&spi->dev,
354 "can't read data with the following length: %u\n",
355 rlen);
356 ret = -EINVAL;
357 }
358
359 return ret;
360}
361
362static int wilc_spi_tx_rx(struct wilc *wilc, u8 *wb, u8 *rb, u32 rlen)
363{
364 struct spi_device *spi = to_spi_device(wilc->dev);
365 int ret;
366
367 if (rlen > 0) {
368 struct spi_message msg;
369 struct spi_transfer tr = {
370 .rx_buf = rb,
371 .tx_buf = wb,
372 .len = rlen,
373 .bits_per_word = 8,
374 .delay = {
375 .value = 0,
376 .unit = SPI_DELAY_UNIT_USECS
377 },
378
379 };
380
381 memset(&msg, 0, sizeof(msg));
382 spi_message_init(&msg);
383 msg.spi = spi;
384
385 spi_message_add_tail(&tr, &msg);
386 ret = spi_sync(spi, &msg);
387 if (ret < 0)
388 dev_err(&spi->dev, "SPI transaction failed\n");
389 } else {
390 dev_err(&spi->dev,
391 "can't read data with the following length: %u\n",
392 rlen);
393 ret = -EINVAL;
394 }
395
396 return ret;
397}
398
399static int spi_data_write(struct wilc *wilc, u8 *b, u32 sz)
400{
401 struct spi_device *spi = to_spi_device(wilc->dev);
402 struct wilc_spi *spi_priv = wilc->bus_data;
403 int ix, nbytes;
404 int result = 0;
405 u8 cmd, order, crc[2];
406 u16 crc_calc;
407
408 /*
409 * Data
410 */
411 ix = 0;
412 do {
413 if (sz <= DATA_PKT_SZ) {
414 nbytes = sz;
415 order = 0x3;
416 } else {
417 nbytes = DATA_PKT_SZ;
418 if (ix == 0)
419 order = 0x1;
420 else
421 order = 0x02;
422 }
423
424 /*
425 * Write command
426 */
427 cmd = 0xf0;
428 cmd |= order;
429
430 if (wilc_spi_tx(wilc, &cmd, 1)) {
431 dev_err(&spi->dev,
432 "Failed data block cmd write, bus error...\n");
433 result = -EINVAL;
434 break;
435 }
436
437 /*
438 * Write data
439 */
440 if (wilc_spi_tx(wilc, &b[ix], nbytes)) {
441 dev_err(&spi->dev,
442 "Failed data block write, bus error...\n");
443 result = -EINVAL;
444 break;
445 }
446
447 /*
448 * Write CRC
449 */
450 if (spi_priv->crc16_enabled) {
451 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
452 crc[0] = crc_calc >> 8;
453 crc[1] = crc_calc;
454 if (wilc_spi_tx(wilc, crc, 2)) {
455 dev_err(&spi->dev, "Failed data block crc write, bus error...\n");
456 result = -EINVAL;
457 break;
458 }
459 }
460
461 /*
462 * No need to wait for response
463 */
464 ix += nbytes;
465 sz -= nbytes;
466 } while (sz);
467
468 return result;
469}
470
471/********************************************
472 *
473 * Spi Internal Read/Write Function
474 *
475 ********************************************/
476static u8 wilc_get_crc7(u8 *buffer, u32 len)
477{
478 return crc7_be(0xfe, buffer, len);
479}
480
481static int wilc_spi_single_read(struct wilc *wilc, u8 cmd, u32 adr, void *b,
482 u8 clockless)
483{
484 struct spi_device *spi = to_spi_device(wilc->dev);
485 struct wilc_spi *spi_priv = wilc->bus_data;
486 u8 wb[32], rb[32];
487 int cmd_len, resp_len, i;
488 u16 crc_calc, crc_recv;
489 struct wilc_spi_cmd *c;
490 struct wilc_spi_rsp_data *r;
491 struct wilc_spi_read_rsp_data *r_data;
492
493 memset(wb, 0x0, sizeof(wb));
494 memset(rb, 0x0, sizeof(rb));
495 c = (struct wilc_spi_cmd *)wb;
496 c->cmd_type = cmd;
497 if (cmd == CMD_SINGLE_READ) {
498 c->u.simple_cmd.addr[0] = adr >> 16;
499 c->u.simple_cmd.addr[1] = adr >> 8;
500 c->u.simple_cmd.addr[2] = adr;
501 } else if (cmd == CMD_INTERNAL_READ) {
502 c->u.simple_cmd.addr[0] = adr >> 8;
503 if (clockless == 1)
504 c->u.simple_cmd.addr[0] |= BIT(7);
505 c->u.simple_cmd.addr[1] = adr;
506 c->u.simple_cmd.addr[2] = 0x0;
507 } else {
508 dev_err(&spi->dev, "cmd [%x] not supported\n", cmd);
509 return -EINVAL;
510 }
511
512 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
513 resp_len = sizeof(*r) + sizeof(*r_data) + WILC_SPI_RSP_HDR_EXTRA_DATA;
514
515 if (spi_priv->crc7_enabled) {
516 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
517 cmd_len += 1;
518 resp_len += 2;
519 }
520
521 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
522 dev_err(&spi->dev,
523 "spi buffer size too small (%d) (%d) (%zu)\n",
524 cmd_len, resp_len, ARRAY_SIZE(wb));
525 return -EINVAL;
526 }
527
528 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
529 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
530 return -EINVAL;
531 }
532
533 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
534 if (r->rsp_cmd_type != cmd && !clockless) {
535 if (!spi_priv->probing_crc)
536 dev_err(&spi->dev,
537 "Failed cmd, cmd (%02x), resp (%02x)\n",
538 cmd, r->rsp_cmd_type);
539 return -EINVAL;
540 }
541
542 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
543 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
544 r->status);
545 return -EINVAL;
546 }
547
548 for (i = 0; i < WILC_SPI_RSP_HDR_EXTRA_DATA; ++i)
549 if (WILC_GET_RESP_HDR_START(r->data[i]) == 0xf)
550 break;
551
552 if (i >= WILC_SPI_RSP_HDR_EXTRA_DATA) {
553 dev_err(&spi->dev, "Error, data start missing\n");
554 return -EINVAL;
555 }
556
557 r_data = (struct wilc_spi_read_rsp_data *)&r->data[i];
558
559 if (b)
560 memcpy(b, r_data->data, 4);
561
562 if (!clockless && spi_priv->crc16_enabled) {
563 crc_recv = (r_data->crc[0] << 8) | r_data->crc[1];
564 crc_calc = crc_itu_t(0xffff, r_data->data, 4);
565 if (crc_recv != crc_calc) {
566 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
567 "(calculated 0x%04x)\n", __func__,
568 crc_recv, crc_calc);
569 return -EINVAL;
570 }
571 }
572
573 return 0;
574}
575
576static int wilc_spi_write_cmd(struct wilc *wilc, u8 cmd, u32 adr, u32 data,
577 u8 clockless)
578{
579 struct spi_device *spi = to_spi_device(wilc->dev);
580 struct wilc_spi *spi_priv = wilc->bus_data;
581 u8 wb[32], rb[32];
582 int cmd_len, resp_len;
583 struct wilc_spi_cmd *c;
584 struct wilc_spi_rsp_data *r;
585
586 memset(wb, 0x0, sizeof(wb));
587 memset(rb, 0x0, sizeof(rb));
588 c = (struct wilc_spi_cmd *)wb;
589 c->cmd_type = cmd;
590 if (cmd == CMD_INTERNAL_WRITE) {
591 c->u.internal_w_cmd.addr[0] = adr >> 8;
592 if (clockless == 1)
593 c->u.internal_w_cmd.addr[0] |= BIT(7);
594
595 c->u.internal_w_cmd.addr[1] = adr;
596 c->u.internal_w_cmd.data = cpu_to_be32(data);
597 cmd_len = offsetof(struct wilc_spi_cmd, u.internal_w_cmd.crc);
598 if (spi_priv->crc7_enabled)
599 c->u.internal_w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
600 } else if (cmd == CMD_SINGLE_WRITE) {
601 c->u.w_cmd.addr[0] = adr >> 16;
602 c->u.w_cmd.addr[1] = adr >> 8;
603 c->u.w_cmd.addr[2] = adr;
604 c->u.w_cmd.data = cpu_to_be32(data);
605 cmd_len = offsetof(struct wilc_spi_cmd, u.w_cmd.crc);
606 if (spi_priv->crc7_enabled)
607 c->u.w_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
608 } else {
609 dev_err(&spi->dev, "write cmd [%x] not supported\n", cmd);
610 return -EINVAL;
611 }
612
613 if (spi_priv->crc7_enabled)
614 cmd_len += 1;
615
616 resp_len = sizeof(*r);
617
618 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
619 dev_err(&spi->dev,
620 "spi buffer size too small (%d) (%d) (%zu)\n",
621 cmd_len, resp_len, ARRAY_SIZE(wb));
622 return -EINVAL;
623 }
624
625 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
626 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
627 return -EINVAL;
628 }
629
630 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
631 /*
632 * Clockless registers operations might return unexptected responses,
633 * even if successful.
634 */
635 if (r->rsp_cmd_type != cmd && !clockless) {
636 dev_err(&spi->dev,
637 "Failed cmd response, cmd (%02x), resp (%02x)\n",
638 cmd, r->rsp_cmd_type);
639 return -EINVAL;
640 }
641
642 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS && !clockless) {
643 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
644 r->status);
645 return -EINVAL;
646 }
647
648 return 0;
649}
650
651static int wilc_spi_dma_rw(struct wilc *wilc, u8 cmd, u32 adr, u8 *b, u32 sz)
652{
653 struct spi_device *spi = to_spi_device(wilc->dev);
654 struct wilc_spi *spi_priv = wilc->bus_data;
655 u16 crc_recv, crc_calc;
656 u8 wb[32], rb[32];
657 int cmd_len, resp_len;
658 int retry, ix = 0;
659 u8 crc[2];
660 struct wilc_spi_cmd *c;
661 struct wilc_spi_rsp_data *r;
662
663 memset(wb, 0x0, sizeof(wb));
664 memset(rb, 0x0, sizeof(rb));
665 c = (struct wilc_spi_cmd *)wb;
666 c->cmd_type = cmd;
667 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_READ) {
668 c->u.dma_cmd.addr[0] = adr >> 16;
669 c->u.dma_cmd.addr[1] = adr >> 8;
670 c->u.dma_cmd.addr[2] = adr;
671 c->u.dma_cmd.size[0] = sz >> 8;
672 c->u.dma_cmd.size[1] = sz;
673 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd.crc);
674 if (spi_priv->crc7_enabled)
675 c->u.dma_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
676 } else if (cmd == CMD_DMA_EXT_WRITE || cmd == CMD_DMA_EXT_READ) {
677 c->u.dma_cmd_ext.addr[0] = adr >> 16;
678 c->u.dma_cmd_ext.addr[1] = adr >> 8;
679 c->u.dma_cmd_ext.addr[2] = adr;
680 c->u.dma_cmd_ext.size[0] = sz >> 16;
681 c->u.dma_cmd_ext.size[1] = sz >> 8;
682 c->u.dma_cmd_ext.size[2] = sz;
683 cmd_len = offsetof(struct wilc_spi_cmd, u.dma_cmd_ext.crc);
684 if (spi_priv->crc7_enabled)
685 c->u.dma_cmd_ext.crc[0] = wilc_get_crc7(wb, cmd_len);
686 } else {
687 dev_err(&spi->dev, "dma read write cmd [%x] not supported\n",
688 cmd);
689 return -EINVAL;
690 }
691 if (spi_priv->crc7_enabled)
692 cmd_len += 1;
693
694 resp_len = sizeof(*r);
695
696 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
697 dev_err(&spi->dev, "spi buffer size too small (%d)(%d) (%zu)\n",
698 cmd_len, resp_len, ARRAY_SIZE(wb));
699 return -EINVAL;
700 }
701
702 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
703 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
704 return -EINVAL;
705 }
706
707 r = (struct wilc_spi_rsp_data *)&rb[cmd_len];
708 if (r->rsp_cmd_type != cmd) {
709 dev_err(&spi->dev,
710 "Failed cmd response, cmd (%02x), resp (%02x)\n",
711 cmd, r->rsp_cmd_type);
712 return -EINVAL;
713 }
714
715 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
716 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
717 r->status);
718 return -EINVAL;
719 }
720
721 if (cmd == CMD_DMA_WRITE || cmd == CMD_DMA_EXT_WRITE)
722 return 0;
723
724 while (sz > 0) {
725 int nbytes;
726 u8 rsp;
727
728 nbytes = min_t(u32, sz, DATA_PKT_SZ);
729
730 /*
731 * Data Response header
732 */
733 retry = 100;
734 do {
735 if (wilc_spi_rx(wilc, &rsp, 1)) {
736 dev_err(&spi->dev,
737 "Failed resp read, bus err\n");
738 return -EINVAL;
739 }
740 if (WILC_GET_RESP_HDR_START(rsp) == 0xf)
741 break;
742 } while (retry--);
743
744 /*
745 * Read bytes
746 */
747 if (wilc_spi_rx(wilc, &b[ix], nbytes)) {
748 dev_err(&spi->dev,
749 "Failed block read, bus err\n");
750 return -EINVAL;
751 }
752
753 /*
754 * Read CRC
755 */
756 if (spi_priv->crc16_enabled) {
757 if (wilc_spi_rx(wilc, crc, 2)) {
758 dev_err(&spi->dev,
759 "Failed block CRC read, bus err\n");
760 return -EINVAL;
761 }
762 crc_recv = (crc[0] << 8) | crc[1];
763 crc_calc = crc_itu_t(0xffff, &b[ix], nbytes);
764 if (crc_recv != crc_calc) {
765 dev_err(&spi->dev, "%s: bad CRC 0x%04x "
766 "(calculated 0x%04x)\n", __func__,
767 crc_recv, crc_calc);
768 return -EINVAL;
769 }
770 }
771
772 ix += nbytes;
773 sz -= nbytes;
774 }
775 return 0;
776}
777
778static int wilc_spi_special_cmd(struct wilc *wilc, u8 cmd)
779{
780 struct spi_device *spi = to_spi_device(wilc->dev);
781 struct wilc_spi *spi_priv = wilc->bus_data;
782 u8 wb[32], rb[32];
783 int cmd_len, resp_len = 0;
784 struct wilc_spi_cmd *c;
785 struct wilc_spi_special_cmd_rsp *r;
786
787 if (cmd != CMD_TERMINATE && cmd != CMD_REPEAT && cmd != CMD_RESET)
788 return -EINVAL;
789
790 memset(wb, 0x0, sizeof(wb));
791 memset(rb, 0x0, sizeof(rb));
792 c = (struct wilc_spi_cmd *)wb;
793 c->cmd_type = cmd;
794
795 if (cmd == CMD_RESET)
796 memset(c->u.simple_cmd.addr, 0xFF, 3);
797
798 cmd_len = offsetof(struct wilc_spi_cmd, u.simple_cmd.crc);
799 resp_len = sizeof(*r);
800
801 if (spi_priv->crc7_enabled) {
802 c->u.simple_cmd.crc[0] = wilc_get_crc7(wb, cmd_len);
803 cmd_len += 1;
804 }
805 if (cmd_len + resp_len > ARRAY_SIZE(wb)) {
806 dev_err(&spi->dev, "spi buffer size too small (%d) (%d) (%zu)\n",
807 cmd_len, resp_len, ARRAY_SIZE(wb));
808 return -EINVAL;
809 }
810
811 if (wilc_spi_tx_rx(wilc, wb, rb, cmd_len + resp_len)) {
812 dev_err(&spi->dev, "Failed cmd write, bus error...\n");
813 return -EINVAL;
814 }
815
816 r = (struct wilc_spi_special_cmd_rsp *)&rb[cmd_len];
817 if (r->rsp_cmd_type != cmd) {
818 if (!spi_priv->probing_crc)
819 dev_err(&spi->dev,
820 "Failed cmd response, cmd (%02x), resp (%02x)\n",
821 cmd, r->rsp_cmd_type);
822 return -EINVAL;
823 }
824
825 if (r->status != WILC_SPI_COMMAND_STAT_SUCCESS) {
826 dev_err(&spi->dev, "Failed cmd state response state (%02x)\n",
827 r->status);
828 return -EINVAL;
829 }
830 return 0;
831}
832
833static int wilc_spi_read_reg(struct wilc *wilc, u32 addr, u32 *data)
834{
835 struct spi_device *spi = to_spi_device(wilc->dev);
836 int result;
837 u8 cmd = CMD_SINGLE_READ;
838 u8 clockless = 0;
839
840 if (addr < WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
841 /* Clockless register */
842 cmd = CMD_INTERNAL_READ;
843 clockless = 1;
844 }
845
846 result = wilc_spi_single_read(wilc, cmd, addr, data, clockless);
847 if (result) {
848 dev_err(&spi->dev, "Failed cmd, read reg (%08x)...\n", addr);
849 return result;
850 }
851
852 le32_to_cpus(data);
853
854 return 0;
855}
856
857static int wilc_spi_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
858{
859 struct spi_device *spi = to_spi_device(wilc->dev);
860 int result;
861
862 if (size <= 4)
863 return -EINVAL;
864
865 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_READ, addr, buf, size);
866 if (result) {
867 dev_err(&spi->dev, "Failed cmd, read block (%08x)...\n", addr);
868 return result;
869 }
870
871 return 0;
872}
873
874static int spi_internal_write(struct wilc *wilc, u32 adr, u32 dat)
875{
876 struct spi_device *spi = to_spi_device(wilc->dev);
877 int result;
878
879 result = wilc_spi_write_cmd(wilc, CMD_INTERNAL_WRITE, adr, dat, 0);
880 if (result) {
881 dev_err(&spi->dev, "Failed internal write cmd...\n");
882 return result;
883 }
884
885 return 0;
886}
887
888static int spi_internal_read(struct wilc *wilc, u32 adr, u32 *data)
889{
890 struct spi_device *spi = to_spi_device(wilc->dev);
891 struct wilc_spi *spi_priv = wilc->bus_data;
892 int result;
893
894 result = wilc_spi_single_read(wilc, CMD_INTERNAL_READ, adr, data, 0);
895 if (result) {
896 if (!spi_priv->probing_crc)
897 dev_err(&spi->dev, "Failed internal read cmd...\n");
898 return result;
899 }
900
901 le32_to_cpus(data);
902
903 return 0;
904}
905
906/********************************************
907 *
908 * Spi interfaces
909 *
910 ********************************************/
911
912static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
913{
914 struct spi_device *spi = to_spi_device(wilc->dev);
915 int result;
916 u8 cmd = CMD_SINGLE_WRITE;
917 u8 clockless = 0;
918
919 if (addr < WILC_SPI_CLOCKLESS_ADDR_LIMIT) {
920 /* Clockless register */
921 cmd = CMD_INTERNAL_WRITE;
922 clockless = 1;
923 }
924
925 result = wilc_spi_write_cmd(wilc, cmd, addr, data, clockless);
926 if (result) {
927 dev_err(&spi->dev, "Failed cmd, write reg (%08x)...\n", addr);
928 return result;
929 }
930
931 return 0;
932}
933
934static int spi_data_rsp(struct wilc *wilc, u8 cmd)
935{
936 struct spi_device *spi = to_spi_device(wilc->dev);
937 int result, i;
938 u8 rsp[4];
939
940 /*
941 * The response to data packets is two bytes long. For
942 * efficiency's sake, wilc_spi_write() wisely ignores the
943 * responses for all packets but the final one. The downside
944 * of that optimization is that when the final data packet is
945 * short, we may receive (part of) the response to the
946 * second-to-last packet before the one for the final packet.
947 * To handle this, we always read 4 bytes and then search for
948 * the last byte that contains the "Response Start" code (0xc
949 * in the top 4 bits). We then know that this byte is the
950 * first response byte of the final data packet.
951 */
952 result = wilc_spi_rx(wilc, rsp, sizeof(rsp));
953 if (result) {
954 dev_err(&spi->dev, "Failed bus error...\n");
955 return result;
956 }
957
958 for (i = sizeof(rsp) - 2; i >= 0; --i)
959 if (FIELD_GET(RSP_START_FIELD, rsp[i]) == RSP_START_TAG)
960 break;
961
962 if (i < 0) {
963 dev_err(&spi->dev,
964 "Data packet response missing (%02x %02x %02x %02x)\n",
965 rsp[0], rsp[1], rsp[2], rsp[3]);
966 return -1;
967 }
968
969 /* rsp[i] is the last response start byte */
970
971 if (FIELD_GET(RSP_TYPE_FIELD, rsp[i]) != RSP_TYPE_LAST_PACKET
972 || rsp[i + 1] != RSP_STATE_NO_ERROR) {
973 dev_err(&spi->dev, "Data response error (%02x %02x)\n",
974 rsp[i], rsp[i + 1]);
975 return -1;
976 }
977 return 0;
978}
979
980static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
981{
982 struct spi_device *spi = to_spi_device(wilc->dev);
983 int result;
984
985 /*
986 * has to be greated than 4
987 */
988 if (size <= 4)
989 return -EINVAL;
990
991 result = wilc_spi_dma_rw(wilc, CMD_DMA_EXT_WRITE, addr, NULL, size);
992 if (result) {
993 dev_err(&spi->dev,
994 "Failed cmd, write block (%08x)...\n", addr);
995 return result;
996 }
997
998 /*
999 * Data
1000 */
1001 result = spi_data_write(wilc, buf, size);
1002 if (result) {
1003 dev_err(&spi->dev, "Failed block data write...\n");
1004 return result;
1005 }
1006
1007 /*
1008 * Data response
1009 */
1010 return spi_data_rsp(wilc, CMD_DMA_EXT_WRITE);
1011}
1012
1013/********************************************
1014 *
1015 * Bus interfaces
1016 *
1017 ********************************************/
1018
1019static int wilc_spi_reset(struct wilc *wilc)
1020{
1021 struct spi_device *spi = to_spi_device(wilc->dev);
1022 struct wilc_spi *spi_priv = wilc->bus_data;
1023 int result;
1024
1025 result = wilc_spi_special_cmd(wilc, CMD_RESET);
1026 if (result && !spi_priv->probing_crc)
1027 dev_err(&spi->dev, "Failed cmd reset\n");
1028
1029 return result;
1030}
1031
1032static bool wilc_spi_is_init(struct wilc *wilc)
1033{
1034 struct wilc_spi *spi_priv = wilc->bus_data;
1035
1036 return spi_priv->isinit;
1037}
1038
1039static int wilc_spi_deinit(struct wilc *wilc)
1040{
1041 struct wilc_spi *spi_priv = wilc->bus_data;
1042
1043 spi_priv->isinit = false;
1044 wilc_wlan_power(wilc, false);
1045 return 0;
1046}
1047
1048static int wilc_spi_init(struct wilc *wilc, bool resume)
1049{
1050 struct spi_device *spi = to_spi_device(wilc->dev);
1051 struct wilc_spi *spi_priv = wilc->bus_data;
1052 u32 reg;
1053 u32 chipid;
1054 int ret, i;
1055
1056 if (spi_priv->isinit) {
1057 /* Confirm we can read chipid register without error: */
1058 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1059 if (ret == 0)
1060 return 0;
1061
1062 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1063 }
1064
1065 wilc_wlan_power(wilc, true);
1066
1067 /*
1068 * configure protocol
1069 */
1070
1071 /*
1072 * Infer the CRC settings that are currently in effect. This
1073 * is necessary because we can't be sure that the chip has
1074 * been RESET (e.g, after module unload and reload).
1075 */
1076 spi_priv->probing_crc = true;
1077 spi_priv->crc7_enabled = enable_crc7;
1078 spi_priv->crc16_enabled = false; /* don't check CRC16 during probing */
1079 for (i = 0; i < 2; ++i) {
1080 ret = spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1081 if (ret == 0)
1082 break;
1083 spi_priv->crc7_enabled = !enable_crc7;
1084 }
1085 if (ret) {
1086 dev_err(&spi->dev, "Failed with CRC7 on and off.\n");
1087 return ret;
1088 }
1089
1090 /* set up the desired CRC configuration: */
1091 reg &= ~(PROTOCOL_REG_CRC7_MASK | PROTOCOL_REG_CRC16_MASK);
1092 if (enable_crc7)
1093 reg |= PROTOCOL_REG_CRC7_MASK;
1094 if (enable_crc16)
1095 reg |= PROTOCOL_REG_CRC16_MASK;
1096
1097 /* set up the data packet size: */
1098 BUILD_BUG_ON(DATA_PKT_LOG_SZ < DATA_PKT_LOG_SZ_MIN
1099 || DATA_PKT_LOG_SZ > DATA_PKT_LOG_SZ_MAX);
1100 reg &= ~PROTOCOL_REG_PKT_SZ_MASK;
1101 reg |= FIELD_PREP(PROTOCOL_REG_PKT_SZ_MASK,
1102 DATA_PKT_LOG_SZ - DATA_PKT_LOG_SZ_MIN);
1103
1104 /* establish the new setup: */
1105 ret = spi_internal_write(wilc, WILC_SPI_PROTOCOL_OFFSET, reg);
1106 if (ret) {
1107 dev_err(&spi->dev,
1108 "[wilc spi %d]: Failed internal write reg\n",
1109 __LINE__);
1110 return ret;
1111 }
1112 /* update our state to match new protocol settings: */
1113 spi_priv->crc7_enabled = enable_crc7;
1114 spi_priv->crc16_enabled = enable_crc16;
1115
1116 /* re-read to make sure new settings are in effect: */
1117 spi_internal_read(wilc, WILC_SPI_PROTOCOL_OFFSET, ®);
1118
1119 spi_priv->probing_crc = false;
1120
1121 /*
1122 * make sure can read chip id without protocol error
1123 */
1124 ret = wilc_spi_read_reg(wilc, WILC_CHIPID, &chipid);
1125 if (ret) {
1126 dev_err(&spi->dev, "Fail cmd read chip id...\n");
1127 return ret;
1128 }
1129
1130 spi_priv->isinit = true;
1131
1132 return 0;
1133}
1134
1135static int wilc_spi_read_size(struct wilc *wilc, u32 *size)
1136{
1137 int ret;
1138
1139 ret = spi_internal_read(wilc,
1140 WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE, size);
1141 *size = FIELD_GET(IRQ_DMA_WD_CNT_MASK, *size);
1142
1143 return ret;
1144}
1145
1146static int wilc_spi_read_int(struct wilc *wilc, u32 *int_status)
1147{
1148 return spi_internal_read(wilc, WILC_SPI_INT_STATUS - WILC_SPI_REG_BASE,
1149 int_status);
1150}
1151
1152static int wilc_spi_clear_int_ext(struct wilc *wilc, u32 val)
1153{
1154 int ret;
1155 int retry = SPI_ENABLE_VMM_RETRY_LIMIT;
1156 u32 check;
1157
1158 while (retry) {
1159 ret = spi_internal_write(wilc,
1160 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1161 val);
1162 if (ret)
1163 break;
1164
1165 ret = spi_internal_read(wilc,
1166 WILC_SPI_INT_CLEAR - WILC_SPI_REG_BASE,
1167 &check);
1168 if (ret || ((check & EN_VMM) == (val & EN_VMM)))
1169 break;
1170
1171 retry--;
1172 }
1173 return ret;
1174}
1175
1176static int wilc_spi_sync_ext(struct wilc *wilc, int nint)
1177{
1178 struct spi_device *spi = to_spi_device(wilc->dev);
1179 u32 reg;
1180 int ret, i;
1181
1182 if (nint > MAX_NUM_INT) {
1183 dev_err(&spi->dev, "Too many interrupts (%d)...\n", nint);
1184 return -EINVAL;
1185 }
1186
1187 /*
1188 * interrupt pin mux select
1189 */
1190 ret = wilc_spi_read_reg(wilc, WILC_PIN_MUX_0, ®);
1191 if (ret) {
1192 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1193 WILC_PIN_MUX_0);
1194 return ret;
1195 }
1196 reg |= BIT(8);
1197 ret = wilc_spi_write_reg(wilc, WILC_PIN_MUX_0, reg);
1198 if (ret) {
1199 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1200 WILC_PIN_MUX_0);
1201 return ret;
1202 }
1203
1204 /*
1205 * interrupt enable
1206 */
1207 ret = wilc_spi_read_reg(wilc, WILC_INTR_ENABLE, ®);
1208 if (ret) {
1209 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1210 WILC_INTR_ENABLE);
1211 return ret;
1212 }
1213
1214 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
1215 reg |= (BIT((27 + i)));
1216
1217 ret = wilc_spi_write_reg(wilc, WILC_INTR_ENABLE, reg);
1218 if (ret) {
1219 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1220 WILC_INTR_ENABLE);
1221 return ret;
1222 }
1223 if (nint) {
1224 ret = wilc_spi_read_reg(wilc, WILC_INTR2_ENABLE, ®);
1225 if (ret) {
1226 dev_err(&spi->dev, "Failed read reg (%08x)...\n",
1227 WILC_INTR2_ENABLE);
1228 return ret;
1229 }
1230
1231 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
1232 reg |= BIT(i);
1233
1234 ret = wilc_spi_write_reg(wilc, WILC_INTR2_ENABLE, reg);
1235 if (ret) {
1236 dev_err(&spi->dev, "Failed write reg (%08x)...\n",
1237 WILC_INTR2_ENABLE);
1238 return ret;
1239 }
1240 }
1241
1242 return 0;
1243}
1244
1245/* Global spi HIF function table */
1246static const struct wilc_hif_func wilc_hif_spi = {
1247 .hif_init = wilc_spi_init,
1248 .hif_deinit = wilc_spi_deinit,
1249 .hif_read_reg = wilc_spi_read_reg,
1250 .hif_write_reg = wilc_spi_write_reg,
1251 .hif_block_rx = wilc_spi_read,
1252 .hif_block_tx = wilc_spi_write,
1253 .hif_read_int = wilc_spi_read_int,
1254 .hif_clear_int_ext = wilc_spi_clear_int_ext,
1255 .hif_read_size = wilc_spi_read_size,
1256 .hif_block_tx_ext = wilc_spi_write,
1257 .hif_block_rx_ext = wilc_spi_read,
1258 .hif_sync_ext = wilc_spi_sync_ext,
1259 .hif_reset = wilc_spi_reset,
1260 .hif_is_init = wilc_spi_is_init,
1261};