omap_hsmmc.c 21 KB

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