Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 1 | /* |
| 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 Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 25 | #include <linux/io.h> |
| 26 | #include <linux/gpio.h> |
| 27 | #include <linux/mmc/card.h> |
| 28 | #include <linux/mmc/host.h> |
Zhangfei Gao | bfed345 | 2011-06-20 22:11:52 +0800 | [diff] [blame] | 29 | #include <linux/platform_data/pxa_sdhci.h> |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 30 | #include <linux/slab.h> |
Chris Ball | b650352 | 2012-04-10 22:34:33 -0400 | [diff] [blame] | 31 | #include <linux/of.h> |
| 32 | #include <linux/of_device.h> |
| 33 | |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 34 | #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 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 54 | static void pxav2_reset(struct sdhci_host *host, u8 mask) |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 55 | { |
| 56 | struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc)); |
| 57 | struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data; |
| 58 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 59 | sdhci_reset(host, mask); |
| 60 | |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 61 | if (mask == SDHCI_RESET_ALL) { |
| 62 | u16 tmp = 0; |
| 63 | |
| 64 | /* |
| 65 | * tune timing of read data/command when crc error happen |
| 66 | * no performance impact |
| 67 | */ |
Tanmay Upadhyay | 329f223 | 2011-09-14 11:29:02 +0530 | [diff] [blame] | 68 | if (pdata && pdata->clk_delay_sel == 1) { |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 69 | tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP); |
| 70 | |
| 71 | tmp &= ~(SDCLK_DELAY_MASK << SDCLK_DELAY_SHIFT); |
| 72 | tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK) |
| 73 | << SDCLK_DELAY_SHIFT; |
| 74 | tmp &= ~(SDCLK_SEL_MASK << SDCLK_SEL_SHIFT); |
| 75 | tmp |= (1 & SDCLK_SEL_MASK) << SDCLK_SEL_SHIFT; |
| 76 | |
| 77 | writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP); |
| 78 | } |
| 79 | |
Tanmay Upadhyay | 329f223 | 2011-09-14 11:29:02 +0530 | [diff] [blame] | 80 | if (pdata && (pdata->flags & PXA_FLAG_ENABLE_CLOCK_GATING)) { |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 81 | tmp = readw(host->ioaddr + SD_FIFO_PARAM); |
| 82 | tmp &= ~CLK_GATE_SETTING_BITS; |
| 83 | writew(tmp, host->ioaddr + SD_FIFO_PARAM); |
| 84 | } else { |
| 85 | tmp = readw(host->ioaddr + SD_FIFO_PARAM); |
| 86 | tmp &= ~CLK_GATE_SETTING_BITS; |
| 87 | tmp |= CLK_GATE_SETTING_BITS; |
| 88 | writew(tmp, host->ioaddr + SD_FIFO_PARAM); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 93 | static void pxav2_mmc_set_bus_width(struct sdhci_host *host, int width) |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 94 | { |
| 95 | u8 ctrl; |
| 96 | u16 tmp; |
| 97 | |
| 98 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 99 | tmp = readw(host->ioaddr + SD_CE_ATA_2); |
| 100 | if (width == MMC_BUS_WIDTH_8) { |
| 101 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 102 | tmp |= MMC_CARD | MMC_WIDTH; |
| 103 | } else { |
| 104 | tmp &= ~(MMC_CARD | MMC_WIDTH); |
| 105 | if (width == MMC_BUS_WIDTH_4) |
| 106 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 107 | else |
| 108 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 109 | } |
| 110 | writew(tmp, host->ioaddr + SD_CE_ATA_2); |
| 111 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 112 | } |
| 113 | |
Lars-Peter Clausen | c915568 | 2013-03-13 19:26:05 +0100 | [diff] [blame] | 114 | static const struct sdhci_ops pxav2_sdhci_ops = { |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 115 | .set_clock = sdhci_set_clock, |
Lars-Peter Clausen | d005d94 | 2013-01-28 19:27:12 +0100 | [diff] [blame] | 116 | .get_max_clock = sdhci_pltfm_clk_get_max_clock, |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 117 | .set_bus_width = pxav2_mmc_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 118 | .reset = pxav2_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 119 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 120 | }; |
| 121 | |
Chris Ball | b650352 | 2012-04-10 22:34:33 -0400 | [diff] [blame] | 122 | #ifdef CONFIG_OF |
| 123 | static const struct of_device_id sdhci_pxav2_of_match[] = { |
| 124 | { |
| 125 | .compatible = "mrvl,pxav2-mmc", |
| 126 | }, |
| 127 | {}, |
| 128 | }; |
| 129 | MODULE_DEVICE_TABLE(of, sdhci_pxav2_of_match); |
| 130 | |
| 131 | static struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev) |
| 132 | { |
| 133 | struct sdhci_pxa_platdata *pdata; |
| 134 | struct device_node *np = dev->of_node; |
| 135 | u32 bus_width; |
| 136 | u32 clk_delay_cycles; |
| 137 | |
| 138 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 139 | if (!pdata) |
| 140 | return NULL; |
| 141 | |
| 142 | if (of_find_property(np, "non-removable", NULL)) |
| 143 | pdata->flags |= PXA_FLAG_CARD_PERMANENT; |
| 144 | |
| 145 | of_property_read_u32(np, "bus-width", &bus_width); |
| 146 | if (bus_width == 8) |
| 147 | pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT; |
| 148 | |
| 149 | of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles); |
| 150 | if (clk_delay_cycles > 0) { |
| 151 | pdata->clk_delay_sel = 1; |
| 152 | pdata->clk_delay_cycles = clk_delay_cycles; |
| 153 | } |
| 154 | |
| 155 | return pdata; |
| 156 | } |
| 157 | #else |
| 158 | static inline struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev) |
| 159 | { |
| 160 | return NULL; |
| 161 | } |
| 162 | #endif |
| 163 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 164 | static int sdhci_pxav2_probe(struct platform_device *pdev) |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 165 | { |
| 166 | struct sdhci_pltfm_host *pltfm_host; |
| 167 | struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data; |
| 168 | struct device *dev = &pdev->dev; |
| 169 | struct sdhci_host *host = NULL; |
Chris Ball | b650352 | 2012-04-10 22:34:33 -0400 | [diff] [blame] | 170 | const struct of_device_id *match; |
| 171 | |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 172 | int ret; |
| 173 | struct clk *clk; |
| 174 | |
Christian Daudt | 0e74823 | 2013-05-29 13:50:05 -0700 | [diff] [blame] | 175 | host = sdhci_pltfm_init(pdev, NULL, 0); |
Sebastian Hesselbarth | 6a686c3 | 2014-10-21 11:22:33 +0200 | [diff] [blame] | 176 | if (IS_ERR(host)) |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 177 | return PTR_ERR(host); |
Sebastian Hesselbarth | 6a686c3 | 2014-10-21 11:22:33 +0200 | [diff] [blame] | 178 | |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 179 | pltfm_host = sdhci_priv(host); |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 180 | |
| 181 | clk = clk_get(dev, "PXA-SDHCLK"); |
| 182 | if (IS_ERR(clk)) { |
| 183 | dev_err(dev, "failed to get io clock\n"); |
| 184 | ret = PTR_ERR(clk); |
| 185 | goto err_clk_get; |
| 186 | } |
| 187 | pltfm_host->clk = clk; |
Alexey Khoroshilov | 21b2228 | 2017-02-11 01:54:46 +0300 | [diff] [blame] | 188 | ret = clk_prepare_enable(clk); |
| 189 | if (ret) { |
| 190 | dev_err(&pdev->dev, "failed to enable io clock\n"); |
| 191 | goto err_clk_enable; |
| 192 | } |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 193 | |
| 194 | host->quirks = SDHCI_QUIRK_BROKEN_ADMA |
| 195 | | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
| 196 | | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN; |
| 197 | |
Chris Ball | b650352 | 2012-04-10 22:34:33 -0400 | [diff] [blame] | 198 | match = of_match_device(of_match_ptr(sdhci_pxav2_of_match), &pdev->dev); |
| 199 | if (match) { |
| 200 | pdata = pxav2_get_mmc_pdata(dev); |
| 201 | } |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 202 | if (pdata) { |
| 203 | if (pdata->flags & PXA_FLAG_CARD_PERMANENT) { |
| 204 | /* on-chip device */ |
| 205 | host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION; |
| 206 | host->mmc->caps |= MMC_CAP_NONREMOVABLE; |
| 207 | } |
| 208 | |
| 209 | /* If slot design supports 8 bit data, indicate this to MMC. */ |
| 210 | if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT) |
| 211 | host->mmc->caps |= MMC_CAP_8_BIT_DATA; |
| 212 | |
| 213 | if (pdata->quirks) |
| 214 | host->quirks |= pdata->quirks; |
| 215 | if (pdata->host_caps) |
| 216 | host->mmc->caps |= pdata->host_caps; |
| 217 | if (pdata->pm_caps) |
| 218 | host->mmc->pm_caps |= pdata->pm_caps; |
| 219 | } |
| 220 | |
| 221 | host->ops = &pxav2_sdhci_ops; |
| 222 | |
| 223 | ret = sdhci_add_host(host); |
| 224 | if (ret) { |
| 225 | dev_err(&pdev->dev, "failed to add host\n"); |
| 226 | goto err_add_host; |
| 227 | } |
| 228 | |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 229 | return 0; |
| 230 | |
| 231 | err_add_host: |
Chao Xie | 164378e | 2012-07-31 14:35:25 +0800 | [diff] [blame] | 232 | clk_disable_unprepare(clk); |
Alexey Khoroshilov | 21b2228 | 2017-02-11 01:54:46 +0300 | [diff] [blame] | 233 | err_clk_enable: |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 234 | clk_put(clk); |
| 235 | err_clk_get: |
| 236 | sdhci_pltfm_free(pdev); |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 237 | return ret; |
| 238 | } |
| 239 | |
Bill Pemberton | 6e0ee71 | 2012-11-19 13:26:03 -0500 | [diff] [blame] | 240 | static int sdhci_pxav2_remove(struct platform_device *pdev) |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 241 | { |
| 242 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 243 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 244 | |
| 245 | sdhci_remove_host(host, 1); |
| 246 | |
Chao Xie | 164378e | 2012-07-31 14:35:25 +0800 | [diff] [blame] | 247 | clk_disable_unprepare(pltfm_host->clk); |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 248 | clk_put(pltfm_host->clk); |
| 249 | sdhci_pltfm_free(pdev); |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 250 | |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static struct platform_driver sdhci_pxav2_driver = { |
| 255 | .driver = { |
| 256 | .name = "sdhci-pxav2", |
Axel Lin | 59d2230 | 2015-05-05 17:11:54 +0800 | [diff] [blame] | 257 | .of_match_table = of_match_ptr(sdhci_pxav2_of_match), |
Ulf Hansson | fa243f6 | 2016-07-27 13:07:21 +0200 | [diff] [blame] | 258 | .pm = &sdhci_pltfm_pmops, |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 259 | }, |
| 260 | .probe = sdhci_pxav2_probe, |
Bill Pemberton | 0433c14 | 2012-11-19 13:20:26 -0500 | [diff] [blame] | 261 | .remove = sdhci_pxav2_remove, |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 262 | }; |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 263 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 264 | module_platform_driver(sdhci_pxav2_driver); |
Zhangfei Gao | 9f5d71e | 2011-06-08 17:41:58 +0800 | [diff] [blame] | 265 | |
| 266 | MODULE_DESCRIPTION("SDHCI driver for pxav2"); |
| 267 | MODULE_AUTHOR("Marvell International Ltd."); |
| 268 | MODULE_LICENSE("GPL v2"); |
| 269 | |