aboutsummaryrefslogtreecommitdiffstats
path: root/libertas_uap/uap_sdio_mmc.c
blob: 86f55b569a3ffb610b2d37357c55169c65ff1747 (plain)
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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
/** @file uap_sdio_mmc.c
 *  @brief This file contains SDIO IF (interface) module
 *  related functions.
 *
 * Copyright (C) 2007-2009, Marvell International Ltd.
 *
 * 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 along with the File in the gpl.txt file or by writing to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 or on the worldwide web at http://www.gnu.org/licenses/gpl.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.
 *
 */
/****************************************************
Change log:
****************************************************/

#include	"uap_sdio_mmc.h"

#include <linux/firmware.h>

/** define SDIO block size */
/* We support up to 480-byte block size due to FW buffer limitation. */
#define SD_BLOCK_SIZE		256

/** define allocated buffer size */
#define ALLOC_BUF_SIZE		(((MAX(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, \
					MRVDRV_SIZE_OF_CMD_BUFFER) + INTF_HEADER_LEN \
					+ SD_BLOCK_SIZE - 1) / SD_BLOCK_SIZE) * SD_BLOCK_SIZE)

/** Max retry number of CMD53 write */
#define MAX_WRITE_IOMEM_RETRY	2

/********************************************************
		Local Variables
********************************************************/

/** SDIO Rx unit */
static u8 sdio_rx_unit = 0;

/**Interrupt status */
static u8 sd_ireg = 0;
/********************************************************
		Global Variables
********************************************************/
extern u8 *helper_name;
extern u8 *fw_name;
/** Default helper name */
#define DEFAULT_HELPER_NAME "mrvl/helper_sd.bin"
/** Default firmware name */
#define DEFAULT_FW_NAME "mrvl/sd8688_ap.bin"

/********************************************************
		Local Functions
********************************************************/
/**
 *  @brief This function reads the IO register.
 *
 *  @param priv    A pointer to uap_private structure
 *  @param reg	   register to be read
 *  @param dat	   A pointer to variable that keeps returned value
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
sbi_read_ioreg(uap_private * priv, u32 reg, u8 * dat)
{
    struct sdio_mmc_card *card;
    int ret = UAP_STATUS_FAILURE;

    ENTER();

    card = priv->uap_dev.card;
    if (!card || !card->func) {
        PRINTM(ERROR, "sbi_read_ioreg(): card or function is NULL!\n");
        goto done;
    }

    *dat = sdio_readb(card->func, reg, &ret);
    if (ret) {
        PRINTM(ERROR, "sbi_read_ioreg(): sdio_readb failed! ret=%d\n", ret);
        goto done;
    }

    PRINTM(INFO, "sbi_read_ioreg() priv=%p func=%d reg=%#x dat=%#x\n", priv,
           card->func->num, reg, *dat);

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function writes the IO register.
 *
 *  @param priv    A pointer to uap_private structure
 *  @param reg	   register to be written
 *  @param dat	   the value to be written
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
sbi_write_ioreg(uap_private * priv, u32 reg, u8 dat)
{
    struct sdio_mmc_card *card;
    int ret = UAP_STATUS_FAILURE;

    ENTER();

    card = priv->uap_dev.card;
    if (!card || !card->func) {
        PRINTM(ERROR, "sbi_write_ioreg(): card or function is NULL!\n");
        goto done;
    }

    PRINTM(INFO, "sbi_write_ioreg() priv=%p func=%d reg=%#x dat=%#x\n", priv,
           card->func->num, reg, dat);

    sdio_writeb(card->func, dat, reg, &ret);
    if (ret) {
        PRINTM(ERROR, "sbi_write_ioreg(): sdio_readb failed! ret=%d\n", ret);
        goto done;
    }

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function get rx_unit value
 *
 *  @param priv    A pointer to uap_private structure
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
sd_get_rx_unit(uap_private * priv)
{
    int ret = UAP_STATUS_SUCCESS;
    u8 reg;

    ENTER();

    ret = sbi_read_ioreg(priv, CARD_RX_UNIT_REG, &reg);
    if (ret == UAP_STATUS_SUCCESS)
        sdio_rx_unit = reg;

    LEAVE();
    return ret;
}

/**
 *  @brief This function reads rx length
 *
 *  @param priv    A pointer to uap_private structure
 *  @param dat	   A pointer to keep returned data
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
sd_read_rx_len(uap_private * priv, u16 * dat)
{
    int ret = UAP_STATUS_SUCCESS;
    u8 reg;

    ENTER();

    ret = sbi_read_ioreg(priv, CARD_RX_LEN_REG, &reg);
    if (ret == UAP_STATUS_SUCCESS)
        *dat = (u16) reg << sdio_rx_unit;

    LEAVE();
    return ret;
}

/**
 *  @brief This function reads fw status registers
 *
 *  @param priv    A pointer to uap_private structure
 *  @param dat	   A pointer to keep returned data
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
sd_read_firmware_status(uap_private * priv, u16 * dat)
{
    int ret = UAP_STATUS_SUCCESS;
    u8 fws0;
    u8 fws1;

    ENTER();

    ret = sbi_read_ioreg(priv, CARD_FW_STATUS0_REG, &fws0);
    if (ret < 0) {
        LEAVE();
        return UAP_STATUS_FAILURE;
    }

    ret = sbi_read_ioreg(priv, CARD_FW_STATUS1_REG, &fws1);
    if (ret < 0) {
        LEAVE();
        return UAP_STATUS_FAILURE;
    }

    *dat = (((u16) fws1) << 8) | fws0;

    LEAVE();
    return UAP_STATUS_SUCCESS;
}

/**
 *  @brief This function polls the card status register.
 *
 *  @param priv    	A pointer to uap_private structure
 *  @param bits    	the bit mask
 *  @return 	   	UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
mv_sdio_poll_card_status(uap_private * priv, u8 bits)
{
    int tries;
    u8 cs;

    ENTER();

    for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
        if (sbi_read_ioreg(priv, CARD_STATUS_REG, &cs) < 0)
            break;
        else if ((cs & bits) == bits) {
            LEAVE();
            return UAP_STATUS_SUCCESS;
        }
        udelay(10);
    }

    PRINTM(WARN, "mv_sdio_poll_card_status failed, tries = %d\n", tries);

    LEAVE();
    return UAP_STATUS_FAILURE;
}

/**
 *  @brief This function set the sdio bus width.
 *
 *  @param priv    	A pointer to uap_private structure
 *  @param mode    	1--1 bit mode, 4--4 bit mode
 *  @return 	   	UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
#if 0
static int
sdio_set_bus_width(uap_private * priv, u8 mode)
{
    ENTER();
    LEAVE();
    return UAP_STATUS_SUCCESS;
}
#endif

/**
 *  @brief This function reads data from the card.
 *
 *  @param priv    	A pointer to uap_private structure
 *  @return 	   	UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
sd_card_to_host(uap_private * priv)
{
    int ret = UAP_STATUS_SUCCESS;
    u16 buf_len = 0;
    int buf_block_len;
    int blksz;
    struct sk_buff *skb = NULL;
    u16 type;
    u8 *payload = NULL;
    struct sdio_mmc_card *card = priv->uap_dev.card;

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "card or function is NULL!\n");
        ret = UAP_STATUS_FAILURE;
        goto exit;
    }

    /* Read the length of data to be transferred */
    ret = sd_read_rx_len(priv, &buf_len);
    if (ret < 0) {
        PRINTM(ERROR, "card_to_host, read scratch reg failed\n");
        ret = UAP_STATUS_FAILURE;
        goto exit;
    }

    /* Allocate buffer */
    blksz = SD_BLOCK_SIZE;
    buf_block_len = (buf_len + blksz - 1) / blksz;
    if (buf_len <= INTF_HEADER_LEN || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
        PRINTM(ERROR, "card_to_host, invalid packet length: %d\n", buf_len);
        ret = UAP_STATUS_FAILURE;
        goto exit;
    }
