hsmmc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * linux/arch/arm/mach-omap2/hsmmc.c
  3. *
  4. * Copyright (C) 2007-2008 Texas Instruments
  5. * Copyright (C) 2008 Nokia Corporation
  6. * Author: Texas Instruments
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/gpio.h>
  17. #include <mach/hardware.h>
  18. #include <plat/mmc.h>
  19. #include <plat/omap-pm.h>
  20. #include <plat/mux.h>
  21. #include <plat/omap_device.h>
  22. #include "mux.h"
  23. #include "hsmmc.h"
  24. #include "control.h"
  25. #if defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE)
  26. static u16 control_pbias_offset;
  27. static u16 control_devconf1_offset;
  28. static u16 control_mmc1;
  29. #define HSMMC_NAME_LEN 9
  30. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  31. static int hsmmc_get_context_loss(struct device *dev)
  32. {
  33. return omap_pm_get_dev_context_loss_count(dev);
  34. }
  35. #else
  36. #define hsmmc_get_context_loss NULL
  37. #endif
  38. static void omap_hsmmc1_before_set_reg(struct device *dev, int slot,
  39. int power_on, int vdd)
  40. {
  41. u32 reg, prog_io;
  42. struct omap_mmc_platform_data *mmc = dev->platform_data;
  43. if (mmc->slots[0].remux)
  44. mmc->slots[0].remux(dev, slot, power_on);
  45. /*
  46. * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
  47. * card with Vcc regulator (from twl4030 or whatever). OMAP has both
  48. * 1.8V and 3.0V modes, controlled by the PBIAS register.
  49. *
  50. * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
  51. * is most naturally TWL VSIM; those pins also use PBIAS.
  52. *
  53. * FIXME handle VMMC1A as needed ...
  54. */
  55. if (power_on) {
  56. if (cpu_is_omap2430()) {
  57. reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
  58. if ((1 << vdd) >= MMC_VDD_30_31)
  59. reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
  60. else
  61. reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
  62. omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
  63. }
  64. if (mmc->slots[0].internal_clock) {
  65. reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
  66. reg |= OMAP2_MMCSDIO1ADPCLKISEL;
  67. omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
  68. }
  69. reg = omap_ctrl_readl(control_pbias_offset);
  70. if (cpu_is_omap3630()) {
  71. /* Set MMC I/O to 52Mhz */
  72. prog_io = omap_ctrl_readl(OMAP343X_CONTROL_PROG_IO1);
  73. prog_io |= OMAP3630_PRG_SDMMC1_SPEEDCTRL;
  74. omap_ctrl_writel(prog_io, OMAP343X_CONTROL_PROG_IO1);
  75. } else {
  76. reg |= OMAP2_PBIASSPEEDCTRL0;
  77. }
  78. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  79. omap_ctrl_writel(reg, control_pbias_offset);
  80. } else {
  81. reg = omap_ctrl_readl(control_pbias_offset);
  82. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  83. omap_ctrl_writel(reg, control_pbias_offset);
  84. }
  85. }
  86. static void omap_hsmmc1_after_set_reg(struct device *dev, int slot,
  87. int power_on, int vdd)
  88. {
  89. u32 reg;
  90. /* 100ms delay required for PBIAS configuration */
  91. msleep(100);
  92. if (power_on) {
  93. reg = omap_ctrl_readl(control_pbias_offset);
  94. reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
  95. if ((1 << vdd) <= MMC_VDD_165_195)
  96. reg &= ~OMAP2_PBIASLITEVMODE0;
  97. else
  98. reg |= OMAP2_PBIASLITEVMODE0;
  99. omap_ctrl_writel(reg, control_pbias_offset);
  100. } else {
  101. reg = omap_ctrl_readl(control_pbias_offset);
  102. reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
  103. OMAP2_PBIASLITEVMODE0);
  104. omap_ctrl_writel(reg, control_pbias_offset);
  105. }
  106. }
  107. static void omap4_hsmmc1_before_set_reg(struct device *dev, int slot,
  108. int power_on, int vdd)
  109. {
  110. u32 reg;
  111. /*
  112. * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
  113. * card with Vcc regulator (from twl4030 or whatever). OMAP has both
  114. * 1.8V and 3.0V modes, controlled by the PBIAS register.
  115. */
  116. reg = omap4_ctrl_pad_readl(control_pbias_offset);
  117. reg &= ~(OMAP4_MMC1_PBIASLITE_PWRDNZ_MASK |
  118. OMAP4_MMC1_PWRDNZ_MASK |
  119. OMAP4_MMC1_PBIASLITE_VMODE_MASK);
  120. omap4_ctrl_pad_writel(reg, control_pbias_offset);
  121. }
  122. static void omap4_hsmmc1_after_set_reg(struct device *dev, int slot,
  123. int power_on, int vdd)
  124. {
  125. u32 reg;
  126. unsigned long timeout;
  127. if (power_on) {
  128. reg = omap4_ctrl_pad_readl(control_pbias_offset);
  129. reg |= OMAP4_MMC1_PBIASLITE_PWRDNZ_MASK;
  130. if ((1 << vdd) <= MMC_VDD_165_195)
  131. reg &= ~OMAP4_MMC1_PBIASLITE_VMODE_MASK;
  132. else
  133. reg |= OMAP4_MMC1_PBIASLITE_VMODE_MASK;
  134. reg |= (OMAP4_MMC1_PBIASLITE_PWRDNZ_MASK |
  135. OMAP4_MMC1_PWRDNZ_MASK);
  136. omap4_ctrl_pad_writel(reg, control_pbias_offset);
  137. timeout = jiffies + msecs_to_jiffies(5);
  138. do {
  139. reg = omap4_ctrl_pad_readl(control_pbias_offset);
  140. if (!(reg & OMAP4_MMC1_PBIASLITE_VMODE_ERROR_MASK))
  141. break;
  142. usleep_range(100, 200);
  143. } while (!time_after(jiffies, timeout));
  144. if (reg & OMAP4_MMC1_PBIASLITE_VMODE_ERROR_MASK) {
  145. pr_err("Pbias Voltage is not same as LDO\n");
  146. /* Caution : On VMODE_ERROR Power Down MMC IO */
  147. reg &= ~(OMAP4_MMC1_PWRDNZ_MASK);
  148. omap4_ctrl_pad_writel(reg, control_pbias_offset);
  149. }
  150. }
  151. }
  152. static void hsmmc23_before_set_reg(struct device *dev, int slot,
  153. int power_on, int vdd)
  154. {
  155. struct omap_mmc_platform_data *mmc = dev->platform_data;
  156. if (mmc->slots[0].remux)
  157. mmc->slots[0].remux(dev, slot, power_on);
  158. if (power_on) {
  159. /* Only MMC2 supports a CLKIN */
  160. if (mmc->slots[0].internal_clock) {
  161. u32 reg;
  162. reg = omap_ctrl_readl(control_devconf1_offset);
  163. reg |= OMAP2_MMCSDIO2ADPCLKISEL;
  164. omap_ctrl_writel(reg, control_devconf1_offset);
  165. }
  166. }
  167. }
  168. static int nop_mmc_set_power(struct device *dev, int slot, int power_on,
  169. int vdd)
  170. {
  171. return 0;
  172. }
  173. static inline void omap_hsmmc_mux(struct omap_mmc_platform_data *mmc_controller,
  174. int controller_nr)
  175. {
  176. if (gpio_is_valid(mmc_controller->slots[0].switch_pin))
  177. omap_mux_init_gpio(mmc_controller->slots[0].switch_pin,
  178. OMAP_PIN_INPUT_PULLUP);
  179. if (gpio_is_valid(mmc_controller->slots[0].gpio_wp))
  180. omap_mux_init_gpio(mmc_controller->slots[0].gpio_wp,
  181. OMAP_PIN_INPUT_PULLUP);
  182. if (cpu_is_omap34xx()) {
  183. if (controller_nr == 0) {
  184. omap_mux_init_signal("sdmmc1_clk",
  185. OMAP_PIN_INPUT_PULLUP);
  186. omap_mux_init_signal("sdmmc1_cmd",
  187. OMAP_PIN_INPUT_PULLUP);
  188. omap_mux_init_signal("sdmmc1_dat0",
  189. OMAP_PIN_INPUT_PULLUP);
  190. if (mmc_controller->slots[0].caps &
  191. (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)) {
  192. omap_mux_init_signal("sdmmc1_dat1",
  193. OMAP_PIN_INPUT_PULLUP);
  194. omap_mux_init_signal("sdmmc1_dat2",
  195. OMAP_PIN_INPUT_PULLUP);
  196. omap_mux_init_signal("sdmmc1_dat3",
  197. OMAP_PIN_INPUT_PULLUP);
  198. }
  199. if (mmc_controller->slots[0].caps &
  200. MMC_CAP_8_BIT_DATA) {
  201. omap_mux_init_signal("sdmmc1_dat4",
  202. OMAP_PIN_INPUT_PULLUP);
  203. omap_mux_init_signal("sdmmc1_dat5",
  204. OMAP_PIN_INPUT_PULLUP);
  205. omap_mux_init_signal("sdmmc1_dat6",
  206. OMAP_PIN_INPUT_PULLUP);
  207. omap_mux_init_signal("sdmmc1_dat7",
  208. OMAP_PIN_INPUT_PULLUP);
  209. }
  210. }
  211. if (controller_nr == 1) {
  212. /* MMC2 */
  213. omap_mux_init_signal("sdmmc2_clk",
  214. OMAP_PIN_INPUT_PULLUP);
  215. omap_mux_init_signal("sdmmc2_cmd",
  216. OMAP_PIN_INPUT_PULLUP);
  217. omap_mux_init_signal("sdmmc2_dat0",
  218. OMAP_PIN_INPUT_PULLUP);
  219. /*
  220. * For 8 wire configurations, Lines DAT4, 5, 6 and 7
  221. * need to be muxed in the board-*.c files
  222. */
  223. if (mmc_controller->slots[0].caps &
  224. (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)) {
  225. omap_mux_init_signal("sdmmc2_dat1",
  226. OMAP_PIN_INPUT_PULLUP);
  227. omap_mux_init_signal("sdmmc2_dat2",
  228. OMAP_PIN_INPUT_PULLUP);
  229. omap_mux_init_signal("sdmmc2_dat3",
  230. OMAP_PIN_INPUT_PULLUP);
  231. }
  232. if (mmc_controller->slots[0].caps &
  233. MMC_CAP_8_BIT_DATA) {
  234. omap_mux_init_signal("sdmmc2_dat4.sdmmc2_dat4",
  235. OMAP_PIN_INPUT_PULLUP);
  236. omap_mux_init_signal("sdmmc2_dat5.sdmmc2_dat5",
  237. OMAP_PIN_INPUT_PULLUP);
  238. omap_mux_init_signal("sdmmc2_dat6.sdmmc2_dat6",
  239. OMAP_PIN_INPUT_PULLUP);
  240. omap_mux_init_signal("sdmmc2_dat7.sdmmc2_dat7",
  241. OMAP_PIN_INPUT_PULLUP);
  242. }
  243. }
  244. /*
  245. * For MMC3 the pins need to be muxed in the board-*.c files
  246. */
  247. }
  248. }
  249. static int __init omap_hsmmc_pdata_init(struct omap2_hsmmc_info *c,
  250. struct omap_mmc_platform_data *mmc)
  251. {
  252. char *hc_name;
  253. hc_name = kzalloc(sizeof(char) * (HSMMC_NAME_LEN + 1), GFP_KERNEL);
  254. if (!hc_name) {
  255. pr_err("Cannot allocate memory for controller slot name\n");
  256. kfree(hc_name);
  257. return -ENOMEM;
  258. }
  259. if (c->name)
  260. strncpy(hc_name, c->name, HSMMC_NAME_LEN);
  261. else
  262. snprintf(hc_name, (HSMMC_NAME_LEN + 1), "mmc%islot%i",
  263. c->mmc, 1);
  264. mmc->slots[0].name = hc_name;
  265. mmc->nr_slots = 1;
  266. mmc->slots[0].caps = c->caps;
  267. mmc->slots[0].internal_clock = !c->ext_clock;
  268. mmc->dma_mask = 0xffffffff;
  269. if (cpu_is_omap44xx())
  270. mmc->reg_offset = OMAP4_MMC_REG_OFFSET;
  271. else
  272. mmc->reg_offset = 0;
  273. mmc->get_context_loss_count = hsmmc_get_context_loss;
  274. mmc->slots[0].switch_pin = c->gpio_cd;
  275. mmc->slots[0].gpio_wp = c->gpio_wp;
  276. mmc->slots[0].remux = c->remux;
  277. mmc->slots[0].init_card = c->init_card;
  278. if (c->cover_only)
  279. mmc->slots[0].cover = 1;
  280. if (c->nonremovable)
  281. mmc->slots[0].nonremovable = 1;
  282. if (c->power_saving)
  283. mmc->slots[0].power_saving = 1;
  284. if (c->no_off)
  285. mmc->slots[0].no_off = 1;
  286. if (c->no_off_init)
  287. mmc->slots[0].no_regulator_off_init = c->no_off_init;
  288. if (c->vcc_aux_disable_is_sleep)
  289. mmc->slots[0].vcc_aux_disable_is_sleep = 1;
  290. /*
  291. * NOTE: MMC slots should have a Vcc regulator set up.
  292. * This may be from a TWL4030-family chip, another
  293. * controllable regulator, or a fixed supply.
  294. *
  295. * temporary HACK: ocr_mask instead of fixed supply
  296. */
  297. mmc->slots[0].ocr_mask = c->ocr_mask;
  298. if (cpu_is_omap3517() || cpu_is_omap3505())
  299. mmc->slots[0].set_power = nop_mmc_set_power;
  300. else
  301. mmc->slots[0].features |= HSMMC_HAS_PBIAS;
  302. if (cpu_is_omap44xx() && (omap_rev() > OMAP4430_REV_ES1_0))
  303. mmc->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
  304. switch (c->mmc) {
  305. case 1:
  306. if (mmc->slots[0].features & HSMMC_HAS_PBIAS) {
  307. /* on-chip level shifting via PBIAS0/PBIAS1 */
  308. if (cpu_is_omap44xx()) {
  309. mmc->slots[0].before_set_reg =
  310. omap4_hsmmc1_before_set_reg;
  311. mmc->slots[0].after_set_reg =
  312. omap4_hsmmc1_after_set_reg;
  313. } else {
  314. mmc->slots[0].before_set_reg =
  315. omap_hsmmc1_before_set_reg;
  316. mmc->slots[0].after_set_reg =
  317. omap_hsmmc1_after_set_reg;
  318. }
  319. }
  320. /* OMAP3630 HSMMC1 supports only 4-bit */
  321. if (cpu_is_omap3630() &&
  322. (c->caps & MMC_CAP_8_BIT_DATA)) {
  323. c->caps &= ~MMC_CAP_8_BIT_DATA;
  324. c->caps |= MMC_CAP_4_BIT_DATA;
  325. mmc->slots[0].caps = c->caps;
  326. }
  327. break;
  328. case 2:
  329. if (c->ext_clock)
  330. c->transceiver = 1;
  331. if (c->transceiver && (c->caps & MMC_CAP_8_BIT_DATA)) {
  332. c->caps &= ~MMC_CAP_8_BIT_DATA;
  333. c->caps |= MMC_CAP_4_BIT_DATA;
  334. }
  335. /* FALLTHROUGH */
  336. case 3:
  337. if (mmc->slots[0].features & HSMMC_HAS_PBIAS) {
  338. /* off-chip level shifting, or none */
  339. mmc->slots[0].before_set_reg = hsmmc23_before_set_reg;
  340. mmc->slots[0].after_set_reg = NULL;
  341. }
  342. break;
  343. case 4:
  344. case 5:
  345. mmc->slots[0].before_set_reg = NULL;
  346. mmc->slots[0].after_set_reg = NULL;
  347. break;
  348. default:
  349. pr_err("MMC%d configuration not supported!\n", c->mmc);
  350. kfree(hc_name);
  351. return -ENODEV;
  352. }
  353. return 0;
  354. }
  355. #define MAX_OMAP_MMC_HWMOD_NAME_LEN 16
  356. void __init omap_init_hsmmc(struct omap2_hsmmc_info *hsmmcinfo, int ctrl_nr)
  357. {
  358. struct omap_hwmod *oh;
  359. struct platform_device *pdev;
  360. char oh_name[MAX_OMAP_MMC_HWMOD_NAME_LEN];
  361. struct omap_mmc_platform_data *mmc_data;
  362. struct omap_mmc_dev_attr *mmc_dev_attr;
  363. char *name;
  364. int l;
  365. mmc_data = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
  366. if (!mmc_data) {
  367. pr_err("Cannot allocate memory for mmc device!\n");
  368. goto done;
  369. }
  370. if (omap_hsmmc_pdata_init(hsmmcinfo, mmc_data) < 0) {
  371. pr_err("%s fails!\n", __func__);
  372. goto done;
  373. }
  374. omap_hsmmc_mux(mmc_data, (ctrl_nr - 1));
  375. name = "omap_hsmmc";
  376. l = snprintf(oh_name, MAX_OMAP_MMC_HWMOD_NAME_LEN,
  377. "mmc%d", ctrl_nr);
  378. WARN(l >= MAX_OMAP_MMC_HWMOD_NAME_LEN,
  379. "String buffer overflow in MMC%d device setup\n", ctrl_nr);
  380. oh = omap_hwmod_lookup(oh_name);
  381. if (!oh) {
  382. pr_err("Could not look up %s\n", oh_name);
  383. kfree(mmc_data->slots[0].name);
  384. goto done;
  385. }
  386. if (oh->dev_attr != NULL) {
  387. mmc_dev_attr = oh->dev_attr;
  388. mmc_data->controller_flags = mmc_dev_attr->flags;
  389. }
  390. pdev = omap_device_build(name, ctrl_nr - 1, oh, mmc_data,
  391. sizeof(struct omap_mmc_platform_data), NULL, 0, false);
  392. if (IS_ERR(pdev)) {
  393. WARN(1, "Can't build omap_device for %s:%s.\n", name, oh->name);
  394. kfree(mmc_data->slots[0].name);
  395. goto done;
  396. }
  397. /*
  398. * return device handle to board setup code
  399. * required to populate for regulator framework structure
  400. */
  401. hsmmcinfo->dev = &pdev->dev;
  402. done:
  403. kfree(mmc_data);
  404. }
  405. void __init omap2_hsmmc_init(struct omap2_hsmmc_info *controllers)
  406. {
  407. u32 reg;
  408. if (!cpu_is_omap44xx()) {
  409. if (cpu_is_omap2430()) {
  410. control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
  411. control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
  412. } else {
  413. control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
  414. control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
  415. }
  416. } else {
  417. control_pbias_offset =
  418. OMAP4_CTRL_MODULE_PAD_CORE_CONTROL_PBIASLITE;
  419. control_mmc1 = OMAP4_CTRL_MODULE_PAD_CORE_CONTROL_MMC1;
  420. reg = omap4_ctrl_pad_readl(control_mmc1);
  421. reg |= (OMAP4_SDMMC1_PUSTRENGTH_GRP0_MASK |
  422. OMAP4_SDMMC1_PUSTRENGTH_GRP1_MASK);
  423. reg &= ~(OMAP4_SDMMC1_PUSTRENGTH_GRP2_MASK |
  424. OMAP4_SDMMC1_PUSTRENGTH_GRP3_MASK);
  425. reg |= (OMAP4_SDMMC1_DR0_SPEEDCTRL_MASK |
  426. OMAP4_SDMMC1_DR1_SPEEDCTRL_MASK |
  427. OMAP4_SDMMC1_DR2_SPEEDCTRL_MASK);
  428. omap4_ctrl_pad_writel(reg, control_mmc1);
  429. }
  430. for (; controllers->mmc; controllers++)
  431. omap_init_hsmmc(controllers, controllers->mmc);
  432. }
  433. #endif