gen_atmel_mci.c 14 KB

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