bcm2835_sdhost.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * bcm2835 sdhost driver.
  4. *
  5. * The 2835 has two SD controllers: The Arasan sdhci controller
  6. * (supported by the iproc driver) and a custom sdhost controller
  7. * (supported by this driver).
  8. *
  9. * The sdhci controller supports both sdcard and sdio. The sdhost
  10. * controller supports the sdcard only, but has better performance.
  11. * Also note that the rpi3 has sdio wifi, so driving the sdcard with
  12. * the sdhost controller allows to use the sdhci controller for wifi
  13. * support.
  14. *
  15. * The configuration is done by devicetree via pin muxing. Both
  16. * SD controller are available on the same pins (2 pin groups = pin 22
  17. * to 27 + pin 48 to 53). So it's possible to use both SD controllers
  18. * at the same time with different pin groups.
  19. *
  20. * This code was ported to U-Boot by
  21. * Alexander Graf <agraf@suse.de>
  22. * and is based on drivers/mmc/host/bcm2835.c in Linux which is written by
  23. * Phil Elwell <phil@raspberrypi.org>
  24. * Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
  25. * which is based on
  26. * mmc-bcm2835.c by Gellert Weisz
  27. * which is, in turn, based on
  28. * sdhci-bcm2708.c by Broadcom
  29. * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
  30. * sdhci.c and sdhci-pci.c by Pierre Ossman
  31. */
  32. #include <clk.h>
  33. #include <common.h>
  34. #include <dm.h>
  35. #include <mmc.h>
  36. #include <asm/arch/msg.h>
  37. #include <asm/arch/mbox.h>
  38. #include <asm/unaligned.h>
  39. #include <dm/device_compat.h>
  40. #include <linux/bitops.h>
  41. #include <linux/bug.h>
  42. #include <linux/compat.h>
  43. #include <linux/delay.h>
  44. #include <linux/io.h>
  45. #include <linux/iopoll.h>
  46. #include <linux/sizes.h>
  47. #include <mach/gpio.h>
  48. #include <power/regulator.h>
  49. #define msleep(a) udelay(a * 1000)
  50. #define SDCMD 0x00 /* Command to SD card - 16 R/W */
  51. #define SDARG 0x04 /* Argument to SD card - 32 R/W */
  52. #define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
  53. #define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */
  54. #define SDRSP0 0x10 /* SD card response (31:0) - 32 R */
  55. #define SDRSP1 0x14 /* SD card response (63:32) - 32 R */
  56. #define SDRSP2 0x18 /* SD card response (95:64) - 32 R */
  57. #define SDRSP3 0x1c /* SD card response (127:96) - 32 R */
  58. #define SDHSTS 0x20 /* SD host status - 11 R/W */
  59. #define SDVDD 0x30 /* SD card power control - 1 R/W */
  60. #define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */
  61. #define SDHCFG 0x38 /* Host configuration - 2 R/W */
  62. #define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */
  63. #define SDDATA 0x40 /* Data to/from SD card - 32 R/W */
  64. #define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */
  65. #define SDCMD_NEW_FLAG 0x8000
  66. #define SDCMD_FAIL_FLAG 0x4000
  67. #define SDCMD_BUSYWAIT 0x800
  68. #define SDCMD_NO_RESPONSE 0x400
  69. #define SDCMD_LONG_RESPONSE 0x200
  70. #define SDCMD_WRITE_CMD 0x80
  71. #define SDCMD_READ_CMD 0x40
  72. #define SDCMD_CMD_MASK 0x3f
  73. #define SDCDIV_MAX_CDIV 0x7ff
  74. #define SDHSTS_BUSY_IRPT 0x400
  75. #define SDHSTS_BLOCK_IRPT 0x200
  76. #define SDHSTS_SDIO_IRPT 0x100
  77. #define SDHSTS_REW_TIME_OUT 0x80
  78. #define SDHSTS_CMD_TIME_OUT 0x40
  79. #define SDHSTS_CRC16_ERROR 0x20
  80. #define SDHSTS_CRC7_ERROR 0x10
  81. #define SDHSTS_FIFO_ERROR 0x08
  82. #define SDHSTS_DATA_FLAG 0x01
  83. #define SDHSTS_CLEAR_MASK (SDHSTS_BUSY_IRPT | \
  84. SDHSTS_BLOCK_IRPT | \
  85. SDHSTS_SDIO_IRPT | \
  86. SDHSTS_REW_TIME_OUT | \
  87. SDHSTS_CMD_TIME_OUT | \
  88. SDHSTS_CRC16_ERROR | \
  89. SDHSTS_CRC7_ERROR | \
  90. SDHSTS_FIFO_ERROR)
  91. #define SDHSTS_TRANSFER_ERROR_MASK (SDHSTS_CRC7_ERROR | \
  92. SDHSTS_CRC16_ERROR | \
  93. SDHSTS_REW_TIME_OUT | \
  94. SDHSTS_FIFO_ERROR)
  95. #define SDHSTS_ERROR_MASK (SDHSTS_CMD_TIME_OUT | \
  96. SDHSTS_TRANSFER_ERROR_MASK)
  97. #define SDHCFG_BUSY_IRPT_EN BIT(10)
  98. #define SDHCFG_BLOCK_IRPT_EN BIT(8)
  99. #define SDHCFG_SDIO_IRPT_EN BIT(5)
  100. #define SDHCFG_DATA_IRPT_EN BIT(4)
  101. #define SDHCFG_SLOW_CARD BIT(3)
  102. #define SDHCFG_WIDE_EXT_BUS BIT(2)
  103. #define SDHCFG_WIDE_INT_BUS BIT(1)
  104. #define SDHCFG_REL_CMD_LINE BIT(0)
  105. #define SDVDD_POWER_OFF 0
  106. #define SDVDD_POWER_ON 1
  107. #define SDEDM_FORCE_DATA_MODE BIT(19)
  108. #define SDEDM_CLOCK_PULSE BIT(20)
  109. #define SDEDM_BYPASS BIT(21)
  110. #define SDEDM_FIFO_FILL_SHIFT 4
  111. #define SDEDM_FIFO_FILL_MASK 0x1f
  112. static u32 edm_fifo_fill(u32 edm)
  113. {
  114. return (edm >> SDEDM_FIFO_FILL_SHIFT) & SDEDM_FIFO_FILL_MASK;
  115. }
  116. #define SDEDM_WRITE_THRESHOLD_SHIFT 9
  117. #define SDEDM_READ_THRESHOLD_SHIFT 14
  118. #define SDEDM_THRESHOLD_MASK 0x1f
  119. #define SDEDM_FSM_MASK 0xf
  120. #define SDEDM_FSM_IDENTMODE 0x0
  121. #define SDEDM_FSM_DATAMODE 0x1
  122. #define SDEDM_FSM_READDATA 0x2
  123. #define SDEDM_FSM_WRITEDATA 0x3
  124. #define SDEDM_FSM_READWAIT 0x4
  125. #define SDEDM_FSM_READCRC 0x5
  126. #define SDEDM_FSM_WRITECRC 0x6
  127. #define SDEDM_FSM_WRITEWAIT1 0x7
  128. #define SDEDM_FSM_POWERDOWN 0x8
  129. #define SDEDM_FSM_POWERUP 0x9
  130. #define SDEDM_FSM_WRITESTART1 0xa
  131. #define SDEDM_FSM_WRITESTART2 0xb
  132. #define SDEDM_FSM_GENPULSES 0xc
  133. #define SDEDM_FSM_WRITEWAIT2 0xd
  134. #define SDEDM_FSM_STARTPOWDOWN 0xf
  135. #define SDDATA_FIFO_WORDS 16
  136. #define FIFO_READ_THRESHOLD 4
  137. #define FIFO_WRITE_THRESHOLD 4
  138. #define SDDATA_FIFO_PIO_BURST 8
  139. #define SDHST_TIMEOUT_MAX_USEC 100000
  140. struct bcm2835_plat {
  141. struct mmc_config cfg;
  142. struct mmc mmc;
  143. };
  144. struct bcm2835_host {
  145. void __iomem *ioaddr;
  146. u32 phys_addr;
  147. int clock; /* Current clock speed */
  148. unsigned int max_clk; /* Max possible freq */
  149. unsigned int blocks; /* remaining PIO blocks */
  150. u32 ns_per_fifo_word;
  151. /* cached registers */
  152. u32 hcfg;
  153. u32 cdiv;
  154. struct mmc_cmd *cmd; /* Current command */
  155. struct mmc_data *data; /* Current data request */
  156. bool use_busy:1; /* Wait for busy interrupt */
  157. struct udevice *dev;
  158. struct mmc *mmc;
  159. struct bcm2835_plat *plat;
  160. unsigned int firmware_sets_cdiv:1;
  161. };
  162. static void bcm2835_dumpregs(struct bcm2835_host *host)
  163. {
  164. dev_dbg(host->dev, "=========== REGISTER DUMP ===========\n");
  165. dev_dbg(host->dev, "SDCMD 0x%08x\n", readl(host->ioaddr + SDCMD));
  166. dev_dbg(host->dev, "SDARG 0x%08x\n", readl(host->ioaddr + SDARG));
  167. dev_dbg(host->dev, "SDTOUT 0x%08x\n", readl(host->ioaddr + SDTOUT));
  168. dev_dbg(host->dev, "SDCDIV 0x%08x\n", readl(host->ioaddr + SDCDIV));
  169. dev_dbg(host->dev, "SDRSP0 0x%08x\n", readl(host->ioaddr + SDRSP0));
  170. dev_dbg(host->dev, "SDRSP1 0x%08x\n", readl(host->ioaddr + SDRSP1));
  171. dev_dbg(host->dev, "SDRSP2 0x%08x\n", readl(host->ioaddr + SDRSP2));
  172. dev_dbg(host->dev, "SDRSP3 0x%08x\n", readl(host->ioaddr + SDRSP3));
  173. dev_dbg(host->dev, "SDHSTS 0x%08x\n", readl(host->ioaddr + SDHSTS));
  174. dev_dbg(host->dev, "SDVDD 0x%08x\n", readl(host->ioaddr + SDVDD));
  175. dev_dbg(host->dev, "SDEDM 0x%08x\n", readl(host->ioaddr + SDEDM));
  176. dev_dbg(host->dev, "SDHCFG 0x%08x\n", readl(host->ioaddr + SDHCFG));
  177. dev_dbg(host->dev, "SDHBCT 0x%08x\n", readl(host->ioaddr + SDHBCT));
  178. dev_dbg(host->dev, "SDHBLC 0x%08x\n", readl(host->ioaddr + SDHBLC));
  179. dev_dbg(host->dev, "===========================================\n");
  180. }
  181. static void bcm2835_reset_internal(struct bcm2835_host *host)
  182. {
  183. u32 temp;
  184. writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
  185. writel(0, host->ioaddr + SDCMD);
  186. writel(0, host->ioaddr + SDARG);
  187. /* Set timeout to a big enough value so we don't hit it */
  188. writel(0xf00000, host->ioaddr + SDTOUT);
  189. writel(0, host->ioaddr + SDCDIV);
  190. /* Clear status register */
  191. writel(SDHSTS_CLEAR_MASK, host->ioaddr + SDHSTS);
  192. writel(0, host->ioaddr + SDHCFG);
  193. writel(0, host->ioaddr + SDHBCT);
  194. writel(0, host->ioaddr + SDHBLC);
  195. /* Limit fifo usage due to silicon bug */
  196. temp = readl(host->ioaddr + SDEDM);
  197. temp &= ~((SDEDM_THRESHOLD_MASK << SDEDM_READ_THRESHOLD_SHIFT) |
  198. (SDEDM_THRESHOLD_MASK << SDEDM_WRITE_THRESHOLD_SHIFT));
  199. temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
  200. (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
  201. writel(temp, host->ioaddr + SDEDM);
  202. /* Wait for FIFO threshold to populate */
  203. msleep(20);
  204. writel(SDVDD_POWER_ON, host->ioaddr + SDVDD);
  205. /* Wait for all components to go through power on cycle */
  206. msleep(20);
  207. host->clock = 0;
  208. writel(host->hcfg, host->ioaddr + SDHCFG);
  209. writel(SDCDIV_MAX_CDIV, host->ioaddr + SDCDIV);
  210. }
  211. static int bcm2835_wait_transfer_complete(struct bcm2835_host *host)
  212. {
  213. ulong tstart_ms = get_timer(0);
  214. while (1) {
  215. u32 edm, fsm;
  216. edm = readl(host->ioaddr + SDEDM);
  217. fsm = edm & SDEDM_FSM_MASK;
  218. if ((fsm == SDEDM_FSM_IDENTMODE) ||
  219. (fsm == SDEDM_FSM_DATAMODE))
  220. break;
  221. if ((fsm == SDEDM_FSM_READWAIT) ||
  222. (fsm == SDEDM_FSM_WRITESTART1) ||
  223. (fsm == SDEDM_FSM_READDATA)) {
  224. writel(edm | SDEDM_FORCE_DATA_MODE,
  225. host->ioaddr + SDEDM);
  226. break;
  227. }
  228. /* Error out after ~1s */
  229. ulong tlapse_ms = get_timer(tstart_ms);
  230. if ( tlapse_ms > 1000 /* ms */ ) {
  231. dev_err(host->dev,
  232. "wait_transfer_complete - still waiting after %lu ms\n",
  233. tlapse_ms);
  234. bcm2835_dumpregs(host);
  235. return -ETIMEDOUT;
  236. }
  237. }
  238. return 0;
  239. }
  240. static int bcm2835_transfer_block_pio(struct bcm2835_host *host, bool is_read)
  241. {
  242. struct mmc_data *data = host->data;
  243. size_t blksize = data->blocksize;
  244. int copy_words;
  245. u32 hsts = 0;
  246. u32 *buf;
  247. if (blksize % sizeof(u32))
  248. return -EINVAL;
  249. buf = is_read ? (u32 *)data->dest : (u32 *)data->src;
  250. if (is_read)
  251. data->dest += blksize;
  252. else
  253. data->src += blksize;
  254. copy_words = blksize / sizeof(u32);
  255. /*
  256. * Copy all contents from/to the FIFO as far as it reaches,
  257. * then wait for it to fill/empty again and rewind.
  258. */
  259. while (copy_words) {
  260. int burst_words, words;
  261. u32 edm;
  262. burst_words = min(SDDATA_FIFO_PIO_BURST, copy_words);
  263. edm = readl(host->ioaddr + SDEDM);
  264. if (is_read)
  265. words = edm_fifo_fill(edm);
  266. else
  267. words = SDDATA_FIFO_WORDS - edm_fifo_fill(edm);
  268. if (words < burst_words) {
  269. int fsm_state = (edm & SDEDM_FSM_MASK);
  270. if ((is_read &&
  271. (fsm_state != SDEDM_FSM_READDATA &&
  272. fsm_state != SDEDM_FSM_READWAIT &&
  273. fsm_state != SDEDM_FSM_READCRC)) ||
  274. (!is_read &&
  275. (fsm_state != SDEDM_FSM_WRITEDATA &&
  276. fsm_state != SDEDM_FSM_WRITEWAIT1 &&
  277. fsm_state != SDEDM_FSM_WRITEWAIT2 &&
  278. fsm_state != SDEDM_FSM_WRITECRC &&
  279. fsm_state != SDEDM_FSM_WRITESTART1 &&
  280. fsm_state != SDEDM_FSM_WRITESTART2))) {
  281. hsts = readl(host->ioaddr + SDHSTS);
  282. printf("fsm %x, hsts %08x\n", fsm_state, hsts);
  283. if (hsts & SDHSTS_ERROR_MASK)
  284. break;
  285. }
  286. continue;
  287. } else if (words > copy_words) {
  288. words = copy_words;
  289. }
  290. copy_words -= words;
  291. /* Copy current chunk to/from the FIFO */
  292. while (words) {
  293. if (is_read)
  294. *(buf++) = readl(host->ioaddr + SDDATA);
  295. else
  296. writel(*(buf++), host->ioaddr + SDDATA);
  297. words--;
  298. }
  299. }
  300. return 0;
  301. }
  302. static int bcm2835_transfer_pio(struct bcm2835_host *host)
  303. {
  304. u32 sdhsts;
  305. bool is_read;
  306. int ret = 0;
  307. is_read = (host->data->flags & MMC_DATA_READ) != 0;
  308. ret = bcm2835_transfer_block_pio(host, is_read);
  309. if (ret)
  310. return ret;
  311. sdhsts = readl(host->ioaddr + SDHSTS);
  312. if (sdhsts & (SDHSTS_CRC16_ERROR |
  313. SDHSTS_CRC7_ERROR |
  314. SDHSTS_FIFO_ERROR)) {
  315. printf("%s transfer error - HSTS %08x\n",
  316. is_read ? "read" : "write", sdhsts);
  317. ret = -EILSEQ;
  318. } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
  319. SDHSTS_REW_TIME_OUT))) {
  320. printf("%s timeout error - HSTS %08x\n",
  321. is_read ? "read" : "write", sdhsts);
  322. ret = -ETIMEDOUT;
  323. }
  324. return ret;
  325. }
  326. static void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_cmd *cmd,
  327. struct mmc_data *data)
  328. {
  329. WARN_ON(host->data);
  330. host->data = data;
  331. if (!data)
  332. return;
  333. /* Use PIO */
  334. host->blocks = data->blocks;
  335. writel(data->blocksize, host->ioaddr + SDHBCT);
  336. writel(data->blocks, host->ioaddr + SDHBLC);
  337. }
  338. static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host)
  339. {
  340. u32 value;
  341. int ret;
  342. int timeout_us = SDHST_TIMEOUT_MAX_USEC;
  343. ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
  344. !(value & SDCMD_NEW_FLAG), timeout_us);
  345. if (ret == -ETIMEDOUT)
  346. printf("%s: timeout (%d us)\n", __func__, timeout_us);
  347. return value;
  348. }
  349. static int bcm2835_send_command(struct bcm2835_host *host, struct mmc_cmd *cmd,
  350. struct mmc_data *data)
  351. {
  352. u32 sdcmd, sdhsts;
  353. WARN_ON(host->cmd);
  354. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) {
  355. printf("unsupported response type!\n");
  356. return -EINVAL;
  357. }
  358. sdcmd = bcm2835_read_wait_sdcmd(host);
  359. if (sdcmd & SDCMD_NEW_FLAG) {
  360. printf("previous command never completed.\n");
  361. bcm2835_dumpregs(host);
  362. return -EBUSY;
  363. }
  364. host->cmd = cmd;
  365. /* Clear any error flags */
  366. sdhsts = readl(host->ioaddr + SDHSTS);
  367. if (sdhsts & SDHSTS_ERROR_MASK)
  368. writel(sdhsts, host->ioaddr + SDHSTS);
  369. bcm2835_prepare_data(host, cmd, data);
  370. writel(cmd->cmdarg, host->ioaddr + SDARG);
  371. sdcmd = cmd->cmdidx & SDCMD_CMD_MASK;
  372. host->use_busy = false;
  373. if (!(cmd->resp_type & MMC_RSP_PRESENT)) {
  374. sdcmd |= SDCMD_NO_RESPONSE;
  375. } else {
  376. if (cmd->resp_type & MMC_RSP_136)
  377. sdcmd |= SDCMD_LONG_RESPONSE;
  378. if (cmd->resp_type & MMC_RSP_BUSY) {
  379. sdcmd |= SDCMD_BUSYWAIT;
  380. host->use_busy = true;
  381. }
  382. }
  383. if (data) {
  384. if (data->flags & MMC_DATA_WRITE)
  385. sdcmd |= SDCMD_WRITE_CMD;
  386. if (data->flags & MMC_DATA_READ)
  387. sdcmd |= SDCMD_READ_CMD;
  388. }
  389. writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
  390. return 0;
  391. }
  392. static int bcm2835_finish_command(struct bcm2835_host *host)
  393. {
  394. struct mmc_cmd *cmd = host->cmd;
  395. u32 sdcmd;
  396. int ret = 0;
  397. sdcmd = bcm2835_read_wait_sdcmd(host);
  398. /* Check for errors */
  399. if (sdcmd & SDCMD_NEW_FLAG) {
  400. printf("command never completed.\n");
  401. bcm2835_dumpregs(host);
  402. return -EIO;
  403. } else if (sdcmd & SDCMD_FAIL_FLAG) {
  404. u32 sdhsts = readl(host->ioaddr + SDHSTS);
  405. /* Clear the errors */
  406. writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
  407. if (!(sdhsts & SDHSTS_CRC7_ERROR) ||
  408. (host->cmd->cmdidx != MMC_CMD_SEND_OP_COND)) {
  409. if (sdhsts & SDHSTS_CMD_TIME_OUT) {
  410. ret = -ETIMEDOUT;
  411. } else {
  412. printf("unexpected command %d error\n",
  413. host->cmd->cmdidx);
  414. bcm2835_dumpregs(host);
  415. ret = -EILSEQ;
  416. }
  417. return ret;
  418. }
  419. }
  420. if (cmd->resp_type & MMC_RSP_PRESENT) {
  421. if (cmd->resp_type & MMC_RSP_136) {
  422. int i;
  423. for (i = 0; i < 4; i++) {
  424. cmd->response[3 - i] =
  425. readl(host->ioaddr + SDRSP0 + i * 4);
  426. }
  427. } else {
  428. cmd->response[0] = readl(host->ioaddr + SDRSP0);
  429. }
  430. }
  431. /* Processed actual command. */
  432. host->cmd = NULL;
  433. return ret;
  434. }
  435. static int bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
  436. {
  437. int ret = -EINVAL;
  438. if (!(intmask & SDHSTS_ERROR_MASK))
  439. return 0;
  440. if (!host->cmd)
  441. return -EINVAL;
  442. printf("sdhost_busy_irq: intmask %08x\n", intmask);
  443. if (intmask & SDHSTS_CRC7_ERROR) {
  444. ret = -EILSEQ;
  445. } else if (intmask & (SDHSTS_CRC16_ERROR |
  446. SDHSTS_FIFO_ERROR)) {
  447. ret = -EILSEQ;
  448. } else if (intmask & (SDHSTS_REW_TIME_OUT | SDHSTS_CMD_TIME_OUT)) {
  449. ret = -ETIMEDOUT;
  450. }
  451. bcm2835_dumpregs(host);
  452. return ret;
  453. }
  454. static int bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
  455. {
  456. int ret = 0;
  457. if (!host->data)
  458. return 0;
  459. if (intmask & (SDHSTS_CRC16_ERROR | SDHSTS_FIFO_ERROR))
  460. ret = -EILSEQ;
  461. if (intmask & SDHSTS_REW_TIME_OUT)
  462. ret = -ETIMEDOUT;
  463. if (ret)
  464. printf("%s:%d %d\n", __func__, __LINE__, ret);
  465. return ret;
  466. }
  467. static int bcm2835_transmit(struct bcm2835_host *host)
  468. {
  469. u32 intmask = readl(host->ioaddr + SDHSTS);
  470. int ret;
  471. /* Check for errors */
  472. ret = bcm2835_check_data_error(host, intmask);
  473. if (ret)
  474. return ret;
  475. ret = bcm2835_check_cmd_error(host, intmask);
  476. if (ret)
  477. return ret;
  478. /* Handle wait for busy end */
  479. if (host->use_busy && (intmask & SDHSTS_BUSY_IRPT)) {
  480. writel(SDHSTS_BUSY_IRPT, host->ioaddr + SDHSTS);
  481. host->use_busy = false;
  482. bcm2835_finish_command(host);
  483. }
  484. /* Handle PIO data transfer */
  485. if (host->data) {
  486. ret = bcm2835_transfer_pio(host);
  487. if (ret)
  488. return ret;
  489. host->blocks--;
  490. if (host->blocks == 0) {
  491. /* Wait for command to complete for real */
  492. ret = bcm2835_wait_transfer_complete(host);
  493. if (ret)
  494. return ret;
  495. /* Transfer complete */
  496. host->data = NULL;
  497. }
  498. }
  499. return 0;
  500. }
  501. static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
  502. {
  503. int div;
  504. u32 clock_rate[2] = { 0 };
  505. /* The SDCDIV register has 11 bits, and holds (div - 2). But
  506. * in data mode the max is 50MHz wihout a minimum, and only
  507. * the bottom 3 bits are used. Since the switch over is
  508. * automatic (unless we have marked the card as slow...),
  509. * chosen values have to make sense in both modes. Ident mode
  510. * must be 100-400KHz, so can range check the requested
  511. * clock. CMD15 must be used to return to data mode, so this
  512. * can be monitored.
  513. *
  514. * clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
  515. * 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
  516. *
  517. * 623->400KHz/27.8MHz
  518. * reset value (507)->491159/50MHz
  519. *
  520. * BUT, the 3-bit clock divisor in data mode is too small if
  521. * the core clock is higher than 250MHz, so instead use the
  522. * SLOW_CARD configuration bit to force the use of the ident
  523. * clock divisor at all times.
  524. */
  525. if (host->firmware_sets_cdiv) {
  526. bcm2835_set_sdhost_clock(clock, &clock_rate[0], &clock_rate[1]);
  527. clock = max(clock_rate[0], clock_rate[1]);
  528. } else {
  529. if (clock < 100000) {
  530. /* Can't stop the clock, but make it as slow as possible
  531. * to show willing
  532. */
  533. host->cdiv = SDCDIV_MAX_CDIV;
  534. writel(host->cdiv, host->ioaddr + SDCDIV);
  535. return;
  536. }
  537. div = host->max_clk / clock;
  538. if (div < 2)
  539. div = 2;
  540. if ((host->max_clk / div) > clock)
  541. div++;
  542. div -= 2;
  543. if (div > SDCDIV_MAX_CDIV)
  544. div = SDCDIV_MAX_CDIV;
  545. clock = host->max_clk / (div + 2);
  546. host->cdiv = div;
  547. writel(host->cdiv, host->ioaddr + SDCDIV);
  548. }
  549. host->mmc->clock = clock;
  550. /* Calibrate some delays */
  551. host->ns_per_fifo_word = (1000000000 / clock) *
  552. ((host->mmc->card_caps & MMC_MODE_4BIT) ? 8 : 32);
  553. /* Set the timeout to 500ms */
  554. writel(host->mmc->clock / 2, host->ioaddr + SDTOUT);
  555. }
  556. static inline int is_power_of_2(u64 x)
  557. {
  558. return !(x & (x - 1));
  559. }
  560. static int bcm2835_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  561. struct mmc_data *data)
  562. {
  563. struct bcm2835_host *host = dev_get_priv(dev);
  564. u32 edm, fsm;
  565. int ret = 0;
  566. if (data && !is_power_of_2(data->blocksize)) {
  567. printf("unsupported block size (%d bytes)\n", data->blocksize);
  568. if (cmd)
  569. return -EINVAL;
  570. }
  571. edm = readl(host->ioaddr + SDEDM);
  572. fsm = edm & SDEDM_FSM_MASK;
  573. if ((fsm != SDEDM_FSM_IDENTMODE) &&
  574. (fsm != SDEDM_FSM_DATAMODE) &&
  575. (cmd && cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)) {
  576. printf("previous command (%d) not complete (EDM %08x)\n",
  577. readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK, edm);
  578. bcm2835_dumpregs(host);
  579. if (cmd)
  580. return -EILSEQ;
  581. return 0;
  582. }
  583. if (cmd) {
  584. ret = bcm2835_send_command(host, cmd, data);
  585. if (!ret && !host->use_busy)
  586. ret = bcm2835_finish_command(host);
  587. }
  588. /* Wait for completion of busy signal or data transfer */
  589. while (host->use_busy || host->data) {
  590. ret = bcm2835_transmit(host);
  591. if (ret)
  592. break;
  593. }
  594. return ret;
  595. }
  596. static int bcm2835_set_ios(struct udevice *dev)
  597. {
  598. struct bcm2835_host *host = dev_get_priv(dev);
  599. struct mmc *mmc = mmc_get_mmc_dev(dev);
  600. if (!mmc->clock || mmc->clock != host->clock) {
  601. bcm2835_set_clock(host, mmc->clock);
  602. host->clock = mmc->clock;
  603. }
  604. /* set bus width */
  605. host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
  606. if (mmc->bus_width == 4)
  607. host->hcfg |= SDHCFG_WIDE_EXT_BUS;
  608. host->hcfg |= SDHCFG_WIDE_INT_BUS;
  609. /* Disable clever clock switching, to cope with fast core clocks */
  610. host->hcfg |= SDHCFG_SLOW_CARD;
  611. writel(host->hcfg, host->ioaddr + SDHCFG);
  612. return 0;
  613. }
  614. static void bcm2835_add_host(struct bcm2835_host *host)
  615. {
  616. struct mmc_config *cfg = &host->plat->cfg;
  617. cfg->f_max = host->max_clk;
  618. cfg->f_min = host->max_clk / SDCDIV_MAX_CDIV;
  619. cfg->b_max = 65535;
  620. dev_dbg(host->dev, "f_max %d, f_min %d\n",
  621. cfg->f_max, cfg->f_min);
  622. /* host controller capabilities */
  623. cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz;
  624. /* report supported voltage ranges */
  625. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  626. /* Set interrupt enables */
  627. host->hcfg = SDHCFG_BUSY_IRPT_EN;
  628. bcm2835_reset_internal(host);
  629. }
  630. static int bcm2835_probe(struct udevice *dev)
  631. {
  632. struct bcm2835_plat *plat = dev_get_plat(dev);
  633. struct bcm2835_host *host = dev_get_priv(dev);
  634. struct mmc *mmc = mmc_get_mmc_dev(dev);
  635. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  636. u32 clock_rate[2] = { ~0 };
  637. host->dev = dev;
  638. host->mmc = mmc;
  639. host->plat = plat;
  640. upriv->mmc = &plat->mmc;
  641. plat->cfg.name = dev->name;
  642. host->phys_addr = dev_read_addr(dev);
  643. if (host->phys_addr == FDT_ADDR_T_NONE)
  644. return -EINVAL;
  645. host->ioaddr = devm_ioremap(dev, host->phys_addr, SZ_256);
  646. if (!host->ioaddr)
  647. return -ENOMEM;
  648. host->max_clk = bcm2835_get_mmc_clock(BCM2835_MBOX_CLOCK_ID_CORE);
  649. bcm2835_set_sdhost_clock(0, &clock_rate[0], &clock_rate[1]);
  650. host->firmware_sets_cdiv = (clock_rate[0] != ~0);
  651. bcm2835_add_host(host);
  652. dev_dbg(dev, "%s -> OK\n", __func__);
  653. return 0;
  654. }
  655. static const struct udevice_id bcm2835_match[] = {
  656. { .compatible = "brcm,bcm2835-sdhost" },
  657. { }
  658. };
  659. static const struct dm_mmc_ops bcm2835_ops = {
  660. .send_cmd = bcm2835_send_cmd,
  661. .set_ios = bcm2835_set_ios,
  662. };
  663. static int bcm2835_bind(struct udevice *dev)
  664. {
  665. struct bcm2835_plat *plat = dev_get_plat(dev);
  666. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  667. }
  668. U_BOOT_DRIVER(bcm2835_sdhost) = {
  669. .name = "bcm2835-sdhost",
  670. .id = UCLASS_MMC,
  671. .of_match = bcm2835_match,
  672. .bind = bcm2835_bind,
  673. .probe = bcm2835_probe,
  674. .priv_auto = sizeof(struct bcm2835_host),
  675. .plat_auto = sizeof(struct bcm2835_plat),
  676. .ops = &bcm2835_ops,
  677. };