sdhci-of-esdhc.c 23 KB

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