bcm2835_sdhost.c 24 KB

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