gen_atmel_mci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * Copyright (C) 2010
  3. * Rob Emanuele <rob@emanuele.us>
  4. * Reinhard Meyer, EMK Elektronik <reinhard.meyer@emk-elektronik.de>
  5. *
  6. * Original Driver:
  7. * Copyright (C) 2004-2006 Atmel Corporation
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <clk.h>
  13. #include <dm.h>
  14. #include <mmc.h>
  15. #include <part.h>
  16. #include <malloc.h>
  17. #include <asm/io.h>
  18. #include <linux/errno.h>
  19. #include <asm/byteorder.h>
  20. #include <asm/arch/clk.h>
  21. #include <asm/arch/hardware.h>
  22. #include "atmel_mci.h"
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #ifndef CONFIG_SYS_MMC_CLK_OD
  25. # define CONFIG_SYS_MMC_CLK_OD 150000
  26. #endif
  27. #define MMC_DEFAULT_BLKLEN 512
  28. #if defined(CONFIG_ATMEL_MCI_PORTB)
  29. # define MCI_BUS 1
  30. #else
  31. # define MCI_BUS 0
  32. #endif
  33. #ifdef CONFIG_DM_MMC
  34. struct atmel_mci_plat {
  35. struct mmc mmc;
  36. struct mmc_config cfg;
  37. struct atmel_mci *mci;
  38. };
  39. #endif
  40. struct atmel_mci_priv {
  41. #ifndef CONFIG_DM_MMC
  42. struct mmc_config cfg;
  43. struct atmel_mci *mci;
  44. #endif
  45. unsigned int initialized:1;
  46. unsigned int curr_clk;
  47. #ifdef CONFIG_DM_MMC
  48. ulong bus_clk_rate;
  49. #endif
  50. };
  51. /* Read Atmel MCI IP version */
  52. static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
  53. {
  54. return readl(&mci->version) & 0x00000fff;
  55. }
  56. /*
  57. * Print command and status:
  58. *
  59. * - always when DEBUG is defined
  60. * - on command errors
  61. */
  62. static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
  63. {
  64. debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
  65. cmdr, cmdr & 0x3F, arg, status, msg);
  66. }
  67. /* Setup for MCI Clock and Block Size */
  68. #ifdef CONFIG_DM_MMC
  69. static void mci_set_mode(struct udevice *dev, u32 hz, u32 blklen)
  70. {
  71. struct atmel_mci_plat *plat = dev_get_platdata(dev);
  72. struct atmel_mci_priv *priv = dev_get_priv(dev);
  73. struct mmc *mmc = &plat->mmc;
  74. u32 bus_hz = priv->bus_clk_rate;
  75. atmel_mci_t *mci = plat->mci;
  76. #else
  77. static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
  78. {
  79. struct atmel_mci_priv *priv = mmc->priv;
  80. u32 bus_hz = get_mci_clk_rate();
  81. atmel_mci_t *mci = priv->mci;
  82. #endif
  83. u32 clkdiv = 255;
  84. unsigned int version = atmel_mci_get_version(mci);
  85. u32 clkodd = 0;
  86. u32 mr;
  87. debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
  88. bus_hz, hz, blklen);
  89. if (hz > 0) {
  90. if (version >= 0x500) {
  91. clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2;
  92. if (clkdiv > 511)
  93. clkdiv = 511;
  94. clkodd = clkdiv & 1;
  95. clkdiv >>= 1;
  96. debug("mci: setting clock %u Hz, block size %u\n",
  97. bus_hz / (clkdiv * 2 + clkodd + 2), blklen);
  98. } else {
  99. /* find clkdiv yielding a rate <= than requested */
  100. for (clkdiv = 0; clkdiv < 255; clkdiv++) {
  101. if ((bus_hz / (clkdiv + 1) / 2) <= hz)
  102. break;
  103. }
  104. debug("mci: setting clock %u Hz, block size %u\n",
  105. (bus_hz / (clkdiv + 1)) / 2, blklen);
  106. }
  107. }
  108. if (version >= 0x500)
  109. priv->curr_clk = bus_hz / (clkdiv * 2 + clkodd + 2);
  110. else
  111. priv->curr_clk = (bus_hz / (clkdiv + 1)) / 2;
  112. blklen &= 0xfffc;
  113. mr = MMCI_BF(CLKDIV, clkdiv);
  114. /* MCI IP version >= 0x200 has R/WPROOF */
  115. if (version >= 0x200)
  116. mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF);
  117. /*
  118. * MCI IP version >= 0x500 use bit 16 as clkodd.
  119. * MCI IP version < 0x500 use upper 16 bits for blklen.
  120. */
  121. if (version >= 0x500)
  122. mr |= MMCI_BF(CLKODD, clkodd);
  123. else
  124. mr |= MMCI_BF(BLKLEN, blklen);
  125. writel(mr, &mci->mr);
  126. /* MCI IP version >= 0x200 has blkr */
  127. if (version >= 0x200)
  128. writel(MMCI_BF(BLKLEN, blklen), &mci->blkr);
  129. if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS)
  130. writel(MMCI_BIT(HSMODE), &mci->cfg);
  131. priv->initialized = 1;
  132. }
  133. /* Return the CMDR with flags for a given command and data packet */
  134. static u32 mci_encode_cmd(
  135. struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
  136. {
  137. u32 cmdr = 0;
  138. /* Default Flags for Errors */
  139. *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
  140. MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
  141. /* Default Flags for the Command */
  142. cmdr |= MMCI_BIT(MAXLAT);
  143. if (data) {
  144. cmdr |= MMCI_BF(TRCMD, 1);
  145. if (data->blocks > 1)
  146. cmdr |= MMCI_BF(TRTYP, 1);
  147. if (data->flags & MMC_DATA_READ)
  148. cmdr |= MMCI_BIT(TRDIR);
  149. }
  150. if (cmd->resp_type & MMC_RSP_CRC)
  151. *error_flags |= MMCI_BIT(RCRCE);
  152. if (cmd->resp_type & MMC_RSP_136)
  153. cmdr |= MMCI_BF(RSPTYP, 2);
  154. else if (cmd->resp_type & MMC_RSP_BUSY)
  155. cmdr |= MMCI_BF(RSPTYP, 3);
  156. else if (cmd->resp_type & MMC_RSP_PRESENT)
  157. cmdr |= MMCI_BF(RSPTYP, 1);
  158. return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
  159. }
  160. /* Entered into function pointer in mci_send_cmd */
  161. static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
  162. {
  163. u32 status;
  164. do {
  165. status = readl(&mci->sr);
  166. if (status & (error_flags | MMCI_BIT(OVRE)))
  167. goto io_fail;
  168. } while (!(status & MMCI_BIT(RXRDY)));
  169. if (status & MMCI_BIT(RXRDY)) {
  170. *data = readl(&mci->rdr);
  171. status = 0;
  172. }
  173. io_fail:
  174. return status;
  175. }
  176. /* Entered into function pointer in mci_send_cmd */
  177. static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
  178. {
  179. u32 status;
  180. do {
  181. status = readl(&mci->sr);
  182. if (status & (error_flags | MMCI_BIT(UNRE)))
  183. goto io_fail;
  184. } while (!(status & MMCI_BIT(TXRDY)));
  185. if (status & MMCI_BIT(TXRDY)) {
  186. writel(*data, &mci->tdr);
  187. status = 0;
  188. }
  189. io_fail:
  190. return status;
  191. }
  192. /*
  193. * Entered into mmc structure during driver init
  194. *
  195. * Sends a command out on the bus and deals with the block data.
  196. * Takes the mmc pointer, a command pointer, and an optional data pointer.
  197. */
  198. #ifdef CONFIG_DM_MMC
  199. static int atmel_mci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  200. struct mmc_data *data)
  201. {
  202. struct atmel_mci_plat *plat = dev_get_platdata(dev);
  203. struct atmel_mci_priv *priv = dev_get_priv(dev);
  204. struct mmc *mmc = mmc_get_mmc_dev(dev);
  205. atmel_mci_t *mci = plat->mci;
  206. #else
  207. static int
  208. mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  209. {
  210. struct atmel_mci_priv *priv = mmc->priv;
  211. atmel_mci_t *mci = priv->mci;
  212. #endif
  213. u32 cmdr;
  214. u32 error_flags = 0;
  215. u32 status;
  216. if (!priv->initialized) {
  217. puts ("MCI not initialized!\n");
  218. return -ECOMM;
  219. }
  220. /* Figure out the transfer arguments */
  221. cmdr = mci_encode_cmd(cmd, data, &error_flags);
  222. /* For multi blocks read/write, set the block register */
  223. if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
  224. || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
  225. writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len),
  226. &mci->blkr);
  227. /* Send the command */
  228. writel(cmd->cmdarg, &mci->argr);
  229. writel(cmdr, &mci->cmdr);
  230. #ifdef DEBUG
  231. dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
  232. #endif
  233. /* Wait for the command to complete */
  234. while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
  235. if ((status & error_flags) & MMCI_BIT(RTOE)) {
  236. dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
  237. return -ETIMEDOUT;
  238. } else if (status & error_flags) {
  239. dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
  240. return -ECOMM;
  241. }
  242. /* Copy the response to the response buffer */
  243. if (cmd->resp_type & MMC_RSP_136) {
  244. cmd->response[0] = readl(&mci->rspr);
  245. cmd->response[1] = readl(&mci->rspr1);
  246. cmd->response[2] = readl(&mci->rspr2);
  247. cmd->response[3] = readl(&mci->rspr3);
  248. } else
  249. cmd->response[0] = readl(&mci->rspr);
  250. /* transfer all of the blocks */
  251. if (data) {
  252. u32 word_count, block_count;
  253. u32* ioptr;
  254. u32 sys_blocksize, dummy, i;
  255. u32 (*mci_data_op)
  256. (atmel_mci_t *mci, u32* data, u32 error_flags);
  257. if (data->flags & MMC_DATA_READ) {
  258. mci_data_op = mci_data_read;
  259. sys_blocksize = mmc->read_bl_len;
  260. ioptr = (u32*)data->dest;
  261. } else {
  262. mci_data_op = mci_data_write;
  263. sys_blocksize = mmc->write_bl_len;
  264. ioptr = (u32*)data->src;
  265. }
  266. status = 0;
  267. for (block_count = 0;
  268. block_count < data->blocks && !status;
  269. block_count++) {
  270. word_count = 0;
  271. do {
  272. status = mci_data_op(mci, ioptr, error_flags);
  273. word_count++;
  274. ioptr++;
  275. } while (!status && word_count < (data->blocksize/4));
  276. #ifdef DEBUG
  277. if (data->flags & MMC_DATA_READ)
  278. {
  279. u32 cnt = word_count * 4;
  280. printf("Read Data:\n");
  281. print_buffer(0, data->dest + cnt * block_count,
  282. 1, cnt, 0);
  283. }
  284. #endif
  285. #ifdef DEBUG
  286. if (!status && word_count < (sys_blocksize / 4))
  287. printf("filling rest of block...\n");
  288. #endif
  289. /* fill the rest of a full block */
  290. while (!status && word_count < (sys_blocksize / 4)) {
  291. status = mci_data_op(mci, &dummy,
  292. error_flags);
  293. word_count++;
  294. }
  295. if (status) {
  296. dump_cmd(cmdr, cmd->cmdarg, status,
  297. "Data Transfer Failed");
  298. return -ECOMM;
  299. }
  300. }
  301. /* Wait for Transfer End */
  302. i = 0;
  303. do {
  304. status = readl(&mci->sr);
  305. if (status & error_flags) {
  306. dump_cmd(cmdr, cmd->cmdarg, status,
  307. "DTIP Wait Failed");
  308. return -ECOMM;
  309. }
  310. i++;
  311. } while ((status & MMCI_BIT(DTIP)) && i < 10000);
  312. if (status & MMCI_BIT(DTIP)) {
  313. dump_cmd(cmdr, cmd->cmdarg, status,
  314. "XFER DTIP never unset, ignoring");
  315. }
  316. }
  317. /*
  318. * After the switch command, wait for 8 clocks before the next
  319. * command
  320. */
  321. if (cmd->cmdidx == MMC_CMD_SWITCH)
  322. udelay(8*1000000 / priv->curr_clk); /* 8 clk in us */
  323. return 0;
  324. }
  325. #ifdef CONFIG_DM_MMC
  326. static int atmel_mci_set_ios(struct udevice *dev)
  327. {
  328. struct atmel_mci_plat *plat = dev_get_platdata(dev);
  329. struct mmc *mmc = mmc_get_mmc_dev(dev);
  330. atmel_mci_t *mci = plat->mci;
  331. #else
  332. /* Entered into mmc structure during driver init */
  333. static int mci_set_ios(struct mmc *mmc)
  334. {
  335. struct atmel_mci_priv *priv = mmc->priv;
  336. atmel_mci_t *mci = priv->mci;
  337. #endif
  338. int bus_width = mmc->bus_width;
  339. unsigned int version = atmel_mci_get_version(mci);
  340. int busw;
  341. /* Set the clock speed */
  342. #ifdef CONFIG_DM_MMC
  343. mci_set_mode(dev, mmc->clock, MMC_DEFAULT_BLKLEN);
  344. #else
  345. mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
  346. #endif
  347. /*
  348. * set the bus width and select slot for this interface
  349. * there is no capability for multiple slots on the same interface yet
  350. */
  351. if ((version & 0xf00) >= 0x300) {
  352. switch (bus_width) {
  353. case 8:
  354. busw = 3;
  355. break;
  356. case 4:
  357. busw = 2;
  358. break;
  359. default:
  360. busw = 0;
  361. break;
  362. }
  363. writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  364. } else {
  365. busw = (bus_width == 4) ? 1 : 0;
  366. writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  367. }
  368. return 0;
  369. }
  370. #ifdef CONFIG_DM_MMC
  371. static int atmel_mci_hw_init(struct udevice *dev)
  372. {
  373. struct atmel_mci_plat *plat = dev_get_platdata(dev);
  374. atmel_mci_t *mci = plat->mci;
  375. #else
  376. /* Entered into mmc structure during driver init */
  377. static int mci_init(struct mmc *mmc)
  378. {
  379. struct atmel_mci_priv *priv = mmc->priv;
  380. atmel_mci_t *mci = priv->mci;
  381. #endif
  382. /* Initialize controller */
  383. writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
  384. writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
  385. writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
  386. writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
  387. /* This delay can be optimized, but stick with max value */
  388. writel(0x7f, &mci->dtor);
  389. /* Disable Interrupts */
  390. writel(~0UL, &mci->idr);
  391. /* Set default clocks and blocklen */
  392. #ifdef CONFIG_DM_MMC
  393. mci_set_mode(dev, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  394. #else
  395. mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  396. #endif
  397. return 0;
  398. }
  399. #ifndef CONFIG_DM_MMC
  400. static const struct mmc_ops atmel_mci_ops = {
  401. .send_cmd = mci_send_cmd,
  402. .set_ios = mci_set_ios,
  403. .init = mci_init,
  404. };
  405. /*
  406. * This is the only exported function
  407. *
  408. * Call it with the MCI register base address
  409. */
  410. int atmel_mci_init(void *regs)
  411. {
  412. struct mmc *mmc;
  413. struct mmc_config *cfg;
  414. struct atmel_mci_priv *priv;
  415. unsigned int version;
  416. priv = calloc(1, sizeof(*priv));
  417. if (!priv)
  418. return -ENOMEM;
  419. cfg = &priv->cfg;
  420. cfg->name = "mci";
  421. cfg->ops = &atmel_mci_ops;
  422. priv->mci = (struct atmel_mci *)regs;
  423. priv->initialized = 0;
  424. /* need to be able to pass these in on a board by board basis */
  425. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  426. version = atmel_mci_get_version(priv->mci);
  427. if ((version & 0xf00) >= 0x300) {
  428. cfg->host_caps = MMC_MODE_8BIT;
  429. cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
  430. }
  431. cfg->host_caps |= MMC_MODE_4BIT;
  432. /*
  433. * min and max frequencies determined by
  434. * max and min of clock divider
  435. */
  436. cfg->f_min = get_mci_clk_rate() / (2*256);
  437. cfg->f_max = get_mci_clk_rate() / (2*1);
  438. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  439. mmc = mmc_create(cfg, priv);
  440. if (mmc == NULL) {
  441. free(priv);
  442. return -ENODEV;
  443. }
  444. /* NOTE: possibly leaking the priv structure */
  445. return 0;
  446. }
  447. #endif
  448. #ifdef CONFIG_DM_MMC
  449. static const struct dm_mmc_ops atmel_mci_mmc_ops = {
  450. .send_cmd = atmel_mci_send_cmd,
  451. .set_ios = atmel_mci_set_ios,
  452. };
  453. static void atmel_mci_setup_cfg(struct udevice *dev)
  454. {
  455. struct atmel_mci_plat *plat = dev_get_platdata(dev);
  456. struct atmel_mci_priv *priv = dev_get_priv(dev);
  457. struct mmc_config *cfg;
  458. u32 version;
  459. cfg = &plat->cfg;
  460. cfg->name = "Atmel mci";
  461. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  462. /*
  463. * If the version is above 3.0, the capabilities of the 8-bit
  464. * bus width and high speed are supported.
  465. */
  466. version = atmel_mci_get_version(plat->mci);
  467. if ((version & 0xf00) >= 0x300) {
  468. cfg->host_caps = MMC_MODE_8BIT |
  469. MMC_MODE_HS | MMC_MODE_HS_52MHz;
  470. }
  471. cfg->host_caps |= MMC_MODE_4BIT;
  472. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  473. cfg->f_min = priv->bus_clk_rate / (2 * 256);
  474. cfg->f_max = priv->bus_clk_rate / 2;
  475. }
  476. static int atmel_mci_enable_clk(struct udevice *dev)
  477. {
  478. struct atmel_mci_priv *priv = dev_get_priv(dev);
  479. struct clk clk;
  480. ulong clk_rate;
  481. int ret = 0;
  482. ret = clk_get_by_index(dev, 0, &clk);
  483. if (ret) {
  484. ret = -EINVAL;
  485. goto failed;
  486. }
  487. ret = clk_enable(&clk);
  488. if (ret)
  489. goto failed;
  490. clk_rate = clk_get_rate(&clk);
  491. if (!clk_rate) {
  492. ret = -EINVAL;
  493. goto failed;
  494. }
  495. priv->bus_clk_rate = clk_rate;
  496. failed:
  497. clk_free(&clk);
  498. return ret;
  499. }
  500. static int atmel_mci_probe(struct udevice *dev)
  501. {
  502. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  503. struct atmel_mci_plat *plat = dev_get_platdata(dev);
  504. struct mmc *mmc;
  505. int ret;
  506. ret = atmel_mci_enable_clk(dev);
  507. if (ret)
  508. return ret;
  509. plat->mci = (struct atmel_mci *)devfdt_get_addr_ptr(dev);
  510. atmel_mci_setup_cfg(dev);
  511. mmc = &plat->mmc;
  512. mmc->cfg = &plat->cfg;
  513. mmc->dev = dev;
  514. upriv->mmc = mmc;
  515. atmel_mci_hw_init(dev);
  516. return 0;
  517. }
  518. static int atmel_mci_bind(struct udevice *dev)
  519. {
  520. struct atmel_mci_plat *plat = dev_get_platdata(dev);
  521. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  522. }
  523. static const struct udevice_id atmel_mci_ids[] = {
  524. { .compatible = "atmel,hsmci" },
  525. { }
  526. };
  527. U_BOOT_DRIVER(atmel_mci) = {
  528. .name = "atmel-mci",
  529. .id = UCLASS_MMC,
  530. .of_match = atmel_mci_ids,
  531. .bind = atmel_mci_bind,
  532. .probe = atmel_mci_probe,
  533. .platdata_auto_alloc_size = sizeof(struct atmel_mci_plat),
  534. .priv_auto_alloc_size = sizeof(struct atmel_mci_priv),
  535. .ops = &atmel_mci_mmc_ops,
  536. };
  537. #endif