sdhci-s3c.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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_8bit_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_8bit_width() and support 8bit width.
  286. */
  287. static int sdhci_s3c_platform_8bit_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_8bit_width = sdhci_s3c_platform_8bit_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. struct sdhci_s3c *sc = sdhci_priv(host);
  318. unsigned long flags;
  319. if (host) {
  320. spin_lock_irqsave(&host->lock, flags);
  321. if (state) {
  322. dev_dbg(&dev->dev, "card inserted.\n");
  323. #ifdef CONFIG_PM_RUNTIME
  324. clk_prepare_enable(sc->clk_io);
  325. #endif
  326. host->flags &= ~SDHCI_DEVICE_DEAD;
  327. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  328. } else {
  329. dev_dbg(&dev->dev, "card removed.\n");
  330. host->flags |= SDHCI_DEVICE_DEAD;
  331. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  332. #ifdef CONFIG_PM_RUNTIME
  333. clk_disable_unprepare(sc->clk_io);
  334. #endif
  335. }
  336. tasklet_schedule(&host->card_tasklet);
  337. spin_unlock_irqrestore(&host->lock, flags);
  338. }
  339. }
  340. static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
  341. {
  342. struct sdhci_s3c *sc = dev_id;
  343. int status = gpio_get_value(sc->ext_cd_gpio);
  344. if (sc->pdata->ext_cd_gpio_invert)
  345. status = !status;
  346. sdhci_s3c_notify_change(sc->pdev, status);
  347. return IRQ_HANDLED;
  348. }
  349. static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
  350. {
  351. struct s3c_sdhci_platdata *pdata = sc->pdata;
  352. struct device *dev = &sc->pdev->dev;
  353. if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
  354. sc->ext_cd_gpio = pdata->ext_cd_gpio;
  355. sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
  356. if (sc->ext_cd_irq &&
  357. request_threaded_irq(sc->ext_cd_irq, NULL,
  358. sdhci_s3c_gpio_card_detect_thread,
  359. IRQF_TRIGGER_RISING |
  360. IRQF_TRIGGER_FALLING |
  361. IRQF_ONESHOT,
  362. dev_name(dev), sc) == 0) {
  363. int status = gpio_get_value(sc->ext_cd_gpio);
  364. if (pdata->ext_cd_gpio_invert)
  365. status = !status;
  366. sdhci_s3c_notify_change(sc->pdev, status);
  367. } else {
  368. dev_warn(dev, "cannot request irq for card detect\n");
  369. sc->ext_cd_irq = 0;
  370. }
  371. } else {
  372. dev_err(dev, "cannot request gpio for card detect\n");
  373. }
  374. }
  375. #ifdef CONFIG_OF
  376. static int __devinit sdhci_s3c_parse_dt(struct device *dev,
  377. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  378. {
  379. struct device_node *node = dev->of_node;
  380. struct sdhci_s3c *ourhost = to_s3c(host);
  381. u32 max_width;
  382. int gpio, cnt, ret;
  383. /* if the bus-width property is not specified, assume width as 1 */
  384. if (of_property_read_u32(node, "bus-width", &max_width))
  385. max_width = 1;
  386. pdata->max_width = max_width;
  387. ourhost->gpios = devm_kzalloc(dev, NUM_GPIOS(pdata->max_width) *
  388. sizeof(int), GFP_KERNEL);
  389. if (!ourhost->gpios)
  390. return -ENOMEM;
  391. /* get the card detection method */
  392. if (of_get_property(node, "broken-cd", NULL)) {
  393. pdata->cd_type = S3C_SDHCI_CD_NONE;
  394. goto setup_bus;
  395. }
  396. if (of_get_property(node, "non-removable", NULL)) {
  397. pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
  398. goto setup_bus;
  399. }
  400. gpio = of_get_named_gpio(node, "cd-gpios", 0);
  401. if (gpio_is_valid(gpio)) {
  402. pdata->cd_type = S3C_SDHCI_CD_GPIO;
  403. goto found_cd;
  404. } else if (gpio != -ENOENT) {
  405. dev_err(dev, "invalid card detect gpio specified\n");
  406. return -EINVAL;
  407. }
  408. gpio = of_get_named_gpio(node, "samsung,cd-pinmux-gpio", 0);
  409. if (gpio_is_valid(gpio)) {
  410. pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
  411. goto found_cd;
  412. } else if (gpio != -ENOENT) {
  413. dev_err(dev, "invalid card detect gpio specified\n");
  414. return -EINVAL;
  415. }
  416. /* assuming internal card detect that will be configured by pinctrl */
  417. pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
  418. goto setup_bus;
  419. found_cd:
  420. if (pdata->cd_type == S3C_SDHCI_CD_GPIO) {
  421. pdata->ext_cd_gpio = gpio;
  422. ourhost->ext_cd_gpio = -1;
  423. if (of_get_property(node, "cd-inverted", NULL))
  424. pdata->ext_cd_gpio_invert = 1;
  425. } else if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL) {
  426. ret = devm_gpio_request(dev, gpio, "sdhci-cd");
  427. if (ret) {
  428. dev_err(dev, "card detect gpio request failed\n");
  429. return -EINVAL;
  430. }
  431. ourhost->ext_cd_gpio = gpio;
  432. }
  433. setup_bus:
  434. if (!IS_ERR(ourhost->pctrl))
  435. return 0;
  436. /* get the gpios for command, clock and data lines */
  437. for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
  438. gpio = of_get_gpio(node, cnt);
  439. if (!gpio_is_valid(gpio)) {
  440. dev_err(dev, "invalid gpio[%d]\n", cnt);
  441. return -EINVAL;
  442. }
  443. ourhost->gpios[cnt] = gpio;
  444. }
  445. for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
  446. ret = devm_gpio_request(dev, ourhost->gpios[cnt], "sdhci-gpio");
  447. if (ret) {
  448. dev_err(dev, "gpio[%d] request failed\n", cnt);
  449. return -EINVAL;
  450. }
  451. }
  452. return 0;
  453. }
  454. #else
  455. static int __devinit sdhci_s3c_parse_dt(struct device *dev,
  456. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  457. {
  458. return -EINVAL;
  459. }
  460. #endif
  461. static const struct of_device_id sdhci_s3c_dt_match[];
  462. static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
  463. struct platform_device *pdev)
  464. {
  465. #ifdef CONFIG_OF
  466. if (pdev->dev.of_node) {
  467. const struct of_device_id *match;
  468. match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
  469. return (struct sdhci_s3c_drv_data *)match->data;
  470. }
  471. #endif
  472. return (struct sdhci_s3c_drv_data *)
  473. platform_get_device_id(pdev)->driver_data;
  474. }
  475. static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
  476. {
  477. struct s3c_sdhci_platdata *pdata;
  478. struct sdhci_s3c_drv_data *drv_data;
  479. struct device *dev = &pdev->dev;
  480. struct sdhci_host *host;
  481. struct sdhci_s3c *sc;
  482. struct resource *res;
  483. int ret, irq, ptr, clks;
  484. if (!pdev->dev.platform_data && !pdev->dev.of_node) {
  485. dev_err(dev, "no device data specified\n");
  486. return -ENOENT;
  487. }
  488. irq = platform_get_irq(pdev, 0);
  489. if (irq < 0) {
  490. dev_err(dev, "no irq specified\n");
  491. return irq;
  492. }
  493. host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
  494. if (IS_ERR(host)) {
  495. dev_err(dev, "sdhci_alloc_host() failed\n");
  496. return PTR_ERR(host);
  497. }
  498. sc = sdhci_priv(host);
  499. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  500. if (!pdata) {
  501. ret = -ENOMEM;
  502. goto err_pdata_io_clk;
  503. }
  504. sc->pctrl = devm_pinctrl_get_select_default(&pdev->dev);
  505. if (pdev->dev.of_node) {
  506. ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
  507. if (ret)
  508. goto err_pdata_io_clk;
  509. } else {
  510. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  511. sc->ext_cd_gpio = -1; /* invalid gpio number */
  512. }
  513. drv_data = sdhci_s3c_get_driver_data(pdev);
  514. sc->host = host;
  515. sc->pdev = pdev;
  516. sc->pdata = pdata;
  517. platform_set_drvdata(pdev, host);
  518. sc->clk_io = clk_get(dev, "hsmmc");
  519. if (IS_ERR(sc->clk_io)) {
  520. dev_err(dev, "failed to get io clock\n");
  521. ret = PTR_ERR(sc->clk_io);
  522. goto err_pdata_io_clk;
  523. }
  524. /* enable the local io clock and keep it running for the moment. */
  525. clk_prepare_enable(sc->clk_io);
  526. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  527. struct clk *clk;
  528. char name[14];
  529. snprintf(name, 14, "mmc_busclk.%d", ptr);
  530. clk = clk_get(dev, name);
  531. if (IS_ERR(clk))
  532. continue;
  533. clks++;
  534. sc->clk_bus[ptr] = clk;
  535. /*
  536. * save current clock index to know which clock bus
  537. * is used later in overriding functions.
  538. */
  539. sc->cur_clk = ptr;
  540. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  541. ptr, name, clk_get_rate(clk));
  542. }
  543. if (clks == 0) {
  544. dev_err(dev, "failed to find any bus clocks\n");
  545. ret = -ENOENT;
  546. goto err_no_busclks;
  547. }
  548. #ifndef CONFIG_PM_RUNTIME
  549. clk_prepare_enable(sc->clk_bus[sc->cur_clk]);
  550. #endif
  551. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  552. host->ioaddr = devm_request_and_ioremap(&pdev->dev, res);
  553. if (!host->ioaddr) {
  554. dev_err(dev, "failed to map registers\n");
  555. ret = -ENXIO;
  556. goto err_req_regs;
  557. }
  558. /* Ensure we have minimal gpio selected CMD/CLK/Detect */
  559. if (pdata->cfg_gpio)
  560. pdata->cfg_gpio(pdev, pdata->max_width);
  561. host->hw_name = "samsung-hsmmc";
  562. host->ops = &sdhci_s3c_ops;
  563. host->quirks = 0;
  564. host->irq = irq;
  565. /* Setup quirks for the controller */
  566. host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
  567. host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
  568. if (drv_data)
  569. host->quirks |= drv_data->sdhci_quirks;
  570. #ifndef CONFIG_MMC_SDHCI_S3C_DMA
  571. /* we currently see overruns on errors, so disable the SDMA
  572. * support as well. */
  573. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  574. #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
  575. /* It seems we do not get an DATA transfer complete on non-busy
  576. * transfers, not sure if this is a problem with this specific
  577. * SDHCI block, or a missing configuration that needs to be set. */
  578. host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
  579. /* This host supports the Auto CMD12 */
  580. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  581. /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
  582. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
  583. if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
  584. pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  585. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  586. if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  587. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  588. switch (pdata->max_width) {
  589. case 8:
  590. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  591. case 4:
  592. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  593. break;
  594. }
  595. if (pdata->pm_caps)
  596. host->mmc->pm_caps |= pdata->pm_caps;
  597. host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
  598. SDHCI_QUIRK_32BIT_DMA_SIZE);
  599. /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
  600. host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
  601. /*
  602. * If controller does not have internal clock divider,
  603. * we can use overriding functions instead of default.
  604. */
  605. if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
  606. sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
  607. sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
  608. sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
  609. }
  610. /* It supports additional host capabilities if needed */
  611. if (pdata->host_caps)
  612. host->mmc->caps |= pdata->host_caps;
  613. if (pdata->host_caps2)
  614. host->mmc->caps2 |= pdata->host_caps2;
  615. pm_runtime_enable(&pdev->dev);
  616. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  617. pm_runtime_use_autosuspend(&pdev->dev);
  618. pm_suspend_ignore_children(&pdev->dev, 1);
  619. ret = sdhci_add_host(host);
  620. if (ret) {
  621. dev_err(dev, "sdhci_add_host() failed\n");
  622. pm_runtime_forbid(&pdev->dev);
  623. pm_runtime_get_noresume(&pdev->dev);
  624. goto err_req_regs;
  625. }
  626. /* The following two methods of card detection might call
  627. sdhci_s3c_notify_change() immediately, so they can be called
  628. only after sdhci_add_host(). Setup errors are ignored. */
  629. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
  630. pdata->ext_cd_init(&sdhci_s3c_notify_change);
  631. if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
  632. gpio_is_valid(pdata->ext_cd_gpio))
  633. sdhci_s3c_setup_card_detect_gpio(sc);
  634. #ifdef CONFIG_PM_RUNTIME
  635. if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  636. clk_disable_unprepare(sc->clk_io);
  637. #endif
  638. return 0;
  639. err_req_regs:
  640. #ifndef CONFIG_PM_RUNTIME
  641. clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
  642. #endif
  643. for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  644. if (sc->clk_bus[ptr]) {
  645. clk_put(sc->clk_bus[ptr]);
  646. }
  647. }
  648. err_no_busclks:
  649. clk_disable_unprepare(sc->clk_io);
  650. clk_put(sc->clk_io);
  651. err_pdata_io_clk:
  652. sdhci_free_host(host);
  653. return ret;
  654. }
  655. static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
  656. {
  657. struct sdhci_host *host = platform_get_drvdata(pdev);
  658. struct sdhci_s3c *sc = sdhci_priv(host);
  659. struct s3c_sdhci_platdata *pdata = sc->pdata;
  660. int ptr;
  661. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
  662. pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
  663. if (sc->ext_cd_irq)
  664. free_irq(sc->ext_cd_irq, sc);
  665. #ifdef CONFIG_PM_RUNTIME
  666. if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  667. clk_prepare_enable(sc->clk_io);
  668. #endif
  669. sdhci_remove_host(host, 1);
  670. pm_runtime_dont_use_autosuspend(&pdev->dev);
  671. pm_runtime_disable(&pdev->dev);
  672. #ifndef CONFIG_PM_RUNTIME
  673. clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
  674. #endif
  675. for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  676. if (sc->clk_bus[ptr]) {
  677. clk_put(sc->clk_bus[ptr]);
  678. }
  679. }
  680. clk_disable_unprepare(sc->clk_io);
  681. clk_put(sc->clk_io);
  682. sdhci_free_host(host);
  683. platform_set_drvdata(pdev, NULL);
  684. return 0;
  685. }
  686. #ifdef CONFIG_PM_SLEEP
  687. static int sdhci_s3c_suspend(struct device *dev)
  688. {
  689. struct sdhci_host *host = dev_get_drvdata(dev);
  690. return sdhci_suspend_host(host);
  691. }
  692. static int sdhci_s3c_resume(struct device *dev)
  693. {
  694. struct sdhci_host *host = dev_get_drvdata(dev);
  695. return sdhci_resume_host(host);
  696. }
  697. #endif
  698. #ifdef CONFIG_PM_RUNTIME
  699. static int sdhci_s3c_runtime_suspend(struct device *dev)
  700. {
  701. struct sdhci_host *host = dev_get_drvdata(dev);
  702. struct sdhci_s3c *ourhost = to_s3c(host);
  703. struct clk *busclk = ourhost->clk_io;
  704. int ret;
  705. ret = sdhci_runtime_suspend_host(host);
  706. clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
  707. clk_disable_unprepare(busclk);
  708. return ret;
  709. }
  710. static int sdhci_s3c_runtime_resume(struct device *dev)
  711. {
  712. struct sdhci_host *host = dev_get_drvdata(dev);
  713. struct sdhci_s3c *ourhost = to_s3c(host);
  714. struct clk *busclk = ourhost->clk_io;
  715. int ret;
  716. clk_prepare_enable(busclk);
  717. clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
  718. ret = sdhci_runtime_resume_host(host);
  719. return ret;
  720. }
  721. #endif
  722. #ifdef CONFIG_PM
  723. static const struct dev_pm_ops sdhci_s3c_pmops = {
  724. SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
  725. SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
  726. NULL)
  727. };
  728. #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
  729. #else
  730. #define SDHCI_S3C_PMOPS NULL
  731. #endif
  732. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
  733. static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
  734. .sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
  735. };
  736. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
  737. #else
  738. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
  739. #endif
  740. static struct platform_device_id sdhci_s3c_driver_ids[] = {
  741. {
  742. .name = "s3c-sdhci",
  743. .driver_data = (kernel_ulong_t)NULL,
  744. }, {
  745. .name = "exynos4-sdhci",
  746. .driver_data = EXYNOS4_SDHCI_DRV_DATA,
  747. },
  748. { }
  749. };
  750. MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
  751. #ifdef CONFIG_OF
  752. static const struct of_device_id sdhci_s3c_dt_match[] = {
  753. { .compatible = "samsung,s3c6410-sdhci", },
  754. { .compatible = "samsung,exynos4210-sdhci",
  755. .data = (void *)EXYNOS4_SDHCI_DRV_DATA },
  756. {},
  757. };
  758. MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
  759. #endif
  760. static struct platform_driver sdhci_s3c_driver = {
  761. .probe = sdhci_s3c_probe,
  762. .remove = __devexit_p(sdhci_s3c_remove),
  763. .id_table = sdhci_s3c_driver_ids,
  764. .driver = {
  765. .owner = THIS_MODULE,
  766. .name = "s3c-sdhci",
  767. .of_match_table = of_match_ptr(sdhci_s3c_dt_match),
  768. .pm = SDHCI_S3C_PMOPS,
  769. },
  770. };
  771. module_platform_driver(sdhci_s3c_driver);
  772. MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
  773. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  774. MODULE_LICENSE("GPL v2");
  775. MODULE_ALIAS("platform:s3c-sdhci");