1*7657c3a7SAlbert Herranz /* 2*7657c3a7SAlbert Herranz * Freescale eSDHC controller driver. 3*7657c3a7SAlbert Herranz * 4*7657c3a7SAlbert Herranz * Copyright (c) 2007 Freescale Semiconductor, Inc. 5*7657c3a7SAlbert Herranz * Copyright (c) 2009 MontaVista Software, Inc. 6*7657c3a7SAlbert Herranz * 7*7657c3a7SAlbert Herranz * Authors: Xiaobo Xie <X.Xie@freescale.com> 8*7657c3a7SAlbert Herranz * Anton Vorontsov <avorontsov@ru.mvista.com> 9*7657c3a7SAlbert Herranz * 10*7657c3a7SAlbert Herranz * This program is free software; you can redistribute it and/or modify 11*7657c3a7SAlbert Herranz * it under the terms of the GNU General Public License as published by 12*7657c3a7SAlbert Herranz * the Free Software Foundation; either version 2 of the License, or (at 13*7657c3a7SAlbert Herranz * your option) any later version. 14*7657c3a7SAlbert Herranz */ 15*7657c3a7SAlbert Herranz 16*7657c3a7SAlbert Herranz #include <linux/io.h> 17*7657c3a7SAlbert Herranz #include <linux/delay.h> 18*7657c3a7SAlbert Herranz #include <linux/mmc/host.h> 19*7657c3a7SAlbert Herranz #include "sdhci-of.h" 20*7657c3a7SAlbert Herranz #include "sdhci.h" 21*7657c3a7SAlbert Herranz 22*7657c3a7SAlbert Herranz /* 23*7657c3a7SAlbert Herranz * Ops and quirks for the Freescale eSDHC controller. 24*7657c3a7SAlbert Herranz */ 25*7657c3a7SAlbert Herranz 26*7657c3a7SAlbert Herranz #define ESDHC_DMA_SYSCTL 0x40c 27*7657c3a7SAlbert Herranz #define ESDHC_DMA_SNOOP 0x00000040 28*7657c3a7SAlbert Herranz 29*7657c3a7SAlbert Herranz #define ESDHC_SYSTEM_CONTROL 0x2c 30*7657c3a7SAlbert Herranz #define ESDHC_CLOCK_MASK 0x0000fff0 31*7657c3a7SAlbert Herranz #define ESDHC_PREDIV_SHIFT 8 32*7657c3a7SAlbert Herranz #define ESDHC_DIVIDER_SHIFT 4 33*7657c3a7SAlbert Herranz #define ESDHC_CLOCK_PEREN 0x00000004 34*7657c3a7SAlbert Herranz #define ESDHC_CLOCK_HCKEN 0x00000002 35*7657c3a7SAlbert Herranz #define ESDHC_CLOCK_IPGEN 0x00000001 36*7657c3a7SAlbert Herranz 37*7657c3a7SAlbert Herranz #define ESDHC_HOST_CONTROL_RES 0x05 38*7657c3a7SAlbert Herranz 39*7657c3a7SAlbert Herranz static u16 esdhc_readw(struct sdhci_host *host, int reg) 40*7657c3a7SAlbert Herranz { 41*7657c3a7SAlbert Herranz u16 ret; 42*7657c3a7SAlbert Herranz 43*7657c3a7SAlbert Herranz if (unlikely(reg == SDHCI_HOST_VERSION)) 44*7657c3a7SAlbert Herranz ret = in_be16(host->ioaddr + reg); 45*7657c3a7SAlbert Herranz else 46*7657c3a7SAlbert Herranz ret = sdhci_be32bs_readw(host, reg); 47*7657c3a7SAlbert Herranz return ret; 48*7657c3a7SAlbert Herranz } 49*7657c3a7SAlbert Herranz 50*7657c3a7SAlbert Herranz static void esdhc_writew(struct sdhci_host *host, u16 val, int reg) 51*7657c3a7SAlbert Herranz { 52*7657c3a7SAlbert Herranz if (reg == SDHCI_BLOCK_SIZE) { 53*7657c3a7SAlbert Herranz /* 54*7657c3a7SAlbert Herranz * Two last DMA bits are reserved, and first one is used for 55*7657c3a7SAlbert Herranz * non-standard blksz of 4096 bytes that we don't support 56*7657c3a7SAlbert Herranz * yet. So clear the DMA boundary bits. 57*7657c3a7SAlbert Herranz */ 58*7657c3a7SAlbert Herranz val &= ~SDHCI_MAKE_BLKSZ(0x7, 0); 59*7657c3a7SAlbert Herranz } 60*7657c3a7SAlbert Herranz sdhci_be32bs_writew(host, val, reg); 61*7657c3a7SAlbert Herranz } 62*7657c3a7SAlbert Herranz 63*7657c3a7SAlbert Herranz static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg) 64*7657c3a7SAlbert Herranz { 65*7657c3a7SAlbert Herranz /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */ 66*7657c3a7SAlbert Herranz if (reg == SDHCI_HOST_CONTROL) 67*7657c3a7SAlbert Herranz val &= ~ESDHC_HOST_CONTROL_RES; 68*7657c3a7SAlbert Herranz sdhci_be32bs_writeb(host, val, reg); 69*7657c3a7SAlbert Herranz } 70*7657c3a7SAlbert Herranz 71*7657c3a7SAlbert Herranz static void esdhc_set_clock(struct sdhci_host *host, unsigned int clock) 72*7657c3a7SAlbert Herranz { 73*7657c3a7SAlbert Herranz int pre_div = 2; 74*7657c3a7SAlbert Herranz int div = 1; 75*7657c3a7SAlbert Herranz 76*7657c3a7SAlbert Herranz clrbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN | 77*7657c3a7SAlbert Herranz ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK); 78*7657c3a7SAlbert Herranz 79*7657c3a7SAlbert Herranz if (clock == 0) 80*7657c3a7SAlbert Herranz goto out; 81*7657c3a7SAlbert Herranz 82*7657c3a7SAlbert Herranz while (host->max_clk / pre_div / 16 > clock && pre_div < 256) 83*7657c3a7SAlbert Herranz pre_div *= 2; 84*7657c3a7SAlbert Herranz 85*7657c3a7SAlbert Herranz while (host->max_clk / pre_div / div > clock && div < 16) 86*7657c3a7SAlbert Herranz div++; 87*7657c3a7SAlbert Herranz 88*7657c3a7SAlbert Herranz dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", 89*7657c3a7SAlbert Herranz clock, host->max_clk / pre_div / div); 90*7657c3a7SAlbert Herranz 91*7657c3a7SAlbert Herranz pre_div >>= 1; 92*7657c3a7SAlbert Herranz div--; 93*7657c3a7SAlbert Herranz 94*7657c3a7SAlbert Herranz setbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN | 95*7657c3a7SAlbert Herranz ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN | 96*7657c3a7SAlbert Herranz div << ESDHC_DIVIDER_SHIFT | pre_div << ESDHC_PREDIV_SHIFT); 97*7657c3a7SAlbert Herranz mdelay(100); 98*7657c3a7SAlbert Herranz out: 99*7657c3a7SAlbert Herranz host->clock = clock; 100*7657c3a7SAlbert Herranz } 101*7657c3a7SAlbert Herranz 102*7657c3a7SAlbert Herranz static int esdhc_enable_dma(struct sdhci_host *host) 103*7657c3a7SAlbert Herranz { 104*7657c3a7SAlbert Herranz setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP); 105*7657c3a7SAlbert Herranz return 0; 106*7657c3a7SAlbert Herranz } 107*7657c3a7SAlbert Herranz 108*7657c3a7SAlbert Herranz static unsigned int esdhc_get_max_clock(struct sdhci_host *host) 109*7657c3a7SAlbert Herranz { 110*7657c3a7SAlbert Herranz struct sdhci_of_host *of_host = sdhci_priv(host); 111*7657c3a7SAlbert Herranz 112*7657c3a7SAlbert Herranz return of_host->clock; 113*7657c3a7SAlbert Herranz } 114*7657c3a7SAlbert Herranz 115*7657c3a7SAlbert Herranz static unsigned int esdhc_get_min_clock(struct sdhci_host *host) 116*7657c3a7SAlbert Herranz { 117*7657c3a7SAlbert Herranz struct sdhci_of_host *of_host = sdhci_priv(host); 118*7657c3a7SAlbert Herranz 119*7657c3a7SAlbert Herranz return of_host->clock / 256 / 16; 120*7657c3a7SAlbert Herranz } 121*7657c3a7SAlbert Herranz 122*7657c3a7SAlbert Herranz struct sdhci_of_data sdhci_esdhc = { 123*7657c3a7SAlbert Herranz .quirks = SDHCI_QUIRK_FORCE_BLK_SZ_2048 | 124*7657c3a7SAlbert Herranz SDHCI_QUIRK_BROKEN_CARD_DETECTION | 125*7657c3a7SAlbert Herranz SDHCI_QUIRK_NO_BUSY_IRQ | 126*7657c3a7SAlbert Herranz SDHCI_QUIRK_NONSTANDARD_CLOCK | 127*7657c3a7SAlbert Herranz SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | 128*7657c3a7SAlbert Herranz SDHCI_QUIRK_PIO_NEEDS_DELAY | 129*7657c3a7SAlbert Herranz SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET | 130*7657c3a7SAlbert Herranz SDHCI_QUIRK_NO_CARD_NO_RESET, 131*7657c3a7SAlbert Herranz .ops = { 132*7657c3a7SAlbert Herranz .readl = sdhci_be32bs_readl, 133*7657c3a7SAlbert Herranz .readw = esdhc_readw, 134*7657c3a7SAlbert Herranz .readb = sdhci_be32bs_readb, 135*7657c3a7SAlbert Herranz .writel = sdhci_be32bs_writel, 136*7657c3a7SAlbert Herranz .writew = esdhc_writew, 137*7657c3a7SAlbert Herranz .writeb = esdhc_writeb, 138*7657c3a7SAlbert Herranz .set_clock = esdhc_set_clock, 139*7657c3a7SAlbert Herranz .enable_dma = esdhc_enable_dma, 140*7657c3a7SAlbert Herranz .get_max_clock = esdhc_get_max_clock, 141*7657c3a7SAlbert Herranz .get_min_clock = esdhc_get_min_clock, 142*7657c3a7SAlbert Herranz }, 143*7657c3a7SAlbert Herranz }; 144