sdhci-esdhc-imx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Freescale eSDHC i.MX controller driver for the platform bus.
  3. *
  4. * derived from the OF-version.
  5. *
  6. * Copyright (c) 2010 Pengutronix e.K.
  7. * Author: Wolfram Sang <w.sang@pengutronix.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/clk.h>
  17. #include <linux/gpio.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/mmc/host.h>
  21. #include <linux/mmc/mmc.h>
  22. #include <linux/mmc/sdio.h>
  23. #include <linux/mmc/slot-gpio.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <linux/platform_data/mmc-esdhc-imx.h>
  29. #include "sdhci-pltfm.h"
  30. #include "sdhci-esdhc.h"
  31. #define ESDHC_CTRL_D3CD 0x08
  32. /* VENDOR SPEC register */
  33. #define ESDHC_VENDOR_SPEC 0xc0
  34. #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
  35. #define ESDHC_WTMK_LVL 0x44
  36. #define ESDHC_MIX_CTRL 0x48
  37. #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
  38. /* Bits 3 and 6 are not SDHCI standard definitions */
  39. #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
  40. /*
  41. * Our interpretation of the SDHCI_HOST_CONTROL register
  42. */
  43. #define ESDHC_CTRL_4BITBUS (0x1 << 1)
  44. #define ESDHC_CTRL_8BITBUS (0x2 << 1)
  45. #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
  46. /*
  47. * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
  48. * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
  49. * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
  50. * Define this macro DMA error INT for fsl eSDHC
  51. */
  52. #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
  53. /*
  54. * The CMDTYPE of the CMD register (offset 0xE) should be set to
  55. * "11" when the STOP CMD12 is issued on imx53 to abort one
  56. * open ended multi-blk IO. Otherwise the TC INT wouldn't
  57. * be generated.
  58. * In exact block transfer, the controller doesn't complete the
  59. * operations automatically as required at the end of the
  60. * transfer and remains on hold if the abort command is not sent.
  61. * As a result, the TC flag is not asserted and SW received timeout
  62. * exeception. Bit1 of Vendor Spec registor is used to fix it.
  63. */
  64. #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
  65. enum imx_esdhc_type {
  66. IMX25_ESDHC,
  67. IMX35_ESDHC,
  68. IMX51_ESDHC,
  69. IMX53_ESDHC,
  70. IMX6Q_USDHC,
  71. };
  72. struct pltfm_imx_data {
  73. int flags;
  74. u32 scratchpad;
  75. enum imx_esdhc_type devtype;
  76. struct pinctrl *pinctrl;
  77. struct esdhc_platform_data boarddata;
  78. struct clk *clk_ipg;
  79. struct clk *clk_ahb;
  80. struct clk *clk_per;
  81. enum {
  82. NO_CMD_PENDING, /* no multiblock command pending*/
  83. MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
  84. WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
  85. } multiblock_status;
  86. };
  87. static struct platform_device_id imx_esdhc_devtype[] = {
  88. {
  89. .name = "sdhci-esdhc-imx25",
  90. .driver_data = IMX25_ESDHC,
  91. }, {
  92. .name = "sdhci-esdhc-imx35",
  93. .driver_data = IMX35_ESDHC,
  94. }, {
  95. .name = "sdhci-esdhc-imx51",
  96. .driver_data = IMX51_ESDHC,
  97. }, {
  98. .name = "sdhci-esdhc-imx53",
  99. .driver_data = IMX53_ESDHC,
  100. }, {
  101. .name = "sdhci-usdhc-imx6q",
  102. .driver_data = IMX6Q_USDHC,
  103. }, {
  104. /* sentinel */
  105. }
  106. };
  107. MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
  108. static const struct of_device_id imx_esdhc_dt_ids[] = {
  109. { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
  110. { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
  111. { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
  112. { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
  113. { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
  114. { /* sentinel */ }
  115. };
  116. MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
  117. static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
  118. {
  119. return data->devtype == IMX25_ESDHC;
  120. }
  121. static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
  122. {
  123. return data->devtype == IMX35_ESDHC;
  124. }
  125. static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
  126. {
  127. return data->devtype == IMX51_ESDHC;
  128. }
  129. static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
  130. {
  131. return data->devtype == IMX53_ESDHC;
  132. }
  133. static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
  134. {
  135. return data->devtype == IMX6Q_USDHC;
  136. }
  137. static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
  138. {
  139. void __iomem *base = host->ioaddr + (reg & ~0x3);
  140. u32 shift = (reg & 0x3) * 8;
  141. writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
  142. }
  143. static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
  144. {
  145. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  146. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  147. u32 val = readl(host->ioaddr + reg);
  148. if (unlikely(reg == SDHCI_CAPABILITIES)) {
  149. /* In FSL esdhc IC module, only bit20 is used to indicate the
  150. * ADMA2 capability of esdhc, but this bit is messed up on
  151. * some SOCs (e.g. on MX25, MX35 this bit is set, but they
  152. * don't actually support ADMA2). So set the BROKEN_ADMA
  153. * uirk on MX25/35 platforms.
  154. */
  155. if (val & SDHCI_CAN_DO_ADMA1) {
  156. val &= ~SDHCI_CAN_DO_ADMA1;
  157. val |= SDHCI_CAN_DO_ADMA2;
  158. }
  159. }
  160. if (unlikely(reg == SDHCI_INT_STATUS)) {
  161. if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
  162. val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  163. val |= SDHCI_INT_ADMA_ERROR;
  164. }
  165. /*
  166. * mask off the interrupt we get in response to the manually
  167. * sent CMD12
  168. */
  169. if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
  170. ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
  171. val &= ~SDHCI_INT_RESPONSE;
  172. writel(SDHCI_INT_RESPONSE, host->ioaddr +
  173. SDHCI_INT_STATUS);
  174. imx_data->multiblock_status = NO_CMD_PENDING;
  175. }
  176. }
  177. return val;
  178. }
  179. static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
  180. {
  181. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  182. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  183. u32 data;
  184. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
  185. if (val & SDHCI_INT_CARD_INT) {
  186. /*
  187. * Clear and then set D3CD bit to avoid missing the
  188. * card interrupt. This is a eSDHC controller problem
  189. * so we need to apply the following workaround: clear
  190. * and set D3CD bit will make eSDHC re-sample the card
  191. * interrupt. In case a card interrupt was lost,
  192. * re-sample it by the following steps.
  193. */
  194. data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
  195. data &= ~ESDHC_CTRL_D3CD;
  196. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  197. data |= ESDHC_CTRL_D3CD;
  198. writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
  199. }
  200. }
  201. if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  202. && (reg == SDHCI_INT_STATUS)
  203. && (val & SDHCI_INT_DATA_END))) {
  204. u32 v;
  205. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  206. v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  207. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  208. if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
  209. {
  210. /* send a manual CMD12 with RESPTYP=none */
  211. data = MMC_STOP_TRANSMISSION << 24 |
  212. SDHCI_CMD_ABORTCMD << 16;
  213. writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
  214. imx_data->multiblock_status = WAIT_FOR_INT;
  215. }
  216. }
  217. if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
  218. if (val & SDHCI_INT_ADMA_ERROR) {
  219. val &= ~SDHCI_INT_ADMA_ERROR;
  220. val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
  221. }
  222. }
  223. writel(val, host->ioaddr + reg);
  224. }
  225. static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
  226. {
  227. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  228. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  229. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  230. reg ^= 2;
  231. if (is_imx6q_usdhc(imx_data)) {
  232. /*
  233. * The usdhc register returns a wrong host version.
  234. * Correct it here.
  235. */
  236. return SDHCI_SPEC_300;
  237. }
  238. }
  239. return readw(host->ioaddr + reg);
  240. }
  241. static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
  242. {
  243. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  244. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  245. switch (reg) {
  246. case SDHCI_TRANSFER_MODE:
  247. if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
  248. && (host->cmd->opcode == SD_IO_RW_EXTENDED)
  249. && (host->cmd->data->blocks > 1)
  250. && (host->cmd->data->flags & MMC_DATA_READ)) {
  251. u32 v;
  252. v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
  253. v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
  254. writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
  255. }
  256. if (is_imx6q_usdhc(imx_data)) {
  257. u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
  258. /* Swap AC23 bit */
  259. if (val & SDHCI_TRNS_AUTO_CMD23) {
  260. val &= ~SDHCI_TRNS_AUTO_CMD23;
  261. val |= ESDHC_MIX_CTRL_AC23EN;
  262. }
  263. m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
  264. writel(m, host->ioaddr + ESDHC_MIX_CTRL);
  265. } else {
  266. /*
  267. * Postpone this write, we must do it together with a
  268. * command write that is down below.
  269. */
  270. imx_data->scratchpad = val;
  271. }
  272. return;
  273. case SDHCI_COMMAND:
  274. if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
  275. val |= SDHCI_CMD_ABORTCMD;
  276. if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
  277. (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
  278. imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
  279. if (is_imx6q_usdhc(imx_data))
  280. writel(val << 16,
  281. host->ioaddr + SDHCI_TRANSFER_MODE);
  282. else
  283. writel(val << 16 | imx_data->scratchpad,
  284. host->ioaddr + SDHCI_TRANSFER_MODE);
  285. return;
  286. case SDHCI_BLOCK_SIZE:
  287. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  288. break;
  289. }
  290. esdhc_clrset_le(host, 0xffff, val, reg);
  291. }
  292. static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
  293. {
  294. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  295. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  296. u32 new_val;
  297. u32 mask;
  298. switch (reg) {
  299. case SDHCI_POWER_CONTROL:
  300. /*
  301. * FSL put some DMA bits here
  302. * If your board has a regulator, code should be here
  303. */
  304. return;
  305. case SDHCI_HOST_CONTROL:
  306. /* FSL messed up here, so we need to manually compose it. */
  307. new_val = val & SDHCI_CTRL_LED;
  308. /* ensure the endianness */
  309. new_val |= ESDHC_HOST_CONTROL_LE;
  310. /* bits 8&9 are reserved on mx25 */
  311. if (!is_imx25_esdhc(imx_data)) {
  312. /* DMA mode bits are shifted */
  313. new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
  314. }
  315. /*
  316. * Do not touch buswidth bits here. This is done in
  317. * esdhc_pltfm_bus_width.
  318. * Do not touch the D3CD bit either which is used for the
  319. * SDIO interrupt errata workaround.
  320. */
  321. mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
  322. esdhc_clrset_le(host, mask, new_val, reg);
  323. return;
  324. }
  325. esdhc_clrset_le(host, 0xff, val, reg);
  326. /*
  327. * The esdhc has a design violation to SDHC spec which tells
  328. * that software reset should not affect card detection circuit.
  329. * But esdhc clears its SYSCTL register bits [0..2] during the
  330. * software reset. This will stop those clocks that card detection
  331. * circuit relies on. To work around it, we turn the clocks on back
  332. * to keep card detection circuit functional.
  333. */
  334. if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
  335. esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
  336. /*
  337. * The reset on usdhc fails to clear MIX_CTRL register.
  338. * Do it manually here.
  339. */
  340. if (is_imx6q_usdhc(imx_data))
  341. writel(0, host->ioaddr + ESDHC_MIX_CTRL);
  342. }
  343. }
  344. static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
  345. {
  346. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  347. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  348. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  349. u32 f_host = clk_get_rate(pltfm_host->clk);
  350. if (boarddata->f_max && (boarddata->f_max < f_host))
  351. return boarddata->f_max;
  352. else
  353. return f_host;
  354. }
  355. static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
  356. {
  357. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  358. return clk_get_rate(pltfm_host->clk) / 256 / 16;
  359. }
  360. static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
  361. unsigned int clock)
  362. {
  363. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  364. unsigned int host_clock = clk_get_rate(pltfm_host->clk);
  365. int pre_div = 2;
  366. int div = 1;
  367. u32 temp;
  368. if (clock == 0)
  369. goto out;
  370. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  371. temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  372. | ESDHC_CLOCK_MASK);
  373. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  374. while (host_clock / pre_div / 16 > clock && pre_div < 256)
  375. pre_div *= 2;
  376. while (host_clock / pre_div / div > clock && div < 16)
  377. div++;
  378. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  379. clock, host_clock / pre_div / div);
  380. pre_div >>= 1;
  381. div--;
  382. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  383. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  384. | (div << ESDHC_DIVIDER_SHIFT)
  385. | (pre_div << ESDHC_PREDIV_SHIFT));
  386. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  387. mdelay(1);
  388. out:
  389. host->clock = clock;
  390. }
  391. static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
  392. {
  393. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  394. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  395. struct esdhc_platform_data *boarddata = &imx_data->boarddata;
  396. switch (boarddata->wp_type) {
  397. case ESDHC_WP_GPIO:
  398. return mmc_gpio_get_ro(host->mmc);
  399. case ESDHC_WP_CONTROLLER:
  400. return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  401. SDHCI_WRITE_PROTECT);
  402. case ESDHC_WP_NONE:
  403. break;
  404. }
  405. return -ENOSYS;
  406. }
  407. static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width)
  408. {
  409. u32 ctrl;
  410. switch (width) {
  411. case MMC_BUS_WIDTH_8:
  412. ctrl = ESDHC_CTRL_8BITBUS;
  413. break;
  414. case MMC_BUS_WIDTH_4:
  415. ctrl = ESDHC_CTRL_4BITBUS;
  416. break;
  417. default:
  418. ctrl = 0;
  419. break;
  420. }
  421. esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
  422. SDHCI_HOST_CONTROL);
  423. return 0;
  424. }
  425. static const struct sdhci_ops sdhci_esdhc_ops = {
  426. .read_l = esdhc_readl_le,
  427. .read_w = esdhc_readw_le,
  428. .write_l = esdhc_writel_le,
  429. .write_w = esdhc_writew_le,
  430. .write_b = esdhc_writeb_le,
  431. .set_clock = esdhc_pltfm_set_clock,
  432. .get_max_clock = esdhc_pltfm_get_max_clock,
  433. .get_min_clock = esdhc_pltfm_get_min_clock,
  434. .get_ro = esdhc_pltfm_get_ro,
  435. .platform_bus_width = esdhc_pltfm_bus_width,
  436. };
  437. static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
  438. .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
  439. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  440. | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
  441. | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  442. .ops = &sdhci_esdhc_ops,
  443. };
  444. #ifdef CONFIG_OF
  445. static int
  446. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  447. struct esdhc_platform_data *boarddata)
  448. {
  449. struct device_node *np = pdev->dev.of_node;
  450. if (!np)
  451. return -ENODEV;
  452. if (of_get_property(np, "non-removable", NULL))
  453. boarddata->cd_type = ESDHC_CD_PERMANENT;
  454. if (of_get_property(np, "fsl,cd-controller", NULL))
  455. boarddata->cd_type = ESDHC_CD_CONTROLLER;
  456. if (of_get_property(np, "fsl,wp-controller", NULL))
  457. boarddata->wp_type = ESDHC_WP_CONTROLLER;
  458. boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  459. if (gpio_is_valid(boarddata->cd_gpio))
  460. boarddata->cd_type = ESDHC_CD_GPIO;
  461. boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  462. if (gpio_is_valid(boarddata->wp_gpio))
  463. boarddata->wp_type = ESDHC_WP_GPIO;
  464. of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
  465. of_property_read_u32(np, "max-frequency", &boarddata->f_max);
  466. return 0;
  467. }
  468. #else
  469. static inline int
  470. sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
  471. struct esdhc_platform_data *boarddata)
  472. {
  473. return -ENODEV;
  474. }
  475. #endif
  476. static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
  477. {
  478. const struct of_device_id *of_id =
  479. of_match_device(imx_esdhc_dt_ids, &pdev->dev);
  480. struct sdhci_pltfm_host *pltfm_host;
  481. struct sdhci_host *host;
  482. struct esdhc_platform_data *boarddata;
  483. int err;
  484. struct pltfm_imx_data *imx_data;
  485. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
  486. if (IS_ERR(host))
  487. return PTR_ERR(host);
  488. pltfm_host = sdhci_priv(host);
  489. imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
  490. if (!imx_data) {
  491. err = -ENOMEM;
  492. goto free_sdhci;
  493. }
  494. if (of_id)
  495. pdev->id_entry = of_id->data;
  496. imx_data->devtype = pdev->id_entry->driver_data;
  497. pltfm_host->priv = imx_data;
  498. imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  499. if (IS_ERR(imx_data->clk_ipg)) {
  500. err = PTR_ERR(imx_data->clk_ipg);
  501. goto free_sdhci;
  502. }
  503. imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  504. if (IS_ERR(imx_data->clk_ahb)) {
  505. err = PTR_ERR(imx_data->clk_ahb);
  506. goto free_sdhci;
  507. }
  508. imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
  509. if (IS_ERR(imx_data->clk_per)) {
  510. err = PTR_ERR(imx_data->clk_per);
  511. goto free_sdhci;
  512. }
  513. pltfm_host->clk = imx_data->clk_per;
  514. clk_prepare_enable(imx_data->clk_per);
  515. clk_prepare_enable(imx_data->clk_ipg);
  516. clk_prepare_enable(imx_data->clk_ahb);
  517. imx_data->pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  518. if (IS_ERR(imx_data->pinctrl)) {
  519. err = PTR_ERR(imx_data->pinctrl);
  520. goto disable_clk;
  521. }
  522. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  523. if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
  524. /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
  525. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
  526. | SDHCI_QUIRK_BROKEN_ADMA;
  527. if (is_imx53_esdhc(imx_data))
  528. imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
  529. /*
  530. * The imx6q ROM code will change the default watermark level setting
  531. * to something insane. Change it back here.
  532. */
  533. if (is_imx6q_usdhc(imx_data))
  534. writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
  535. boarddata = &imx_data->boarddata;
  536. if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
  537. if (!host->mmc->parent->platform_data) {
  538. dev_err(mmc_dev(host->mmc), "no board data!\n");
  539. err = -EINVAL;
  540. goto disable_clk;
  541. }
  542. imx_data->boarddata = *((struct esdhc_platform_data *)
  543. host->mmc->parent->platform_data);
  544. }
  545. /* write_protect */
  546. if (boarddata->wp_type == ESDHC_WP_GPIO) {
  547. err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
  548. if (err) {
  549. dev_err(mmc_dev(host->mmc),
  550. "failed to request write-protect gpio!\n");
  551. goto disable_clk;
  552. }
  553. host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  554. }
  555. /* card_detect */
  556. switch (boarddata->cd_type) {
  557. case ESDHC_CD_GPIO:
  558. err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
  559. if (err) {
  560. dev_err(mmc_dev(host->mmc),
  561. "failed to request card-detect gpio!\n");
  562. goto disable_clk;
  563. }
  564. /* fall through */
  565. case ESDHC_CD_CONTROLLER:
  566. /* we have a working card_detect back */
  567. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  568. break;
  569. case ESDHC_CD_PERMANENT:
  570. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  571. break;
  572. case ESDHC_CD_NONE:
  573. break;
  574. }
  575. switch (boarddata->max_bus_width) {
  576. case 8:
  577. host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  578. break;
  579. case 4:
  580. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  581. break;
  582. case 1:
  583. default:
  584. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  585. break;
  586. }
  587. err = sdhci_add_host(host);
  588. if (err)
  589. goto disable_clk;
  590. return 0;
  591. disable_clk:
  592. clk_disable_unprepare(imx_data->clk_per);
  593. clk_disable_unprepare(imx_data->clk_ipg);
  594. clk_disable_unprepare(imx_data->clk_ahb);
  595. free_sdhci:
  596. sdhci_pltfm_free(pdev);
  597. return err;
  598. }
  599. static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
  600. {
  601. struct sdhci_host *host = platform_get_drvdata(pdev);
  602. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  603. struct pltfm_imx_data *imx_data = pltfm_host->priv;
  604. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  605. sdhci_remove_host(host, dead);
  606. clk_disable_unprepare(imx_data->clk_per);
  607. clk_disable_unprepare(imx_data->clk_ipg);
  608. clk_disable_unprepare(imx_data->clk_ahb);
  609. sdhci_pltfm_free(pdev);
  610. return 0;
  611. }
  612. static struct platform_driver sdhci_esdhc_imx_driver = {
  613. .driver = {
  614. .name = "sdhci-esdhc-imx",
  615. .owner = THIS_MODULE,
  616. .of_match_table = imx_esdhc_dt_ids,
  617. .pm = SDHCI_PLTFM_PMOPS,
  618. },
  619. .id_table = imx_esdhc_devtype,
  620. .probe = sdhci_esdhc_imx_probe,
  621. .remove = sdhci_esdhc_imx_remove,
  622. };
  623. module_platform_driver(sdhci_esdhc_imx_driver);
  624. MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
  625. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  626. MODULE_LICENSE("GPL v2");