#ifdef PXA3XX_DMA_ALIGN
    skb = dev_alloc_skb(buf_block_len * blksz + PXA3XX_DMA_ALIGNMENT);
#else
    skb = dev_alloc_skb(buf_block_len * blksz);
#endif
    if (skb == NULL) {
        PRINTM(WARN, "No free skb\n");
        goto exit;
    }
#ifdef PXA3XX_DMA_ALIGN
    if ((u32) skb->data & (PXA3XX_DMA_ALIGNMENT - 1)) {
        skb_put(skb, (u32) skb->data & (PXA3XX_DMA_ALIGNMENT - 1));
        skb_pull(skb, (u32) skb->data & (PXA3XX_DMA_ALIGNMENT - 1));
    }
#endif /* PXA3XX_DMA_ALIGN */

    payload = skb->tail;
    ret = sdio_readsb(card->func, payload, priv->uap_dev.ioport,
                      buf_block_len * blksz);
    if (ret < 0) {
        PRINTM(ERROR, "card_to_host, read iomem failed: %d\n", ret);
        ret = UAP_STATUS_FAILURE;
        goto exit;
    }
    HEXDUMP("SDIO Blk Rd", payload, blksz * buf_block_len);
    /*
     * This is SDIO specific header
     *  u16 length,
     *  u16 type (MV_TYPE_DAT = 0, MV_TYPE_CMD = 1, MV_TYPE_EVENT = 3)
     */
    buf_len = uap_le16_to_cpu(*(u16 *) & payload[0]);
    type = uap_le16_to_cpu(*(u16 *) & payload[2]);
    switch (type) {
    case MV_TYPE_EVENT:
        skb_put(skb, buf_len);
        skb_pull(skb, INTF_HEADER_LEN);
        uap_process_event(priv, skb->data, skb->len);
        kfree_skb(skb);
        skb = NULL;
        break;
    case MV_TYPE_CMD:
        skb_put(skb, buf_len);
        skb_pull(skb, INTF_HEADER_LEN);
        priv->adapter->cmd_pending = FALSE;
        if (priv->adapter->cmd_wait_option ==
            HostCmd_OPTION_WAITFORRSP_SLEEPCONFIRM) {
            priv->adapter->cmd_wait_option = FALSE;
            uap_process_sleep_confirm_resp(priv, skb->data, skb->len);
        } else if (priv->adapter->cmd_wait_option) {
            memcpy(priv->adapter->CmdBuf, skb->data, skb->len);
            priv->adapter->CmdSize = skb->len;
            priv->adapter->cmd_wait_option = FALSE;
            priv->adapter->CmdWaitQWoken = TRUE;
            wake_up_interruptible(&priv->adapter->cmdwait_q);
        }
        kfree_skb(skb);
        skb = NULL;
        break;
    case MV_TYPE_DAT:
        skb_put(skb, buf_len);
        skb_pull(skb, INTF_HEADER_LEN);
        uap_process_rx_packet(priv, skb);
        break;
    default:
        priv->stats.rx_errors++;
        priv->stats.rx_dropped++;
        /* Driver specified event and command resp should be handle here */
        PRINTM(INFO, "Unknown PKT type:%d\n", type);
        kfree_skb(skb);
        skb = NULL;
        break;
    }
  exit:
    if (ret) {
        if (skb)
            kfree_skb(skb);
    }

    LEAVE();
    return ret;
}

