This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdio.c
3155 lines (2614 loc) · 76.8 KB
/
sdio.c
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
/*
* Marvell Wireless LAN device driver: SDIO specific handling
*
* Copyright (C) 2011-2014, Marvell International Ltd.
* Copyright (C) 2018-2020 Laird Connectivity
*
* This software file (the "File") is distributed by Marvell International
* Ltd. under the terms of the GNU General Public License Version 2, June 1991
* (the "License"). You may use, redistribute and/or modify this File in
* accordance with the terms and conditions of the License, a copy of which
* is available by writing to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
* worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
* IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
* ARE EXPRESSLY DISCLAIMED. The License provides additional details about
* this warranty disclaimer.
*/
#include <linux/module.h>
#include <linux/of.h>
#include <linux/firmware.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include "sysadpt.h"
#include "dev.h"
#include "main.h"
#include "fwcmd.h"
#include "rx.h"
#include "tx.h"
#include "sdio.h"
#include "hostcmd.h"
#include <../drivers/mmc/core/card.h>
#define MWL_SDIODRV_VERSION "10.3.0.16-20160105"
#define LRD_SDIO_VERSION LRD_BLD_VERSION "-" MWL_SDIODRV_VERSION
#define LRD_SDIO_DESC "Laird 60 Series Wireless SDIO Network Driver"
#define INTF_HEADER_LEN 4
enum mwl_pm_action {
MWL_PM_PREPARE,
MWL_PM_COMPLETE,
MWL_PM_RESUME,
MWL_PM_RESUME_EARLY,
MWL_PM_RESTORE_EARLY,
MWL_PM_SUSPEND,
MWL_PM_SUSPEND_LATE,
MWL_PM_FREEZE_LATE,
MWL_PM_POWEROFF
};
static struct mwl_chip_info mwl_chip_tbl[] = {
[MWL8864] = {
.part_name = "88W8864",
.fw_image = MWL_FW_ROOT"/88W8864_sdio.bin",
.antenna_tx = ANTENNA_TX_4_AUTO,
.antenna_rx = ANTENNA_RX_4_AUTO,
},
[MWL8897] = {
.part_name = "88W8897",
.fw_image = MWL_FW_ROOT"/88W8897_sdio.bin",
.antenna_tx = ANTENNA_TX_2,
.antenna_rx = ANTENNA_RX_2,
},
[MWL8997] = {
.part_name = "88W8997",
.fw_image = MWL_FW_ROOT"/88W8997_sdio.bin",
.mfg_image = MWL_FW_ROOT"/88W8997_sdio_mfg.bin",
.antenna_tx = ANTENNA_TX_2,
.antenna_rx = ANTENNA_RX_2,
},
};
static unsigned int reset_pwd_gpio = ARCH_NR_GPIOS;
static int mwl_write_data_sync(struct mwl_priv *priv,
u8 *buffer, u32 pkt_len, u32 port);
static int mwl_sdio_enable_int(struct mwl_priv *priv, bool enable);
static int mwl_sdio_event(struct mwl_priv *priv);
static void mwl_sdio_tx_workq(struct work_struct *work);
static void mwl_sdio_rx_recv(unsigned long data);
static int mwl_sdio_read_fw_status(struct mwl_priv *priv, u16 *dat);
static int mwl_sdio_reset(struct mwl_priv *priv);
static int mwl_sdio_set_gpio(struct mwl_sdio_card *card, int value);
static int mwl_sdio_restart_handler(struct mwl_priv *priv);
/* Device ID for SD8897 */
#define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
#define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
/* 88W8997 datasheet requires PMIC_EN/PMU_EN to remain de-asserted for a minimum of 100ms */
#define SDIO_DEFAULT_POWERDOWN_DELAY_MS 100
static void
mwl_sdio_interrupt(struct sdio_func *func);
/* WLAN IDs */
static const struct sdio_device_id mwl_sdio_id_tbl[] = {
{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
.driver_data = MWL8997},
{ },
};
static const struct of_device_id mwl_sdio_of_match_table[] = {
{ .compatible = "marvell,sd8997" },
{ }
};
/*
* This function reads data from SDIO card register.
*/
static int
mwl_read_reg(struct mwl_priv *priv, u32 reg, u8 *data)
{
struct mwl_sdio_card *card = priv->intf;
int ret = -1;
u8 val;
sdio_claim_host(card->func);
val = sdio_readb(card->func, reg, &ret);
sdio_release_host(card->func);
*data = val;
return ret;
}
/*
* This function writes data into SDIO card register.
*/
static int
mwl_write_reg(struct mwl_priv *priv, u32 reg, u8 data)
{
struct mwl_sdio_card *card = priv->intf;
int rc = 0;
sdio_claim_host(card->func);
sdio_writeb(card->func, data, reg, &rc);
sdio_release_host(card->func);
return rc;
}
static void mwl_free_sdio_mpa_buffers(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
kfree(card->mpa_tx.buf);
card->mpa_tx.buf = NULL;
card->mpa_tx.buf_size = 0;
kfree(card->mpa_rx.buf);
card->mpa_rx.buf = NULL;
card->mpa_rx.buf_size = 0;
}
/*
* This function allocates the MPA Tx and Rx buffers.
*/
static int mwl_alloc_sdio_mpa_buffers(struct mwl_priv *priv,
u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
{
struct mwl_sdio_card *card = priv->intf;
u32 rx_buf_size;
card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
if (!card->mpa_tx.buf)
goto error;
card->mpa_tx.buf_size = mpa_tx_buf_size;
rx_buf_size = max_t(u32, mpa_rx_buf_size,
(u32)SDIO_MAX_AGGR_BUF_SIZE);
card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
if (!card->mpa_rx.buf)
goto error;
card->mpa_rx.buf_size = rx_buf_size;
card->tx_pkt_unaligned_cnt = 0;
return 0;
error:
mwl_free_sdio_mpa_buffers(priv);
return -ENOMEM;
}
static void *mwl_alloc_dma_align_buf(int rx_len, gfp_t flags)
{
struct sk_buff *skb;
int buf_len, pad;
buf_len = rx_len + MWL_RX_HEADROOM + MWL_DMA_ALIGN_SZ;
skb = __dev_alloc_skb(buf_len, flags);
if (!skb)
return NULL;
skb_reserve(skb, MWL_RX_HEADROOM);
pad = MWL_ALIGN_ADDR(skb->data, MWL_DMA_ALIGN_SZ) -
(long)skb->data;
skb_reserve(skb, pad);
return skb;
}
/*
* This function polls the card status.
*/
static int
mwl_sdio_poll_card_status(struct mwl_priv *priv, u8 bits)
{
struct mwl_sdio_card *card = priv->intf;
u32 tries;
u8 cs;
for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
if (mwl_read_reg(priv, card->reg->poll_reg, &cs))
break;
else if ((cs & bits) == bits)
return 0;
lrdmwl_delay(10);
}
wiphy_err(priv->hw->wiphy,
"poll card status failed, tries = %d\n", tries);
return -1;
}
/*
* This function is used to initialize IO ports for the
* chipsets supporting SDIO new mode eg SD8897.
*/
static int mwl_init_sdio_new_mode(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
int rc = 0;
u8 reg;
card->ioport = MEM_PORT;
/* enable sdio new mode */
if (mwl_read_reg(priv, card->reg->card_cfg_2_1_reg, ®)) {
rc = -EIO;
goto err_new_mode;
}
if (mwl_write_reg(priv, card->reg->card_cfg_2_1_reg,
reg | CMD53_NEW_MODE)) {
rc = -EIO;
goto err_new_mode;
}
/* Configure cmd port and enable reading rx length from the register */
if (mwl_read_reg(priv, card->reg->cmd_cfg_0, ®)) {
rc = -EIO;
goto err_new_mode;
}
if (mwl_write_reg(priv, card->reg->cmd_cfg_0,
reg | CMD_PORT_RD_LEN_EN)) {
rc = -EIO;
goto err_new_mode;
}
/* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
* completed
*/
if (mwl_read_reg(priv, card->reg->cmd_cfg_1, ®)) {
rc = -EIO;
goto err_new_mode;
}
if (mwl_write_reg(priv, card->reg->cmd_cfg_1,
reg | CMD_PORT_AUTO_EN)) {
rc = -EIO;
goto err_new_mode;
}
err_new_mode:
return rc;
}
/* This function initializes the IO ports.
*
* The following operations are performed -
* - Read the IO ports (0, 1 and 2)
* - Set host interrupt Reset-To-Read to clear
* - Set auto re-enable interrupt
*/
static int mwl_init_sdio_ioport(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
int rc;
u8 reg;
card->ioport = 0;
rc = mwl_init_sdio_new_mode(priv);
if (rc)
goto cont;
/* Read the IO port */
rc = mwl_read_reg(priv, card->reg->io_port_0_reg, ®);
if (rc)
goto err_init_ioport;
card->ioport |= (reg & 0xff);
rc = mwl_read_reg(priv, card->reg->io_port_1_reg, ®);
if (rc)
goto err_init_ioport;
card->ioport |= ((reg & 0xff) << 8);
rc = mwl_read_reg(priv, card->reg->io_port_2_reg, ®);
if (rc)
goto err_init_ioport;
card->ioport |= ((reg & 0xff) << 16);
cont:
wiphy_info(priv->hw->wiphy, "%s: SDIO FUNC1 IO port: %#x\n",
MWL_DRV_NAME, card->ioport);
/* Set Host interrupt reset to read to clear */
rc = mwl_read_reg(priv, card->reg->host_int_rsr_reg, ®);
if (rc)
goto err_init_ioport;
mwl_write_reg(priv, card->reg->host_int_rsr_reg,
reg | card->reg->sdio_int_mask);
/* Dnld/Upld ready set to auto reset */
rc = mwl_read_reg(priv, card->reg->card_misc_cfg_reg, ®);
if (rc)
goto err_init_ioport;
mwl_write_reg(priv, card->reg->card_misc_cfg_reg,
reg | AUTO_RE_ENABLE_INT);
err_init_ioport:
return rc;
}
static int mwl_sdio_enable_int(struct mwl_priv *priv, bool enable)
{
struct mwl_sdio_card *card = priv->intf;
struct sdio_func *func = card->func;
int ret;
sdio_claim_host(func);
sdio_writeb(func, card->reg->host_int_enable,
card->reg->host_int_mask_reg ^
(enable ? 0 : card->reg->host_int_mask_reg),
&ret);
sdio_release_host(func);
wiphy_dbg(priv->hw->wiphy,
"=>%s(): %s host interrupt %s\n", __func__,
enable ? "enable" : "disable", ret ? "failed" : "ok");
return ret;
}
static int mwl_sdio_init_irq(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
struct sdio_func *func = card->func;
int ret;
wiphy_info(priv->hw->wiphy,
"%s, register IRQ\n", __func__);
sdio_claim_host(func);
/* Request the SDIO IRQ */
ret = sdio_claim_irq(func, mwl_sdio_interrupt);
if (ret)
wiphy_err(priv->hw->wiphy,
"claim irq failed: ret=%d\n", ret);
sdio_release_host(func);
return ret;
}
static int mwl_sdio_init_post(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
struct sdio_func *func = card->func;
if (priv->stop_shutdown && (mmc_card_is_removable(func->card->host) ||
!gpio_is_valid(card->reset_pwd_gpio))) {
priv->stop_shutdown = false;
dev_err(&func->dev,
"Power down when network down mode supported only, "
"when card set to non-removable and driver has direct control "
"over PMU Enable GPIO\n");
}
if (priv->mfg_mode) {
priv->ds_enable = DS_ENABLE_OFF;
//Assume ST 60 with one interface
priv->radio_caps.capability = 0;
priv->radio_caps.num_mac = 1;
priv->stop_shutdown = false;
}
return 0;
}
static int mwl_sdio_init(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
struct sdio_func *func = card->func;
const struct mwl_sdio_card_reg *reg = card->reg;
int rc;
u8 sdio_ireg;
int num;
/* not sure this patch is needed or not?? */
func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
sdio_claim_host(func);
rc = sdio_enable_func(func);
sdio_release_host(func);
if (rc != 0) {
dev_err(&func->dev, "%s: failed to enable sdio function\n", __func__);
return rc;
}
priv->host_if = MWL_IF_SDIO;
card->priv = priv;
priv->dev = &func->dev;
sdio_set_drvdata(card->func, priv->hw);
priv->if_ops.hardware_restart = mwl_sdio_restart_handler;
/*
* Read the host_int_status_reg for ACK the first interrupt got
* from the bootloader. If we don't do this we get a interrupt
* as soon as we register the irq.
*/
mwl_read_reg(priv, card->reg->host_int_status_reg, &sdio_ireg);
/* Get SDIO ioport */
mwl_init_sdio_ioport(priv);
/* Initialize SDIO variables in card */
card->mp_rd_bitmap = 0;
card->mp_wr_bitmap = 0;
card->curr_rd_port = reg->start_rd_port;
card->curr_wr_port = reg->start_wr_port;
card->mp_data_port_mask = reg->data_port_mask;
card->mpa_tx.buf_len = 0;
card->mpa_tx.pkt_cnt = 0;
card->mpa_tx.start_port = 0;
card->mpa_tx.enabled = 1;
card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
card->mpa_rx.buf_len = 0;
card->mpa_rx.pkt_cnt = 0;
card->mpa_rx.start_port = 0;
card->mpa_rx.enabled = 1;
card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
/* Allocate buffers for SDIO MP-A */
card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
if (!card->mp_regs) {
rc = -ENOMEM;
goto err_return;
}
/* Allocate skb pointer buffers */
card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
card->mp_agg_pkt_limit, GFP_KERNEL);
if (!card->mpa_rx.skb_arr) {
rc = -ENOMEM;
goto err_return;
}
card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
card->mp_agg_pkt_limit, GFP_KERNEL);
if (!card->mpa_rx.len_arr) {
rc = -ENOMEM;
goto err_return;
}
rc = mwl_alloc_sdio_mpa_buffers(priv,
card->mp_tx_agg_buf_size,
card->mp_rx_agg_buf_size);
/* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
if (rc && (card->mp_tx_agg_buf_size == MWL_MP_AGGR_BUF_SIZE_MAX ||
card->mp_rx_agg_buf_size == MWL_MP_AGGR_BUF_SIZE_MAX)) {
/* Disable rx single port aggregation */
card->host_disable_sdio_rx_aggr = true;
rc = mwl_alloc_sdio_mpa_buffers
(priv, MWL_MP_AGGR_BUF_SIZE_32K,
MWL_MP_AGGR_BUF_SIZE_32K);
if (rc) {
/* Disable multi port aggregation */
card->mpa_tx.enabled = 0;
card->mpa_rx.enabled = 0;
}
}
spin_lock_init(&card->int_lock);
spin_lock_init(&card->rx_proc_lock);
init_waitqueue_head(&card->cmd_wait_q.wait);
skb_queue_head_init(&card->rx_data_q);
card->cmd_wait_q.status = 0;
card->cmd_resp_recvd = false;
card->cmd_id = 0;
card->int_status = 0;
init_waitqueue_head(&card->wait_deepsleep);
sdio_claim_host(card->func);
/* Set block size */
rc = sdio_set_block_size(card->func, MWL_SDIO_BLOCK_SIZE);
sdio_release_host(card->func);
if (rc) {
wiphy_err(priv->hw->wiphy,
"cannot set SDIO block size rc 0x%04x\n", rc);
goto err_return;
}
priv->chip_type = card->chip_type;
/* This routine is called during restart scenarios, but we keep the
* original pcmd_buf/pcmd_event_buf until driver unload
*/
if (!priv->pcmd_buf) {
priv->pcmd_buf = kzalloc(CMD_BUF_SIZE, GFP_KERNEL);
if (!priv->pcmd_buf) {
wiphy_err(priv->hw->wiphy,
"%s: cannot alloc memory for command buffer\n",
MWL_DRV_NAME);
rc = -ENOMEM;
goto err_return;
}
}
else
memset(priv->pcmd_buf, 0x00, CMD_BUF_SIZE);
if (!priv->pcmd_event_buf) {
priv->pcmd_event_buf = kzalloc(CMD_BUF_SIZE, GFP_KERNEL);
if (!priv->pcmd_event_buf) {
wiphy_err(priv->hw->wiphy,"%s: cannot alloc memory for command_event buffer\n",
MWL_DRV_NAME);
rc = -ENOMEM;
goto err_return;
}
}
else
memset(priv->pcmd_event_buf, 0x00, CMD_BUF_SIZE);
/* Initialize the tasklet first in case there are tx/rx interrupts */
if (!priv->recovery_in_progress) {
// Do not reinitialize the tasklet; it could be on the CPU queue
// Tasklet has been disabled in mwl_mac80211_stop
tasklet_init(&priv->rx_task, (void *)mwl_sdio_rx_recv,
(unsigned long)priv->hw);
tasklet_disable(&priv->rx_task);
}
for (num = 0; num < SYSADPT_NUM_OF_DESC_DATA; num++)
skb_queue_head_init(&priv->txq[num]);
mwl_sdio_init_irq(priv);
#ifdef CONFIG_PM
priv->wow.capable =
(sdio_get_host_pm_caps(func) & MMC_PM_KEEP_POWER) != 0;
#endif
// Re-initialization needed in case of restart
card->is_deepsleep = 0;
card->dnld_cmd_failure = 0;
return 0;
err_return:
mwl_free_sdio_mpa_buffers(priv);
kfree(priv->pcmd_buf); priv->pcmd_buf = NULL;
kfree(priv->pcmd_event_buf); priv->pcmd_event_buf = NULL;
kfree(card->mpa_rx.skb_arr); card->mpa_rx.skb_arr = NULL;
kfree(card->mpa_rx.len_arr); card->mpa_rx.len_arr = NULL;
kfree(card->mp_regs); card->mp_regs = NULL;
return rc;
}
/*
* This function sends data to the card.
*/
static int mwl_write_data_to_card(struct mwl_priv *priv,
u8 *payload, u32 pkt_len, u32 port)
{
u32 i = 0;
int ret;
do {
ret = mwl_write_data_sync(priv, payload, pkt_len, port);
if (ret && ret != -ENOMEDIUM) {
i++;
wiphy_err(priv->hw->wiphy,
"host_to_card, write iomem\t"
"(%d) failed: %d\n", i, ret);
if (mwl_write_reg(priv, CONFIGURATION_REG, 0x04))
wiphy_err(priv->hw->wiphy,
"write CFG reg failed\n");
ret = -1;
if (i > MAX_WRITE_IOMEM_RETRY)
return ret;
}
} while (ret == -1);
return ret;
}
static int mwl_sdio_send_command(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
struct cmd_header *cmd_hdr = (struct cmd_header *)&priv->pcmd_buf[
INTF_CMDHEADER_LEN(INTF_HEADER_LEN)];
u32 buf_block_len;
u32 blk_size;
u16 len;
u32 pkt_len;
u32 port;
int rc;
__le16 *pbuf = (__le16 *)priv->pcmd_buf;
int status;
unsigned long flags;
/* Wait till the card informs CMD_DNLD_RDY interrupt except
* for get HW spec command */
if (cmd_hdr->command != cpu_to_le16(HOSTCMD_CMD_GET_HW_SPEC)) {
status = wait_event_timeout(card->cmd_wait_q.wait,
(card->int_status & DN_LD_CMD_PORT_HOST_INT_STATUS),
(12 * HZ));
if (status == 0) {
wiphy_err(priv->hw->wiphy, "CMD_DNLD failure\n");
priv->cmd_timeout = true;
return -1;
} else {
spin_lock_irqsave(&card->int_lock, flags);
card->int_status &= ~DN_LD_CMD_PORT_HOST_INT_STATUS;
spin_unlock_irqrestore(&card->int_lock, flags);
}
}
len = le16_to_cpu(cmd_hdr->len) +
INTF_CMDHEADER_LEN(INTF_HEADER_LEN)*sizeof(unsigned short);
port = CMD_PORT_SLCT;
blk_size = MWL_SDIO_BLOCK_SIZE;
buf_block_len = (len + blk_size - 1) / blk_size;
pkt_len = buf_block_len * blk_size;
card->cmd_resp_recvd = false;
card->cmd_id = (u16)(le16_to_cpu(cmd_hdr->command) & ~HOSTCMD_RESP_BIT);
pbuf[0] = cpu_to_le16(pkt_len);
pbuf[1] = cpu_to_le16(MWL_TYPE_CMD);
rc = mwl_write_data_to_card(priv, (u8 *)&priv->pcmd_buf[0],
pkt_len, (card->ioport + port));
if (rc < 0) {
//If command fails to write, reset the int status so next command can be processed.
spin_lock_irqsave(&card->int_lock, flags);
card->int_status |= DN_LD_CMD_PORT_HOST_INT_STATUS;
spin_unlock_irqrestore(&card->int_lock, flags);
card->dnld_cmd_failure++;
if (card->dnld_cmd_failure > MAX_DNLD_CMD_FAILURES) {
wiphy_err(priv->hw->wiphy, "CMD_DNLD threshold failure\n");
priv->cmd_timeout = true;
}
}
else {
card->dnld_cmd_failure = 0;
}
return rc;
}
static void mwl_sdio_cleanup(struct mwl_priv *priv)
{
int num;
struct mwl_sdio_card *card = priv->intf;
/* Disable Interrupt before tx/rx cleanup */
sdio_claim_host(card->func);
sdio_release_irq(card->func);
sdio_release_host(card->func);
wiphy_info(priv->hw->wiphy, "%s, unregister IRQ\n", __func__);
// mwl_sdio_cleanup is called both in driver shutdown and recovery scenarios
// Only kill tasklet (which just means remove from CPU queue if it exists there) in shutdown scenario
// Ensure tasklet is not both disabled and queued to avoid CPU hang
if (priv->shutdown) {
tasklet_enable(&priv->rx_task);
tasklet_kill(&priv->rx_task);
}
/* Free Tx bufs */
for (num = 0; num < SYSADPT_NUM_OF_DESC_DATA; num++) {
skb_queue_purge(&priv->txq[num]);
priv->fw_desc_cnt[num] = 0;
}
/* Free Rx bufs */
skb_queue_purge(&card->rx_data_q);
mwl_free_sdio_mpa_buffers(priv);
kfree(card->mpa_rx.skb_arr); card->mpa_rx.skb_arr = NULL;
kfree(card->mpa_rx.len_arr); card->mpa_rx.len_arr = NULL;
kfree(card->mp_regs); card->mp_regs = NULL;
sdio_claim_host(card->func);
sdio_disable_func(card->func);
sdio_release_host(card->func);
}
static bool mwl_sdio_check_card_status(struct mwl_priv *priv)
{
return true;
}
/*
* This function writes multiple data into SDIO card memory.
*
* This does not work in suspended mode.
*/
static int
mwl_write_data_sync(struct mwl_priv *priv,
u8 *buffer, u32 pkt_len, u32 port)
{
struct mwl_sdio_card *card = priv->intf;
int ret;
u8 blk_mode = (port & MWL_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
u32 blk_size = (blk_mode == BLOCK_MODE) ? MWL_SDIO_BLOCK_SIZE : 1;
u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (pkt_len / MWL_SDIO_BLOCK_SIZE) : pkt_len;
u32 ioport = (port & MWL_SDIO_IO_PORT_MASK);
if (card->is_suspended && !priv->recovery_in_progress) {
wiphy_err(priv->hw->wiphy,
"%s: not allowed while suspended\n", __func__);
return -1;
}
sdio_claim_host(card->func);
ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
sdio_release_host(card->func);
if (ret && ret != -ENOMEDIUM) {
wiphy_err(priv->hw->wiphy," %s : sdio_writesb failed,buffer ptr = 0x%p ioport = 0x%x,"
" size = %d, error = %d\n", __func__, buffer, ioport, blk_cnt * blk_size,ret);
}
return ret;
}
/*
* This function reads the firmware status.
*/
static int
mwl_sdio_read_fw_status(struct mwl_priv *priv, u16 *dat)
{
struct mwl_sdio_card *card = priv->intf;
const struct mwl_sdio_card_reg *reg = card->reg;
u8 fws0, fws1;
if (mwl_read_reg(priv, reg->status_reg_0, &fws0))
return -1;
if (mwl_read_reg(priv, reg->status_reg_1, &fws1))
return -1;
*dat = (u16) ((fws1 << 8) | fws0);
return 0;
}
/*
* This function checks the firmware status in card.
*
* The winner interface is also determined by this function.
*/
static int mwl_check_fw_status(struct mwl_priv *priv,
u32 poll_num)
{
int ret = 0;
u16 firmware_stat = 0;
u32 tries;
wiphy_info(priv->hw->wiphy,"Checking fw status %d\n", poll_num);
/* Wait for firmware initialization event */
for (tries = 1; tries <= poll_num; tries++) {
ret = mwl_sdio_read_fw_status(priv, &firmware_stat);
if (ret)
{
if (!(tries % 10) ) {
wiphy_err(priv->hw->wiphy,"fw read failed %d\n", ret);
}
continue;
}
if (firmware_stat == FIRMWARE_READY_SDIO) {
ret = 0;
wiphy_info(priv->hw->wiphy,
"firmware is ready %d\n", tries);
break;
} else {
if(!(tries % 10) ){
wiphy_info(priv->hw->wiphy,
"Waiting on fw status %d 0x%x\n", tries, firmware_stat);
}
msleep(100);
ret = -1;
}
}
return ret;
}
/*
* This function downloads the firmware to the card.
*
* Firmware is downloaded to the card in blocks. Every block download
* is tested for CRC errors, and retried a number of times before
* returning failure.
*/
static int mwl_sdio_program_firmware(struct mwl_priv *priv)
{
struct mwl_sdio_card *card = priv->intf;
const struct mwl_sdio_card_reg *reg = card->reg;
const struct firmware *fw;
u8 *fw_data;
u32 fw_len;
int ret;
u32 offset = 0;
u8 base0, base1;
u8 *fwbuf;
u16 len = 0;
u32 txlen, tx_bytes = 0, tries;
u32 i = 0;
u32 ioport = (card->ioport & MWL_SDIO_IO_PORT_MASK);
u16 firmware_status = 0;
fw = priv->fw_ucode;
fw_len = fw->size;
fw_data = (u8 *)fw->data;
if (!fw_len) {
wiphy_err(priv->hw->wiphy,
"Firmware image not found! Terminating download\n");
return -1;
}
/* Assume that the allocated buffer is 8-byte aligned */
fwbuf = kzalloc(MWL_UPLD_SIZE, GFP_KERNEL);
if (!fwbuf) {
wiphy_err(priv->hw->wiphy,"buffer allocation failed\n");
return -ENOMEM;
}
wiphy_info(priv->hw->wiphy,
"Downloading FW image (%d bytes)\n", fw_len);
mwl_sdio_enable_int(priv, false);
for (i = 0; i < 2; ++i) {
sdio_claim_host(card->func);
ret = mwl_sdio_read_fw_status(priv, &firmware_status);
sdio_release_host(card->func);
if (ret == 0 && firmware_status == FIRMWARE_READY_SDIO) {
wiphy_err(priv->hw->wiphy,
"Firmware already initialized! Resetting radio...\n");
if (priv->if_ops.down_dev)
priv->if_ops.down_dev(priv);
ret = mwl_sdio_reset(priv);
if (priv->if_ops.up_dev)
priv->if_ops.up_dev(priv);
if (ret) {
kfree(fwbuf);
return ret;
}
}
else
break;
}
if (i >= 2) {
kfree(fwbuf);
return -1;
}
sdio_claim_host(card->func);
/* Perform firmware data transfer */
i = 0;
for(;;) {
/* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
bits */
ret = mwl_sdio_poll_card_status(priv, CARD_IO_READY |
DN_LD_CARD_RDY);
if (ret) {
wiphy_err(priv->hw->wiphy,
"FW downloading \t"
"poll status timeout @ %d\n", offset);
goto err_dnld;
}
/* More data? */
if (offset >= fw_len)
break;
for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
ret = mwl_read_reg(priv, reg->base_0_reg,
&base0);
if (ret) {
wiphy_err(priv->hw->wiphy,
"dev BASE0 register read failed:\t"
"base0=%#04X(%d). Terminating dnld\n",
base0, base0);
goto err_dnld;
}
ret = mwl_read_reg(priv, reg->base_1_reg,
&base1);
if (ret) {
wiphy_err(priv->hw->wiphy,
"dev BASE1 register read failed:\t"
"base1=%#04X(%d). Terminating dnld\n",
base1, base1);
goto err_dnld;
}
len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
if (len)
break;
lrdmwl_delay(10);
}
if (!len) {
break;
} else if (len > MWL_UPLD_SIZE) {
wiphy_err(priv->hw->wiphy,
"FW dnld failed @ %d, invalid length %d\n",
offset, len);
ret = -1;
goto err_dnld;
}
txlen = len;
if (len & BIT(0)) {