blob: d24c282e5eb8a498551b57a448d9748d356a0957 [file] [log] [blame]
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +08001/*
2 * Copyright (C) 2010 Marvell International Ltd.
3 * Zhangfei Gao <zhangfei.gao@marvell.com>
4 * Kevin Wang <dwang4@marvell.com>
5 * Jun Nie <njun@marvell.com>
6 * Qiming Wu <wuqm@marvell.com>
7 * Philip Rakity <prakity@marvell.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/platform_device.h>
23#include <linux/clk.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040024#include <linux/module.h>
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +080025#include <linux/io.h>
26#include <linux/gpio.h>
27#include <linux/mmc/card.h>
28#include <linux/mmc/host.h>
Zhangfei Gaobfed3452011-06-20 22:11:52 +080029#include <linux/platform_data/pxa_sdhci.h>
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +080030#include <linux/slab.h>
Chris Ballb6503522012-04-10 22:34:33 -040031#include <linux/of.h>
32#include <linux/of_device.h>
33
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +080034#include "sdhci.h"
35#include "sdhci-pltfm.h"
36
37#define SD_FIFO_PARAM 0xe0
38#define DIS_PAD_SD_CLK_GATE 0x0400 /* Turn on/off Dynamic SD Clock Gating */
39#define CLK_GATE_ON 0x0200 /* Disable/enable Clock Gate */
40#define CLK_GATE_CTL 0x0100 /* Clock Gate Control */
41#define CLK_GATE_SETTING_BITS (DIS_PAD_SD_CLK_GATE | \
42 CLK_GATE_ON | CLK_GATE_CTL)
43
44#define SD_CLOCK_BURST_SIZE_SETUP 0xe6
45#define SDCLK_SEL_SHIFT 8
46#define SDCLK_SEL_MASK 0x3
47#define SDCLK_DELAY_SHIFT 10
48#define SDCLK_DELAY_MASK 0x3c
49
50#define SD_CE_ATA_2 0xea
51#define MMC_CARD 0x1000
52#define MMC_WIDTH 0x0100
53
54static void pxav2_set_private_registers(struct sdhci_host *host, u8 mask)
55{
56 struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
57 struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
58
59 if (mask == SDHCI_RESET_ALL) {
60 u16 tmp = 0;
61
62 /*
63 * tune timing of read data/command when crc error happen
64 * no performance impact
65 */
Tanmay Upadhyay329f2232011-09-14 11:29:02 +053066 if (pdata && pdata->clk_delay_sel == 1) {
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +080067 tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
68
69 tmp &= ~(SDCLK_DELAY_MASK << SDCLK_DELAY_SHIFT);
70 tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
71 << SDCLK_DELAY_SHIFT;
72 tmp &= ~(SDCLK_SEL_MASK << SDCLK_SEL_SHIFT);
73 tmp |= (1 & SDCLK_SEL_MASK) << SDCLK_SEL_SHIFT;
74
75 writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
76 }
77
Tanmay Upadhyay329f2232011-09-14 11:29:02 +053078 if (pdata && (pdata->flags & PXA_FLAG_ENABLE_CLOCK_GATING)) {
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +080079 tmp = readw(host->ioaddr + SD_FIFO_PARAM);
80 tmp &= ~CLK_GATE_SETTING_BITS;
81 writew(tmp, host->ioaddr + SD_FIFO_PARAM);
82 } else {
83 tmp = readw(host->ioaddr + SD_FIFO_PARAM);
84 tmp &= ~CLK_GATE_SETTING_BITS;
85 tmp |= CLK_GATE_SETTING_BITS;
86 writew(tmp, host->ioaddr + SD_FIFO_PARAM);
87 }
88 }
89}
90
Russell King2317f562014-04-25 12:57:07 +010091static void pxav2_mmc_set_bus_width(struct sdhci_host *host, int width)
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +080092{
93 u8 ctrl;
94 u16 tmp;
95
96 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
97 tmp = readw(host->ioaddr + SD_CE_ATA_2);
98 if (width == MMC_BUS_WIDTH_8) {
99 ctrl &= ~SDHCI_CTRL_4BITBUS;
100 tmp |= MMC_CARD | MMC_WIDTH;
101 } else {
102 tmp &= ~(MMC_CARD | MMC_WIDTH);
103 if (width == MMC_BUS_WIDTH_4)
104 ctrl |= SDHCI_CTRL_4BITBUS;
105 else
106 ctrl &= ~SDHCI_CTRL_4BITBUS;
107 }
108 writew(tmp, host->ioaddr + SD_CE_ATA_2);
109 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800110}
111
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100112static const struct sdhci_ops pxav2_sdhci_ops = {
Lars-Peter Clausend005d942013-01-28 19:27:12 +0100113 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800114 .platform_reset_exit = pxav2_set_private_registers,
Russell King2317f562014-04-25 12:57:07 +0100115 .set_bus_width = pxav2_mmc_set_bus_width,
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800116};
117
Chris Ballb6503522012-04-10 22:34:33 -0400118#ifdef CONFIG_OF
119static const struct of_device_id sdhci_pxav2_of_match[] = {
120 {
121 .compatible = "mrvl,pxav2-mmc",
122 },
123 {},
124};
125MODULE_DEVICE_TABLE(of, sdhci_pxav2_of_match);
126
127static struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
128{
129 struct sdhci_pxa_platdata *pdata;
130 struct device_node *np = dev->of_node;
131 u32 bus_width;
132 u32 clk_delay_cycles;
133
134 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
135 if (!pdata)
136 return NULL;
137
138 if (of_find_property(np, "non-removable", NULL))
139 pdata->flags |= PXA_FLAG_CARD_PERMANENT;
140
141 of_property_read_u32(np, "bus-width", &bus_width);
142 if (bus_width == 8)
143 pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT;
144
145 of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
146 if (clk_delay_cycles > 0) {
147 pdata->clk_delay_sel = 1;
148 pdata->clk_delay_cycles = clk_delay_cycles;
149 }
150
151 return pdata;
152}
153#else
154static inline struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
155{
156 return NULL;
157}
158#endif
159
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500160static int sdhci_pxav2_probe(struct platform_device *pdev)
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800161{
162 struct sdhci_pltfm_host *pltfm_host;
163 struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
164 struct device *dev = &pdev->dev;
165 struct sdhci_host *host = NULL;
166 struct sdhci_pxa *pxa = NULL;
Chris Ballb6503522012-04-10 22:34:33 -0400167 const struct of_device_id *match;
168
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800169 int ret;
170 struct clk *clk;
171
172 pxa = kzalloc(sizeof(struct sdhci_pxa), GFP_KERNEL);
173 if (!pxa)
174 return -ENOMEM;
175
Christian Daudt0e748232013-05-29 13:50:05 -0700176 host = sdhci_pltfm_init(pdev, NULL, 0);
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800177 if (IS_ERR(host)) {
178 kfree(pxa);
179 return PTR_ERR(host);
180 }
181 pltfm_host = sdhci_priv(host);
182 pltfm_host->priv = pxa;
183
184 clk = clk_get(dev, "PXA-SDHCLK");
185 if (IS_ERR(clk)) {
186 dev_err(dev, "failed to get io clock\n");
187 ret = PTR_ERR(clk);
188 goto err_clk_get;
189 }
190 pltfm_host->clk = clk;
Chao Xie164378e2012-07-31 14:35:25 +0800191 clk_prepare_enable(clk);
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800192
193 host->quirks = SDHCI_QUIRK_BROKEN_ADMA
194 | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
195 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
196
Chris Ballb6503522012-04-10 22:34:33 -0400197 match = of_match_device(of_match_ptr(sdhci_pxav2_of_match), &pdev->dev);
198 if (match) {
199 pdata = pxav2_get_mmc_pdata(dev);
200 }
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800201 if (pdata) {
202 if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
203 /* on-chip device */
204 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
205 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
206 }
207
208 /* If slot design supports 8 bit data, indicate this to MMC. */
209 if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
210 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
211
212 if (pdata->quirks)
213 host->quirks |= pdata->quirks;
214 if (pdata->host_caps)
215 host->mmc->caps |= pdata->host_caps;
216 if (pdata->pm_caps)
217 host->mmc->pm_caps |= pdata->pm_caps;
218 }
219
220 host->ops = &pxav2_sdhci_ops;
221
222 ret = sdhci_add_host(host);
223 if (ret) {
224 dev_err(&pdev->dev, "failed to add host\n");
225 goto err_add_host;
226 }
227
228 platform_set_drvdata(pdev, host);
229
230 return 0;
231
232err_add_host:
Chao Xie164378e2012-07-31 14:35:25 +0800233 clk_disable_unprepare(clk);
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800234 clk_put(clk);
235err_clk_get:
236 sdhci_pltfm_free(pdev);
237 kfree(pxa);
238 return ret;
239}
240
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500241static int sdhci_pxav2_remove(struct platform_device *pdev)
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800242{
243 struct sdhci_host *host = platform_get_drvdata(pdev);
244 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
245 struct sdhci_pxa *pxa = pltfm_host->priv;
246
247 sdhci_remove_host(host, 1);
248
Chao Xie164378e2012-07-31 14:35:25 +0800249 clk_disable_unprepare(pltfm_host->clk);
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800250 clk_put(pltfm_host->clk);
251 sdhci_pltfm_free(pdev);
252 kfree(pxa);
253
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800254 return 0;
255}
256
257static struct platform_driver sdhci_pxav2_driver = {
258 .driver = {
259 .name = "sdhci-pxav2",
260 .owner = THIS_MODULE,
Chris Ballb6503522012-04-10 22:34:33 -0400261#ifdef CONFIG_OF
262 .of_match_table = sdhci_pxav2_of_match,
263#endif
Manuel Lauss29495aa2011-11-03 11:09:45 +0100264 .pm = SDHCI_PLTFM_PMOPS,
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800265 },
266 .probe = sdhci_pxav2_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500267 .remove = sdhci_pxav2_remove,
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800268};
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800269
Axel Lind1f81a62011-11-26 12:55:43 +0800270module_platform_driver(sdhci_pxav2_driver);
Zhangfei Gao9f5d71e2011-06-08 17:41:58 +0800271
272MODULE_DESCRIPTION("SDHCI driver for pxav2");
273MODULE_AUTHOR("Marvell International Ltd.");
274MODULE_LICENSE("GPL v2");
275