sdhci-s3c.c 24 KB

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