omap_hsmmc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * (C) Copyright 2008
  3. * Texas Instruments, <www.ti.com>
  4. * Sukumar Ghorai <s-ghorai@ti.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation's version 2 of
  12. * the License.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <config.h>
  25. #include <common.h>
  26. #include <malloc.h>
  27. #include <mmc.h>
  28. #include <part.h>
  29. #include <i2c.h>
  30. #include <twl4030.h>
  31. #include <twl6030.h>
  32. #include <palmas.h>
  33. #include <asm/io.h>
  34. #include <asm/arch/mmc_host_def.h>
  35. #if !defined(CONFIG_SOC_KEYSTONE)
  36. #include <asm/gpio.h>
  37. #include <asm/arch/sys_proto.h>
  38. #endif
  39. #include <dm.h>
  40. DECLARE_GLOBAL_DATA_PTR;
  41. /* simplify defines to OMAP_HSMMC_USE_GPIO */
  42. #if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
  43. (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
  44. #define OMAP_HSMMC_USE_GPIO
  45. #else
  46. #undef OMAP_HSMMC_USE_GPIO
  47. #endif
  48. /* common definitions for all OMAPs */
  49. #define SYSCTL_SRC (1 << 25)
  50. #define SYSCTL_SRD (1 << 26)
  51. struct omap_hsmmc_data {
  52. struct hsmmc *base_addr;
  53. struct mmc_config cfg;
  54. #ifdef OMAP_HSMMC_USE_GPIO
  55. #ifdef CONFIG_DM_MMC
  56. struct gpio_desc cd_gpio; /* Change Detect GPIO */
  57. struct gpio_desc wp_gpio; /* Write Protect GPIO */
  58. bool cd_inverted;
  59. #else
  60. int cd_gpio;
  61. int wp_gpio;
  62. #endif
  63. #endif
  64. };
  65. /* If we fail after 1 second wait, something is really bad */
  66. #define MAX_RETRY_MS 1000
  67. static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
  68. static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
  69. unsigned int siz);
  70. #if defined(OMAP_HSMMC_USE_GPIO) && !defined(CONFIG_DM_MMC)
  71. static int omap_mmc_setup_gpio_in(int gpio, const char *label)
  72. {
  73. int ret;
  74. #ifndef CONFIG_DM_GPIO
  75. if (!gpio_is_valid(gpio))
  76. return -1;
  77. #endif
  78. ret = gpio_request(gpio, label);
  79. if (ret)
  80. return ret;
  81. ret = gpio_direction_input(gpio);
  82. if (ret)
  83. return ret;
  84. return gpio;
  85. }
  86. #endif
  87. #if defined(CONFIG_OMAP44XX)
  88. static void omap4_vmmc_pbias_config(struct mmc *mmc)
  89. {
  90. u32 value = 0;
  91. value = readl((*ctrl)->control_pbiaslite);
  92. value &= ~(MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ);
  93. writel(value, (*ctrl)->control_pbiaslite);
  94. value = readl((*ctrl)->control_pbiaslite);
  95. value |= MMC1_PBIASLITE_VMODE | MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ;
  96. writel(value, (*ctrl)->control_pbiaslite);
  97. }
  98. #endif
  99. #if defined(CONFIG_OMAP54XX) && defined(CONFIG_PALMAS_POWER)
  100. static void omap5_pbias_config(struct mmc *mmc)
  101. {
  102. u32 value = 0;
  103. value = readl((*ctrl)->control_pbias);
  104. value &= ~SDCARD_PWRDNZ;
  105. writel(value, (*ctrl)->control_pbias);
  106. udelay(10); /* wait 10 us */
  107. value &= ~SDCARD_BIAS_PWRDNZ;
  108. writel(value, (*ctrl)->control_pbias);
  109. palmas_mmc1_poweron_ldo();
  110. value = readl((*ctrl)->control_pbias);
  111. value |= SDCARD_BIAS_PWRDNZ;
  112. writel(value, (*ctrl)->control_pbias);
  113. udelay(150); /* wait 150 us */
  114. value |= SDCARD_PWRDNZ;
  115. writel(value, (*ctrl)->control_pbias);
  116. udelay(150); /* wait 150 us */
  117. }
  118. #endif
  119. static unsigned char mmc_board_init(struct mmc *mmc)
  120. {
  121. #if defined(CONFIG_OMAP34XX)
  122. t2_t *t2_base = (t2_t *)T2_BASE;
  123. struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
  124. u32 pbias_lite;
  125. pbias_lite = readl(&t2_base->pbias_lite);
  126. pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
  127. #ifdef CONFIG_TARGET_OMAP3_CAIRO
  128. /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
  129. pbias_lite &= ~PBIASLITEVMODE0;
  130. #endif
  131. writel(pbias_lite, &t2_base->pbias_lite);
  132. writel(pbias_lite | PBIASLITEPWRDNZ1 |
  133. PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
  134. &t2_base->pbias_lite);
  135. writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
  136. &t2_base->devconf0);
  137. writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
  138. &t2_base->devconf1);
  139. /* Change from default of 52MHz to 26MHz if necessary */
  140. if (!(mmc->cfg->host_caps & MMC_MODE_HS_52MHz))
  141. writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
  142. &t2_base->ctl_prog_io1);
  143. writel(readl(&prcm_base->fclken1_core) |
  144. EN_MMC1 | EN_MMC2 | EN_MMC3,
  145. &prcm_base->fclken1_core);
  146. writel(readl(&prcm_base->iclken1_core) |
  147. EN_MMC1 | EN_MMC2 | EN_MMC3,
  148. &prcm_base->iclken1_core);
  149. #endif
  150. #if defined(CONFIG_OMAP44XX)
  151. /* PBIAS config needed for MMC1 only */
  152. if (mmc->block_dev.devnum == 0)
  153. omap4_vmmc_pbias_config(mmc);
  154. #endif
  155. #if defined(CONFIG_OMAP54XX) && defined(CONFIG_PALMAS_POWER)
  156. if (mmc->block_dev.devnum == 0)
  157. omap5_pbias_config(mmc);
  158. #endif
  159. return 0;
  160. }
  161. void mmc_init_stream(struct hsmmc *mmc_base)
  162. {
  163. ulong start;
  164. writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
  165. writel(MMC_CMD0, &mmc_base->cmd);
  166. start = get_timer(0);
  167. while (!(readl(&mmc_base->stat) & CC_MASK)) {
  168. if (get_timer(0) - start > MAX_RETRY_MS) {
  169. printf("%s: timedout waiting for cc!\n", __func__);
  170. return;
  171. }
  172. }
  173. writel(CC_MASK, &mmc_base->stat)
  174. ;
  175. writel(MMC_CMD0, &mmc_base->cmd)
  176. ;
  177. start = get_timer(0);
  178. while (!(readl(&mmc_base->stat) & CC_MASK)) {
  179. if (get_timer(0) - start > MAX_RETRY_MS) {
  180. printf("%s: timedout waiting for cc2!\n", __func__);
  181. return;
  182. }
  183. }
  184. writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
  185. }
  186. static int omap_hsmmc_init_setup(struct mmc *mmc)
  187. {
  188. struct hsmmc *mmc_base;
  189. unsigned int reg_val;
  190. unsigned int dsor;
  191. ulong start;
  192. mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
  193. mmc_board_init(mmc);
  194. writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
  195. &mmc_base->sysconfig);
  196. start = get_timer(0);
  197. while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
  198. if (get_timer(0) - start > MAX_RETRY_MS) {
  199. printf("%s: timedout waiting for cc2!\n", __func__);
  200. return TIMEOUT;
  201. }
  202. }
  203. writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
  204. start = get_timer(0);
  205. while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
  206. if (get_timer(0) - start > MAX_RETRY_MS) {
  207. printf("%s: timedout waiting for softresetall!\n",
  208. __func__);
  209. return TIMEOUT;
  210. }
  211. }
  212. writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
  213. writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
  214. &mmc_base->capa);
  215. reg_val = readl(&mmc_base->con) & RESERVED_MASK;
  216. writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
  217. MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
  218. HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
  219. dsor = 240;
  220. mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
  221. (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
  222. mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
  223. (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
  224. start = get_timer(0);
  225. while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
  226. if (get_timer(0) - start > MAX_RETRY_MS) {
  227. printf("%s: timedout waiting for ics!\n", __func__);
  228. return TIMEOUT;
  229. }
  230. }
  231. writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
  232. writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
  233. writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
  234. IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
  235. &mmc_base->ie);
  236. mmc_init_stream(mmc_base);
  237. return 0;
  238. }
  239. /*
  240. * MMC controller internal finite state machine reset
  241. *
  242. * Used to reset command or data internal state machines, using respectively
  243. * SRC or SRD bit of SYSCTL register
  244. */
  245. static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
  246. {
  247. ulong start;
  248. mmc_reg_out(&mmc_base->sysctl, bit, bit);
  249. /*
  250. * CMD(DAT) lines reset procedures are slightly different
  251. * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
  252. * According to OMAP3 TRM:
  253. * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
  254. * returns to 0x0.
  255. * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
  256. * procedure steps must be as follows:
  257. * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
  258. * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
  259. * 2. Poll the SRC(SRD) bit until it is set to 0x1.
  260. * 3. Wait until the SRC (SRD) bit returns to 0x0
  261. * (reset procedure is completed).
  262. */
  263. #if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
  264. defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
  265. if (!(readl(&mmc_base->sysctl) & bit)) {
  266. start = get_timer(0);
  267. while (!(readl(&mmc_base->sysctl) & bit)) {
  268. if (get_timer(0) - start > MAX_RETRY_MS)
  269. return;
  270. }
  271. }
  272. #endif
  273. start = get_timer(0);
  274. while ((readl(&mmc_base->sysctl) & bit) != 0) {
  275. if (get_timer(0) - start > MAX_RETRY_MS) {
  276. printf("%s: timedout waiting for sysctl %x to clear\n",
  277. __func__, bit);
  278. return;
  279. }
  280. }
  281. }
  282. static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  283. struct mmc_data *data)
  284. {
  285. struct hsmmc *mmc_base;
  286. unsigned int flags, mmc_stat;
  287. ulong start;
  288. mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
  289. start = get_timer(0);
  290. while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
  291. if (get_timer(0) - start > MAX_RETRY_MS) {
  292. printf("%s: timedout waiting on cmd inhibit to clear\n",
  293. __func__);
  294. return TIMEOUT;
  295. }
  296. }
  297. writel(0xFFFFFFFF, &mmc_base->stat);
  298. start = get_timer(0);
  299. while (readl(&mmc_base->stat)) {
  300. if (get_timer(0) - start > MAX_RETRY_MS) {
  301. printf("%s: timedout waiting for STAT (%x) to clear\n",
  302. __func__, readl(&mmc_base->stat));
  303. return TIMEOUT;
  304. }
  305. }
  306. /*
  307. * CMDREG
  308. * CMDIDX[13:8] : Command index
  309. * DATAPRNT[5] : Data Present Select
  310. * ENCMDIDX[4] : Command Index Check Enable
  311. * ENCMDCRC[3] : Command CRC Check Enable
  312. * RSPTYP[1:0]
  313. * 00 = No Response
  314. * 01 = Length 136
  315. * 10 = Length 48
  316. * 11 = Length 48 Check busy after response
  317. */
  318. /* Delay added before checking the status of frq change
  319. * retry not supported by mmc.c(core file)
  320. */
  321. if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
  322. udelay(50000); /* wait 50 ms */
  323. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  324. flags = 0;
  325. else if (cmd->resp_type & MMC_RSP_136)
  326. flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
  327. else if (cmd->resp_type & MMC_RSP_BUSY)
  328. flags = RSP_TYPE_LGHT48B;
  329. else
  330. flags = RSP_TYPE_LGHT48;
  331. /* enable default flags */
  332. flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
  333. MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE);
  334. if (cmd->resp_type & MMC_RSP_CRC)
  335. flags |= CCCE_CHECK;
  336. if (cmd->resp_type & MMC_RSP_OPCODE)
  337. flags |= CICE_CHECK;
  338. if (data) {
  339. if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
  340. (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
  341. flags |= (MSBS_MULTIBLK | BCE_ENABLE);
  342. data->blocksize = 512;
  343. writel(data->blocksize | (data->blocks << 16),
  344. &mmc_base->blk);
  345. } else
  346. writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
  347. if (data->flags & MMC_DATA_READ)
  348. flags |= (DP_DATA | DDIR_READ);
  349. else
  350. flags |= (DP_DATA | DDIR_WRITE);
  351. }
  352. writel(cmd->cmdarg, &mmc_base->arg);
  353. udelay(20); /* To fix "No status update" error on eMMC */
  354. writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
  355. start = get_timer(0);
  356. do {
  357. mmc_stat = readl(&mmc_base->stat);
  358. if (get_timer(0) - start > MAX_RETRY_MS) {
  359. printf("%s : timeout: No status update\n", __func__);
  360. return TIMEOUT;
  361. }
  362. } while (!mmc_stat);
  363. if ((mmc_stat & IE_CTO) != 0) {
  364. mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
  365. return TIMEOUT;
  366. } else if ((mmc_stat & ERRI_MASK) != 0)
  367. return -1;
  368. if (mmc_stat & CC_MASK) {
  369. writel(CC_MASK, &mmc_base->stat);
  370. if (cmd->resp_type & MMC_RSP_PRESENT) {
  371. if (cmd->resp_type & MMC_RSP_136) {
  372. /* response type 2 */
  373. cmd->response[3] = readl(&mmc_base->rsp10);
  374. cmd->response[2] = readl(&mmc_base->rsp32);
  375. cmd->response[1] = readl(&mmc_base->rsp54);
  376. cmd->response[0] = readl(&mmc_base->rsp76);
  377. } else
  378. /* response types 1, 1b, 3, 4, 5, 6 */
  379. cmd->response[0] = readl(&mmc_base->rsp10);
  380. }
  381. }
  382. if (data && (data->flags & MMC_DATA_READ)) {
  383. mmc_read_data(mmc_base, data->dest,
  384. data->blocksize * data->blocks);
  385. } else if (data && (data->flags & MMC_DATA_WRITE)) {
  386. mmc_write_data(mmc_base, data->src,
  387. data->blocksize * data->blocks);
  388. }
  389. return 0;
  390. }
  391. static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
  392. {
  393. unsigned int *output_buf = (unsigned int *)buf;
  394. unsigned int mmc_stat;
  395. unsigned int count;
  396. /*
  397. * Start Polled Read
  398. */
  399. count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
  400. count /= 4;
  401. while (size) {
  402. ulong start = get_timer(0);
  403. do {
  404. mmc_stat = readl(&mmc_base->stat);
  405. if (get_timer(0) - start > MAX_RETRY_MS) {
  406. printf("%s: timedout waiting for status!\n",
  407. __func__);
  408. return TIMEOUT;
  409. }
  410. } while (mmc_stat == 0);
  411. if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
  412. mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
  413. if ((mmc_stat & ERRI_MASK) != 0)
  414. return 1;
  415. if (mmc_stat & BRR_MASK) {
  416. unsigned int k;
  417. writel(readl(&mmc_base->stat) | BRR_MASK,
  418. &mmc_base->stat);
  419. for (k = 0; k < count; k++) {
  420. *output_buf = readl(&mmc_base->data);
  421. output_buf++;
  422. }
  423. size -= (count*4);
  424. }
  425. if (mmc_stat & BWR_MASK)
  426. writel(readl(&mmc_base->stat) | BWR_MASK,
  427. &mmc_base->stat);
  428. if (mmc_stat & TC_MASK) {
  429. writel(readl(&mmc_base->stat) | TC_MASK,
  430. &mmc_base->stat);
  431. break;
  432. }
  433. }
  434. return 0;
  435. }
  436. static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
  437. unsigned int size)
  438. {
  439. unsigned int *input_buf = (unsigned int *)buf;
  440. unsigned int mmc_stat;
  441. unsigned int count;
  442. /*
  443. * Start Polled Write
  444. */
  445. count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
  446. count /= 4;
  447. while (size) {
  448. ulong start = get_timer(0);
  449. do {
  450. mmc_stat = readl(&mmc_base->stat);
  451. if (get_timer(0) - start > MAX_RETRY_MS) {
  452. printf("%s: timedout waiting for status!\n",
  453. __func__);
  454. return TIMEOUT;
  455. }
  456. } while (mmc_stat == 0);
  457. if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
  458. mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
  459. if ((mmc_stat & ERRI_MASK) != 0)
  460. return 1;
  461. if (mmc_stat & BWR_MASK) {
  462. unsigned int k;
  463. writel(readl(&mmc_base->stat) | BWR_MASK,
  464. &mmc_base->stat);
  465. for (k = 0; k < count; k++) {
  466. writel(*input_buf, &mmc_base->data);
  467. input_buf++;
  468. }
  469. size -= (count*4);
  470. }
  471. if (mmc_stat & BRR_MASK)
  472. writel(readl(&mmc_base->stat) | BRR_MASK,
  473. &mmc_base->stat);
  474. if (mmc_stat & TC_MASK) {
  475. writel(readl(&mmc_base->stat) | TC_MASK,
  476. &mmc_base->stat);
  477. break;
  478. }
  479. }
  480. return 0;
  481. }
  482. static void omap_hsmmc_set_ios(struct mmc *mmc)
  483. {
  484. struct hsmmc *mmc_base;
  485. unsigned int dsor = 0;
  486. ulong start;
  487. mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
  488. /* configue bus width */
  489. switch (mmc->bus_width) {
  490. case 8:
  491. writel(readl(&mmc_base->con) | DTW_8_BITMODE,
  492. &mmc_base->con);
  493. break;
  494. case 4:
  495. writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
  496. &mmc_base->con);
  497. writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
  498. &mmc_base->hctl);
  499. break;
  500. case 1:
  501. default:
  502. writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
  503. &mmc_base->con);
  504. writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
  505. &mmc_base->hctl);
  506. break;
  507. }
  508. /* configure clock with 96Mhz system clock.
  509. */
  510. if (mmc->clock != 0) {
  511. dsor = (MMC_CLOCK_REFERENCE * 1000000 / mmc->clock);
  512. if ((MMC_CLOCK_REFERENCE * 1000000) / dsor > mmc->clock)
  513. dsor++;
  514. }
  515. mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
  516. (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
  517. mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
  518. (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
  519. start = get_timer(0);
  520. while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
  521. if (get_timer(0) - start > MAX_RETRY_MS) {
  522. printf("%s: timedout waiting for ics!\n", __func__);
  523. return;
  524. }
  525. }
  526. writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
  527. }
  528. #ifdef OMAP_HSMMC_USE_GPIO
  529. #ifdef CONFIG_DM_MMC
  530. static int omap_hsmmc_getcd(struct mmc *mmc)
  531. {
  532. struct omap_hsmmc_data *priv = mmc->priv;
  533. int value;
  534. value = dm_gpio_get_value(&priv->cd_gpio);
  535. /* if no CD return as 1 */
  536. if (value < 0)
  537. return 1;
  538. if (priv->cd_inverted)
  539. return !value;
  540. return value;
  541. }
  542. static int omap_hsmmc_getwp(struct mmc *mmc)
  543. {
  544. struct omap_hsmmc_data *priv = mmc->priv;
  545. int value;
  546. value = dm_gpio_get_value(&priv->wp_gpio);
  547. /* if no WP return as 0 */
  548. if (value < 0)
  549. return 0;
  550. return value;
  551. }
  552. #else
  553. static int omap_hsmmc_getcd(struct mmc *mmc)
  554. {
  555. struct omap_hsmmc_data *priv_data = mmc->priv;
  556. int cd_gpio;
  557. /* if no CD return as 1 */
  558. cd_gpio = priv_data->cd_gpio;
  559. if (cd_gpio < 0)
  560. return 1;
  561. /* NOTE: assumes card detect signal is active-low */
  562. return !gpio_get_value(cd_gpio);
  563. }
  564. static int omap_hsmmc_getwp(struct mmc *mmc)
  565. {
  566. struct omap_hsmmc_data *priv_data = mmc->priv;
  567. int wp_gpio;
  568. /* if no WP return as 0 */
  569. wp_gpio = priv_data->wp_gpio;
  570. if (wp_gpio < 0)
  571. return 0;
  572. /* NOTE: assumes write protect signal is active-high */
  573. return gpio_get_value(wp_gpio);
  574. }
  575. #endif
  576. #endif
  577. static const struct mmc_ops omap_hsmmc_ops = {
  578. .send_cmd = omap_hsmmc_send_cmd,
  579. .set_ios = omap_hsmmc_set_ios,
  580. .init = omap_hsmmc_init_setup,
  581. #ifdef OMAP_HSMMC_USE_GPIO
  582. .getcd = omap_hsmmc_getcd,
  583. .getwp = omap_hsmmc_getwp,
  584. #endif
  585. };
  586. #ifndef CONFIG_DM_MMC
  587. int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
  588. int wp_gpio)
  589. {
  590. struct mmc *mmc;
  591. struct omap_hsmmc_data *priv_data;
  592. struct mmc_config *cfg;
  593. uint host_caps_val;
  594. priv_data = malloc(sizeof(*priv_data));
  595. if (priv_data == NULL)
  596. return -1;
  597. host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
  598. switch (dev_index) {
  599. case 0:
  600. priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
  601. break;
  602. #ifdef OMAP_HSMMC2_BASE
  603. case 1:
  604. priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
  605. #if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
  606. defined(CONFIG_DRA7XX) || defined(CONFIG_AM57XX) || \
  607. defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
  608. defined(CONFIG_HSMMC2_8BIT)
  609. /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
  610. host_caps_val |= MMC_MODE_8BIT;
  611. #endif
  612. break;
  613. #endif
  614. #ifdef OMAP_HSMMC3_BASE
  615. case 2:
  616. priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
  617. #if (defined(CONFIG_DRA7XX) || defined(CONFIG_AM57XX)) && defined(CONFIG_HSMMC3_8BIT)
  618. /* Enable 8-bit interface for eMMC on DRA7XX */
  619. host_caps_val |= MMC_MODE_8BIT;
  620. #endif
  621. break;
  622. #endif
  623. default:
  624. priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
  625. return 1;
  626. }
  627. #ifdef OMAP_HSMMC_USE_GPIO
  628. /* on error gpio values are set to -1, which is what we want */
  629. priv_data->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
  630. priv_data->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
  631. #endif
  632. cfg = &priv_data->cfg;
  633. cfg->name = "OMAP SD/MMC";
  634. cfg->ops = &omap_hsmmc_ops;
  635. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  636. cfg->host_caps = host_caps_val & ~host_caps_mask;
  637. cfg->f_min = 400000;
  638. if (f_max != 0)
  639. cfg->f_max = f_max;
  640. else {
  641. if (cfg->host_caps & MMC_MODE_HS) {
  642. if (cfg->host_caps & MMC_MODE_HS_52MHz)
  643. cfg->f_max = 52000000;
  644. else
  645. cfg->f_max = 26000000;
  646. } else
  647. cfg->f_max = 20000000;
  648. }
  649. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  650. #if defined(CONFIG_OMAP34XX)
  651. /*
  652. * Silicon revs 2.1 and older do not support multiblock transfers.
  653. */
  654. if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
  655. cfg->b_max = 1;
  656. #endif
  657. mmc = mmc_create(cfg, priv_data);
  658. if (mmc == NULL)
  659. return -1;
  660. return 0;
  661. }
  662. #else
  663. static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
  664. {
  665. struct omap_hsmmc_data *priv = dev_get_priv(dev);
  666. const void *fdt = gd->fdt_blob;
  667. int node = dev->of_offset;
  668. struct mmc_config *cfg;
  669. int val;
  670. priv->base_addr = (struct hsmmc *)dev_get_addr(dev);
  671. cfg = &priv->cfg;
  672. cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
  673. val = fdtdec_get_int(fdt, node, "bus-width", -1);
  674. if (val < 0) {
  675. printf("error: bus-width property missing\n");
  676. return -ENOENT;
  677. }
  678. switch (val) {
  679. case 0x8:
  680. cfg->host_caps |= MMC_MODE_8BIT;
  681. case 0x4:
  682. cfg->host_caps |= MMC_MODE_4BIT;
  683. break;
  684. default:
  685. printf("error: invalid bus-width property\n");
  686. return -ENOENT;
  687. }
  688. cfg->f_min = 400000;
  689. cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000);
  690. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  691. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  692. priv->cd_inverted = fdtdec_get_bool(fdt, node, "cd-inverted");
  693. return 0;
  694. }
  695. static int omap_hsmmc_probe(struct udevice *dev)
  696. {
  697. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  698. struct omap_hsmmc_data *priv = dev_get_priv(dev);
  699. struct mmc_config *cfg;
  700. struct mmc *mmc;
  701. cfg = &priv->cfg;
  702. cfg->name = "OMAP SD/MMC";
  703. cfg->ops = &omap_hsmmc_ops;
  704. mmc = mmc_create(cfg, priv);
  705. if (mmc == NULL)
  706. return -1;
  707. upriv->mmc = mmc;
  708. return 0;
  709. }
  710. static const struct udevice_id omap_hsmmc_ids[] = {
  711. { .compatible = "ti,omap3-hsmmc" },
  712. { .compatible = "ti,omap4-hsmmc" },
  713. { .compatible = "ti,am33xx-hsmmc" },
  714. { }
  715. };
  716. U_BOOT_DRIVER(omap_hsmmc) = {
  717. .name = "omap_hsmmc",
  718. .id = UCLASS_MMC,
  719. .of_match = omap_hsmmc_ids,
  720. .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
  721. .probe = omap_hsmmc_probe,
  722. .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
  723. };
  724. #endif