bcmspibrcm.c 51 KB

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