/**
 *  @brief This function enables the host interrupts mask
 *
 *  @param priv    A pointer to uap_private structure
 *  @param mask	   the interrupt mask
 *  @return 	   UAP_STATUS_SUCCESS
 */
static int
enable_host_int_mask(uap_private * priv, u8 mask)
{
    int ret = UAP_STATUS_SUCCESS;

    ENTER();

    /* Simply write the mask to the register */
    ret = sbi_write_ioreg(priv, HOST_INT_MASK_REG, mask);

    if (ret) {
        PRINTM(WARN, "Unable to enable the host interrupt!\n");
        ret = UAP_STATUS_FAILURE;
    }

    LEAVE();
    return ret;
}

/**  @brief This function disables the host interrupts mask.
 *
 *  @param priv    A pointer to uap_private structure
 *  @param mask	   the interrupt mask
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
disable_host_int_mask(uap_private * priv, u8 mask)
{
    int ret = UAP_STATUS_SUCCESS;
    u8 host_int_mask;

    ENTER();

    /* Read back the host_int_mask register */
    ret = sbi_read_ioreg(priv, HOST_INT_MASK_REG, &host_int_mask);
    if (ret) {
        ret = UAP_STATUS_FAILURE;
        goto done;
    }

    /* Update with the mask and write back to the register */
    host_int_mask &= ~mask;
    ret = sbi_write_ioreg(priv, HOST_INT_MASK_REG, host_int_mask);
    if (ret < 0) {
        PRINTM(WARN, "Unable to diable the host interrupt!\n");
        ret = UAP_STATUS_FAILURE;
        goto done;
    }

  done:
    LEAVE();
    return ret;
}

/********************************************************
		Global Functions
********************************************************/

/**
 *  @brief This function handles the interrupt.
 *
 *  @param func	   A pointer to sdio_func structure.
 *  @return 	   n/a
 */
static void
sbi_interrupt(struct sdio_func *func)
{
    struct sdio_mmc_card *card;
    uap_private *priv;
    u8 ireg = 0;
    int ret = UAP_STATUS_SUCCESS;

    ENTER();

    card = sdio_get_drvdata(func);
    if (!card || !card->priv) {
        PRINTM(MSG, "%s: sbi_interrupt(%p) card or priv is NULL, card=%p\n",
               __FUNCTION__, func, card);
        LEAVE();
        return;
    }
    priv = card->priv;
#ifdef FW_WAKEUP_TIME
    if ((priv->adapter->wt_pwrup_sending != 0L) &&
        (priv->adapter->wt_int == 0L))
        priv->adapter->wt_int = get_utimeofday();
#endif

    ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret);
    if (ret) {
        PRINTM(WARN, "sdio_read_ioreg: read int status register failed\n");
        goto done;
    }
    if (ireg != 0) {
        /*
         * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
         * Clear the interrupt status register and re-enable the interrupt
         */
        PRINTM(INFO, "sdio_ireg = 0x%x\n", ireg);
        sdio_writeb(card->func,
                    ~(ireg) & (DN_LD_HOST_INT_STATUS | UP_LD_HOST_INT_STATUS),
                    HOST_INTSTATUS_REG, &ret);
        if (ret) {
            PRINTM(WARN,
                   "sdio_write_ioreg: clear int status register failed\n");
            goto done;
        }
    }
    OS_INT_DISABLE;
    sd_ireg |= ireg;
    OS_INT_RESTORE;

    uap_interrupt(priv);
  done:
    LEAVE();
}

/**
 *  @brief This function probe the card
 *
 *  @param func    A pointer to sdio_func structure
 *  @param id	   A pointer to structure sd_device_id
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
static int
uap_probe(struct sdio_func *func, const struct sdio_device_id *id)
{
    int ret = UAP_STATUS_FAILURE;
    struct sdio_mmc_card *card = NULL;

    ENTER();

    PRINTM(MSG, "%s: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
           __FUNCTION__, func->vendor, func->device, func->class, func->num);

    card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
    if (!card) {
        ret = -ENOMEM;
        goto done;
    }

    card->func = func;

    if (!uap_add_card(card)) {
        PRINTM(ERROR, "%s: uap_add_callback failed\n", __FUNCTION__);
        kfree(card);
        ret = UAP_STATUS_FAILURE;
        goto done;
    }

    ret = UAP_STATUS_SUCCESS;

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function removes the card
 *
 *  @param func    A pointer to sdio_func structure
 *  @return        N/A
 */
