sdhci-s3c.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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/mmc/host.h>
  27. #include <plat/sdhci.h>
  28. #include <plat/regs-sdhci.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. * @gpios: List of gpio numbers parsed from device tree.
  41. * @clk_io: The clock for the internal bus interface.
  42. * @clk_bus: The clocks that are available for the SD/MMC bus clock.
  43. */
  44. struct sdhci_s3c {
  45. struct sdhci_host *host;
  46. struct platform_device *pdev;
  47. struct resource *ioarea;
  48. struct s3c_sdhci_platdata *pdata;
  49. unsigned int cur_clk;
  50. int ext_cd_irq;
  51. int ext_cd_gpio;
  52. int *gpios;
  53. struct clk *clk_io;
  54. struct clk *clk_bus[MAX_BUS_CLK];
  55. };
  56. /**
  57. * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
  58. * @sdhci_quirks: sdhci host specific quirks.
  59. *
  60. * Specifies platform specific configuration of sdhci controller.
  61. * Note: A structure for driver specific platform data is used for future
  62. * expansion of its usage.
  63. */
  64. struct sdhci_s3c_drv_data {
  65. unsigned int sdhci_quirks;
  66. };
  67. static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
  68. {
  69. return sdhci_priv(host);
  70. }
  71. /**
  72. * get_curclk - convert ctrl2 register to clock source number
  73. * @ctrl2: Control2 register value.
  74. */
  75. static u32 get_curclk(u32 ctrl2)
  76. {
  77. ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  78. ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  79. return ctrl2;
  80. }
  81. static void sdhci_s3c_check_sclk(struct sdhci_host *host)
  82. {
  83. struct sdhci_s3c *ourhost = to_s3c(host);
  84. u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  85. if (get_curclk(tmp) != ourhost->cur_clk) {
  86. dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
  87. tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  88. tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  89. writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
  90. }
  91. }
  92. /**
  93. * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
  94. * @host: The SDHCI host instance.
  95. *
  96. * Callback to return the maximum clock rate acheivable by the controller.
  97. */
  98. static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
  99. {
  100. struct sdhci_s3c *ourhost = to_s3c(host);
  101. struct clk *busclk;
  102. unsigned int rate, max;
  103. int clk;
  104. /* note, a reset will reset the clock source */
  105. sdhci_s3c_check_sclk(host);
  106. for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
  107. busclk = ourhost->clk_bus[clk];
  108. if (!busclk)
  109. continue;
  110. rate = clk_get_rate(busclk);
  111. if (rate > max)
  112. max = rate;
  113. }
  114. return max;
  115. }
  116. /**
  117. * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
  118. * @ourhost: Our SDHCI instance.
  119. * @src: The source clock index.
  120. * @wanted: The clock frequency wanted.
  121. */
  122. static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
  123. unsigned int src,
  124. unsigned int wanted)
  125. {
  126. unsigned long rate;
  127. struct clk *clksrc = ourhost->clk_bus[src];
  128. int div;
  129. if (!clksrc)
  130. return UINT_MAX;
  131. /*
  132. * If controller uses a non-standard clock division, find the best clock
  133. * speed possible with selected clock source and skip the division.
  134. */
  135. if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
  136. rate = clk_round_rate(clksrc, wanted);
  137. return wanted - rate;
  138. }
  139. rate = clk_get_rate(clksrc);
  140. for (div = 1; div < 256; div *= 2) {
  141. if ((rate / div) <= wanted)
  142. break;
  143. }
  144. dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
  145. src, rate, wanted, rate / div);
  146. return wanted - (rate / div);
  147. }
  148. /**
  149. * sdhci_s3c_set_clock - callback on clock change
  150. * @host: The SDHCI host being changed
  151. * @clock: The clock rate being requested.
  152. *
  153. * When the card's clock is going to be changed, look at the new frequency
  154. * and find the best clock source to go with it.
  155. */
  156. static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
  157. {
  158. struct sdhci_s3c *ourhost = to_s3c(host);
  159. unsigned int best = UINT_MAX;
  160. unsigned int delta;
  161. int best_src = 0;
  162. int src;
  163. u32 ctrl;
  164. /* don't bother if the clock is going off. */
  165. if (clock == 0)
  166. return;
  167. for (src = 0; src < MAX_BUS_CLK; src++) {
  168. delta = sdhci_s3c_consider_clock(ourhost, src, clock);
  169. if (delta < best) {
  170. best = delta;
  171. best_src = src;
  172. }
  173. }
  174. dev_dbg(&ourhost->pdev->dev,
  175. "selected source %d, clock %d, delta %d\n",
  176. best_src, clock, best);
  177. /* select the new clock source */
  178. if (ourhost->cur_clk != best_src) {
  179. struct clk *clk = ourhost->clk_bus[best_src];
  180. clk_prepare_enable(clk);
  181. clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
  182. /* turn clock off to card before changing clock source */
  183. writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
  184. ourhost->cur_clk = best_src;
  185. host->max_clk = clk_get_rate(clk);
  186. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  187. ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  188. ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  189. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  190. }
  191. /* reprogram default hardware configuration */
  192. writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
  193. host->ioaddr + S3C64XX_SDHCI_CONTROL4);
  194. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  195. ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
  196. S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
  197. S3C_SDHCI_CTRL2_ENFBCLKRX |
  198. S3C_SDHCI_CTRL2_DFCNT_NONE |
  199. S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
  200. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  201. /* reconfigure the controller for new clock rate */
  202. ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
  203. if (clock < 25 * 1000000)
  204. ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
  205. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
  206. }
  207. /**
  208. * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
  209. * @host: The SDHCI host being queried
  210. *
  211. * To init mmc host properly a minimal clock value is needed. For high system
  212. * bus clock's values the standard formula gives values out of allowed range.
  213. * The clock still can be set to lower values, if clock source other then
  214. * system bus is selected.
  215. */
  216. static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
  217. {
  218. struct sdhci_s3c *ourhost = to_s3c(host);
  219. unsigned int delta, min = UINT_MAX;
  220. int src;
  221. for (src = 0; src < MAX_BUS_CLK; src++) {
  222. delta = sdhci_s3c_consider_clock(ourhost, src, 0);
  223. if (delta == UINT_MAX)
  224. continue;
  225. /* delta is a negative value in this case */
  226. if (-delta < min)
  227. min = -delta;
  228. }
  229. return min;
  230. }
  231. /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
  232. static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
  233. {
  234. struct sdhci_s3c *ourhost = to_s3c(host);
  235. return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
  236. }
  237. /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
  238. static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
  239. {
  240. struct sdhci_s3c *ourhost = to_s3c(host);
  241. /*
  242. * initial clock can be in the frequency range of
  243. * 100KHz-400KHz, so we set it as max value.
  244. */
  245. return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
  246. }
  247. /* sdhci_cmu_set_clock - callback on clock change.*/
  248. static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
  249. {
  250. struct sdhci_s3c *ourhost = to_s3c(host);
  251. struct device *dev = &ourhost->pdev->dev;
  252. unsigned long timeout;
  253. u16 clk = 0;
  254. /* don't bother if the clock is going off */
  255. if (clock == 0)
  256. return;
  257. sdhci_s3c_set_clock(host, clock);
  258. clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
  259. host->clock = clock;
  260. clk = SDHCI_CLOCK_INT_EN;
  261. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  262. /* Wait max 20 ms */
  263. timeout = 20;
  264. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  265. & SDHCI_CLOCK_INT_STABLE)) {
  266. if (timeout == 0) {
  267. dev_err(dev, "%s: Internal clock never stabilised.\n",
  268. mmc_hostname(host->mmc));
  269. return;
  270. }
  271. timeout--;
  272. mdelay(1);
  273. }
  274. clk |= SDHCI_CLOCK_CARD_EN;
  275. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  276. }
  277. /**
  278. * sdhci_s3c_platform_8bit_width - support 8bit buswidth
  279. * @host: The SDHCI host being queried
  280. * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
  281. *
  282. * We have 8-bit width support but is not a v3 controller.
  283. * So we add platform_8bit_width() and support 8bit width.
  284. */
  285. static int sdhci_s3c_platform_8bit_width(struct sdhci_host *host, int width)
  286. {
  287. u8 ctrl;
  288. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  289. switch (width) {
  290. case MMC_BUS_WIDTH_8:
  291. ctrl |= SDHCI_CTRL_8BITBUS;
  292. ctrl &= ~SDHCI_CTRL_4BITBUS;
  293. break;
  294. case MMC_BUS_WIDTH_4:
  295. ctrl |= SDHCI_CTRL_4BITBUS;
  296. ctrl &= ~SDHCI_CTRL_8BITBUS;
  297. break;
  298. default:
  299. ctrl &= ~SDHCI_CTRL_4BITBUS;
  300. ctrl &= ~SDHCI_CTRL_8BITBUS;
  301. break;
  302. }
  303. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  304. return 0;
  305. }
  306. static struct sdhci_ops sdhci_s3c_ops = {
  307. .get_max_clock = sdhci_s3c_get_max_clk,
  308. .set_clock = sdhci_s3c_set_clock,
  309. .get_min_clock = sdhci_s3c_get_min_clock,
  310. .platform_8bit_width = sdhci_s3c_platform_8bit_width,
  311. };
  312. static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
  313. {
  314. struct sdhci_host *host = platform_get_drvdata(dev);
  315. struct sdhci_s3c *sc = sdhci_priv(host);
  316. unsigned long flags;
  317. if (host) {
  318. spin_lock_irqsave(&host->lock, flags);
  319. if (state) {
  320. dev_dbg(&dev->dev, "card inserted.\n");
  321. #ifdef CONFIG_PM_RUNTIME
  322. clk_prepare_enable(sc->clk_io);
  323. #endif
  324. host->flags &= ~SDHCI_DEVICE_DEAD;
  325. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  326. } else {
  327. dev_dbg(&dev->dev, "card removed.\n");
  328. host->flags |= SDHCI_DEVICE_DEAD;
  329. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  330. #ifdef CONFIG_PM_RUNTIME
  331. clk_disable_unprepare(sc->clk_io);
  332. #endif
  333. }
  334. tasklet_schedule(&host->card_tasklet);
  335. spin_unlock_irqrestore(&host->lock, flags);
  336. }
  337. }
  338. static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
  339. {
  340. struct sdhci_s3c *sc = dev_id;
  341. int status = gpio_get_value(sc->ext_cd_gpio);
  342. if (sc->pdata->ext_cd_gpio_invert)
  343. status = !status;
  344. sdhci_s3c_notify_change(sc->pdev, status);
  345. return IRQ_HANDLED;
  346. }
  347. static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
  348. {
  349. struct s3c_sdhci_platdata *pdata = sc->pdata;
  350. struct device *dev = &sc->pdev->dev;
  351. if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
  352. sc->ext_cd_gpio = pdata->ext_cd_gpio;
  353. sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
  354. if (sc->ext_cd_irq &&
  355. request_threaded_irq(sc->ext_cd_irq, NULL,
  356. sdhci_s3c_gpio_card_detect_thread,
  357. IRQF_TRIGGER_RISING |
  358. IRQF_TRIGGER_FALLING |
  359. IRQF_ONESHOT,
  360. dev_name(dev), sc) == 0) {
  361. int status = gpio_get_value(sc->ext_cd_gpio);
  362. if (pdata->ext_cd_gpio_invert)
  363. status = !status;
  364. sdhci_s3c_notify_change(sc->pdev, status);
  365. } else {
  366. dev_warn(dev, "cannot request irq for card detect\n");
  367. sc->ext_cd_irq = 0;
  368. }
  369. } else {
  370. dev_err(dev, "cannot request gpio for card detect\n");
  371. }
  372. }
  373. #ifdef CONFIG_OF
  374. static int __devinit sdhci_s3c_parse_dt(struct device *dev,
  375. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  376. {
  377. struct device_node *node = dev->of_node;
  378. struct sdhci_s3c *ourhost = to_s3c(host);
  379. u32 max_width;
  380. int gpio, cnt, ret;
  381. /* if the bus-width property is not specified, assume width as 1 */
  382. if (of_property_read_u32(node, "bus-width", &max_width))
  383. max_width = 1;
  384. pdata->max_width = max_width;
  385. ourhost->gpios = devm_kzalloc(dev, NUM_GPIOS(pdata->max_width) *
  386. sizeof(int), GFP_KERNEL);
  387. if (!ourhost->gpios)
  388. return -ENOMEM;
  389. /* get the card detection method */
  390. if (of_get_property(node, "broken-cd", NULL)) {
  391. pdata->cd_type = S3C_SDHCI_CD_NONE;
  392. goto setup_bus;
  393. }
  394. if (of_get_property(node, "non-removable", NULL)) {
  395. pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
  396. goto setup_bus;
  397. }
  398. gpio = of_get_named_gpio(node, "cd-gpios", 0);
  399. if (gpio_is_valid(gpio)) {
  400. pdata->cd_type = S3C_SDHCI_CD_GPIO;
  401. goto found_cd;
  402. } else if (gpio != -ENOENT) {
  403. dev_err(dev, "invalid card detect gpio specified\n");
  404. return -EINVAL;
  405. }
  406. gpio = of_get_named_gpio(node, "samsung,cd-pinmux-gpio", 0);
  407. if (gpio_is_valid(gpio)) {
  408. pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
  409. goto found_cd;
  410. } else if (gpio != -ENOENT) {
  411. dev_err(dev, "invalid card detect gpio specified\n");
  412. return -EINVAL;
  413. }
  414. dev_info(dev, "assuming no card detect line available\n");
  415. pdata->cd_type = S3C_SDHCI_CD_NONE;
  416. found_cd:
  417. if (pdata->cd_type == S3C_SDHCI_CD_GPIO) {
  418. pdata->ext_cd_gpio = gpio;
  419. ourhost->ext_cd_gpio = -1;
  420. if (of_get_property(node, "cd-inverted", NULL))
  421. pdata->ext_cd_gpio_invert = 1;
  422. } else if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL) {
  423. ret = gpio_request(gpio, "sdhci-cd");
  424. if (ret) {
  425. dev_err(dev, "card detect gpio request failed\n");
  426. return -EINVAL;
  427. }
  428. ourhost->ext_cd_gpio = gpio;
  429. }
  430. setup_bus:
  431. /* get the gpios for command, clock and data lines */
  432. for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
  433. gpio = of_get_gpio(node, cnt);
  434. if (!gpio_is_valid(gpio)) {
  435. dev_err(dev, "invalid gpio[%d]\n", cnt);
  436. goto err_free_dt_cd_gpio;
  437. }
  438. ourhost->gpios[cnt] = gpio;
  439. }
  440. for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
  441. ret = gpio_request(ourhost->gpios[cnt], "sdhci-gpio");
  442. if (ret) {
  443. dev_err(dev, "gpio[%d] request failed\n", cnt);
  444. goto err_free_dt_gpios;
  445. }
  446. }
  447. return 0;
  448. err_free_dt_gpios:
  449. while (--cnt >= 0)
  450. gpio_free(ourhost->gpios[cnt]);
  451. err_free_dt_cd_gpio:
  452. if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL)
  453. gpio_free(ourhost->ext_cd_gpio);
  454. return -EINVAL;
  455. }
  456. #else
  457. static int __devinit 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 __devinit 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;
  505. }
  506. if (pdev->dev.of_node) {
  507. ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
  508. if (ret)
  509. goto err_pdata;
  510. } else {
  511. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  512. sc->ext_cd_gpio = -1; /* invalid gpio number */
  513. }
  514. drv_data = sdhci_s3c_get_driver_data(pdev);
  515. sc->host = host;
  516. sc->pdev = pdev;
  517. sc->pdata = pdata;
  518. platform_set_drvdata(pdev, host);
  519. sc->clk_io = clk_get(dev, "hsmmc");
  520. if (IS_ERR(sc->clk_io)) {
  521. dev_err(dev, "failed to get io clock\n");
  522. ret = PTR_ERR(sc->clk_io);
  523. goto err_io_clk;
  524. }
  525. /* enable the local io clock and keep it running for the moment. */
  526. clk_prepare_enable(sc->clk_io);
  527. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  528. struct clk *clk;
  529. char name[14];
  530. snprintf(name, 14, "mmc_busclk.%d", ptr);
  531. clk = clk_get(dev, name);
  532. if (IS_ERR(clk))
  533. continue;
  534. clks++;
  535. sc->clk_bus[ptr] = clk;
  536. /*
  537. * save current clock index to know which clock bus
  538. * is used later in overriding functions.
  539. */
  540. sc->cur_clk = ptr;
  541. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  542. ptr, name, clk_get_rate(clk));
  543. }
  544. if (clks == 0) {
  545. dev_err(dev, "failed to find any bus clocks\n");
  546. ret = -ENOENT;
  547. goto err_no_busclks;
  548. }
  549. #ifndef CONFIG_PM_RUNTIME
  550. clk_prepare_enable(sc->clk_bus[sc->cur_clk]);
  551. #endif
  552. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  553. host->ioaddr = devm_request_and_ioremap(&pdev->dev, res);
  554. if (!host->ioaddr) {
  555. dev_err(dev, "failed to map registers\n");
  556. ret = -ENXIO;
  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_io_clk:
  653. for (ptr = 0; ptr < NUM_GPIOS(sc->pdata->max_width); ptr++)
  654. gpio_free(sc->gpios[ptr]);
  655. if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL)
  656. gpio_free(sc->ext_cd_gpio);
  657. err_pdata:
  658. sdhci_free_host(host);
  659. return ret;
  660. }
  661. static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
  662. {
  663. struct sdhci_host *host = platform_get_drvdata(pdev);
  664. struct sdhci_s3c *sc = sdhci_priv(host);
  665. struct s3c_sdhci_platdata *pdata = sc->pdata;
  666. int ptr;
  667. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
  668. pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
  669. if (sc->ext_cd_irq)
  670. free_irq(sc->ext_cd_irq, sc);
  671. if (gpio_is_valid(sc->ext_cd_gpio))
  672. gpio_free(sc->ext_cd_gpio);
  673. #ifdef CONFIG_PM_RUNTIME
  674. if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  675. clk_prepare_enable(sc->clk_io);
  676. #endif
  677. sdhci_remove_host(host, 1);
  678. pm_runtime_dont_use_autosuspend(&pdev->dev);
  679. pm_runtime_disable(&pdev->dev);
  680. #ifndef CONFIG_PM_RUNTIME
  681. clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
  682. #endif
  683. for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  684. if (sc->clk_bus[ptr]) {
  685. clk_put(sc->clk_bus[ptr]);
  686. }
  687. }
  688. clk_disable_unprepare(sc->clk_io);
  689. clk_put(sc->clk_io);
  690. if (pdev->dev.of_node) {
  691. for (ptr = 0; ptr < NUM_GPIOS(sc->pdata->max_width); ptr++)
  692. gpio_free(sc->gpios[ptr]);
  693. }
  694. sdhci_free_host(host);
  695. platform_set_drvdata(pdev, NULL);
  696. return 0;
  697. }
  698. #ifdef CONFIG_PM_SLEEP
  699. static int sdhci_s3c_suspend(struct device *dev)
  700. {
  701. struct sdhci_host *host = dev_get_drvdata(dev);
  702. return sdhci_suspend_host(host);
  703. }
  704. static int sdhci_s3c_resume(struct device *dev)
  705. {
  706. struct sdhci_host *host = dev_get_drvdata(dev);
  707. return sdhci_resume_host(host);
  708. }
  709. #endif
  710. #ifdef CONFIG_PM_RUNTIME
  711. static int sdhci_s3c_runtime_suspend(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. ret = sdhci_runtime_suspend_host(host);
  718. clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
  719. clk_disable_unprepare(busclk);
  720. return ret;
  721. }
  722. static int sdhci_s3c_runtime_resume(struct device *dev)
  723. {
  724. struct sdhci_host *host = dev_get_drvdata(dev);
  725. struct sdhci_s3c *ourhost = to_s3c(host);
  726. struct clk *busclk = ourhost->clk_io;
  727. int ret;
  728. clk_prepare_enable(busclk);
  729. clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
  730. ret = sdhci_runtime_resume_host(host);
  731. return ret;
  732. }
  733. #endif
  734. #ifdef CONFIG_PM
  735. static const struct dev_pm_ops sdhci_s3c_pmops = {
  736. SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
  737. SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
  738. NULL)
  739. };
  740. #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
  741. #else
  742. #define SDHCI_S3C_PMOPS NULL
  743. #endif
  744. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
  745. static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
  746. .sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
  747. };
  748. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
  749. #else
  750. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
  751. #endif
  752. static struct platform_device_id sdhci_s3c_driver_ids[] = {
  753. {
  754. .name = "s3c-sdhci",
  755. .driver_data = (kernel_ulong_t)NULL,
  756. }, {
  757. .name = "exynos4-sdhci",
  758. .driver_data = EXYNOS4_SDHCI_DRV_DATA,
  759. },
  760. { }
  761. };
  762. MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
  763. #ifdef CONFIG_OF
  764. static const struct of_device_id sdhci_s3c_dt_match[] = {
  765. { .compatible = "samsung,s3c6410-sdhci", },
  766. { .compatible = "samsung,exynos4210-sdhci",
  767. .data = (void *)EXYNOS4_SDHCI_DRV_DATA },
  768. {},
  769. };
  770. MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
  771. #endif
  772. static struct platform_driver sdhci_s3c_driver = {
  773. .probe = sdhci_s3c_probe,
  774. .remove = __devexit_p(sdhci_s3c_remove),
  775. .id_table = sdhci_s3c_driver_ids,
  776. .driver = {
  777. .owner = THIS_MODULE,
  778. .name = "s3c-sdhci",
  779. .of_match_table = of_match_ptr(sdhci_s3c_dt_match),
  780. .pm = SDHCI_S3C_PMOPS,
  781. },
  782. };
  783. module_platform_driver(sdhci_s3c_driver);
  784. MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
  785. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  786. MODULE_LICENSE("GPL v2");
  787. MODULE_ALIAS("platform:s3c-sdhci");