bcm2835_sdhost.c 21 KB

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