sdhci-of-esdhc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Freescale eSDHC controller driver.
  3. *
  4. * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
  5. * Copyright (c) 2009 MontaVista Software, Inc.
  6. *
  7. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  8. * Anton Vorontsov <avorontsov@ru.mvista.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/delay.h>
  20. #include <linux/module.h>
  21. #include <linux/sys_soc.h>
  22. #include <linux/clk.h>
  23. #include <linux/ktime.h>
  24. #include <linux/mmc/host.h>
  25. #include "sdhci-pltfm.h"
  26. #include "sdhci-esdhc.h"
  27. #define VENDOR_V_22 0x12
  28. #define VENDOR_V_23 0x13
  29. struct sdhci_esdhc {
  30. u8 vendor_ver;
  31. u8 spec_ver;
  32. bool quirk_incorrect_hostver;
  33. unsigned int peripheral_clock;
  34. };
  35. /**
  36. * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
  37. * to make it compatible with SD spec.
  38. *
  39. * @host: pointer to sdhci_host
  40. * @spec_reg: SD spec register address
  41. * @value: 32bit eSDHC register value on spec_reg address
  42. *
  43. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  44. * registers are 32 bits. There are differences in register size, register
  45. * address, register function, bit position and function between eSDHC spec
  46. * and SD spec.
  47. *
  48. * Return a fixed up register value
  49. */
  50. static u32 esdhc_readl_fixup(struct sdhci_host *host,
  51. int spec_reg, u32 value)
  52. {
  53. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  54. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  55. u32 ret;
  56. /*
  57. * The bit of ADMA flag in eSDHC is not compatible with standard
  58. * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
  59. * supported by eSDHC.
  60. * And for many FSL eSDHC controller, the reset value of field
  61. * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
  62. * only these vendor version is greater than 2.2/0x12 support ADMA.
  63. */
  64. if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
  65. if (esdhc->vendor_ver > VENDOR_V_22) {
  66. ret = value | SDHCI_CAN_DO_ADMA2;
  67. return ret;
  68. }
  69. }
  70. /*
  71. * The DAT[3:0] line signal levels and the CMD line signal level are
  72. * not compatible with standard SDHC register. The line signal levels
  73. * DAT[7:0] are at bits 31:24 and the command line signal level is at
  74. * bit 23. All other bits are the same as in the standard SDHC
  75. * register.
  76. */
  77. if (spec_reg == SDHCI_PRESENT_STATE) {
  78. ret = value & 0x000fffff;
  79. ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
  80. ret |= (value << 1) & SDHCI_CMD_LVL;
  81. return ret;
  82. }
  83. /*
  84. * DTS properties of mmc host are used to enable each speed mode
  85. * according to soc and board capability. So clean up
  86. * SDR50/SDR104/DDR50 support bits here.
  87. */
  88. if (spec_reg == SDHCI_CAPABILITIES_1) {
  89. ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
  90. SDHCI_SUPPORT_DDR50);
  91. return ret;
  92. }
  93. ret = value;
  94. return ret;
  95. }
  96. static u16 esdhc_readw_fixup(struct sdhci_host *host,
  97. int spec_reg, u32 value)
  98. {
  99. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  100. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  101. u16 ret;
  102. int shift = (spec_reg & 0x2) * 8;
  103. if (spec_reg == SDHCI_HOST_VERSION)
  104. ret = value & 0xffff;
  105. else
  106. ret = (value >> shift) & 0xffff;
  107. /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
  108. * vendor version and spec version information.
  109. */
  110. if ((spec_reg == SDHCI_HOST_VERSION) &&
  111. (esdhc->quirk_incorrect_hostver))
  112. ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
  113. return ret;
  114. }
  115. static u8 esdhc_readb_fixup(struct sdhci_host *host,
  116. int spec_reg, u32 value)
  117. {
  118. u8 ret;
  119. u8 dma_bits;
  120. int shift = (spec_reg & 0x3) * 8;
  121. ret = (value >> shift) & 0xff;
  122. /*
  123. * "DMA select" locates at offset 0x28 in SD specification, but on
  124. * P5020 or P3041, it locates at 0x29.
  125. */
  126. if (spec_reg == SDHCI_HOST_CONTROL) {
  127. /* DMA select is 22,23 bits in Protocol Control Register */
  128. dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
  129. /* fixup the result */
  130. ret &= ~SDHCI_CTRL_DMA_MASK;
  131. ret |= dma_bits;
  132. }
  133. return ret;
  134. }
  135. /**
  136. * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
  137. * written into eSDHC register.
  138. *
  139. * @host: pointer to sdhci_host
  140. * @spec_reg: SD spec register address
  141. * @value: 8/16/32bit SD spec register value that would be written
  142. * @old_value: 32bit eSDHC register value on spec_reg address
  143. *
  144. * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
  145. * registers are 32 bits. There are differences in register size, register
  146. * address, register function, bit position and function between eSDHC spec
  147. * and SD spec.
  148. *
  149. * Return a fixed up register value
  150. */
  151. static u32 esdhc_writel_fixup(struct sdhci_host *host,
  152. int spec_reg, u32 value, u32 old_value)
  153. {
  154. u32 ret;
  155. /*
  156. * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
  157. * when SYSCTL[RSTD] is set for some special operations.
  158. * No any impact on other operation.
  159. */
  160. if (spec_reg == SDHCI_INT_ENABLE)
  161. ret = value | SDHCI_INT_BLK_GAP;
  162. else
  163. ret = value;
  164. return ret;
  165. }
  166. static u32 esdhc_writew_fixup(struct sdhci_host *host,
  167. int spec_reg, u16 value, u32 old_value)
  168. {
  169. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  170. int shift = (spec_reg & 0x2) * 8;
  171. u32 ret;
  172. switch (spec_reg) {
  173. case SDHCI_TRANSFER_MODE:
  174. /*
  175. * Postpone this write, we must do it together with a
  176. * command write that is down below. Return old value.
  177. */
  178. pltfm_host->xfer_mode_shadow = value;
  179. return old_value;
  180. case SDHCI_COMMAND:
  181. ret = (value << 16) | pltfm_host->xfer_mode_shadow;
  182. return ret;
  183. }
  184. ret = old_value & (~(0xffff << shift));
  185. ret |= (value << shift);
  186. if (spec_reg == SDHCI_BLOCK_SIZE) {
  187. /*
  188. * Two last DMA bits are reserved, and first one is used for
  189. * non-standard blksz of 4096 bytes that we don't support
  190. * yet. So clear the DMA boundary bits.
  191. */
  192. ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
  193. }
  194. return ret;
  195. }
  196. static u32 esdhc_writeb_fixup(struct sdhci_host *host,
  197. int spec_reg, u8 value, u32 old_value)
  198. {
  199. u32 ret;
  200. u32 dma_bits;
  201. u8 tmp;
  202. int shift = (spec_reg & 0x3) * 8;
  203. /*
  204. * eSDHC doesn't have a standard power control register, so we do
  205. * nothing here to avoid incorrect operation.
  206. */
  207. if (spec_reg == SDHCI_POWER_CONTROL)
  208. return old_value;
  209. /*
  210. * "DMA select" location is offset 0x28 in SD specification, but on
  211. * P5020 or P3041, it's located at 0x29.
  212. */
  213. if (spec_reg == SDHCI_HOST_CONTROL) {
  214. /*
  215. * If host control register is not standard, exit
  216. * this function
  217. */
  218. if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
  219. return old_value;
  220. /* DMA select is 22,23 bits in Protocol Control Register */
  221. dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
  222. ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
  223. tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
  224. (old_value & SDHCI_CTRL_DMA_MASK);
  225. ret = (ret & (~0xff)) | tmp;
  226. /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
  227. ret &= ~ESDHC_HOST_CONTROL_RES;
  228. return ret;
  229. }
  230. ret = (old_value & (~(0xff << shift))) | (value << shift);
  231. return ret;
  232. }
  233. static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
  234. {
  235. u32 ret;
  236. u32 value;
  237. if (reg == SDHCI_CAPABILITIES_1)
  238. value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
  239. else
  240. value = ioread32be(host->ioaddr + reg);
  241. ret = esdhc_readl_fixup(host, reg, value);
  242. return ret;
  243. }
  244. static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
  245. {
  246. u32 ret;
  247. u32 value;
  248. if (reg == SDHCI_CAPABILITIES_1)
  249. value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
  250. else
  251. value = ioread32(host->ioaddr + reg);
  252. ret = esdhc_readl_fixup(host, reg, value);
  253. return ret;
  254. }
  255. static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
  256. {
  257. u16 ret;
  258. u32 value;
  259. int base = reg & ~0x3;
  260. value = ioread32be(host->ioaddr + base);
  261. ret = esdhc_readw_fixup(host, reg, value);
  262. return ret;
  263. }
  264. static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
  265. {
  266. u16 ret;
  267. u32 value;
  268. int base = reg & ~0x3;
  269. value = ioread32(host->ioaddr + base);
  270. ret = esdhc_readw_fixup(host, reg, value);
  271. return ret;
  272. }
  273. static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
  274. {
  275. u8 ret;
  276. u32 value;
  277. int base = reg & ~0x3;
  278. value = ioread32be(host->ioaddr + base);
  279. ret = esdhc_readb_fixup(host, reg, value);
  280. return ret;
  281. }
  282. static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
  283. {
  284. u8 ret;
  285. u32 value;
  286. int base = reg & ~0x3;
  287. value = ioread32(host->ioaddr + base);
  288. ret = esdhc_readb_fixup(host, reg, value);
  289. return ret;
  290. }
  291. static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
  292. {
  293. u32 value;
  294. value = esdhc_writel_fixup(host, reg, val, 0);
  295. iowrite32be(value, host->ioaddr + reg);
  296. }
  297. static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
  298. {
  299. u32 value;
  300. value = esdhc_writel_fixup(host, reg, val, 0);
  301. iowrite32(value, host->ioaddr + reg);
  302. }
  303. static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
  304. {
  305. int base = reg & ~0x3;
  306. u32 value;
  307. u32 ret;
  308. value = ioread32be(host->ioaddr + base);
  309. ret = esdhc_writew_fixup(host, reg, val, value);
  310. if (reg != SDHCI_TRANSFER_MODE)
  311. iowrite32be(ret, host->ioaddr + base);
  312. }
  313. static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
  314. {
  315. int base = reg & ~0x3;
  316. u32 value;
  317. u32 ret;
  318. value = ioread32(host->ioaddr + base);
  319. ret = esdhc_writew_fixup(host, reg, val, value);
  320. if (reg != SDHCI_TRANSFER_MODE)
  321. iowrite32(ret, host->ioaddr + base);
  322. }
  323. static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
  324. {
  325. int base = reg & ~0x3;
  326. u32 value;
  327. u32 ret;
  328. value = ioread32be(host->ioaddr + base);
  329. ret = esdhc_writeb_fixup(host, reg, val, value);
  330. iowrite32be(ret, host->ioaddr + base);
  331. }
  332. static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
  333. {
  334. int base = reg & ~0x3;
  335. u32 value;
  336. u32 ret;
  337. value = ioread32(host->ioaddr + base);
  338. ret = esdhc_writeb_fixup(host, reg, val, value);
  339. iowrite32(ret, host->ioaddr + base);
  340. }
  341. /*
  342. * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
  343. * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
  344. * and Block Gap Event(IRQSTAT[BGE]) are also set.
  345. * For Continue, apply soft reset for data(SYSCTL[RSTD]);
  346. * and re-issue the entire read transaction from beginning.
  347. */
  348. static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
  349. {
  350. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  351. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  352. bool applicable;
  353. dma_addr_t dmastart;
  354. dma_addr_t dmanow;
  355. applicable = (intmask & SDHCI_INT_DATA_END) &&
  356. (intmask & SDHCI_INT_BLK_GAP) &&
  357. (esdhc->vendor_ver == VENDOR_V_23);
  358. if (!applicable)
  359. return;
  360. host->data->error = 0;
  361. dmastart = sg_dma_address(host->data->sg);
  362. dmanow = dmastart + host->data->bytes_xfered;
  363. /*
  364. * Force update to the next DMA block boundary.
  365. */
  366. dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  367. SDHCI_DEFAULT_BOUNDARY_SIZE;
  368. host->data->bytes_xfered = dmanow - dmastart;
  369. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  370. }
  371. static int esdhc_of_enable_dma(struct sdhci_host *host)
  372. {
  373. u32 value;
  374. value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  375. value |= ESDHC_DMA_SNOOP;
  376. sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
  377. return 0;
  378. }
  379. static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
  380. {
  381. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  382. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  383. if (esdhc->peripheral_clock)
  384. return esdhc->peripheral_clock;
  385. else
  386. return pltfm_host->clock;
  387. }
  388. static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
  389. {
  390. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  391. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  392. unsigned int clock;
  393. if (esdhc->peripheral_clock)
  394. clock = esdhc->peripheral_clock;
  395. else
  396. clock = pltfm_host->clock;
  397. return clock / 256 / 16;
  398. }
  399. static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
  400. {
  401. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  402. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  403. int pre_div = 1;
  404. int div = 1;
  405. ktime_t timeout;
  406. u32 temp;
  407. host->mmc->actual_clock = 0;
  408. if (clock == 0)
  409. return;
  410. /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
  411. if (esdhc->vendor_ver < VENDOR_V_23)
  412. pre_div = 2;
  413. /*
  414. * Limit SD clock to 167MHz for ls1046a according to its datasheet
  415. */
  416. if (clock > 167000000 &&
  417. of_find_compatible_node(NULL, NULL, "fsl,ls1046a-esdhc"))
  418. clock = 167000000;
  419. /*
  420. * Limit SD clock to 125MHz for ls1012a according to its datasheet
  421. */
  422. if (clock > 125000000 &&
  423. of_find_compatible_node(NULL, NULL, "fsl,ls1012a-esdhc"))
  424. clock = 125000000;
  425. /* Workaround to reduce the clock frequency for p1010 esdhc */
  426. if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
  427. if (clock > 20000000)
  428. clock -= 5000000;
  429. if (clock > 40000000)
  430. clock -= 5000000;
  431. }
  432. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  433. temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
  434. ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
  435. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  436. while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
  437. pre_div *= 2;
  438. while (host->max_clk / pre_div / div > clock && div < 16)
  439. div++;
  440. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  441. clock, host->max_clk / pre_div / div);
  442. host->mmc->actual_clock = host->max_clk / pre_div / div;
  443. pre_div >>= 1;
  444. div--;
  445. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  446. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  447. | (div << ESDHC_DIVIDER_SHIFT)
  448. | (pre_div << ESDHC_PREDIV_SHIFT));
  449. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  450. /* Wait max 20 ms */
  451. timeout = ktime_add_ms(ktime_get(), 20);
  452. while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) {
  453. if (ktime_after(ktime_get(), timeout)) {
  454. pr_err("%s: Internal clock never stabilised.\n",
  455. mmc_hostname(host->mmc));
  456. return;
  457. }
  458. udelay(10);
  459. }
  460. temp |= ESDHC_CLOCK_SDCLKEN;
  461. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  462. }
  463. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  464. {
  465. u32 ctrl;
  466. ctrl = sdhci_readl(host, ESDHC_PROCTL);
  467. ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
  468. switch (width) {
  469. case MMC_BUS_WIDTH_8:
  470. ctrl |= ESDHC_CTRL_8BITBUS;
  471. break;
  472. case MMC_BUS_WIDTH_4:
  473. ctrl |= ESDHC_CTRL_4BITBUS;
  474. break;
  475. default:
  476. break;
  477. }
  478. sdhci_writel(host, ctrl, ESDHC_PROCTL);
  479. }
  480. static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
  481. {
  482. u32 val;
  483. ktime_t timeout;
  484. val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  485. if (enable)
  486. val |= ESDHC_CLOCK_SDCLKEN;
  487. else
  488. val &= ~ESDHC_CLOCK_SDCLKEN;
  489. sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
  490. /* Wait max 20 ms */
  491. timeout = ktime_add_ms(ktime_get(), 20);
  492. val = ESDHC_CLOCK_STABLE;
  493. while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) {
  494. if (ktime_after(ktime_get(), timeout)) {
  495. pr_err("%s: Internal clock never stabilised.\n",
  496. mmc_hostname(host->mmc));
  497. break;
  498. }
  499. udelay(10);
  500. }
  501. }
  502. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  503. {
  504. sdhci_reset(host, mask);
  505. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  506. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  507. }
  508. /* The SCFG, Supplemental Configuration Unit, provides SoC specific
  509. * configuration and status registers for the device. There is a
  510. * SDHC IO VSEL control register on SCFG for some platforms. It's
  511. * used to support SDHC IO voltage switching.
  512. */
  513. static const struct of_device_id scfg_device_ids[] = {
  514. { .compatible = "fsl,t1040-scfg", },
  515. { .compatible = "fsl,ls1012a-scfg", },
  516. { .compatible = "fsl,ls1046a-scfg", },
  517. {}
  518. };
  519. /* SDHC IO VSEL control register definition */
  520. #define SCFG_SDHCIOVSELCR 0x408
  521. #define SDHCIOVSELCR_TGLEN 0x80000000
  522. #define SDHCIOVSELCR_VSELVAL 0x60000000
  523. #define SDHCIOVSELCR_SDHC_VS 0x00000001
  524. static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
  525. struct mmc_ios *ios)
  526. {
  527. struct sdhci_host *host = mmc_priv(mmc);
  528. struct device_node *scfg_node;
  529. void __iomem *scfg_base = NULL;
  530. u32 sdhciovselcr;
  531. u32 val;
  532. /*
  533. * Signal Voltage Switching is only applicable for Host Controllers
  534. * v3.00 and above.
  535. */
  536. if (host->version < SDHCI_SPEC_300)
  537. return 0;
  538. val = sdhci_readl(host, ESDHC_PROCTL);
  539. switch (ios->signal_voltage) {
  540. case MMC_SIGNAL_VOLTAGE_330:
  541. val &= ~ESDHC_VOLT_SEL;
  542. sdhci_writel(host, val, ESDHC_PROCTL);
  543. return 0;
  544. case MMC_SIGNAL_VOLTAGE_180:
  545. scfg_node = of_find_matching_node(NULL, scfg_device_ids);
  546. if (scfg_node)
  547. scfg_base = of_iomap(scfg_node, 0);
  548. if (scfg_base) {
  549. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  550. SDHCIOVSELCR_VSELVAL;
  551. iowrite32be(sdhciovselcr,
  552. scfg_base + SCFG_SDHCIOVSELCR);
  553. val |= ESDHC_VOLT_SEL;
  554. sdhci_writel(host, val, ESDHC_PROCTL);
  555. mdelay(5);
  556. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  557. SDHCIOVSELCR_SDHC_VS;
  558. iowrite32be(sdhciovselcr,
  559. scfg_base + SCFG_SDHCIOVSELCR);
  560. iounmap(scfg_base);
  561. } else {
  562. val |= ESDHC_VOLT_SEL;
  563. sdhci_writel(host, val, ESDHC_PROCTL);
  564. }
  565. return 0;
  566. default:
  567. return 0;
  568. }
  569. }
  570. static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  571. {
  572. struct sdhci_host *host = mmc_priv(mmc);
  573. u32 val;
  574. /* Use tuning block for tuning procedure */
  575. esdhc_clock_enable(host, false);
  576. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  577. val |= ESDHC_FLUSH_ASYNC_FIFO;
  578. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  579. val = sdhci_readl(host, ESDHC_TBCTL);
  580. val |= ESDHC_TB_EN;
  581. sdhci_writel(host, val, ESDHC_TBCTL);
  582. esdhc_clock_enable(host, true);
  583. return sdhci_execute_tuning(mmc, opcode);
  584. }
  585. #ifdef CONFIG_PM_SLEEP
  586. static u32 esdhc_proctl;
  587. static int esdhc_of_suspend(struct device *dev)
  588. {
  589. struct sdhci_host *host = dev_get_drvdata(dev);
  590. esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
  591. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  592. mmc_retune_needed(host->mmc);
  593. return sdhci_suspend_host(host);
  594. }
  595. static int esdhc_of_resume(struct device *dev)
  596. {
  597. struct sdhci_host *host = dev_get_drvdata(dev);
  598. int ret = sdhci_resume_host(host);
  599. if (ret == 0) {
  600. /* Isn't this already done by sdhci_resume_host() ? --rmk */
  601. esdhc_of_enable_dma(host);
  602. sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  603. }
  604. return ret;
  605. }
  606. #endif
  607. static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
  608. esdhc_of_suspend,
  609. esdhc_of_resume);
  610. static const struct sdhci_ops sdhci_esdhc_be_ops = {
  611. .read_l = esdhc_be_readl,
  612. .read_w = esdhc_be_readw,
  613. .read_b = esdhc_be_readb,
  614. .write_l = esdhc_be_writel,
  615. .write_w = esdhc_be_writew,
  616. .write_b = esdhc_be_writeb,
  617. .set_clock = esdhc_of_set_clock,
  618. .enable_dma = esdhc_of_enable_dma,
  619. .get_max_clock = esdhc_of_get_max_clock,
  620. .get_min_clock = esdhc_of_get_min_clock,
  621. .adma_workaround = esdhc_of_adma_workaround,
  622. .set_bus_width = esdhc_pltfm_set_bus_width,
  623. .reset = esdhc_reset,
  624. .set_uhs_signaling = sdhci_set_uhs_signaling,
  625. };
  626. static const struct sdhci_ops sdhci_esdhc_le_ops = {
  627. .read_l = esdhc_le_readl,
  628. .read_w = esdhc_le_readw,
  629. .read_b = esdhc_le_readb,
  630. .write_l = esdhc_le_writel,
  631. .write_w = esdhc_le_writew,
  632. .write_b = esdhc_le_writeb,
  633. .set_clock = esdhc_of_set_clock,
  634. .enable_dma = esdhc_of_enable_dma,
  635. .get_max_clock = esdhc_of_get_max_clock,
  636. .get_min_clock = esdhc_of_get_min_clock,
  637. .adma_workaround = esdhc_of_adma_workaround,
  638. .set_bus_width = esdhc_pltfm_set_bus_width,
  639. .reset = esdhc_reset,
  640. .set_uhs_signaling = sdhci_set_uhs_signaling,
  641. };
  642. static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
  643. .quirks = ESDHC_DEFAULT_QUIRKS |
  644. #ifdef CONFIG_PPC
  645. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  646. #endif
  647. SDHCI_QUIRK_NO_CARD_NO_RESET |
  648. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  649. .ops = &sdhci_esdhc_be_ops,
  650. };
  651. static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
  652. .quirks = ESDHC_DEFAULT_QUIRKS |
  653. SDHCI_QUIRK_NO_CARD_NO_RESET |
  654. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  655. .ops = &sdhci_esdhc_le_ops,
  656. };
  657. static struct soc_device_attribute soc_incorrect_hostver[] = {
  658. { .family = "QorIQ T4240", .revision = "1.0", },
  659. { .family = "QorIQ T4240", .revision = "2.0", },
  660. { },
  661. };
  662. static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
  663. {
  664. struct sdhci_pltfm_host *pltfm_host;
  665. struct sdhci_esdhc *esdhc;
  666. struct device_node *np;
  667. struct clk *clk;
  668. u32 val;
  669. u16 host_ver;
  670. pltfm_host = sdhci_priv(host);
  671. esdhc = sdhci_pltfm_priv(pltfm_host);
  672. host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
  673. esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
  674. SDHCI_VENDOR_VER_SHIFT;
  675. esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
  676. if (soc_device_match(soc_incorrect_hostver))
  677. esdhc->quirk_incorrect_hostver = true;
  678. else
  679. esdhc->quirk_incorrect_hostver = false;
  680. np = pdev->dev.of_node;
  681. clk = of_clk_get(np, 0);
  682. if (!IS_ERR(clk)) {
  683. /*
  684. * esdhc->peripheral_clock would be assigned with a value
  685. * which is eSDHC base clock when use periperal clock.
  686. * For ls1046a, the clock value got by common clk API is
  687. * peripheral clock while the eSDHC base clock is 1/2
  688. * peripheral clock.
  689. */
  690. if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
  691. esdhc->peripheral_clock = clk_get_rate(clk) / 2;
  692. else
  693. esdhc->peripheral_clock = clk_get_rate(clk);
  694. clk_put(clk);
  695. }
  696. if (esdhc->peripheral_clock) {
  697. esdhc_clock_enable(host, false);
  698. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  699. val |= ESDHC_PERIPHERAL_CLK_SEL;
  700. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  701. esdhc_clock_enable(host, true);
  702. }
  703. }
  704. static int sdhci_esdhc_probe(struct platform_device *pdev)
  705. {
  706. struct sdhci_host *host;
  707. struct device_node *np;
  708. struct sdhci_pltfm_host *pltfm_host;
  709. struct sdhci_esdhc *esdhc;
  710. int ret;
  711. np = pdev->dev.of_node;
  712. if (of_property_read_bool(np, "little-endian"))
  713. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
  714. sizeof(struct sdhci_esdhc));
  715. else
  716. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
  717. sizeof(struct sdhci_esdhc));
  718. if (IS_ERR(host))
  719. return PTR_ERR(host);
  720. host->mmc_host_ops.start_signal_voltage_switch =
  721. esdhc_signal_voltage_switch;
  722. host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
  723. host->tuning_delay = 1;
  724. esdhc_init(pdev, host);
  725. sdhci_get_of_property(pdev);
  726. pltfm_host = sdhci_priv(host);
  727. esdhc = sdhci_pltfm_priv(pltfm_host);
  728. if (esdhc->vendor_ver == VENDOR_V_22)
  729. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  730. if (esdhc->vendor_ver > VENDOR_V_22)
  731. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  732. if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
  733. of_device_is_compatible(np, "fsl,p5020-esdhc") ||
  734. of_device_is_compatible(np, "fsl,p4080-esdhc") ||
  735. of_device_is_compatible(np, "fsl,p1020-esdhc") ||
  736. of_device_is_compatible(np, "fsl,t1040-esdhc"))
  737. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  738. if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
  739. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  740. if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
  741. /*
  742. * Freescale messed up with P2020 as it has a non-standard
  743. * host control register
  744. */
  745. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
  746. }
  747. /* call to generic mmc_of_parse to support additional capabilities */
  748. ret = mmc_of_parse(host->mmc);
  749. if (ret)
  750. goto err;
  751. mmc_of_parse_voltage(np, &host->ocr_mask);
  752. ret = sdhci_add_host(host);
  753. if (ret)
  754. goto err;
  755. return 0;
  756. err:
  757. sdhci_pltfm_free(pdev);
  758. return ret;
  759. }
  760. static const struct of_device_id sdhci_esdhc_of_match[] = {
  761. { .compatible = "fsl,mpc8379-esdhc" },
  762. { .compatible = "fsl,mpc8536-esdhc" },
  763. { .compatible = "fsl,esdhc" },
  764. { }
  765. };
  766. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  767. static struct platform_driver sdhci_esdhc_driver = {
  768. .driver = {
  769. .name = "sdhci-esdhc",
  770. .of_match_table = sdhci_esdhc_of_match,
  771. .pm = &esdhc_of_dev_pm_ops,
  772. },
  773. .probe = sdhci_esdhc_probe,
  774. .remove = sdhci_pltfm_unregister,
  775. };
  776. module_platform_driver(sdhci_esdhc_driver);
  777. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  778. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  779. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  780. MODULE_LICENSE("GPL v2");