mmc-twl4030.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * linux/arch/arm/mach-omap2/mmc-twl4030.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/err.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <mach/hardware.h>
  22. #include <plat/control.h>
  23. #include <plat/mmc.h>
  24. #include <plat/board.h>
  25. #include "mmc-twl4030.h"
  26. #if defined(CONFIG_REGULATOR) && \
  27. (defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE))
  28. static u16 control_pbias_offset;
  29. static u16 control_devconf1_offset;
  30. #define HSMMC_NAME_LEN 9
  31. static struct twl_mmc_controller {
  32. struct omap_mmc_platform_data *mmc;
  33. /* Vcc == configured supply
  34. * Vcc_alt == optional
  35. * - MMC1, supply for DAT4..DAT7
  36. * - MMC2/MMC2, external level shifter voltage supply, for
  37. * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
  38. */
  39. struct regulator *vcc;
  40. struct regulator *vcc_aux;
  41. char name[HSMMC_NAME_LEN + 1];
  42. } hsmmc[OMAP34XX_NR_MMC];
  43. static int twl_mmc_card_detect(int irq)
  44. {
  45. unsigned i;
  46. for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
  47. struct omap_mmc_platform_data *mmc;
  48. mmc = hsmmc[i].mmc;
  49. if (!mmc)
  50. continue;
  51. if (irq != mmc->slots[0].card_detect_irq)
  52. continue;
  53. /* NOTE: assumes card detect signal is active-low */
  54. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  55. }
  56. return -ENOSYS;
  57. }
  58. static int twl_mmc_get_ro(struct device *dev, int slot)
  59. {
  60. struct omap_mmc_platform_data *mmc = dev->platform_data;
  61. /* NOTE: assumes write protect signal is active-high */
  62. return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
  63. }
  64. static int twl_mmc_get_cover_state(struct device *dev, int slot)
  65. {
  66. struct omap_mmc_platform_data *mmc = dev->platform_data;
  67. /* NOTE: assumes card detect signal is active-low */
  68. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  69. }
  70. /*
  71. * MMC Slot Initialization.
  72. */
  73. static int twl_mmc_late_init(struct device *dev)
  74. {
  75. struct omap_mmc_platform_data *mmc = dev->platform_data;
  76. int ret = 0;
  77. int i;
  78. /* MMC/SD/SDIO doesn't require a card detect switch */
  79. if (gpio_is_valid(mmc->slots[0].switch_pin)) {
  80. ret = gpio_request(mmc->slots[0].switch_pin, "mmc_cd");
  81. if (ret)
  82. goto done;
  83. ret = gpio_direction_input(mmc->slots[0].switch_pin);
  84. if (ret)
  85. goto err;
  86. }
  87. /* require at least main regulator */
  88. for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
  89. if (hsmmc[i].name == mmc->slots[0].name) {
  90. struct regulator *reg;
  91. hsmmc[i].mmc = mmc;
  92. reg = regulator_get(dev, "vmmc");
  93. if (IS_ERR(reg)) {
  94. dev_dbg(dev, "vmmc regulator missing\n");
  95. /* HACK: until fixed.c regulator is usable,
  96. * we don't require a main regulator
  97. * for MMC2 or MMC3
  98. */
  99. if (i != 0)
  100. break;
  101. ret = PTR_ERR(reg);
  102. hsmmc[i].vcc = NULL;
  103. goto err;
  104. }
  105. hsmmc[i].vcc = reg;
  106. mmc->slots[0].ocr_mask = mmc_regulator_get_ocrmask(reg);
  107. /* allow an aux regulator */
  108. reg = regulator_get(dev, "vmmc_aux");
  109. hsmmc[i].vcc_aux = IS_ERR(reg) ? NULL : reg;
  110. /* UGLY HACK: workaround regulator framework bugs.
  111. * When the bootloader leaves a supply active, it's
  112. * initialized with zero usecount ... and we can't
  113. * disable it without first enabling it. Until the
  114. * framework is fixed, we need a workaround like this
  115. * (which is safe for MMC, but not in general).
  116. */
  117. if (regulator_is_enabled(hsmmc[i].vcc) > 0) {
  118. regulator_enable(hsmmc[i].vcc);
  119. regulator_disable(hsmmc[i].vcc);
  120. }
  121. if (hsmmc[i].vcc_aux) {
  122. if (regulator_is_enabled(reg) > 0) {
  123. regulator_enable(reg);
  124. regulator_disable(reg);
  125. }
  126. }
  127. break;
  128. }
  129. }
  130. return 0;
  131. err:
  132. gpio_free(mmc->slots[0].switch_pin);
  133. done:
  134. mmc->slots[0].card_detect_irq = 0;
  135. mmc->slots[0].card_detect = NULL;
  136. dev_err(dev, "err %d configuring card detect\n", ret);
  137. return ret;
  138. }
  139. static void twl_mmc_cleanup(struct device *dev)
  140. {
  141. struct omap_mmc_platform_data *mmc = dev->platform_data;
  142. int i;
  143. gpio_free(mmc->slots[0].switch_pin);
  144. for(i = 0; i < ARRAY_SIZE(hsmmc); i++) {
  145. regulator_put(hsmmc[i].vcc);
  146. regulator_put(hsmmc[i].vcc_aux);
  147. }
  148. }
  149. #ifdef CONFIG_PM
  150. static int twl_mmc_suspend(struct device *dev, int slot)
  151. {
  152. struct omap_mmc_platform_data *mmc = dev->platform_data;
  153. disable_irq(mmc->slots[0].card_detect_irq);
  154. return 0;
  155. }
  156. static int twl_mmc_resume(struct device *dev, int slot)
  157. {
  158. struct omap_mmc_platform_data *mmc = dev->platform_data;
  159. enable_irq(mmc->slots[0].card_detect_irq);
  160. return 0;
  161. }
  162. #else
  163. #define twl_mmc_suspend NULL
  164. #define twl_mmc_resume NULL
  165. #endif
  166. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  167. static int twl4030_mmc_get_context_loss(struct device *dev)
  168. {
  169. /* FIXME: PM DPS not implemented yet */
  170. return 0;
  171. }
  172. #else
  173. #define twl4030_mmc_get_context_loss NULL
  174. #endif
  175. static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
  176. int vdd)
  177. {
  178. u32 reg, prog_io;
  179. int ret = 0;
  180. struct twl_mmc_controller *c = &hsmmc[0];
  181. struct omap_mmc_platform_data *mmc = dev->platform_data;
  182. /*
  183. * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
  184. * card with Vcc regulator (from twl4030 or whatever). OMAP has both
  185. * 1.8V and 3.0V modes, controlled by the PBIAS register.
  186. *
  187. * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
  188. * is most naturally TWL VSIM; those pins also use PBIAS.
  189. *
  190. * FIXME handle VMMC1A as needed ...
  191. */
  192. if (power_on) {
  193. if (cpu_is_omap2430()) {
  194. reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
  195. if ((1 << vdd) >= MMC_VDD_30_31)
  196. reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
  197. else
  198. reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
  199. omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
  200. }
  201. if (mmc->slots[0].internal_clock) {
  202. reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
  203. reg |= OMAP2_MMCSDIO1ADPCLKISEL;
  204. omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
  205. }
  206. reg = omap_ctrl_readl(control_pbias_offset);
  207. if (cpu_is_omap3630()) {
  208. /* Set MMC I/O to 52Mhz */
  209. prog_io = omap_ctrl_readl(OMAP343X_CONTROL_PROG_IO1);
  210. prog_io |= OMAP3630_PRG_SDMMC1_SPEEDCTRL;
  211. omap_ctrl_writel(prog_io, OMAP343X_CONTROL_PROG_IO1);
  212. } else {
  213. reg |= OMAP2_PBIASSPEEDCTRL0;
  214. }
  215. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  216. omap_ctrl_writel(reg, control_pbias_offset);
  217. ret = mmc_regulator_set_ocr(c->vcc, vdd);
  218. /* 100ms delay required for PBIAS configuration */
  219. msleep(100);
  220. reg = omap_ctrl_readl(control_pbias_offset);
  221. reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
  222. if ((1 << vdd) <= MMC_VDD_165_195)
  223. reg &= ~OMAP2_PBIASLITEVMODE0;
  224. else
  225. reg |= OMAP2_PBIASLITEVMODE0;
  226. omap_ctrl_writel(reg, control_pbias_offset);
  227. } else {
  228. reg = omap_ctrl_readl(control_pbias_offset);
  229. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  230. omap_ctrl_writel(reg, control_pbias_offset);
  231. ret = mmc_regulator_set_ocr(c->vcc, 0);
  232. /* 100ms delay required for PBIAS configuration */
  233. msleep(100);
  234. reg = omap_ctrl_readl(control_pbias_offset);
  235. reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
  236. OMAP2_PBIASLITEVMODE0);
  237. omap_ctrl_writel(reg, control_pbias_offset);
  238. }
  239. return ret;
  240. }
  241. static int twl_mmc23_set_power(struct device *dev, int slot, int power_on, int vdd)
  242. {
  243. int ret = 0;
  244. struct twl_mmc_controller *c = NULL;
  245. struct omap_mmc_platform_data *mmc = dev->platform_data;
  246. int i;
  247. for (i = 1; i < ARRAY_SIZE(hsmmc); i++) {
  248. if (mmc == hsmmc[i].mmc) {
  249. c = &hsmmc[i];
  250. break;
  251. }
  252. }
  253. if (c == NULL)
  254. return -ENODEV;
  255. /* If we don't see a Vcc regulator, assume it's a fixed
  256. * voltage always-on regulator.
  257. */
  258. if (!c->vcc)
  259. return 0;
  260. /*
  261. * Assume Vcc regulator is used only to power the card ... OMAP
  262. * VDDS is used to power the pins, optionally with a transceiver to
  263. * support cards using voltages other than VDDS (1.8V nominal). When a
  264. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  265. *
  266. * In some cases this regulator won't support enable/disable;
  267. * e.g. it's a fixed rail for a WLAN chip.
  268. *
  269. * In other cases vcc_aux switches interface power. Example, for
  270. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  271. * chips/cards need an interface voltage rail too.
  272. */
  273. if (power_on) {
  274. /* only MMC2 supports a CLKIN */
  275. if (mmc->slots[0].internal_clock) {
  276. u32 reg;
  277. reg = omap_ctrl_readl(control_devconf1_offset);
  278. reg |= OMAP2_MMCSDIO2ADPCLKISEL;
  279. omap_ctrl_writel(reg, control_devconf1_offset);
  280. }
  281. ret = mmc_regulator_set_ocr(c->vcc, vdd);
  282. /* enable interface voltage rail, if needed */
  283. if (ret == 0 && c->vcc_aux) {
  284. ret = regulator_enable(c->vcc_aux);
  285. if (ret < 0)
  286. ret = mmc_regulator_set_ocr(c->vcc, 0);
  287. }
  288. } else {
  289. if (c->vcc_aux && (ret = regulator_is_enabled(c->vcc_aux)) > 0)
  290. ret = regulator_disable(c->vcc_aux);
  291. if (ret == 0)
  292. ret = mmc_regulator_set_ocr(c->vcc, 0);
  293. }
  294. return ret;
  295. }
  296. static int twl_mmc1_set_sleep(struct device *dev, int slot, int sleep, int vdd,
  297. int cardsleep)
  298. {
  299. struct twl_mmc_controller *c = &hsmmc[0];
  300. int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
  301. return regulator_set_mode(c->vcc, mode);
  302. }
  303. static int twl_mmc23_set_sleep(struct device *dev, int slot, int sleep, int vdd,
  304. int cardsleep)
  305. {
  306. struct twl_mmc_controller *c = NULL;
  307. struct omap_mmc_platform_data *mmc = dev->platform_data;
  308. int i, err, mode;
  309. for (i = 1; i < ARRAY_SIZE(hsmmc); i++) {
  310. if (mmc == hsmmc[i].mmc) {
  311. c = &hsmmc[i];
  312. break;
  313. }
  314. }
  315. if (c == NULL)
  316. return -ENODEV;
  317. /*
  318. * If we don't see a Vcc regulator, assume it's a fixed
  319. * voltage always-on regulator.
  320. */
  321. if (!c->vcc)
  322. return 0;
  323. mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
  324. if (!c->vcc_aux)
  325. return regulator_set_mode(c->vcc, mode);
  326. if (cardsleep) {
  327. /* VCC can be turned off if card is asleep */
  328. struct regulator *vcc_aux = c->vcc_aux;
  329. c->vcc_aux = NULL;
  330. if (sleep)
  331. err = twl_mmc23_set_power(dev, slot, 0, 0);
  332. else
  333. err = twl_mmc23_set_power(dev, slot, 1, vdd);
  334. c->vcc_aux = vcc_aux;
  335. } else
  336. err = regulator_set_mode(c->vcc, mode);
  337. if (err)
  338. return err;
  339. return regulator_set_mode(c->vcc_aux, mode);
  340. }
  341. static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC] __initdata;
  342. void __init twl4030_mmc_init(struct twl4030_hsmmc_info *controllers)
  343. {
  344. struct twl4030_hsmmc_info *c;
  345. int nr_hsmmc = ARRAY_SIZE(hsmmc_data);
  346. if (cpu_is_omap2430()) {
  347. control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
  348. control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
  349. nr_hsmmc = 2;
  350. } else {
  351. control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
  352. control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
  353. }
  354. for (c = controllers; c->mmc; c++) {
  355. struct twl_mmc_controller *twl = hsmmc + c->mmc - 1;
  356. struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
  357. if (!c->mmc || c->mmc > nr_hsmmc) {
  358. pr_debug("MMC%d: no such controller\n", c->mmc);
  359. continue;
  360. }
  361. if (mmc) {
  362. pr_debug("MMC%d: already configured\n", c->mmc);
  363. continue;
  364. }
  365. mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
  366. if (!mmc) {
  367. pr_err("Cannot allocate memory for mmc device!\n");
  368. return;
  369. }
  370. if (c->name)
  371. strncpy(twl->name, c->name, HSMMC_NAME_LEN);
  372. else
  373. snprintf(twl->name, ARRAY_SIZE(twl->name),
  374. "mmc%islot%i", c->mmc, 1);
  375. mmc->slots[0].name = twl->name;
  376. mmc->nr_slots = 1;
  377. mmc->slots[0].wires = c->wires;
  378. mmc->slots[0].internal_clock = !c->ext_clock;
  379. mmc->dma_mask = 0xffffffff;
  380. mmc->init = twl_mmc_late_init;
  381. /* note: twl4030 card detect GPIOs can disable VMMCx ... */
  382. if (gpio_is_valid(c->gpio_cd)) {
  383. mmc->cleanup = twl_mmc_cleanup;
  384. mmc->suspend = twl_mmc_suspend;
  385. mmc->resume = twl_mmc_resume;
  386. mmc->slots[0].switch_pin = c->gpio_cd;
  387. mmc->slots[0].card_detect_irq = gpio_to_irq(c->gpio_cd);
  388. if (c->cover_only)
  389. mmc->slots[0].get_cover_state = twl_mmc_get_cover_state;
  390. else
  391. mmc->slots[0].card_detect = twl_mmc_card_detect;
  392. } else
  393. mmc->slots[0].switch_pin = -EINVAL;
  394. mmc->get_context_loss_count =
  395. twl4030_mmc_get_context_loss;
  396. /* write protect normally uses an OMAP gpio */
  397. if (gpio_is_valid(c->gpio_wp)) {
  398. gpio_request(c->gpio_wp, "mmc_wp");
  399. gpio_direction_input(c->gpio_wp);
  400. mmc->slots[0].gpio_wp = c->gpio_wp;
  401. mmc->slots[0].get_ro = twl_mmc_get_ro;
  402. } else
  403. mmc->slots[0].gpio_wp = -EINVAL;
  404. if (c->nonremovable)
  405. mmc->slots[0].nonremovable = 1;
  406. if (c->power_saving)
  407. mmc->slots[0].power_saving = 1;
  408. /* NOTE: MMC slots should have a Vcc regulator set up.
  409. * This may be from a TWL4030-family chip, another
  410. * controllable regulator, or a fixed supply.
  411. *
  412. * temporary HACK: ocr_mask instead of fixed supply
  413. */
  414. mmc->slots[0].ocr_mask = c->ocr_mask;
  415. switch (c->mmc) {
  416. case 1:
  417. /* on-chip level shifting via PBIAS0/PBIAS1 */
  418. mmc->slots[0].set_power = twl_mmc1_set_power;
  419. mmc->slots[0].set_sleep = twl_mmc1_set_sleep;
  420. /* Omap3630 HSMMC1 supports only 4-bit */
  421. if (cpu_is_omap3630() && c->wires > 4) {
  422. c->wires = 4;
  423. mmc->slots[0].wires = c->wires;
  424. }
  425. break;
  426. case 2:
  427. if (c->ext_clock)
  428. c->transceiver = 1;
  429. if (c->transceiver && c->wires > 4)
  430. c->wires = 4;
  431. /* FALLTHROUGH */
  432. case 3:
  433. /* off-chip level shifting, or none */
  434. mmc->slots[0].set_power = twl_mmc23_set_power;
  435. mmc->slots[0].set_sleep = twl_mmc23_set_sleep;
  436. break;
  437. default:
  438. pr_err("MMC%d configuration not supported!\n", c->mmc);
  439. kfree(mmc);
  440. continue;
  441. }
  442. hsmmc_data[c->mmc - 1] = mmc;
  443. }
  444. omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);
  445. /* pass the device nodes back to board setup code */
  446. for (c = controllers; c->mmc; c++) {
  447. struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
  448. if (!c->mmc || c->mmc > nr_hsmmc)
  449. continue;
  450. c->dev = mmc->dev;
  451. }
  452. }
  453. #endif