hsmmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 <linux/mmc/host.h>
  18. #include <linux/platform_data/gpio-omap.h>
  19. #include <linux/platform_data/hsmmc-omap.h>
  20. #include "soc.h"
  21. #include "omap_device.h"
  22. #include "omap-pm.h"
  23. #include "mux.h"
  24. #include "hsmmc.h"
  25. #include "control.h"
  26. #if IS_ENABLED(CONFIG_MMC_OMAP_HS)
  27. static u16 control_pbias_offset;
  28. static u16 control_devconf1_offset;
  29. #define HSMMC_NAME_LEN 9
  30. static void omap_hsmmc1_before_set_reg(struct device *dev,
  31. int power_on, int vdd)
  32. {
  33. u32 reg, prog_io;
  34. struct omap_hsmmc_platform_data *mmc = dev->platform_data;
  35. if (mmc->remux)
  36. mmc->remux(dev, power_on);
  37. /*
  38. * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
  39. * card with Vcc regulator (from twl4030 or whatever). OMAP has both
  40. * 1.8V and 3.0V modes, controlled by the PBIAS register.
  41. *
  42. * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
  43. * is most naturally TWL VSIM; those pins also use PBIAS.
  44. *
  45. * FIXME handle VMMC1A as needed ...
  46. */
  47. if (power_on) {
  48. if (cpu_is_omap2430()) {
  49. reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
  50. if ((1 << vdd) >= MMC_VDD_30_31)
  51. reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
  52. else
  53. reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
  54. omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
  55. }
  56. if (mmc->internal_clock) {
  57. reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
  58. reg |= OMAP2_MMCSDIO1ADPCLKISEL;
  59. omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
  60. }
  61. reg = omap_ctrl_readl(control_pbias_offset);
  62. if (cpu_is_omap3630()) {
  63. /* Set MMC I/O to 52MHz */
  64. prog_io = omap_ctrl_readl(OMAP343X_CONTROL_PROG_IO1);
  65. prog_io |= OMAP3630_PRG_SDMMC1_SPEEDCTRL;
  66. omap_ctrl_writel(prog_io, OMAP343X_CONTROL_PROG_IO1);
  67. } else {
  68. reg |= OMAP2_PBIASSPEEDCTRL0;
  69. }
  70. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  71. omap_ctrl_writel(reg, control_pbias_offset);
  72. } else {
  73. reg = omap_ctrl_readl(control_pbias_offset);
  74. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  75. omap_ctrl_writel(reg, control_pbias_offset);
  76. }
  77. }
  78. static void omap_hsmmc1_after_set_reg(struct device *dev, int power_on, int vdd)
  79. {
  80. u32 reg;
  81. /* 100ms delay required for PBIAS configuration */
  82. msleep(100);
  83. if (power_on) {
  84. reg = omap_ctrl_readl(control_pbias_offset);
  85. reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
  86. if ((1 << vdd) <= MMC_VDD_165_195)
  87. reg &= ~OMAP2_PBIASLITEVMODE0;
  88. else
  89. reg |= OMAP2_PBIASLITEVMODE0;
  90. omap_ctrl_writel(reg, control_pbias_offset);
  91. } else {
  92. reg = omap_ctrl_readl(control_pbias_offset);
  93. reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
  94. OMAP2_PBIASLITEVMODE0);
  95. omap_ctrl_writel(reg, control_pbias_offset);
  96. }
  97. }
  98. static void hsmmc2_select_input_clk_src(struct omap_hsmmc_platform_data *mmc)
  99. {
  100. u32 reg;
  101. reg = omap_ctrl_readl(control_devconf1_offset);
  102. if (mmc->internal_clock)
  103. reg |= OMAP2_MMCSDIO2ADPCLKISEL;
  104. else
  105. reg &= ~OMAP2_MMCSDIO2ADPCLKISEL;
  106. omap_ctrl_writel(reg, control_devconf1_offset);
  107. }
  108. static void hsmmc2_before_set_reg(struct device *dev, int power_on, int vdd)
  109. {
  110. struct omap_hsmmc_platform_data *mmc = dev->platform_data;
  111. if (mmc->remux)
  112. mmc->remux(dev, power_on);
  113. if (power_on)
  114. hsmmc2_select_input_clk_src(mmc);
  115. }
  116. static int am35x_hsmmc2_set_power(struct device *dev, int power_on, int vdd)
  117. {
  118. struct omap_hsmmc_platform_data *mmc = dev->platform_data;
  119. if (power_on)
  120. hsmmc2_select_input_clk_src(mmc);
  121. return 0;
  122. }
  123. static int nop_mmc_set_power(struct device *dev, int power_on, int vdd)
  124. {
  125. return 0;
  126. }
  127. static inline void omap_hsmmc_mux(struct omap_hsmmc_platform_data
  128. *mmc_controller, int controller_nr)
  129. {
  130. if (gpio_is_valid(mmc_controller->gpio_cd) &&
  131. (mmc_controller->gpio_cd < OMAP_MAX_GPIO_LINES))
  132. omap_mux_init_gpio(mmc_controller->gpio_cd,
  133. OMAP_PIN_INPUT_PULLUP);
  134. if (gpio_is_valid(mmc_controller->gpio_cod) &&
  135. (mmc_controller->gpio_cod < OMAP_MAX_GPIO_LINES))
  136. omap_mux_init_gpio(mmc_controller->gpio_cod,
  137. OMAP_PIN_INPUT_PULLUP);
  138. if (gpio_is_valid(mmc_controller->gpio_wp) &&
  139. (mmc_controller->gpio_wp < OMAP_MAX_GPIO_LINES))
  140. omap_mux_init_gpio(mmc_controller->gpio_wp,
  141. OMAP_PIN_INPUT_PULLUP);
  142. if (cpu_is_omap34xx()) {
  143. if (controller_nr == 0) {
  144. omap_mux_init_signal("sdmmc1_clk",
  145. OMAP_PIN_INPUT_PULLUP);
  146. omap_mux_init_signal("sdmmc1_cmd",
  147. OMAP_PIN_INPUT_PULLUP);
  148. omap_mux_init_signal("sdmmc1_dat0",
  149. OMAP_PIN_INPUT_PULLUP);
  150. if (mmc_controller->caps &
  151. (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)) {
  152. omap_mux_init_signal("sdmmc1_dat1",
  153. OMAP_PIN_INPUT_PULLUP);
  154. omap_mux_init_signal("sdmmc1_dat2",
  155. OMAP_PIN_INPUT_PULLUP);
  156. omap_mux_init_signal("sdmmc1_dat3",
  157. OMAP_PIN_INPUT_PULLUP);
  158. }
  159. if (mmc_controller->caps &
  160. MMC_CAP_8_BIT_DATA) {
  161. omap_mux_init_signal("sdmmc1_dat4",
  162. OMAP_PIN_INPUT_PULLUP);
  163. omap_mux_init_signal("sdmmc1_dat5",
  164. OMAP_PIN_INPUT_PULLUP);
  165. omap_mux_init_signal("sdmmc1_dat6",
  166. OMAP_PIN_INPUT_PULLUP);
  167. omap_mux_init_signal("sdmmc1_dat7",
  168. OMAP_PIN_INPUT_PULLUP);
  169. }
  170. }
  171. if (controller_nr == 1) {
  172. /* MMC2 */
  173. omap_mux_init_signal("sdmmc2_clk",
  174. OMAP_PIN_INPUT_PULLUP);
  175. omap_mux_init_signal("sdmmc2_cmd",
  176. OMAP_PIN_INPUT_PULLUP);
  177. omap_mux_init_signal("sdmmc2_dat0",
  178. OMAP_PIN_INPUT_PULLUP);
  179. /*
  180. * For 8 wire configurations, Lines DAT4, 5, 6 and 7
  181. * need to be muxed in the board-*.c files
  182. */
  183. if (mmc_controller->caps &
  184. (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)) {
  185. omap_mux_init_signal("sdmmc2_dat1",
  186. OMAP_PIN_INPUT_PULLUP);
  187. omap_mux_init_signal("sdmmc2_dat2",
  188. OMAP_PIN_INPUT_PULLUP);
  189. omap_mux_init_signal("sdmmc2_dat3",
  190. OMAP_PIN_INPUT_PULLUP);
  191. }
  192. if (mmc_controller->caps &
  193. MMC_CAP_8_BIT_DATA) {
  194. omap_mux_init_signal("sdmmc2_dat4.sdmmc2_dat4",
  195. OMAP_PIN_INPUT_PULLUP);
  196. omap_mux_init_signal("sdmmc2_dat5.sdmmc2_dat5",
  197. OMAP_PIN_INPUT_PULLUP);
  198. omap_mux_init_signal("sdmmc2_dat6.sdmmc2_dat6",
  199. OMAP_PIN_INPUT_PULLUP);
  200. omap_mux_init_signal("sdmmc2_dat7.sdmmc2_dat7",
  201. OMAP_PIN_INPUT_PULLUP);
  202. }
  203. }
  204. /*
  205. * For MMC3 the pins need to be muxed in the board-*.c files
  206. */
  207. }
  208. }
  209. static int __init omap_hsmmc_pdata_init(struct omap2_hsmmc_info *c,
  210. struct omap_hsmmc_platform_data *mmc)
  211. {
  212. char *hc_name;
  213. hc_name = kzalloc(sizeof(char) * (HSMMC_NAME_LEN + 1), GFP_KERNEL);
  214. if (!hc_name) {
  215. pr_err("Cannot allocate memory for controller slot name\n");
  216. kfree(hc_name);
  217. return -ENOMEM;
  218. }
  219. if (c->name)
  220. strncpy(hc_name, c->name, HSMMC_NAME_LEN);
  221. else
  222. snprintf(hc_name, (HSMMC_NAME_LEN + 1), "mmc%islot%i",
  223. c->mmc, 1);
  224. mmc->name = hc_name;
  225. mmc->caps = c->caps;
  226. mmc->internal_clock = !c->ext_clock;
  227. mmc->reg_offset = 0;
  228. if (c->cover_only) {
  229. /* detect if mobile phone cover removed */
  230. mmc->gpio_cd = -EINVAL;
  231. mmc->gpio_cod = c->gpio_cd;
  232. } else {
  233. /* card detect pin on the mmc socket itself */
  234. mmc->gpio_cd = c->gpio_cd;
  235. mmc->gpio_cod = -EINVAL;
  236. }
  237. mmc->gpio_wp = c->gpio_wp;
  238. mmc->remux = c->remux;
  239. mmc->init_card = c->init_card;
  240. if (c->nonremovable)
  241. mmc->nonremovable = 1;
  242. /*
  243. * NOTE: MMC slots should have a Vcc regulator set up.
  244. * This may be from a TWL4030-family chip, another
  245. * controllable regulator, or a fixed supply.
  246. *
  247. * temporary HACK: ocr_mask instead of fixed supply
  248. */
  249. if (soc_is_am35xx())
  250. mmc->ocr_mask = MMC_VDD_165_195 |
  251. MMC_VDD_26_27 |
  252. MMC_VDD_27_28 |
  253. MMC_VDD_29_30 |
  254. MMC_VDD_30_31 |
  255. MMC_VDD_31_32;
  256. else
  257. mmc->ocr_mask = c->ocr_mask;
  258. if (!soc_is_am35xx())
  259. mmc->features |= HSMMC_HAS_PBIAS;
  260. switch (c->mmc) {
  261. case 1:
  262. if (mmc->features & HSMMC_HAS_PBIAS) {
  263. /* on-chip level shifting via PBIAS0/PBIAS1 */
  264. mmc->before_set_reg =
  265. omap_hsmmc1_before_set_reg;
  266. mmc->after_set_reg =
  267. omap_hsmmc1_after_set_reg;
  268. }
  269. if (soc_is_am35xx())
  270. mmc->set_power = nop_mmc_set_power;
  271. /* OMAP3630 HSMMC1 supports only 4-bit */
  272. if (cpu_is_omap3630() &&
  273. (c->caps & MMC_CAP_8_BIT_DATA)) {
  274. c->caps &= ~MMC_CAP_8_BIT_DATA;
  275. c->caps |= MMC_CAP_4_BIT_DATA;
  276. mmc->caps = c->caps;
  277. }
  278. break;
  279. case 2:
  280. if (soc_is_am35xx())
  281. mmc->set_power = am35x_hsmmc2_set_power;
  282. if (c->ext_clock)
  283. c->transceiver = 1;
  284. if (c->transceiver && (c->caps & MMC_CAP_8_BIT_DATA)) {
  285. c->caps &= ~MMC_CAP_8_BIT_DATA;
  286. c->caps |= MMC_CAP_4_BIT_DATA;
  287. }
  288. if (mmc->features & HSMMC_HAS_PBIAS) {
  289. /* off-chip level shifting, or none */
  290. mmc->before_set_reg = hsmmc2_before_set_reg;
  291. mmc->after_set_reg = NULL;
  292. }
  293. break;
  294. case 3:
  295. case 4:
  296. case 5:
  297. mmc->before_set_reg = NULL;
  298. mmc->after_set_reg = NULL;
  299. break;
  300. default:
  301. pr_err("MMC%d configuration not supported!\n", c->mmc);
  302. kfree(hc_name);
  303. return -ENODEV;
  304. }
  305. return 0;
  306. }
  307. static int omap_hsmmc_done;
  308. void omap_hsmmc_late_init(struct omap2_hsmmc_info *c)
  309. {
  310. struct platform_device *pdev;
  311. struct omap_hsmmc_platform_data *mmc_pdata;
  312. int res;
  313. if (omap_hsmmc_done != 1)
  314. return;
  315. omap_hsmmc_done++;
  316. for (; c->mmc; c++) {
  317. if (!c->deferred)
  318. continue;
  319. pdev = c->pdev;
  320. if (!pdev)
  321. continue;
  322. mmc_pdata = pdev->dev.platform_data;
  323. if (!mmc_pdata)
  324. continue;
  325. if (c->cover_only) {
  326. /* detect if mobile phone cover removed */
  327. mmc_pdata->gpio_cd = -EINVAL;
  328. mmc_pdata->gpio_cod = c->gpio_cd;
  329. } else {
  330. /* card detect pin on the mmc socket itself */
  331. mmc_pdata->gpio_cd = c->gpio_cd;
  332. mmc_pdata->gpio_cod = -EINVAL;
  333. }
  334. mmc_pdata->gpio_wp = c->gpio_wp;
  335. res = omap_device_register(pdev);
  336. if (res)
  337. pr_err("Could not late init MMC %s\n",
  338. c->name);
  339. }
  340. }
  341. #define MAX_OMAP_MMC_HWMOD_NAME_LEN 16
  342. static void __init omap_hsmmc_init_one(struct omap2_hsmmc_info *hsmmcinfo,
  343. int ctrl_nr)
  344. {
  345. struct omap_hwmod *oh;
  346. struct omap_hwmod *ohs[1];
  347. struct omap_device *od;
  348. struct platform_device *pdev;
  349. char oh_name[MAX_OMAP_MMC_HWMOD_NAME_LEN];
  350. struct omap_hsmmc_platform_data *mmc_data;
  351. struct omap_hsmmc_dev_attr *mmc_dev_attr;
  352. char *name;
  353. int res;
  354. mmc_data = kzalloc(sizeof(*mmc_data), GFP_KERNEL);
  355. if (!mmc_data) {
  356. pr_err("Cannot allocate memory for mmc device!\n");
  357. return;
  358. }
  359. res = omap_hsmmc_pdata_init(hsmmcinfo, mmc_data);
  360. if (res < 0)
  361. goto free_mmc;
  362. omap_hsmmc_mux(mmc_data, (ctrl_nr - 1));
  363. name = "omap_hsmmc";
  364. res = snprintf(oh_name, MAX_OMAP_MMC_HWMOD_NAME_LEN,
  365. "mmc%d", ctrl_nr);
  366. WARN(res >= MAX_OMAP_MMC_HWMOD_NAME_LEN,
  367. "String buffer overflow in MMC%d device setup\n", ctrl_nr);
  368. oh = omap_hwmod_lookup(oh_name);
  369. if (!oh) {
  370. pr_err("Could not look up %s\n", oh_name);
  371. goto free_name;
  372. }
  373. ohs[0] = oh;
  374. if (oh->dev_attr != NULL) {
  375. mmc_dev_attr = oh->dev_attr;
  376. mmc_data->controller_flags = mmc_dev_attr->flags;
  377. /*
  378. * erratum 2.1.1.128 doesn't apply if board has
  379. * a transceiver is attached
  380. */
  381. if (hsmmcinfo->transceiver)
  382. mmc_data->controller_flags &=
  383. ~OMAP_HSMMC_BROKEN_MULTIBLOCK_READ;
  384. }
  385. pdev = platform_device_alloc(name, ctrl_nr - 1);
  386. if (!pdev) {
  387. pr_err("Could not allocate pdev for %s\n", name);
  388. goto free_name;
  389. }
  390. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  391. od = omap_device_alloc(pdev, ohs, 1);
  392. if (IS_ERR(od)) {
  393. pr_err("Could not allocate od for %s\n", name);
  394. goto put_pdev;
  395. }
  396. res = platform_device_add_data(pdev, mmc_data,
  397. sizeof(struct omap_hsmmc_platform_data));
  398. if (res) {
  399. pr_err("Could not add pdata for %s\n", name);
  400. goto put_pdev;
  401. }
  402. hsmmcinfo->pdev = pdev;
  403. if (hsmmcinfo->deferred)
  404. goto free_mmc;
  405. res = omap_device_register(pdev);
  406. if (res) {
  407. pr_err("Could not register od for %s\n", name);
  408. goto free_od;
  409. }
  410. goto free_mmc;
  411. free_od:
  412. omap_device_delete(od);
  413. put_pdev:
  414. platform_device_put(pdev);
  415. free_name:
  416. kfree(mmc_data->name);
  417. free_mmc:
  418. kfree(mmc_data);
  419. }
  420. void __init omap_hsmmc_init(struct omap2_hsmmc_info *controllers)
  421. {
  422. if (omap_hsmmc_done)
  423. return;
  424. omap_hsmmc_done = 1;
  425. if (cpu_is_omap2430()) {
  426. control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
  427. control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
  428. } else {
  429. control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
  430. control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
  431. }
  432. for (; controllers->mmc; controllers++)
  433. omap_hsmmc_init_one(controllers, controllers->mmc);
  434. }
  435. #endif