static void
uap_remove(struct sdio_func *func)
{
    struct sdio_mmc_card *card;

    ENTER();

    if (func) {
        card = sdio_get_drvdata(func);
        if (card) {
            uap_remove_card(card);
            kfree(card);
        }
    }

    LEAVE();
}

#ifdef CONFIG_PM
/**
 *  @brief This function handles client driver suspend
 *
 *  @param func    A pointer to sdio_func structure
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
uap_suspend(struct sdio_func *func)
{
    ENTER();
    LEAVE();
    return 0;
}

/**
 *  @brief This function handles client driver resume
 *
 *  @param func    A pointer to sdio_func structure
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
uap_resume(struct sdio_func *func)
{
    ENTER();
    LEAVE();
    return 0;
}
#endif

/** Device ID for SD8688 */
#define  SD_DEVICE_ID_8688_UAP 0x9104
/** UAP IDs */
static const struct sdio_device_id uap_ids[] = {
    {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SD_DEVICE_ID_8688_UAP)},
    {},
};

MODULE_DEVICE_TABLE(sdio, uap_ids);

static struct sdio_driver uap_sdio = {
    .name = "uap_sdio",
    .id_table = uap_ids,
    .probe = uap_probe,
    .remove = uap_remove,
#ifdef CONFIG_PM
/*    .suspend	= uap_suspend, */
/*    .resume	= uap_resume, */
#endif

};

/**
 *  @brief This function registers the IF module in bus driver.
 *
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int __init
sbi_register()
{
    int ret = UAP_STATUS_SUCCESS;

    ENTER();

    /* SDIO Driver Registration */
    if (sdio_register_driver(&uap_sdio) != 0) {
        PRINTM(FATAL, "SDIO Driver Registration Failed \n");
        ret = UAP_STATUS_FAILURE;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief This function de-registers the IF module in bus driver.
 *
 *  @return 	   n/a
 */
void __exit
sbi_unregister(void)
{
    ENTER();

    /* SDIO Driver Unregistration */
    sdio_unregister_driver(&uap_sdio);

    LEAVE();
}

/**
 *  @brief This function checks the interrupt status and handle it accordingly.
 *
 *  @param priv    A pointer to uap_private structure
 *  @param ireg    A pointer to variable that keeps returned value
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_get_int_status(uap_private * priv, u8 * ireg)
{
    int ret = UAP_STATUS_SUCCESS;
    u8 sdio_ireg = 0;
    struct sdio_mmc_card *card = priv->uap_dev.card;

    ENTER();

    *ireg = 0;
    OS_INT_DISABLE;
    sdio_ireg = sd_ireg;
    sd_ireg = 0;
    OS_INT_RESTORE;

    sdio_claim_host(card->func);

    if (sdio_ireg & DN_LD_HOST_INT_STATUS) {    /* tx_done INT */
        if (!priv->uap_dev.cmd_sent) {  /* tx_done already received */
            PRINTM(INFO,
                   "warning: tx_done already received: tx_dnld_rdy=0x%x int status=0x%x\n",
                   priv->uap_dev.cmd_sent, sdio_ireg);
        } else {
            priv->uap_dev.cmd_sent = FALSE;
            priv->uap_dev.data_sent = FALSE;
            if ( (priv->uap_dev.netdev->reg_state == NETREG_REGISTERED) && (skb_queue_len(&priv->adapter->tx_queue) < TX_LOW_WATERMARK)) {
                os_start_queue(priv);
	    }
        }
    }
    if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
        sd_card_to_host(priv);
    }

    *ireg = sdio_ireg;
    ret = UAP_STATUS_SUCCESS;
    sdio_release_host(card->func);

    LEAVE();
    return ret;
}

/**
 *  @brief This function disables the host interrupts.
 *
 *  @param priv    A pointer to uap_private structure
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_disable_host_int(uap_private * priv)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;
    int ret;

    ENTER();

    sdio_claim_host(card->func);
    ret = disable_host_int_mask(priv, HIM_DISABLE);
    sdio_release_host(card->func);

    LEAVE();
    return ret;
}

/**
 *  @brief This function enables the host interrupts.
 *
 *  @param priv    A pointer to uap_private structure
 *  @return 	   UAP_STATUS_SUCCESS
 */
int
sbi_enable_host_int(uap_private * priv)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;
    int ret;

    ENTER();

    sdio_claim_host(card->func);
    ret = enable_host_int_mask(priv, HIM_ENABLE);
    sdio_release_host(card->func);

    LEAVE();
    return ret;
}

/**
 *  @brief This function de-registers the device.
 *
 *  @param priv    A pointer to uap_private structure
 *  @return 	   UAP_STATUS_SUCCESS
 */
int
sbi_unregister_dev(uap_private * priv)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "Error: card or function is NULL!\n");
        goto done;
    }

    sdio_claim_host(card->func);
    sdio_release_irq(card->func);
    sdio_disable_func(card->func);
    sdio_release_host(card->func);

    sdio_set_drvdata(card->func, NULL);

  done:
    LEAVE();
    return UAP_STATUS_SUCCESS;
}

