]> Git Repo - linux.git/blob - drivers/mmc/host/sh_mobile_sdhi.c
Merge branches 'for-3.12/devm', 'for-3.12/i2c-hid', 'for-3.12/i2c-hid-dt', 'for-3...
[linux.git] / drivers / mmc / host / sh_mobile_sdhi.c
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 <[email protected]>,
17  *          Samuel Ortiz <[email protected]>
18  *
19  */
20
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>
33
34 #include "tmio_mmc.h"
35
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
46 struct sh_mobile_sdhi {
47         struct clk *clk;
48         struct tmio_mmc_data mmc_data;
49         struct tmio_mmc_dma dma_priv;
50 };
51
52 static int sh_mobile_sdhi_clk_enable(struct platform_device *pdev, unsigned int *f)
53 {
54         struct mmc_host *mmc = platform_get_drvdata(pdev);
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 {
67         struct mmc_host *mmc = platform_get_drvdata(pdev);
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
73 static void sh_mobile_sdhi_set_pwr(struct platform_device *pdev, int state)
74 {
75         struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
76
77         p->set_pwr(pdev, state);
78 }
79
80 static int sh_mobile_sdhi_get_cd(struct platform_device *pdev)
81 {
82         struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
83
84         return p->get_cd(pdev);
85 }
86
87 static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
88 {
89         int timeout = 1000;
90
91         while (--timeout && !(sd_ctrl_read16(host, CTL_STATUS2) & (1 << 13)))
92                 udelay(1);
93
94         if (!timeout) {
95                 dev_warn(host->pdata->dev, "timeout waiting for SD bus idle\n");
96                 return -EBUSY;
97         }
98
99         return 0;
100 }
101
102 static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
103 {
104         switch (addr)
105         {
106         case CTL_SD_CMD:
107         case CTL_STOP_INTERNAL_ACTION:
108         case CTL_XFER_BLK_COUNT:
109         case CTL_SD_CARD_CLK_CTL:
110         case CTL_SD_XFER_LEN:
111         case CTL_SD_MEM_CARD_OPT:
112         case CTL_TRANSACTION_CTL:
113         case CTL_DMA_ENABLE:
114                 return sh_mobile_sdhi_wait_idle(host);
115         }
116
117         return 0;
118 }
119
120 static void sh_mobile_sdhi_cd_wakeup(const struct platform_device *pdev)
121 {
122         mmc_detect_change(platform_get_drvdata(pdev), msecs_to_jiffies(100));
123 }
124
125 static const struct sh_mobile_sdhi_ops sdhi_ops = {
126         .cd_wakeup = sh_mobile_sdhi_cd_wakeup,
127 };
128
129 static const struct of_device_id sh_mobile_sdhi_of_match[] = {
130         { .compatible = "renesas,shmobile-sdhi" },
131         { .compatible = "renesas,sh7372-sdhi" },
132         { .compatible = "renesas,r8a7740-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
133         {},
134 };
135 MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
136
137 static int sh_mobile_sdhi_probe(struct platform_device *pdev)
138 {
139         const struct of_device_id *of_id =
140                 of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
141         struct sh_mobile_sdhi *priv;
142         struct tmio_mmc_data *mmc_data;
143         struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
144         struct tmio_mmc_host *host;
145         int irq, ret, i = 0;
146         bool multiplexed_isr = true;
147         struct tmio_mmc_dma *dma_priv;
148
149         priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
150         if (priv == NULL) {
151                 dev_err(&pdev->dev, "kzalloc failed\n");
152                 return -ENOMEM;
153         }
154
155         mmc_data = &priv->mmc_data;
156         dma_priv = &priv->dma_priv;
157
158         if (p) {
159                 if (p->init) {
160                         ret = p->init(pdev, &sdhi_ops);
161                         if (ret)
162                                 return ret;
163                 }
164         }
165
166         priv->clk = devm_clk_get(&pdev->dev, NULL);
167         if (IS_ERR(priv->clk)) {
168                 ret = PTR_ERR(priv->clk);
169                 dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
170                 goto eclkget;
171         }
172
173         mmc_data->clk_enable = sh_mobile_sdhi_clk_enable;
174         mmc_data->clk_disable = sh_mobile_sdhi_clk_disable;
175         mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
176         mmc_data->write16_hook = sh_mobile_sdhi_write16_hook;
177         if (p) {
178                 mmc_data->flags = p->tmio_flags;
179                 mmc_data->ocr_mask = p->tmio_ocr_mask;
180                 mmc_data->capabilities |= p->tmio_caps;
181                 mmc_data->capabilities2 |= p->tmio_caps2;
182                 mmc_data->cd_gpio = p->cd_gpio;
183                 if (p->set_pwr)
184                         mmc_data->set_pwr = sh_mobile_sdhi_set_pwr;
185                 if (p->get_cd)
186                         mmc_data->get_cd = sh_mobile_sdhi_get_cd;
187
188                 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) {
189                         /*
190                          * Yes, we have to provide slave IDs twice to TMIO:
191                          * once as a filter parameter and once for channel
192                          * configuration as an explicit slave ID
193                          */
194                         dma_priv->chan_priv_tx = (void *)p->dma_slave_tx;
195                         dma_priv->chan_priv_rx = (void *)p->dma_slave_rx;
196                         dma_priv->slave_id_tx = p->dma_slave_tx;
197                         dma_priv->slave_id_rx = p->dma_slave_rx;
198                 }
199         }
200
201         dma_priv->alignment_shift = 1; /* 2-byte alignment */
202         dma_priv->filter = shdma_chan_filter;
203
204         mmc_data->dma = dma_priv;
205
206         /*
207          * All SDHI blocks support 2-byte and larger block sizes in 4-bit
208          * bus width mode.
209          */
210         mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
211
212         /*
213          * All SDHI blocks support SDIO IRQ signalling.
214          */
215         mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
216
217         if (of_id && of_id->data) {
218                 const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
219                 mmc_data->flags |= of_data->tmio_flags;
220         }
221
222         ret = tmio_mmc_host_probe(&host, pdev, mmc_data);
223         if (ret < 0)
224                 goto eprobe;
225
226         /*
227          * Allow one or more specific (named) ISRs or
228          * one or more multiplexed (un-named) ISRs.
229          */
230
231         irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
232         if (irq >= 0) {
233                 multiplexed_isr = false;
234                 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_card_detect_irq, 0,
235                                   dev_name(&pdev->dev), host);
236                 if (ret)
237                         goto eirq;
238         }
239
240         irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
241         if (irq >= 0) {
242                 multiplexed_isr = false;
243                 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdio_irq, 0,
244                                   dev_name(&pdev->dev), host);
245                 if (ret)
246                         goto eirq;
247         }
248
249         irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD);
250         if (irq >= 0) {
251                 multiplexed_isr = false;
252                 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdcard_irq, 0,
253                                   dev_name(&pdev->dev), host);
254                 if (ret)
255                         goto eirq;
256         } else if (!multiplexed_isr) {
257                 dev_err(&pdev->dev,
258                         "Principal SD-card IRQ is missing among named interrupts\n");
259                 ret = irq;
260                 goto eirq;
261         }
262
263         if (multiplexed_isr) {
264                 while (1) {
265                         irq = platform_get_irq(pdev, i);
266                         if (irq < 0)
267                                 break;
268                         i++;
269                         ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
270                                           dev_name(&pdev->dev), host);
271                         if (ret)
272                                 goto eirq;
273                 }
274
275                 /* There must be at least one IRQ source */
276                 if (!i) {
277                         ret = irq;
278                         goto eirq;
279                 }
280         }
281
282         dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
283                  mmc_hostname(host->mmc), (unsigned long)
284                  (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
285                  host->mmc->f_max / 1000000);
286
287         return ret;
288
289 eirq:
290         tmio_mmc_host_remove(host);
291 eprobe:
292 eclkget:
293         if (p && p->cleanup)
294                 p->cleanup(pdev);
295         return ret;
296 }
297
298 static int sh_mobile_sdhi_remove(struct platform_device *pdev)
299 {
300         struct mmc_host *mmc = platform_get_drvdata(pdev);
301         struct tmio_mmc_host *host = mmc_priv(mmc);
302         struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
303
304         tmio_mmc_host_remove(host);
305
306         if (p && p->cleanup)
307                 p->cleanup(pdev);
308
309         return 0;
310 }
311
312 static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
313         .suspend = tmio_mmc_host_suspend,
314         .resume = tmio_mmc_host_resume,
315         .runtime_suspend = tmio_mmc_host_runtime_suspend,
316         .runtime_resume = tmio_mmc_host_runtime_resume,
317 };
318
319 static struct platform_driver sh_mobile_sdhi_driver = {
320         .driver         = {
321                 .name   = "sh_mobile_sdhi",
322                 .owner  = THIS_MODULE,
323                 .pm     = &tmio_mmc_dev_pm_ops,
324                 .of_match_table = sh_mobile_sdhi_of_match,
325         },
326         .probe          = sh_mobile_sdhi_probe,
327         .remove         = sh_mobile_sdhi_remove,
328 };
329
330 module_platform_driver(sh_mobile_sdhi_driver);
331
332 MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
333 MODULE_AUTHOR("Magnus Damm");
334 MODULE_LICENSE("GPL v2");
335 MODULE_ALIAS("platform:sh_mobile_sdhi");
This page took 0.051462 seconds and 4 git commands to generate.