Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Freescale eSDHC controller driver. |
| 3 | * |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 4 | * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc. |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009 MontaVista Software, Inc. |
| 6 | * |
| 7 | * Authors: Xiaobo Xie <X.Xie@freescale.com> |
| 8 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or (at |
| 13 | * your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/io.h> |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 17 | #include <linux/of.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 18 | #include <linux/delay.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 20 | #include <linux/mmc/host.h> |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 21 | #include "sdhci-pltfm.h" |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 22 | #include "sdhci-esdhc.h" |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 23 | |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 24 | #define VENDOR_V_22 0x12 |
| 25 | static u32 esdhc_readl(struct sdhci_host *host, int reg) |
| 26 | { |
| 27 | u32 ret; |
| 28 | |
| 29 | ret = in_be32(host->ioaddr + reg); |
| 30 | /* |
| 31 | * The bit of ADMA flag in eSDHC is not compatible with standard |
| 32 | * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is |
| 33 | * supported by eSDHC. |
| 34 | * And for many FSL eSDHC controller, the reset value of field |
| 35 | * SDHCI_CAN_DO_ADMA1 is one, but some of them can't support ADMA, |
| 36 | * only these vendor version is greater than 2.2/0x12 support ADMA. |
| 37 | * For FSL eSDHC, must aligned 4-byte, so use 0xFC to read the |
| 38 | * the verdor version number, oxFE is SDHCI_HOST_VERSION. |
| 39 | */ |
| 40 | if ((reg == SDHCI_CAPABILITIES) && (ret & SDHCI_CAN_DO_ADMA1)) { |
| 41 | u32 tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); |
| 42 | tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; |
| 43 | if (tmp > VENDOR_V_22) |
| 44 | ret |= SDHCI_CAN_DO_ADMA2; |
| 45 | } |
| 46 | |
| 47 | return ret; |
| 48 | } |
| 49 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 50 | static u16 esdhc_readw(struct sdhci_host *host, int reg) |
| 51 | { |
| 52 | u16 ret; |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 53 | int base = reg & ~0x3; |
| 54 | int shift = (reg & 0x2) * 8; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 55 | |
| 56 | if (unlikely(reg == SDHCI_HOST_VERSION)) |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 57 | ret = in_be32(host->ioaddr + base) & 0xffff; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 58 | else |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 59 | ret = (in_be32(host->ioaddr + base) >> shift) & 0xffff; |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static u8 esdhc_readb(struct sdhci_host *host, int reg) |
| 64 | { |
| 65 | int base = reg & ~0x3; |
| 66 | int shift = (reg & 0x3) * 8; |
| 67 | u8 ret = (in_be32(host->ioaddr + base) >> shift) & 0xff; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * "DMA select" locates at offset 0x28 in SD specification, but on |
| 71 | * P5020 or P3041, it locates at 0x29. |
| 72 | */ |
| 73 | if (reg == SDHCI_HOST_CONTROL) { |
| 74 | u32 dma_bits; |
| 75 | |
| 76 | dma_bits = in_be32(host->ioaddr + reg); |
| 77 | /* DMA select is 22,23 bits in Protocol Control Register */ |
| 78 | dma_bits = (dma_bits >> 5) & SDHCI_CTRL_DMA_MASK; |
| 79 | |
| 80 | /* fixup the result */ |
| 81 | ret &= ~SDHCI_CTRL_DMA_MASK; |
| 82 | ret |= dma_bits; |
| 83 | } |
| 84 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static void esdhc_writew(struct sdhci_host *host, u16 val, int reg) |
| 89 | { |
| 90 | if (reg == SDHCI_BLOCK_SIZE) { |
| 91 | /* |
| 92 | * Two last DMA bits are reserved, and first one is used for |
| 93 | * non-standard blksz of 4096 bytes that we don't support |
| 94 | * yet. So clear the DMA boundary bits. |
| 95 | */ |
| 96 | val &= ~SDHCI_MAKE_BLKSZ(0x7, 0); |
| 97 | } |
| 98 | sdhci_be32bs_writew(host, val, reg); |
| 99 | } |
| 100 | |
| 101 | static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg) |
| 102 | { |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 103 | /* |
| 104 | * "DMA select" location is offset 0x28 in SD specification, but on |
| 105 | * P5020 or P3041, it's located at 0x29. |
| 106 | */ |
| 107 | if (reg == SDHCI_HOST_CONTROL) { |
| 108 | u32 dma_bits; |
| 109 | |
| 110 | /* DMA select is 22,23 bits in Protocol Control Register */ |
| 111 | dma_bits = (val & SDHCI_CTRL_DMA_MASK) << 5; |
| 112 | clrsetbits_be32(host->ioaddr + reg , SDHCI_CTRL_DMA_MASK << 5, |
| 113 | dma_bits); |
| 114 | val &= ~SDHCI_CTRL_DMA_MASK; |
| 115 | val |= in_be32(host->ioaddr + reg) & SDHCI_CTRL_DMA_MASK; |
| 116 | } |
| 117 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 118 | /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */ |
| 119 | if (reg == SDHCI_HOST_CONTROL) |
| 120 | val &= ~ESDHC_HOST_CONTROL_RES; |
| 121 | sdhci_be32bs_writeb(host, val, reg); |
| 122 | } |
| 123 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 124 | static int esdhc_of_enable_dma(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 125 | { |
| 126 | setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP); |
| 127 | return 0; |
| 128 | } |
| 129 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 130 | static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 131 | { |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 132 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 133 | |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 134 | return pltfm_host->clock; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 137 | static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 138 | { |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 139 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 140 | |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 141 | return pltfm_host->clock / 256 / 16; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 144 | static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) |
| 145 | { |
| 146 | /* Workaround to reduce the clock frequency for p1010 esdhc */ |
| 147 | if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) { |
| 148 | if (clock > 20000000) |
| 149 | clock -= 5000000; |
| 150 | if (clock > 40000000) |
| 151 | clock -= 5000000; |
| 152 | } |
| 153 | |
| 154 | /* Set the clock */ |
| 155 | esdhc_set_clock(host, clock); |
| 156 | } |
| 157 | |
Jerry Huang | 192b537 | 2012-02-04 17:13:13 -0500 | [diff] [blame] | 158 | #ifdef CONFIG_PM |
| 159 | static u32 esdhc_proctl; |
| 160 | static void esdhc_of_suspend(struct sdhci_host *host) |
| 161 | { |
| 162 | esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL); |
| 163 | } |
| 164 | |
| 165 | static void esdhc_of_resume(struct sdhci_host *host) |
| 166 | { |
| 167 | esdhc_of_enable_dma(host); |
| 168 | sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); |
| 169 | } |
| 170 | #endif |
| 171 | |
Jerry Huang | 63ef5d8 | 2012-10-25 13:47:19 +0800 | [diff] [blame] | 172 | static void esdhc_of_platform_init(struct sdhci_host *host) |
| 173 | { |
| 174 | u32 vvn; |
| 175 | |
| 176 | vvn = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); |
| 177 | vvn = (vvn & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; |
| 178 | if (vvn == VENDOR_V_22) |
| 179 | host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; |
| 180 | } |
| 181 | |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 182 | static struct sdhci_ops sdhci_esdhc_ops = { |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 183 | .read_l = esdhc_readl, |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 184 | .read_w = esdhc_readw, |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 185 | .read_b = esdhc_readb, |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 186 | .write_l = sdhci_be32bs_writel, |
| 187 | .write_w = esdhc_writew, |
| 188 | .write_b = esdhc_writeb, |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 189 | .set_clock = esdhc_of_set_clock, |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 190 | .enable_dma = esdhc_of_enable_dma, |
| 191 | .get_max_clock = esdhc_of_get_max_clock, |
| 192 | .get_min_clock = esdhc_of_get_min_clock, |
Jerry Huang | 63ef5d8 | 2012-10-25 13:47:19 +0800 | [diff] [blame] | 193 | .platform_init = esdhc_of_platform_init, |
Jerry Huang | 192b537 | 2012-02-04 17:13:13 -0500 | [diff] [blame] | 194 | #ifdef CONFIG_PM |
| 195 | .platform_suspend = esdhc_of_suspend, |
| 196 | .platform_resume = esdhc_of_resume, |
| 197 | #endif |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 198 | }; |
| 199 | |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 200 | static struct sdhci_pltfm_data sdhci_esdhc_pdata = { |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 201 | /* |
| 202 | * card detection could be handled via GPIO |
| 203 | * eSDHC cannot support End Attribute in NOP ADMA descriptor |
| 204 | */ |
Richard Zhu | e481e45 | 2011-03-21 13:22:13 +0800 | [diff] [blame] | 205 | .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 206 | | SDHCI_QUIRK_NO_CARD_NO_RESET |
| 207 | | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 208 | .ops = &sdhci_esdhc_ops, |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 209 | }; |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 210 | |
| 211 | static int __devinit sdhci_esdhc_probe(struct platform_device *pdev) |
| 212 | { |
| 213 | return sdhci_pltfm_register(pdev, &sdhci_esdhc_pdata); |
| 214 | } |
| 215 | |
| 216 | static int __devexit sdhci_esdhc_remove(struct platform_device *pdev) |
| 217 | { |
| 218 | return sdhci_pltfm_unregister(pdev); |
| 219 | } |
| 220 | |
| 221 | static const struct of_device_id sdhci_esdhc_of_match[] = { |
| 222 | { .compatible = "fsl,mpc8379-esdhc" }, |
| 223 | { .compatible = "fsl,mpc8536-esdhc" }, |
| 224 | { .compatible = "fsl,esdhc" }, |
| 225 | { } |
| 226 | }; |
| 227 | MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); |
| 228 | |
| 229 | static struct platform_driver sdhci_esdhc_driver = { |
| 230 | .driver = { |
| 231 | .name = "sdhci-esdhc", |
| 232 | .owner = THIS_MODULE, |
| 233 | .of_match_table = sdhci_esdhc_of_match, |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 234 | .pm = SDHCI_PLTFM_PMOPS, |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 235 | }, |
| 236 | .probe = sdhci_esdhc_probe, |
Bill Pemberton | 0433c14 | 2012-11-19 13:20:26 -0500 | [diff] [blame^] | 237 | .remove = sdhci_esdhc_remove, |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 238 | }; |
| 239 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 240 | module_platform_driver(sdhci_esdhc_driver); |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 241 | |
| 242 | MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); |
| 243 | MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " |
| 244 | "Anton Vorontsov <avorontsov@ru.mvista.com>"); |
| 245 | MODULE_LICENSE("GPL v2"); |