/**
 *  @brief This function registers the device.
 *
 *  @param priv    A pointer to uap_private structure
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_register_dev(uap_private * priv)
{
    int ret = UAP_STATUS_FAILURE;
    u8 reg;
    struct sdio_mmc_card *card = priv->uap_dev.card;
    struct sdio_func *func;

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "Error: card or function is NULL!\n");
        goto done;
    }

    func = card->func;

    /* Initialize the private structure */
    priv->uap_dev.ioport = 0;

    sdio_claim_host(func);

    ret = sdio_enable_func(func);
    if (ret) {
        PRINTM(FATAL, "sdio_enable_func() failed: ret=%d\n", ret);
        goto release_host;
    }

    ret = sdio_claim_irq(func, sbi_interrupt);
    if (ret) {
        PRINTM(FATAL, "sdio_claim_irq failed: ret=%d\n", ret);
        goto disable_func;
    }

    /* Read the IO port */
    ret = sbi_read_ioreg(priv, IO_PORT_0_REG, &reg);
    if (ret)
        goto release_irq;
    else
        priv->uap_dev.ioport |= reg;

    ret = sbi_read_ioreg(priv, IO_PORT_1_REG, &reg);
    if (ret)
        goto release_irq;
    else
        priv->uap_dev.ioport |= (reg << 8);

    ret = sbi_read_ioreg(priv, IO_PORT_2_REG, &reg);
    if (ret)
        goto release_irq;
    else
        priv->uap_dev.ioport |= (reg << 16);

    PRINTM(INFO, "SDIO FUNC #%d IO port: 0x%x\n", func->num,
           priv->uap_dev.ioport);

    ret = sdio_set_block_size(card->func, SD_BLOCK_SIZE);
    if (ret) {
        PRINTM(ERROR, "%s: cannot set SDIO block size\n", __FUNCTION__);
        ret = UAP_STATUS_FAILURE;
        goto release_irq;
    }
    priv->hotplug_device = &func->dev;

    if (helper_name == NULL) {
        helper_name = DEFAULT_HELPER_NAME;
    }
    if (fw_name == NULL) {
        fw_name = DEFAULT_FW_NAME;
    }
    sdio_release_host(func);

    sdio_set_drvdata(func, card);

    ret = UAP_STATUS_SUCCESS;
    goto done;

  release_irq:
    sdio_release_irq(func);
  disable_func:
    sdio_disable_func(func);
  release_host:
    sdio_release_host(func);

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function sends data to the card.
 *
 *  @param priv    A pointer to uap_private structure
 *  @param payload A pointer to the data/cmd buffer
 *  @param nb	   the length of data/cmd
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_host_to_card(uap_private * priv, u8 * payload, u16 nb)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;
    int ret = UAP_STATUS_SUCCESS;
    int buf_block_len;
    int blksz;
    int i = 0;
    u8 *buf = NULL;
#ifdef PXA3XX_DMA_ALIGN
    void *tmpbuf = NULL;
    int tmpbufsz;
#endif

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "card or function is NULL!\n");
        LEAVE();
        return UAP_STATUS_FAILURE;
    }
    buf = payload;
#ifdef PXA3XX_DMA_ALIGN
    if ((u32) payload & (PXA3XX_DMA_ALIGNMENT - 1)) {
        tmpbufsz = ALIGN_SZ(nb, PXA3XX_DMA_ALIGNMENT);
        tmpbuf = kmalloc(tmpbufsz, GFP_KERNEL);
        memset(tmpbuf, 0, tmpbufsz);
        /* Ensure 8-byte aligned CMD buffer */
        buf = (u8 *) ALIGN_ADDR(tmpbuf, PXA3XX_DMA_ALIGNMENT);
        memcpy(buf, payload, nb);
    }
#endif
    /* Allocate buffer and copy payload */
    blksz = SD_BLOCK_SIZE;
    buf_block_len = (nb + blksz - 1) / blksz;
    sdio_claim_host(card->func);
#define MAX_WRITE_IOMEM_RETRY	2
    priv->uap_dev.cmd_sent = TRUE;
    priv->uap_dev.data_sent = TRUE;
    do {
        /* Transfer data to card */
        ret = sdio_writesb(card->func, priv->uap_dev.ioport, buf,
                           buf_block_len * blksz);
        if (ret < 0) {
            i++;
            PRINTM(ERROR, "host_to_card, write iomem (%d) failed: %d\n", i,
                   ret);
            ret = UAP_STATUS_FAILURE;
            if (i > MAX_WRITE_IOMEM_RETRY)
                goto exit;
        } else {
            HEXDUMP("SDIO Blk Wr", payload, nb);
        }
    } while (ret == UAP_STATUS_FAILURE);
  exit:
    sdio_release_host(card->func);
#ifdef PXA3XX_DMA_ALIGN
    if (tmpbuf)
        kfree(tmpbuf);
#endif
    if (ret == UAP_STATUS_FAILURE) {
        priv->uap_dev.cmd_sent = FALSE;
        priv->uap_dev.data_sent = FALSE;
    }
    LEAVE();
    return ret;
}

