tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / net / wireless / bcmdhd / bcmspibrcm.c
1 /*
2  * Broadcom BCMSDH to gSPI Protocol Conversion Layer
3  *
4  * Copyright (C) 1999-2015, Broadcom Corporation
5  * 
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  * 
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  * 
20  *      Notwithstanding the above, under no circumstances may you combine this
21  * software in any way with any other Broadcom software provided under a license
22  * other than the GPL, without Broadcom's express prior written consent.
23  *
24  * $Id: bcmspibrcm.c 373331 2012-12-07 04:46:22Z $
25  */
26
27 #define HSMODE
28
29 #include <typedefs.h>
30
31 #include <bcmdevs.h>
32 #include <bcmendian.h>
33 #include <bcmutils.h>
34 #include <osl.h>
35 #include <hndsoc.h>
36 #include <siutils.h>
37 #include <sbchipc.h>
38 #include <sbsdio.h>     /* SDIO device core hardware definitions. */
39 #include <spid.h>
40
41 #include <bcmsdbus.h>   /* bcmsdh to/from specific controller APIs */
42 #include <sdiovar.h>    /* ioctl/iovars */
43 #include <sdio.h>       /* SDIO Device and Protocol Specs */
44
45 #include <pcicfg.h>
46
47
48 #include <bcmspibrcm.h>
49 #ifdef BCMSPI_ANDROID
50 extern void spi_sendrecv(sdioh_info_t *sd, uint8 *msg_out, uint8 *msg_in, int msglen);
51 #else
52 #include <bcmspi.h>
53 #endif /* BCMSPI_ANDROID */
54
55 /* these are for the older cores... for newer cores we have control for each of them */
56 #define F0_RESPONSE_DELAY       16
57 #define F1_RESPONSE_DELAY       16
58 #define F2_RESPONSE_DELAY       F0_RESPONSE_DELAY
59
60
61 #define GSPI_F0_RESP_DELAY              0
62 #define GSPI_F1_RESP_DELAY              F1_RESPONSE_DELAY
63 #define GSPI_F2_RESP_DELAY              0
64 #define GSPI_F3_RESP_DELAY              0
65
66 #define CMDLEN          4
67
68 #define DWORDMODE_ON (sd->chip == BCM4329_CHIP_ID) && (sd->chiprev == 2) && (sd->dwordmode == TRUE)
69
70 /* Globals */
71 #if defined(DHD_DEBUG)
72 uint sd_msglevel = SDH_ERROR_VAL;
73 #else
74 uint sd_msglevel = 0;
75 #endif 
76
77 uint sd_hiok = FALSE;           /* Use hi-speed mode if available? */
78 uint sd_sdmode = SDIOH_MODE_SPI;                /* Use SD4 mode by default */
79 uint sd_f2_blocksize = 64;              /* Default blocksize */
80
81
82 uint sd_divisor = 2;
83 uint sd_power = 1;              /* Default to SD Slot powered ON */
84 uint sd_clock = 1;              /* Default to SD Clock turned ON */
85 uint sd_crc = 0;                /* Default to SPI CRC Check turned OFF */
86 uint sd_pci_slot = 0xFFFFffff; /* Used to force selection of a particular PCI slot */
87
88 uint8   spi_outbuf[SPI_MAX_PKT_LEN];
89 uint8   spi_inbuf[SPI_MAX_PKT_LEN];
90
91 /* 128bytes buffer is enough to clear data-not-available and program response-delay F0 bits
92  * assuming we will not exceed F0 response delay > 100 bytes at 48MHz.
93  */
94 #define BUF2_PKT_LEN    128
95 uint8   spi_outbuf2[BUF2_PKT_LEN];
96 uint8   spi_inbuf2[BUF2_PKT_LEN];
97 #ifdef BCMSPI_ANDROID
98 uint *dhd_spi_lockcount = NULL;
99 #endif /* BCMSPI_ANDROID */
100
101 #if !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW))
102 #define SPISWAP_WD4(x) bcmswap32(x);
103 #define SPISWAP_WD2(x) (bcmswap16(x & 0xffff)) | \
104                                                 (bcmswap16((x & 0xffff0000) >> 16) << 16);
105 #else
106 #define SPISWAP_WD4(x) x;
107 #define SPISWAP_WD2(x) bcmswap32by16(x);
108 #endif
109
110 /* Prototypes */
111 static bool bcmspi_test_card(sdioh_info_t *sd);
112 static bool bcmspi_host_device_init_adapt(sdioh_info_t *sd);
113 static int bcmspi_set_highspeed_mode(sdioh_info_t *sd, bool hsmode);
114 static int bcmspi_cmd_issue(sdioh_info_t *sd, bool use_dma, uint32 cmd_arg,
115                            uint32 *data, uint32 datalen);
116 static int bcmspi_card_regread(sdioh_info_t *sd, int func, uint32 regaddr,
117                               int regsize, uint32 *data);
118 static int bcmspi_card_regwrite(sdioh_info_t *sd, int func, uint32 regaddr,
119                                int regsize, uint32 data);
120 static int bcmspi_card_bytewrite(sdioh_info_t *sd, int func, uint32 regaddr,
121                                uint8 *data);
122 static int bcmspi_driver_init(sdioh_info_t *sd);
123 static int bcmspi_card_buf(sdioh_info_t *sd, int rw, int func, bool fifo,
124                           uint32 addr, int nbytes, uint32 *data);
125 static int bcmspi_card_regread_fixedaddr(sdioh_info_t *sd, int func, uint32 regaddr, int regsize,
126                                  uint32 *data);
127 static void bcmspi_cmd_getdstatus(sdioh_info_t *sd, uint32 *dstatus_buffer);
128 static int bcmspi_update_stats(sdioh_info_t *sd, uint32 cmd_arg);
129
130 /*
131  *  Public entry points & extern's
132  */
133 extern sdioh_info_t *
134 sdioh_attach(osl_t *osh, void *bar0, uint irq)
135 {
136         sdioh_info_t *sd;
137
138         sd_trace(("%s\n", __FUNCTION__));
139         if ((sd = (sdioh_info_t *)MALLOC(osh, sizeof(sdioh_info_t))) == NULL) {
140                 sd_err(("%s: out of memory, malloced %d bytes\n", __FUNCTION__, MALLOCED(osh)));
141                 return NULL;
142         }
143         bzero((char *)sd, sizeof(sdioh_info_t));
144         sd->osh = osh;
145         if (spi_osinit(sd) != 0) {
146                 sd_err(("%s: spi_osinit() failed\n", __FUNCTION__));
147                 MFREE(sd->osh, sd, sizeof(sdioh_info_t));
148                 return NULL;
149         }
150
151 #ifndef BCMSPI_ANDROID
152         sd->bar0 = bar0;
153 #endif /* !BCMSPI_ANDROID */
154         sd->irq = irq;
155 #ifndef BCMSPI_ANDROID
156         sd->intr_handler = NULL;
157         sd->intr_handler_arg = NULL;
158         sd->intr_handler_valid = FALSE;
159 #endif /* !BCMSPI_ANDROID */
160
161         /* Set defaults */
162         sd->use_client_ints = TRUE;
163         sd->sd_use_dma = FALSE; /* DMA Not supported */
164
165         /* Spi device default is 16bit mode, change to 4 when device is changed to 32bit
166          * mode
167          */
168         sd->wordlen = 2;
169
170 #ifdef BCMSPI_ANDROID
171         dhd_spi_lockcount = &sd->lockcount;
172 #endif /* BCMSPI_ANDROID */
173
174 #ifndef BCMSPI_ANDROID
175         if (!spi_hw_attach(sd)) {
176                 sd_err(("%s: spi_hw_attach() failed\n", __FUNCTION__));
177                 spi_osfree(sd);
178                 MFREE(sd->osh, sd, sizeof(sdioh_info_t));
179                 return (NULL);
180         }
181 #endif /* !BCMSPI_ANDROID */
182
183         if (bcmspi_driver_init(sd) != SUCCESS) {
184                 sd_err(("%s: bcmspi_driver_init() failed()\n", __FUNCTION__));
185 #ifndef BCMSPI_ANDROID
186                 spi_hw_detach(sd);
187 #endif /* !BCMSPI_ANDROID */
188                 spi_osfree(sd);
189                 MFREE(sd->osh, sd, sizeof(sdioh_info_t));
190                 return (NULL);
191         }
192
193         if (spi_register_irq(sd, irq) != SUCCESS) {
194                 sd_err(("%s: spi_register_irq() failed for irq = %d\n", __FUNCTION__, irq));
195 #ifndef BCMSPI_ANDROID
196                 spi_hw_detach(sd);
197 #endif /* !BCMSPI_ANDROID */
198                 spi_osfree(sd);
199                 MFREE(sd->osh, sd, sizeof(sdioh_info_t));
200                 return (NULL);
201         }
202
203         sd_trace(("%s: Done\n", __FUNCTION__));
204
205         return sd;
206 }
207
208 extern SDIOH_API_RC
209 sdioh_detach(osl_t *osh, sdioh_info_t *sd)
210 {
211         sd_trace(("%s\n", __FUNCTION__));
212         if (sd) {
213                 sd_err(("%s: detaching from hardware\n", __FUNCTION__));
214                 spi_free_irq(sd->irq, sd);
215 #ifndef BCMSPI_ANDROID
216                 spi_hw_detach(sd);
217 #endif /* !BCMSPI_ANDROID */
218                 spi_osfree(sd);
219 #ifdef BCMSPI_ANDROID
220                 dhd_spi_lockcount = NULL;
221 #endif /* !BCMSPI_ANDROID */
222                 MFREE(sd->osh, sd, sizeof(sdioh_info_t));
223         }
224         return SDIOH_API_RC_SUCCESS;
225 }
226
227 /* Configure callback to client when we recieve client interrupt */
228 extern SDIOH_API_RC
229 sdioh_interrupt_register(sdioh_info_t *sd, sdioh_cb_fn_t fn, void *argh)
230 {
231         sd_trace(("%s: Entering\n", __FUNCTION__));
232 #if !defined(OOB_INTR_ONLY)
233         sd->intr_handler = fn;
234         sd->intr_handler_arg = argh;
235         sd->intr_handler_valid = TRUE;
236 #endif /* !defined(OOB_INTR_ONLY) */
237         return SDIOH_API_RC_SUCCESS;
238 }
239
240 extern SDIOH_API_RC
241 sdioh_interrupt_deregister(sdioh_info_t *sd)
242 {
243         sd_trace(("%s: Entering\n", __FUNCTION__));
244 #if !defined(OOB_INTR_ONLY)
245         sd->intr_handler_valid = FALSE;
246         sd->intr_handler = NULL;
247         sd->intr_handler_arg = NULL;
248 #endif /* !defined(OOB_INTR_ONLY) */
249         return SDIOH_API_RC_SUCCESS;
250 }
251
252 extern SDIOH_API_RC
253 sdioh_interrupt_query(sdioh_info_t *sd, bool *onoff)
254 {
255 #ifndef BCMSPI_ANDROID
256         sd_trace(("%s: Entering\n", __FUNCTION__));
257         *onoff = sd->client_intr_enabled;
258 #endif /* !BCMSPI_ANDROID */
259         return SDIOH_API_RC_SUCCESS;
260 }
261
262 #if defined(DHD_DEBUG)
263 extern bool
264 sdioh_interrupt_pending(sdioh_info_t *sd)
265 {
266         return 0;
267 }
268 #endif
269
270 extern SDIOH_API_RC
271 sdioh_query_device(sdioh_info_t *sd)
272 {
273         /* Return a BRCM ID appropriate to the dongle class */
274         return (sd->num_funcs > 1) ? BCM4329_D11N_ID : BCM4318_D11G_ID;
275 }
276
277 /* Provide dstatus bits of spi-transaction for dhd layers. */
278 extern uint32
279 sdioh_get_dstatus(sdioh_info_t *sd)
280 {
281         return sd->card_dstatus;
282 }
283
284 extern void
285 sdioh_chipinfo(sdioh_info_t *sd, uint32 chip, uint32 chiprev)
286 {
287         sd->chip = chip;
288         sd->chiprev = chiprev;
289 }
290
291 extern void
292 sdioh_dwordmode(sdioh_info_t *sd, bool set)
293 {
294         uint8 reg = 0;
295         int status;
296
297         if ((status = sdioh_request_byte(sd, SDIOH_READ, SPI_FUNC_0, SPID_STATUS_ENABLE, &reg)) !=
298              SUCCESS) {
299                 sd_err(("%s: Failed to set dwordmode in gSPI\n", __FUNCTION__));
300                 return;
301         }
302
303         if (set) {
304                 reg |= DWORD_PKT_LEN_EN;
305                 sd->dwordmode = TRUE;
306                 sd->client_block_size[SPI_FUNC_2] = 4096; /* h2spi's limit is 4KB, we support 8KB */
307         } else {
308                 reg &= ~DWORD_PKT_LEN_EN;
309                 sd->dwordmode = FALSE;
310                 sd->client_block_size[SPI_FUNC_2] = 2048;
311         }
312
313         if ((status = sdioh_request_byte(sd, SDIOH_WRITE, SPI_FUNC_0, SPID_STATUS_ENABLE, &reg)) !=
314              SUCCESS) {
315                 sd_err(("%s: Failed to set dwordmode in gSPI\n", __FUNCTION__));
316                 return;
317         }
318 }
319
320
321 uint
322 sdioh_query_iofnum(sdioh_info_t *sd)
323 {
324         return sd->num_funcs;
325 }
326
327 /* IOVar table */
328 enum {
329         IOV_MSGLEVEL = 1,
330         IOV_BLOCKMODE,
331         IOV_BLOCKSIZE,
332         IOV_DMA,
333         IOV_USEINTS,
334         IOV_NUMINTS,
335         IOV_NUMLOCALINTS,
336         IOV_HOSTREG,
337         IOV_DEVREG,
338         IOV_DIVISOR,
339         IOV_SDMODE,
340         IOV_HISPEED,
341         IOV_HCIREGS,
342         IOV_POWER,
343         IOV_CLOCK,
344         IOV_SPIERRSTATS,
345         IOV_RESP_DELAY_ALL
346 };
347
348 const bcm_iovar_t sdioh_iovars[] = {
349         {"sd_msglevel", IOV_MSGLEVEL,   0,      IOVT_UINT32,    0 },
350         {"sd_blocksize", IOV_BLOCKSIZE, 0,      IOVT_UINT32,    0 }, /* ((fn << 16) | size) */
351         {"sd_dma",      IOV_DMA,        0,      IOVT_BOOL,      0 },
352         {"sd_ints",     IOV_USEINTS,    0,      IOVT_BOOL,      0 },
353         {"sd_numints",  IOV_NUMINTS,    0,      IOVT_UINT32,    0 },
354         {"sd_numlocalints", IOV_NUMLOCALINTS, 0, IOVT_UINT32,   0 },
355         {"sd_hostreg",  IOV_HOSTREG,    0,      IOVT_BUFFER,    sizeof(sdreg_t) },
356         {"sd_devreg",   IOV_DEVREG,     0,      IOVT_BUFFER,    sizeof(sdreg_t) },
357         {"sd_divisor",  IOV_DIVISOR,    0,      IOVT_UINT32,    0 },
358         {"sd_power",    IOV_POWER,      0,      IOVT_UINT32,    0 },
359         {"sd_clock",    IOV_CLOCK,      0,      IOVT_UINT32,    0 },
360         {"sd_mode",     IOV_SDMODE,     0,      IOVT_UINT32,    100},
361         {"sd_highspeed",        IOV_HISPEED,    0,      IOVT_UINT32,    0},
362         {"spi_errstats", IOV_SPIERRSTATS, 0, IOVT_BUFFER, sizeof(struct spierrstats_t) },
363         {"spi_respdelay",       IOV_RESP_DELAY_ALL,     0,      IOVT_BOOL,      0 },
364         {NULL, 0, 0, 0, 0 }
365 };
366
367 int
368 sdioh_iovar_op(sdioh_info_t *si, const char *name,
369                void *params, int plen, void *arg, int len, bool set)
370 {
371         const bcm_iovar_t *vi = NULL;
372         int bcmerror = 0;
373         int val_size;
374         int32 int_val = 0;
375         bool bool_val;
376         uint32 actionid;
377 /*
378         sdioh_regs_t *regs;
379 */
380
381         ASSERT(name);
382         ASSERT(len >= 0);
383
384         /* Get must have return space; Set does not take qualifiers */
385         ASSERT(set || (arg && len));
386         ASSERT(!set || (!params && !plen));
387
388         sd_trace(("%s: Enter (%s %s)\n", __FUNCTION__, (set ? "set" : "get"), name));
389
390         if ((vi = bcm_iovar_lookup(sdioh_iovars, name)) == NULL) {
391                 bcmerror = BCME_UNSUPPORTED;
392                 goto exit;
393         }
394
395         if ((bcmerror = bcm_iovar_lencheck(vi, arg, len, set)) != 0)
396                 goto exit;
397
398         /* Set up params so get and set can share the convenience variables */
399         if (params == NULL) {
400                 params = arg;
401                 plen = len;
402         }
403
404         if (vi->type == IOVT_VOID)
405                 val_size = 0;
406         else if (vi->type == IOVT_BUFFER)
407                 val_size = len;
408         else
409                 val_size = sizeof(int);
410
411         if (plen >= (int)sizeof(int_val))
412                 bcopy(params, &int_val, sizeof(int_val));
413
414         bool_val = (int_val != 0) ? TRUE : FALSE;
415
416         actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
417         switch (actionid) {
418         case IOV_GVAL(IOV_MSGLEVEL):
419                 int_val = (int32)sd_msglevel;
420                 bcopy(&int_val, arg, val_size);
421                 break;
422
423         case IOV_SVAL(IOV_MSGLEVEL):
424                 sd_msglevel = int_val;
425                 break;
426
427         case IOV_GVAL(IOV_BLOCKSIZE):
428                 if ((uint32)int_val > si->num_funcs) {
429                         bcmerror = BCME_BADARG;
430                         break;
431                 }
432                 int_val = (int32)si->client_block_size[int_val];
433                 bcopy(&int_val, arg, val_size);
434                 break;
435
436         case IOV_GVAL(IOV_DMA):
437                 int_val = (int32)si->sd_use_dma;
438                 bcopy(&int_val, arg, val_size);
439                 break;
440
441         case IOV_SVAL(IOV_DMA):
442                 si->sd_use_dma = (bool)int_val;
443                 break;
444
445         case IOV_GVAL(IOV_USEINTS):
446                 int_val = (int32)si->use_client_ints;
447                 bcopy(&int_val, arg, val_size);
448                 break;
449
450         case IOV_SVAL(IOV_USEINTS):
451                 break;
452
453         case IOV_GVAL(IOV_DIVISOR):
454                 int_val = (uint32)sd_divisor;
455                 bcopy(&int_val, arg, val_size);
456                 break;
457
458 #ifndef BCMSPI_ANDROID
459         case IOV_SVAL(IOV_DIVISOR):
460                 sd_divisor = int_val;
461                 if (!spi_start_clock(si, (uint16)sd_divisor)) {
462                         sd_err(("%s: set clock failed\n", __FUNCTION__));
463                         bcmerror = BCME_ERROR;
464                 }
465                 break;
466 #endif /* !BCMSPI_ANDROID */
467
468         case IOV_GVAL(IOV_POWER):
469                 int_val = (uint32)sd_power;
470                 bcopy(&int_val, arg, val_size);
471                 break;
472
473         case IOV_SVAL(IOV_POWER):
474                 sd_power = int_val;
475                 break;
476
477         case IOV_GVAL(IOV_CLOCK):
478                 int_val = (uint32)sd_clock;
479                 bcopy(&int_val, arg, val_size);
480                 break;
481
482         case IOV_SVAL(IOV_CLOCK):
483                 sd_clock = int_val;
484                 break;
485
486         case IOV_GVAL(IOV_SDMODE):
487                 int_val = (uint32)sd_sdmode;
488                 bcopy(&int_val, arg, val_size);
489                 break;
490
491         case IOV_SVAL(IOV_SDMODE):
492                 sd_sdmode = int_val;
493                 break;
494
495         case IOV_GVAL(IOV_HISPEED):
496                 int_val = (uint32)sd_hiok;
497                 bcopy(&int_val, arg, val_size);
498                 break;
499
500         case IOV_SVAL(IOV_HISPEED):
501                 sd_hiok = int_val;
502
503                 if (!bcmspi_set_highspeed_mode(si, (bool)sd_hiok)) {
504                         sd_err(("%s: Failed changing highspeed mode to %d.\n",
505                                 __FUNCTION__, sd_hiok));
506                         bcmerror = BCME_ERROR;
507                         return ERROR;
508                 }
509                 break;
510
511         case IOV_GVAL(IOV_NUMINTS):
512                 int_val = (int32)si->intrcount;
513                 bcopy(&int_val, arg, val_size);
514                 break;
515
516         case IOV_GVAL(IOV_NUMLOCALINTS):
517                 int_val = (int32)si->local_intrcount;
518                 bcopy(&int_val, arg, val_size);
519                 break;
520         case IOV_GVAL(IOV_DEVREG):
521         {
522                 sdreg_t *sd_ptr = (sdreg_t *)params;
523                 uint8 data;
524
525                 if (sdioh_cfg_read(si, sd_ptr->func, sd_ptr->offset, &data)) {
526                         bcmerror = BCME_SDIO_ERROR;
527                         break;
528                 }
529
530                 int_val = (int)data;
531                 bcopy(&int_val, arg, sizeof(int_val));
532                 break;
533         }
534
535         case IOV_SVAL(IOV_DEVREG):
536         {
537                 sdreg_t *sd_ptr = (sdreg_t *)params;
538                 uint8 data = (uint8)sd_ptr->value;
539
540                 if (sdioh_cfg_write(si, sd_ptr->func, sd_ptr->offset, &data)) {
541                         bcmerror = BCME_SDIO_ERROR;
542                         break;
543                 }
544                 break;
545         }
546
547
548         case IOV_GVAL(IOV_SPIERRSTATS):
549         {
550                 bcopy(&si->spierrstats, arg, sizeof(struct spierrstats_t));
551                 break;
552         }
553
554         case IOV_SVAL(IOV_SPIERRSTATS):
555         {
556                 bzero(&si->spierrstats, sizeof(struct spierrstats_t));
557                 break;
558         }
559
560         case IOV_GVAL(IOV_RESP_DELAY_ALL):
561                 int_val = (int32)si->resp_delay_all;
562                 bcopy(&int_val, arg, val_size);
563                 break;
564
565         case IOV_SVAL(IOV_RESP_DELAY_ALL):
566                 si->resp_delay_all = (bool)int_val;
567                 int_val = STATUS_ENABLE|INTR_WITH_STATUS;
568                 if (si->resp_delay_all)
569                         int_val |= RESP_DELAY_ALL;
570                 else {
571                         if (bcmspi_card_regwrite(si, SPI_FUNC_0, SPID_RESPONSE_DELAY, 1,
572                              F1_RESPONSE_DELAY) != SUCCESS) {
573                                 sd_err(("%s: Unable to set response delay.\n", __FUNCTION__));
574                                 bcmerror = BCME_SDIO_ERROR;
575                                 break;
576                         }
577                 }
578
579                 if (bcmspi_card_regwrite(si, SPI_FUNC_0, SPID_STATUS_ENABLE, 1, int_val)
580                      != SUCCESS) {
581                         sd_err(("%s: Unable to set response delay.\n", __FUNCTION__));
582                         bcmerror = BCME_SDIO_ERROR;
583                         break;
584                 }
585                 break;
586
587         default:
588                 bcmerror = BCME_UNSUPPORTED;
589                 break;
590         }
591 exit:
592
593         return bcmerror;
594 }
595
596 extern SDIOH_API_RC
597 sdioh_cfg_read(sdioh_info_t *sd, uint fnc_num, uint32 addr, uint8 *data)
598 {
599         SDIOH_API_RC status;
600         /* No lock needed since sdioh_request_byte does locking */
601         status = sdioh_request_byte(sd, SDIOH_READ, fnc_num, addr, data);
602         return status;
603 }
604
605 extern SDIOH_API_RC
606 sdioh_cfg_write(sdioh_info_t *sd, uint fnc_num, uint32 addr, uint8 *data)
607 {
608         /* No lock needed since sdioh_request_byte does locking */
609         SDIOH_API_RC status;
610
611         if ((fnc_num == SPI_FUNC_1) && (addr == SBSDIO_FUNC1_FRAMECTRL)) {
612                 uint8 dummy_data;
613                 status = sdioh_cfg_read(sd, fnc_num, addr, &dummy_data);
614                 if (status) {
615                         sd_err(("sdioh_cfg_read() failed.\n"));
616                         return status;
617                 }
618         }
619
620         status = sdioh_request_byte(sd, SDIOH_WRITE, fnc_num, addr, data);
621         return status;
622 }
623
624 extern SDIOH_API_RC
625 sdioh_cis_read(sdioh_info_t *sd, uint func, uint8 *cisd, uint32 length)
626 {
627         uint32 count;
628         int offset;
629         uint32 cis_byte;
630         uint16 *cis = (uint16 *)cisd;
631         uint bar0 = SI_ENUM_BASE;
632         int status;
633         uint8 data;
634
635         sd_trace(("%s: Func %d\n", __FUNCTION__, func));
636
637         spi_lock(sd);
638
639         /* Set sb window address to 0x18000000 */
640         data = (bar0 >> 8) & SBSDIO_SBADDRLOW_MASK;
641         status = bcmspi_card_bytewrite(sd, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW, &data);
642         if (status == SUCCESS) {
643                 data = (bar0 >> 16) & SBSDIO_SBADDRMID_MASK;
644                 status = bcmspi_card_bytewrite(sd, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID, &data);
645         } else {
646                 sd_err(("%s: Unable to set sb-addr-windows\n", __FUNCTION__));
647                 spi_unlock(sd);
648                 return (BCME_ERROR);
649         }
650         if (status == SUCCESS) {
651                 data = (bar0 >> 24) & SBSDIO_SBADDRHIGH_MASK;
652                 status = bcmspi_card_bytewrite(sd, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH, &data);
653         } else {
654                 sd_err(("%s: Unable to set sb-addr-windows\n", __FUNCTION__));
655                 spi_unlock(sd);
656                 return (BCME_ERROR);
657         }
658
659         offset =  CC_SROM_OTP; /* OTP offset in chipcommon. */
660         for (count = 0; count < length/2; count++) {
661                 if (bcmspi_card_regread (sd, SDIO_FUNC_1, offset, 2, &cis_byte) < 0) {
662                         sd_err(("%s: regread failed: Can't read CIS\n", __FUNCTION__));
663                         spi_unlock(sd);
664                         return (BCME_ERROR);
665                 }
666
667                 *cis = (uint16)cis_byte;
668                 cis++;
669                 offset += 2;
670         }
671
672         spi_unlock(sd);
673
674         return (BCME_OK);
675 }
676
677 extern SDIOH_API_RC
678 sdioh_request_byte(sdioh_info_t *sd, uint rw, uint func, uint regaddr, uint8 *byte)
679 {
680         int status;
681         uint32 cmd_arg;
682         uint32 dstatus;
683         uint32 data = (uint32)(*byte);
684
685         spi_lock(sd);
686
687         cmd_arg = 0;
688         cmd_arg = SFIELD(cmd_arg, SPI_FUNCTION, func);
689         cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 1);       /* Incremental access */
690         cmd_arg = SFIELD(cmd_arg, SPI_REG_ADDR, regaddr);
691         cmd_arg = SFIELD(cmd_arg, SPI_RW_FLAG, rw == SDIOH_READ ? 0 : 1);
692         cmd_arg = SFIELD(cmd_arg, SPI_LEN, 1);
693
694         if (rw == SDIOH_READ) {
695                 sd_trace(("%s: RD cmd_arg=0x%x func=%d regaddr=0x%x\n",
696                           __FUNCTION__, cmd_arg, func, regaddr));
697         } else {
698                 sd_trace(("%s: WR cmd_arg=0x%x func=%d regaddr=0x%x data=0x%x\n",
699                           __FUNCTION__, cmd_arg, func, regaddr, data));
700         }
701
702         if ((status = bcmspi_cmd_issue(sd, sd->sd_use_dma, cmd_arg, &data, 1)) != SUCCESS) {
703                 spi_unlock(sd);
704                 return status;
705         }
706
707         if (rw == SDIOH_READ) {
708                 *byte = (uint8)data;
709                 sd_trace(("%s: RD result=0x%x\n", __FUNCTION__, *byte));
710         }
711
712         bcmspi_cmd_getdstatus(sd, &dstatus);
713         if (dstatus)
714                 sd_trace(("dstatus=0x%x\n", dstatus));
715
716         spi_unlock(sd);
717         return SDIOH_API_RC_SUCCESS;
718 }
719
720 extern SDIOH_API_RC
721 sdioh_request_word(sdioh_info_t *sd, uint cmd_type, uint rw, uint func, uint addr,
722                    uint32 *word, uint nbytes)
723 {
724         int status;
725
726         spi_lock(sd);
727
728         if (rw == SDIOH_READ)
729                 status = bcmspi_card_regread(sd, func, addr, nbytes, word);
730         else
731                 status = bcmspi_card_regwrite(sd, func, addr, nbytes, *word);
732
733         spi_unlock(sd);
734         return (status == SUCCESS ?  SDIOH_API_RC_SUCCESS : SDIOH_API_RC_FAIL);
735 }
736
737 extern SDIOH_API_RC
738 sdioh_request_buffer(sdioh_info_t *sd, uint pio_dma, uint fix_inc, uint rw, uint func,
739                      uint addr, uint reg_width, uint buflen_u, uint8 *buffer, void *pkt)
740 {
741         int len;
742         int buflen = (int)buflen_u;
743         bool fifo = (fix_inc == SDIOH_DATA_FIX);
744
745         spi_lock(sd);
746
747         ASSERT(reg_width == 4);
748         ASSERT(buflen_u < (1 << 30));
749         ASSERT(sd->client_block_size[func]);
750
751         sd_data(("%s: %c len %d r_cnt %d t_cnt %d, pkt @0x%p\n",
752                  __FUNCTION__, rw == SDIOH_READ ? 'R' : 'W',
753                  buflen_u, sd->r_cnt, sd->t_cnt, pkt));
754
755         /* Break buffer down into blocksize chunks. */
756         while (buflen > 0) {
757                 len = MIN(sd->client_block_size[func], buflen);
758                 if (bcmspi_card_buf(sd, rw, func, fifo, addr, len, (uint32 *)buffer) != SUCCESS) {
759                         sd_err(("%s: bcmspi_card_buf %s failed\n",
760                                 __FUNCTION__, rw == SDIOH_READ ? "Read" : "Write"));
761                         spi_unlock(sd);
762                         return SDIOH_API_RC_FAIL;
763                 }
764                 buffer += len;
765                 buflen -= len;
766                 if (!fifo)
767                         addr += len;
768         }
769         spi_unlock(sd);
770         return SDIOH_API_RC_SUCCESS;
771 }
772
773 /* This function allows write to gspi bus when another rd/wr function is deep down the call stack.
774  * Its main aim is to have simpler spi writes rather than recursive writes.
775  * e.g. When there is a need to program response delay on the fly after detecting the SPI-func
776  * this call will allow to program the response delay.
777  */
778 static int
779 bcmspi_card_byterewrite(sdioh_info_t *sd, int func, uint32 regaddr, uint8 byte)
780 {
781         uint32 cmd_arg;
782         uint32 datalen = 1;
783         uint32 hostlen;
784
785         cmd_arg = 0;
786
787         cmd_arg = SFIELD(cmd_arg, SPI_RW_FLAG, 1);
788         cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 1);       /* Incremental access */
789         cmd_arg = SFIELD(cmd_arg, SPI_FUNCTION, func);
790         cmd_arg = SFIELD(cmd_arg, SPI_REG_ADDR, regaddr);
791         cmd_arg = SFIELD(cmd_arg, SPI_LEN, datalen);
792
793         sd_trace(("%s cmd_arg = 0x%x\n", __FUNCTION__, cmd_arg));
794
795
796         /* Set up and issue the SPI command.  MSByte goes out on bus first.  Increase datalen
797          * according to the wordlen mode(16/32bit) the device is in.
798          */
799         ASSERT(sd->wordlen == 4 || sd->wordlen == 2);
800         datalen = ROUNDUP(datalen, sd->wordlen);
801
802         /* Start by copying command in the spi-outbuffer */
803         if (sd->wordlen == 4) { /* 32bit spid */
804                 *(uint32 *)spi_outbuf2 = SPISWAP_WD4(cmd_arg);
805                 if (datalen & 0x3)
806                         datalen += (4 - (datalen & 0x3));
807         } else if (sd->wordlen == 2) { /* 16bit spid */
808                 *(uint32 *)spi_outbuf2 = SPISWAP_WD2(cmd_arg);
809                 if (datalen & 0x1)
810                         datalen++;
811         } else {
812                 sd_err(("%s: Host is %d bit spid, could not create SPI command.\n",
813                         __FUNCTION__, 8 * sd->wordlen));
814                 return ERROR;
815         }
816
817         /* for Write, put the data into the output buffer  */
818         if (datalen != 0) {
819                         if (sd->wordlen == 4) { /* 32bit spid */
820                                 *(uint32 *)&spi_outbuf2[CMDLEN] = SPISWAP_WD4(byte);
821                         } else if (sd->wordlen == 2) { /* 16bit spid */
822                                 *(uint32 *)&spi_outbuf2[CMDLEN] = SPISWAP_WD2(byte);
823                         }
824         }
825
826         /* +4 for cmd, +4 for dstatus */
827         hostlen = datalen + 8;
828         hostlen += (4 - (hostlen & 0x3));
829         spi_sendrecv(sd, spi_outbuf2, spi_inbuf2, hostlen);
830
831         /* Last 4bytes are dstatus.  Device is configured to return status bits. */
832         if (sd->wordlen == 4) { /* 32bit spid */
833                 sd->card_dstatus = SPISWAP_WD4(*(uint32 *)&spi_inbuf2[datalen + CMDLEN ]);
834         } else if (sd->wordlen == 2) { /* 16bit spid */
835                 sd->card_dstatus = SPISWAP_WD2(*(uint32 *)&spi_inbuf2[datalen + CMDLEN ]);
836         } else {
837                 sd_err(("%s: Host is %d bit machine, could not read SPI dstatus.\n",
838                         __FUNCTION__, 8 * sd->wordlen));
839                 return ERROR;
840         }
841
842         if (sd->card_dstatus)
843                 sd_trace(("dstatus after byte rewrite = 0x%x\n", sd->card_dstatus));
844
845         return (BCME_OK);
846 }
847
848 /* Program the response delay corresponding to the spi function */
849 static int
850 bcmspi_prog_resp_delay(sdioh_info_t *sd, int func, uint8 resp_delay)
851 {
852         if (sd->resp_delay_all == FALSE)
853                 return (BCME_OK);
854
855         if (sd->prev_fun == func)
856                 return (BCME_OK);
857
858         if (F0_RESPONSE_DELAY == F1_RESPONSE_DELAY)
859                 return (BCME_OK);
860
861         bcmspi_card_byterewrite(sd, SPI_FUNC_0, SPID_RESPONSE_DELAY, resp_delay);
862
863         /* Remember function for which to avoid reprogramming resp-delay in next iteration */
864         sd->prev_fun = func;
865
866         return (BCME_OK);
867
868 }
869
870 #define GSPI_RESYNC_PATTERN     0x0
871
872 /* A resync pattern is a 32bit MOSI line with all zeros. Its a special command in gSPI.
873  * It resets the spi-bkplane logic so that all F1 related ping-pong buffer logic is
874  * synchronised and all queued resuests are cancelled.
875  */
876 static int
877 bcmspi_resync_f1(sdioh_info_t *sd)
878 {
879         uint32 cmd_arg = GSPI_RESYNC_PATTERN, data = 0, datalen = 0;
880
881
882         /* Set up and issue the SPI command.  MSByte goes out on bus first.  Increase datalen
883          * according to the wordlen mode(16/32bit) the device is in.
884          */
885         ASSERT(sd->wordlen == 4 || sd->wordlen == 2);
886         datalen = ROUNDUP(datalen, sd->wordlen);
887
888         /* Start by copying command in the spi-outbuffer */
889         *(uint32 *)spi_outbuf2 = cmd_arg;
890
891         /* for Write, put the data into the output buffer  */
892         *(uint32 *)&spi_outbuf2[CMDLEN] = data;
893
894         /* +4 for cmd, +4 for dstatus */
895         spi_sendrecv(sd, spi_outbuf2, spi_inbuf2, datalen + 8);
896
897         /* Last 4bytes are dstatus.  Device is configured to return status bits. */
898         if (sd->wordlen == 4) { /* 32bit spid */
899                 sd->card_dstatus = SPISWAP_WD4(*(uint32 *)&spi_inbuf2[datalen + CMDLEN ]);
900         } else if (sd->wordlen == 2) { /* 16bit spid */
901                 sd->card_dstatus = SPISWAP_WD2(*(uint32 *)&spi_inbuf2[datalen + CMDLEN ]);
902         } else {
903                 sd_err(("%s: Host is %d bit machine, could not read SPI dstatus.\n",
904                         __FUNCTION__, 8 * sd->wordlen));
905                 return ERROR;
906         }
907
908         if (sd->card_dstatus)
909                 sd_trace(("dstatus after resync pattern write = 0x%x\n", sd->card_dstatus));
910
911         return (BCME_OK);
912 }
913
914 uint32 dstatus_count = 0;
915
916 static int
917 bcmspi_update_stats(sdioh_info_t *sd, uint32 cmd_arg)
918 {
919         uint32 dstatus = sd->card_dstatus;
920         struct spierrstats_t *spierrstats = &sd->spierrstats;
921         int err = SUCCESS;
922
923         sd_trace(("cmd = 0x%x, dstatus = 0x%x\n", cmd_arg, dstatus));
924
925         /* Store dstatus of last few gSPI transactions */
926         spierrstats->dstatus[dstatus_count % NUM_PREV_TRANSACTIONS] = dstatus;
927         spierrstats->spicmd[dstatus_count % NUM_PREV_TRANSACTIONS] = cmd_arg;
928         dstatus_count++;
929
930         if (sd->card_init_done == FALSE)
931                 return err;
932
933         if (dstatus & STATUS_DATA_NOT_AVAILABLE) {
934                 spierrstats->dna++;
935                 sd_trace(("Read data not available on F1 addr = 0x%x\n",
936                         GFIELD(cmd_arg, SPI_REG_ADDR)));
937                 /* Clear dna bit */
938                 bcmspi_card_byterewrite(sd, SPI_FUNC_0, SPID_INTR_REG, DATA_UNAVAILABLE);
939         }
940
941         if (dstatus & STATUS_UNDERFLOW) {
942                 spierrstats->rdunderflow++;
943                 sd_err(("FIFO underflow happened due to current F2 read command.\n"));
944         }
945
946         if (dstatus & STATUS_OVERFLOW) {
947                 spierrstats->wroverflow++;
948                 sd_err(("FIFO overflow happened due to current (F1/F2) write command.\n"));
949                 bcmspi_card_byterewrite(sd, SPI_FUNC_0, SPID_INTR_REG, F1_OVERFLOW);
950                 bcmspi_resync_f1(sd);
951                 sd_err(("Recovering from F1 FIFO overflow.\n"));
952         }
953
954         if (dstatus & STATUS_F2_INTR) {
955                 spierrstats->f2interrupt++;
956                 sd_trace(("Interrupt from F2.  SW should clear corresponding IntStatus bits\n"));
957         }
958
959         if (dstatus & STATUS_F3_INTR) {
960                 spierrstats->f3interrupt++;
961                 sd_err(("Interrupt from F3.  SW should clear corresponding IntStatus bits\n"));
962         }
963
964         if (dstatus & STATUS_HOST_CMD_DATA_ERR) {
965                 spierrstats->hostcmddataerr++;
966                 sd_err(("Error in CMD or Host data, detected by CRC/Checksum (optional)\n"));
967         }
968
969         if (dstatus & STATUS_F2_PKT_AVAILABLE) {
970                 spierrstats->f2pktavailable++;
971                 sd_trace(("Packet is available/ready in F2 TX FIFO\n"));
972                 sd_trace(("Packet length = %d\n", sd->dwordmode ?
973                          ((dstatus & STATUS_F2_PKT_LEN_MASK) >> (STATUS_F2_PKT_LEN_SHIFT - 2)) :
974                          ((dstatus & STATUS_F2_PKT_LEN_MASK) >> STATUS_F2_PKT_LEN_SHIFT)));
975         }
976
977         if (dstatus & STATUS_F3_PKT_AVAILABLE) {
978                 spierrstats->f3pktavailable++;
979                 sd_err(("Packet is available/ready in F3 TX FIFO\n"));
980                 sd_err(("Packet length = %d\n",
981                         (dstatus & STATUS_F3_PKT_LEN_MASK) >> STATUS_F3_PKT_LEN_SHIFT));
982         }
983
984         return err;
985 }
986
987 extern int
988 sdioh_abort(sdioh_info_t *sd, uint func)
989 {
990         return 0;
991 }
992
993 int
994 sdioh_start(sdioh_info_t *sd, int stage)
995 {
996         return SUCCESS;
997 }
998
999 int
1000 sdioh_stop(sdioh_info_t *sd)
1001 {
1002         return SUCCESS;
1003 }
1004
1005 int
1006 sdioh_waitlockfree(sdioh_info_t *sd)
1007 {
1008         return SUCCESS;
1009 }
1010
1011
1012 /*
1013  * Private/Static work routines
1014  */
1015 static int
1016 bcmspi_host_init(sdioh_info_t *sd)
1017 {
1018
1019         /* Default power on mode */
1020         sd->sd_mode = SDIOH_MODE_SPI;
1021         sd->polled_mode = TRUE;
1022         sd->host_init_done = TRUE;
1023         sd->card_init_done = FALSE;
1024         sd->adapter_slot = 1;
1025
1026         return (SUCCESS);
1027 }
1028
1029 static int
1030 get_client_blocksize(sdioh_info_t *sd)
1031 {
1032         uint32 regdata[2];
1033         int status;
1034
1035         /* Find F1/F2/F3 max packet size */
1036         if ((status = bcmspi_card_regread(sd, 0, SPID_F1_INFO_REG,
1037                                          8, regdata)) != SUCCESS) {
1038                 return status;
1039         }
1040
1041         sd_trace(("pkt_size regdata[0] = 0x%x, regdata[1] = 0x%x\n",
1042                 regdata[0], regdata[1]));
1043
1044         sd->client_block_size[1] = (regdata[0] & F1_MAX_PKT_SIZE) >> 2;
1045         sd_trace(("Func1 blocksize = %d\n", sd->client_block_size[1]));
1046         ASSERT(sd->client_block_size[1] == BLOCK_SIZE_F1);
1047
1048         sd->client_block_size[2] = ((regdata[0] >> 16) & F2_MAX_PKT_SIZE) >> 2;
1049         sd_trace(("Func2 blocksize = %d\n", sd->client_block_size[2]));
1050         ASSERT(sd->client_block_size[2] == BLOCK_SIZE_F2);
1051
1052         sd->client_block_size[3] = (regdata[1] & F3_MAX_PKT_SIZE) >> 2;
1053         sd_trace(("Func3 blocksize = %d\n", sd->client_block_size[3]));
1054         ASSERT(sd->client_block_size[3] == BLOCK_SIZE_F3);
1055
1056         return 0;
1057 }
1058
1059 static int
1060 bcmspi_client_init(sdioh_info_t *sd)
1061 {
1062         uint32  status_en_reg = 0;
1063         sd_trace(("%s: Powering up slot %d\n", __FUNCTION__, sd->adapter_slot));
1064
1065 #ifndef BCMSPI_ANDROID
1066 #ifdef HSMODE
1067         if (!spi_start_clock(sd, (uint16)sd_divisor)) {
1068                 sd_err(("spi_start_clock failed\n"));
1069                 return ERROR;
1070         }
1071 #else
1072         /* Start at ~400KHz clock rate for initialization */
1073         if (!spi_start_clock(sd, 128)) {
1074                 sd_err(("spi_start_clock failed\n"));
1075                 return ERROR;
1076         }
1077 #endif /* HSMODE */
1078 #endif /* !BCMSPI_ANDROID */
1079
1080         if (!bcmspi_host_device_init_adapt(sd)) {
1081                 sd_err(("bcmspi_host_device_init_adapt failed\n"));
1082                 return ERROR;
1083         }
1084
1085         if (!bcmspi_test_card(sd)) {
1086                 sd_err(("bcmspi_test_card failed\n"));
1087                 return ERROR;
1088         }
1089
1090         sd->num_funcs = SPI_MAX_IOFUNCS;
1091
1092         get_client_blocksize(sd);
1093
1094         /* Apply resync pattern cmd with all zeros to reset spi-bkplane F1 logic */
1095         bcmspi_resync_f1(sd);
1096
1097         sd->dwordmode = FALSE;
1098
1099         bcmspi_card_regread(sd, 0, SPID_STATUS_ENABLE, 1, &status_en_reg);
1100
1101         sd_trace(("%s: Enabling interrupt with dstatus \n", __FUNCTION__));
1102         status_en_reg |= INTR_WITH_STATUS;
1103
1104         if (bcmspi_card_regwrite(sd, SPI_FUNC_0, SPID_STATUS_ENABLE, 1,
1105             status_en_reg & 0xff) != SUCCESS) {
1106                 sd_err(("%s: Unable to set response delay for all fun's.\n", __FUNCTION__));
1107                 return ERROR;
1108         }
1109
1110 #ifndef HSMODE
1111 #ifndef BCMSPI_ANDROID
1112         /* After configuring for High-Speed mode, set the desired clock rate. */
1113         if (!spi_start_clock(sd, 4)) {
1114                 sd_err(("spi_start_clock failed\n"));
1115                 return ERROR;
1116         }
1117 #endif /* !BCMSPI_ANDROID */
1118 #endif /* HSMODE */
1119
1120         /* check to see if the response delay needs to be programmed properly */
1121         {
1122                 uint32 f1_respdelay = 0;
1123                 bcmspi_card_regread(sd, 0, SPID_RESP_DELAY_F1, 1, &f1_respdelay);
1124                 if ((f1_respdelay == 0) || (f1_respdelay == 0xFF)) {
1125                         /* older sdiodevice core and has no separte resp delay for each of */
1126                         sd_err(("older corerev < 4 so use the same resp delay for all funcs\n"));
1127                         sd->resp_delay_new = FALSE;
1128                 }
1129                 else {
1130                         /* older sdiodevice core and has no separte resp delay for each of */
1131                         int ret_val;
1132                         sd->resp_delay_new = TRUE;
1133                         sd_err(("new corerev >= 4 so set the resp delay for each of the funcs\n"));
1134                         sd_trace(("resp delay for funcs f0(%d), f1(%d), f2(%d), f3(%d)\n",
1135                                 GSPI_F0_RESP_DELAY, GSPI_F1_RESP_DELAY,
1136                                 GSPI_F2_RESP_DELAY, GSPI_F3_RESP_DELAY));
1137                         ret_val = bcmspi_card_regwrite(sd, SPI_FUNC_0, SPID_RESP_DELAY_F0, 1,
1138                                 GSPI_F0_RESP_DELAY);
1139                         if (ret_val != SUCCESS) {
1140                                 sd_err(("%s: Unable to set response delay for F0\n", __FUNCTION__));
1141                                 return ERROR;
1142                         }
1143                         ret_val = bcmspi_card_regwrite(sd, SPI_FUNC_0, SPID_RESP_DELAY_F1, 1,
1144                                 GSPI_F1_RESP_DELAY);
1145                         if (ret_val != SUCCESS) {
1146                                 sd_err(("%s: Unable to set response delay for F1\n", __FUNCTION__));
1147                                 return ERROR;
1148                         }
1149                         ret_val = bcmspi_card_regwrite(sd, SPI_FUNC_0, SPID_RESP_DELAY_F2, 1,
1150                                 GSPI_F2_RESP_DELAY);
1151                         if (ret_val != SUCCESS) {
1152                                 sd_err(("%s: Unable to set response delay for F2\n", __FUNCTION__));
1153                                 return ERROR;
1154                         }
1155                         ret_val = bcmspi_card_regwrite(sd, SPI_FUNC_0, SPID_RESP_DELAY_F3, 1,
1156                                 GSPI_F3_RESP_DELAY);
1157                         if (ret_val != SUCCESS) {
1158                                 sd_err(("%s: Unable to set response delay for F2\n", __FUNCTION__));
1159                                 return ERROR;
1160                         }
1161                 }
1162         }
1163
1164
1165         sd->card_init_done = TRUE;
1166
1167         /* get the device rev to program the prop respdelays */
1168
1169         return SUCCESS;
1170 }
1171
1172 static int
1173 bcmspi_set_highspeed_mode(sdioh_info_t *sd, bool hsmode)
1174 {
1175         uint32 regdata;
1176         int status;
1177
1178         if ((status = bcmspi_card_regread(sd, 0, SPID_CONFIG,
1179                                          4, &regdata)) != SUCCESS)
1180                 return status;
1181
1182         sd_trace(("In %s spih-ctrl = 0x%x \n", __FUNCTION__, regdata));
1183
1184
1185         if (hsmode == TRUE) {
1186                 sd_trace(("Attempting to enable High-Speed mode.\n"));
1187
1188                 if (regdata & HIGH_SPEED_MODE) {
1189                         sd_trace(("Device is already in High-Speed mode.\n"));
1190                         return status;
1191                 } else {
1192                         regdata |= HIGH_SPEED_MODE;
1193                         sd_trace(("Writing %08x to device at %08x\n", regdata, SPID_CONFIG));
1194                         if ((status = bcmspi_card_regwrite(sd, 0, SPID_CONFIG,
1195                                                           4, regdata)) != SUCCESS) {
1196                                 return status;
1197                         }
1198                 }
1199         } else {
1200                 sd_trace(("Attempting to disable High-Speed mode.\n"));
1201
1202                 if (regdata & HIGH_SPEED_MODE) {
1203                         regdata &= ~HIGH_SPEED_MODE;
1204                         sd_trace(("Writing %08x to device at %08x\n", regdata, SPID_CONFIG));
1205                         if ((status = bcmspi_card_regwrite(sd, 0, SPID_CONFIG,
1206                                                           4, regdata)) != SUCCESS)
1207                                 return status;
1208                 }
1209                  else {
1210                         sd_trace(("Device is already in Low-Speed mode.\n"));
1211                         return status;
1212                 }
1213         }
1214 #ifndef BCMSPI_ANDROID
1215         spi_controller_highspeed_mode(sd, hsmode);
1216 #endif /* !BCMSPI_ANDROID */
1217
1218         return TRUE;
1219 }
1220
1221 #define bcmspi_find_curr_mode(sd) { \
1222         sd->wordlen = 2; \
1223         status = bcmspi_card_regread_fixedaddr(sd, 0, SPID_TEST_READ, 4, &regdata); \
1224         regdata &= 0xff; \
1225         if ((regdata == 0xad) || (regdata == 0x5b) || \
1226             (regdata == 0x5d) || (regdata == 0x5a)) \
1227                 break; \
1228         sd->wordlen = 4; \
1229         status = bcmspi_card_regread_fixedaddr(sd, 0, SPID_TEST_READ, 4, &regdata); \
1230         regdata &= 0xff; \
1231         if ((regdata == 0xad) || (regdata == 0x5b) || \
1232             (regdata == 0x5d) || (regdata == 0x5a)) \
1233                 break; \
1234         sd_trace(("Silicon testability issue: regdata = 0x%x." \
1235                 " Expected 0xad, 0x5a, 0x5b or 0x5d.\n", regdata)); \
1236         OSL_DELAY(100000); \
1237 }
1238
1239 #define INIT_ADAPT_LOOP         100
1240
1241 /* Adapt clock-phase-speed-bitwidth between host and device */
1242 static bool
1243 bcmspi_host_device_init_adapt(sdioh_info_t *sd)
1244 {
1245         uint32 wrregdata, regdata = 0;
1246         int status;
1247         int i;
1248
1249         /* Due to a silicon testability issue, the first command from the Host
1250          * to the device will get corrupted (first bit will be lost). So the
1251          * Host should poll the device with a safe read request. ie: The Host
1252          * should try to read F0 addr 0x14 using the Fixed address mode
1253          * (This will prevent a unintended write command to be detected by device)
1254          */
1255         for (i = 0; i < INIT_ADAPT_LOOP; i++) {
1256         /* If device was not power-cycled it will stay in 32bit mode with
1257          * response-delay-all bit set.  Alternate the iteration so that
1258          * read either with or without response-delay for F0 to succeed.
1259          */
1260                 bcmspi_find_curr_mode(sd);
1261                 sd->resp_delay_all = (i & 0x1) ? TRUE : FALSE;
1262
1263                 bcmspi_find_curr_mode(sd);
1264                 sd->dwordmode = TRUE;
1265
1266                 bcmspi_find_curr_mode(sd);
1267                 sd->dwordmode = FALSE;
1268         }
1269
1270         /* Bail out, device not detected */
1271         if (i == INIT_ADAPT_LOOP)
1272                 return FALSE;
1273
1274         /* Softreset the spid logic */
1275         if ((sd->dwordmode) || (sd->wordlen == 4)) {
1276                 bcmspi_card_regwrite(sd, 0, SPID_RESET_BP, 1, RESET_ON_WLAN_BP_RESET|RESET_SPI);
1277                 bcmspi_card_regread(sd, 0, SPID_RESET_BP, 1, &regdata);
1278                 sd_trace(("reset reg read = 0x%x\n", regdata));
1279                 sd_trace(("dwordmode = %d, wordlen = %d, resp_delay_all = %d\n", sd->dwordmode,
1280                        sd->wordlen, sd->resp_delay_all));
1281                 /* Restore default state after softreset */
1282                 sd->wordlen = 2;
1283                 sd->dwordmode = FALSE;
1284         }
1285
1286         if (sd->wordlen == 4) {
1287                 if ((status = bcmspi_card_regread(sd, 0, SPID_TEST_READ, 4, &regdata)) !=
1288                      SUCCESS)
1289                                 return FALSE;
1290                 if (regdata == TEST_RO_DATA_32BIT_LE) {
1291                         sd_trace(("Spid is already in 32bit LE mode. Value read = 0x%x\n",
1292                                   regdata));
1293                         sd_trace(("Spid power was left on.\n"));
1294                 } else {
1295                         sd_err(("Spid power was left on but signature read failed."
1296                                 " Value read = 0x%x\n", regdata));
1297                         return FALSE;
1298                 }
1299         } else {
1300                 sd->wordlen = 2;
1301
1302 #define CTRL_REG_DEFAULT        0x00010430 /* according to the host m/c */
1303
1304                 wrregdata = (CTRL_REG_DEFAULT);
1305
1306                 if ((status = bcmspi_card_regread(sd, 0, SPID_TEST_READ, 4, &regdata)) != SUCCESS)
1307                         return FALSE;
1308                 sd_trace(("(we are still in 16bit mode) 32bit READ LE regdata = 0x%x\n", regdata));
1309
1310 #ifndef HSMODE
1311                 wrregdata |= (CLOCK_PHASE | CLOCK_POLARITY);
1312                 wrregdata &= ~HIGH_SPEED_MODE;
1313                 bcmspi_card_regwrite(sd, 0, SPID_CONFIG, 4, wrregdata);
1314 #endif /* HSMODE */
1315
1316                 for (i = 0; i < INIT_ADAPT_LOOP; i++) {
1317                         if ((regdata == 0xfdda7d5b) || (regdata == 0xfdda7d5a)) {
1318                                 sd_trace(("0xfeedbead was leftshifted by 1-bit.\n"));
1319                                 if ((status = bcmspi_card_regread(sd, 0, SPID_TEST_READ, 4,
1320                                      &regdata)) != SUCCESS)
1321                                         return FALSE;
1322                         }
1323                         OSL_DELAY(1000);
1324                 }
1325
1326 #ifndef CUSTOMER_HW4
1327                 /* Change to host controller intr-polarity of active-low */
1328                 wrregdata &= ~INTR_POLARITY;
1329 #else
1330                 /* Change to host controller intr-polarity of active-high */
1331                 wrregdata |= INTR_POLARITY;
1332 #endif
1333                 sd_trace(("(we are still in 16bit mode) 32bit Write LE reg-ctrl-data = 0x%x\n",
1334                         wrregdata));
1335                 /* Change to 32bit mode */
1336                 wrregdata |= WORD_LENGTH_32;
1337                 bcmspi_card_regwrite(sd, 0, SPID_CONFIG, 4, wrregdata);
1338
1339                 /* Change command/data packaging in 32bit LE mode */
1340                 sd->wordlen = 4;
1341
1342                 if ((status = bcmspi_card_regread(sd, 0, SPID_TEST_READ, 4, &regdata)) != SUCCESS)
1343                         return FALSE;
1344
1345                 if (regdata == TEST_RO_DATA_32BIT_LE) {
1346                         sd_trace(("Read spid passed. Value read = 0x%x\n", regdata));
1347                         sd_trace(("Spid had power-on cycle OR spi was soft-resetted \n"));
1348                 } else {
1349                         sd_err(("Stale spid reg values read as it was kept powered. Value read ="
1350                           "0x%x\n", regdata));
1351                         return FALSE;
1352                 }
1353         }
1354
1355
1356         return TRUE;
1357 }
1358
1359 static bool
1360 bcmspi_test_card(sdioh_info_t *sd)
1361 {
1362         uint32 regdata;
1363         int status;
1364
1365         if ((status = bcmspi_card_regread(sd, 0, SPID_TEST_READ, 4, &regdata)) != SUCCESS)
1366                 return FALSE;
1367
1368         if (regdata == (TEST_RO_DATA_32BIT_LE))
1369                 sd_trace(("32bit LE regdata = 0x%x\n", regdata));
1370         else {
1371                 sd_trace(("Incorrect 32bit LE regdata = 0x%x\n", regdata));
1372                 return FALSE;
1373         }
1374
1375
1376 #define RW_PATTERN1     0xA0A1A2A3
1377 #define RW_PATTERN2     0x4B5B6B7B
1378
1379         regdata = RW_PATTERN1;
1380         if ((status = bcmspi_card_regwrite(sd, 0, SPID_TEST_RW, 4, regdata)) != SUCCESS)
1381                 return FALSE;
1382         regdata = 0;
1383         if ((status = bcmspi_card_regread(sd, 0, SPID_TEST_RW, 4, &regdata)) != SUCCESS)
1384                 return FALSE;
1385         if (regdata != RW_PATTERN1) {
1386                 sd_err(("Write-Read spid failed. Value wrote = 0x%x, Value read = 0x%x\n",
1387                         RW_PATTERN1, regdata));
1388                 return FALSE;
1389         } else
1390                 sd_trace(("R/W spid passed. Value read = 0x%x\n", regdata));
1391
1392         regdata = RW_PATTERN2;
1393         if ((status = bcmspi_card_regwrite(sd, 0, SPID_TEST_RW, 4, regdata)) != SUCCESS)
1394                 return FALSE;
1395         regdata = 0;
1396         if ((status = bcmspi_card_regread(sd, 0, SPID_TEST_RW, 4, &regdata)) != SUCCESS)
1397                 return FALSE;
1398         if (regdata != RW_PATTERN2) {
1399                 sd_err(("Write-Read spid failed. Value wrote = 0x%x, Value read = 0x%x\n",
1400                         RW_PATTERN2, regdata));
1401                 return FALSE;
1402         } else
1403                 sd_trace(("R/W spid passed. Value read = 0x%x\n", regdata));
1404
1405         return TRUE;
1406 }
1407
1408 static int
1409 bcmspi_driver_init(sdioh_info_t *sd)
1410 {
1411         sd_trace(("%s\n", __FUNCTION__));
1412         if ((bcmspi_host_init(sd)) != SUCCESS) {
1413                 return ERROR;
1414         }
1415
1416         if (bcmspi_client_init(sd) != SUCCESS) {
1417                 return ERROR;
1418         }
1419
1420         return SUCCESS;
1421 }
1422
1423 /* Read device reg */
1424 static int
1425 bcmspi_card_regread(sdioh_info_t *sd, int func, uint32 regaddr, int regsize, uint32 *data)
1426 {
1427         int status;
1428         uint32 cmd_arg, dstatus;
1429
1430         ASSERT(regsize);
1431
1432         if (func == 2)
1433                 sd_trace(("Reg access on F2 will generate error indication in dstatus bits.\n"));
1434
1435         cmd_arg = 0;
1436         cmd_arg = SFIELD(cmd_arg, SPI_RW_FLAG, 0);
1437         cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 1);       /* Incremental access */
1438         cmd_arg = SFIELD(cmd_arg, SPI_FUNCTION, func);
1439         cmd_arg = SFIELD(cmd_arg, SPI_REG_ADDR, regaddr);
1440         cmd_arg = SFIELD(cmd_arg, SPI_LEN, regsize == BLOCK_SIZE_F2 ? 0 : regsize);
1441
1442         sd_trace(("%s: RD cmd_arg=0x%x func=%d regaddr=0x%x regsize=%d\n",
1443                   __FUNCTION__, cmd_arg, func, regaddr, regsize));
1444
1445         if ((status = bcmspi_cmd_issue(sd, sd->sd_use_dma, cmd_arg, data, regsize)) != SUCCESS)
1446                 return status;
1447
1448         bcmspi_cmd_getdstatus(sd, &dstatus);
1449         if (dstatus)
1450                 sd_trace(("dstatus =0x%x\n", dstatus));
1451
1452         return SUCCESS;
1453 }
1454
1455 static int
1456 bcmspi_card_regread_fixedaddr(sdioh_info_t *sd, int func, uint32 regaddr, int regsize, uint32 *data)
1457 {
1458
1459         int status;
1460         uint32 cmd_arg;
1461         uint32 dstatus;
1462
1463         ASSERT(regsize);
1464
1465         if (func == 2)
1466                 sd_trace(("Reg access on F2 will generate error indication in dstatus bits.\n"));
1467
1468         cmd_arg = 0;
1469         cmd_arg = SFIELD(cmd_arg, SPI_RW_FLAG, 0);
1470         cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 0);       /* Fixed access */
1471         cmd_arg = SFIELD(cmd_arg, SPI_FUNCTION, func);
1472         cmd_arg = SFIELD(cmd_arg, SPI_REG_ADDR, regaddr);
1473         cmd_arg = SFIELD(cmd_arg, SPI_LEN, regsize);
1474
1475         sd_trace(("%s: RD cmd_arg=0x%x func=%d regaddr=0x%x regsize=%d\n",
1476                   __FUNCTION__, cmd_arg, func, regaddr, regsize));
1477
1478         if ((status = bcmspi_cmd_issue(sd, sd->sd_use_dma, cmd_arg, data, regsize)) != SUCCESS)
1479                 return status;
1480
1481         sd_trace(("%s: RD result=0x%x\n", __FUNCTION__, *data));
1482
1483         bcmspi_cmd_getdstatus(sd, &dstatus);
1484         sd_trace(("dstatus =0x%x\n", dstatus));
1485         return SUCCESS;
1486 }
1487
1488 /* write a device register */
1489 static int
1490 bcmspi_card_regwrite(sdioh_info_t *sd, int func, uint32 regaddr, int regsize, uint32 data)
1491 {
1492         int status;
1493         uint32 cmd_arg, dstatus;
1494
1495         ASSERT(regsize);
1496
1497         cmd_arg = 0;
1498
1499         cmd_arg = SFIELD(cmd_arg, SPI_RW_FLAG, 1);
1500         cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 1);       /* Incremental access */
1501         cmd_arg = SFIELD(cmd_arg, SPI_FUNCTION, func);
1502         cmd_arg = SFIELD(cmd_arg, SPI_REG_ADDR, regaddr);
1503         cmd_arg = SFIELD(cmd_arg, SPI_LEN, regsize == BLOCK_SIZE_F2 ? 0 : regsize);
1504
1505         sd_trace(("%s: WR cmd_arg=0x%x func=%d regaddr=0x%x regsize=%d data=0x%x\n",
1506                   __FUNCTION__, cmd_arg, func, regaddr, regsize, data));
1507
1508         if ((status = bcmspi_cmd_issue(sd, sd->sd_use_dma, cmd_arg, &data, regsize)) != SUCCESS)
1509                 return status;
1510
1511         bcmspi_cmd_getdstatus(sd, &dstatus);
1512         if (dstatus)
1513                 sd_trace(("dstatus=0x%x\n", dstatus));
1514
1515         return SUCCESS;
1516 }
1517
1518 /* write a device register - 1 byte */
1519 static int
1520 bcmspi_card_bytewrite(sdioh_info_t *sd, int func, uint32 regaddr, uint8 *byte)
1521 {
1522         int status;
1523         uint32 cmd_arg;
1524         uint32 dstatus;
1525         uint32 data = (uint32)(*byte);
1526
1527         cmd_arg = 0;
1528         cmd_arg = SFIELD(cmd_arg, SPI_FUNCTION, func);
1529         cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 1);       /* Incremental access */
1530         cmd_arg = SFIELD(cmd_arg, SPI_REG_ADDR, regaddr);
1531         cmd_arg = SFIELD(cmd_arg, SPI_RW_FLAG, 1);
1532         cmd_arg = SFIELD(cmd_arg, SPI_LEN, 1);
1533
1534         sd_trace(("%s: WR cmd_arg=0x%x func=%d regaddr=0x%x data=0x%x\n",
1535                   __FUNCTION__, cmd_arg, func, regaddr, data));
1536
1537         if ((status = bcmspi_cmd_issue(sd, sd->sd_use_dma, cmd_arg, &data, 1)) != SUCCESS)
1538                 return status;
1539
1540         bcmspi_cmd_getdstatus(sd, &dstatus);
1541         if (dstatus)
1542                 sd_trace(("dstatus =0x%x\n", dstatus));
1543
1544         return SUCCESS;
1545 }
1546
1547 void
1548 bcmspi_cmd_getdstatus(sdioh_info_t *sd, uint32 *dstatus_buffer)
1549 {
1550         *dstatus_buffer = sd->card_dstatus;
1551 }
1552
1553 /* 'data' is of type uint32 whereas other buffers are of type uint8 */
1554 static int
1555 bcmspi_cmd_issue(sdioh_info_t *sd, bool use_dma, uint32 cmd_arg,
1556                 uint32 *data, uint32 datalen)
1557 {
1558         uint32  i, j;
1559         uint8   resp_delay = 0;
1560         int     err = SUCCESS;
1561         uint32  hostlen;
1562         uint32 spilen = 0;
1563         uint32 dstatus_idx = 0;
1564         uint16 templen, buslen, len, *ptr = NULL;
1565
1566         sd_trace(("spi cmd = 0x%x\n", cmd_arg));
1567
1568         if (DWORDMODE_ON) {
1569                 spilen = GFIELD(cmd_arg, SPI_LEN);
1570                 if ((GFIELD(cmd_arg, SPI_FUNCTION) == SPI_FUNC_0) ||
1571                     (GFIELD(cmd_arg, SPI_FUNCTION) == SPI_FUNC_1))
1572                         dstatus_idx = spilen * 3;
1573
1574                 if ((GFIELD(cmd_arg, SPI_FUNCTION) == SPI_FUNC_2) &&
1575                     (GFIELD(cmd_arg, SPI_RW_FLAG) == 1)) {
1576                         spilen = spilen << 2;
1577                         dstatus_idx = (spilen % 16) ? (16 - (spilen % 16)) : 0;
1578                         /* convert len to mod16 size */
1579                         spilen = ROUNDUP(spilen, 16);
1580                         cmd_arg = SFIELD(cmd_arg, SPI_LEN, (spilen >> 2));
1581                 }
1582         }
1583
1584         /* Set up and issue the SPI command.  MSByte goes out on bus first.  Increase datalen
1585          * according to the wordlen mode(16/32bit) the device is in.
1586          */
1587         if (sd->wordlen == 4) { /* 32bit spid */
1588                 *(uint32 *)spi_outbuf = SPISWAP_WD4(cmd_arg);
1589                 if (datalen & 0x3)
1590                         datalen += (4 - (datalen & 0x3));
1591         } else if (sd->wordlen == 2) { /* 16bit spid */
1592                 *(uint32 *)spi_outbuf = SPISWAP_WD2(cmd_arg);
1593                 if (datalen & 0x1)
1594                         datalen++;
1595                 if (datalen < 4)
1596                         datalen = ROUNDUP(datalen, 4);
1597         } else {
1598                 sd_err(("Host is %d bit spid, could not create SPI command.\n",
1599                         8 * sd->wordlen));
1600                 return ERROR;
1601         }
1602
1603         /* for Write, put the data into the output buffer */
1604         if (GFIELD(cmd_arg, SPI_RW_FLAG) == 1) {
1605                 /* We send len field of hw-header always a mod16 size, both from host and dongle */
1606                 if (DWORDMODE_ON) {
1607                         if (GFIELD(cmd_arg, SPI_FUNCTION) == SPI_FUNC_2) {
1608                                 ptr = (uint16 *)&data[0];
1609                                 templen = *ptr;
1610                                 /* ASSERT(*ptr == ~*(ptr + 1)); */
1611                                 templen = ROUNDUP(templen, 16);
1612                                 *ptr = templen;
1613                                 sd_trace(("actual tx len = %d\n", (uint16)(~*(ptr+1))));
1614                         }
1615                 }
1616
1617                 if (datalen != 0) {
1618                         for (i = 0; i < datalen/4; i++) {
1619                                 if (sd->wordlen == 4) { /* 32bit spid */
1620                                         *(uint32 *)&spi_outbuf[i * 4 + CMDLEN] =
1621                                                 SPISWAP_WD4(data[i]);
1622                                 } else if (sd->wordlen == 2) { /* 16bit spid */
1623                                         *(uint32 *)&spi_outbuf[i * 4 + CMDLEN] =
1624                                                 SPISWAP_WD2(data[i]);
1625                                 }
1626                         }
1627                 }
1628         }
1629
1630         /* Append resp-delay number of bytes and clock them out for F0/1/2 reads. */
1631         if ((GFIELD(cmd_arg, SPI_RW_FLAG) == 0)) {
1632                 int func = GFIELD(cmd_arg, SPI_FUNCTION);
1633                 switch (func) {
1634                         case 0:
1635                                 if (sd->resp_delay_new)
1636                                         resp_delay = GSPI_F0_RESP_DELAY;
1637                                 else
1638                                         resp_delay = sd->resp_delay_all ? F0_RESPONSE_DELAY : 0;
1639                                 break;
1640                         case 1:
1641                                 if (sd->resp_delay_new)
1642                                         resp_delay = GSPI_F1_RESP_DELAY;
1643                                 else
1644                                         resp_delay = F1_RESPONSE_DELAY;
1645                                 break;
1646                         case 2:
1647                                 if (sd->resp_delay_new)
1648                                         resp_delay = GSPI_F2_RESP_DELAY;
1649                                 else
1650                                         resp_delay = sd->resp_delay_all ? F2_RESPONSE_DELAY : 0;
1651                                 break;
1652                         default:
1653                                 ASSERT(0);
1654                                 break;
1655                 }
1656                 /* Program response delay */
1657                 if (sd->resp_delay_new == FALSE)
1658                         bcmspi_prog_resp_delay(sd, func, resp_delay);
1659         }
1660
1661         /* +4 for cmd and +4 for dstatus */
1662         hostlen = datalen + 8 + resp_delay;
1663         hostlen += dstatus_idx;
1664 #ifdef BCMSPI_ANDROID
1665         if (hostlen%4) {
1666                 sd_err(("Unaligned data len %d, hostlen %d\n",
1667                         datalen, hostlen));
1668 #endif /* BCMSPI_ANDROID */
1669         hostlen += (4 - (hostlen & 0x3));
1670 #ifdef BCMSPI_ANDROID
1671         }
1672 #endif /* BCMSPI_ANDROID */
1673         spi_sendrecv(sd, spi_outbuf, spi_inbuf, hostlen);
1674
1675         /* for Read, get the data into the input buffer */
1676         if (datalen != 0) {
1677                 if (GFIELD(cmd_arg, SPI_RW_FLAG) == 0) { /* if read cmd */
1678                         for (j = 0; j < datalen/4; j++) {
1679                                 if (sd->wordlen == 4) { /* 32bit spid */
1680                                         data[j] = SPISWAP_WD4(*(uint32 *)&spi_inbuf[j * 4 +
1681                                                     CMDLEN + resp_delay]);
1682                                 } else if (sd->wordlen == 2) { /* 16bit spid */
1683                                         data[j] = SPISWAP_WD2(*(uint32 *)&spi_inbuf[j * 4 +
1684                                                     CMDLEN + resp_delay]);
1685                                 }
1686                         }
1687
1688                         if ((DWORDMODE_ON) && (GFIELD(cmd_arg, SPI_FUNCTION) == SPI_FUNC_2)) {
1689                                 ptr = (uint16 *)&data[0];
1690                                 templen = *ptr;
1691                                 buslen = len = ~(*(ptr + 1));
1692                                 buslen = ROUNDUP(buslen, 16);
1693                                 /* populate actual len in hw-header */
1694                                 if (templen == buslen)
1695                                         *ptr = len;
1696                         }
1697                 }
1698         }
1699
1700         /* Restore back the len field of the hw header */
1701         if (DWORDMODE_ON) {
1702                 if ((GFIELD(cmd_arg, SPI_FUNCTION) == SPI_FUNC_2) &&
1703                     (GFIELD(cmd_arg, SPI_RW_FLAG) == 1)) {
1704                         ptr = (uint16 *)&data[0];
1705                         *ptr = (uint16)(~*(ptr+1));
1706                 }
1707         }
1708
1709         dstatus_idx += (datalen + CMDLEN + resp_delay);
1710         /* Last 4bytes are dstatus.  Device is configured to return status bits. */
1711         if (sd->wordlen == 4) { /* 32bit spid */
1712                 sd->card_dstatus = SPISWAP_WD4(*(uint32 *)&spi_inbuf[dstatus_idx]);
1713         } else if (sd->wordlen == 2) { /* 16bit spid */
1714                 sd->card_dstatus = SPISWAP_WD2(*(uint32 *)&spi_inbuf[dstatus_idx]);
1715         } else {
1716                 sd_err(("Host is %d bit machine, could not read SPI dstatus.\n",
1717                         8 * sd->wordlen));
1718                 return ERROR;
1719         }
1720         if (sd->card_dstatus == 0xffffffff) {
1721                 sd_err(("looks like not a GSPI device or device is not powered.\n"));
1722         }
1723
1724         err = bcmspi_update_stats(sd, cmd_arg);
1725
1726         return err;
1727
1728 }
1729
1730 static int
1731 bcmspi_card_buf(sdioh_info_t *sd, int rw, int func, bool fifo,
1732                 uint32 addr, int nbytes, uint32 *data)
1733 {
1734         int status;
1735         uint32 cmd_arg;
1736         bool write = rw == SDIOH_READ ? 0 : 1;
1737         uint retries = 0;
1738
1739         bool enable;
1740         uint32  spilen;
1741
1742         cmd_arg = 0;
1743
1744         ASSERT(nbytes);
1745         ASSERT(nbytes <= sd->client_block_size[func]);
1746
1747         if (write) sd->t_cnt++; else sd->r_cnt++;
1748
1749         if (func == 2) {
1750                 /* Frame len check limited by gSPI. */
1751                 if ((nbytes > 2000) && write) {
1752                         sd_trace((">2KB write: F2 wr of %d bytes\n", nbytes));
1753                 }
1754                 /* ASSERT(nbytes <= 2048); Fix bigger len gspi issue and uncomment. */
1755                 /* If F2 fifo on device is not ready to receive data, don't do F2 transfer */
1756                 if (write) {
1757                         uint32 dstatus;
1758                         /* check F2 ready with cached one */
1759                         bcmspi_cmd_getdstatus(sd, &dstatus);
1760                         if ((dstatus & STATUS_F2_RX_READY) == 0) {
1761                                 retries = WAIT_F2RXFIFORDY;
1762                                 enable = 0;
1763                                 while (retries-- && !enable) {
1764                                         OSL_DELAY(WAIT_F2RXFIFORDY_DELAY * 1000);
1765                                         bcmspi_card_regread(sd, SPI_FUNC_0, SPID_STATUS_REG, 4,
1766                                                            &dstatus);
1767                                         if (dstatus & STATUS_F2_RX_READY)
1768                                                 enable = TRUE;
1769                                 }
1770                                 if (!enable) {
1771                                         struct spierrstats_t *spierrstats = &sd->spierrstats;
1772                                         spierrstats->f2rxnotready++;
1773                                         sd_err(("F2 FIFO is not ready to receive data.\n"));
1774                                         return ERROR;
1775                                 }
1776                                 sd_trace(("No of retries on F2 ready %d\n",
1777                                         (WAIT_F2RXFIFORDY - retries)));
1778                         }
1779                 }
1780         }
1781
1782         /* F2 transfers happen on 0 addr */
1783         addr = (func == 2) ? 0 : addr;
1784
1785         /* In pio mode buffer is read using fixed address fifo in func 1 */
1786         if ((func == 1) && (fifo))
1787                 cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 0);
1788         else
1789                 cmd_arg = SFIELD(cmd_arg, SPI_ACCESS, 1);
1790
1791         cmd_arg = SFIELD(cmd_arg, SPI_FUNCTION, func);
1792         cmd_arg = SFIELD(cmd_arg, SPI_REG_ADDR, addr);
1793         cmd_arg = SFIELD(cmd_arg, SPI_RW_FLAG, write);
1794         spilen = sd->data_xfer_count = MIN(sd->client_block_size[func], nbytes);
1795         if ((sd->dwordmode == TRUE) && (GFIELD(cmd_arg, SPI_FUNCTION) == SPI_FUNC_2)) {
1796                 /* convert len to mod4 size */
1797                 spilen = spilen + ((spilen & 0x3) ? (4 - (spilen & 0x3)): 0);
1798                 cmd_arg = SFIELD(cmd_arg, SPI_LEN, (spilen >> 2));
1799         } else
1800                 cmd_arg = SFIELD(cmd_arg, SPI_LEN, spilen);
1801
1802         if ((func == 2) && (fifo == 1)) {
1803                 sd_data(("%s: %s func %d, %s, addr 0x%x, len %d bytes, r_cnt %d t_cnt %d\n",
1804                           __FUNCTION__, write ? "Wr" : "Rd", func, "INCR",
1805                           addr, nbytes, sd->r_cnt, sd->t_cnt));
1806         }
1807
1808         sd_trace(("%s cmd_arg = 0x%x\n", __FUNCTION__, cmd_arg));
1809         sd_data(("%s: %s func %d, %s, addr 0x%x, len %d bytes, r_cnt %d t_cnt %d\n",
1810                  __FUNCTION__, write ? "Wd" : "Rd", func, "INCR",
1811                  addr, nbytes, sd->r_cnt, sd->t_cnt));
1812
1813
1814         if ((status = bcmspi_cmd_issue(sd, sd->sd_use_dma, cmd_arg, data, nbytes)) != SUCCESS) {
1815                 sd_err(("%s: cmd_issue failed for %s\n", __FUNCTION__,
1816                         (write ? "write" : "read")));
1817                 return status;
1818         }
1819
1820         /* gSPI expects that hw-header-len is equal to spi-command-len */
1821         if ((func == 2) && (rw == SDIOH_WRITE) && (sd->dwordmode == FALSE)) {
1822                 ASSERT((uint16)sd->data_xfer_count == (uint16)(*data & 0xffff));
1823                 ASSERT((uint16)sd->data_xfer_count == (uint16)(~((*data & 0xffff0000) >> 16)));
1824         }
1825
1826         if ((nbytes > 2000) && !write) {
1827                 sd_trace((">2KB read: F2 rd of %d bytes\n", nbytes));
1828         }
1829
1830         return SUCCESS;
1831 }
1832
1833 /* Reset and re-initialize the device */
1834 int
1835 sdioh_sdio_reset(sdioh_info_t *si)
1836 {
1837         si->card_init_done = FALSE;
1838         return bcmspi_client_init(si);
1839 }
1840
1841 SDIOH_API_RC
1842 sdioh_gpioouten(sdioh_info_t *sd, uint32 gpio)
1843 {
1844         return SDIOH_API_RC_FAIL;
1845 }
1846
1847 SDIOH_API_RC
1848 sdioh_gpioout(sdioh_info_t *sd, uint32 gpio, bool enab)
1849 {
1850         return SDIOH_API_RC_FAIL;
1851 }
1852
1853 bool
1854 sdioh_gpioin(sdioh_info_t *sd, uint32 gpio)
1855 {
1856         return FALSE;
1857 }
1858
1859 SDIOH_API_RC
1860 sdioh_gpio_init(sdioh_info_t *sd)
1861 {
1862         return SDIOH_API_RC_FAIL;
1863 }