xref: /linux/drivers/mmc/host/sdhci-of-esdhc.c (revision 0433c14356702e296f474f77ebd42f0a9d9a5487)
17657c3a7SAlbert Herranz /*
27657c3a7SAlbert Herranz  * Freescale eSDHC controller driver.
37657c3a7SAlbert Herranz  *
4f060bc9cSJerry Huang  * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
57657c3a7SAlbert Herranz  * Copyright (c) 2009 MontaVista Software, Inc.
67657c3a7SAlbert Herranz  *
77657c3a7SAlbert Herranz  * Authors: Xiaobo Xie <X.Xie@freescale.com>
87657c3a7SAlbert Herranz  *	    Anton Vorontsov <avorontsov@ru.mvista.com>
97657c3a7SAlbert Herranz  *
107657c3a7SAlbert Herranz  * This program is free software; you can redistribute it and/or modify
117657c3a7SAlbert Herranz  * it under the terms of the GNU General Public License as published by
127657c3a7SAlbert Herranz  * the Free Software Foundation; either version 2 of the License, or (at
137657c3a7SAlbert Herranz  * your option) any later version.
147657c3a7SAlbert Herranz  */
157657c3a7SAlbert Herranz 
167657c3a7SAlbert Herranz #include <linux/io.h>
17f060bc9cSJerry Huang #include <linux/of.h>
187657c3a7SAlbert Herranz #include <linux/delay.h>
1988b47679SPaul Gortmaker #include <linux/module.h>
207657c3a7SAlbert Herranz #include <linux/mmc/host.h>
2138576af1SShawn Guo #include "sdhci-pltfm.h"
2280872e21SWolfram Sang #include "sdhci-esdhc.h"
237657c3a7SAlbert Herranz 
24137ccd46SJerry Huang #define VENDOR_V_22	0x12
25137ccd46SJerry Huang static u32 esdhc_readl(struct sdhci_host *host, int reg)
26137ccd46SJerry Huang {
27137ccd46SJerry Huang 	u32 ret;
28137ccd46SJerry Huang 
29137ccd46SJerry Huang 	ret = in_be32(host->ioaddr + reg);
30137ccd46SJerry Huang 	/*
31137ccd46SJerry Huang 	 * The bit of ADMA flag in eSDHC is not compatible with standard
32137ccd46SJerry Huang 	 * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
33137ccd46SJerry Huang 	 * supported by eSDHC.
34137ccd46SJerry Huang 	 * And for many FSL eSDHC controller, the reset value of field
35137ccd46SJerry Huang 	 * SDHCI_CAN_DO_ADMA1 is one, but some of them can't support ADMA,
36137ccd46SJerry Huang 	 * only these vendor version is greater than 2.2/0x12 support ADMA.
37137ccd46SJerry Huang 	 * For FSL eSDHC, must aligned 4-byte, so use 0xFC to read the
38137ccd46SJerry Huang 	 * the verdor version number, oxFE is SDHCI_HOST_VERSION.
39137ccd46SJerry Huang 	 */
40137ccd46SJerry Huang 	if ((reg == SDHCI_CAPABILITIES) && (ret & SDHCI_CAN_DO_ADMA1)) {
41137ccd46SJerry Huang 		u32 tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
42137ccd46SJerry Huang 		tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
43137ccd46SJerry Huang 		if (tmp > VENDOR_V_22)
44137ccd46SJerry Huang 			ret |= SDHCI_CAN_DO_ADMA2;
45137ccd46SJerry Huang 	}
46137ccd46SJerry Huang 
47137ccd46SJerry Huang 	return ret;
48137ccd46SJerry Huang }
49137ccd46SJerry Huang 
507657c3a7SAlbert Herranz static u16 esdhc_readw(struct sdhci_host *host, int reg)
517657c3a7SAlbert Herranz {
527657c3a7SAlbert Herranz 	u16 ret;
53e51cbc9eSXu lei 	int base = reg & ~0x3;
54e51cbc9eSXu lei 	int shift = (reg & 0x2) * 8;
557657c3a7SAlbert Herranz 
567657c3a7SAlbert Herranz 	if (unlikely(reg == SDHCI_HOST_VERSION))
57e51cbc9eSXu lei 		ret = in_be32(host->ioaddr + base) & 0xffff;
587657c3a7SAlbert Herranz 	else
59e51cbc9eSXu lei 		ret = (in_be32(host->ioaddr + base) >> shift) & 0xffff;
60e51cbc9eSXu lei 	return ret;
61e51cbc9eSXu lei }
62e51cbc9eSXu lei 
63e51cbc9eSXu lei static u8 esdhc_readb(struct sdhci_host *host, int reg)
64e51cbc9eSXu lei {
65e51cbc9eSXu lei 	int base = reg & ~0x3;
66e51cbc9eSXu lei 	int shift = (reg & 0x3) * 8;
67e51cbc9eSXu lei 	u8 ret = (in_be32(host->ioaddr + base) >> shift) & 0xff;
68ba8c4dc9SRoy Zang 
69ba8c4dc9SRoy Zang 	/*
70ba8c4dc9SRoy Zang 	 * "DMA select" locates at offset 0x28 in SD specification, but on
71ba8c4dc9SRoy Zang 	 * P5020 or P3041, it locates at 0x29.
72ba8c4dc9SRoy Zang 	 */
73ba8c4dc9SRoy Zang 	if (reg == SDHCI_HOST_CONTROL) {
74ba8c4dc9SRoy Zang 		u32 dma_bits;
75ba8c4dc9SRoy Zang 
76ba8c4dc9SRoy Zang 		dma_bits = in_be32(host->ioaddr + reg);
77ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
78ba8c4dc9SRoy Zang 		dma_bits = (dma_bits >> 5) & SDHCI_CTRL_DMA_MASK;
79ba8c4dc9SRoy Zang 
80ba8c4dc9SRoy Zang 		/* fixup the result */
81ba8c4dc9SRoy Zang 		ret &= ~SDHCI_CTRL_DMA_MASK;
82ba8c4dc9SRoy Zang 		ret |= dma_bits;
83ba8c4dc9SRoy Zang 	}
84ba8c4dc9SRoy Zang 
857657c3a7SAlbert Herranz 	return ret;
867657c3a7SAlbert Herranz }
877657c3a7SAlbert Herranz 
887657c3a7SAlbert Herranz static void esdhc_writew(struct sdhci_host *host, u16 val, int reg)
897657c3a7SAlbert Herranz {
907657c3a7SAlbert Herranz 	if (reg == SDHCI_BLOCK_SIZE) {
917657c3a7SAlbert Herranz 		/*
927657c3a7SAlbert Herranz 		 * Two last DMA bits are reserved, and first one is used for
937657c3a7SAlbert Herranz 		 * non-standard blksz of 4096 bytes that we don't support
947657c3a7SAlbert Herranz 		 * yet. So clear the DMA boundary bits.
957657c3a7SAlbert Herranz 		 */
967657c3a7SAlbert Herranz 		val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
977657c3a7SAlbert Herranz 	}
987657c3a7SAlbert Herranz 	sdhci_be32bs_writew(host, val, reg);
997657c3a7SAlbert Herranz }
1007657c3a7SAlbert Herranz 
1017657c3a7SAlbert Herranz static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg)
1027657c3a7SAlbert Herranz {
103ba8c4dc9SRoy Zang 	/*
104ba8c4dc9SRoy Zang 	 * "DMA select" location is offset 0x28 in SD specification, but on
105ba8c4dc9SRoy Zang 	 * P5020 or P3041, it's located at 0x29.
106ba8c4dc9SRoy Zang 	 */
107ba8c4dc9SRoy Zang 	if (reg == SDHCI_HOST_CONTROL) {
108ba8c4dc9SRoy Zang 		u32 dma_bits;
109ba8c4dc9SRoy Zang 
110ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
111ba8c4dc9SRoy Zang 		dma_bits = (val & SDHCI_CTRL_DMA_MASK) << 5;
112ba8c4dc9SRoy Zang 		clrsetbits_be32(host->ioaddr + reg , SDHCI_CTRL_DMA_MASK << 5,
113ba8c4dc9SRoy Zang 			dma_bits);
114ba8c4dc9SRoy Zang 		val &= ~SDHCI_CTRL_DMA_MASK;
115ba8c4dc9SRoy Zang 		val |= in_be32(host->ioaddr + reg) & SDHCI_CTRL_DMA_MASK;
116ba8c4dc9SRoy Zang 	}
117ba8c4dc9SRoy Zang 
1187657c3a7SAlbert Herranz 	/* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */
1197657c3a7SAlbert Herranz 	if (reg == SDHCI_HOST_CONTROL)
1207657c3a7SAlbert Herranz 		val &= ~ESDHC_HOST_CONTROL_RES;
1217657c3a7SAlbert Herranz 	sdhci_be32bs_writeb(host, val, reg);
1227657c3a7SAlbert Herranz }
1237657c3a7SAlbert Herranz 
12480872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host)
1257657c3a7SAlbert Herranz {
1267657c3a7SAlbert Herranz 	setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP);
1277657c3a7SAlbert Herranz 	return 0;
1287657c3a7SAlbert Herranz }
1297657c3a7SAlbert Herranz 
13080872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
1317657c3a7SAlbert Herranz {
132e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1337657c3a7SAlbert Herranz 
134e307148fSShawn Guo 	return pltfm_host->clock;
1357657c3a7SAlbert Herranz }
1367657c3a7SAlbert Herranz 
13780872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
1387657c3a7SAlbert Herranz {
139e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1407657c3a7SAlbert Herranz 
141e307148fSShawn Guo 	return pltfm_host->clock / 256 / 16;
1427657c3a7SAlbert Herranz }
1437657c3a7SAlbert Herranz 
144f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
145f060bc9cSJerry Huang {
146f060bc9cSJerry Huang 	/* Workaround to reduce the clock frequency for p1010 esdhc */
147f060bc9cSJerry Huang 	if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
148f060bc9cSJerry Huang 		if (clock > 20000000)
149f060bc9cSJerry Huang 			clock -= 5000000;
150f060bc9cSJerry Huang 		if (clock > 40000000)
151f060bc9cSJerry Huang 			clock -= 5000000;
152f060bc9cSJerry Huang 	}
153f060bc9cSJerry Huang 
154f060bc9cSJerry Huang 	/* Set the clock */
155f060bc9cSJerry Huang 	esdhc_set_clock(host, clock);
156f060bc9cSJerry Huang }
157f060bc9cSJerry Huang 
158192b5372SJerry Huang #ifdef CONFIG_PM
159192b5372SJerry Huang static u32 esdhc_proctl;
160192b5372SJerry Huang static void esdhc_of_suspend(struct sdhci_host *host)
161192b5372SJerry Huang {
162192b5372SJerry Huang 	esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL);
163192b5372SJerry Huang }
164192b5372SJerry Huang 
165192b5372SJerry Huang static void esdhc_of_resume(struct sdhci_host *host)
166192b5372SJerry Huang {
167192b5372SJerry Huang 	esdhc_of_enable_dma(host);
168192b5372SJerry Huang 	sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
169192b5372SJerry Huang }
170192b5372SJerry Huang #endif
171192b5372SJerry Huang 
17263ef5d8cSJerry Huang static void esdhc_of_platform_init(struct sdhci_host *host)
17363ef5d8cSJerry Huang {
17463ef5d8cSJerry Huang 	u32 vvn;
17563ef5d8cSJerry Huang 
17663ef5d8cSJerry Huang 	vvn = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS);
17763ef5d8cSJerry Huang 	vvn = (vvn & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT;
17863ef5d8cSJerry Huang 	if (vvn == VENDOR_V_22)
17963ef5d8cSJerry Huang 		host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
18063ef5d8cSJerry Huang }
18163ef5d8cSJerry Huang 
182e307148fSShawn Guo static struct sdhci_ops sdhci_esdhc_ops = {
183137ccd46SJerry Huang 	.read_l = esdhc_readl,
184dc297c92SMatt Fleming 	.read_w = esdhc_readw,
185e51cbc9eSXu lei 	.read_b = esdhc_readb,
186dc297c92SMatt Fleming 	.write_l = sdhci_be32bs_writel,
187dc297c92SMatt Fleming 	.write_w = esdhc_writew,
188dc297c92SMatt Fleming 	.write_b = esdhc_writeb,
189f060bc9cSJerry Huang 	.set_clock = esdhc_of_set_clock,
19080872e21SWolfram Sang 	.enable_dma = esdhc_of_enable_dma,
19180872e21SWolfram Sang 	.get_max_clock = esdhc_of_get_max_clock,
19280872e21SWolfram Sang 	.get_min_clock = esdhc_of_get_min_clock,
19363ef5d8cSJerry Huang 	.platform_init = esdhc_of_platform_init,
194192b5372SJerry Huang #ifdef CONFIG_PM
195192b5372SJerry Huang 	.platform_suspend = esdhc_of_suspend,
196192b5372SJerry Huang 	.platform_resume = esdhc_of_resume,
197192b5372SJerry Huang #endif
198e307148fSShawn Guo };
199e307148fSShawn Guo 
20038576af1SShawn Guo static struct sdhci_pltfm_data sdhci_esdhc_pdata = {
201137ccd46SJerry Huang 	/*
202137ccd46SJerry Huang 	 * card detection could be handled via GPIO
203137ccd46SJerry Huang 	 * eSDHC cannot support End Attribute in NOP ADMA descriptor
204137ccd46SJerry Huang 	 */
205e307148fSShawn Guo 	.quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION
206137ccd46SJerry Huang 		| SDHCI_QUIRK_NO_CARD_NO_RESET
207137ccd46SJerry Huang 		| SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
208e307148fSShawn Guo 	.ops = &sdhci_esdhc_ops,
2097657c3a7SAlbert Herranz };
21038576af1SShawn Guo 
21138576af1SShawn Guo static int __devinit sdhci_esdhc_probe(struct platform_device *pdev)
21238576af1SShawn Guo {
21338576af1SShawn Guo 	return sdhci_pltfm_register(pdev, &sdhci_esdhc_pdata);
21438576af1SShawn Guo }
21538576af1SShawn Guo 
21638576af1SShawn Guo static int __devexit sdhci_esdhc_remove(struct platform_device *pdev)
21738576af1SShawn Guo {
21838576af1SShawn Guo 	return sdhci_pltfm_unregister(pdev);
21938576af1SShawn Guo }
22038576af1SShawn Guo 
22138576af1SShawn Guo static const struct of_device_id sdhci_esdhc_of_match[] = {
22238576af1SShawn Guo 	{ .compatible = "fsl,mpc8379-esdhc" },
22338576af1SShawn Guo 	{ .compatible = "fsl,mpc8536-esdhc" },
22438576af1SShawn Guo 	{ .compatible = "fsl,esdhc" },
22538576af1SShawn Guo 	{ }
22638576af1SShawn Guo };
22738576af1SShawn Guo MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
22838576af1SShawn Guo 
22938576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = {
23038576af1SShawn Guo 	.driver = {
23138576af1SShawn Guo 		.name = "sdhci-esdhc",
23238576af1SShawn Guo 		.owner = THIS_MODULE,
23338576af1SShawn Guo 		.of_match_table = sdhci_esdhc_of_match,
23429495aa0SManuel Lauss 		.pm = SDHCI_PLTFM_PMOPS,
23538576af1SShawn Guo 	},
23638576af1SShawn Guo 	.probe = sdhci_esdhc_probe,
237*0433c143SBill Pemberton 	.remove = sdhci_esdhc_remove,
23838576af1SShawn Guo };
23938576af1SShawn Guo 
240d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver);
24138576af1SShawn Guo 
24238576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
24338576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
24438576af1SShawn Guo 	      "Anton Vorontsov <avorontsov@ru.mvista.com>");
24538576af1SShawn Guo MODULE_LICENSE("GPL v2");
246