4 * Copyright (C) 2009 Magnus Damm
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Based on "Compaq ASIC3 support":
12 * Copyright 2001 Compaq Computer Corporation.
13 * Copyright 2004-2005 Phil Blundell
14 * Copyright 2007-2008 OpenedHand Ltd.
16 * Authors: Phil Blundell <pb@handhelds.org>,
17 * Samuel Ortiz <sameo@openedhand.com>
21 #include <linux/kernel.h>
22 #include <linux/clk.h>
23 #include <linux/slab.h>
24 #include <linux/mod_devicetable.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/sh_mobile_sdhi.h>
30 #include <linux/mfd/tmio.h>
31 #include <linux/sh_dma.h>
32 #include <linux/delay.h>
38 struct sh_mobile_sdhi_of_data {
39 unsigned long tmio_flags;
40 unsigned long capabilities;
41 unsigned long capabilities2;
42 dma_addr_t dma_rx_offset;
45 static const struct sh_mobile_sdhi_of_data sh_mobile_sdhi_of_cfg[] = {
47 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
51 static const struct sh_mobile_sdhi_of_data of_rcar_gen1_compatible = {
52 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE,
53 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
56 static const struct sh_mobile_sdhi_of_data of_rcar_gen2_compatible = {
57 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE,
58 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
59 .capabilities2 = MMC_CAP2_NO_MULTI_READ,
60 .dma_rx_offset = 0x2000,
63 static const struct of_device_id sh_mobile_sdhi_of_match[] = {
64 { .compatible = "renesas,sdhi-shmobile" },
65 { .compatible = "renesas,sdhi-sh7372" },
66 { .compatible = "renesas,sdhi-sh73a0", .data = &sh_mobile_sdhi_of_cfg[0], },
67 { .compatible = "renesas,sdhi-r8a73a4", .data = &sh_mobile_sdhi_of_cfg[0], },
68 { .compatible = "renesas,sdhi-r8a7740", .data = &sh_mobile_sdhi_of_cfg[0], },
69 { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
70 { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
71 { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
72 { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
75 MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
77 struct sh_mobile_sdhi {
79 struct tmio_mmc_data mmc_data;
80 struct tmio_mmc_dma dma_priv;
83 static int sh_mobile_sdhi_clk_enable(struct platform_device *pdev, unsigned int *f)
85 struct mmc_host *mmc = platform_get_drvdata(pdev);
86 struct tmio_mmc_host *host = mmc_priv(mmc);
87 struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
88 int ret = clk_prepare_enable(priv->clk);
92 *f = clk_get_rate(priv->clk);
96 static void sh_mobile_sdhi_clk_disable(struct platform_device *pdev)
98 struct mmc_host *mmc = platform_get_drvdata(pdev);
99 struct tmio_mmc_host *host = mmc_priv(mmc);
100 struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
101 clk_disable_unprepare(priv->clk);
104 static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
108 while (--timeout && !(sd_ctrl_read16(host, CTL_STATUS2) & (1 << 13)))
112 dev_warn(host->pdata->dev, "timeout waiting for SD bus idle\n");
119 static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
124 case CTL_STOP_INTERNAL_ACTION:
125 case CTL_XFER_BLK_COUNT:
126 case CTL_SD_CARD_CLK_CTL:
127 case CTL_SD_XFER_LEN:
128 case CTL_SD_MEM_CARD_OPT:
129 case CTL_TRANSACTION_CTL:
131 return sh_mobile_sdhi_wait_idle(host);
137 static void sh_mobile_sdhi_cd_wakeup(const struct platform_device *pdev)
139 mmc_detect_change(platform_get_drvdata(pdev), msecs_to_jiffies(100));
142 static const struct sh_mobile_sdhi_ops sdhi_ops = {
143 .cd_wakeup = sh_mobile_sdhi_cd_wakeup,
146 static int sh_mobile_sdhi_probe(struct platform_device *pdev)
148 const struct of_device_id *of_id =
149 of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
150 struct sh_mobile_sdhi *priv;
151 struct tmio_mmc_data *mmc_data;
152 struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
153 struct tmio_mmc_host *host;
154 struct resource *res;
156 bool multiplexed_isr = true;
157 struct tmio_mmc_dma *dma_priv;
160 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
164 priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
166 dev_err(&pdev->dev, "kzalloc failed\n");
170 mmc_data = &priv->mmc_data;
171 dma_priv = &priv->dma_priv;
175 ret = p->init(pdev, &sdhi_ops);
181 priv->clk = devm_clk_get(&pdev->dev, NULL);
182 if (IS_ERR(priv->clk)) {
183 ret = PTR_ERR(priv->clk);
184 dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
188 mmc_data->clk_enable = sh_mobile_sdhi_clk_enable;
189 mmc_data->clk_disable = sh_mobile_sdhi_clk_disable;
190 mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
191 mmc_data->write16_hook = sh_mobile_sdhi_write16_hook;
193 mmc_data->flags = p->tmio_flags;
194 mmc_data->ocr_mask = p->tmio_ocr_mask;
195 mmc_data->capabilities |= p->tmio_caps;
196 mmc_data->capabilities2 |= p->tmio_caps2;
197 mmc_data->cd_gpio = p->cd_gpio;
199 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) {
201 * Yes, we have to provide slave IDs twice to TMIO:
202 * once as a filter parameter and once for channel
203 * configuration as an explicit slave ID
205 dma_priv->chan_priv_tx = (void *)p->dma_slave_tx;
206 dma_priv->chan_priv_rx = (void *)p->dma_slave_rx;
207 dma_priv->slave_id_tx = p->dma_slave_tx;
208 dma_priv->slave_id_rx = p->dma_slave_rx;
212 dma_priv->alignment_shift = 1; /* 2-byte alignment */
213 dma_priv->filter = shdma_chan_filter;
215 mmc_data->dma = dma_priv;
218 * All SDHI blocks support 2-byte and larger block sizes in 4-bit
221 mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
224 * All SDHI blocks support SDIO IRQ signalling.
226 mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
229 * All SDHI have CMD12 controll bit
231 mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
233 if (of_id && of_id->data) {
234 const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
235 mmc_data->flags |= of_data->tmio_flags;
236 mmc_data->capabilities |= of_data->capabilities;
237 mmc_data->capabilities2 |= of_data->capabilities2;
238 dma_priv->dma_rx_offset = of_data->dma_rx_offset;
241 /* SD control register space size is 0x100, 0x200 for bus_shift=1 */
242 mmc_data->bus_shift = resource_size(res) >> 9;
244 ret = tmio_mmc_host_probe(&host, pdev, mmc_data);
250 * this Workaround can be more clever method
252 ver = sd_ctrl_read16(host, CTL_VERSION);
254 sd_ctrl_write16(host, EXT_ACC, 1);
257 * Allow one or more specific (named) ISRs or
258 * one or more multiplexed (un-named) ISRs.
261 irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
263 multiplexed_isr = false;
264 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_card_detect_irq, 0,
265 dev_name(&pdev->dev), host);
270 irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
272 multiplexed_isr = false;
273 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdio_irq, 0,
274 dev_name(&pdev->dev), host);
279 irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD);
281 multiplexed_isr = false;
282 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdcard_irq, 0,
283 dev_name(&pdev->dev), host);
286 } else if (!multiplexed_isr) {
288 "Principal SD-card IRQ is missing among named interrupts\n");
293 if (multiplexed_isr) {
295 irq = platform_get_irq(pdev, i);
299 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
300 dev_name(&pdev->dev), host);
305 /* There must be at least one IRQ source */
312 dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
313 mmc_hostname(host->mmc), (unsigned long)
314 (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
315 host->mmc->f_max / 1000000);
320 tmio_mmc_host_remove(host);
328 static int sh_mobile_sdhi_remove(struct platform_device *pdev)
330 struct mmc_host *mmc = platform_get_drvdata(pdev);
331 struct tmio_mmc_host *host = mmc_priv(mmc);
332 struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
334 tmio_mmc_host_remove(host);
342 static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
343 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
344 pm_runtime_force_resume)
345 SET_PM_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
346 tmio_mmc_host_runtime_resume,
350 static struct platform_driver sh_mobile_sdhi_driver = {
352 .name = "sh_mobile_sdhi",
353 .owner = THIS_MODULE,
354 .pm = &tmio_mmc_dev_pm_ops,
355 .of_match_table = sh_mobile_sdhi_of_match,
357 .probe = sh_mobile_sdhi_probe,
358 .remove = sh_mobile_sdhi_remove,
361 module_platform_driver(sh_mobile_sdhi_driver);
363 MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
364 MODULE_AUTHOR("Magnus Damm");
365 MODULE_LICENSE("GPL v2");
366 MODULE_ALIAS("platform:sh_mobile_sdhi");