sdhci-s3c.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /* linux/drivers/mmc/host/sdhci-s3c.c
  2. *
  3. * Copyright 2008 Openmoko Inc.
  4. * Copyright 2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * SDHCI (HSMMC) support for Samsung SoC
  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 version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/platform_data/mmc-sdhci-s3c.h>
  18. #include <linux/slab.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/gpio.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/mmc/host.h>
  28. #include "sdhci-s3c-regs.h"
  29. #include "sdhci.h"
  30. #define MAX_BUS_CLK (4)
  31. /* Number of gpio's used is max data bus width + command and clock lines */
  32. #define NUM_GPIOS(x) (x + 2)
  33. /**
  34. * struct sdhci_s3c - S3C SDHCI instance
  35. * @host: The SDHCI host created
  36. * @pdev: The platform device we where created from.
  37. * @ioarea: The resource created when we claimed the IO area.
  38. * @pdata: The platform data for this controller.
  39. * @cur_clk: The index of the current bus clock.
  40. * @clk_io: The clock for the internal bus interface.
  41. * @clk_bus: The clocks that are available for the SD/MMC bus clock.
  42. */
  43. struct sdhci_s3c {
  44. struct sdhci_host *host;
  45. struct platform_device *pdev;
  46. struct resource *ioarea;
  47. struct s3c_sdhci_platdata *pdata;
  48. unsigned int cur_clk;
  49. int ext_cd_irq;
  50. int ext_cd_gpio;
  51. struct clk *clk_io;
  52. struct clk *clk_bus[MAX_BUS_CLK];
  53. };
  54. /**
  55. * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
  56. * @sdhci_quirks: sdhci host specific quirks.
  57. *
  58. * Specifies platform specific configuration of sdhci controller.
  59. * Note: A structure for driver specific platform data is used for future
  60. * expansion of its usage.
  61. */
  62. struct sdhci_s3c_drv_data {
  63. unsigned int sdhci_quirks;
  64. };
  65. static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
  66. {
  67. return sdhci_priv(host);
  68. }
  69. /**
  70. * get_curclk - convert ctrl2 register to clock source number
  71. * @ctrl2: Control2 register value.
  72. */
  73. static u32 get_curclk(u32 ctrl2)
  74. {
  75. ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  76. ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  77. return ctrl2;
  78. }
  79. static void sdhci_s3c_check_sclk(struct sdhci_host *host)
  80. {
  81. struct sdhci_s3c *ourhost = to_s3c(host);
  82. u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  83. if (get_curclk(tmp) != ourhost->cur_clk) {
  84. dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
  85. tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  86. tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  87. writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
  88. }
  89. }
  90. /**
  91. * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
  92. * @host: The SDHCI host instance.
  93. *
  94. * Callback to return the maximum clock rate acheivable by the controller.
  95. */
  96. static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
  97. {
  98. struct sdhci_s3c *ourhost = to_s3c(host);
  99. struct clk *busclk;
  100. unsigned int rate, max;
  101. int clk;
  102. /* note, a reset will reset the clock source */
  103. sdhci_s3c_check_sclk(host);
  104. for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
  105. busclk = ourhost->clk_bus[clk];
  106. if (!busclk)
  107. continue;
  108. rate = clk_get_rate(busclk);
  109. if (rate > max)
  110. max = rate;
  111. }
  112. return max;
  113. }
  114. /**
  115. * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
  116. * @ourhost: Our SDHCI instance.
  117. * @src: The source clock index.
  118. * @wanted: The clock frequency wanted.
  119. */
  120. static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
  121. unsigned int src,
  122. unsigned int wanted)
  123. {
  124. unsigned long rate;
  125. struct clk *clksrc = ourhost->clk_bus[src];
  126. int div;
  127. if (!clksrc)
  128. return UINT_MAX;
  129. /*
  130. * If controller uses a non-standard clock division, find the best clock
  131. * speed possible with selected clock source and skip the division.
  132. */
  133. if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
  134. rate = clk_round_rate(clksrc, wanted);
  135. return wanted - rate;
  136. }
  137. rate = clk_get_rate(clksrc);
  138. for (div = 1; div < 256; div *= 2) {
  139. if ((rate / div) <= wanted)
  140. break;
  141. }
  142. dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
  143. src, rate, wanted, rate / div);
  144. return wanted - (rate / div);
  145. }
  146. /**
  147. * sdhci_s3c_set_clock - callback on clock change
  148. * @host: The SDHCI host being changed
  149. * @clock: The clock rate being requested.
  150. *
  151. * When the card's clock is going to be changed, look at the new frequency
  152. * and find the best clock source to go with it.
  153. */
  154. static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
  155. {
  156. struct sdhci_s3c *ourhost = to_s3c(host);
  157. unsigned int best = UINT_MAX;
  158. unsigned int delta;
  159. int best_src = 0;
  160. int src;
  161. u32 ctrl;
  162. /* don't bother if the clock is going off. */
  163. if (clock == 0)
  164. return;
  165. for (src = 0; src < MAX_BUS_CLK; src++) {
  166. delta = sdhci_s3c_consider_clock(ourhost, src, clock);
  167. if (delta < best) {
  168. best = delta;
  169. best_src = src;
  170. }
  171. }
  172. dev_dbg(&ourhost->pdev->dev,
  173. "selected source %d, clock %d, delta %d\n",
  174. best_src, clock, best);
  175. /* select the new clock source */
  176. if (ourhost->cur_clk != best_src) {
  177. struct clk *clk = ourhost->clk_bus[best_src];
  178. clk_prepare_enable(clk);
  179. clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
  180. /* turn clock off to card before changing clock source */
  181. writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
  182. ourhost->cur_clk = best_src;
  183. host->max_clk = clk_get_rate(clk);
  184. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  185. ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  186. ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  187. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  188. }
  189. /* reprogram default hardware configuration */
  190. writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
  191. host->ioaddr + S3C64XX_SDHCI_CONTROL4);
  192. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  193. ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
  194. S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
  195. S3C_SDHCI_CTRL2_ENFBCLKRX |
  196. S3C_SDHCI_CTRL2_DFCNT_NONE |
  197. S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
  198. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  199. /* reconfigure the controller for new clock rate */
  200. ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
  201. if (clock < 25 * 1000000)
  202. ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
  203. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
  204. }
  205. /**
  206. * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
  207. * @host: The SDHCI host being queried
  208. *
  209. * To init mmc host properly a minimal clock value is needed. For high system
  210. * bus clock's values the standard formula gives values out of allowed range.
  211. * The clock still can be set to lower values, if clock source other then
  212. * system bus is selected.
  213. */
  214. static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
  215. {
  216. struct sdhci_s3c *ourhost = to_s3c(host);
  217. unsigned int delta, min = UINT_MAX;
  218. int src;
  219. for (src = 0; src < MAX_BUS_CLK; src++) {
  220. delta = sdhci_s3c_consider_clock(ourhost, src, 0);
  221. if (delta == UINT_MAX)
  222. continue;
  223. /* delta is a negative value in this case */
  224. if (-delta < min)
  225. min = -delta;
  226. }
  227. return min;
  228. }
  229. /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
  230. static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
  231. {
  232. struct sdhci_s3c *ourhost = to_s3c(host);
  233. return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
  234. }
  235. /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
  236. static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
  237. {
  238. struct sdhci_s3c *ourhost = to_s3c(host);
  239. /*
  240. * initial clock can be in the frequency range of
  241. * 100KHz-400KHz, so we set it as max value.
  242. */
  243. return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
  244. }
  245. /* sdhci_cmu_set_clock - callback on clock change.*/
  246. static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
  247. {
  248. struct sdhci_s3c *ourhost = to_s3c(host);
  249. struct device *dev = &ourhost->pdev->dev;
  250. unsigned long timeout;
  251. u16 clk = 0;
  252. /* If the clock is going off, set to 0 at clock control register */
  253. if (clock == 0) {
  254. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  255. host->clock = clock;
  256. return;
  257. }
  258. sdhci_s3c_set_clock(host, clock);
  259. clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
  260. host->clock = clock;
  261. clk = SDHCI_CLOCK_INT_EN;
  262. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  263. /* Wait max 20 ms */
  264. timeout = 20;
  265. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  266. & SDHCI_CLOCK_INT_STABLE)) {
  267. if (timeout == 0) {
  268. dev_err(dev, "%s: Internal clock never stabilised.\n",
  269. mmc_hostname(host->mmc));
  270. return;
  271. }
  272. timeout--;
  273. mdelay(1);
  274. }
  275. clk |= SDHCI_CLOCK_CARD_EN;
  276. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  277. }
  278. /**
  279. * sdhci_s3c_platform_bus_width - support 8bit buswidth
  280. * @host: The SDHCI host being queried
  281. * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
  282. *
  283. * We have 8-bit width support but is not a v3 controller.
  284. * So we add platform_bus_width() and support 8bit width.
  285. */
  286. static int sdhci_s3c_platform_bus_width(struct sdhci_host *host, int width)
  287. {
  288. u8 ctrl;
  289. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  290. switch (width) {
  291. case MMC_BUS_WIDTH_8:
  292. ctrl |= SDHCI_CTRL_8BITBUS;
  293. ctrl &= ~SDHCI_CTRL_4BITBUS;
  294. break;
  295. case MMC_BUS_WIDTH_4:
  296. ctrl |= SDHCI_CTRL_4BITBUS;
  297. ctrl &= ~SDHCI_CTRL_8BITBUS;
  298. break;
  299. default:
  300. ctrl &= ~SDHCI_CTRL_4BITBUS;
  301. ctrl &= ~SDHCI_CTRL_8BITBUS;
  302. break;
  303. }
  304. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  305. return 0;
  306. }
  307. static struct sdhci_ops sdhci_s3c_ops = {
  308. .get_max_clock = sdhci_s3c_get_max_clk,
  309. .set_clock = sdhci_s3c_set_clock,
  310. .get_min_clock = sdhci_s3c_get_min_clock,
  311. .platform_bus_width = sdhci_s3c_platform_bus_width,
  312. };
  313. static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
  314. {
  315. struct sdhci_host *host = platform_get_drvdata(dev);
  316. #ifdef CONFIG_PM_RUNTIME
  317. struct sdhci_s3c *sc = sdhci_priv(host);
  318. #endif
  319. unsigned long flags;
  320. if (host) {
  321. spin_lock_irqsave(&host->lock, flags);
  322. if (state) {
  323. dev_dbg(&dev->dev, "card inserted.\n");
  324. #ifdef CONFIG_PM_RUNTIME
  325. clk_prepare_enable(sc->clk_io);
  326. #endif
  327. host->flags &= ~SDHCI_DEVICE_DEAD;
  328. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  329. } else {
  330. dev_dbg(&dev->dev, "card removed.\n");
  331. host->flags |= SDHCI_DEVICE_DEAD;
  332. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  333. #ifdef CONFIG_PM_RUNTIME
  334. clk_disable_unprepare(sc->clk_io);
  335. #endif
  336. }
  337. tasklet_schedule(&host->card_tasklet);
  338. spin_unlock_irqrestore(&host->lock, flags);
  339. }
  340. }
  341. static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
  342. {
  343. struct sdhci_s3c *sc = dev_id;
  344. int status = gpio_get_value(sc->ext_cd_gpio);
  345. if (sc->pdata->ext_cd_gpio_invert)
  346. status = !status;
  347. sdhci_s3c_notify_change(sc->pdev, status);
  348. return IRQ_HANDLED;
  349. }
  350. static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
  351. {
  352. struct s3c_sdhci_platdata *pdata = sc->pdata;
  353. struct device *dev = &sc->pdev->dev;
  354. if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
  355. sc->ext_cd_gpio = pdata->ext_cd_gpio;
  356. sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
  357. if (sc->ext_cd_irq &&
  358. request_threaded_irq(sc->ext_cd_irq, NULL,
  359. sdhci_s3c_gpio_card_detect_thread,
  360. IRQF_TRIGGER_RISING |
  361. IRQF_TRIGGER_FALLING |
  362. IRQF_ONESHOT,
  363. dev_name(dev), sc) == 0) {
  364. int status = gpio_get_value(sc->ext_cd_gpio);
  365. if (pdata->ext_cd_gpio_invert)
  366. status = !status;
  367. sdhci_s3c_notify_change(sc->pdev, status);
  368. } else {
  369. dev_warn(dev, "cannot request irq for card detect\n");
  370. sc->ext_cd_irq = 0;
  371. }
  372. } else {
  373. dev_err(dev, "cannot request gpio for card detect\n");
  374. }
  375. }
  376. #ifdef CONFIG_OF
  377. static int sdhci_s3c_parse_dt(struct device *dev,
  378. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  379. {
  380. struct device_node *node = dev->of_node;
  381. struct sdhci_s3c *ourhost = to_s3c(host);
  382. u32 max_width;
  383. int gpio;
  384. /* if the bus-width property is not specified, assume width as 1 */
  385. if (of_property_read_u32(node, "bus-width", &max_width))
  386. max_width = 1;
  387. pdata->max_width = max_width;
  388. /* get the card detection method */
  389. if (of_get_property(node, "broken-cd", NULL)) {
  390. pdata->cd_type = S3C_SDHCI_CD_NONE;
  391. return 0;
  392. }
  393. if (of_get_property(node, "non-removable", NULL)) {
  394. pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
  395. return 0;
  396. }
  397. gpio = of_get_named_gpio(node, "cd-gpios", 0);
  398. if (gpio_is_valid(gpio)) {
  399. pdata->cd_type = S3C_SDHCI_CD_GPIO;
  400. pdata->ext_cd_gpio = gpio;
  401. ourhost->ext_cd_gpio = -1;
  402. if (of_get_property(node, "cd-inverted", NULL))
  403. pdata->ext_cd_gpio_invert = 1;
  404. return 0;
  405. } else if (gpio != -ENOENT) {
  406. dev_err(dev, "invalid card detect gpio specified\n");
  407. return -EINVAL;
  408. }
  409. /* assuming internal card detect that will be configured by pinctrl */
  410. pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
  411. return 0;
  412. }
  413. #else
  414. static int sdhci_s3c_parse_dt(struct device *dev,
  415. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  416. {
  417. return -EINVAL;
  418. }
  419. #endif
  420. static const struct of_device_id sdhci_s3c_dt_match[];
  421. static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
  422. struct platform_device *pdev)
  423. {
  424. #ifdef CONFIG_OF
  425. if (pdev->dev.of_node) {
  426. const struct of_device_id *match;
  427. match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
  428. return (struct sdhci_s3c_drv_data *)match->data;
  429. }
  430. #endif
  431. return (struct sdhci_s3c_drv_data *)
  432. platform_get_device_id(pdev)->driver_data;
  433. }
  434. static int sdhci_s3c_probe(struct platform_device *pdev)
  435. {
  436. struct s3c_sdhci_platdata *pdata;
  437. struct sdhci_s3c_drv_data *drv_data;
  438. struct device *dev = &pdev->dev;
  439. struct sdhci_host *host;
  440. struct sdhci_s3c *sc;
  441. struct resource *res;
  442. int ret, irq, ptr, clks;
  443. if (!pdev->dev.platform_data && !pdev->dev.of_node) {
  444. dev_err(dev, "no device data specified\n");
  445. return -ENOENT;
  446. }
  447. irq = platform_get_irq(pdev, 0);
  448. if (irq < 0) {
  449. dev_err(dev, "no irq specified\n");
  450. return irq;
  451. }
  452. host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
  453. if (IS_ERR(host)) {
  454. dev_err(dev, "sdhci_alloc_host() failed\n");
  455. return PTR_ERR(host);
  456. }
  457. sc = sdhci_priv(host);
  458. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  459. if (!pdata) {
  460. ret = -ENOMEM;
  461. goto err_pdata_io_clk;
  462. }
  463. if (pdev->dev.of_node) {
  464. ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
  465. if (ret)
  466. goto err_pdata_io_clk;
  467. } else {
  468. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  469. sc->ext_cd_gpio = -1; /* invalid gpio number */
  470. }
  471. drv_data = sdhci_s3c_get_driver_data(pdev);
  472. sc->host = host;
  473. sc->pdev = pdev;
  474. sc->pdata = pdata;
  475. platform_set_drvdata(pdev, host);
  476. sc->clk_io = devm_clk_get(dev, "hsmmc");
  477. if (IS_ERR(sc->clk_io)) {
  478. dev_err(dev, "failed to get io clock\n");
  479. ret = PTR_ERR(sc->clk_io);
  480. goto err_pdata_io_clk;
  481. }
  482. /* enable the local io clock and keep it running for the moment. */
  483. clk_prepare_enable(sc->clk_io);
  484. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  485. struct clk *clk;
  486. char name[14];
  487. snprintf(name, 14, "mmc_busclk.%d", ptr);
  488. clk = devm_clk_get(dev, name);
  489. if (IS_ERR(clk))
  490. continue;
  491. clks++;
  492. sc->clk_bus[ptr] = clk;
  493. /*
  494. * save current clock index to know which clock bus
  495. * is used later in overriding functions.
  496. */
  497. sc->cur_clk = ptr;
  498. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  499. ptr, name, clk_get_rate(clk));
  500. }
  501. if (clks == 0) {
  502. dev_err(dev, "failed to find any bus clocks\n");
  503. ret = -ENOENT;
  504. goto err_no_busclks;
  505. }
  506. #ifndef CONFIG_PM_RUNTIME
  507. clk_prepare_enable(sc->clk_bus[sc->cur_clk]);
  508. #endif
  509. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  510. host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  511. if (IS_ERR(host->ioaddr)) {
  512. ret = PTR_ERR(host->ioaddr);
  513. goto err_req_regs;
  514. }
  515. /* Ensure we have minimal gpio selected CMD/CLK/Detect */
  516. if (pdata->cfg_gpio)
  517. pdata->cfg_gpio(pdev, pdata->max_width);
  518. host->hw_name = "samsung-hsmmc";
  519. host->ops = &sdhci_s3c_ops;
  520. host->quirks = 0;
  521. host->quirks2 = 0;
  522. host->irq = irq;
  523. /* Setup quirks for the controller */
  524. host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
  525. host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
  526. if (drv_data)
  527. host->quirks |= drv_data->sdhci_quirks;
  528. #ifndef CONFIG_MMC_SDHCI_S3C_DMA
  529. /* we currently see overruns on errors, so disable the SDMA
  530. * support as well. */
  531. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  532. #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
  533. /* It seems we do not get an DATA transfer complete on non-busy
  534. * transfers, not sure if this is a problem with this specific
  535. * SDHCI block, or a missing configuration that needs to be set. */
  536. host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
  537. /* This host supports the Auto CMD12 */
  538. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  539. /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
  540. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
  541. if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
  542. pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  543. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  544. if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  545. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  546. switch (pdata->max_width) {
  547. case 8:
  548. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  549. case 4:
  550. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  551. break;
  552. }
  553. if (pdata->pm_caps)
  554. host->mmc->pm_caps |= pdata->pm_caps;
  555. host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
  556. SDHCI_QUIRK_32BIT_DMA_SIZE);
  557. /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
  558. host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
  559. /*
  560. * If controller does not have internal clock divider,
  561. * we can use overriding functions instead of default.
  562. */
  563. if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
  564. sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
  565. sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
  566. sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
  567. }
  568. /* It supports additional host capabilities if needed */
  569. if (pdata->host_caps)
  570. host->mmc->caps |= pdata->host_caps;
  571. if (pdata->host_caps2)
  572. host->mmc->caps2 |= pdata->host_caps2;
  573. pm_runtime_enable(&pdev->dev);
  574. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  575. pm_runtime_use_autosuspend(&pdev->dev);
  576. pm_suspend_ignore_children(&pdev->dev, 1);
  577. ret = sdhci_add_host(host);
  578. if (ret) {
  579. dev_err(dev, "sdhci_add_host() failed\n");
  580. pm_runtime_forbid(&pdev->dev);
  581. pm_runtime_get_noresume(&pdev->dev);
  582. goto err_req_regs;
  583. }
  584. /* The following two methods of card detection might call
  585. sdhci_s3c_notify_change() immediately, so they can be called
  586. only after sdhci_add_host(). Setup errors are ignored. */
  587. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
  588. pdata->ext_cd_init(&sdhci_s3c_notify_change);
  589. if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
  590. gpio_is_valid(pdata->ext_cd_gpio))
  591. sdhci_s3c_setup_card_detect_gpio(sc);
  592. #ifdef CONFIG_PM_RUNTIME
  593. if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  594. clk_disable_unprepare(sc->clk_io);
  595. #endif
  596. return 0;
  597. err_req_regs:
  598. #ifndef CONFIG_PM_RUNTIME
  599. clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
  600. #endif
  601. err_no_busclks:
  602. clk_disable_unprepare(sc->clk_io);
  603. err_pdata_io_clk:
  604. sdhci_free_host(host);
  605. return ret;
  606. }
  607. static int sdhci_s3c_remove(struct platform_device *pdev)
  608. {
  609. struct sdhci_host *host = platform_get_drvdata(pdev);
  610. struct sdhci_s3c *sc = sdhci_priv(host);
  611. struct s3c_sdhci_platdata *pdata = sc->pdata;
  612. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
  613. pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
  614. if (sc->ext_cd_irq)
  615. free_irq(sc->ext_cd_irq, sc);
  616. #ifdef CONFIG_PM_RUNTIME
  617. if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  618. clk_prepare_enable(sc->clk_io);
  619. #endif
  620. sdhci_remove_host(host, 1);
  621. pm_runtime_dont_use_autosuspend(&pdev->dev);
  622. pm_runtime_disable(&pdev->dev);
  623. #ifndef CONFIG_PM_RUNTIME
  624. clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
  625. #endif
  626. clk_disable_unprepare(sc->clk_io);
  627. sdhci_free_host(host);
  628. return 0;
  629. }
  630. #ifdef CONFIG_PM_SLEEP
  631. static int sdhci_s3c_suspend(struct device *dev)
  632. {
  633. struct sdhci_host *host = dev_get_drvdata(dev);
  634. return sdhci_suspend_host(host);
  635. }
  636. static int sdhci_s3c_resume(struct device *dev)
  637. {
  638. struct sdhci_host *host = dev_get_drvdata(dev);
  639. return sdhci_resume_host(host);
  640. }
  641. #endif
  642. #ifdef CONFIG_PM_RUNTIME
  643. static int sdhci_s3c_runtime_suspend(struct device *dev)
  644. {
  645. struct sdhci_host *host = dev_get_drvdata(dev);
  646. struct sdhci_s3c *ourhost = to_s3c(host);
  647. struct clk *busclk = ourhost->clk_io;
  648. int ret;
  649. ret = sdhci_runtime_suspend_host(host);
  650. clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
  651. clk_disable_unprepare(busclk);
  652. return ret;
  653. }
  654. static int sdhci_s3c_runtime_resume(struct device *dev)
  655. {
  656. struct sdhci_host *host = dev_get_drvdata(dev);
  657. struct sdhci_s3c *ourhost = to_s3c(host);
  658. struct clk *busclk = ourhost->clk_io;
  659. int ret;
  660. clk_prepare_enable(busclk);
  661. clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
  662. ret = sdhci_runtime_resume_host(host);
  663. return ret;
  664. }
  665. #endif
  666. #ifdef CONFIG_PM
  667. static const struct dev_pm_ops sdhci_s3c_pmops = {
  668. SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
  669. SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
  670. NULL)
  671. };
  672. #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
  673. #else
  674. #define SDHCI_S3C_PMOPS NULL
  675. #endif
  676. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
  677. static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
  678. .sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
  679. };
  680. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
  681. #else
  682. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
  683. #endif
  684. static struct platform_device_id sdhci_s3c_driver_ids[] = {
  685. {
  686. .name = "s3c-sdhci",
  687. .driver_data = (kernel_ulong_t)NULL,
  688. }, {
  689. .name = "exynos4-sdhci",
  690. .driver_data = EXYNOS4_SDHCI_DRV_DATA,
  691. },
  692. { }
  693. };
  694. MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
  695. #ifdef CONFIG_OF
  696. static const struct of_device_id sdhci_s3c_dt_match[] = {
  697. { .compatible = "samsung,s3c6410-sdhci", },
  698. { .compatible = "samsung,exynos4210-sdhci",
  699. .data = (void *)EXYNOS4_SDHCI_DRV_DATA },
  700. {},
  701. };
  702. MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
  703. #endif
  704. static struct platform_driver sdhci_s3c_driver = {
  705. .probe = sdhci_s3c_probe,
  706. .remove = sdhci_s3c_remove,
  707. .id_table = sdhci_s3c_driver_ids,
  708. .driver = {
  709. .owner = THIS_MODULE,
  710. .name = "s3c-sdhci",
  711. .of_match_table = of_match_ptr(sdhci_s3c_dt_match),
  712. .pm = SDHCI_S3C_PMOPS,
  713. },
  714. };
  715. module_platform_driver(sdhci_s3c_driver);
  716. MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
  717. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  718. MODULE_LICENSE("GPL v2");
  719. MODULE_ALIAS("platform:s3c-sdhci");