sdhci-of-esdhc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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_clock_enable(struct sdhci_host *host, bool enable)
  400. {
  401. u32 val;
  402. ktime_t timeout;
  403. val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  404. if (enable)
  405. val |= ESDHC_CLOCK_SDCLKEN;
  406. else
  407. val &= ~ESDHC_CLOCK_SDCLKEN;
  408. sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
  409. /* Wait max 20 ms */
  410. timeout = ktime_add_ms(ktime_get(), 20);
  411. val = ESDHC_CLOCK_STABLE;
  412. while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) {
  413. if (ktime_after(ktime_get(), timeout)) {
  414. pr_err("%s: Internal clock never stabilised.\n",
  415. mmc_hostname(host->mmc));
  416. break;
  417. }
  418. udelay(10);
  419. }
  420. }
  421. static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
  422. {
  423. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  424. struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
  425. int pre_div = 1;
  426. int div = 1;
  427. ktime_t timeout;
  428. u32 temp;
  429. host->mmc->actual_clock = 0;
  430. if (clock == 0) {
  431. esdhc_clock_enable(host, false);
  432. return;
  433. }
  434. /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
  435. if (esdhc->vendor_ver < VENDOR_V_23)
  436. pre_div = 2;
  437. /*
  438. * Limit SD clock to 167MHz for ls1046a according to its datasheet
  439. */
  440. if (clock > 167000000 &&
  441. of_find_compatible_node(NULL, NULL, "fsl,ls1046a-esdhc"))
  442. clock = 167000000;
  443. /*
  444. * Limit SD clock to 125MHz for ls1012a according to its datasheet
  445. */
  446. if (clock > 125000000 &&
  447. of_find_compatible_node(NULL, NULL, "fsl,ls1012a-esdhc"))
  448. clock = 125000000;
  449. /* Workaround to reduce the clock frequency for p1010 esdhc */
  450. if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
  451. if (clock > 20000000)
  452. clock -= 5000000;
  453. if (clock > 40000000)
  454. clock -= 5000000;
  455. }
  456. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  457. temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
  458. ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
  459. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  460. while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
  461. pre_div *= 2;
  462. while (host->max_clk / pre_div / div > clock && div < 16)
  463. div++;
  464. dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
  465. clock, host->max_clk / pre_div / div);
  466. host->mmc->actual_clock = host->max_clk / pre_div / div;
  467. pre_div >>= 1;
  468. div--;
  469. temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
  470. temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
  471. | (div << ESDHC_DIVIDER_SHIFT)
  472. | (pre_div << ESDHC_PREDIV_SHIFT));
  473. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  474. /* Wait max 20 ms */
  475. timeout = ktime_add_ms(ktime_get(), 20);
  476. while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) {
  477. if (ktime_after(ktime_get(), timeout)) {
  478. pr_err("%s: Internal clock never stabilised.\n",
  479. mmc_hostname(host->mmc));
  480. return;
  481. }
  482. udelay(10);
  483. }
  484. temp |= ESDHC_CLOCK_SDCLKEN;
  485. sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
  486. }
  487. static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
  488. {
  489. u32 ctrl;
  490. ctrl = sdhci_readl(host, ESDHC_PROCTL);
  491. ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
  492. switch (width) {
  493. case MMC_BUS_WIDTH_8:
  494. ctrl |= ESDHC_CTRL_8BITBUS;
  495. break;
  496. case MMC_BUS_WIDTH_4:
  497. ctrl |= ESDHC_CTRL_4BITBUS;
  498. break;
  499. default:
  500. break;
  501. }
  502. sdhci_writel(host, ctrl, ESDHC_PROCTL);
  503. }
  504. static void esdhc_reset(struct sdhci_host *host, u8 mask)
  505. {
  506. u32 val;
  507. sdhci_reset(host, mask);
  508. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  509. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  510. if (mask & SDHCI_RESET_ALL) {
  511. val = sdhci_readl(host, ESDHC_TBCTL);
  512. val &= ~ESDHC_TB_EN;
  513. sdhci_writel(host, val, ESDHC_TBCTL);
  514. }
  515. }
  516. /* The SCFG, Supplemental Configuration Unit, provides SoC specific
  517. * configuration and status registers for the device. There is a
  518. * SDHC IO VSEL control register on SCFG for some platforms. It's
  519. * used to support SDHC IO voltage switching.
  520. */
  521. static const struct of_device_id scfg_device_ids[] = {
  522. { .compatible = "fsl,t1040-scfg", },
  523. { .compatible = "fsl,ls1012a-scfg", },
  524. { .compatible = "fsl,ls1046a-scfg", },
  525. {}
  526. };
  527. /* SDHC IO VSEL control register definition */
  528. #define SCFG_SDHCIOVSELCR 0x408
  529. #define SDHCIOVSELCR_TGLEN 0x80000000
  530. #define SDHCIOVSELCR_VSELVAL 0x60000000
  531. #define SDHCIOVSELCR_SDHC_VS 0x00000001
  532. static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
  533. struct mmc_ios *ios)
  534. {
  535. struct sdhci_host *host = mmc_priv(mmc);
  536. struct device_node *scfg_node;
  537. void __iomem *scfg_base = NULL;
  538. u32 sdhciovselcr;
  539. u32 val;
  540. /*
  541. * Signal Voltage Switching is only applicable for Host Controllers
  542. * v3.00 and above.
  543. */
  544. if (host->version < SDHCI_SPEC_300)
  545. return 0;
  546. val = sdhci_readl(host, ESDHC_PROCTL);
  547. switch (ios->signal_voltage) {
  548. case MMC_SIGNAL_VOLTAGE_330:
  549. val &= ~ESDHC_VOLT_SEL;
  550. sdhci_writel(host, val, ESDHC_PROCTL);
  551. return 0;
  552. case MMC_SIGNAL_VOLTAGE_180:
  553. scfg_node = of_find_matching_node(NULL, scfg_device_ids);
  554. if (scfg_node)
  555. scfg_base = of_iomap(scfg_node, 0);
  556. if (scfg_base) {
  557. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  558. SDHCIOVSELCR_VSELVAL;
  559. iowrite32be(sdhciovselcr,
  560. scfg_base + SCFG_SDHCIOVSELCR);
  561. val |= ESDHC_VOLT_SEL;
  562. sdhci_writel(host, val, ESDHC_PROCTL);
  563. mdelay(5);
  564. sdhciovselcr = SDHCIOVSELCR_TGLEN |
  565. SDHCIOVSELCR_SDHC_VS;
  566. iowrite32be(sdhciovselcr,
  567. scfg_base + SCFG_SDHCIOVSELCR);
  568. iounmap(scfg_base);
  569. } else {
  570. val |= ESDHC_VOLT_SEL;
  571. sdhci_writel(host, val, ESDHC_PROCTL);
  572. }
  573. return 0;
  574. default:
  575. return 0;
  576. }
  577. }
  578. static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  579. {
  580. struct sdhci_host *host = mmc_priv(mmc);
  581. u32 val;
  582. /* Use tuning block for tuning procedure */
  583. esdhc_clock_enable(host, false);
  584. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  585. val |= ESDHC_FLUSH_ASYNC_FIFO;
  586. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  587. val = sdhci_readl(host, ESDHC_TBCTL);
  588. val |= ESDHC_TB_EN;
  589. sdhci_writel(host, val, ESDHC_TBCTL);
  590. esdhc_clock_enable(host, true);
  591. return sdhci_execute_tuning(mmc, opcode);
  592. }
  593. #ifdef CONFIG_PM_SLEEP
  594. static u32 esdhc_proctl;
  595. static int esdhc_of_suspend(struct device *dev)
  596. {
  597. struct sdhci_host *host = dev_get_drvdata(dev);
  598. esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
  599. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  600. mmc_retune_needed(host->mmc);
  601. return sdhci_suspend_host(host);
  602. }
  603. static int esdhc_of_resume(struct device *dev)
  604. {
  605. struct sdhci_host *host = dev_get_drvdata(dev);
  606. int ret = sdhci_resume_host(host);
  607. if (ret == 0) {
  608. /* Isn't this already done by sdhci_resume_host() ? --rmk */
  609. esdhc_of_enable_dma(host);
  610. sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
  611. }
  612. return ret;
  613. }
  614. #endif
  615. static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
  616. esdhc_of_suspend,
  617. esdhc_of_resume);
  618. static const struct sdhci_ops sdhci_esdhc_be_ops = {
  619. .read_l = esdhc_be_readl,
  620. .read_w = esdhc_be_readw,
  621. .read_b = esdhc_be_readb,
  622. .write_l = esdhc_be_writel,
  623. .write_w = esdhc_be_writew,
  624. .write_b = esdhc_be_writeb,
  625. .set_clock = esdhc_of_set_clock,
  626. .enable_dma = esdhc_of_enable_dma,
  627. .get_max_clock = esdhc_of_get_max_clock,
  628. .get_min_clock = esdhc_of_get_min_clock,
  629. .adma_workaround = esdhc_of_adma_workaround,
  630. .set_bus_width = esdhc_pltfm_set_bus_width,
  631. .reset = esdhc_reset,
  632. .set_uhs_signaling = sdhci_set_uhs_signaling,
  633. };
  634. static const struct sdhci_ops sdhci_esdhc_le_ops = {
  635. .read_l = esdhc_le_readl,
  636. .read_w = esdhc_le_readw,
  637. .read_b = esdhc_le_readb,
  638. .write_l = esdhc_le_writel,
  639. .write_w = esdhc_le_writew,
  640. .write_b = esdhc_le_writeb,
  641. .set_clock = esdhc_of_set_clock,
  642. .enable_dma = esdhc_of_enable_dma,
  643. .get_max_clock = esdhc_of_get_max_clock,
  644. .get_min_clock = esdhc_of_get_min_clock,
  645. .adma_workaround = esdhc_of_adma_workaround,
  646. .set_bus_width = esdhc_pltfm_set_bus_width,
  647. .reset = esdhc_reset,
  648. .set_uhs_signaling = sdhci_set_uhs_signaling,
  649. };
  650. static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
  651. .quirks = ESDHC_DEFAULT_QUIRKS |
  652. #ifdef CONFIG_PPC
  653. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  654. #endif
  655. SDHCI_QUIRK_NO_CARD_NO_RESET |
  656. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  657. .ops = &sdhci_esdhc_be_ops,
  658. };
  659. static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
  660. .quirks = ESDHC_DEFAULT_QUIRKS |
  661. SDHCI_QUIRK_NO_CARD_NO_RESET |
  662. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  663. .ops = &sdhci_esdhc_le_ops,
  664. };
  665. static struct soc_device_attribute soc_incorrect_hostver[] = {
  666. { .family = "QorIQ T4240", .revision = "1.0", },
  667. { .family = "QorIQ T4240", .revision = "2.0", },
  668. { },
  669. };
  670. static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
  671. {
  672. struct sdhci_pltfm_host *pltfm_host;
  673. struct sdhci_esdhc *esdhc;
  674. struct device_node *np;
  675. struct clk *clk;
  676. u32 val;
  677. u16 host_ver;
  678. pltfm_host = sdhci_priv(host);
  679. esdhc = sdhci_pltfm_priv(pltfm_host);
  680. host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
  681. esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
  682. SDHCI_VENDOR_VER_SHIFT;
  683. esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
  684. if (soc_device_match(soc_incorrect_hostver))
  685. esdhc->quirk_incorrect_hostver = true;
  686. else
  687. esdhc->quirk_incorrect_hostver = false;
  688. np = pdev->dev.of_node;
  689. clk = of_clk_get(np, 0);
  690. if (!IS_ERR(clk)) {
  691. /*
  692. * esdhc->peripheral_clock would be assigned with a value
  693. * which is eSDHC base clock when use periperal clock.
  694. * For ls1046a, the clock value got by common clk API is
  695. * peripheral clock while the eSDHC base clock is 1/2
  696. * peripheral clock.
  697. */
  698. if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
  699. esdhc->peripheral_clock = clk_get_rate(clk) / 2;
  700. else
  701. esdhc->peripheral_clock = clk_get_rate(clk);
  702. clk_put(clk);
  703. }
  704. if (esdhc->peripheral_clock) {
  705. esdhc_clock_enable(host, false);
  706. val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
  707. val |= ESDHC_PERIPHERAL_CLK_SEL;
  708. sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
  709. esdhc_clock_enable(host, true);
  710. }
  711. }
  712. static int sdhci_esdhc_probe(struct platform_device *pdev)
  713. {
  714. struct sdhci_host *host;
  715. struct device_node *np;
  716. struct sdhci_pltfm_host *pltfm_host;
  717. struct sdhci_esdhc *esdhc;
  718. int ret;
  719. np = pdev->dev.of_node;
  720. if (of_property_read_bool(np, "little-endian"))
  721. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
  722. sizeof(struct sdhci_esdhc));
  723. else
  724. host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
  725. sizeof(struct sdhci_esdhc));
  726. if (IS_ERR(host))
  727. return PTR_ERR(host);
  728. host->mmc_host_ops.start_signal_voltage_switch =
  729. esdhc_signal_voltage_switch;
  730. host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
  731. host->tuning_delay = 1;
  732. esdhc_init(pdev, host);
  733. sdhci_get_of_property(pdev);
  734. pltfm_host = sdhci_priv(host);
  735. esdhc = sdhci_pltfm_priv(pltfm_host);
  736. if (esdhc->vendor_ver == VENDOR_V_22)
  737. host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
  738. if (esdhc->vendor_ver > VENDOR_V_22)
  739. host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
  740. if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
  741. of_device_is_compatible(np, "fsl,p5020-esdhc") ||
  742. of_device_is_compatible(np, "fsl,p4080-esdhc") ||
  743. of_device_is_compatible(np, "fsl,p1020-esdhc") ||
  744. of_device_is_compatible(np, "fsl,t1040-esdhc"))
  745. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  746. if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
  747. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  748. if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
  749. /*
  750. * Freescale messed up with P2020 as it has a non-standard
  751. * host control register
  752. */
  753. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
  754. }
  755. /* call to generic mmc_of_parse to support additional capabilities */
  756. ret = mmc_of_parse(host->mmc);
  757. if (ret)
  758. goto err;
  759. mmc_of_parse_voltage(np, &host->ocr_mask);
  760. ret = sdhci_add_host(host);
  761. if (ret)
  762. goto err;
  763. return 0;
  764. err:
  765. sdhci_pltfm_free(pdev);
  766. return ret;
  767. }
  768. static const struct of_device_id sdhci_esdhc_of_match[] = {
  769. { .compatible = "fsl,mpc8379-esdhc" },
  770. { .compatible = "fsl,mpc8536-esdhc" },
  771. { .compatible = "fsl,esdhc" },
  772. { }
  773. };
  774. MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
  775. static struct platform_driver sdhci_esdhc_driver = {
  776. .driver = {
  777. .name = "sdhci-esdhc",
  778. .of_match_table = sdhci_esdhc_of_match,
  779. .pm = &esdhc_of_dev_pm_ops,
  780. },
  781. .probe = sdhci_esdhc_probe,
  782. .remove = sdhci_pltfm_unregister,
  783. };
  784. module_platform_driver(sdhci_esdhc_driver);
  785. MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
  786. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  787. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  788. MODULE_LICENSE("GPL v2");