Commit | Line | Data |
---|---|---|
a87d5638 MD |
1 | /* |
2 | * SuperH Mobile SDHI | |
3 | * | |
4 | * Copyright (C) 2009 Magnus Damm | |
5 | * | |
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. | |
9 | * | |
10 | * Based on "Compaq ASIC3 support": | |
11 | * | |
12 | * Copyright 2001 Compaq Computer Corporation. | |
13 | * Copyright 2004-2005 Phil Blundell | |
14 | * Copyright 2007-2008 OpenedHand Ltd. | |
15 | * | |
16 | * Authors: Phil Blundell <pb@handhelds.org>, | |
17 | * Samuel Ortiz <sameo@openedhand.com> | |
18 | * | |
19 | */ | |
20 | ||
21 | #include <linux/kernel.h> | |
22 | #include <linux/clk.h> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
c7bb4487 | 24 | #include <linux/mod_devicetable.h> |
88b47679 | 25 | #include <linux/module.h> |
5a00a971 | 26 | #include <linux/of_device.h> |
a87d5638 | 27 | #include <linux/platform_device.h> |
3c49e810 | 28 | #include <linux/mmc/host.h> |
42051e8a | 29 | #include <linux/mmc/sh_mobile_sdhi.h> |
a87d5638 | 30 | #include <linux/mfd/tmio.h> |
056676da | 31 | #include <linux/sh_dma.h> |
973ed3af | 32 | #include <linux/delay.h> |
a87d5638 | 33 | |
42051e8a GL |
34 | #include "tmio_mmc.h" |
35 | ||
5a00a971 GL |
36 | struct sh_mobile_sdhi_of_data { |
37 | unsigned long tmio_flags; | |
38 | }; | |
39 | ||
40 | static const struct sh_mobile_sdhi_of_data sh_mobile_sdhi_of_cfg[] = { | |
41 | { | |
42 | .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT, | |
43 | }, | |
44 | }; | |
45 | ||
a87d5638 MD |
46 | struct sh_mobile_sdhi { |
47 | struct clk *clk; | |
48 | struct tmio_mmc_data mmc_data; | |
056676da | 49 | struct tmio_mmc_dma dma_priv; |
a87d5638 MD |
50 | }; |
51 | ||
56c49287 GL |
52 | static int sh_mobile_sdhi_clk_enable(struct platform_device *pdev, unsigned int *f) |
53 | { | |
6e2c0f3f | 54 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
56c49287 GL |
55 | struct tmio_mmc_host *host = mmc_priv(mmc); |
56 | struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data); | |
57 | int ret = clk_enable(priv->clk); | |
58 | if (ret < 0) | |
59 | return ret; | |
60 | ||
61 | *f = clk_get_rate(priv->clk); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | static void sh_mobile_sdhi_clk_disable(struct platform_device *pdev) | |
66 | { | |
6e2c0f3f | 67 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
56c49287 GL |
68 | struct tmio_mmc_host *host = mmc_priv(mmc); |
69 | struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data); | |
70 | clk_disable(priv->clk); | |
71 | } | |
72 | ||
973ed3af SH |
73 | static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host) |
74 | { | |
75 | int timeout = 1000; | |
76 | ||
77 | while (--timeout && !(sd_ctrl_read16(host, CTL_STATUS2) & (1 << 13))) | |
78 | udelay(1); | |
79 | ||
80 | if (!timeout) { | |
81 | dev_warn(host->pdata->dev, "timeout waiting for SD bus idle\n"); | |
82 | return -EBUSY; | |
83 | } | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr) | |
89 | { | |
90 | switch (addr) | |
91 | { | |
92 | case CTL_SD_CMD: | |
93 | case CTL_STOP_INTERNAL_ACTION: | |
94 | case CTL_XFER_BLK_COUNT: | |
95 | case CTL_SD_CARD_CLK_CTL: | |
96 | case CTL_SD_XFER_LEN: | |
97 | case CTL_SD_MEM_CARD_OPT: | |
98 | case CTL_TRANSACTION_CTL: | |
99 | case CTL_DMA_ENABLE: | |
100 | return sh_mobile_sdhi_wait_idle(host); | |
101 | } | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
7f524217 GL |
106 | static void sh_mobile_sdhi_cd_wakeup(const struct platform_device *pdev) |
107 | { | |
6e2c0f3f | 108 | mmc_detect_change(platform_get_drvdata(pdev), msecs_to_jiffies(100)); |
7f524217 GL |
109 | } |
110 | ||
111 | static const struct sh_mobile_sdhi_ops sdhi_ops = { | |
112 | .cd_wakeup = sh_mobile_sdhi_cd_wakeup, | |
113 | }; | |
114 | ||
5a00a971 | 115 | static const struct of_device_id sh_mobile_sdhi_of_match[] = { |
df1d0584 GL |
116 | { .compatible = "renesas,sdhi-shmobile" }, |
117 | { .compatible = "renesas,sdhi-sh7372" }, | |
118 | { .compatible = "renesas,sdhi-sh73a0", .data = &sh_mobile_sdhi_of_cfg[0], }, | |
119 | { .compatible = "renesas,sdhi-r8a73a4", .data = &sh_mobile_sdhi_of_cfg[0], }, | |
120 | { .compatible = "renesas,sdhi-r8a7740", .data = &sh_mobile_sdhi_of_cfg[0], }, | |
121 | { .compatible = "renesas,sdhi-r8a7778", .data = &sh_mobile_sdhi_of_cfg[0], }, | |
122 | { .compatible = "renesas,sdhi-r8a7779", .data = &sh_mobile_sdhi_of_cfg[0], }, | |
123 | { .compatible = "renesas,sdhi-r8a7790", .data = &sh_mobile_sdhi_of_cfg[0], }, | |
5a00a971 GL |
124 | {}, |
125 | }; | |
126 | MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match); | |
127 | ||
c3be1efd | 128 | static int sh_mobile_sdhi_probe(struct platform_device *pdev) |
a87d5638 | 129 | { |
5a00a971 GL |
130 | const struct of_device_id *of_id = |
131 | of_match_device(sh_mobile_sdhi_of_match, &pdev->dev); | |
a87d5638 | 132 | struct sh_mobile_sdhi *priv; |
056676da GL |
133 | struct tmio_mmc_data *mmc_data; |
134 | struct sh_mobile_sdhi_info *p = pdev->dev.platform_data; | |
42051e8a | 135 | struct tmio_mmc_host *host; |
d5098cb6 SH |
136 | int irq, ret, i = 0; |
137 | bool multiplexed_isr = true; | |
87ae7bbe | 138 | struct tmio_mmc_dma *dma_priv; |
a87d5638 | 139 | |
ac51b961 | 140 | priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL); |
a87d5638 MD |
141 | if (priv == NULL) { |
142 | dev_err(&pdev->dev, "kzalloc failed\n"); | |
143 | return -ENOMEM; | |
144 | } | |
145 | ||
056676da | 146 | mmc_data = &priv->mmc_data; |
87ae7bbe | 147 | dma_priv = &priv->dma_priv; |
056676da | 148 | |
c7bb4487 | 149 | if (p) { |
c7bb4487 GL |
150 | if (p->init) { |
151 | ret = p->init(pdev, &sdhi_ops); | |
152 | if (ret) | |
ac51b961 | 153 | return ret; |
c7bb4487 | 154 | } |
e82b4ac9 BH |
155 | } |
156 | ||
ac51b961 | 157 | priv->clk = devm_clk_get(&pdev->dev, NULL); |
a87d5638 | 158 | if (IS_ERR(priv->clk)) { |
a87d5638 | 159 | ret = PTR_ERR(priv->clk); |
56ae1adc | 160 | dev_err(&pdev->dev, "cannot get clock: %d\n", ret); |
42051e8a | 161 | goto eclkget; |
a87d5638 MD |
162 | } |
163 | ||
56c49287 GL |
164 | mmc_data->clk_enable = sh_mobile_sdhi_clk_enable; |
165 | mmc_data->clk_disable = sh_mobile_sdhi_clk_disable; | |
056676da | 166 | mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED; |
7b952137 | 167 | mmc_data->write16_hook = sh_mobile_sdhi_write16_hook; |
bb0fe533 | 168 | if (p) { |
f87c20a9 | 169 | mmc_data->flags = p->tmio_flags; |
bb0fe533 | 170 | mmc_data->ocr_mask = p->tmio_ocr_mask; |
998283e2 | 171 | mmc_data->capabilities |= p->tmio_caps; |
d7d8d500 | 172 | mmc_data->capabilities2 |= p->tmio_caps2; |
58126c87 | 173 | mmc_data->cd_gpio = p->cd_gpio; |
42051e8a | 174 | |
3e713373 | 175 | if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) { |
eec95ee2 GL |
176 | /* |
177 | * Yes, we have to provide slave IDs twice to TMIO: | |
178 | * once as a filter parameter and once for channel | |
179 | * configuration as an explicit slave ID | |
180 | */ | |
181 | dma_priv->chan_priv_tx = (void *)p->dma_slave_tx; | |
182 | dma_priv->chan_priv_rx = (void *)p->dma_slave_rx; | |
183 | dma_priv->slave_id_tx = p->dma_slave_tx; | |
184 | dma_priv->slave_id_rx = p->dma_slave_rx; | |
42051e8a | 185 | } |
bb0fe533 | 186 | } |
056676da | 187 | |
87ae7bbe GL |
188 | dma_priv->alignment_shift = 1; /* 2-byte alignment */ |
189 | dma_priv->filter = shdma_chan_filter; | |
190 | ||
191 | mmc_data->dma = dma_priv; | |
192 | ||
f1334fb3 YG |
193 | /* |
194 | * All SDHI blocks support 2-byte and larger block sizes in 4-bit | |
195 | * bus width mode. | |
196 | */ | |
197 | mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES; | |
198 | ||
23b66071 AH |
199 | /* |
200 | * All SDHI blocks support SDIO IRQ signalling. | |
201 | */ | |
202 | mmc_data->flags |= TMIO_MMC_SDIO_IRQ; | |
203 | ||
5a00a971 GL |
204 | if (of_id && of_id->data) { |
205 | const struct sh_mobile_sdhi_of_data *of_data = of_id->data; | |
206 | mmc_data->flags |= of_data->tmio_flags; | |
207 | } | |
208 | ||
42051e8a GL |
209 | ret = tmio_mmc_host_probe(&host, pdev, mmc_data); |
210 | if (ret < 0) | |
211 | goto eprobe; | |
a87d5638 | 212 | |
d5098cb6 SH |
213 | /* |
214 | * Allow one or more specific (named) ISRs or | |
215 | * one or more multiplexed (un-named) ISRs. | |
216 | */ | |
217 | ||
218 | irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT); | |
219 | if (irq >= 0) { | |
220 | multiplexed_isr = false; | |
ac51b961 | 221 | ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_card_detect_irq, 0, |
d5098cb6 SH |
222 | dev_name(&pdev->dev), host); |
223 | if (ret) | |
ac51b961 | 224 | goto eirq; |
d5098cb6 SH |
225 | } |
226 | ||
227 | irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO); | |
228 | if (irq >= 0) { | |
229 | multiplexed_isr = false; | |
ac51b961 | 230 | ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdio_irq, 0, |
d5098cb6 SH |
231 | dev_name(&pdev->dev), host); |
232 | if (ret) | |
ac51b961 | 233 | goto eirq; |
d5098cb6 SH |
234 | } |
235 | ||
236 | irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD); | |
237 | if (irq >= 0) { | |
238 | multiplexed_isr = false; | |
ac51b961 | 239 | ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdcard_irq, 0, |
d6a1f863 | 240 | dev_name(&pdev->dev), host); |
d5098cb6 | 241 | if (ret) |
ac51b961 | 242 | goto eirq; |
d5098cb6 SH |
243 | } else if (!multiplexed_isr) { |
244 | dev_err(&pdev->dev, | |
245 | "Principal SD-card IRQ is missing among named interrupts\n"); | |
246 | ret = irq; | |
ac51b961 | 247 | goto eirq; |
d5098cb6 SH |
248 | } |
249 | ||
250 | if (multiplexed_isr) { | |
251 | while (1) { | |
252 | irq = platform_get_irq(pdev, i); | |
253 | if (irq < 0) | |
254 | break; | |
255 | i++; | |
ac51b961 | 256 | ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0, |
d5098cb6 SH |
257 | dev_name(&pdev->dev), host); |
258 | if (ret) | |
ac51b961 | 259 | goto eirq; |
d6a1f863 | 260 | } |
d5098cb6 SH |
261 | |
262 | /* There must be at least one IRQ source */ | |
7913ae7d WY |
263 | if (!i) { |
264 | ret = irq; | |
ac51b961 | 265 | goto eirq; |
7913ae7d | 266 | } |
8e7bfdb3 | 267 | } |
d5098cb6 | 268 | |
1f7d6819 MD |
269 | dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n", |
270 | mmc_hostname(host->mmc), (unsigned long) | |
58126c87 | 271 | (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start), |
369213bd | 272 | host->mmc->f_max / 1000000); |
a87d5638 | 273 | |
42051e8a | 274 | return ret; |
a87d5638 | 275 | |
ac51b961 | 276 | eirq: |
8e7bfdb3 | 277 | tmio_mmc_host_remove(host); |
42051e8a | 278 | eprobe: |
42051e8a | 279 | eclkget: |
c7bb4487 | 280 | if (p && p->cleanup) |
e82b4ac9 | 281 | p->cleanup(pdev); |
a87d5638 MD |
282 | return ret; |
283 | } | |
284 | ||
285 | static int sh_mobile_sdhi_remove(struct platform_device *pdev) | |
286 | { | |
42051e8a GL |
287 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
288 | struct tmio_mmc_host *host = mmc_priv(mmc); | |
25958804 | 289 | struct sh_mobile_sdhi_info *p = pdev->dev.platform_data; |
d6a1f863 | 290 | |
742a0c7c GL |
291 | tmio_mmc_host_remove(host); |
292 | ||
c7bb4487 | 293 | if (p && p->cleanup) |
e82b4ac9 BH |
294 | p->cleanup(pdev); |
295 | ||
a87d5638 MD |
296 | return 0; |
297 | } | |
298 | ||
e6ee7182 GL |
299 | static const struct dev_pm_ops tmio_mmc_dev_pm_ops = { |
300 | .suspend = tmio_mmc_host_suspend, | |
301 | .resume = tmio_mmc_host_resume, | |
25958804 GL |
302 | .runtime_suspend = tmio_mmc_host_runtime_suspend, |
303 | .runtime_resume = tmio_mmc_host_runtime_resume, | |
e6ee7182 GL |
304 | }; |
305 | ||
a87d5638 MD |
306 | static struct platform_driver sh_mobile_sdhi_driver = { |
307 | .driver = { | |
308 | .name = "sh_mobile_sdhi", | |
309 | .owner = THIS_MODULE, | |
e6ee7182 | 310 | .pm = &tmio_mmc_dev_pm_ops, |
c7bb4487 | 311 | .of_match_table = sh_mobile_sdhi_of_match, |
a87d5638 MD |
312 | }, |
313 | .probe = sh_mobile_sdhi_probe, | |
0433c143 | 314 | .remove = sh_mobile_sdhi_remove, |
a87d5638 MD |
315 | }; |
316 | ||
d1f81a64 | 317 | module_platform_driver(sh_mobile_sdhi_driver); |
a87d5638 MD |
318 | |
319 | MODULE_DESCRIPTION("SuperH Mobile SDHI driver"); | |
320 | MODULE_AUTHOR("Magnus Damm"); | |
321 | MODULE_LICENSE("GPL v2"); | |
42051e8a | 322 | MODULE_ALIAS("platform:sh_mobile_sdhi"); |