/**
 *  @brief This function reads CIS information.
 *
 *  @param priv    A pointer to uap_private structure
 *  @param cisinfo A pointer to CIS information output buffer
 *  @param cislen  A pointer to length of CIS info output buffer
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
#if 0
static int
sbi_get_cis_info(uap_private * priv, void *cisinfo, int *cislen)
{
#define CIS_PTR (0x8000)
    struct sdio_mmc_card *card = priv->uap_dev.card;
    unsigned int i, cis_ptr = CIS_PTR;
    int ret = UAP_STATUS_FAILURE;

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "sbi_get_cis_info(): card or function is NULL!\n");
        goto exit;
    }
#define MAX_SDIO_CIS_INFO_LEN (256)
    if (!cisinfo || (*cislen < MAX_SDIO_CIS_INFO_LEN)) {
        PRINTM(WARN, "ERROR! get_cis_info: insufficient buffer passed\n");
        goto exit;
    }

    *cislen = MAX_SDIO_CIS_INFO_LEN;

    sdio_claim_host(card->func);

    PRINTM(INFO, "cis_ptr=%#x\n", cis_ptr);

    /* Read the Tuple Data */
    for (i = 0; i < *cislen; i++) {
        ((unsigned char *) cisinfo)[i] =
            sdio_readb(card->func, cis_ptr + i, &ret);
        if (ret) {
            PRINTM(WARN, "get_cis_info error: ret=%d\n", ret);
            ret = UAP_STATUS_FAILURE;
            goto done;
        }
        PRINTM(INFO, "cisinfo[%d]=%#x\n", i, ((unsigned char *) cisinfo)[i]);
    }

  done:
    sdio_release_host(card->func);
  exit:
    LEAVE();
    return ret;
}
#endif
/**
 *  @brief This function downloads helper image to the card.
 *
 *  @param priv    	A pointer to uap_private structure
 *  @return 	   	UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_prog_helper(uap_private * priv)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;
    u8 *helper = NULL;
    int helperlen;
    int ret = UAP_STATUS_SUCCESS;
    void *tmphlprbuf = NULL;
    int tmphlprbufsz;
    u8 *hlprbuf;
    int hlprblknow;
    u32 tx_len;
#ifdef FW_DOWNLOAD_SPEED
    u32 tv1, tv2;
#endif

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "sbi_prog_helper(): card or function is NULL!\n");
        goto done;
    }

    if (priv->fw_helper) {
        helper = (u8 *) priv->fw_helper->data;
        helperlen = priv->fw_helper->size;
    } else {
        PRINTM(MSG, "No helper image found! Terminating download.\n");
        LEAVE();
        return UAP_STATUS_FAILURE;
    }

    PRINTM(INFO, "Downloading helper image (%d bytes), block size %d bytes\n",
           helperlen, SD_BLOCK_SIZE);

#ifdef FW_DOWNLOAD_SPEED
    tv1 = get_utimeofday();
#endif

#ifdef PXA3XX_DMA_ALIGN
    tmphlprbufsz = ALIGN_SZ(UAP_UPLD_SIZE, PXA3XX_DMA_ALIGNMENT);
#else /* !PXA3XX_DMA_ALIGN */
    tmphlprbufsz = UAP_UPLD_SIZE;
#endif /* !PXA3XX_DMA_ALIGN */
    tmphlprbuf = kmalloc(tmphlprbufsz, GFP_KERNEL);
    if (!tmphlprbuf) {
        PRINTM(ERROR,
               "Unable to allocate buffer for helper. Terminating download\n");
        ret = UAP_STATUS_FAILURE;
        goto done;
    }
    memset(tmphlprbuf, 0, tmphlprbufsz);
#ifdef PXA3XX_DMA_ALIGN
    hlprbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, PXA3XX_DMA_ALIGNMENT);
#else /* !PXA3XX_DMA_ALIGN */
    hlprbuf = (u8 *) tmphlprbuf;
#endif /* !PXA3XX_DMA_ALIGN */

    sdio_claim_host(card->func);

    /* Perform helper data transfer */
    tx_len = (FIRMWARE_TRANSFER_NBLOCK * SD_BLOCK_SIZE) - INTF_HEADER_LEN;
    hlprblknow = 0;
    do {
        /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY bits */
        ret = mv_sdio_poll_card_status(priv, CARD_IO_READY | DN_LD_CARD_RDY);
        if (ret < 0) {
            PRINTM(FATAL, "Helper download poll status timeout @ %d\n",
                   hlprblknow);
            goto done;
        }

        /* More data? */
        if (hlprblknow >= helperlen)
            break;

        /* Set blocksize to transfer - checking for last block */
        if (helperlen - hlprblknow < tx_len)
            tx_len = helperlen - hlprblknow;

        /* Set length to the 4-byte header */
        *(u32 *) hlprbuf = uap_cpu_to_le32(tx_len);

        /* Copy payload to buffer */
        memcpy(&hlprbuf[INTF_HEADER_LEN], &helper[hlprblknow], tx_len);

        PRINTM(INFO, ".");

        /* Send data */
        ret = sdio_writesb(card->func, priv->uap_dev.ioport,
                           hlprbuf, FIRMWARE_TRANSFER_NBLOCK * SD_BLOCK_SIZE);

        if (ret < 0) {
            PRINTM(FATAL, "IO error during helper download @ %d\n", hlprblknow);
            goto done;
        }

        hlprblknow += tx_len;
    } while (TRUE);

