sdhci-s3c.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* linux/drivers/mmc/host/sdhci-s3c.c
  3. *
  4. * Copyright 2008 Openmoko Inc.
  5. * Copyright 2008 Simtec Electronics
  6. * Ben Dooks <ben@simtec.co.uk>
  7. * http://armlinux.simtec.co.uk/
  8. *
  9. * SDHCI (HSMMC) support for Samsung SoC
  10. */
  11. #include <linux/spinlock.h>
  12. #include <linux/delay.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/platform_data/mmc-sdhci-s3c.h>
  16. #include <linux/slab.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/pm.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/mmc/host.h>
  27. #include "sdhci.h"
  28. #define MAX_BUS_CLK (4)
  29. #define S3C_SDHCI_CONTROL2 (0x80)
  30. #define S3C_SDHCI_CONTROL3 (0x84)
  31. #define S3C64XX_SDHCI_CONTROL4 (0x8C)
  32. #define S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR BIT(31)
  33. #define S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK BIT(30)
  34. #define S3C_SDHCI_CTRL2_CDINVRXD3 BIT(29)
  35. #define S3C_SDHCI_CTRL2_SLCARDOUT BIT(28)
  36. #define S3C_SDHCI_CTRL2_FLTCLKSEL_MASK (0xf << 24)
  37. #define S3C_SDHCI_CTRL2_FLTCLKSEL_SHIFT (24)
  38. #define S3C_SDHCI_CTRL2_FLTCLKSEL(_x) ((_x) << 24)
  39. #define S3C_SDHCI_CTRL2_LVLDAT_MASK (0xff << 16)
  40. #define S3C_SDHCI_CTRL2_LVLDAT_SHIFT (16)
  41. #define S3C_SDHCI_CTRL2_LVLDAT(_x) ((_x) << 16)
  42. #define S3C_SDHCI_CTRL2_ENFBCLKTX BIT(15)
  43. #define S3C_SDHCI_CTRL2_ENFBCLKRX BIT(14)
  44. #define S3C_SDHCI_CTRL2_SDCDSEL BIT(13)
  45. #define S3C_SDHCI_CTRL2_SDSIGPC BIT(12)
  46. #define S3C_SDHCI_CTRL2_ENBUSYCHKTXSTART BIT(11)
  47. #define S3C_SDHCI_CTRL2_DFCNT_MASK (0x3 << 9)
  48. #define S3C_SDHCI_CTRL2_DFCNT_SHIFT (9)
  49. #define S3C_SDHCI_CTRL2_DFCNT_NONE (0x0 << 9)
  50. #define S3C_SDHCI_CTRL2_DFCNT_4SDCLK (0x1 << 9)
  51. #define S3C_SDHCI_CTRL2_DFCNT_16SDCLK (0x2 << 9)
  52. #define S3C_SDHCI_CTRL2_DFCNT_64SDCLK (0x3 << 9)
  53. #define S3C_SDHCI_CTRL2_ENCLKOUTHOLD BIT(8)
  54. #define S3C_SDHCI_CTRL2_RWAITMODE BIT(7)
  55. #define S3C_SDHCI_CTRL2_DISBUFRD BIT(6)
  56. #define S3C_SDHCI_CTRL2_SELBASECLK_MASK (0x3 << 4)
  57. #define S3C_SDHCI_CTRL2_SELBASECLK_SHIFT (4)
  58. #define S3C_SDHCI_CTRL2_PWRSYNC BIT(3)
  59. #define S3C_SDHCI_CTRL2_ENCLKOUTMSKCON BIT(1)
  60. #define S3C_SDHCI_CTRL2_HWINITFIN BIT(0)
  61. #define S3C_SDHCI_CTRL3_FCSEL3 BIT(31)
  62. #define S3C_SDHCI_CTRL3_FCSEL2 BIT(23)
  63. #define S3C_SDHCI_CTRL3_FCSEL1 BIT(15)
  64. #define S3C_SDHCI_CTRL3_FCSEL0 BIT(7)
  65. #define S3C_SDHCI_CTRL3_FIA3_MASK (0x7f << 24)
  66. #define S3C_SDHCI_CTRL3_FIA3_SHIFT (24)
  67. #define S3C_SDHCI_CTRL3_FIA3(_x) ((_x) << 24)
  68. #define S3C_SDHCI_CTRL3_FIA2_MASK (0x7f << 16)
  69. #define S3C_SDHCI_CTRL3_FIA2_SHIFT (16)
  70. #define S3C_SDHCI_CTRL3_FIA2(_x) ((_x) << 16)
  71. #define S3C_SDHCI_CTRL3_FIA1_MASK (0x7f << 8)
  72. #define S3C_SDHCI_CTRL3_FIA1_SHIFT (8)
  73. #define S3C_SDHCI_CTRL3_FIA1(_x) ((_x) << 8)
  74. #define S3C_SDHCI_CTRL3_FIA0_MASK (0x7f << 0)
  75. #define S3C_SDHCI_CTRL3_FIA0_SHIFT (0)
  76. #define S3C_SDHCI_CTRL3_FIA0(_x) ((_x) << 0)
  77. #define S3C64XX_SDHCI_CONTROL4_DRIVE_MASK (0x3 << 16)
  78. #define S3C64XX_SDHCI_CONTROL4_DRIVE_SHIFT (16)
  79. #define S3C64XX_SDHCI_CONTROL4_DRIVE_2mA (0x0 << 16)
  80. #define S3C64XX_SDHCI_CONTROL4_DRIVE_4mA (0x1 << 16)
  81. #define S3C64XX_SDHCI_CONTROL4_DRIVE_7mA (0x2 << 16)
  82. #define S3C64XX_SDHCI_CONTROL4_DRIVE_9mA (0x3 << 16)
  83. #define S3C64XX_SDHCI_CONTROL4_BUSY (1)
  84. /**
  85. * struct sdhci_s3c - S3C SDHCI instance
  86. * @host: The SDHCI host created
  87. * @pdev: The platform device we where created from.
  88. * @ioarea: The resource created when we claimed the IO area.
  89. * @pdata: The platform data for this controller.
  90. * @cur_clk: The index of the current bus clock.
  91. * @ext_cd_irq: External card detect interrupt.
  92. * @clk_io: The clock for the internal bus interface.
  93. * @clk_rates: Clock frequencies.
  94. * @clk_bus: The clocks that are available for the SD/MMC bus clock.
  95. * @no_divider: No or non-standard internal clock divider.
  96. */
  97. struct sdhci_s3c {
  98. struct sdhci_host *host;
  99. struct platform_device *pdev;
  100. struct resource *ioarea;
  101. struct s3c_sdhci_platdata *pdata;
  102. int cur_clk;
  103. int ext_cd_irq;
  104. struct clk *clk_io;
  105. struct clk *clk_bus[MAX_BUS_CLK];
  106. unsigned long clk_rates[MAX_BUS_CLK];
  107. bool no_divider;
  108. };
  109. /**
  110. * struct sdhci_s3c_drv_data - S3C SDHCI platform specific driver data
  111. * @sdhci_quirks: sdhci host specific quirks.
  112. * @no_divider: no or non-standard internal clock divider.
  113. *
  114. * Specifies platform specific configuration of sdhci controller.
  115. * Note: A structure for driver specific platform data is used for future
  116. * expansion of its usage.
  117. */
  118. struct sdhci_s3c_drv_data {
  119. unsigned int sdhci_quirks;
  120. bool no_divider;
  121. };
  122. static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
  123. {
  124. return sdhci_priv(host);
  125. }
  126. /**
  127. * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
  128. * @host: The SDHCI host instance.
  129. *
  130. * Callback to return the maximum clock rate acheivable by the controller.
  131. */
  132. static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
  133. {
  134. struct sdhci_s3c *ourhost = to_s3c(host);
  135. unsigned long rate, max = 0;
  136. int src;
  137. for (src = 0; src < MAX_BUS_CLK; src++) {
  138. rate = ourhost->clk_rates[src];
  139. if (rate > max)
  140. max = rate;
  141. }
  142. return max;
  143. }
  144. /**
  145. * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
  146. * @ourhost: Our SDHCI instance.
  147. * @src: The source clock index.
  148. * @wanted: The clock frequency wanted.
  149. */
  150. static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
  151. unsigned int src,
  152. unsigned int wanted)
  153. {
  154. unsigned long rate;
  155. struct clk *clksrc = ourhost->clk_bus[src];
  156. int shift;
  157. if (IS_ERR(clksrc))
  158. return UINT_MAX;
  159. /*
  160. * If controller uses a non-standard clock division, find the best clock
  161. * speed possible with selected clock source and skip the division.
  162. */
  163. if (ourhost->no_divider) {
  164. rate = clk_round_rate(clksrc, wanted);
  165. return wanted - rate;
  166. }
  167. rate = ourhost->clk_rates[src];
  168. for (shift = 0; shift <= 8; ++shift) {
  169. if ((rate >> shift) <= wanted)
  170. break;
  171. }
  172. if (shift > 8) {
  173. dev_dbg(&ourhost->pdev->dev,
  174. "clk %d: rate %ld, min rate %lu > wanted %u\n",
  175. src, rate, rate / 256, wanted);
  176. return UINT_MAX;
  177. }
  178. dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
  179. src, rate, wanted, rate >> shift);
  180. return wanted - (rate >> shift);
  181. }
  182. /**
  183. * sdhci_s3c_set_clock - callback on clock change
  184. * @host: The SDHCI host being changed
  185. * @clock: The clock rate being requested.
  186. *
  187. * When the card's clock is going to be changed, look at the new frequency
  188. * and find the best clock source to go with it.
  189. */
  190. static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
  191. {
  192. struct sdhci_s3c *ourhost = to_s3c(host);
  193. unsigned int best = UINT_MAX;
  194. unsigned int delta;
  195. int best_src = 0;
  196. int src;
  197. u32 ctrl;
  198. host->mmc->actual_clock = 0;
  199. /* don't bother if the clock is going off. */
  200. if (clock == 0) {
  201. sdhci_set_clock(host, clock);
  202. return;
  203. }
  204. for (src = 0; src < MAX_BUS_CLK; src++) {
  205. delta = sdhci_s3c_consider_clock(ourhost, src, clock);
  206. if (delta < best) {
  207. best = delta;
  208. best_src = src;
  209. }
  210. }
  211. dev_dbg(&ourhost->pdev->dev,
  212. "selected source %d, clock %d, delta %d\n",
  213. best_src, clock, best);
  214. /* select the new clock source */
  215. if (ourhost->cur_clk != best_src) {
  216. struct clk *clk = ourhost->clk_bus[best_src];
  217. clk_prepare_enable(clk);
  218. if (ourhost->cur_clk >= 0)
  219. clk_disable_unprepare(
  220. ourhost->clk_bus[ourhost->cur_clk]);
  221. ourhost->cur_clk = best_src;
  222. host->max_clk = ourhost->clk_rates[best_src];
  223. }
  224. /* turn clock off to card before changing clock source */
  225. writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
  226. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  227. ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  228. ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  229. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  230. /* reprogram default hardware configuration */
  231. writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
  232. host->ioaddr + S3C64XX_SDHCI_CONTROL4);
  233. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  234. ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
  235. S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
  236. S3C_SDHCI_CTRL2_ENFBCLKRX |
  237. S3C_SDHCI_CTRL2_DFCNT_NONE |
  238. S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
  239. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  240. /* reconfigure the controller for new clock rate */
  241. ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
  242. if (clock < 25 * 1000000)
  243. ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
  244. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
  245. sdhci_set_clock(host, clock);
  246. }
  247. /**
  248. * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
  249. * @host: The SDHCI host being queried
  250. *
  251. * To init mmc host properly a minimal clock value is needed. For high system
  252. * bus clock's values the standard formula gives values out of allowed range.
  253. * The clock still can be set to lower values, if clock source other then
  254. * system bus is selected.
  255. */
  256. static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
  257. {
  258. struct sdhci_s3c *ourhost = to_s3c(host);
  259. unsigned long rate, min = ULONG_MAX;
  260. int src;
  261. for (src = 0; src < MAX_BUS_CLK; src++) {
  262. rate = ourhost->clk_rates[src] / 256;
  263. if (!rate)
  264. continue;
  265. if (rate < min)
  266. min = rate;
  267. }
  268. return min;
  269. }
  270. /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
  271. static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
  272. {
  273. struct sdhci_s3c *ourhost = to_s3c(host);
  274. unsigned long rate, max = 0;
  275. int src;
  276. for (src = 0; src < MAX_BUS_CLK; src++) {
  277. struct clk *clk;
  278. clk = ourhost->clk_bus[src];
  279. if (IS_ERR(clk))
  280. continue;
  281. rate = clk_round_rate(clk, ULONG_MAX);
  282. if (rate > max)
  283. max = rate;
  284. }
  285. return max;
  286. }
  287. /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
  288. static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
  289. {
  290. struct sdhci_s3c *ourhost = to_s3c(host);
  291. unsigned long rate, min = ULONG_MAX;
  292. int src;
  293. for (src = 0; src < MAX_BUS_CLK; src++) {
  294. struct clk *clk;
  295. clk = ourhost->clk_bus[src];
  296. if (IS_ERR(clk))
  297. continue;
  298. rate = clk_round_rate(clk, 0);
  299. if (rate < min)
  300. min = rate;
  301. }
  302. return min;
  303. }
  304. /* sdhci_cmu_set_clock - callback on clock change.*/
  305. static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
  306. {
  307. struct sdhci_s3c *ourhost = to_s3c(host);
  308. struct device *dev = &ourhost->pdev->dev;
  309. unsigned long timeout;
  310. u16 clk = 0;
  311. int ret;
  312. host->mmc->actual_clock = 0;
  313. /* If the clock is going off, set to 0 at clock control register */
  314. if (clock == 0) {
  315. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  316. return;
  317. }
  318. sdhci_s3c_set_clock(host, clock);
  319. /* Reset SD Clock Enable */
  320. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  321. clk &= ~SDHCI_CLOCK_CARD_EN;
  322. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  323. ret = clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
  324. if (ret != 0) {
  325. dev_err(dev, "%s: failed to set clock rate %uHz\n",
  326. mmc_hostname(host->mmc), clock);
  327. return;
  328. }
  329. clk = SDHCI_CLOCK_INT_EN;
  330. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  331. /* Wait max 20 ms */
  332. timeout = 20;
  333. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  334. & SDHCI_CLOCK_INT_STABLE)) {
  335. if (timeout == 0) {
  336. dev_err(dev, "%s: Internal clock never stabilised.\n",
  337. mmc_hostname(host->mmc));
  338. return;
  339. }
  340. timeout--;
  341. mdelay(1);
  342. }
  343. clk |= SDHCI_CLOCK_CARD_EN;
  344. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  345. }
  346. static struct sdhci_ops sdhci_s3c_ops = {
  347. .get_max_clock = sdhci_s3c_get_max_clk,
  348. .set_clock = sdhci_s3c_set_clock,
  349. .get_min_clock = sdhci_s3c_get_min_clock,
  350. .set_bus_width = sdhci_set_bus_width,
  351. .reset = sdhci_reset,
  352. .set_uhs_signaling = sdhci_set_uhs_signaling,
  353. };
  354. #ifdef CONFIG_OF
  355. static int sdhci_s3c_parse_dt(struct device *dev,
  356. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  357. {
  358. struct device_node *node = dev->of_node;
  359. u32 max_width;
  360. /* if the bus-width property is not specified, assume width as 1 */
  361. if (of_property_read_u32(node, "bus-width", &max_width))
  362. max_width = 1;
  363. pdata->max_width = max_width;
  364. /* get the card detection method */
  365. if (of_get_property(node, "broken-cd", NULL)) {
  366. pdata->cd_type = S3C_SDHCI_CD_NONE;
  367. return 0;
  368. }
  369. if (of_get_property(node, "non-removable", NULL)) {
  370. pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
  371. return 0;
  372. }
  373. if (of_get_named_gpio(node, "cd-gpios", 0))
  374. return 0;
  375. /* assuming internal card detect that will be configured by pinctrl */
  376. pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
  377. return 0;
  378. }
  379. #else
  380. static int sdhci_s3c_parse_dt(struct device *dev,
  381. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  382. {
  383. return -EINVAL;
  384. }
  385. #endif
  386. static inline const struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
  387. struct platform_device *pdev)
  388. {
  389. #ifdef CONFIG_OF
  390. if (pdev->dev.of_node)
  391. return of_device_get_match_data(&pdev->dev);
  392. #endif
  393. return (const struct sdhci_s3c_drv_data *)
  394. platform_get_device_id(pdev)->driver_data;
  395. }
  396. static int sdhci_s3c_probe(struct platform_device *pdev)
  397. {
  398. struct s3c_sdhci_platdata *pdata;
  399. const struct sdhci_s3c_drv_data *drv_data;
  400. struct device *dev = &pdev->dev;
  401. struct sdhci_host *host;
  402. struct sdhci_s3c *sc;
  403. int ret, irq, ptr, clks;
  404. if (!pdev->dev.platform_data && !pdev->dev.of_node) {
  405. dev_err(dev, "no device data specified\n");
  406. return -ENOENT;
  407. }
  408. irq = platform_get_irq(pdev, 0);
  409. if (irq < 0)
  410. return irq;
  411. host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
  412. if (IS_ERR(host)) {
  413. dev_err(dev, "sdhci_alloc_host() failed\n");
  414. return PTR_ERR(host);
  415. }
  416. sc = sdhci_priv(host);
  417. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  418. if (!pdata) {
  419. ret = -ENOMEM;
  420. goto err_pdata_io_clk;
  421. }
  422. if (pdev->dev.of_node) {
  423. ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
  424. if (ret)
  425. goto err_pdata_io_clk;
  426. } else {
  427. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  428. }
  429. drv_data = sdhci_s3c_get_driver_data(pdev);
  430. sc->host = host;
  431. sc->pdev = pdev;
  432. sc->pdata = pdata;
  433. sc->cur_clk = -1;
  434. platform_set_drvdata(pdev, host);
  435. sc->clk_io = devm_clk_get(dev, "hsmmc");
  436. if (IS_ERR(sc->clk_io)) {
  437. dev_err(dev, "failed to get io clock\n");
  438. ret = PTR_ERR(sc->clk_io);
  439. goto err_pdata_io_clk;
  440. }
  441. /* enable the local io clock and keep it running for the moment. */
  442. clk_prepare_enable(sc->clk_io);
  443. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  444. char name[14];
  445. snprintf(name, 14, "mmc_busclk.%d", ptr);
  446. sc->clk_bus[ptr] = devm_clk_get(dev, name);
  447. if (IS_ERR(sc->clk_bus[ptr]))
  448. continue;
  449. clks++;
  450. sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]);
  451. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  452. ptr, name, sc->clk_rates[ptr]);
  453. }
  454. if (clks == 0) {
  455. dev_err(dev, "failed to find any bus clocks\n");
  456. ret = -ENOENT;
  457. goto err_no_busclks;
  458. }
  459. host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
  460. if (IS_ERR(host->ioaddr)) {
  461. ret = PTR_ERR(host->ioaddr);
  462. goto err_req_regs;
  463. }
  464. /* Ensure we have minimal gpio selected CMD/CLK/Detect */
  465. if (pdata->cfg_gpio)
  466. pdata->cfg_gpio(pdev, pdata->max_width);
  467. host->hw_name = "samsung-hsmmc";
  468. host->ops = &sdhci_s3c_ops;
  469. host->quirks = 0;
  470. host->quirks2 = 0;
  471. host->irq = irq;
  472. /* Setup quirks for the controller */
  473. host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
  474. host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
  475. if (drv_data) {
  476. host->quirks |= drv_data->sdhci_quirks;
  477. sc->no_divider = drv_data->no_divider;
  478. }
  479. #ifndef CONFIG_MMC_SDHCI_S3C_DMA
  480. /* we currently see overruns on errors, so disable the SDMA
  481. * support as well. */
  482. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  483. #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
  484. /* It seems we do not get an DATA transfer complete on non-busy
  485. * transfers, not sure if this is a problem with this specific
  486. * SDHCI block, or a missing configuration that needs to be set. */
  487. host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
  488. /* This host supports the Auto CMD12 */
  489. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  490. /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
  491. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
  492. if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
  493. pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  494. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  495. if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  496. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  497. switch (pdata->max_width) {
  498. case 8:
  499. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  500. fallthrough;
  501. case 4:
  502. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  503. break;
  504. }
  505. if (pdata->pm_caps)
  506. host->mmc->pm_caps |= pdata->pm_caps;
  507. host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
  508. SDHCI_QUIRK_32BIT_DMA_SIZE);
  509. /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
  510. host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
  511. /*
  512. * If controller does not have internal clock divider,
  513. * we can use overriding functions instead of default.
  514. */
  515. if (sc->no_divider) {
  516. sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
  517. sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
  518. sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
  519. }
  520. /* It supports additional host capabilities if needed */
  521. if (pdata->host_caps)
  522. host->mmc->caps |= pdata->host_caps;
  523. if (pdata->host_caps2)
  524. host->mmc->caps2 |= pdata->host_caps2;
  525. pm_runtime_enable(&pdev->dev);
  526. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  527. pm_runtime_use_autosuspend(&pdev->dev);
  528. pm_suspend_ignore_children(&pdev->dev, 1);
  529. ret = mmc_of_parse(host->mmc);
  530. if (ret)
  531. goto err_req_regs;
  532. ret = sdhci_add_host(host);
  533. if (ret)
  534. goto err_req_regs;
  535. #ifdef CONFIG_PM
  536. if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  537. clk_disable_unprepare(sc->clk_io);
  538. #endif
  539. return 0;
  540. err_req_regs:
  541. pm_runtime_disable(&pdev->dev);
  542. err_no_busclks:
  543. clk_disable_unprepare(sc->clk_io);
  544. err_pdata_io_clk:
  545. sdhci_free_host(host);
  546. return ret;
  547. }
  548. static int sdhci_s3c_remove(struct platform_device *pdev)
  549. {
  550. struct sdhci_host *host = platform_get_drvdata(pdev);
  551. struct sdhci_s3c *sc = sdhci_priv(host);
  552. if (sc->ext_cd_irq)
  553. free_irq(sc->ext_cd_irq, sc);
  554. #ifdef CONFIG_PM
  555. if (sc->pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  556. clk_prepare_enable(sc->clk_io);
  557. #endif
  558. sdhci_remove_host(host, 1);
  559. pm_runtime_dont_use_autosuspend(&pdev->dev);
  560. pm_runtime_disable(&pdev->dev);
  561. clk_disable_unprepare(sc->clk_io);
  562. sdhci_free_host(host);
  563. return 0;
  564. }
  565. #ifdef CONFIG_PM_SLEEP
  566. static int sdhci_s3c_suspend(struct device *dev)
  567. {
  568. struct sdhci_host *host = dev_get_drvdata(dev);
  569. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  570. mmc_retune_needed(host->mmc);
  571. return sdhci_suspend_host(host);
  572. }
  573. static int sdhci_s3c_resume(struct device *dev)
  574. {
  575. struct sdhci_host *host = dev_get_drvdata(dev);
  576. return sdhci_resume_host(host);
  577. }
  578. #endif
  579. #ifdef CONFIG_PM
  580. static int sdhci_s3c_runtime_suspend(struct device *dev)
  581. {
  582. struct sdhci_host *host = dev_get_drvdata(dev);
  583. struct sdhci_s3c *ourhost = to_s3c(host);
  584. struct clk *busclk = ourhost->clk_io;
  585. int ret;
  586. ret = sdhci_runtime_suspend_host(host);
  587. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  588. mmc_retune_needed(host->mmc);
  589. if (ourhost->cur_clk >= 0)
  590. clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
  591. clk_disable_unprepare(busclk);
  592. return ret;
  593. }
  594. static int sdhci_s3c_runtime_resume(struct device *dev)
  595. {
  596. struct sdhci_host *host = dev_get_drvdata(dev);
  597. struct sdhci_s3c *ourhost = to_s3c(host);
  598. struct clk *busclk = ourhost->clk_io;
  599. int ret;
  600. clk_prepare_enable(busclk);
  601. if (ourhost->cur_clk >= 0)
  602. clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
  603. ret = sdhci_runtime_resume_host(host, 0);
  604. return ret;
  605. }
  606. #endif
  607. static const struct dev_pm_ops sdhci_s3c_pmops = {
  608. SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
  609. SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
  610. NULL)
  611. };
  612. static const struct platform_device_id sdhci_s3c_driver_ids[] = {
  613. {
  614. .name = "s3c-sdhci",
  615. .driver_data = (kernel_ulong_t)NULL,
  616. },
  617. { }
  618. };
  619. MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
  620. #ifdef CONFIG_OF
  621. static const struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
  622. .no_divider = true,
  623. };
  624. static const struct of_device_id sdhci_s3c_dt_match[] = {
  625. { .compatible = "samsung,s3c6410-sdhci", },
  626. { .compatible = "samsung,exynos4210-sdhci",
  627. .data = &exynos4_sdhci_drv_data },
  628. {},
  629. };
  630. MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
  631. #endif
  632. static struct platform_driver sdhci_s3c_driver = {
  633. .probe = sdhci_s3c_probe,
  634. .remove = sdhci_s3c_remove,
  635. .id_table = sdhci_s3c_driver_ids,
  636. .driver = {
  637. .name = "s3c-sdhci",
  638. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  639. .of_match_table = of_match_ptr(sdhci_s3c_dt_match),
  640. .pm = &sdhci_s3c_pmops,
  641. },
  642. };
  643. module_platform_driver(sdhci_s3c_driver);
  644. MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
  645. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  646. MODULE_LICENSE("GPL v2");