PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/mmc/host/omap_hsmmc.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 2493 lines | 1820 code | 401 blank | 272 comment | 336 complexity | fae7bfffea0f9a44361d44ffba8f8b2e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * drivers/mmc/host/omap_hsmmc.c
  3. *
  4. * Driver for OMAP2430/3430 MMC controller.
  5. *
  6. * Copyright (C) 2007 Texas Instruments.
  7. *
  8. * Authors:
  9. * Syed Mohammed Khasim <x0khasim@ti.com>
  10. * Madhusudhan <madhu.cr@ti.com>
  11. * Mohit Jalori <mjalori@ti.com>
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/timer.h>
  27. #include <linux/clk.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/mmc/core.h>
  30. #include <linux/mmc/card.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/io.h>
  33. #include <linux/semaphore.h>
  34. #include <linux/gpio.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/pm_runtime.h>
  37. #include <plat/dma.h>
  38. #include <mach/hardware.h>
  39. #include <plat/board.h>
  40. #include <plat/mmc.h>
  41. #include <plat/cpu.h>
  42. #include <plat/omap-pm.h>
  43. /* OMAP HSMMC Host Controller Registers */
  44. #define OMAP_HSMMC_SYSCONFIG 0x0010
  45. #define OMAP_HSMMC_SYSSTATUS 0x0014
  46. #define OMAP_HSMMC_CON 0x002C
  47. #define OMAP_HSMMC_BLK 0x0104
  48. #define OMAP_HSMMC_ARG 0x0108
  49. #define OMAP_HSMMC_CMD 0x010C
  50. #define OMAP_HSMMC_RSP10 0x0110
  51. #define OMAP_HSMMC_RSP32 0x0114
  52. #define OMAP_HSMMC_RSP54 0x0118
  53. #define OMAP_HSMMC_RSP76 0x011C
  54. #define OMAP_HSMMC_DATA 0x0120
  55. #define OMAP_HSMMC_HCTL 0x0128
  56. #define OMAP_HSMMC_SYSCTL 0x012C
  57. #define OMAP_HSMMC_STAT 0x0130
  58. #define OMAP_HSMMC_IE 0x0134
  59. #define OMAP_HSMMC_ISE 0x0138
  60. #define OMAP_HSMMC_CAPA 0x0140
  61. #define VS18 (1 << 26)
  62. #define VS30 (1 << 25)
  63. #define SDVS18 (0x5 << 9)
  64. #define SDVS30 (0x6 << 9)
  65. #define SDVS33 (0x7 << 9)
  66. #define SDVS_MASK 0x00000E00
  67. #define SDVSCLR 0xFFFFF1FF
  68. #define SDVSDET 0x00000400
  69. #define AUTOIDLE 0x1
  70. #define SDBP (1 << 8)
  71. #define DTO 0xe
  72. #define ICE 0x1
  73. #define ICS 0x2
  74. #define CEN (1 << 2)
  75. #define CLKD_MASK 0x0000FFC0
  76. #define CLKD_SHIFT 6
  77. #define DTO_MASK 0x000F0000
  78. #define DTO_SHIFT 16
  79. #define INT_EN_MASK 0x307F0033
  80. #define BWR_ENABLE (1 << 4)
  81. #define BRR_ENABLE (1 << 5)
  82. #define DTO_ENABLE (1 << 20)
  83. #define INIT_STREAM (1 << 1)
  84. #define DP_SELECT (1 << 21)
  85. #define DDIR (1 << 4)
  86. #define DMA_EN 0x1
  87. #define MSBS (1 << 5)
  88. #define BCE (1 << 1)
  89. #define FOUR_BIT (1 << 1)
  90. #define DW8 (1 << 5)
  91. #define CC 0x1
  92. #define TC 0x02
  93. #define OD 0x1
  94. #define ERR (1 << 15)
  95. #define CMD_TIMEOUT (1 << 16)
  96. #define DATA_TIMEOUT (1 << 20)
  97. #define CMD_CRC (1 << 17)
  98. #define DATA_CRC (1 << 21)
  99. #define CARD_ERR (1 << 28)
  100. #define STAT_CLEAR 0xFFFFFFFF
  101. #define INIT_STREAM_CMD 0x00000000
  102. #define DUAL_VOLT_OCR_BIT 7
  103. #define SRC (1 << 25)
  104. #define SRD (1 << 26)
  105. #define SOFTRESET (1 << 1)
  106. #define RESETDONE (1 << 0)
  107. /*
  108. * FIXME: Most likely all the data using these _DEVID defines should come
  109. * from the platform_data, or implemented in controller and slot specific
  110. * functions.
  111. */
  112. #define OMAP_MMC1_DEVID 0
  113. #define OMAP_MMC2_DEVID 1
  114. #define OMAP_MMC3_DEVID 2
  115. #define OMAP_MMC4_DEVID 3
  116. #define OMAP_MMC5_DEVID 4
  117. #define MMC_TIMEOUT_MS 20
  118. #define OMAP_MMC_MASTER_CLOCK 96000000
  119. #define DRIVER_NAME "omap_hsmmc"
  120. /* Timeouts for entering power saving states on inactivity, msec */
  121. #define OMAP_MMC_DISABLED_TIMEOUT 100
  122. #define OMAP_MMC_SLEEP_TIMEOUT 1000
  123. #define OMAP_MMC_OFF_TIMEOUT 8000
  124. /*
  125. * One controller can have multiple slots, like on some omap boards using
  126. * omap.c controller driver. Luckily this is not currently done on any known
  127. * omap_hsmmc.c device.
  128. */
  129. #define mmc_slot(host) (host->pdata->slots[host->slot_id])
  130. /*
  131. * MMC Host controller read/write API's
  132. */
  133. #define OMAP_HSMMC_READ(base, reg) \
  134. __raw_readl((base) + OMAP_HSMMC_##reg)
  135. #define OMAP_HSMMC_WRITE(base, reg, val) \
  136. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  137. struct omap_hsmmc_host {
  138. struct device *dev;
  139. struct mmc_host *mmc;
  140. struct mmc_request *mrq;
  141. struct mmc_command *cmd;
  142. struct mmc_data *data;
  143. struct clk *fclk;
  144. struct clk *iclk;
  145. struct clk *dbclk;
  146. /*
  147. * vcc == configured supply
  148. * vcc_aux == optional
  149. * - MMC1, supply for DAT4..DAT7
  150. * - MMC2/MMC2, external level shifter voltage supply, for
  151. * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
  152. */
  153. struct regulator *vcc;
  154. struct regulator *vcc_aux;
  155. struct work_struct mmc_carddetect_work;
  156. void __iomem *base;
  157. resource_size_t mapbase;
  158. spinlock_t irq_lock; /* Prevent races with irq handler */
  159. unsigned int id;
  160. unsigned int dma_len;
  161. unsigned int dma_sg_idx;
  162. unsigned int master_clock;
  163. unsigned char bus_mode;
  164. unsigned char power_mode;
  165. u32 *buffer;
  166. u32 bytesleft;
  167. int suspended;
  168. int irq;
  169. int use_dma, dma_ch;
  170. int dma_line_tx, dma_line_rx;
  171. int slot_id;
  172. int got_dbclk;
  173. int response_busy;
  174. int dpm_state;
  175. int vdd;
  176. int protect_card;
  177. int reqs_blocked;
  178. int use_reg;
  179. int req_in_progress;
  180. struct omap_mmc_platform_data *pdata;
  181. };
  182. static void omap_hsmmc_status_notify_cb(int card_present, void *dev_id)
  183. {
  184. struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
  185. unsigned int status, oldstat;
  186. pr_debug("%s: card_present %d\n", mmc_hostname(host->mmc),
  187. card_present);
  188. if (!mmc_slot(host).mmc_data.status) {
  189. mmc_detect_change(host->mmc, 0);
  190. return;
  191. }
  192. status = mmc_slot(host).mmc_data.status(mmc_dev(host->mmc));
  193. oldstat = mmc_slot(host).mmc_data.card_present;
  194. mmc_slot(host).mmc_data.card_present = status;
  195. if (status ^ oldstat) {
  196. pr_debug("%s: Slot status change detected (%d -> %d)\n",
  197. mmc_hostname(host->mmc), oldstat, status);
  198. mmc_detect_change(host->mmc, 0);
  199. }
  200. }
  201. static int omap_hsmmc_card_detect(struct device *dev, int slot)
  202. {
  203. struct omap_mmc_platform_data *mmc = dev->platform_data;
  204. /* NOTE: assumes card detect signal is active-low */
  205. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  206. }
  207. static int omap_hsmmc_get_wp(struct device *dev, int slot)
  208. {
  209. struct omap_mmc_platform_data *mmc = dev->platform_data;
  210. /* NOTE: assumes write protect signal is active-high */
  211. return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
  212. }
  213. static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
  214. {
  215. struct omap_mmc_platform_data *mmc = dev->platform_data;
  216. /* NOTE: assumes card detect signal is active-low */
  217. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  218. }
  219. #ifdef CONFIG_PM
  220. static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
  221. {
  222. struct omap_mmc_platform_data *mmc = dev->platform_data;
  223. disable_irq(mmc->slots[0].card_detect_irq);
  224. return 0;
  225. }
  226. static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
  227. {
  228. struct omap_mmc_platform_data *mmc = dev->platform_data;
  229. enable_irq(mmc->slots[0].card_detect_irq);
  230. return 0;
  231. }
  232. #else
  233. #define omap_hsmmc_suspend_cdirq NULL
  234. #define omap_hsmmc_resume_cdirq NULL
  235. #endif
  236. #ifdef CONFIG_REGULATOR
  237. static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
  238. int vdd)
  239. {
  240. struct omap_hsmmc_host *host =
  241. platform_get_drvdata(to_platform_device(dev));
  242. int ret;
  243. if (mmc_slot(host).before_set_reg)
  244. mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
  245. if (power_on)
  246. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  247. else
  248. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
  249. if (mmc_slot(host).after_set_reg)
  250. mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
  251. return ret;
  252. }
  253. static int omap_hsmmc_235_set_power(struct device *dev, int slot, int power_on,
  254. int vdd)
  255. {
  256. struct omap_hsmmc_host *host =
  257. platform_get_drvdata(to_platform_device(dev));
  258. int ret = 0;
  259. /*
  260. * If we don't see a Vcc regulator, assume it's a fixed
  261. * voltage always-on regulator.
  262. */
  263. if (!host->vcc)
  264. return 0;
  265. if (mmc_slot(host).before_set_reg)
  266. mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
  267. /*
  268. * Assume Vcc regulator is used only to power the card ... OMAP
  269. * VDDS is used to power the pins, optionally with a transceiver to
  270. * support cards using voltages other than VDDS (1.8V nominal). When a
  271. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  272. *
  273. * In some cases this regulator won't support enable/disable;
  274. * e.g. it's a fixed rail for a WLAN chip.
  275. *
  276. * In other cases vcc_aux switches interface power. Example, for
  277. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  278. * chips/cards need an interface voltage rail too.
  279. */
  280. if (power_on) {
  281. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  282. /* Enable interface voltage rail, if needed */
  283. if (ret == 0 && host->vcc_aux) {
  284. ret = regulator_enable(host->vcc_aux);
  285. if (ret < 0)
  286. ret = mmc_regulator_set_ocr(host->mmc,
  287. host->vcc, 0);
  288. }
  289. } else {
  290. /* Shut down the rail */
  291. if (host->vcc_aux)
  292. ret = regulator_disable(host->vcc_aux);
  293. if (!ret) {
  294. /* Then proceed to shut down the local regulator */
  295. ret = mmc_regulator_set_ocr(host->mmc,
  296. host->vcc, 0);
  297. }
  298. }
  299. if (mmc_slot(host).after_set_reg)
  300. mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
  301. return ret;
  302. }
  303. static int omap_hsmmc_4_set_power(struct device *dev, int slot, int power_on,
  304. int vdd)
  305. {
  306. return 0;
  307. }
  308. static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
  309. int vdd, int cardsleep)
  310. {
  311. struct omap_hsmmc_host *host =
  312. platform_get_drvdata(to_platform_device(dev));
  313. int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
  314. return regulator_set_mode(host->vcc, mode);
  315. }
  316. static int omap_hsmmc_235_set_sleep(struct device *dev, int slot, int sleep,
  317. int vdd, int cardsleep)
  318. {
  319. struct omap_hsmmc_host *host =
  320. platform_get_drvdata(to_platform_device(dev));
  321. int err, mode;
  322. /*
  323. * If we don't see a Vcc regulator, assume it's a fixed
  324. * voltage always-on regulator.
  325. */
  326. if (!host->vcc)
  327. return 0;
  328. mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
  329. if (!host->vcc_aux)
  330. return regulator_set_mode(host->vcc, mode);
  331. if (cardsleep) {
  332. /* VCC can be turned off if card is asleep */
  333. if (sleep)
  334. err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
  335. else
  336. err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  337. } else
  338. err = regulator_set_mode(host->vcc, mode);
  339. if (err)
  340. return err;
  341. if (!mmc_slot(host).vcc_aux_disable_is_sleep)
  342. return regulator_set_mode(host->vcc_aux, mode);
  343. if (sleep)
  344. return regulator_disable(host->vcc_aux);
  345. else
  346. return regulator_enable(host->vcc_aux);
  347. }
  348. static int omap_hsmmc_4_set_sleep(struct device *dev, int slot, int sleep,
  349. int vdd, int cardsleep)
  350. {
  351. return 0;
  352. }
  353. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  354. {
  355. struct regulator *reg;
  356. int ret = 0;
  357. int ocr_value = 0;
  358. switch (host->id) {
  359. case OMAP_MMC1_DEVID:
  360. /* On-chip level shifting via PBIAS0/PBIAS1 */
  361. mmc_slot(host).set_power = omap_hsmmc_1_set_power;
  362. mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
  363. break;
  364. case OMAP_MMC2_DEVID:
  365. case OMAP_MMC3_DEVID:
  366. case OMAP_MMC5_DEVID:
  367. /* Off-chip level shifting, or none */
  368. mmc_slot(host).set_power = omap_hsmmc_235_set_power;
  369. mmc_slot(host).set_sleep = omap_hsmmc_235_set_sleep;
  370. break;
  371. case OMAP_MMC4_DEVID:
  372. mmc_slot(host).set_power = omap_hsmmc_4_set_power;
  373. mmc_slot(host).set_sleep = omap_hsmmc_4_set_sleep;
  374. default:
  375. pr_err("MMC%d configuration not supported!\n", host->id);
  376. return -EINVAL;
  377. }
  378. reg = regulator_get(host->dev, "vmmc");
  379. if (IS_ERR(reg)) {
  380. dev_dbg(host->dev, "vmmc regulator missing\n");
  381. /*
  382. * HACK: until fixed.c regulator is usable,
  383. * we don't require a main regulator
  384. * for MMC2 or MMC3
  385. */
  386. if (host->id == OMAP_MMC1_DEVID) {
  387. ret = PTR_ERR(reg);
  388. goto err;
  389. }
  390. } else {
  391. host->vcc = reg;
  392. ocr_value = mmc_regulator_get_ocrmask(reg);
  393. if (!mmc_slot(host).ocr_mask) {
  394. mmc_slot(host).ocr_mask = ocr_value;
  395. } else {
  396. if (!(mmc_slot(host).ocr_mask & ocr_value)) {
  397. pr_err("MMC%d ocrmask %x is not supported\n",
  398. host->id, mmc_slot(host).ocr_mask);
  399. mmc_slot(host).ocr_mask = 0;
  400. return -EINVAL;
  401. }
  402. }
  403. /* Allow an aux regulator */
  404. reg = regulator_get(host->dev, "vmmc_aux");
  405. host->vcc_aux = IS_ERR(reg) ? NULL : reg;
  406. /* For eMMC do not power off when not in sleep state */
  407. if (mmc_slot(host).no_regulator_off_init)
  408. return 0;
  409. /*
  410. * UGLY HACK: workaround regulator framework bugs.
  411. * When the bootloader leaves a supply active, it's
  412. * initialized with zero usecount ... and we can't
  413. * disable it without first enabling it. Until the
  414. * framework is fixed, we need a workaround like this
  415. * (which is safe for MMC, but not in general).
  416. */
  417. if (regulator_is_enabled(host->vcc) > 0) {
  418. regulator_enable(host->vcc);
  419. regulator_disable(host->vcc);
  420. }
  421. if (host->vcc_aux) {
  422. if (regulator_is_enabled(reg) > 0) {
  423. regulator_enable(reg);
  424. regulator_disable(reg);
  425. }
  426. }
  427. }
  428. return 0;
  429. err:
  430. mmc_slot(host).set_power = NULL;
  431. mmc_slot(host).set_sleep = NULL;
  432. return ret;
  433. }
  434. static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  435. {
  436. regulator_put(host->vcc);
  437. regulator_put(host->vcc_aux);
  438. mmc_slot(host).set_power = NULL;
  439. mmc_slot(host).set_sleep = NULL;
  440. }
  441. static inline int omap_hsmmc_have_reg(void)
  442. {
  443. return 1;
  444. }
  445. #else
  446. static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  447. {
  448. return -EINVAL;
  449. }
  450. static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  451. {
  452. }
  453. static inline int omap_hsmmc_have_reg(void)
  454. {
  455. return 0;
  456. }
  457. #endif
  458. static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
  459. {
  460. int ret;
  461. if (gpio_is_valid(pdata->slots[0].switch_pin)) {
  462. if (pdata->slots[0].cover)
  463. pdata->slots[0].get_cover_state =
  464. omap_hsmmc_get_cover_state;
  465. else
  466. pdata->slots[0].card_detect = omap_hsmmc_card_detect;
  467. pdata->slots[0].card_detect_irq =
  468. gpio_to_irq(pdata->slots[0].switch_pin);
  469. ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
  470. if (ret)
  471. return ret;
  472. ret = gpio_direction_input(pdata->slots[0].switch_pin);
  473. if (ret)
  474. goto err_free_sp;
  475. } else
  476. pdata->slots[0].switch_pin = -EINVAL;
  477. if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
  478. pdata->slots[0].get_ro = omap_hsmmc_get_wp;
  479. ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
  480. if (ret)
  481. goto err_free_cd;
  482. ret = gpio_direction_input(pdata->slots[0].gpio_wp);
  483. if (ret)
  484. goto err_free_wp;
  485. } else
  486. pdata->slots[0].gpio_wp = -EINVAL;
  487. return 0;
  488. err_free_wp:
  489. gpio_free(pdata->slots[0].gpio_wp);
  490. err_free_cd:
  491. if (gpio_is_valid(pdata->slots[0].switch_pin))
  492. err_free_sp:
  493. gpio_free(pdata->slots[0].switch_pin);
  494. return ret;
  495. }
  496. static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
  497. {
  498. if (gpio_is_valid(pdata->slots[0].gpio_wp))
  499. gpio_free(pdata->slots[0].gpio_wp);
  500. if (gpio_is_valid(pdata->slots[0].switch_pin))
  501. gpio_free(pdata->slots[0].switch_pin);
  502. }
  503. /*
  504. * Stop clock to the card
  505. */
  506. static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
  507. {
  508. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  509. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  510. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  511. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
  512. }
  513. static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
  514. struct mmc_command *cmd)
  515. {
  516. unsigned int irq_mask;
  517. if (host->use_dma)
  518. irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
  519. else
  520. irq_mask = INT_EN_MASK;
  521. /* Disable timeout for erases */
  522. if (cmd->opcode == MMC_ERASE)
  523. irq_mask &= ~DTO_ENABLE;
  524. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  525. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  526. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  527. }
  528. static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
  529. {
  530. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  531. OMAP_HSMMC_WRITE(host->base, IE, 0);
  532. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  533. }
  534. #ifdef CONFIG_PM
  535. /*
  536. * Restore the MMC host context, if it was lost as result of a
  537. * power state change.
  538. */
  539. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  540. {
  541. struct mmc_ios *ios = &host->mmc->ios;
  542. u32 hctl, capa, con;
  543. u16 dsor = 0;
  544. unsigned long timeout;
  545. if (!omap_pm_was_context_lost(host->dev))
  546. return 1;
  547. /* Wait for hardware reset */
  548. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  549. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  550. && time_before(jiffies, timeout))
  551. ;
  552. /* Do software reset */
  553. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
  554. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  555. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  556. && time_before(jiffies, timeout))
  557. ;
  558. OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
  559. OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
  560. if (host->id == OMAP_MMC1_DEVID) {
  561. if (host->power_mode != MMC_POWER_OFF &&
  562. (1 << ios->vdd) <= MMC_VDD_23_24)
  563. hctl = SDVS18;
  564. else
  565. hctl = SDVS30;
  566. capa = VS30 | VS18;
  567. } else {
  568. hctl = SDVS18;
  569. capa = VS18;
  570. }
  571. OMAP_HSMMC_WRITE(host->base, HCTL,
  572. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  573. OMAP_HSMMC_WRITE(host->base, CAPA,
  574. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  575. OMAP_HSMMC_WRITE(host->base, HCTL,
  576. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  577. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  578. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  579. && time_before(jiffies, timeout))
  580. ;
  581. omap_hsmmc_disable_irq(host);
  582. /* Do not initialize card-specific things if the power is off */
  583. if (host->power_mode == MMC_POWER_OFF)
  584. goto out;
  585. con = OMAP_HSMMC_READ(host->base, CON);
  586. switch (ios->bus_width) {
  587. case MMC_BUS_WIDTH_8:
  588. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  589. break;
  590. case MMC_BUS_WIDTH_4:
  591. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  592. OMAP_HSMMC_WRITE(host->base, HCTL,
  593. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  594. break;
  595. case MMC_BUS_WIDTH_1:
  596. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  597. OMAP_HSMMC_WRITE(host->base, HCTL,
  598. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  599. break;
  600. }
  601. if (ios->clock) {
  602. dsor = host->master_clock / ios->clock;
  603. if (dsor < 1)
  604. dsor = 1;
  605. if (host->master_clock / dsor > ios->clock)
  606. dsor++;
  607. if (dsor > 250)
  608. dsor = 250;
  609. }
  610. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  611. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  612. OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
  613. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  614. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  615. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  616. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  617. && time_before(jiffies, timeout))
  618. ;
  619. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  620. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  621. con = OMAP_HSMMC_READ(host->base, CON);
  622. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  623. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  624. else
  625. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  626. out:
  627. dev_dbg(mmc_dev(host->mmc), "context is restored\n");
  628. return 0;
  629. }
  630. /*
  631. * Save the MMC host context (store the number of power state changes so far).
  632. */
  633. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  634. {
  635. return;
  636. }
  637. #else
  638. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  639. {
  640. return 0;
  641. }
  642. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  643. {
  644. }
  645. #endif
  646. /*
  647. * Send init stream sequence to card
  648. * before sending IDLE command
  649. */
  650. static void send_init_stream(struct omap_hsmmc_host *host)
  651. {
  652. int reg = 0;
  653. unsigned long timeout;
  654. if (host->protect_card)
  655. return;
  656. disable_irq(host->irq);
  657. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  658. OMAP_HSMMC_WRITE(host->base, CON,
  659. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  660. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  661. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  662. while ((reg != CC) && time_before(jiffies, timeout))
  663. reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
  664. OMAP_HSMMC_WRITE(host->base, CON,
  665. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  666. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  667. OMAP_HSMMC_READ(host->base, STAT);
  668. enable_irq(host->irq);
  669. }
  670. static inline
  671. int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
  672. {
  673. int r = 1;
  674. if (mmc_slot(host).get_cover_state)
  675. r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
  676. return r;
  677. }
  678. static ssize_t
  679. omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
  680. char *buf)
  681. {
  682. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  683. struct omap_hsmmc_host *host = mmc_priv(mmc);
  684. return sprintf(buf, "%s\n",
  685. omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
  686. }
  687. static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
  688. static ssize_t
  689. omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
  690. char *buf)
  691. {
  692. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  693. struct omap_hsmmc_host *host = mmc_priv(mmc);
  694. return sprintf(buf, "%s\n", mmc_slot(host).name);
  695. }
  696. static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
  697. /*
  698. * Configure the response type and send the cmd.
  699. */
  700. static void
  701. omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
  702. struct mmc_data *data)
  703. {
  704. int cmdreg = 0, resptype = 0, cmdtype = 0;
  705. dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  706. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  707. host->cmd = cmd;
  708. omap_hsmmc_enable_irq(host, cmd);
  709. host->response_busy = 0;
  710. if (cmd->flags & MMC_RSP_PRESENT) {
  711. if (cmd->flags & MMC_RSP_136)
  712. resptype = 1;
  713. else if (cmd->flags & MMC_RSP_BUSY) {
  714. resptype = 3;
  715. host->response_busy = 1;
  716. } else
  717. resptype = 2;
  718. }
  719. /*
  720. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  721. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  722. * a val of 0x3, rest 0x0.
  723. */
  724. if (cmd == host->mrq->stop)
  725. cmdtype = 0x3;
  726. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  727. if (data) {
  728. cmdreg |= DP_SELECT | MSBS | BCE;
  729. if (data->flags & MMC_DATA_READ)
  730. cmdreg |= DDIR;
  731. else
  732. cmdreg &= ~(DDIR);
  733. }
  734. if (host->use_dma)
  735. cmdreg |= DMA_EN;
  736. host->req_in_progress = 1;
  737. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  738. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  739. }
  740. static int
  741. omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
  742. {
  743. if (data->flags & MMC_DATA_WRITE)
  744. return DMA_TO_DEVICE;
  745. else
  746. return DMA_FROM_DEVICE;
  747. }
  748. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  749. {
  750. int dma_ch;
  751. spin_lock(&host->irq_lock);
  752. host->req_in_progress = 0;
  753. dma_ch = host->dma_ch;
  754. spin_unlock(&host->irq_lock);
  755. omap_hsmmc_disable_irq(host);
  756. /* Do not complete the request if DMA is still in progress */
  757. if (mrq->data && host->use_dma && dma_ch != -1)
  758. return;
  759. host->mrq = NULL;
  760. mmc_request_done(host->mmc, mrq);
  761. }
  762. /*
  763. * Notify the transfer complete to MMC core
  764. */
  765. static void
  766. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  767. {
  768. if (!data) {
  769. struct mmc_request *mrq = host->mrq;
  770. /* TC before CC from CMD6 - don't know why, but it happens */
  771. if (host->cmd && host->cmd->opcode == 6 &&
  772. host->response_busy) {
  773. host->response_busy = 0;
  774. return;
  775. }
  776. omap_hsmmc_request_done(host, mrq);
  777. return;
  778. }
  779. host->data = NULL;
  780. if (!data->error)
  781. data->bytes_xfered += data->blocks * (data->blksz);
  782. else
  783. data->bytes_xfered = 0;
  784. if (!data->stop) {
  785. omap_hsmmc_request_done(host, data->mrq);
  786. return;
  787. }
  788. omap_hsmmc_start_command(host, data->stop, NULL);
  789. }
  790. /*
  791. * Notify the core about command completion
  792. */
  793. static void
  794. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  795. {
  796. host->cmd = NULL;
  797. if (cmd->flags & MMC_RSP_PRESENT) {
  798. if (cmd->flags & MMC_RSP_136) {
  799. /* response type 2 */
  800. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  801. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  802. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  803. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  804. } else {
  805. /* response types 1, 1b, 3, 4, 5, 6 */
  806. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  807. }
  808. }
  809. if ((host->data == NULL && !host->response_busy) || cmd->error)
  810. omap_hsmmc_request_done(host, cmd->mrq);
  811. }
  812. /*
  813. * DMA clean up for command errors
  814. */
  815. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  816. {
  817. int dma_ch;
  818. host->data->error = errno;
  819. spin_lock(&host->irq_lock);
  820. dma_ch = host->dma_ch;
  821. host->dma_ch = -1;
  822. spin_unlock(&host->irq_lock);
  823. if (host->use_dma && dma_ch != -1) {
  824. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
  825. host->data->sg_len,
  826. omap_hsmmc_get_dma_dir(host, host->data));
  827. omap_free_dma(dma_ch);
  828. }
  829. host->data = NULL;
  830. }
  831. /*
  832. * Readable error output
  833. */
  834. #ifdef CONFIG_MMC_DEBUG
  835. static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
  836. {
  837. /* --- means reserved bit without definition at documentation */
  838. static const char *omap_hsmmc_status_bits[] = {
  839. "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
  840. "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
  841. "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
  842. "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
  843. };
  844. char res[256];
  845. char *buf = res;
  846. int len, i;
  847. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  848. buf += len;
  849. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  850. if (status & (1 << i)) {
  851. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  852. buf += len;
  853. }
  854. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  855. }
  856. #endif /* CONFIG_MMC_DEBUG */
  857. /*
  858. * MMC controller internal state machines reset
  859. *
  860. * Used to reset command or data internal state machines, using respectively
  861. * SRC or SRD bit of SYSCTL register
  862. * Can be called from interrupt context
  863. */
  864. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  865. unsigned long bit)
  866. {
  867. unsigned long i = 0;
  868. unsigned long limit = (loops_per_jiffy *
  869. msecs_to_jiffies(MMC_TIMEOUT_MS));
  870. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  871. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  872. /*
  873. * OMAP4 ES2 and greater has an updated reset logic.
  874. * Monitor a 0->1 transition first
  875. */
  876. if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
  877. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  878. && (i++ < limit))
  879. cpu_relax();
  880. }
  881. i = 0;
  882. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  883. (i++ < limit))
  884. cpu_relax();
  885. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  886. dev_err(mmc_dev(host->mmc),
  887. "Timeout waiting on controller reset in %s\n",
  888. __func__);
  889. }
  890. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  891. {
  892. struct mmc_data *data;
  893. int end_cmd = 0, end_trans = 0;
  894. if (!host->req_in_progress) {
  895. do {
  896. OMAP_HSMMC_WRITE(host->base, STAT, status);
  897. /* Flush posted write */
  898. status = OMAP_HSMMC_READ(host->base, STAT);
  899. } while (status & INT_EN_MASK);
  900. return;
  901. }
  902. data = host->data;
  903. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  904. if (status & ERR) {
  905. #ifdef CONFIG_MMC_DEBUG
  906. omap_hsmmc_report_irq(host, status);
  907. #endif
  908. if ((status & CMD_TIMEOUT) ||
  909. (status & CMD_CRC)) {
  910. if (host->cmd) {
  911. if (status & CMD_TIMEOUT) {
  912. omap_hsmmc_reset_controller_fsm(host,
  913. SRC);
  914. host->cmd->error = -ETIMEDOUT;
  915. } else {
  916. host->cmd->error = -EILSEQ;
  917. }
  918. end_cmd = 1;
  919. }
  920. if (host->data || host->response_busy) {
  921. if (host->data)
  922. omap_hsmmc_dma_cleanup(host,
  923. -ETIMEDOUT);
  924. host->response_busy = 0;
  925. omap_hsmmc_reset_controller_fsm(host, SRD);
  926. }
  927. }
  928. if ((status & DATA_TIMEOUT) ||
  929. (status & DATA_CRC)) {
  930. if (host->data || host->response_busy) {
  931. int err = (status & DATA_TIMEOUT) ?
  932. -ETIMEDOUT : -EILSEQ;
  933. if (host->data)
  934. omap_hsmmc_dma_cleanup(host, err);
  935. else
  936. host->mrq->cmd->error = err;
  937. host->response_busy = 0;
  938. omap_hsmmc_reset_controller_fsm(host, SRD);
  939. end_trans = 1;
  940. }
  941. }
  942. if (status & CARD_ERR) {
  943. dev_dbg(mmc_dev(host->mmc),
  944. "Ignoring card err CMD%d\n", host->cmd->opcode);
  945. if (host->cmd)
  946. end_cmd = 1;
  947. if (host->data)
  948. end_trans = 1;
  949. }
  950. }
  951. OMAP_HSMMC_WRITE(host->base, STAT, status);
  952. if (end_cmd || ((status & CC) && host->cmd))
  953. omap_hsmmc_cmd_done(host, host->cmd);
  954. if ((end_trans || (status & TC)) && host->mrq)
  955. omap_hsmmc_xfer_done(host, data);
  956. }
  957. /*
  958. * MMC controller IRQ handler
  959. */
  960. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  961. {
  962. struct omap_hsmmc_host *host = dev_id;
  963. int status;
  964. status = OMAP_HSMMC_READ(host->base, STAT);
  965. do {
  966. omap_hsmmc_do_irq(host, status);
  967. /* Flush posted write */
  968. status = OMAP_HSMMC_READ(host->base, STAT);
  969. } while (status & INT_EN_MASK);
  970. return IRQ_HANDLED;
  971. }
  972. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  973. {
  974. unsigned long i;
  975. OMAP_HSMMC_WRITE(host->base, HCTL,
  976. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  977. for (i = 0; i < loops_per_jiffy; i++) {
  978. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  979. break;
  980. cpu_relax();
  981. }
  982. }
  983. /*
  984. * Switch MMC interface voltage ... only relevant for MMC1.
  985. *
  986. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  987. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  988. * Some chips, like eMMC ones, use internal transceivers.
  989. */
  990. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  991. {
  992. u32 reg_val = 0;
  993. int ret;
  994. /* Disable the clocks */
  995. pm_runtime_put_sync(host->dev);
  996. if (host->got_dbclk)
  997. clk_disable(host->dbclk);
  998. /* Turn the power off */
  999. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  1000. /* Turn the power ON with given VDD 1.8 or 3.0v */
  1001. if (!ret)
  1002. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
  1003. vdd);
  1004. pm_runtime_get_sync(host->dev);
  1005. if (host->got_dbclk)
  1006. clk_enable(host->dbclk);
  1007. if (ret != 0)
  1008. goto err;
  1009. OMAP_HSMMC_WRITE(host->base, HCTL,
  1010. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  1011. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  1012. /*
  1013. * If a MMC dual voltage card is detected, the set_ios fn calls
  1014. * this fn with VDD bit set for 1.8V. Upon card removal from the
  1015. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  1016. *
  1017. * Cope with a bit of slop in the range ... per data sheets:
  1018. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  1019. * but recommended values are 1.71V to 1.89V
  1020. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  1021. * but recommended values are 2.7V to 3.3V
  1022. *
  1023. * Board setup code shouldn't permit anything very out-of-range.
  1024. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  1025. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  1026. */
  1027. if ((1 << vdd) <= MMC_VDD_23_24)
  1028. reg_val |= SDVS18;
  1029. else
  1030. reg_val |= SDVS30;
  1031. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  1032. set_sd_bus_power(host);
  1033. return 0;
  1034. err:
  1035. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  1036. return ret;
  1037. }
  1038. /* Protect the card while the cover is open */
  1039. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1040. {
  1041. if (!mmc_slot(host).get_cover_state)
  1042. return;
  1043. host->reqs_blocked = 0;
  1044. if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
  1045. if (host->protect_card) {
  1046. printk(KERN_INFO "%s: cover is closed, "
  1047. "card is now accessible\n",
  1048. mmc_hostname(host->mmc));
  1049. host->protect_card = 0;
  1050. }
  1051. } else {
  1052. if (!host->protect_card) {
  1053. printk(KERN_INFO "%s: cover is open, "
  1054. "card is now inaccessible\n",
  1055. mmc_hostname(host->mmc));
  1056. host->protect_card = 1;
  1057. }
  1058. }
  1059. }
  1060. /*
  1061. * Work Item to notify the core about card insertion/removal
  1062. */
  1063. static void omap_hsmmc_detect(struct work_struct *work)
  1064. {
  1065. struct omap_hsmmc_host *host =
  1066. container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
  1067. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  1068. int carddetect;
  1069. if (host->suspended)
  1070. return;
  1071. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1072. if (slot->card_detect)
  1073. carddetect = slot->card_detect(host->dev, host->slot_id);
  1074. else {
  1075. omap_hsmmc_protect_card(host);
  1076. carddetect = -ENOSYS;
  1077. }
  1078. if (carddetect)
  1079. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1080. else
  1081. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  1082. }
  1083. /*
  1084. * ISR for handling card insertion and removal
  1085. */
  1086. static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
  1087. {
  1088. struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
  1089. if (host->suspended)
  1090. return IRQ_HANDLED;
  1091. schedule_work(&host->mmc_carddetect_work);
  1092. return IRQ_HANDLED;
  1093. }
  1094. static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
  1095. struct mmc_data *data)
  1096. {
  1097. int sync_dev;
  1098. if (data->flags & MMC_DATA_WRITE)
  1099. sync_dev = host->dma_line_tx;
  1100. else
  1101. sync_dev = host->dma_line_rx;
  1102. return sync_dev;
  1103. }
  1104. static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
  1105. struct mmc_data *data,
  1106. struct scatterlist *sgl)
  1107. {
  1108. int blksz, nblk, dma_ch;
  1109. dma_ch = host->dma_ch;
  1110. if (data->flags & MMC_DATA_WRITE) {
  1111. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  1112. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  1113. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  1114. sg_dma_address(sgl), 0, 0);
  1115. } else {
  1116. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  1117. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  1118. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  1119. sg_dma_address(sgl), 0, 0);
  1120. }
  1121. blksz = host->data->blksz;
  1122. nblk = sg_dma_len(sgl) / blksz;
  1123. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  1124. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  1125. omap_hsmmc_get_dma_sync_dev(host, data),
  1126. !(data->flags & MMC_DATA_WRITE));
  1127. omap_start_dma(dma_ch);
  1128. }
  1129. /*
  1130. * DMA call back function
  1131. */
  1132. static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
  1133. {
  1134. struct omap_hsmmc_host *host = cb_data;
  1135. struct mmc_data *data = host->mrq->data;
  1136. int dma_ch, req_in_progress;
  1137. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  1138. dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
  1139. ch_status);
  1140. return;
  1141. }
  1142. spin_lock(&host->irq_lock);
  1143. if (host->dma_ch < 0) {
  1144. spin_unlock(&host->irq_lock);
  1145. return;
  1146. }
  1147. host->dma_sg_idx++;
  1148. if (host->dma_sg_idx < host->dma_len) {
  1149. /* Fire up the next transfer. */
  1150. omap_hsmmc_config_dma_params(host, data,
  1151. data->sg + host->dma_sg_idx);
  1152. spin_unlock(&host->irq_lock);
  1153. return;
  1154. }
  1155. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  1156. omap_hsmmc_get_dma_dir(host, data));
  1157. req_in_progress = host->req_in_progress;
  1158. dma_ch = host->dma_ch;
  1159. host->dma_ch = -1;
  1160. spin_unlock(&host->irq_lock);
  1161. omap_free_dma(dma_ch);
  1162. /* If DMA has finished after TC, complete the request */
  1163. if (!req_in_progress) {
  1164. struct mmc_request *mrq = host->mrq;
  1165. host->mrq = NULL;
  1166. mmc_request_done(host->mmc, mrq);
  1167. }
  1168. }
  1169. /*
  1170. * Routine to configure and start DMA for the MMC card
  1171. */
  1172. static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
  1173. struct mmc_request *req)
  1174. {
  1175. int dma_ch = 0, ret = 0, i;
  1176. struct mmc_data *data = req->data;
  1177. /* Sanity check: all the SG entries must be aligned by block size. */
  1178. for (i = 0; i < data->sg_len; i++) {
  1179. struct scatterlist *sgl;
  1180. sgl = data->sg + i;
  1181. if (sgl->length % data->blksz)
  1182. return -EINVAL;
  1183. }
  1184. if ((data->blksz % 4) != 0)
  1185. /* REVISIT: The MMC buffer increments only when MSB is written.
  1186. * Return error for blksz which is non multiple of four.
  1187. */
  1188. return -EINVAL;
  1189. BUG_ON(host->dma_ch != -1);
  1190. ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
  1191. "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
  1192. if (ret != 0) {
  1193. dev_err(mmc_dev(host->mmc),
  1194. "%s: omap_request_dma() failed with %d\n",
  1195. mmc_hostname(host->mmc), ret);
  1196. return ret;
  1197. }
  1198. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  1199. data->sg_len, omap_hsmmc_get_dma_dir(host, data));
  1200. host->dma_ch = dma_ch;
  1201. host->dma_sg_idx = 0;
  1202. omap_hsmmc_config_dma_params(host, data, data->sg);
  1203. return 0;
  1204. }
  1205. static void set_data_timeout(struct omap_hsmmc_host *host,
  1206. unsigned int timeout_ns,
  1207. unsigned int timeout_clks)
  1208. {
  1209. unsigned int timeout, cycle_ns;
  1210. uint32_t reg, clkd, dto = 0;
  1211. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1212. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1213. if (clkd == 0)
  1214. clkd = 1;
  1215. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  1216. timeout = timeout_ns / cycle_ns;
  1217. timeout += timeout_clks;
  1218. if (timeout) {
  1219. while ((timeout & 0x80000000) == 0) {
  1220. dto += 1;
  1221. timeout <<= 1;
  1222. }
  1223. dto = 31 - dto;
  1224. timeout <<= 1;
  1225. if (timeout && dto)
  1226. dto += 1;
  1227. if (dto >= 13)
  1228. dto -= 13;
  1229. else
  1230. dto = 0;
  1231. if (dto > 14)
  1232. dto = 14;
  1233. }
  1234. reg &= ~DTO_MASK;
  1235. reg |= dto << DTO_SHIFT;
  1236. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1237. }
  1238. /*
  1239. * Configure block length for MMC/SD cards and initiate the transfer.
  1240. */
  1241. static int
  1242. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1243. {
  1244. int ret;
  1245. host->data = req->data;
  1246. if (req->data == NULL) {
  1247. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1248. /*
  1249. * Set an arbitrary 100ms data timeout for commands with
  1250. * busy signal.
  1251. */
  1252. if (req->cmd->flags & MMC_RSP_BUSY)
  1253. set_data_timeout(host, 100000000U, 0);
  1254. return 0;
  1255. }
  1256. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1257. | (req->data->blocks << 16));
  1258. set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
  1259. if (host->use_dma) {
  1260. ret = omap_hsmmc_start_dma_transfer(host, req);
  1261. if (ret != 0) {
  1262. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  1263. return ret;
  1264. }
  1265. }
  1266. return 0;
  1267. }
  1268. /*
  1269. * Request function. for read/write operation
  1270. */
  1271. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1272. {
  1273. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1274. int err;
  1275. BUG_ON(host->req_in_progress);
  1276. BUG_ON(host->dma_ch != -1);
  1277. if (host->protect_card) {
  1278. if (host->reqs_blocked < 3) {
  1279. /*
  1280. * Ensure the controller is left in a consistent
  1281. * state by resetting the command and data state
  1282. * machines.
  1283. */
  1284. omap_hsmmc_reset_controller_fsm(host, SRD);
  1285. omap_hsmmc_reset_controller_fsm(host, SRC);
  1286. host->reqs_blocked += 1;
  1287. }
  1288. req->cmd->error = -EBADF;
  1289. if (req->data)
  1290. req->data->error = -EBADF;
  1291. req->cmd->retries = 0;
  1292. mmc_request_done(mmc, req);
  1293. return;
  1294. } else if (host->reqs_blocked)
  1295. host->reqs_blocked = 0;
  1296. WARN_ON(host->mrq != NULL);
  1297. host->mrq = req;
  1298. err = omap_hsmmc_prepare_data(host, req);
  1299. if (err) {
  1300. req->cmd->error = err;
  1301. if (req->data)
  1302. req->data->error = err;
  1303. host->mrq = NULL;
  1304. mmc_request_done(mmc, req);
  1305. return;
  1306. }
  1307. omap_hsmmc_start_command(host, req->cmd, req->data);
  1308. }
  1309. /* Routine to configure clock values. Exposed API to core */
  1310. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1311. {
  1312. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1313. u16 dsor = 0;
  1314. unsigned long regval;
  1315. unsigned long timeout;
  1316. u32 con;
  1317. int do_send_init_stream = 0;
  1318. mmc_host_enable(host->mmc);
  1319. if (ios->power_mode != host->power_mode) {
  1320. switch (ios->power_mode) {
  1321. case MMC_POWER_OFF:
  1322. mmc_slot(host).set_power(host->dev, host->slot_id,
  1323. 0, 0);
  1324. host->vdd = 0;
  1325. break;
  1326. case MMC_POWER_UP:
  1327. mmc_slot(host).set_power(host->dev, host->slot_id,
  1328. 1, ios->vdd);
  1329. host->vdd = ios->vdd;
  1330. break;
  1331. case MMC_POWER_ON:
  1332. do_send_init_stream = 1;
  1333. break;
  1334. }
  1335. host->power_mode = ios->power_mode;
  1336. }
  1337. /* FIXME: set registers based only on changes to ios */
  1338. con = OMAP_HSMMC_READ(host->base, CON);
  1339. switch (mmc->ios.bus_width) {
  1340. case MMC_BUS_WIDTH_8:
  1341. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  1342. break;
  1343. case MMC_BUS_WIDTH_4:
  1344. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  1345. OMAP_HSMMC_WRITE(host->base, HCTL,
  1346. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  1347. break;
  1348. case MMC_BUS_WIDTH_1:
  1349. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  1350. OMAP_HSMMC_WRITE(host->base, HCTL,
  1351. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  1352. break;
  1353. }
  1354. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1355. /* Only MMC1 can interface at 3V without some flavor
  1356. * of external transceiver; but they all handle 1.8V.
  1357. */
  1358. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1359. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1360. /*
  1361. * The mmc_select_voltage fn of the core does
  1362. * not seem to set the power_mode to
  1363. * MMC_POWER_UP upon recalculating the voltage.
  1364. * vdd 1.8v.
  1365. */
  1366. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1367. dev_dbg(mmc_dev(host->mmc),
  1368. "Switch operation failed\n");
  1369. }
  1370. }
  1371. if (ios->clock) {
  1372. dsor = host->master_clock / ios->clock;
  1373. if (dsor < 1)
  1374. dsor = 1;
  1375. if (host->master_clock / dsor > ios->clock)
  1376. dsor++;
  1377. if (dsor > 250)
  1378. dsor = 250;
  1379. }
  1380. omap_hsmmc_stop_clock(host);
  1381. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  1382. regval = regval & ~(CLKD_MASK);
  1383. regval = regval | (dsor << 6) | (DTO << 16);
  1384. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  1385. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  1386. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  1387. /* Wait till the ICS bit is set */
  1388. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  1389. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  1390. && time_before(jiffies, timeout))
  1391. msleep(1);
  1392. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  1393. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  1394. if (do_send_init_stream)
  1395. send_init_stream(host);
  1396. con = OMAP_HSMMC_READ(host->base, CON);
  1397. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  1398. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  1399. else
  1400. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  1401. if (host->power_mode == MMC_POWER_OFF)
  1402. mmc_host_disable(host->mmc);
  1403. else
  1404. mmc_host_lazy_disable(host->mmc);
  1405. }
  1406. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1407. {
  1408. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1409. if (!mmc_slot(host).card_detect)
  1410. return -ENOSYS;
  1411. return mmc_slot(host).card_detect(host->dev, host->slot_id);
  1412. }
  1413. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  1414. {
  1415. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1416. if (!mmc_slot(host).get_ro)
  1417. return -ENOSYS;
  1418. return mmc_slot(host).get_ro(host->dev, 0);
  1419. }
  1420. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1421. {
  1422. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1423. if (mmc_slot(host).init_card)
  1424. mmc_slot(host).init_card(card);
  1425. }
  1426. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1427. {
  1428. u32 hctl, capa, value;
  1429. /* Only MMC1 supports 3.0V */
  1430. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1431. hctl = SDVS30;
  1432. capa = VS30 | VS18;
  1433. } else {
  1434. hctl = SDVS18;
  1435. capa = VS18;
  1436. }
  1437. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1438. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1439. value = OMAP_HSMMC_READ(host->base, CAPA);
  1440. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1441. /* Set the controller to AUTO IDLE mode */
  1442. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  1443. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  1444. /* Set SD bus power bit */
  1445. set_sd_bus_power(host);
  1446. }
  1447. /*
  1448. * Dynamic power saving handling, FSM:
  1449. * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
  1450. * ^___________| | |
  1451. * |______________________|______________________|
  1452. *
  1453. * ENABLED: mmc host is fully functional
  1454. * DISABLED: fclk is off
  1455. * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
  1456. * REGSLEEP: fclk is off, voltage regulator is asleep
  1457. * OFF: fclk is off, voltage regulator is off
  1458. *
  1459. * Transition handlers return the timeout for the next state transition
  1460. * or negative error.
  1461. */
  1462. enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
  1463. /* Handler for [ENABLED -> DISABLED] transition */
  1464. static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
  1465. {
  1466. pm_runtime_put_sync(host->dev);
  1467. host->dpm_state = DISABLED;
  1468. dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
  1469. if (host->power_mode == MMC_POWER_OFF)
  1470. return 0;
  1471. return OMAP_MMC_SLEEP_TIMEOUT;
  1472. }
  1473. /* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
  1474. static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
  1475. {
  1476. int err, new_state;
  1477. if (!mmc_try_claim_host(host->mmc))
  1478. return 0;
  1479. pm_runtime_get_sync(host->dev);
  1480. if (mmc_card_can_sleep(host->mmc)) {
  1481. err = mmc_card_sleep(host->mmc);
  1482. if (err < 0) {
  1483. pm_runtime_put_sync(host->dev);
  1484. mmc_release_host(host->mmc);
  1485. return err;
  1486. }
  1487. new_state = CARDSLEEP;
  1488. } else {
  1489. new_state = REGSLEEP;
  1490. }
  1491. if (mmc_slot(host).set_sleep)
  1492. mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
  1493. new_state == CARDSLEEP);
  1494. /* FIXME: turn off bus power and perhaps interrupts too */
  1495. pm_runtime_put_sync(host->dev);
  1496. host->dpm_state = new_state;
  1497. mmc_release_host(host->mmc);
  1498. dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
  1499. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1500. if (mmc_slot(host).no_off)
  1501. return 0;
  1502. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
  1503. mmc_slot(host).card_detect ||
  1504. (mmc_slot(host).get_cover_state &&
  1505. mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
  1506. return OMAP_MMC_OFF_TIMEOUT;
  1507. return 0;
  1508. }
  1509. /* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
  1510. static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
  1511. {
  1512. if (!mmc_try_claim_host(host->mmc))
  1513. return 0;
  1514. if (mmc_slot(host).no_off)
  1515. return 0;
  1516. if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
  1517. mmc_slot(host).card_detect ||
  1518. (mmc_slot(host).get_cover_state &&
  1519. mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
  1520. mmc_release_host(host->mmc);
  1521. return 0;
  1522. }
  1523. mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  1524. host->vdd = 0;
  1525. host->power_mode = MMC_POWER_OFF;
  1526. dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
  1527. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1528. host->dpm_state = OFF;
  1529. mmc_release_host(host->mmc);
  1530. return 0;
  1531. }
  1532. /* Handler for [DISABLED -> ENABLED] transition */
  1533. static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
  1534. {
  1535. pm_runtime_get_sync(host->dev);
  1536. host->dpm_state = ENABLED;
  1537. dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
  1538. return 0;
  1539. }
  1540. /* Handler for [SLEEP -> ENABLED] transition */
  1541. static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
  1542. {
  1543. if (!mmc_try_claim_host(host->mmc))
  1544. return 0;
  1545. pm_runtime_get_sync(host->dev);
  1546. if (mmc_slot(host).set_sleep)
  1547. mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
  1548. host->vdd, host->dpm_state == CARDSLEEP);
  1549. if (mmc_card_can_sleep(host->mmc))
  1550. mmc_card_awake(host->mmc);
  1551. dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
  1552. host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
  1553. host->dpm_state = ENABLED;
  1554. mmc_release_host(host->mmc);
  1555. return 0;
  1556. }
  1557. /* Handler for [OFF -> ENABLED] transition */
  1558. static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
  1559. {
  1560. pm_runtime_get_sync(host->dev);
  1561. omap_hsmmc_conf_bus_power(host);
  1562. mmc_power_restore_host(host->mmc);
  1563. host->dpm_state = ENABLED;
  1564. dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
  1565. return 0;
  1566. }
  1567. /*
  1568. * Bring MMC host to ENABLED from any other PM state.
  1569. */
  1570. static int omap_hsmmc_enable(struct mmc_host *mmc)
  1571. {
  1572. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1573. switch (host->dpm_state) {
  1574. case DISABLED:
  1575. return omap_hsmmc_disabled_to_enabled(host);
  1576. case CARDSLEEP:
  1577. case REGSLEEP:
  1578. return omap_hsmmc_sleep_to_enabled(host);
  1579. case OFF:
  1580. return omap_hsmmc_off_to_enabled(host);
  1581. default:
  1582. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1583. return -EINVAL;
  1584. }
  1585. }
  1586. /*
  1587. * Bring MMC host in PM state (one level deeper).
  1588. */
  1589. static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
  1590. {
  1591. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1592. switch (host->dpm_state) {
  1593. case ENABLED: {
  1594. int delay;
  1595. delay = omap_hsmmc_enabled_to_disabled(host);
  1596. if (lazy || delay < 0)
  1597. return delay;
  1598. return 0;
  1599. }
  1600. case DISABLED:
  1601. return omap_hsmmc_disabled_to_sleep(host);
  1602. case CARDSLEEP:
  1603. case REGSLEEP:
  1604. return omap_hsmmc_sleep_to_off(host);
  1605. default:
  1606. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1607. return -EINVAL;
  1608. }
  1609. }
  1610. static int omap_hsmmc_enable_simple(struct mmc_host *mmc)
  1611. {
  1612. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1613. pm_runtime_get_sync(host->dev);
  1614. dev_dbg(mmc_dev(host->mmc), "enabled\n");
  1615. return 0;
  1616. }
  1617. static int omap_hsmmc_disable_simple(struct mmc_host *mmc, int lazy)
  1618. {
  1619. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1620. pm_runtime_put_sync(host->dev);
  1621. dev_dbg(mmc_dev(host->mmc), "idle\n");
  1622. return 0;
  1623. }
  1624. static const struct mmc_host_ops omap_hsmmc_ops = {
  1625. .enable = omap_hsmmc_enable_simple,
  1626. .disable = omap_hsmmc_disable_simple,
  1627. .request = omap_hsmmc_request,
  1628. .set_ios = omap_hsmmc_set_ios,
  1629. .get_cd = omap_hsmmc_get_cd,
  1630. .get_ro = omap_hsmmc_get_ro,
  1631. .init_card = omap_hsmmc_init_card,
  1632. /* NYET -- enable_sdio_irq */
  1633. };
  1634. static const struct mmc_host_ops omap_hsmmc_ps_ops = {
  1635. .enable = omap_hsmmc_enable,
  1636. .disable = omap_hsmmc_disable,
  1637. .request = omap_hsmmc_request,
  1638. .set_ios = omap_hsmmc_set_ios,
  1639. .get_cd = omap_hsmmc_get_cd,
  1640. .get_ro = omap_hsmmc_get_ro,
  1641. .init_card = omap_hsmmc_init_card,
  1642. /* NYET -- enable_sdio_irq */
  1643. };
  1644. #ifdef CONFIG_DEBUG_FS
  1645. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1646. {
  1647. struct mmc_host *mmc = s->private;
  1648. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1649. seq_printf(s, "mmc%d:\n"
  1650. " enabled:\t%d\n"
  1651. " dpm_state:\t%d\n"
  1652. " nesting_cnt:\t%d\n"
  1653. " ct",
  1654. mmc->index, mmc->enabled ? 1 : 0,
  1655. host->dpm_state, mmc->nesting_cnt);
  1656. if (host->suspended || host->dpm_state == OFF) {
  1657. seq_printf(s, "host suspended, can't read registers\n");
  1658. return 0;
  1659. }
  1660. pm_runtime_get_sync(host->dev);
  1661. seq_printf(s, "SYSCONFIG:\t0x%08x\n",
  1662. OMAP_HSMMC_READ(host->base, SYSCONFIG));
  1663. seq_printf(s, "CON:\t\t0x%08x\n",
  1664. OMAP_HSMMC_READ(host->base, CON));
  1665. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1666. OMAP_HSMMC_READ(host->base, HCTL));
  1667. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1668. OMAP_HSMMC_READ(host->base, SYSCTL));
  1669. seq_printf(s, "IE:\t\t0x%08x\n",
  1670. OMAP_HSMMC_READ(host->base, IE));
  1671. seq_printf(s, "ISE:\t\t0x%08x\n",
  1672. OMAP_HSMMC_READ(host->base, ISE));
  1673. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1674. OMAP_HSMMC_READ(host->base, CAPA));
  1675. pm_runtime_put_sync(host->dev);
  1676. return 0;
  1677. }
  1678. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1679. {
  1680. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1681. }
  1682. static const struct file_operations mmc_regs_fops = {
  1683. .open = omap_hsmmc_regs_open,
  1684. .read = seq_read,
  1685. .llseek = seq_lseek,
  1686. .release = single_release,
  1687. };
  1688. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1689. {
  1690. if (mmc->debugfs_root)
  1691. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1692. mmc, &mmc_regs_fops);
  1693. }
  1694. #else
  1695. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1696. {
  1697. }
  1698. #endif
  1699. static int __init omap_hsmmc_probe(struct platform_device *pdev)
  1700. {
  1701. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1702. struct mmc_host *mmc;
  1703. struct omap_hsmmc_host *host = NULL;
  1704. struct resource *res;
  1705. int ret, irq;
  1706. if (pdata == NULL) {
  1707. dev_err(&pdev->dev, "Platform Data is missing\n");
  1708. return -ENXIO;
  1709. }
  1710. if (pdata->nr_slots == 0) {
  1711. dev_err(&pdev->dev, "No Slots\n");
  1712. return -ENXIO;
  1713. }
  1714. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1715. irq = platform_get_irq(pdev, 0);
  1716. if (res == NULL || irq < 0)
  1717. return -ENXIO;
  1718. res->start += pdata->reg_offset;
  1719. res->end += pdata->reg_offset;
  1720. res = request_mem_region(res->start, resource_size(res), pdev->name);
  1721. if (res == NULL)
  1722. return -EBUSY;
  1723. ret = omap_hsmmc_gpio_init(pdata);
  1724. if (ret)
  1725. goto err;
  1726. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1727. if (!mmc) {
  1728. ret = -ENOMEM;
  1729. goto err_alloc;
  1730. }
  1731. host = mmc_priv(mmc);
  1732. host->mmc = mmc;
  1733. host->pdata = pdata;
  1734. host->dev = &pdev->dev;
  1735. host->use_dma = 1;
  1736. host->dev->dma_mask = &pdata->dma_mask;
  1737. host->dma_ch = -1;
  1738. host->irq = irq;
  1739. host->id = pdev->id;
  1740. host->slot_id = 0;
  1741. host->mapbase = res->start;
  1742. host->base = ioremap(host->mapbase, SZ_4K);
  1743. host->power_mode = MMC_POWER_OFF;
  1744. host->master_clock = OMAP_MMC_MASTER_CLOCK;
  1745. if (mmc_slot(host).features & HSMMC_HAS_48MHZ_MASTER_CLK)
  1746. host->master_clock = OMAP_MMC_MASTER_CLOCK / 2;
  1747. platform_set_drvdata(pdev, host);
  1748. INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
  1749. if (mmc_slot(host).power_saving)
  1750. mmc->ops = &omap_hsmmc_ps_ops;
  1751. else
  1752. mmc->ops = &omap_hsmmc_ops;
  1753. /*
  1754. * If regulator_disable can only put vcc_aux to sleep then there is
  1755. * no off state.
  1756. */
  1757. if (mmc_slot(host).vcc_aux_disable_is_sleep)
  1758. mmc_slot(host).no_off = 1;
  1759. mmc->f_min = 400000;
  1760. mmc->f_max = 52000000;
  1761. spin_lock_init(&host->irq_lock);
  1762. host->iclk = clk_get(&pdev->dev, "ick");
  1763. if (IS_ERR(host->iclk)) {
  1764. ret = PTR_ERR(host->iclk);
  1765. host->iclk = NULL;
  1766. goto err1;
  1767. }
  1768. host->fclk = clk_get(&pdev->dev, "fck");
  1769. if (IS_ERR(host->fclk)) {
  1770. ret = PTR_ERR(host->fclk);
  1771. host->fclk = NULL;
  1772. clk_put(host->iclk);
  1773. goto err1;
  1774. }
  1775. omap_hsmmc_context_save(host);
  1776. mmc->caps |= MMC_CAP_DISABLE;
  1777. mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
  1778. /* we start off in DISABLED state */
  1779. host->dpm_state = DISABLED;
  1780. pm_runtime_enable(host->dev);
  1781. #ifndef CONFIG_PM_RUNTIME
  1782. /*
  1783. * If runtime PM is not enabled, ensure clocks are always enabled.
  1784. */
  1785. clk_enable(host->iclk);
  1786. clk_enable(host->fclk);
  1787. #endif
  1788. if (mmc_host_enable(host->mmc) != 0)
  1789. goto err1;
  1790. if (cpu_is_omap2430()) {
  1791. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  1792. /*
  1793. * MMC can still work without debounce clock.
  1794. */
  1795. if (IS_ERR(host->dbclk))
  1796. dev_warn(mmc_dev(host->mmc),
  1797. "Failed to get debounce clock\n");
  1798. else
  1799. host->got_dbclk = 1;
  1800. if (host->got_dbclk)
  1801. if (clk_enable(host->dbclk) != 0)
  1802. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  1803. " clk failed\n");
  1804. }
  1805. /* Since we do only SG emulation, we can have as many segs
  1806. * as we want. */
  1807. mmc->max_segs = 1024;
  1808. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1809. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1810. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1811. mmc->max_seg_size = mmc->max_req_size;
  1812. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1813. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  1814. mmc->caps |= mmc_slot(host).caps;
  1815. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1816. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1817. if (mmc_slot(host).nonremovable)
  1818. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1819. mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_IGNORE_PM_NOTIFY;
  1820. if (mmc_slot(host).mmc_data.built_in)
  1821. mmc->pm_flags = MMC_PM_KEEP_POWER | MMC_PM_IGNORE_PM_NOTIFY;
  1822. omap_hsmmc_conf_bus_power(host);
  1823. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1824. if (!res) {
  1825. dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
  1826. goto err_irq;
  1827. }
  1828. host->dma_line_tx = res->start;
  1829. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1830. if (!res) {
  1831. dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
  1832. goto err_irq;
  1833. }
  1834. host->dma_line_rx = res->end;
  1835. /* Request IRQ for MMC operations */
  1836. ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
  1837. mmc_hostname(mmc), host);
  1838. if (ret) {
  1839. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1840. goto err_irq;
  1841. }
  1842. if (pdata->init != NULL) {
  1843. if (pdata->init(&pdev->dev) != 0) {
  1844. dev_dbg(mmc_dev(host->mmc),
  1845. "Unable to configure MMC IRQs\n");
  1846. goto err_irq_cd_init;
  1847. }
  1848. }
  1849. if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
  1850. ret = omap_hsmmc_reg_get(host);
  1851. if (ret)
  1852. goto err_reg;
  1853. host->use_reg = 1;
  1854. }
  1855. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  1856. /* Request IRQ for card detect */
  1857. if ((mmc_slot(host).card_detect_irq)) {
  1858. ret = request_irq(mmc_slot(host).card_detect_irq,
  1859. omap_hsmmc_cd_handler,
  1860. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  1861. | IRQF_DISABLED,
  1862. mmc_hostname(mmc), host);
  1863. if (ret) {
  1864. dev_dbg(mmc_dev(host->mmc),
  1865. "Unable to grab MMC CD IRQ\n");
  1866. goto err_irq_cd;
  1867. }
  1868. pdata->suspend = omap_hsmmc_suspend_cdirq;
  1869. pdata->resume = omap_hsmmc_resume_cdirq;
  1870. } else if (mmc_slot(host).mmc_data.register_status_notify) {
  1871. mmc_slot(host).mmc_data.register_status_notify(omap_hsmmc_status_notify_cb, host);
  1872. }
  1873. if (mmc_slot(host).mmc_data.status)
  1874. mmc_slot(host).mmc_data.card_present =
  1875. mmc_slot(host).mmc_data.status(mmc_dev(host->mmc));
  1876. omap_hsmmc_disable_irq(host);
  1877. mmc_host_lazy_disable(host->mmc);
  1878. omap_hsmmc_protect_card(host);
  1879. mmc_add_host(mmc);
  1880. if (mmc_slot(host).name != NULL) {
  1881. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1882. if (ret < 0)
  1883. goto err_slot_name;
  1884. }
  1885. if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
  1886. ret = device_create_file(&mmc->class_dev,
  1887. &dev_attr_cover_switch);
  1888. if (ret < 0)
  1889. goto err_slot_name;
  1890. }
  1891. omap_hsmmc_debugfs(mmc);
  1892. return 0;
  1893. err_slot_name:
  1894. mmc_remove_host(mmc);
  1895. free_irq(mmc_slot(host).card_detect_irq, host);
  1896. err_irq_cd:
  1897. if (host->use_reg)
  1898. omap_hsmmc_reg_put(host);
  1899. err_reg:
  1900. if (host->pdata->cleanup)
  1901. host->pdata->cleanup(&pdev->dev);
  1902. err_irq_cd_init:
  1903. free_irq(host->irq, host);
  1904. err_irq:
  1905. mmc_host_disable(host->mmc);
  1906. clk_put(host->fclk);
  1907. clk_put(host->iclk);
  1908. if (host->got_dbclk) {
  1909. clk_disable(host->dbclk);
  1910. clk_put(host->dbclk);
  1911. }
  1912. err1:
  1913. iounmap(host->base);
  1914. platform_set_drvdata(pdev, NULL);
  1915. mmc_free_host(mmc);
  1916. err_alloc:
  1917. omap_hsmmc_gpio_free(pdata);
  1918. err:
  1919. release_mem_region(res->start, resource_size(res));
  1920. return ret;
  1921. }
  1922. static int omap_hsmmc_remove(struct platform_device *pdev)
  1923. {
  1924. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1925. struct resource *res;
  1926. if (host) {
  1927. mmc_host_enable(host->mmc);
  1928. mmc_remove_host(host->mmc);
  1929. if (host->use_reg)
  1930. omap_hsmmc_reg_put(host);
  1931. if (host->pdata->cleanup)
  1932. host->pdata->cleanup(&pdev->dev);
  1933. free_irq(host->irq, host);
  1934. if (mmc_slot(host).card_detect_irq)
  1935. free_irq(mmc_slot(host).card_detect_irq, host);
  1936. flush_work_sync(&host->mmc_carddetect_work);
  1937. mmc_host_disable(host->mmc);
  1938. pm_runtime_suspend(host->dev);
  1939. clk_put(host->fclk);
  1940. clk_put(host->iclk);
  1941. if (host->got_dbclk) {
  1942. clk_disable(host->dbclk);
  1943. clk_put(host->dbclk);
  1944. }
  1945. mmc_free_host(host->mmc);
  1946. iounmap(host->base);
  1947. omap_hsmmc_gpio_free(pdev->dev.platform_data);
  1948. }
  1949. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1950. if (res)
  1951. release_mem_region(res->start, resource_size(res));
  1952. platform_set_drvdata(pdev, NULL);
  1953. return 0;
  1954. }
  1955. #ifdef CONFIG_PM
  1956. static int omap_hsmmc_suspend(struct device *dev)
  1957. {
  1958. int ret = 0;
  1959. struct platform_device *pdev = to_platform_device(dev);
  1960. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1961. if (host && host->suspended)
  1962. return 0;
  1963. if (host) {
  1964. host->suspended = 1;
  1965. if (host->pdata->suspend) {
  1966. ret = host->pdata->suspend(&pdev->dev,
  1967. host->slot_id);
  1968. if (ret) {
  1969. dev_dbg(mmc_dev(host->mmc),
  1970. "Unable to handle MMC board"
  1971. " level suspend\n");
  1972. host->suspended = 0;
  1973. return ret;
  1974. }
  1975. }
  1976. cancel_work_sync(&host->mmc_carddetect_work);
  1977. if (host->mmc->card && (host->mmc->card->type != MMC_TYPE_SDIO))
  1978. ret = mmc_suspend_host(host->mmc);
  1979. mmc_host_enable(host->mmc);
  1980. if (ret == 0) {
  1981. omap_hsmmc_disable_irq(host);
  1982. OMAP_HSMMC_WRITE(host->base, HCTL,
  1983. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1984. mmc_host_disable(host->mmc);
  1985. if (host->got_dbclk)
  1986. clk_disable(host->dbclk);
  1987. } else {
  1988. host->suspended = 0;
  1989. if (host->pdata->resume) {
  1990. ret = host->pdata->resume(&pdev->dev,
  1991. host->slot_id);
  1992. if (ret)
  1993. dev_dbg(mmc_dev(host->mmc),
  1994. "Unmask interrupt failed\n");
  1995. }
  1996. /*
  1997. * Directly call platform_bus suspend. runtime PM
  1998. * PM lock is held during system suspend, so will
  1999. * not be auto-matically called
  2000. */
  2001. mmc_host_disable(host->mmc);
  2002. }
  2003. }
  2004. return ret;
  2005. }
  2006. /* Routine to resume the MMC device */
  2007. static int omap_hsmmc_resume(struct device *dev)
  2008. {
  2009. int ret = 0;
  2010. struct platform_device *pdev = to_platform_device(dev);
  2011. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  2012. if (host && !host->suspended)
  2013. return 0;
  2014. if (host) {
  2015. if (mmc_host_enable(host->mmc) != 0) {
  2016. goto clk_en_err;
  2017. }
  2018. if (host->got_dbclk)
  2019. clk_enable(host->dbclk);
  2020. omap_hsmmc_conf_bus_power(host);
  2021. if (host->pdata->resume) {
  2022. ret = host->pdata->resume(&pdev->dev, host->slot_id);
  2023. if (ret)
  2024. dev_dbg(mmc_dev(host->mmc),
  2025. "Unmask interrupt failed\n");
  2026. }
  2027. omap_hsmmc_protect_card(host);
  2028. /* Notify the core to resume the host */
  2029. if (host->mmc->card && (host->mmc->card->type != MMC_TYPE_SDIO))
  2030. ret = mmc_resume_host(host->mmc);
  2031. if (ret == 0)
  2032. host->suspended = 0;
  2033. mmc_host_lazy_disable(host->mmc);
  2034. }
  2035. return ret;
  2036. clk_en_err:
  2037. dev_dbg(mmc_dev(host->mmc),
  2038. "Failed to enable MMC clocks during resume\n");
  2039. return ret;
  2040. }
  2041. #else
  2042. #define omap_hsmmc_suspend NULL
  2043. #define omap_hsmmc_resume NULL
  2044. #endif
  2045. /* called just before device is disabled */
  2046. static int omap_hsmmc_runtime_suspend(struct device *dev)
  2047. {
  2048. struct omap_hsmmc_host *host;
  2049. dev_dbg(dev, "%s\n", __func__);
  2050. host = platform_get_drvdata(to_platform_device(dev));
  2051. omap_hsmmc_context_save(host);
  2052. return 0;
  2053. }
  2054. /* called after device is (re)enabled, ONLY if context was lost */
  2055. static int omap_hsmmc_runtime_resume(struct device *dev)
  2056. {
  2057. struct omap_hsmmc_host *host;
  2058. dev_dbg(dev, "%s\n", __func__);
  2059. host = platform_get_drvdata(to_platform_device(dev));
  2060. omap_hsmmc_context_restore(host);
  2061. return 0;
  2062. }
  2063. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  2064. .suspend = omap_hsmmc_suspend,
  2065. .resume = omap_hsmmc_resume,
  2066. .runtime_suspend = omap_hsmmc_runtime_suspend,
  2067. .runtime_resume = omap_hsmmc_runtime_resume,
  2068. };
  2069. static struct platform_driver omap_hsmmc_driver = {
  2070. .remove = omap_hsmmc_remove,
  2071. .driver = {
  2072. .name = DRIVER_NAME,
  2073. .owner = THIS_MODULE,
  2074. .pm = &omap_hsmmc_dev_pm_ops,
  2075. },
  2076. };
  2077. static int __init omap_hsmmc_init(void)
  2078. {
  2079. /* Register the MMC driver */
  2080. return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
  2081. }
  2082. static void __exit omap_hsmmc_cleanup(void)
  2083. {
  2084. /* Unregister MMC driver */
  2085. platform_driver_unregister(&omap_hsmmc_driver);
  2086. }
  2087. module_init(omap_hsmmc_init);
  2088. module_exit(omap_hsmmc_cleanup);
  2089. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  2090. MODULE_LICENSE("GPL");
  2091. MODULE_ALIAS("platform:" DRIVER_NAME);
  2092. MODULE_AUTHOR("Texas Instruments Inc");