1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
|
/*
* linux/drivers/net/wireless/libertas/if_spi.c
*
* Driver for Marvell SPI WLAN cards.
*
* Copyright 2008 Analog Devices Inc.
*
* Authors:
* Andrey Yurovsky <andrey@cozybit.com>
* Colin McCabe <colin@cozybit.com>
*
* Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/moduleparam.h>
#include <linux/firmware.h>
#include <linux/jiffies.h>
#include <linux/kthread.h>
#include <linux/list.h>
#include <linux/netdevice.h>
#include <linux/semaphore.h>
#include <linux/slab.h>
#include <linux/spi/libertas_spi.h>
#include <linux/spi/spi.h>
#include "host.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
#include "if_spi.h"
struct if_spi_card {
struct spi_device *spi;
struct lbs_private *priv;
struct libertas_spi_platform_data *pdata;
/* The card ID and card revision, as reported by the hardware. */
u16 card_id;
u8 card_rev;
/* The last time that we initiated an SPU operation */
unsigned long prev_xfer_time;
int use_dummy_writes;
unsigned long spu_port_delay;
unsigned long spu_reg_delay;
/* Handles all SPI communication (except for FW load) */
struct task_struct *spi_thread;
int run_thread;
/* Used to wake up the spi_thread */
struct semaphore spi_ready;
struct semaphore spi_thread_terminated;
u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
};
static void free_if_spi_card(struct if_spi_card *card)
{
spi_set_drvdata(card->spi, NULL);
kfree(card);
}
#define MODEL_8385 0x04
#define MODEL_8686 0x0b
#define MODEL_8688 0x10
static const struct lbs_fw_table fw_table[] = {
{ MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
{ MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
{ MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
{ MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
{ MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
{ 0, NULL, NULL }
};
MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
MODULE_FIRMWARE("libertas/gspi8385.bin");
MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
MODULE_FIRMWARE("libertas/gspi8686.bin");
MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
MODULE_FIRMWARE("libertas/gspi8688.bin");
/*
* SPI Interface Unit Routines
*
* The SPU sits between the host and the WLAN module.
* All communication with the firmware is through SPU transactions.
*
* First we have to put a SPU register name on the bus. Then we can
* either read from or write to that register.
*
*/
static void spu_transaction_init(struct if_spi_card *card)
{
if (!time_after(jiffies, card->prev_xfer_time + 1)) {
/* Unfortunately, the SPU requires a delay between successive
* transactions. If our last transaction was more than a jiffy
* ago, we have obviously already delayed enough.
* If not, we have to busy-wait to be on the safe side. */
ndelay(400);
}
}
static void spu_transaction_finish(struct if_spi_card *card)
{
card->prev_xfer_time = jiffies;
}
/* Write out a byte buffer to an SPI register,
* using a series of 16-bit transfers. */
static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
{
int err = 0;
__le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
struct spi_message m;
struct spi_transfer reg_trans;
struct spi_transfer data_trans;
spi_message_init(&m);
memset(®_trans, 0, sizeof(reg_trans));
memset(&data_trans, 0, sizeof(data_trans));
/* You must give an even number of bytes to the SPU, even if it
* doesn't care about the last one. */
BUG_ON(len & 0x1);
spu_transaction_init(card);
/* write SPU register index */
reg_trans.tx_buf = ®_out;
reg_trans.len = sizeof(reg_out);
data_trans.tx_buf = buf;
data_trans.len = len;
spi_message_add_tail(®_trans, &m);
spi_message_add_tail(&data_trans, &m);
err = spi_sync(card->spi, &m);
spu_transaction_finish(card);
return err;
}
static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
{
__le16 buff;
buff = cpu_to_le16(val);
return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
}
static inline int spu_reg_is_port_reg(u16 reg)
{
switch (reg) {
case IF_SPI_IO_RDWRPORT_REG:
case IF_SPI_CMD_RDWRPORT_REG:
case IF_SPI_DATA_RDWRPORT_REG:
return 1;
default:
return 0;
}
}
static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
{
unsigned int delay;
int err = 0;
__le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
struct spi_message m;
struct spi_transfer reg_trans;
struct spi_transfer dummy_trans;
struct spi_transfer data_trans;
/* You must take an even number of bytes from the SPU, even if you
* don't care about the last one. */
BUG_ON(len & 0x1);
spu_transaction_init(card);
spi_message_init(&m);
memset(®_trans, 0, sizeof(reg_trans));
memset(&dummy_trans, 0, sizeof(dummy_trans));
memset(&data_trans, 0, sizeof(data_trans));
/* write SPU register index */
reg_trans.tx_buf = ®_out;
reg_trans.len = sizeof(reg_out);
spi_message_add_tail(®_trans, &m);
delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
card->spu_reg_delay;
if (card->use_dummy_writes) {
/* Clock in dummy cycles while the SPU fills the FIFO */
dummy_trans.len = delay / 8;
spi_message_add_tail(&dummy_trans, &m);
} else {
/* Busy-wait while the SPU fills the FIFO */
reg_trans.delay_usecs =
DIV_ROUND_UP((100 + (delay * 10)), 1000);
}
/* read in data */
data_trans.rx_buf = buf;
data_trans.len = len;
spi_message_add_tail(&data_trans, &m);
err = spi_sync(card->spi, &m);
spu_transaction_finish(card);
return err;
}
/* Read 16 bits from an SPI register */
static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
{
__le16 buf;
int ret;
ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
if (ret == 0)
*val = le16_to_cpup(&buf);
return ret;
}
/* Read 32 bits from an SPI register.
* The low 16 bits are read first. */
static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
{
__le32 buf;
int err;
err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
if (!err)
*val = le32_to_cpup(&buf);
return err;
}
/* Keep reading 16 bits from an SPI register until you get the correct result.
*
* If mask = 0, the correct result is any non-zero number.
* If mask != 0, the correct result is any number where
* number & target_mask == target
*
* Returns -ETIMEDOUT if a second passes without the correct result. */
static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
u16 target_mask, u16 target)
{
int err;
unsigned long timeout = jiffies + 5*HZ;
while (1) {
u16 val;
err = spu_read_u16(card, reg, &val);
if (err)
return err;
if (target_mask) {
if ((val & target_mask) == target)
return 0;
} else {
if (val)
return 0;
}
udelay(100);
if (time_after(jiffies, timeout)) {
lbs_pr_err("%s: timeout with val=%02x, "
"target_mask=%02x, target=%02x\n",
__func__, val, target_mask, target);
return -ETIMEDOUT;
}
}
}
/* Read 16 bits from an SPI register until you receive a specific value.
* Returns -ETIMEDOUT if a 4 tries pass without success. */
static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
{
int err, try;
for (try = 0; try < 4; ++try) {
u32 val = 0;
err = spu_read_u32(card, reg, &val);
if (err)
return err;
if (val == target)
return 0;
mdelay(100);
}
return -ETIMEDOUT;
}
static int spu_set_interrupt_mode(struct if_spi_card *card,
int suppress_host_int,
int auto_int)
{
int err = 0;
/* We can suppress a host interrupt by clearing the appropriate
* bit in the "host interrupt status mask" register */
if (suppress_host_int) {
err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
if (err)
return err;
} else {
err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
IF_SPI_HISM_TX_DOWNLOAD_RDY |
IF_SPI_HISM_RX_UPLOAD_RDY |
IF_SPI_HISM_CMD_DOWNLOAD_RDY |
IF_SPI_HISM_CARDEVENT |
IF_SPI_HISM_CMD_UPLOAD_RDY);
if (err)
return err;
}
/* If auto-interrupts are on, the completion of certain transactions
* will trigger an interrupt automatically. If auto-interrupts
* are off, we need to set the "Card Interrupt Cause" register to
* trigger a card interrupt. */
if (auto_int) {
err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
if (err)
return err;
} else {
err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
if (err)
return err;
}
return err;
}
static int spu_get_chip_revision(struct if_spi_card *card,
u16 *card_id, u8 *card_rev)
{
int err = 0;
u32 dev_ctrl;
err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
if (err)
return err;
*card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
*card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
return err;
}
static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
{
int err = 0;
u16 rval;
/* set bus mode */
err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
if (err)
return err;
/* Check that we were able to read back what we just wrote. */
err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
if (err)
return err;
if ((rval & 0xF) != mode) {
lbs_pr_err("Can't read bus mode register.\n");
return -EIO;
}
return 0;
}
static int spu_init(struct if_spi_card *card, int use_dummy_writes)
{
int err = 0;
u32 delay;
/* We have to start up in timed delay mode so that we can safely
* read the Delay Read Register. */
card->use_dummy_writes = 0;
err = spu_set_bus_mode(card,
IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
if (err)
return err;
card->spu_port_delay = 1000;
card->spu_reg_delay = 1000;
err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
if (err)
return err;
card->spu_port_delay = delay & 0x0000ffff;
card->spu_reg_delay = (delay & 0xffff0000) >> 16;
/* If dummy clock delay mode has been requested, switch to it now */
if (use_dummy_writes) {
card->use_dummy_writes = 1;
err = spu_set_bus_mode(card,
IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
if (err)
return err;
}
lbs_deb_spi("Initialized SPU unit. "
"spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
card->spu_port_delay, card->spu_reg_delay);
return err;
}
/*
* Firmware Loading
*/
static int if_spi_prog_helper_firmware(struct if_spi_card *card,
const struct firmware *firmware)
{
int err = 0;
int bytes_remaining;
const u8 *fw;
u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
lbs_deb_enter(LBS_DEB_SPI);
err = spu_set_interrupt_mode(card, 1, 0);
if (err)
goto out;
bytes_remaining = firmware->size;
fw = firmware->data;
/* Load helper firmware image */
while (bytes_remaining > 0) {
/* Scratch pad 1 should contain the number of bytes we
* want to download to the firmware */
err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
HELPER_FW_LOAD_CHUNK_SZ);
if (err)
goto out;
err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
IF_SPI_HIST_CMD_DOWNLOAD_RDY,
IF_SPI_HIST_CMD_DOWNLOAD_RDY);
if (err)
goto out;
/* Feed the data into the command read/write port reg
* in chunks of 64 bytes */
memset(temp, 0, sizeof(temp));
memcpy(temp, fw,
min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
mdelay(10);
err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
temp, HELPER_FW_LOAD_CHUNK_SZ);
if (err)
goto out;
/* Interrupt the boot code */
err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
if (err)
goto out;
err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
IF_SPI_CIC_CMD_DOWNLOAD_OVER);
if (err)
goto out;
bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
fw += HELPER_FW_LOAD_CHUNK_SZ;
}
/* Once the helper / single stage firmware download is complete,
* write 0 to scratch pad 1 and interrupt the
* bootloader. This completes the helper download. */
err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
if (err)
goto out;
err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
if (err)
goto out;
err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
IF_SPI_CIC_CMD_DOWNLOAD_OVER);
goto out;
lbs_deb_spi("waiting for helper to boot...\n");
out:
if (err)
lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
return err;
}
/* Returns the length of the next packet the firmware expects us to send
* Sets crc_err if the previous transfer had a CRC error. */
static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
int *crc_err)
{
u16 len;
int err = 0;
/* wait until the host interrupt status register indicates
* that we are ready to download */
err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
IF_SPI_HIST_CMD_DOWNLOAD_RDY,
IF_SPI_HIST_CMD_DOWNLOAD_RDY);
if (err) {
lbs_pr_err("timed out waiting for host_int_status\n");
return err;
}
/* Ask the device how many bytes of firmware it wants. */
err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
if (err)
return err;
if (len > IF_SPI_CMD_BUF_SIZE) {
lbs_pr_err("firmware load device requested a larger "
"tranfer than we are prepared to "
"handle. (len = %d)\n", len);
return -EIO;
}
if (len & 0x1) {
lbs_deb_spi("%s: crc error\n", __func__);
len &= ~0x1;
*crc_err = 1;
} else
*crc_err = 0;
return len;
}
static int if_spi_prog_main_firmware(struct if_spi_card *card,
const struct firmware *firmware)
{
int len, prev_len;
int bytes, crc_err = 0, err = 0;
const u8 *fw;
u16 num_crc_errs;
lbs_deb_enter(LBS_DEB_SPI);
err = spu_set_interrupt_mode(card, 1, 0);
if (err)
goto out;
err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
if (err) {
lbs_pr_err("%s: timed out waiting for initial "
"scratch reg = 0\n", __func__);
goto out;
}
num_crc_errs = 0;
prev_len = 0;
bytes = firmware->size;
fw = firmware->data;
while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
if (len < 0) {
err = len;
goto out;
}
if (bytes < 0) {
/* If there are no more bytes left, we would normally
* expect to have terminated with len = 0 */
lbs_pr_err("Firmware load wants more bytes "
"than we have to offer.\n");
break;
}
if (crc_err) {
/* Previous transfer failed. */
if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
lbs_pr_err("Too many CRC errors encountered "
"in firmware load.\n");
err = -EIO;
goto out;
}
} else {
/* Previous transfer succeeded. Advance counters. */
bytes -= prev_len;
fw += prev_len;
}
if (bytes < len) {
memset(card->cmd_buffer, 0, len);
memcpy(card->cmd_buffer, fw, bytes);
} else
memcpy(card->cmd_buffer, fw, len);
err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
if (err)
goto out;
err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
card->cmd_buffer, len);
if (err)
goto out;
err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
IF_SPI_CIC_CMD_DOWNLOAD_OVER);
if (err)
goto out;
prev_len = len;
}
if (bytes > prev_len) {
lbs_pr_err("firmware load wants fewer bytes than "
"we have to offer.\n");
}
/* Confirm firmware download */
err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
SUCCESSFUL_FW_DOWNLOAD_MAGIC);
if (err) {
lbs_pr_err("failed to confirm the firmware download\n");
goto out;
}
out:
if (err)
lbs_pr_err("failed to load firmware (err=%d)\n", err);
lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
return err;
}
/*
* SPI Transfer Thread
*
* The SPI thread handles all SPI transfers, so there is no need for a lock.
*/
/* Move a command from the card to the host */
static int if_spi_c2h_cmd(struct if_spi_card *card)
{
struct lbs_private *priv = card->priv;
unsigned long flags;
int err = 0;
u16 len;
u8 i;
/* We need a buffer big enough to handle whatever people send to
* hw_host_to_card */
BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
/* It's just annoying if the buffer size isn't a multiple of 4, because
* then we might have len < IF_SPI_CMD_BUF_SIZE but
* ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
lbs_deb_enter(LBS_DEB_SPI);
/* How many bytes are there to read? */
err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
if (err)
goto out;
if (!len) {
lbs_pr_err("%s: error: card has no data for host\n",
__func__);
err = -EINVAL;
goto out;
} else if (len > IF_SPI_CMD_BUF_SIZE) {
lbs_pr_err("%s: error: response packet too large: "
"%d bytes, but maximum is %d\n",
__func__, len, IF_SPI_CMD_BUF_SIZE);
err = -EINVAL;
goto out;
}
/* Read the data from the WLAN module into our command buffer */
err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
card->cmd_buffer, ALIGN(len, 4));
if (err)
goto out;
spin_lock_irqsave(&priv->driver_lock, flags);
i = (priv->resp_idx == 0) ? 1 : 0;
BUG_ON(priv->resp_len[i]);
priv->resp_len[i] = len;
memcpy(priv->resp_buf[i], card->cmd_buffer, len);
lbs_notify_command_response(priv, i);
spin_unlock_irqrestore(&priv->driver_lock, flags);
out:
if (err)
lbs_pr_err("%s: err=%d\n", __func__, err);
lbs_deb_leave(LBS_DEB_SPI);
return err;
}
/* Move data from the card to the host */
static int if_spi_c2h_data(struct if_spi_card *card)
{
struct sk_buff *skb;
char *data;
u16 len;
int err = 0;
lbs_deb_enter(LBS_DEB_SPI);
/* How many bytes are there to read? */
err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
if (err)
goto out;
if (!len) {
lbs_pr_err("%s: error: card has no data for host\n",
__func__);
err = -EINVAL;
goto out;
} else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
lbs_pr_err("%s: error: card has %d bytes of data, but "
"our maximum skb size is %zu\n",
__func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
err = -EINVAL;
goto out;
}
/* TODO: should we allocate a smaller skb if we have less data? */
skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
if (!skb) {
err = -ENOBUFS;
goto out;
}
skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
data = skb_put(skb, len);
/* Read the data from the WLAN module into our skb... */
err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
if (err)
goto free_skb;
/* pass the SKB to libertas */
err = lbs_process_rxed_packet(card->priv, skb);
if (err)
goto free_skb;
/* success */
goto out;
free_skb:
dev_kfree_skb(skb);
out:
if (err)
lbs_pr_err("%s: err=%d\n", __func__, err);
lbs_deb_leave(LBS_DEB_SPI);
return err;
}
/* Inform the host about a card event */
static void if_spi_e2h(struct if_spi_card *card)
{
int err = 0;
u32 cause;
struct lbs_private *priv = card->priv;
err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
if (err)
goto out;
/* re-enable the card event interrupt */
spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
~IF_SPI_HICU_CARD_EVENT);
/* generate a card interrupt */
spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
lbs_queue_event(priv, cause & 0xff);
out:
if (err)
lbs_pr_err("%s: error %d\n", __func__, err);
}
static int lbs_spi_thread(void *data)
{
int err;
struct if_spi_card *card = data;
u16 hiStatus;
while (1) {
/* Wait to be woken up by one of two things. First, our ISR
* could tell us that something happened on the WLAN.
* Secondly, libertas could call hw_host_to_card with more
* data, which we might be able to send.
*/
do {
err = down_interruptible(&card->spi_ready);
if (!card->run_thread) {
up(&card->spi_thread_terminated);
do_exit(0);
}
} while (err == EINTR);
/* Read the host interrupt status register to see what we
* can do. */
err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
&hiStatus);
if (err) {
lbs_pr_err("I/O error\n");
goto err;
}
if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
err = if_spi_c2h_cmd(card);
if (err)
goto err;
}
if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
err = if_spi_c2h_data(card);
if (err)
goto err;
}
/* workaround: in PS mode, the card does not set the Command
* Download Ready bit, but it sets TX Download Ready. */
if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
(card->priv->psstate != PS_STATE_FULL_POWER &&
(hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
lbs_host_to_card_done(card->priv);
}
if (hiStatus & IF_SPI_HIST_CARD_EVENT)
if_spi_e2h(card);
err:
if (err)
lbs_pr_err("%s: got error %d\n", __func__, err);
}
}
/* Block until lbs_spi_thread thread has terminated */
static void if_spi_terminate_spi_thread(struct if_spi_card *card)
{
/* It would be nice to use kthread_stop here, but that function
* can't wake threads waiting for a semaphore. */
card->run_thread = 0;
up(&card->spi_ready);
down(&card->spi_thread_terminated);
}
/*
* Host to Card
*
* Called from Libertas to transfer some data to the WLAN device
* We can't sleep here. */
static int if_spi_host_to_card(struct lbs_private *priv,
u8 type, u8 *buf, u16 nb)
{
int err = 0;
struct if_spi_card *card = priv->card;
lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
nb = ALIGN(nb, 4);
switch (type) {
case MVMS_CMD:
err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, buf, nb);
break;
case MVMS_DAT:
err = spu_write(card, IF_SPI_DATA_RDWRPORT_REG, buf, nb);
break;
default:
lbs_pr_err("can't transfer buffer of type %d", type);
err = -EINVAL;
break;
}
lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
return err;
}
/*
* Host Interrupts
*
* Service incoming interrupts from the WLAN device. We can't sleep here, so
* don't try to talk on the SPI bus, just wake up the SPI thread.
*/
static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
{
struct if_spi_card *card = dev_id;
up(&card->spi_ready);
return IRQ_HANDLED;
}
/*
* SPI callbacks
*/
static int __devinit if_spi_probe(struct spi_device *spi)
{
struct if_spi_card *card;
struct lbs_private *priv = NULL;
struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
int err = 0, i;
u32 scratch;
struct sched_param param = { .sched_priority = 1 };
const struct firmware *helper = NULL;
const struct firmware *mainfw = NULL;
lbs_deb_enter(LBS_DEB_SPI);
if (!pdata) {
err = -EINVAL;
goto out;
}
if (pdata->setup) {
err = pdata->setup(spi);
if (err)
goto out;
}
/* Allocate card structure to represent this specific device */
card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
if (!card) {
err = -ENOMEM;
goto out;
}
spi_set_drvdata(spi, card);
card->pdata = pdata;
card->spi = spi;
card->prev_xfer_time = jiffies;
sema_init(&card->spi_ready, 0);
sema_init(&card->spi_thread_terminated, 0);
/* Initialize the SPI Interface Unit */
err = spu_init(card, pdata->use_dummy_writes);
if (err)
goto free_card;
err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
if (err)
goto free_card;
/* Firmware load */
err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
if (err)
goto free_card;
if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
lbs_deb_spi("Firmware is already loaded for "
"Marvell WLAN 802.11 adapter\n");
else {
/* Check if we support this card */
for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
if (card->card_id == fw_table[i].model)
break;
}
if (i == ARRAY_SIZE(fw_table)) {
lbs_pr_err("Unsupported chip_id: 0x%02x\n",
card->card_id);
err = -ENODEV;
goto free_card;
}
err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
card->card_id, &fw_table[0], &helper,
&mainfw);
if (err) {
lbs_pr_err("failed to find firmware (%d)\n", err);
goto free_card;
}
lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
"(chip_id = 0x%04x, chip_rev = 0x%02x) "
"attached to SPI bus_num %d, chip_select %d. "
"spi->max_speed_hz=%d\n",
card->card_id, card->card_rev,
spi->master->bus_num, spi->chip_select,
spi->max_speed_hz);
err = if_spi_prog_helper_firmware(card, helper);
if (err)
goto free_card;
err = if_spi_prog_main_firmware(card, mainfw);
if (err)
goto free_card;
lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
}
err = spu_set_interrupt_mode(card, 0, 1);
if (err)
goto free_card;
/* Register our card with libertas.
* This will call alloc_etherdev */
priv = lbs_add_card(card, &spi->dev);
if (!priv) {
err = -ENOMEM;
goto free_card;
}
card->priv = priv;
priv->card = card;
priv->hw_host_to_card = if_spi_host_to_card;
priv->enter_deep_sleep = NULL;
priv->exit_deep_sleep = NULL;
priv->reset_deep_sleep_wakeup = NULL;
priv->fw_ready = 1;
/* Initialize interrupt handling stuff. */
card->run_thread = 1;
card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
if (IS_ERR(card->spi_thread)) {
card->run_thread = 0;
err = PTR_ERR(card->spi_thread);
lbs_pr_err("error creating SPI thread: err=%d\n", err);
goto remove_card;
}
if (sched_setscheduler(card->spi_thread, SCHED_FIFO, ¶m))
lbs_pr_err("Error setting scheduler, using default.\n");
err = request_irq(spi->irq, if_spi_host_interrupt,
IRQF_TRIGGER_FALLING, "libertas_spi", card);
if (err) {
lbs_pr_err("can't get host irq line-- request_irq failed\n");
goto terminate_thread;
}
/* poke the IRQ handler so that we don't miss the first interrupt */
up(&card->spi_ready);
/* Start the card.
* This will call register_netdev, and we'll start
* getting interrupts... */
err = lbs_start_card(priv);
if (err)
goto release_irq;
lbs_deb_spi("Finished initializing WLAN module.\n");
/* successful exit */
goto out;
release_irq:
free_irq(spi->irq, card);
terminate_thread:
if_spi_terminate_spi_thread(card);
remove_card:
lbs_remove_card(priv); /* will call free_netdev */
free_card:
free_if_spi_card(card);
out:
if (helper)
release_firmware(helper);
if (mainfw)
release_firmware(mainfw);
lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
return err;
}
static int __devexit libertas_spi_remove(struct spi_device *spi)
{
struct if_spi_card *card = spi_get_drvdata(spi);
struct lbs_private *priv = card->priv;
lbs_deb_spi("libertas_spi_remove\n");
lbs_deb_enter(LBS_DEB_SPI);
lbs_stop_card(priv);
lbs_remove_card(priv); /* will call free_netdev */
priv->surpriseremoved = 1;
free_irq(spi->irq, card);
if_spi_terminate_spi_thread(card);
if (card->pdata->teardown)
card->pdata->teardown(spi);
free_if_spi_card(card);
lbs_deb_leave(LBS_DEB_SPI);
return 0;
}
static struct spi_driver libertas_spi_driver = {
.probe = if_spi_probe,
.remove = __devexit_p(libertas_spi_remove),
.driver = {
.name = "libertas_spi",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
};
/*
* Module functions
*/
static int __init if_spi_init_module(void)
{
int ret = 0;
lbs_deb_enter(LBS_DEB_SPI);
printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
ret = spi_register_driver(&libertas_spi_driver);
lbs_deb_leave(LBS_DEB_SPI);
return ret;
}
static void __exit if_spi_exit_module(void)
{
lbs_deb_enter(LBS_DEB_SPI);
spi_unregister_driver(&libertas_spi_driver);
lbs_deb_leave(LBS_DEB_SPI);
}
module_init(if_spi_init_module);
module_exit(if_spi_exit_module);
MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
"Colin McCabe <colin@cozybit.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:libertas_spi");
|