#ifdef FW_DOWNLOAD_SPEED
    tv2 = get_utimeofday();
    PRINTM(INFO, "helper: %ld.%03ld.%03ld ", tv1 / 1000000,
           (tv1 % 1000000) / 1000, tv1 % 1000);
    PRINTM(INFO, " -> %ld.%03ld.%03ld ", tv2 / 1000000, (tv2 % 1000000) / 1000,
           tv2 % 1000);
    tv2 -= tv1;
    PRINTM(INFO, " == %ld.%03ld.%03ld\n", tv2 / 1000000, (tv2 % 1000000) / 1000,
           tv2 % 1000);
#endif

    /* Write last EOF data */
    PRINTM(INFO, "\nTransferring helper image EOF block\n");
    memset(hlprbuf, 0x0, SD_BLOCK_SIZE);
    ret = sdio_writesb(card->func, priv->uap_dev.ioport,
                       hlprbuf, SD_BLOCK_SIZE);

    if (ret < 0) {
        PRINTM(FATAL, "IO error in writing helper image EOF block\n");
        goto done;
    }

    ret = UAP_STATUS_SUCCESS;

  done:
    sdio_release_host(card->func);
    if (tmphlprbuf)
        kfree(tmphlprbuf);

    LEAVE();
    return ret;
}

/**
 *  @brief This function downloads firmware image to the card.
 *
 *  @param priv    	A pointer to uap_private structure
 *  @return 	   	UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_prog_fw_w_helper(uap_private * priv)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;
    u8 *firmware = NULL;
    int firmwarelen;
    u8 base0;
    u8 base1;
    int ret = UAP_STATUS_SUCCESS;
    int offset;
    void *tmpfwbuf = NULL;
    int tmpfwbufsz;
    u8 *fwbuf;
    u16 len;
    int txlen = 0;
    int tx_blocks = 0;
    int i = 0;
    int tries = 0;
#ifdef FW_DOWNLOAD_SPEED
    u32 tv1, tv2;
#endif

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "sbi_prog_fw_w_helper(): card or function is NULL!\n");
        goto done;
    }

    if (priv->firmware) {
        firmware = (u8 *) priv->firmware->data;
        firmwarelen = priv->firmware->size;
    } else {
        PRINTM(MSG, "No firmware image found! Terminating download.\n");
        LEAVE();
        return UAP_STATUS_FAILURE;
    }

    PRINTM(INFO, "Downloading FW image (%d bytes)\n", firmwarelen);

#ifdef FW_DOWNLOAD_SPEED
    tv1 = get_utimeofday();
#endif

#ifdef PXA3XX_DMA_ALIGN
    tmpfwbufsz = ALIGN_SZ(UAP_UPLD_SIZE, PXA3XX_DMA_ALIGNMENT);
#else /* PXA3XX_DMA_ALIGN */
    tmpfwbufsz = UAP_UPLD_SIZE;
#endif /* PXA3XX_DMA_ALIGN */
    tmpfwbuf = kmalloc(tmpfwbufsz, GFP_KERNEL);
    if (!tmpfwbuf) {
        PRINTM(ERROR,
               "Unable to allocate buffer for firmware. Terminating download.\n");
        ret = UAP_STATUS_FAILURE;
        goto done;
    }
    memset(tmpfwbuf, 0, tmpfwbufsz);
#ifdef PXA3XX_DMA_ALIGN
    /* Ensure 8-byte aligned firmware buffer */
    fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, PXA3XX_DMA_ALIGNMENT);
#else /* PXA3XX_DMA_ALIGN */
    fwbuf = (u8 *) tmpfwbuf;
