omap_hsmmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 <mmc.h>
  27. #include <part.h>
  28. #include <i2c.h>
  29. #include <twl4030.h>
  30. #include <twl6030.h>
  31. #include <twl6035.h>
  32. #include <asm/io.h>
  33. #include <asm/arch/mmc_host_def.h>
  34. #include <asm/arch/sys_proto.h>
  35. /* If we fail after 1 second wait, something is really bad */
  36. #define MAX_RETRY_MS 1000
  37. static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
  38. static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
  39. unsigned int siz);
  40. static struct mmc hsmmc_dev[2];
  41. #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
  42. static void omap4_vmmc_pbias_config(struct mmc *mmc)
  43. {
  44. u32 value = 0;
  45. struct omap_sys_ctrl_regs *const ctrl =
  46. (struct omap_sys_ctrl_regs *) SYSCTRL_GENERAL_CORE_BASE;
  47. value = readl(&ctrl->control_pbiaslite);
  48. value &= ~(MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ);
  49. writel(value, &ctrl->control_pbiaslite);
  50. /* set VMMC to 3V */
  51. twl6030_power_mmc_init();
  52. value = readl(&ctrl->control_pbiaslite);
  53. value |= MMC1_PBIASLITE_VMODE | MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ;
  54. writel(value, &ctrl->control_pbiaslite);
  55. }
  56. #endif
  57. #if defined(CONFIG_OMAP54XX) && defined(CONFIG_TWL6035_POWER)
  58. static void omap5_pbias_config(struct mmc *mmc)
  59. {
  60. u32 value = 0;
  61. struct omap_sys_ctrl_regs *const ctrl =
  62. (struct omap_sys_ctrl_regs *) SYSCTRL_GENERAL_CORE_BASE;
  63. value = readl(&ctrl->control_pbias);
  64. value &= ~(SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ);
  65. value |= SDCARD_BIAS_HIZ_MODE;
  66. writel(value, &ctrl->control_pbias);
  67. twl6035_mmc1_poweron_ldo();
  68. value = readl(&ctrl->control_pbias);
  69. value &= ~SDCARD_BIAS_HIZ_MODE;
  70. value |= SDCARD_PBIASLITE_VMODE | SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ;
  71. writel(value, &ctrl->control_pbias);
  72. value = readl(&ctrl->control_pbias);
  73. if (value & (1 << 23)) {
  74. value &= ~(SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ);
  75. value |= SDCARD_BIAS_HIZ_MODE;
  76. writel(value, &ctrl->control_pbias);
  77. }
  78. }
  79. #endif
  80. unsigned char mmc_board_init(struct mmc *mmc)
  81. {
  82. #if defined(CONFIG_TWL4030_POWER)
  83. twl4030_power_mmc_init();
  84. #endif
  85. #if defined(CONFIG_OMAP34XX)
  86. t2_t *t2_base = (t2_t *)T2_BASE;
  87. struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
  88. writel(readl(&t2_base->pbias_lite) | PBIASLITEPWRDNZ1 |
  89. PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
  90. &t2_base->pbias_lite);
  91. writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
  92. &t2_base->devconf0);
  93. writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
  94. &t2_base->devconf1);
  95. /* Change from default of 52MHz to 26MHz if necessary */
  96. if (!(mmc->host_caps & MMC_MODE_HS_52MHz))
  97. writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
  98. &t2_base->ctl_prog_io1);
  99. writel(readl(&prcm_base->fclken1_core) |
  100. EN_MMC1 | EN_MMC2 | EN_MMC3,
  101. &prcm_base->fclken1_core);
  102. writel(readl(&prcm_base->iclken1_core) |
  103. EN_MMC1 | EN_MMC2 | EN_MMC3,
  104. &prcm_base->iclken1_core);
  105. #endif
  106. #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
  107. /* PBIAS config needed for MMC1 only */
  108. if (mmc->block_dev.dev == 0)
  109. omap4_vmmc_pbias_config(mmc);
  110. #endif
  111. #if defined(CONFIG_OMAP54XX) && defined(CONFIG_TWL6035_POWER)
  112. if (mmc->block_dev.dev == 0)
  113. omap5_pbias_config(mmc);
  114. #endif
  115. return 0;
  116. }
  117. void mmc_init_stream(struct hsmmc *mmc_base)
  118. {
  119. ulong start;
  120. writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
  121. writel(MMC_CMD0, &mmc_base->cmd);
  122. start = get_timer(0);
  123. while (!(readl(&mmc_base->stat) & CC_MASK)) {
  124. if (get_timer(0) - start > MAX_RETRY_MS) {
  125. printf("%s: timedout waiting for cc!\n", __func__);
  126. return;
  127. }
  128. }
  129. writel(CC_MASK, &mmc_base->stat)
  130. ;
  131. writel(MMC_CMD0, &mmc_base->cmd)
  132. ;
  133. start = get_timer(0);
  134. while (!(readl(&mmc_base->stat) & CC_MASK)) {
  135. if (get_timer(0) - start > MAX_RETRY_MS) {
  136. printf("%s: timedout waiting for cc2!\n", __func__);
  137. return;
  138. }
  139. }
  140. writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
  141. }
  142. static int mmc_init_setup(struct mmc *mmc)
  143. {
  144. struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
  145. unsigned int reg_val;
  146. unsigned int dsor;
  147. ulong start;
  148. mmc_board_init(mmc);
  149. writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
  150. &mmc_base->sysconfig);
  151. start = get_timer(0);
  152. while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
  153. if (get_timer(0) - start > MAX_RETRY_MS) {
  154. printf("%s: timedout waiting for cc2!\n", __func__);
  155. return TIMEOUT;
  156. }
  157. }
  158. writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
  159. start = get_timer(0);
  160. while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
  161. if (get_timer(0) - start > MAX_RETRY_MS) {
  162. printf("%s: timedout waiting for softresetall!\n",
  163. __func__);
  164. return TIMEOUT;
  165. }
  166. }
  167. writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
  168. writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
  169. &mmc_base->capa);
  170. reg_val = readl(&mmc_base->con) & RESERVED_MASK;
  171. writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
  172. MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
  173. HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
  174. dsor = 240;
  175. mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
  176. (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
  177. mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
  178. (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
  179. start = get_timer(0);
  180. while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
  181. if (get_timer(0) - start > MAX_RETRY_MS) {
  182. printf("%s: timedout waiting for ics!\n", __func__);
  183. return TIMEOUT;
  184. }
  185. }
  186. writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
  187. writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
  188. writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
  189. IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
  190. &mmc_base->ie);
  191. mmc_init_stream(mmc_base);
  192. return 0;
  193. }
  194. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  195. struct mmc_data *data)
  196. {
  197. struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
  198. unsigned int flags, mmc_stat;
  199. ulong start;
  200. start = get_timer(0);
  201. while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
  202. if (get_timer(0) - start > MAX_RETRY_MS) {
  203. printf("%s: timedout waiting on cmd inhibit to clear\n",
  204. __func__);
  205. return TIMEOUT;
  206. }
  207. }
  208. writel(0xFFFFFFFF, &mmc_base->stat);
  209. start = get_timer(0);
  210. while (readl(&mmc_base->stat)) {
  211. if (get_timer(0) - start > MAX_RETRY_MS) {
  212. printf("%s: timedout waiting for stat!\n", __func__);
  213. return TIMEOUT;
  214. }
  215. }
  216. /*
  217. * CMDREG
  218. * CMDIDX[13:8] : Command index
  219. * DATAPRNT[5] : Data Present Select
  220. * ENCMDIDX[4] : Command Index Check Enable
  221. * ENCMDCRC[3] : Command CRC Check Enable
  222. * RSPTYP[1:0]
  223. * 00 = No Response
  224. * 01 = Length 136
  225. * 10 = Length 48
  226. * 11 = Length 48 Check busy after response
  227. */
  228. /* Delay added before checking the status of frq change
  229. * retry not supported by mmc.c(core file)
  230. */
  231. if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
  232. udelay(50000); /* wait 50 ms */
  233. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  234. flags = 0;
  235. else if (cmd->resp_type & MMC_RSP_136)
  236. flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
  237. else if (cmd->resp_type & MMC_RSP_BUSY)
  238. flags = RSP_TYPE_LGHT48B;
  239. else
  240. flags = RSP_TYPE_LGHT48;
  241. /* enable default flags */
  242. flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
  243. MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE);
  244. if (cmd->resp_type & MMC_RSP_CRC)
  245. flags |= CCCE_CHECK;
  246. if (cmd->resp_type & MMC_RSP_OPCODE)
  247. flags |= CICE_CHECK;
  248. if (data) {
  249. if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
  250. (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
  251. flags |= (MSBS_MULTIBLK | BCE_ENABLE);
  252. data->blocksize = 512;
  253. writel(data->blocksize | (data->blocks << 16),
  254. &mmc_base->blk);
  255. } else
  256. writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
  257. if (data->flags & MMC_DATA_READ)
  258. flags |= (DP_DATA | DDIR_READ);
  259. else
  260. flags |= (DP_DATA | DDIR_WRITE);
  261. }
  262. writel(cmd->cmdarg, &mmc_base->arg);
  263. writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
  264. start = get_timer(0);
  265. do {
  266. mmc_stat = readl(&mmc_base->stat);
  267. if (get_timer(0) - start > MAX_RETRY_MS) {
  268. printf("%s : timeout: No status update\n", __func__);
  269. return TIMEOUT;
  270. }
  271. } while (!mmc_stat);
  272. if ((mmc_stat & IE_CTO) != 0)
  273. return TIMEOUT;
  274. else if ((mmc_stat & ERRI_MASK) != 0)
  275. return -1;
  276. if (mmc_stat & CC_MASK) {
  277. writel(CC_MASK, &mmc_base->stat);
  278. if (cmd->resp_type & MMC_RSP_PRESENT) {
  279. if (cmd->resp_type & MMC_RSP_136) {
  280. /* response type 2 */
  281. cmd->response[3] = readl(&mmc_base->rsp10);
  282. cmd->response[2] = readl(&mmc_base->rsp32);
  283. cmd->response[1] = readl(&mmc_base->rsp54);
  284. cmd->response[0] = readl(&mmc_base->rsp76);
  285. } else
  286. /* response types 1, 1b, 3, 4, 5, 6 */
  287. cmd->response[0] = readl(&mmc_base->rsp10);
  288. }
  289. }
  290. if (data && (data->flags & MMC_DATA_READ)) {
  291. mmc_read_data(mmc_base, data->dest,
  292. data->blocksize * data->blocks);
  293. } else if (data && (data->flags & MMC_DATA_WRITE)) {
  294. mmc_write_data(mmc_base, data->src,
  295. data->blocksize * data->blocks);
  296. }
  297. return 0;
  298. }
  299. static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
  300. {
  301. unsigned int *output_buf = (unsigned int *)buf;
  302. unsigned int mmc_stat;
  303. unsigned int count;
  304. /*
  305. * Start Polled Read
  306. */
  307. count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
  308. count /= 4;
  309. while (size) {
  310. ulong start = get_timer(0);
  311. do {
  312. mmc_stat = readl(&mmc_base->stat);
  313. if (get_timer(0) - start > MAX_RETRY_MS) {
  314. printf("%s: timedout waiting for status!\n",
  315. __func__);
  316. return TIMEOUT;
  317. }
  318. } while (mmc_stat == 0);
  319. if ((mmc_stat & ERRI_MASK) != 0)
  320. return 1;
  321. if (mmc_stat & BRR_MASK) {
  322. unsigned int k;
  323. writel(readl(&mmc_base->stat) | BRR_MASK,
  324. &mmc_base->stat);
  325. for (k = 0; k < count; k++) {
  326. *output_buf = readl(&mmc_base->data);
  327. output_buf++;
  328. }
  329. size -= (count*4);
  330. }
  331. if (mmc_stat & BWR_MASK)
  332. writel(readl(&mmc_base->stat) | BWR_MASK,
  333. &mmc_base->stat);
  334. if (mmc_stat & TC_MASK) {
  335. writel(readl(&mmc_base->stat) | TC_MASK,
  336. &mmc_base->stat);
  337. break;
  338. }
  339. }
  340. return 0;
  341. }
  342. static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
  343. unsigned int size)
  344. {
  345. unsigned int *input_buf = (unsigned int *)buf;
  346. unsigned int mmc_stat;
  347. unsigned int count;
  348. /*
  349. * Start Polled Read
  350. */
  351. count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
  352. count /= 4;
  353. while (size) {
  354. ulong start = get_timer(0);
  355. do {
  356. mmc_stat = readl(&mmc_base->stat);
  357. if (get_timer(0) - start > MAX_RETRY_MS) {
  358. printf("%s: timedout waiting for status!\n",
  359. __func__);
  360. return TIMEOUT;
  361. }
  362. } while (mmc_stat == 0);
  363. if ((mmc_stat & ERRI_MASK) != 0)
  364. return 1;
  365. if (mmc_stat & BWR_MASK) {
  366. unsigned int k;
  367. writel(readl(&mmc_base->stat) | BWR_MASK,
  368. &mmc_base->stat);
  369. for (k = 0; k < count; k++) {
  370. writel(*input_buf, &mmc_base->data);
  371. input_buf++;
  372. }
  373. size -= (count*4);
  374. }
  375. if (mmc_stat & BRR_MASK)
  376. writel(readl(&mmc_base->stat) | BRR_MASK,
  377. &mmc_base->stat);
  378. if (mmc_stat & TC_MASK) {
  379. writel(readl(&mmc_base->stat) | TC_MASK,
  380. &mmc_base->stat);
  381. break;
  382. }
  383. }
  384. return 0;
  385. }
  386. static void mmc_set_ios(struct mmc *mmc)
  387. {
  388. struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
  389. unsigned int dsor = 0;
  390. ulong start;
  391. /* configue bus width */
  392. switch (mmc->bus_width) {
  393. case 8:
  394. writel(readl(&mmc_base->con) | DTW_8_BITMODE,
  395. &mmc_base->con);
  396. break;
  397. case 4:
  398. writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
  399. &mmc_base->con);
  400. writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
  401. &mmc_base->hctl);
  402. break;
  403. case 1:
  404. default:
  405. writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
  406. &mmc_base->con);
  407. writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
  408. &mmc_base->hctl);
  409. break;
  410. }
  411. /* configure clock with 96Mhz system clock.
  412. */
  413. if (mmc->clock != 0) {
  414. dsor = (MMC_CLOCK_REFERENCE * 1000000 / mmc->clock);
  415. if ((MMC_CLOCK_REFERENCE * 1000000) / dsor > mmc->clock)
  416. dsor++;
  417. }
  418. mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
  419. (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
  420. mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
  421. (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
  422. start = get_timer(0);
  423. while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
  424. if (get_timer(0) - start > MAX_RETRY_MS) {
  425. printf("%s: timedout waiting for ics!\n", __func__);
  426. return;
  427. }
  428. }
  429. writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
  430. }
  431. int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max)
  432. {
  433. struct mmc *mmc;
  434. mmc = &hsmmc_dev[dev_index];
  435. sprintf(mmc->name, "OMAP SD/MMC");
  436. mmc->send_cmd = mmc_send_cmd;
  437. mmc->set_ios = mmc_set_ios;
  438. mmc->init = mmc_init_setup;
  439. mmc->getcd = NULL;
  440. switch (dev_index) {
  441. case 0:
  442. mmc->priv = (struct hsmmc *)OMAP_HSMMC1_BASE;
  443. break;
  444. #ifdef OMAP_HSMMC2_BASE
  445. case 1:
  446. mmc->priv = (struct hsmmc *)OMAP_HSMMC2_BASE;
  447. break;
  448. #endif
  449. #ifdef OMAP_HSMMC3_BASE
  450. case 2:
  451. mmc->priv = (struct hsmmc *)OMAP_HSMMC3_BASE;
  452. break;
  453. #endif
  454. default:
  455. mmc->priv = (struct hsmmc *)OMAP_HSMMC1_BASE;
  456. return 1;
  457. }
  458. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  459. mmc->host_caps = (MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS |
  460. MMC_MODE_HC) & ~host_caps_mask;
  461. mmc->f_min = 400000;
  462. if (f_max != 0)
  463. mmc->f_max = f_max;
  464. else {
  465. if (mmc->host_caps & MMC_MODE_HS) {
  466. if (mmc->host_caps & MMC_MODE_HS_52MHz)
  467. mmc->f_max = 52000000;
  468. else
  469. mmc->f_max = 26000000;
  470. } else
  471. mmc->f_max = 20000000;
  472. }
  473. mmc->b_max = 0;
  474. #if defined(CONFIG_OMAP34XX)
  475. /*
  476. * Silicon revs 2.1 and older do not support multiblock transfers.
  477. */
  478. if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
  479. mmc->b_max = 1;
  480. #endif
  481. mmc_register(mmc);
  482. return 0;
  483. }