sdhci-s3c.c 19 KB

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