sdhci-s3c.c 24 KB

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