#endif /* PXA3XX_DMA_ALIGN */

    sdio_claim_host(card->func);

    /* Perform firmware data transfer */
    offset = 0;
    do {
        /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY bits */
        ret = mv_sdio_poll_card_status(priv, CARD_IO_READY | DN_LD_CARD_RDY);
        if (ret < 0) {
            PRINTM(FATAL, "FW download with helper poll status timeout @ %d\n",
                   offset);
            goto done;
        }

        /* More data? */
        if (offset >= firmwarelen)
            break;

        for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
            if ((ret = sbi_read_ioreg(priv, HOST_F1_RD_BASE_0, &base0)) < 0) {
                PRINTM(WARN, "Dev BASE0 register read failed:"
                       " base0=0x%04X(%d). Terminating download.\n", base0,
                       base0);
                ret = UAP_STATUS_FAILURE;
                goto done;
            }
            if ((ret = sbi_read_ioreg(priv, HOST_F1_RD_BASE_1, &base1)) < 0) {
                PRINTM(WARN, "Dev BASE1 register read failed:"
                       " base1=0x%04X(%d). Terminating download.\n", base1,
                       base1);
                ret = UAP_STATUS_FAILURE;
                goto done;
            }
            len = (((u16) base1) << 8) | base0;

            /* For SD8688 wait until the length is not 0, 1 or 2 before
               downloading the first FW block, since BOOT code writes the
               register to indicate the helper/FW download winner, the value
               could be 1 or 2 (Func1 or Func2). */
            if ((len && offset) || (len > 2))
                break;
            udelay(10);
        }

        if (len == 0)
            break;
        else if (len > UAP_UPLD_SIZE) {
            PRINTM(FATAL, "FW download failure @ %d, invalid length %d\n",
                   offset, len);
            ret = UAP_STATUS_FAILURE;
            goto done;
        }

        txlen = len;

        if (len & BIT(0)) {
            i++;
            if (i > MAX_WRITE_IOMEM_RETRY) {
                PRINTM(FATAL,
                       "FW download failure @ %d, over max retry count\n",
                       offset);
                ret = UAP_STATUS_FAILURE;
                goto done;
            }
            PRINTM(ERROR, "FW CRC error indicated by the helper:"
                   " len = 0x%04X, txlen = %d\n", len, txlen);
            len &= ~BIT(0);
            /* Setting this to 0 to resend from same offset */
            txlen = 0;
        } else {
            i = 0;

            /* Set blocksize to transfer - checking for last block */
            if (firmwarelen - offset < txlen) {
                txlen = firmwarelen - offset;
            }
            PRINTM(INFO, ".");

            tx_blocks = (txlen + SD_BLOCK_SIZE - 1) / SD_BLOCK_SIZE;

            /* Copy payload to buffer */
            memcpy(fwbuf, &firmware[offset], txlen);
        }

        /* Send data */
        ret = sdio_writesb(card->func, priv->uap_dev.ioport,
                           fwbuf, tx_blocks * SD_BLOCK_SIZE);

        if (ret < 0) {
            PRINTM(ERROR, "FW download, write iomem (%d) failed @ %d\n", i,
                   offset);
            if (sbi_write_ioreg(priv, CONFIGURATION_REG, 0x04) < 0) {
                PRINTM(ERROR, "write ioreg failed (CFG)\n");
            }
        }

        offset += txlen;
    } while (TRUE);

    PRINTM(INFO, "\nFW download over, size %d bytes\n", offset);

    ret = UAP_STATUS_SUCCESS;
  done:
#ifdef FW_DOWNLOAD_SPEED
    tv2 = get_utimeofday();
    PRINTM(INFO, "FW: %ld.%03ld.%03ld ", tv1 / 1000000,
           (tv1 % 1000000) / 1000, tv1 % 1000);
    PRINTM(INFO, " -> %ld.%03ld.%03ld ", tv2 / 1000000,
           (tv2 % 1000000) / 1000, tv2 % 1000);
    tv2 -= tv1;
    PRINTM(INFO, " == %ld.%03ld.%03ld\n", tv2 / 1000000,
           (tv2 % 1000000) / 1000, tv2 % 1000);
#endif
    sdio_release_host(card->func);
    if (tmpfwbuf)
        kfree(tmpfwbuf);

    LEAVE();
    return ret;
}

/**
 *  @brief This function checks if the firmware is ready to accept
 *  command or not.
 *
 *  @param priv    A pointer to uap_private structure
 *  @param pollnum Poll number
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_check_fw_status(uap_private * priv, int pollnum)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;
    int ret = UAP_STATUS_SUCCESS;
    u16 firmwarestat;
    int tries;

    ENTER();

    sdio_claim_host(card->func);

    /* Wait for firmware initialization event */
    for (tries = 0; tries < pollnum; tries++) {
        if ((ret = sd_read_firmware_status(priv, &firmwarestat)) < 0)
            continue;
        if (firmwarestat == FIRMWARE_READY) {
            ret = UAP_STATUS_SUCCESS;
            break;
        } else {
            mdelay(10);
            ret = UAP_STATUS_FAILURE;
        }
    }

    if (ret < 0)
        goto done;

    ret = UAP_STATUS_SUCCESS;
    sd_get_rx_unit(priv);

  done:
    sdio_release_host(card->func);

    LEAVE();
    return ret;
}

/**
 *  @brief This function set bus clock on/off
 *
 *  @param priv    A pointer to uap_private structure
 *  @param option    TRUE--on , FALSE--off
 *  @return 	   UAP_STATUS_SUCCESS
 */
#if 0
static int
sbi_set_bus_clock(uap_private * priv, u8 option)
{
    ENTER();
    LEAVE();
    return UAP_STATUS_SUCCESS;
}
#endif

/**
 *  @brief This function wakeup firmware
 *
 *  @param priv    A pointer to uap_private structure
 *  @return 	   UAP_STATUS_SUCCESS or UAP_STATUS_FAILURE
 */
int
sbi_wakeup_firmware(uap_private * priv)
{
    struct sdio_mmc_card *card = priv->uap_dev.card;
    int ret = UAP_STATUS_SUCCESS;

    ENTER();

    if (!card || !card->func) {
        PRINTM(ERROR, "card or function is NULL!\n");
        LEAVE();
        return UAP_STATUS_FAILURE;
    }
    sdio_claim_host(card->func);
    sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
    sdio_release_host(card->func);
    LEAVE();
    return ret;
}