bcmsdspi.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. /*
  2. * Broadcom BCMSDH to SPI Protocol Conversion Layer
  3. *
  4. * Copyright (C) 1999-2010, 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: bcmsdspi.c,v 1.14.4.2.4.4.6.5 2010/03/10 03:09:48 Exp $
  25. */
  26. #include <typedefs.h>
  27. #include <bcmdevs.h>
  28. #include <bcmendian.h>
  29. #include <bcmutils.h>
  30. #include <osl.h>
  31. #include <siutils.h>
  32. #include <sdio.h> /* SDIO Device and Protocol Specs */
  33. #include <sdioh.h> /* SDIO Host Controller Specification */
  34. #include <bcmsdbus.h> /* bcmsdh to/from specific controller APIs */
  35. #include <sdiovar.h> /* ioctl/iovars */
  36. #include <pcicfg.h>
  37. #include <bcmsdspi.h>
  38. #include <bcmspi.h>
  39. #include <proto/sdspi.h>
  40. #define SD_PAGE 4096
  41. /* Globals */
  42. uint sd_msglevel = SDH_ERROR_VAL;
  43. uint sd_hiok = FALSE; /* Use hi-speed mode if available? */
  44. uint sd_sdmode = SDIOH_MODE_SPI; /* Use SD4 mode by default */
  45. uint sd_f2_blocksize = 512; /* Default blocksize */
  46. uint sd_divisor = 2; /* Default 33MHz/2 = 16MHz for dongle */
  47. uint sd_power = 1; /* Default to SD Slot powered ON */
  48. uint sd_clock = 1; /* Default to SD Clock turned ON */
  49. uint sd_crc = 0; /* Default to SPI CRC Check turned OFF */
  50. uint sd_pci_slot = 0xFFFFffff; /* Used to force selection of a particular PCI slot */
  51. uint sd_toctl = 7;
  52. /* Prototypes */
  53. static bool sdspi_start_power(sdioh_info_t *sd);
  54. static int sdspi_set_highspeed_mode(sdioh_info_t *sd, bool HSMode);
  55. static int sdspi_card_enablefuncs(sdioh_info_t *sd);
  56. static void sdspi_cmd_getrsp(sdioh_info_t *sd, uint32 *rsp_buffer, int count);
  57. static int sdspi_cmd_issue(sdioh_info_t *sd, bool use_dma, uint32 cmd, uint32 arg,
  58. uint32 *data, uint32 datalen);
  59. static int sdspi_card_regread(sdioh_info_t *sd, int func, uint32 regaddr,
  60. int regsize, uint32 *data);
  61. static int sdspi_card_regwrite(sdioh_info_t *sd, int func, uint32 regaddr,
  62. int regsize, uint32 data);
  63. static int sdspi_driver_init(sdioh_info_t *sd);
  64. static bool sdspi_reset(sdioh_info_t *sd, bool host_reset, bool client_reset);
  65. static int sdspi_card_buf(sdioh_info_t *sd, int rw, int func, bool fifo,
  66. uint32 addr, int nbytes, uint32 *data);
  67. static int sdspi_abort(sdioh_info_t *sd, uint func);
  68. static int set_client_block_size(sdioh_info_t *sd, int func, int blocksize);
  69. static uint8 sdspi_crc7(unsigned char* p, uint32 len);
  70. static uint16 sdspi_crc16(unsigned char* p, uint32 len);
  71. static int sdspi_crc_onoff(sdioh_info_t *sd, bool use_crc);
  72. /*
  73. * Public entry points & extern's
  74. */
  75. extern sdioh_info_t *
  76. sdioh_attach(osl_t *osh, void *bar0, uint irq)
  77. {
  78. sdioh_info_t *sd;
  79. sd_trace(("%s\n", __FUNCTION__));
  80. if ((sd = (sdioh_info_t *)MALLOC(osh, sizeof(sdioh_info_t))) == NULL) {
  81. sd_err(("sdioh_attach: out of memory, malloced %d bytes\n", MALLOCED(osh)));
  82. return NULL;
  83. }
  84. bzero((char *)sd, sizeof(sdioh_info_t));
  85. sd->osh = osh;
  86. if (spi_osinit(sd) != 0) {
  87. sd_err(("%s: spi_osinit() failed\n", __FUNCTION__));
  88. MFREE(sd->osh, sd, sizeof(sdioh_info_t));
  89. return NULL;
  90. }
  91. sd->bar0 = (uintptr)bar0;
  92. sd->irq = irq;
  93. sd->intr_handler = NULL;
  94. sd->intr_handler_arg = NULL;
  95. sd->intr_handler_valid = FALSE;
  96. /* Set defaults */
  97. sd->sd_blockmode = FALSE;
  98. sd->use_client_ints = TRUE;
  99. sd->sd_use_dma = FALSE; /* DMA Not supported */
  100. /* Haven't figured out how to make bytemode work with dma */
  101. if (!sd->sd_blockmode)
  102. sd->sd_use_dma = 0;
  103. if (!spi_hw_attach(sd)) {
  104. sd_err(("%s: spi_hw_attach() failed\n", __FUNCTION__));
  105. spi_osfree(sd);
  106. MFREE(sd->osh, sd, sizeof(sdioh_info_t));
  107. return NULL;
  108. }
  109. if (sdspi_driver_init(sd) != SUCCESS) {
  110. if (sdspi_driver_init(sd) != SUCCESS) {
  111. sd_err(("%s:sdspi_driver_init() failed()\n", __FUNCTION__));
  112. spi_hw_detach(sd);
  113. spi_osfree(sd);
  114. MFREE(sd->osh, sd, sizeof(sdioh_info_t));
  115. return (NULL);
  116. }
  117. }
  118. if (spi_register_irq(sd, irq) != SUCCESS) {
  119. sd_err(("%s: spi_register_irq() failed for irq = %d\n", __FUNCTION__, irq));
  120. spi_hw_detach(sd);
  121. spi_osfree(sd);
  122. MFREE(sd->osh, sd, sizeof(sdioh_info_t));
  123. return (NULL);
  124. }
  125. sd_trace(("%s: Done\n", __FUNCTION__));
  126. return sd;
  127. }
  128. extern SDIOH_API_RC
  129. sdioh_detach(osl_t *osh, sdioh_info_t *sd)
  130. {
  131. sd_trace(("%s\n", __FUNCTION__));
  132. if (sd) {
  133. if (sd->card_init_done)
  134. sdspi_reset(sd, 1, 1);
  135. sd_info(("%s: detaching from hardware\n", __FUNCTION__));
  136. spi_free_irq(sd->irq, sd);
  137. spi_hw_detach(sd);
  138. spi_osfree(sd);
  139. MFREE(sd->osh, sd, sizeof(sdioh_info_t));
  140. }
  141. return SDIOH_API_RC_SUCCESS;
  142. }
  143. /* Configure callback to client when we recieve client interrupt */
  144. extern SDIOH_API_RC
  145. sdioh_interrupt_register(sdioh_info_t *sd, sdioh_cb_fn_t fn, void *argh)
  146. {
  147. sd_trace(("%s: Entering\n", __FUNCTION__));
  148. sd->intr_handler = fn;
  149. sd->intr_handler_arg = argh;
  150. sd->intr_handler_valid = TRUE;
  151. return SDIOH_API_RC_SUCCESS;
  152. }
  153. extern SDIOH_API_RC
  154. sdioh_interrupt_deregister(sdioh_info_t *sd)
  155. {
  156. sd_trace(("%s: Entering\n", __FUNCTION__));
  157. sd->intr_handler_valid = FALSE;
  158. sd->intr_handler = NULL;
  159. sd->intr_handler_arg = NULL;
  160. return SDIOH_API_RC_SUCCESS;
  161. }
  162. extern SDIOH_API_RC
  163. sdioh_interrupt_query(sdioh_info_t *sd, bool *onoff)
  164. {
  165. sd_trace(("%s: Entering\n", __FUNCTION__));
  166. *onoff = sd->client_intr_enabled;
  167. return SDIOH_API_RC_SUCCESS;
  168. }
  169. #if defined(DHD_DEBUG)
  170. extern bool
  171. sdioh_interrupt_pending(sdioh_info_t *sd)
  172. {
  173. return 0;
  174. }
  175. #endif
  176. uint
  177. sdioh_query_iofnum(sdioh_info_t *sd)
  178. {
  179. return sd->num_funcs;
  180. }
  181. /* IOVar table */
  182. enum {
  183. IOV_MSGLEVEL = 1,
  184. IOV_BLOCKMODE,
  185. IOV_BLOCKSIZE,
  186. IOV_DMA,
  187. IOV_USEINTS,
  188. IOV_NUMINTS,
  189. IOV_NUMLOCALINTS,
  190. IOV_HOSTREG,
  191. IOV_DEVREG,
  192. IOV_DIVISOR,
  193. IOV_SDMODE,
  194. IOV_HISPEED,
  195. IOV_HCIREGS,
  196. IOV_POWER,
  197. IOV_CLOCK,
  198. IOV_CRC
  199. };
  200. const bcm_iovar_t sdioh_iovars[] = {
  201. {"sd_msglevel", IOV_MSGLEVEL, 0, IOVT_UINT32, 0 },
  202. {"sd_blockmode", IOV_BLOCKMODE, 0, IOVT_BOOL, 0 },
  203. {"sd_blocksize", IOV_BLOCKSIZE, 0, IOVT_UINT32, 0 }, /* ((fn << 16) | size) */
  204. {"sd_dma", IOV_DMA, 0, IOVT_BOOL, 0 },
  205. {"sd_ints", IOV_USEINTS, 0, IOVT_BOOL, 0 },
  206. {"sd_numints", IOV_NUMINTS, 0, IOVT_UINT32, 0 },
  207. {"sd_numlocalints", IOV_NUMLOCALINTS, 0, IOVT_UINT32, 0 },
  208. {"sd_hostreg", IOV_HOSTREG, 0, IOVT_BUFFER, sizeof(sdreg_t) },
  209. {"sd_devreg", IOV_DEVREG, 0, IOVT_BUFFER, sizeof(sdreg_t) },
  210. {"sd_divisor", IOV_DIVISOR, 0, IOVT_UINT32, 0 },
  211. {"sd_power", IOV_POWER, 0, IOVT_UINT32, 0 },
  212. {"sd_clock", IOV_CLOCK, 0, IOVT_UINT32, 0 },
  213. {"sd_crc", IOV_CRC, 0, IOVT_UINT32, 0 },
  214. {"sd_mode", IOV_SDMODE, 0, IOVT_UINT32, 100},
  215. {"sd_highspeed", IOV_HISPEED, 0, IOVT_UINT32, 0},
  216. {NULL, 0, 0, 0, 0 }
  217. };
  218. int
  219. sdioh_iovar_op(sdioh_info_t *si, const char *name,
  220. void *params, int plen, void *arg, int len, bool set)
  221. {
  222. const bcm_iovar_t *vi = NULL;
  223. int bcmerror = 0;
  224. int val_size;
  225. int32 int_val = 0;
  226. bool bool_val;
  227. uint32 actionid;
  228. ASSERT(name);
  229. ASSERT(len >= 0);
  230. /* Get must have return space; Set does not take qualifiers */
  231. ASSERT(set || (arg && len));
  232. ASSERT(!set || (!params && !plen));
  233. sd_trace(("%s: Enter (%s %s)\n", __FUNCTION__, (set ? "set" : "get"), name));
  234. if ((vi = bcm_iovar_lookup(sdioh_iovars, name)) == NULL) {
  235. bcmerror = BCME_UNSUPPORTED;
  236. goto exit;
  237. }
  238. if ((bcmerror = bcm_iovar_lencheck(vi, arg, len, set)) != 0)
  239. goto exit;
  240. /* Set up params so get and set can share the convenience variables */
  241. if (params == NULL) {
  242. params = arg;
  243. plen = len;
  244. }
  245. if (vi->type == IOVT_VOID)
  246. val_size = 0;
  247. else if (vi->type == IOVT_BUFFER)
  248. val_size = len;
  249. else
  250. val_size = sizeof(int);
  251. if (plen >= (int)sizeof(int_val))
  252. bcopy(params, &int_val, sizeof(int_val));
  253. bool_val = (int_val != 0) ? TRUE : FALSE;
  254. actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
  255. switch (actionid) {
  256. case IOV_GVAL(IOV_MSGLEVEL):
  257. int_val = (int32)sd_msglevel;
  258. bcopy(&int_val, arg, val_size);
  259. break;
  260. case IOV_SVAL(IOV_MSGLEVEL):
  261. sd_msglevel = int_val;
  262. break;
  263. case IOV_GVAL(IOV_BLOCKMODE):
  264. int_val = (int32)si->sd_blockmode;
  265. bcopy(&int_val, arg, val_size);
  266. break;
  267. case IOV_SVAL(IOV_BLOCKMODE):
  268. si->sd_blockmode = (bool)int_val;
  269. /* Haven't figured out how to make non-block mode with DMA */
  270. if (!si->sd_blockmode)
  271. si->sd_use_dma = 0;
  272. break;
  273. case IOV_GVAL(IOV_BLOCKSIZE):
  274. if ((uint32)int_val > si->num_funcs) {
  275. bcmerror = BCME_BADARG;
  276. break;
  277. }
  278. int_val = (int32)si->client_block_size[int_val];
  279. bcopy(&int_val, arg, val_size);
  280. break;
  281. case IOV_SVAL(IOV_BLOCKSIZE):
  282. {
  283. uint func = ((uint32)int_val >> 16);
  284. uint blksize = (uint16)int_val;
  285. uint maxsize;
  286. if (func > si->num_funcs) {
  287. bcmerror = BCME_BADARG;
  288. break;
  289. }
  290. switch (func) {
  291. case 0: maxsize = 32; break;
  292. case 1: maxsize = BLOCK_SIZE_4318; break;
  293. case 2: maxsize = BLOCK_SIZE_4328; break;
  294. default: maxsize = 0;
  295. }
  296. if (blksize > maxsize) {
  297. bcmerror = BCME_BADARG;
  298. break;
  299. }
  300. if (!blksize) {
  301. blksize = maxsize;
  302. }
  303. /* Now set it */
  304. spi_lock(si);
  305. bcmerror = set_client_block_size(si, func, blksize);
  306. spi_unlock(si);
  307. break;
  308. }
  309. case IOV_GVAL(IOV_DMA):
  310. int_val = (int32)si->sd_use_dma;
  311. bcopy(&int_val, arg, val_size);
  312. break;
  313. case IOV_SVAL(IOV_DMA):
  314. si->sd_use_dma = (bool)int_val;
  315. break;
  316. case IOV_GVAL(IOV_USEINTS):
  317. int_val = (int32)si->use_client_ints;
  318. bcopy(&int_val, arg, val_size);
  319. break;
  320. case IOV_SVAL(IOV_USEINTS):
  321. break;
  322. case IOV_GVAL(IOV_DIVISOR):
  323. int_val = (uint32)sd_divisor;
  324. bcopy(&int_val, arg, val_size);
  325. break;
  326. case IOV_SVAL(IOV_DIVISOR):
  327. sd_divisor = int_val;
  328. if (!spi_start_clock(si, (uint16)sd_divisor)) {
  329. sd_err(("set clock failed!\n"));
  330. bcmerror = BCME_ERROR;
  331. }
  332. break;
  333. case IOV_GVAL(IOV_POWER):
  334. int_val = (uint32)sd_power;
  335. bcopy(&int_val, arg, val_size);
  336. break;
  337. case IOV_SVAL(IOV_POWER):
  338. sd_power = int_val;
  339. break;
  340. case IOV_GVAL(IOV_CLOCK):
  341. int_val = (uint32)sd_clock;
  342. bcopy(&int_val, arg, val_size);
  343. break;
  344. case IOV_SVAL(IOV_CLOCK):
  345. sd_clock = int_val;
  346. break;
  347. case IOV_GVAL(IOV_CRC):
  348. int_val = (uint32)sd_crc;
  349. bcopy(&int_val, arg, val_size);
  350. break;
  351. case IOV_SVAL(IOV_CRC):
  352. /* Apply new setting, but don't change sd_crc until
  353. * after the CRC-mode is selected in the device. This
  354. * is required because the software must generate a
  355. * correct CRC for the CMD59 in order to be able to
  356. * turn OFF the CRC.
  357. */
  358. sdspi_crc_onoff(si, int_val ? 1 : 0);
  359. sd_crc = int_val;
  360. break;
  361. case IOV_GVAL(IOV_SDMODE):
  362. int_val = (uint32)sd_sdmode;
  363. bcopy(&int_val, arg, val_size);
  364. break;
  365. case IOV_SVAL(IOV_SDMODE):
  366. sd_sdmode = int_val;
  367. break;
  368. case IOV_GVAL(IOV_HISPEED):
  369. int_val = (uint32)sd_hiok;
  370. bcopy(&int_val, arg, val_size);
  371. break;
  372. case IOV_SVAL(IOV_HISPEED):
  373. sd_hiok = int_val;
  374. if (!sdspi_set_highspeed_mode(si, (bool)sd_hiok)) {
  375. sd_err(("Failed changing highspeed mode to %d.\n", sd_hiok));
  376. bcmerror = BCME_ERROR;
  377. return ERROR;
  378. }
  379. break;
  380. case IOV_GVAL(IOV_NUMINTS):
  381. int_val = (int32)si->intrcount;
  382. bcopy(&int_val, arg, val_size);
  383. break;
  384. case IOV_GVAL(IOV_NUMLOCALINTS):
  385. int_val = (int32)si->local_intrcount;
  386. bcopy(&int_val, arg, val_size);
  387. break;
  388. case IOV_GVAL(IOV_HOSTREG):
  389. {
  390. break;
  391. }
  392. case IOV_SVAL(IOV_HOSTREG):
  393. {
  394. sd_err(("IOV_HOSTREG unsupported\n"));
  395. break;
  396. }
  397. case IOV_GVAL(IOV_DEVREG):
  398. {
  399. sdreg_t *sd_ptr = (sdreg_t *)params;
  400. uint8 data;
  401. if (sdioh_cfg_read(si, sd_ptr->func, sd_ptr->offset, &data)) {
  402. bcmerror = BCME_SDIO_ERROR;
  403. break;
  404. }
  405. int_val = (int)data;
  406. bcopy(&int_val, arg, sizeof(int_val));
  407. break;
  408. }
  409. case IOV_SVAL(IOV_DEVREG):
  410. {
  411. sdreg_t *sd_ptr = (sdreg_t *)params;
  412. uint8 data = (uint8)sd_ptr->value;
  413. if (sdioh_cfg_write(si, sd_ptr->func, sd_ptr->offset, &data)) {
  414. bcmerror = BCME_SDIO_ERROR;
  415. break;
  416. }
  417. break;
  418. }
  419. default:
  420. bcmerror = BCME_UNSUPPORTED;
  421. break;
  422. }
  423. exit:
  424. return bcmerror;
  425. }
  426. extern SDIOH_API_RC
  427. sdioh_cfg_read(sdioh_info_t *sd, uint fnc_num, uint32 addr, uint8 *data)
  428. {
  429. SDIOH_API_RC status;
  430. /* No lock needed since sdioh_request_byte does locking */
  431. status = sdioh_request_byte(sd, SDIOH_READ, fnc_num, addr, data);
  432. return status;
  433. }
  434. extern SDIOH_API_RC
  435. sdioh_cfg_write(sdioh_info_t *sd, uint fnc_num, uint32 addr, uint8 *data)
  436. {
  437. /* No lock needed since sdioh_request_byte does locking */
  438. SDIOH_API_RC status;
  439. status = sdioh_request_byte(sd, SDIOH_WRITE, fnc_num, addr, data);
  440. return status;
  441. }
  442. extern SDIOH_API_RC
  443. sdioh_cis_read(sdioh_info_t *sd, uint func, uint8 *cisd, uint32 length)
  444. {
  445. uint32 count;
  446. int offset;
  447. uint32 foo;
  448. uint8 *cis = cisd;
  449. sd_trace(("%s: Func = %d\n", __FUNCTION__, func));
  450. if (!sd->func_cis_ptr[func]) {
  451. bzero(cis, length);
  452. return SDIOH_API_RC_FAIL;
  453. }
  454. spi_lock(sd);
  455. *cis = 0;
  456. for (count = 0; count < length; count++) {
  457. offset = sd->func_cis_ptr[func] + count;
  458. if (sdspi_card_regread (sd, 0, offset, 1, &foo) < 0) {
  459. sd_err(("%s: regread failed: Can't read CIS\n", __FUNCTION__));
  460. spi_unlock(sd);
  461. return SDIOH_API_RC_FAIL;
  462. }
  463. *cis = (uint8)(foo & 0xff);
  464. cis++;
  465. }
  466. spi_unlock(sd);
  467. return SDIOH_API_RC_SUCCESS;
  468. }
  469. extern SDIOH_API_RC
  470. sdioh_request_byte(sdioh_info_t *sd, uint rw, uint func, uint regaddr, uint8 *byte)
  471. {
  472. int status;
  473. uint32 cmd_arg;
  474. uint32 rsp5;
  475. spi_lock(sd);
  476. cmd_arg = 0;
  477. cmd_arg = SFIELD(cmd_arg, CMD52_FUNCTION, func);
  478. cmd_arg = SFIELD(cmd_arg, CMD52_REG_ADDR, regaddr);
  479. cmd_arg = SFIELD(cmd_arg, CMD52_RW_FLAG, rw == SDIOH_READ ? 0 : 1);
  480. cmd_arg = SFIELD(cmd_arg, CMD52_RAW, 0);
  481. cmd_arg = SFIELD(cmd_arg, CMD52_DATA, rw == SDIOH_READ ? 0 : *byte);
  482. sd_trace(("%s: rw=%d, func=%d, regaddr=0x%08x\n", __FUNCTION__, rw, func, regaddr));
  483. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma,
  484. SDIOH_CMD_52, cmd_arg, NULL, 0)) != SUCCESS) {
  485. spi_unlock(sd);
  486. return status;
  487. }
  488. sdspi_cmd_getrsp(sd, &rsp5, 1);
  489. if (rsp5 != 0x00) {
  490. sd_err(("%s: rsp5 flags is 0x%x func=%d\n",
  491. __FUNCTION__, rsp5, func));
  492. /* ASSERT(0); */
  493. spi_unlock(sd);
  494. return SDIOH_API_RC_FAIL;
  495. }
  496. if (rw == SDIOH_READ)
  497. *byte = sd->card_rsp_data >> 24;
  498. spi_unlock(sd);
  499. return SDIOH_API_RC_SUCCESS;
  500. }
  501. extern SDIOH_API_RC
  502. sdioh_request_word(sdioh_info_t *sd, uint cmd_type, uint rw, uint func, uint addr,
  503. uint32 *word, uint nbytes)
  504. {
  505. int status;
  506. spi_lock(sd);
  507. if (rw == SDIOH_READ)
  508. status = sdspi_card_regread(sd, func, addr, nbytes, word);
  509. else
  510. status = sdspi_card_regwrite(sd, func, addr, nbytes, *word);
  511. spi_unlock(sd);
  512. return (status == SUCCESS ? SDIOH_API_RC_SUCCESS : SDIOH_API_RC_FAIL);
  513. }
  514. extern SDIOH_API_RC
  515. sdioh_request_buffer(sdioh_info_t *sd, uint pio_dma, uint fix_inc, uint rw, uint func,
  516. uint addr, uint reg_width, uint buflen_u, uint8 *buffer, void *pkt)
  517. {
  518. int len;
  519. int buflen = (int)buflen_u;
  520. bool fifo = (fix_inc == SDIOH_DATA_FIX);
  521. spi_lock(sd);
  522. ASSERT(reg_width == 4);
  523. ASSERT(buflen_u < (1 << 30));
  524. ASSERT(sd->client_block_size[func]);
  525. sd_data(("%s: %c len %d r_cnt %d t_cnt %d, pkt @0x%p\n",
  526. __FUNCTION__, rw == SDIOH_READ ? 'R' : 'W',
  527. buflen_u, sd->r_cnt, sd->t_cnt, pkt));
  528. /* Break buffer down into blocksize chunks:
  529. * Bytemode: 1 block at a time.
  530. */
  531. while (buflen > 0) {
  532. if (sd->sd_blockmode) {
  533. /* Max xfer is Page size */
  534. len = MIN(SD_PAGE, buflen);
  535. /* Round down to a block boundry */
  536. if (buflen > sd->client_block_size[func])
  537. len = (len/sd->client_block_size[func]) *
  538. sd->client_block_size[func];
  539. } else {
  540. /* Byte mode: One block at a time */
  541. len = MIN(sd->client_block_size[func], buflen);
  542. }
  543. if (sdspi_card_buf(sd, rw, func, fifo, addr, len, (uint32 *)buffer) != SUCCESS) {
  544. spi_unlock(sd);
  545. return SDIOH_API_RC_FAIL;
  546. }
  547. buffer += len;
  548. buflen -= len;
  549. if (!fifo)
  550. addr += len;
  551. }
  552. spi_unlock(sd);
  553. return SDIOH_API_RC_SUCCESS;
  554. }
  555. static int
  556. sdspi_abort(sdioh_info_t *sd, uint func)
  557. {
  558. uint8 spi_databuf[] = { 0x74, 0x80, 0x00, 0x0C, 0xFF, 0x95, 0xFF, 0xFF,
  559. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  560. uint8 spi_rspbuf[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  561. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  562. int err = 0;
  563. sd_err(("Sending SPI Abort to F%d\n", func));
  564. spi_databuf[4] = func & 0x7;
  565. /* write to function 0, addr 6 (IOABORT) func # in 3 LSBs. */
  566. spi_sendrecv(sd, spi_databuf, spi_rspbuf, sizeof(spi_databuf));
  567. return err;
  568. }
  569. extern int
  570. sdioh_abort(sdioh_info_t *sd, uint fnum)
  571. {
  572. int ret;
  573. spi_lock(sd);
  574. ret = sdspi_abort(sd, fnum);
  575. spi_unlock(sd);
  576. return ret;
  577. }
  578. int
  579. sdioh_start(sdioh_info_t *sd, int stage)
  580. {
  581. return SUCCESS;
  582. }
  583. int
  584. sdioh_stop(sdioh_info_t *sd)
  585. {
  586. return SUCCESS;
  587. }
  588. /*
  589. * Private/Static work routines
  590. */
  591. static bool
  592. sdspi_reset(sdioh_info_t *sd, bool host_reset, bool client_reset)
  593. {
  594. if (!sd)
  595. return TRUE;
  596. spi_lock(sd);
  597. /* Reset client card */
  598. if (client_reset && (sd->adapter_slot != -1)) {
  599. if (sdspi_card_regwrite(sd, 0, SDIOD_CCCR_IOABORT, 1, 0x8) != SUCCESS)
  600. sd_err(("%s: Cannot write to card reg 0x%x\n",
  601. __FUNCTION__, SDIOD_CCCR_IOABORT));
  602. else
  603. sd->card_rca = 0;
  604. }
  605. /* The host reset is a NOP in the sd-spi case. */
  606. if (host_reset) {
  607. sd->sd_mode = SDIOH_MODE_SPI;
  608. }
  609. spi_unlock(sd);
  610. return TRUE;
  611. }
  612. static int
  613. sdspi_host_init(sdioh_info_t *sd)
  614. {
  615. sdspi_reset(sd, 1, 0);
  616. /* Default power on mode is SD1 */
  617. sd->sd_mode = SDIOH_MODE_SPI;
  618. sd->polled_mode = TRUE;
  619. sd->host_init_done = TRUE;
  620. sd->card_init_done = FALSE;
  621. sd->adapter_slot = 1;
  622. return (SUCCESS);
  623. }
  624. #define CMD0_RETRIES 3
  625. #define CMD5_RETRIES 10
  626. static int
  627. get_ocr(sdioh_info_t *sd, uint32 *cmd_arg, uint32 *cmd_rsp)
  628. {
  629. uint32 rsp5;
  630. int retries, status;
  631. /* First issue a CMD0 to get the card into SPI mode. */
  632. for (retries = 0; retries <= CMD0_RETRIES; retries++) {
  633. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma,
  634. SDIOH_CMD_0, *cmd_arg, NULL, 0)) != SUCCESS) {
  635. sd_err(("%s: No response to CMD0\n", __FUNCTION__));
  636. continue;
  637. }
  638. sdspi_cmd_getrsp(sd, &rsp5, 1);
  639. if (GFIELD(rsp5, SPI_RSP_ILL_CMD)) {
  640. printf("%s: Card already initialized (continuing)\n", __FUNCTION__);
  641. break;
  642. }
  643. if (GFIELD(rsp5, SPI_RSP_IDLE)) {
  644. printf("%s: Card in SPI mode\n", __FUNCTION__);
  645. break;
  646. }
  647. }
  648. if (retries > CMD0_RETRIES) {
  649. sd_err(("%s: Too many retries for CMD0\n", __FUNCTION__));
  650. return ERROR;
  651. }
  652. /* Get the Card's Operation Condition. */
  653. /* Occasionally the board takes a while to become ready. */
  654. for (retries = 0; retries <= CMD5_RETRIES; retries++) {
  655. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma,
  656. SDIOH_CMD_5, *cmd_arg, NULL, 0)) != SUCCESS) {
  657. sd_err(("%s: No response to CMD5\n", __FUNCTION__));
  658. continue;
  659. }
  660. printf("CMD5 response data was: 0x%08x\n", sd->card_rsp_data);
  661. if (GFIELD(sd->card_rsp_data, RSP4_CARD_READY)) {
  662. printf("%s: Card ready\n", __FUNCTION__);
  663. break;
  664. }
  665. }
  666. if (retries > CMD5_RETRIES) {
  667. sd_err(("%s: Too many retries for CMD5\n", __FUNCTION__));
  668. return ERROR;
  669. }
  670. *cmd_rsp = sd->card_rsp_data;
  671. sdspi_crc_onoff(sd, sd_crc ? 1 : 0);
  672. return (SUCCESS);
  673. }
  674. static int
  675. sdspi_crc_onoff(sdioh_info_t *sd, bool use_crc)
  676. {
  677. uint32 args;
  678. int status;
  679. args = use_crc ? 1 : 0;
  680. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma,
  681. SDIOH_CMD_59, args, NULL, 0)) != SUCCESS) {
  682. sd_err(("%s: No response to CMD59\n", __FUNCTION__));
  683. }
  684. sd_info(("CMD59 response data was: 0x%08x\n", sd->card_rsp_data));
  685. sd_err(("SD-SPI CRC turned %s\n", use_crc ? "ON" : "OFF"));
  686. return (SUCCESS);
  687. }
  688. static int
  689. sdspi_client_init(sdioh_info_t *sd)
  690. {
  691. uint8 fn_ints;
  692. sd_trace(("%s: Powering up slot %d\n", __FUNCTION__, sd->adapter_slot));
  693. /* Start at ~400KHz clock rate for initialization */
  694. if (!spi_start_clock(sd, 128)) {
  695. sd_err(("spi_start_clock failed\n"));
  696. return ERROR;
  697. }
  698. if (!sdspi_start_power(sd)) {
  699. sd_err(("sdspi_start_power failed\n"));
  700. return ERROR;
  701. }
  702. if (sd->num_funcs == 0) {
  703. sd_err(("%s: No IO funcs!\n", __FUNCTION__));
  704. return ERROR;
  705. }
  706. sdspi_card_enablefuncs(sd);
  707. set_client_block_size(sd, 1, BLOCK_SIZE_4318);
  708. fn_ints = INTR_CTL_FUNC1_EN;
  709. if (sd->num_funcs >= 2) {
  710. set_client_block_size(sd, 2, sd_f2_blocksize /* BLOCK_SIZE_4328 */);
  711. fn_ints |= INTR_CTL_FUNC2_EN;
  712. }
  713. /* Enable/Disable Client interrupts */
  714. /* Turn on here but disable at host controller */
  715. if (sdspi_card_regwrite(sd, 0, SDIOD_CCCR_INTEN, 1,
  716. (fn_ints | INTR_CTL_MASTER_EN)) != SUCCESS) {
  717. sd_err(("%s: Could not enable ints in CCCR\n", __FUNCTION__));
  718. return ERROR;
  719. }
  720. /* Switch to High-speed clocking mode if both host and device support it */
  721. sdspi_set_highspeed_mode(sd, (bool)sd_hiok);
  722. /* After configuring for High-Speed mode, set the desired clock rate. */
  723. if (!spi_start_clock(sd, (uint16)sd_divisor)) {
  724. sd_err(("spi_start_clock failed\n"));
  725. return ERROR;
  726. }
  727. sd->card_init_done = TRUE;
  728. return SUCCESS;
  729. }
  730. static int
  731. sdspi_set_highspeed_mode(sdioh_info_t *sd, bool HSMode)
  732. {
  733. uint32 regdata;
  734. int status;
  735. bool hsmode;
  736. if (HSMode == TRUE) {
  737. sd_err(("Attempting to enable High-Speed mode.\n"));
  738. if ((status = sdspi_card_regread(sd, 0, SDIOD_CCCR_SPEED_CONTROL,
  739. 1, &regdata)) != SUCCESS) {
  740. return status;
  741. }
  742. if (regdata & SDIO_SPEED_SHS) {
  743. sd_err(("Device supports High-Speed mode.\n"));
  744. regdata |= SDIO_SPEED_EHS;
  745. sd_err(("Writing %08x to Card at %08x\n",
  746. regdata, SDIOD_CCCR_SPEED_CONTROL));
  747. if ((status = sdspi_card_regwrite(sd, 0, SDIOD_CCCR_SPEED_CONTROL,
  748. 1, regdata)) != BCME_OK) {
  749. return status;
  750. }
  751. hsmode = 1;
  752. sd_err(("High-speed clocking mode enabled.\n"));
  753. }
  754. else {
  755. sd_err(("Device does not support High-Speed Mode.\n"));
  756. hsmode = 0;
  757. }
  758. } else {
  759. if ((status = sdspi_card_regread(sd, 0, SDIOD_CCCR_SPEED_CONTROL,
  760. 1, &regdata)) != SUCCESS) {
  761. return status;
  762. }
  763. regdata = ~SDIO_SPEED_EHS;
  764. sd_err(("Writing %08x to Card at %08x\n",
  765. regdata, SDIOD_CCCR_SPEED_CONTROL));
  766. if ((status = sdspi_card_regwrite(sd, 0, SDIOD_CCCR_SPEED_CONTROL,
  767. 1, regdata)) != BCME_OK) {
  768. return status;
  769. }
  770. sd_err(("Low-speed clocking mode enabled.\n"));
  771. hsmode = 0;
  772. }
  773. spi_controller_highspeed_mode(sd, hsmode);
  774. return TRUE;
  775. }
  776. bool
  777. sdspi_start_power(sdioh_info_t *sd)
  778. {
  779. uint32 cmd_arg;
  780. uint32 cmd_rsp;
  781. sd_trace(("%s\n", __FUNCTION__));
  782. /* Get the Card's Operation Condition. Occasionally the board
  783. * takes a while to become ready
  784. */
  785. cmd_arg = 0;
  786. if (get_ocr(sd, &cmd_arg, &cmd_rsp) != SUCCESS) {
  787. sd_err(("%s: Failed to get OCR; bailing\n", __FUNCTION__));
  788. return FALSE;
  789. }
  790. sd_err(("mem_present = %d\n", GFIELD(cmd_rsp, RSP4_MEM_PRESENT)));
  791. sd_err(("num_funcs = %d\n", GFIELD(cmd_rsp, RSP4_NUM_FUNCS)));
  792. sd_err(("card_ready = %d\n", GFIELD(cmd_rsp, RSP4_CARD_READY)));
  793. sd_err(("OCR = 0x%x\n", GFIELD(cmd_rsp, RSP4_IO_OCR)));
  794. /* Verify that the card supports I/O mode */
  795. if (GFIELD(cmd_rsp, RSP4_NUM_FUNCS) == 0) {
  796. sd_err(("%s: Card does not support I/O\n", __FUNCTION__));
  797. return ERROR;
  798. }
  799. sd->num_funcs = GFIELD(cmd_rsp, RSP4_NUM_FUNCS);
  800. /* Examine voltage: Arasan only supports 3.3 volts,
  801. * so look for 3.2-3.3 Volts and also 3.3-3.4 volts.
  802. */
  803. if ((GFIELD(cmd_rsp, RSP4_IO_OCR) & (0x3 << 20)) == 0) {
  804. sd_err(("This client does not support 3.3 volts!\n"));
  805. return ERROR;
  806. }
  807. return TRUE;
  808. }
  809. static int
  810. sdspi_driver_init(sdioh_info_t *sd)
  811. {
  812. sd_trace(("%s\n", __FUNCTION__));
  813. if ((sdspi_host_init(sd)) != SUCCESS) {
  814. return ERROR;
  815. }
  816. if (sdspi_client_init(sd) != SUCCESS) {
  817. return ERROR;
  818. }
  819. return SUCCESS;
  820. }
  821. static int
  822. sdspi_card_enablefuncs(sdioh_info_t *sd)
  823. {
  824. int status;
  825. uint32 regdata;
  826. uint32 regaddr, fbraddr;
  827. uint8 func;
  828. uint8 *ptr;
  829. sd_trace(("%s\n", __FUNCTION__));
  830. /* Get the Card's common CIS address */
  831. ptr = (uint8 *) &sd->com_cis_ptr;
  832. for (regaddr = SDIOD_CCCR_CISPTR_0; regaddr <= SDIOD_CCCR_CISPTR_2; regaddr++) {
  833. if ((status = sdspi_card_regread (sd, 0, regaddr, 1, &regdata)) != SUCCESS)
  834. return status;
  835. *ptr++ = (uint8) regdata;
  836. }
  837. /* Only the lower 17-bits are valid */
  838. sd->com_cis_ptr &= 0x0001FFFF;
  839. sd->func_cis_ptr[0] = sd->com_cis_ptr;
  840. sd_info(("%s: Card's Common CIS Ptr = 0x%x\n", __FUNCTION__, sd->com_cis_ptr));
  841. /* Get the Card's function CIS (for each function) */
  842. for (fbraddr = SDIOD_FBR_STARTADDR, func = 1;
  843. func <= sd->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) {
  844. ptr = (uint8 *) &sd->func_cis_ptr[func];
  845. for (regaddr = SDIOD_FBR_CISPTR_0; regaddr <= SDIOD_FBR_CISPTR_2; regaddr++) {
  846. if ((status = sdspi_card_regread (sd, 0, regaddr + fbraddr, 1, &regdata))
  847. != SUCCESS)
  848. return status;
  849. *ptr++ = (uint8) regdata;
  850. }
  851. /* Only the lower 17-bits are valid */
  852. sd->func_cis_ptr[func] &= 0x0001FFFF;
  853. sd_info(("%s: Function %d CIS Ptr = 0x%x\n",
  854. __FUNCTION__, func, sd->func_cis_ptr[func]));
  855. }
  856. sd_info(("%s: write ESCI bit\n", __FUNCTION__));
  857. /* Enable continuous SPI interrupt (ESCI bit) */
  858. sdspi_card_regwrite(sd, 0, SDIOD_CCCR_BICTRL, 1, 0x60);
  859. sd_info(("%s: enable f1\n", __FUNCTION__));
  860. /* Enable function 1 on the card */
  861. regdata = SDIO_FUNC_ENABLE_1;
  862. if ((status = sdspi_card_regwrite(sd, 0, SDIOD_CCCR_IOEN, 1, regdata)) != SUCCESS)
  863. return status;
  864. sd_info(("%s: done\n", __FUNCTION__));
  865. return SUCCESS;
  866. }
  867. /* Read client card reg */
  868. static int
  869. sdspi_card_regread(sdioh_info_t *sd, int func, uint32 regaddr, int regsize, uint32 *data)
  870. {
  871. int status;
  872. uint32 cmd_arg;
  873. uint32 rsp5;
  874. cmd_arg = 0;
  875. if ((func == 0) || (regsize == 1)) {
  876. cmd_arg = SFIELD(cmd_arg, CMD52_FUNCTION, func);
  877. cmd_arg = SFIELD(cmd_arg, CMD52_REG_ADDR, regaddr);
  878. cmd_arg = SFIELD(cmd_arg, CMD52_RW_FLAG, SDIOH_XFER_TYPE_READ);
  879. cmd_arg = SFIELD(cmd_arg, CMD52_RAW, 0);
  880. cmd_arg = SFIELD(cmd_arg, CMD52_DATA, 0);
  881. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma, SDIOH_CMD_52, cmd_arg, NULL, 0))
  882. != SUCCESS)
  883. return status;
  884. sdspi_cmd_getrsp(sd, &rsp5, 1);
  885. if (rsp5 != 0x00)
  886. sd_err(("%s: rsp5 flags is 0x%x\t %d\n",
  887. __FUNCTION__, rsp5, func));
  888. *data = sd->card_rsp_data >> 24;
  889. } else {
  890. cmd_arg = SFIELD(cmd_arg, CMD53_BYTE_BLK_CNT, regsize);
  891. cmd_arg = SFIELD(cmd_arg, CMD53_OP_CODE, 1);
  892. cmd_arg = SFIELD(cmd_arg, CMD53_BLK_MODE, 0);
  893. cmd_arg = SFIELD(cmd_arg, CMD53_FUNCTION, func);
  894. cmd_arg = SFIELD(cmd_arg, CMD53_REG_ADDR, regaddr);
  895. cmd_arg = SFIELD(cmd_arg, CMD53_RW_FLAG, SDIOH_XFER_TYPE_READ);
  896. sd->data_xfer_count = regsize;
  897. /* sdspi_cmd_issue() returns with the command complete bit
  898. * in the ISR already cleared
  899. */
  900. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma, SDIOH_CMD_53, cmd_arg, NULL, 0))
  901. != SUCCESS)
  902. return status;
  903. sdspi_cmd_getrsp(sd, &rsp5, 1);
  904. if (rsp5 != 0x00)
  905. sd_err(("%s: rsp5 flags is 0x%x\t %d\n",
  906. __FUNCTION__, rsp5, func));
  907. *data = sd->card_rsp_data;
  908. if (regsize == 2) {
  909. *data &= 0xffff;
  910. }
  911. sd_info(("%s: CMD53 func %d, addr 0x%x, size %d, data 0x%08x\n",
  912. __FUNCTION__, func, regaddr, regsize, *data));
  913. }
  914. return SUCCESS;
  915. }
  916. /* write a client register */
  917. static int
  918. sdspi_card_regwrite(sdioh_info_t *sd, int func, uint32 regaddr, int regsize, uint32 data)
  919. {
  920. int status;
  921. uint32 cmd_arg, rsp5, flags;
  922. cmd_arg = 0;
  923. if ((func == 0) || (regsize == 1)) {
  924. cmd_arg = SFIELD(cmd_arg, CMD52_FUNCTION, func);
  925. cmd_arg = SFIELD(cmd_arg, CMD52_REG_ADDR, regaddr);
  926. cmd_arg = SFIELD(cmd_arg, CMD52_RW_FLAG, SDIOH_XFER_TYPE_WRITE);
  927. cmd_arg = SFIELD(cmd_arg, CMD52_RAW, 0);
  928. cmd_arg = SFIELD(cmd_arg, CMD52_DATA, data & 0xff);
  929. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma, SDIOH_CMD_52, cmd_arg, NULL, 0))
  930. != SUCCESS)
  931. return status;
  932. sdspi_cmd_getrsp(sd, &rsp5, 1);
  933. flags = GFIELD(rsp5, RSP5_FLAGS);
  934. if (flags && (flags != 0x10))
  935. sd_err(("%s: rsp5.rsp5.flags = 0x%x, expecting 0x10\n",
  936. __FUNCTION__, flags));
  937. }
  938. else {
  939. cmd_arg = SFIELD(cmd_arg, CMD53_BYTE_BLK_CNT, regsize);
  940. cmd_arg = SFIELD(cmd_arg, CMD53_OP_CODE, 1);
  941. cmd_arg = SFIELD(cmd_arg, CMD53_BLK_MODE, 0);
  942. cmd_arg = SFIELD(cmd_arg, CMD53_FUNCTION, func);
  943. cmd_arg = SFIELD(cmd_arg, CMD53_REG_ADDR, regaddr);
  944. cmd_arg = SFIELD(cmd_arg, CMD53_RW_FLAG, SDIOH_XFER_TYPE_WRITE);
  945. sd->data_xfer_count = regsize;
  946. sd->cmd53_wr_data = data;
  947. sd_info(("%s: CMD53 func %d, addr 0x%x, size %d, data 0x%08x\n",
  948. __FUNCTION__, func, regaddr, regsize, data));
  949. /* sdspi_cmd_issue() returns with the command complete bit
  950. * in the ISR already cleared
  951. */
  952. if ((status = sdspi_cmd_issue(sd, sd->sd_use_dma, SDIOH_CMD_53, cmd_arg, NULL, 0))
  953. != SUCCESS)
  954. return status;
  955. sdspi_cmd_getrsp(sd, &rsp5, 1);
  956. if (rsp5 != 0x00)
  957. sd_err(("%s: rsp5 flags = 0x%x, expecting 0x00\n",
  958. __FUNCTION__, rsp5));
  959. }
  960. return SUCCESS;
  961. }
  962. void
  963. sdspi_cmd_getrsp(sdioh_info_t *sd, uint32 *rsp_buffer, int count /* num 32 bit words */)
  964. {
  965. *rsp_buffer = sd->card_response;
  966. }
  967. int max_errors = 0;
  968. #define SPI_MAX_PKT_LEN 768
  969. uint8 spi_databuf[SPI_MAX_PKT_LEN];
  970. uint8 spi_rspbuf[SPI_MAX_PKT_LEN];
  971. /* datalen is used for CMD53 length only (0 for sd->data_xfer_count) */
  972. static int
  973. sdspi_cmd_issue(sdioh_info_t *sd, bool use_dma, uint32 cmd, uint32 arg,
  974. uint32 *data, uint32 datalen)
  975. {
  976. uint32 cmd_reg;
  977. uint32 cmd_arg = arg;
  978. uint8 cmd_crc = 0x95; /* correct CRC for CMD0 and don't care for others. */
  979. uint16 dat_crc;
  980. uint8 cmd52data = 0;
  981. uint32 i, j;
  982. uint32 spi_datalen = 0;
  983. uint32 spi_pre_cmd_pad = 0;
  984. uint32 spi_max_response_pad = 128;
  985. cmd_reg = 0;
  986. cmd_reg = SFIELD(cmd_reg, SPI_DIR, 1);
  987. cmd_reg = SFIELD(cmd_reg, SPI_CMD_INDEX, cmd);
  988. if (GFIELD(cmd_arg, CMD52_RW_FLAG) == 1) { /* Same for CMD52 and CMD53 */
  989. cmd_reg = SFIELD(cmd_reg, SPI_RW, 1);
  990. }
  991. switch (cmd) {
  992. case SDIOH_CMD_59: /* CRC_ON_OFF (SPI Mode Only) - Response R1 */
  993. cmd52data = arg & 0x1;
  994. case SDIOH_CMD_0: /* Set Card to Idle State - No Response */
  995. case SDIOH_CMD_5: /* Send Operation condition - Response R4 */
  996. sd_trace(("%s: CMD%d\n", __FUNCTION__, cmd));
  997. spi_datalen = 44;
  998. spi_pre_cmd_pad = 12;
  999. spi_max_response_pad = 28;
  1000. break;
  1001. case SDIOH_CMD_3: /* Ask card to send RCA - Response R6 */
  1002. case SDIOH_CMD_7: /* Select card - Response R1 */
  1003. case SDIOH_CMD_15: /* Set card to inactive state - Response None */
  1004. sd_err(("%s: CMD%d is invalid for SPI Mode.\n", __FUNCTION__, cmd));
  1005. return ERROR;
  1006. break;
  1007. case SDIOH_CMD_52: /* IO R/W Direct (single byte) - Response R5 */
  1008. cmd52data = GFIELD(cmd_arg, CMD52_DATA);
  1009. cmd_arg = arg;
  1010. cmd_reg = SFIELD(cmd_reg, SPI_FUNC, GFIELD(cmd_arg, CMD52_FUNCTION));
  1011. cmd_reg = SFIELD(cmd_reg, SPI_ADDR, GFIELD(cmd_arg, CMD52_REG_ADDR));
  1012. /* Display trace for byte write */
  1013. if (GFIELD(cmd_arg, CMD52_RW_FLAG) == 1) {
  1014. sd_trace(("%s: CMD52: Wr F:%d @0x%04x=%02x\n",
  1015. __FUNCTION__,
  1016. GFIELD(cmd_arg, CMD52_FUNCTION),
  1017. GFIELD(cmd_arg, CMD52_REG_ADDR),
  1018. cmd52data));
  1019. }
  1020. spi_datalen = 32;
  1021. spi_max_response_pad = 28;
  1022. break;
  1023. case SDIOH_CMD_53: /* IO R/W Extended (multiple bytes/blocks) */
  1024. cmd_arg = arg;
  1025. cmd_reg = SFIELD(cmd_reg, SPI_FUNC, GFIELD(cmd_arg, CMD53_FUNCTION));
  1026. cmd_reg = SFIELD(cmd_reg, SPI_ADDR, GFIELD(cmd_arg, CMD53_REG_ADDR));
  1027. cmd_reg = SFIELD(cmd_reg, SPI_BLKMODE, 0);
  1028. cmd_reg = SFIELD(cmd_reg, SPI_OPCODE, GFIELD(cmd_arg, CMD53_OP_CODE));
  1029. cmd_reg = SFIELD(cmd_reg, SPI_STUFF0, (sd->data_xfer_count>>8));
  1030. cmd52data = (uint8)sd->data_xfer_count;
  1031. /* Set upper bit in byte count if necessary, but don't set it for 512 bytes. */
  1032. if ((sd->data_xfer_count > 255) && (sd->data_xfer_count < 512)) {
  1033. cmd_reg |= 1;
  1034. }
  1035. if (GFIELD(cmd_reg, SPI_RW) == 1) { /* Write */
  1036. spi_max_response_pad = 32;
  1037. spi_datalen = (sd->data_xfer_count + spi_max_response_pad) & 0xFFFC;
  1038. } else { /* Read */
  1039. spi_max_response_pad = 32;
  1040. spi_datalen = (sd->data_xfer_count + spi_max_response_pad) & 0xFFFC;
  1041. }
  1042. sd_trace(("%s: CMD53: %s F:%d @0x%04x len=0x%02x\n",
  1043. __FUNCTION__,
  1044. (GFIELD(cmd_reg, SPI_RW) == 1 ? "Wr" : "Rd"),
  1045. GFIELD(cmd_arg, CMD53_FUNCTION),
  1046. GFIELD(cmd_arg, CMD53_REG_ADDR),
  1047. cmd52data));
  1048. break;
  1049. default:
  1050. sd_err(("%s: Unknown command %d\n", __FUNCTION__, cmd));
  1051. return ERROR;
  1052. }
  1053. /* Set up and issue the SDIO command */
  1054. memset(spi_databuf, SDSPI_IDLE_PAD, spi_datalen);
  1055. spi_databuf[spi_pre_cmd_pad + 0] = (cmd_reg & 0xFF000000) >> 24;
  1056. spi_databuf[spi_pre_cmd_pad + 1] = (cmd_reg & 0x00FF0000) >> 16;
  1057. spi_databuf[spi_pre_cmd_pad + 2] = (cmd_reg & 0x0000FF00) >> 8;
  1058. spi_databuf[spi_pre_cmd_pad + 3] = (cmd_reg & 0x000000FF);
  1059. spi_databuf[spi_pre_cmd_pad + 4] = cmd52data;
  1060. /* Generate CRC7 for command, if CRC is enabled, otherwise, a
  1061. * default CRC7 of 0x95, which is correct for CMD0, is used.
  1062. */
  1063. if (sd_crc) {
  1064. cmd_crc = sdspi_crc7(&spi_databuf[spi_pre_cmd_pad], 5);
  1065. }
  1066. spi_databuf[spi_pre_cmd_pad + 5] = cmd_crc;
  1067. #define SPI_STOP_TRAN 0xFD
  1068. /* for CMD53 Write, put the data into the output buffer */
  1069. if ((cmd == SDIOH_CMD_53) && (GFIELD(cmd_arg, CMD53_RW_FLAG) == 1)) {
  1070. if (datalen != 0) {
  1071. spi_databuf[spi_pre_cmd_pad + 9] = SDSPI_IDLE_PAD;
  1072. spi_databuf[spi_pre_cmd_pad + 10] = SDSPI_START_BLOCK;
  1073. for (i = 0; i < sd->data_xfer_count; i++) {
  1074. spi_databuf[i + 11 + spi_pre_cmd_pad] = ((uint8 *)data)[i];
  1075. }
  1076. if (sd_crc) {
  1077. dat_crc = sdspi_crc16(&spi_databuf[spi_pre_cmd_pad+11], i);
  1078. } else {
  1079. dat_crc = 0xAAAA;
  1080. }
  1081. spi_databuf[i + 11 + spi_pre_cmd_pad] = (dat_crc >> 8) & 0xFF;
  1082. spi_databuf[i + 12 + spi_pre_cmd_pad] = dat_crc & 0xFF;
  1083. } else if (sd->data_xfer_count == 2) {
  1084. spi_databuf[spi_pre_cmd_pad + 9] = SDSPI_IDLE_PAD;
  1085. spi_databuf[spi_pre_cmd_pad + 10] = SDSPI_START_BLOCK;
  1086. spi_databuf[spi_pre_cmd_pad + 11] = sd->cmd53_wr_data & 0xFF;
  1087. spi_databuf[spi_pre_cmd_pad + 12] = (sd->cmd53_wr_data & 0x0000FF00) >> 8;
  1088. if (sd_crc) {
  1089. dat_crc = sdspi_crc16(&spi_databuf[spi_pre_cmd_pad+11], 2);
  1090. } else {
  1091. dat_crc = 0x22AA;
  1092. }
  1093. spi_databuf[spi_pre_cmd_pad + 13] = (dat_crc >> 8) & 0xFF;
  1094. spi_databuf[spi_pre_cmd_pad + 14] = (dat_crc & 0xFF);
  1095. } else if (sd->data_xfer_count == 4) {
  1096. spi_databuf[spi_pre_cmd_pad + 9] = SDSPI_IDLE_PAD;
  1097. spi_databuf[spi_pre_cmd_pad + 10] = SDSPI_START_BLOCK;
  1098. spi_databuf[spi_pre_cmd_pad + 11] = sd->cmd53_wr_data & 0xFF;
  1099. spi_databuf[spi_pre_cmd_pad + 12] = (sd->cmd53_wr_data & 0x0000FF00) >> 8;
  1100. spi_databuf[spi_pre_cmd_pad + 13] = (sd->cmd53_wr_data & 0x00FF0000) >> 16;
  1101. spi_databuf[spi_pre_cmd_pad + 14] = (sd->cmd53_wr_data & 0xFF000000) >> 24;
  1102. if (sd_crc) {
  1103. dat_crc = sdspi_crc16(&spi_databuf[spi_pre_cmd_pad+11], 4);
  1104. } else {
  1105. dat_crc = 0x44AA;
  1106. }
  1107. spi_databuf[spi_pre_cmd_pad + 15] = (dat_crc >> 8) & 0xFF;
  1108. spi_databuf[spi_pre_cmd_pad + 16] = (dat_crc & 0xFF);
  1109. } else {
  1110. printf("CMD53 Write: size %d unsupported\n", sd->data_xfer_count);
  1111. }
  1112. }
  1113. spi_sendrecv(sd, spi_databuf, spi_rspbuf, spi_datalen);
  1114. for (i = spi_pre_cmd_pad + SDSPI_COMMAND_LEN; i < spi_max_response_pad; i++) {
  1115. if ((spi_rspbuf[i] & SDSPI_START_BIT_MASK) == 0) {
  1116. break;
  1117. }
  1118. }
  1119. if (i == spi_max_response_pad) {
  1120. sd_err(("%s: Did not get a response for CMD%d\n", __FUNCTION__, cmd));
  1121. return ERROR;
  1122. }
  1123. /* Extract the response. */
  1124. sd->card_response = spi_rspbuf[i];
  1125. /* for CMD53 Read, find the start of the response data... */
  1126. if ((cmd == SDIOH_CMD_53) && (GFIELD(cmd_arg, CMD52_RW_FLAG) == 0)) {
  1127. for (; i < spi_max_response_pad; i++) {
  1128. if (spi_rspbuf[i] == SDSPI_START_BLOCK) {
  1129. break;
  1130. }
  1131. }
  1132. if (i == spi_max_response_pad) {
  1133. printf("Did not get a start of data phase for CMD%d\n", cmd);
  1134. max_errors++;
  1135. sdspi_abort(sd, GFIELD(cmd_arg, CMD53_FUNCTION));
  1136. }
  1137. sd->card_rsp_data = spi_rspbuf[i+1];
  1138. sd->card_rsp_data |= spi_rspbuf[i+2] << 8;
  1139. sd->card_rsp_data |= spi_rspbuf[i+3] << 16;
  1140. sd->card_rsp_data |= spi_rspbuf[i+4] << 24;
  1141. if (datalen != 0) {
  1142. i++;
  1143. for (j = 0; j < sd->data_xfer_count; j++) {
  1144. ((uint8 *)data)[j] = spi_rspbuf[i+j];
  1145. }
  1146. if (sd_crc) {
  1147. uint16 recv_crc;
  1148. recv_crc = spi_rspbuf[i+j] << 8 | spi_rspbuf[i+j+1];
  1149. dat_crc = sdspi_crc16((uint8 *)data, datalen);
  1150. if (dat_crc != recv_crc) {
  1151. sd_err(("%s: Incorrect data CRC: expected 0x%04x, "
  1152. "received 0x%04x\n",
  1153. __FUNCTION__, dat_crc, recv_crc));
  1154. }
  1155. }
  1156. }
  1157. return SUCCESS;
  1158. }
  1159. sd->card_rsp_data = spi_rspbuf[i+4];
  1160. sd->card_rsp_data |= spi_rspbuf[i+3] << 8;
  1161. sd->card_rsp_data |= spi_rspbuf[i+2] << 16;
  1162. sd->card_rsp_data |= spi_rspbuf[i+1] << 24;
  1163. /* Display trace for byte read */
  1164. if ((cmd == SDIOH_CMD_52) && (GFIELD(cmd_arg, CMD52_RW_FLAG) == 0)) {
  1165. sd_trace(("%s: CMD52: Rd F:%d @0x%04x=%02x\n",
  1166. __FUNCTION__,
  1167. GFIELD(cmd_arg, CMD53_FUNCTION),
  1168. GFIELD(cmd_arg, CMD53_REG_ADDR),
  1169. sd->card_rsp_data >> 24));
  1170. }
  1171. return SUCCESS;
  1172. }
  1173. /*
  1174. * On entry: if single-block or non-block, buffer size <= block size.
  1175. * If multi-block, buffer size is unlimited.
  1176. * Question is how to handle the left-overs in either single- or multi-block.
  1177. * I think the caller should break the buffer up so this routine will always
  1178. * use block size == buffer size to handle the end piece of the buffer
  1179. */
  1180. static int
  1181. sdspi_card_buf(sdioh_info_t *sd, int rw, int func, bool fifo, uint32 addr, int nbytes, uint32 *data)
  1182. {
  1183. int status;
  1184. uint32 cmd_arg;
  1185. uint32 rsp5;
  1186. int num_blocks, blocksize;
  1187. bool local_blockmode, local_dma;
  1188. bool read = rw == SDIOH_READ ? 1 : 0;
  1189. ASSERT(nbytes);
  1190. cmd_arg = 0;
  1191. sd_data(("%s: %s 53 func %d, %s, addr 0x%x, len %d bytes, r_cnt %d t_cnt %d\n",
  1192. __FUNCTION__, read ? "Rd" : "Wr", func, fifo ? "FIXED" : "INCR",
  1193. addr, nbytes, sd->r_cnt, sd->t_cnt));
  1194. if (read) sd->r_cnt++; else sd->t_cnt++;
  1195. local_blockmode = sd->sd_blockmode;
  1196. local_dma = sd->sd_use_dma;
  1197. /* Don't bother with block mode on small xfers */
  1198. if (nbytes < sd->client_block_size[func]) {
  1199. sd_info(("setting local blockmode to false: nbytes (%d) != block_size (%d)\n",
  1200. nbytes, sd->client_block_size[func]));
  1201. local_blockmode = FALSE;
  1202. local_dma = FALSE;
  1203. }
  1204. if (local_blockmode) {
  1205. blocksize = MIN(sd->client_block_size[func], nbytes);
  1206. num_blocks = nbytes/blocksize;
  1207. cmd_arg = SFIELD(cmd_arg, CMD53_BYTE_BLK_CNT, num_blocks);
  1208. cmd_arg = SFIELD(cmd_arg, CMD53_BLK_MODE, 1);
  1209. } else {
  1210. num_blocks = 1;
  1211. blocksize = nbytes;
  1212. cmd_arg = SFIELD(cmd_arg, CMD53_BYTE_BLK_CNT, nbytes);
  1213. cmd_arg = SFIELD(cmd_arg, CMD53_BLK_MODE, 0);
  1214. }
  1215. if (fifo)
  1216. cmd_arg = SFIELD(cmd_arg, CMD53_OP_CODE, 0);
  1217. else
  1218. cmd_arg = SFIELD(cmd_arg, CMD53_OP_CODE, 1);
  1219. cmd_arg = SFIELD(cmd_arg, CMD53_FUNCTION, func);
  1220. cmd_arg = SFIELD(cmd_arg, CMD53_REG_ADDR, addr);
  1221. if (read)
  1222. cmd_arg = SFIELD(cmd_arg, CMD53_RW_FLAG, SDIOH_XFER_TYPE_READ);
  1223. else
  1224. cmd_arg = SFIELD(cmd_arg, CMD53_RW_FLAG, SDIOH_XFER_TYPE_WRITE);
  1225. sd->data_xfer_count = nbytes;
  1226. if ((func == 2) && (fifo == 1)) {
  1227. sd_data(("%s: %s 53 func %d, %s, addr 0x%x, len %d bytes, r_cnt %d t_cnt %d\n",
  1228. __FUNCTION__, read ? "Rd" : "Wr", func, fifo ? "FIXED" : "INCR",
  1229. addr, nbytes, sd->r_cnt, sd->t_cnt));
  1230. }
  1231. /* sdspi_cmd_issue() returns with the command complete bit
  1232. * in the ISR already cleared
  1233. */
  1234. if ((status = sdspi_cmd_issue(sd, local_dma,
  1235. SDIOH_CMD_53, cmd_arg,
  1236. data, nbytes)) != SUCCESS) {
  1237. sd_err(("%s: cmd_issue failed for %s\n", __FUNCTION__, (read ? "read" : "write")));
  1238. return status;
  1239. }
  1240. sdspi_cmd_getrsp(sd, &rsp5, 1);
  1241. if (rsp5 != 0x00) {
  1242. sd_err(("%s: rsp5 flags = 0x%x, expecting 0x00\n",
  1243. __FUNCTION__, rsp5));
  1244. return ERROR;
  1245. }
  1246. return SUCCESS;
  1247. }
  1248. static int
  1249. set_client_block_size(sdioh_info_t *sd, int func, int block_size)
  1250. {
  1251. int base;
  1252. int err = 0;
  1253. sd_err(("%s: Setting block size %d, func %d\n", __FUNCTION__, block_size, func));
  1254. sd->client_block_size[func] = block_size;
  1255. /* Set the block size in the SDIO Card register */
  1256. base = func * SDIOD_FBR_SIZE;
  1257. err = sdspi_card_regwrite(sd, 0, base + SDIOD_CCCR_BLKSIZE_0, 1, block_size & 0xff);
  1258. if (!err) {
  1259. err = sdspi_card_regwrite(sd, 0, base + SDIOD_CCCR_BLKSIZE_1, 1,
  1260. (block_size >> 8) & 0xff);
  1261. }
  1262. /*
  1263. * Do not set the block size in the SDIO Host register; that
  1264. * is func dependent and will get done on an individual
  1265. * transaction basis.
  1266. */
  1267. return (err ? BCME_SDIO_ERROR : 0);
  1268. }
  1269. /* Reset and re-initialize the device */
  1270. int
  1271. sdioh_sdio_reset(sdioh_info_t *si)
  1272. {
  1273. si->card_init_done = FALSE;
  1274. return sdspi_client_init(si);
  1275. }
  1276. #define CRC7_POLYNOM 0x09
  1277. #define CRC7_CRCHIGHBIT 0x40
  1278. static uint8 sdspi_crc7(unsigned char* p, uint32 len)
  1279. {
  1280. uint8 c, j, bit, crc = 0;
  1281. uint32 i;
  1282. for (i = 0; i < len; i++) {
  1283. c = *p++;
  1284. for (j = 0x80; j; j >>= 1) {
  1285. bit = crc & CRC7_CRCHIGHBIT;
  1286. crc <<= 1;
  1287. if (c & j) bit ^= CRC7_CRCHIGHBIT;
  1288. if (bit) crc ^= CRC7_POLYNOM;
  1289. }
  1290. }
  1291. /* Convert the CRC7 to an 8-bit SD CRC */
  1292. crc = (crc << 1) | 1;
  1293. return (crc);
  1294. }
  1295. #define CRC16_POLYNOM 0x1021
  1296. #define CRC16_CRCHIGHBIT 0x8000
  1297. static uint16 sdspi_crc16(unsigned char* p, uint32 len)
  1298. {
  1299. uint32 i;
  1300. uint16 j, c, bit;
  1301. uint16 crc = 0;
  1302. for (i = 0; i < len; i++) {
  1303. c = *p++;
  1304. for (j = 0x80; j; j >>= 1) {
  1305. bit = crc & CRC16_CRCHIGHBIT;
  1306. crc <<= 1;
  1307. if (c & j) bit ^= CRC16_CRCHIGHBIT;
  1308. if (bit) crc ^= CRC16_POLYNOM;
  1309. }
  1310. }
  1311. return (crc);
  1312. }