Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3 | * |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 4 | * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Pierre Ossman | 643f720 | 2006-09-30 23:27:52 -0700 | [diff] [blame] | 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or (at |
| 9 | * your option) any later version. |
Pierre Ossman | 84c46a5 | 2007-12-02 19:58:16 +0100 | [diff] [blame] | 10 | * |
| 11 | * Thanks to the following companies for their support: |
| 12 | * |
| 13 | * - JMicron (hardware and technical support) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 16 | #include <linux/delay.h> |
| 17 | #include <linux/highmem.h> |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 20 | #include <linux/scatterlist.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 21 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 22 | #include <linux/leds.h> |
| 23 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 24 | #include <linux/mmc/host.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 25 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 26 | #include "sdhci.h" |
| 27 | |
| 28 | #define DRIVER_NAME "sdhci" |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 29 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 30 | #define DBG(f, x...) \ |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 31 | pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 32 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 33 | static unsigned int debug_quirks = 0; |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 34 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 35 | static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *); |
| 36 | static void sdhci_finish_data(struct sdhci_host *); |
| 37 | |
| 38 | static void sdhci_send_command(struct sdhci_host *, struct mmc_command *); |
| 39 | static void sdhci_finish_command(struct sdhci_host *); |
| 40 | |
| 41 | static void sdhci_dumpregs(struct sdhci_host *host) |
| 42 | { |
| 43 | printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n"); |
| 44 | |
| 45 | printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n", |
| 46 | readl(host->ioaddr + SDHCI_DMA_ADDRESS), |
| 47 | readw(host->ioaddr + SDHCI_HOST_VERSION)); |
| 48 | printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n", |
| 49 | readw(host->ioaddr + SDHCI_BLOCK_SIZE), |
| 50 | readw(host->ioaddr + SDHCI_BLOCK_COUNT)); |
| 51 | printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n", |
| 52 | readl(host->ioaddr + SDHCI_ARGUMENT), |
| 53 | readw(host->ioaddr + SDHCI_TRANSFER_MODE)); |
| 54 | printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n", |
| 55 | readl(host->ioaddr + SDHCI_PRESENT_STATE), |
| 56 | readb(host->ioaddr + SDHCI_HOST_CONTROL)); |
| 57 | printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n", |
| 58 | readb(host->ioaddr + SDHCI_POWER_CONTROL), |
| 59 | readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL)); |
| 60 | printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n", |
Nicolas Pitre | 2df3b71 | 2007-09-29 10:46:20 -0400 | [diff] [blame] | 61 | readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL), |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 62 | readw(host->ioaddr + SDHCI_CLOCK_CONTROL)); |
| 63 | printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n", |
| 64 | readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL), |
| 65 | readl(host->ioaddr + SDHCI_INT_STATUS)); |
| 66 | printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n", |
| 67 | readl(host->ioaddr + SDHCI_INT_ENABLE), |
| 68 | readl(host->ioaddr + SDHCI_SIGNAL_ENABLE)); |
| 69 | printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n", |
| 70 | readw(host->ioaddr + SDHCI_ACMD12_ERR), |
| 71 | readw(host->ioaddr + SDHCI_SLOT_INT_STATUS)); |
| 72 | printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n", |
| 73 | readl(host->ioaddr + SDHCI_CAPABILITIES), |
| 74 | readl(host->ioaddr + SDHCI_MAX_CURRENT)); |
| 75 | |
| 76 | printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n"); |
| 77 | } |
| 78 | |
| 79 | /*****************************************************************************\ |
| 80 | * * |
| 81 | * Low level functions * |
| 82 | * * |
| 83 | \*****************************************************************************/ |
| 84 | |
| 85 | static void sdhci_reset(struct sdhci_host *host, u8 mask) |
| 86 | { |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 87 | unsigned long timeout; |
| 88 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 89 | if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 90 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & |
| 91 | SDHCI_CARD_PRESENT)) |
| 92 | return; |
| 93 | } |
| 94 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 95 | writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET); |
| 96 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 97 | if (mask & SDHCI_RESET_ALL) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 98 | host->clock = 0; |
| 99 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 100 | /* Wait max 100 ms */ |
| 101 | timeout = 100; |
| 102 | |
| 103 | /* hw clears the bit when it's done */ |
| 104 | while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) { |
| 105 | if (timeout == 0) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 106 | printk(KERN_ERR "%s: Reset 0x%x never completed.\n", |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 107 | mmc_hostname(host->mmc), (int)mask); |
| 108 | sdhci_dumpregs(host); |
| 109 | return; |
| 110 | } |
| 111 | timeout--; |
| 112 | mdelay(1); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | static void sdhci_init(struct sdhci_host *host) |
| 117 | { |
| 118 | u32 intmask; |
| 119 | |
| 120 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 121 | |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 122 | intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | |
| 123 | SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | |
| 124 | SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | |
| 125 | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 126 | SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 127 | SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | |
| 128 | SDHCI_INT_ADMA_ERROR; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 129 | |
| 130 | writel(intmask, host->ioaddr + SDHCI_INT_ENABLE); |
| 131 | writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void sdhci_activate_led(struct sdhci_host *host) |
| 135 | { |
| 136 | u8 ctrl; |
| 137 | |
| 138 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 139 | ctrl |= SDHCI_CTRL_LED; |
| 140 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 141 | } |
| 142 | |
| 143 | static void sdhci_deactivate_led(struct sdhci_host *host) |
| 144 | { |
| 145 | u8 ctrl; |
| 146 | |
| 147 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 148 | ctrl &= ~SDHCI_CTRL_LED; |
| 149 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 150 | } |
| 151 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 152 | #ifdef CONFIG_LEDS_CLASS |
| 153 | static void sdhci_led_control(struct led_classdev *led, |
| 154 | enum led_brightness brightness) |
| 155 | { |
| 156 | struct sdhci_host *host = container_of(led, struct sdhci_host, led); |
| 157 | unsigned long flags; |
| 158 | |
| 159 | spin_lock_irqsave(&host->lock, flags); |
| 160 | |
| 161 | if (brightness == LED_OFF) |
| 162 | sdhci_deactivate_led(host); |
| 163 | else |
| 164 | sdhci_activate_led(host); |
| 165 | |
| 166 | spin_unlock_irqrestore(&host->lock, flags); |
| 167 | } |
| 168 | #endif |
| 169 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 170 | /*****************************************************************************\ |
| 171 | * * |
| 172 | * Core functions * |
| 173 | * * |
| 174 | \*****************************************************************************/ |
| 175 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 176 | static void sdhci_read_block_pio(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 177 | { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 178 | unsigned long flags; |
| 179 | size_t blksize, len, chunk; |
| 180 | u32 scratch; |
| 181 | u8 *buf; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 182 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 183 | DBG("PIO reading\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 184 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 185 | blksize = host->data->blksz; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 186 | chunk = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 187 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 188 | local_irq_save(flags); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 189 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 190 | while (blksize) { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 191 | if (!sg_miter_next(&host->sg_miter)) |
| 192 | BUG(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 193 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 194 | len = min(host->sg_miter.length, blksize); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 195 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 196 | blksize -= len; |
| 197 | host->sg_miter.consumed = len; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 198 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 199 | buf = host->sg_miter.addr; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 200 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 201 | while (len) { |
| 202 | if (chunk == 0) { |
| 203 | scratch = readl(host->ioaddr + SDHCI_BUFFER); |
| 204 | chunk = 4; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 205 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 206 | |
| 207 | *buf = scratch & 0xFF; |
| 208 | |
| 209 | buf++; |
| 210 | scratch >>= 8; |
| 211 | chunk--; |
| 212 | len--; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 215 | |
| 216 | sg_miter_stop(&host->sg_miter); |
| 217 | |
| 218 | local_irq_restore(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 219 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 220 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 221 | static void sdhci_write_block_pio(struct sdhci_host *host) |
| 222 | { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 223 | unsigned long flags; |
| 224 | size_t blksize, len, chunk; |
| 225 | u32 scratch; |
| 226 | u8 *buf; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 227 | |
| 228 | DBG("PIO writing\n"); |
| 229 | |
| 230 | blksize = host->data->blksz; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 231 | chunk = 0; |
| 232 | scratch = 0; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 233 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 234 | local_irq_save(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 235 | |
| 236 | while (blksize) { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 237 | if (!sg_miter_next(&host->sg_miter)) |
| 238 | BUG(); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 239 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 240 | len = min(host->sg_miter.length, blksize); |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 241 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 242 | blksize -= len; |
| 243 | host->sg_miter.consumed = len; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 244 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 245 | buf = host->sg_miter.addr; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 246 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 247 | while (len) { |
| 248 | scratch |= (u32)*buf << (chunk * 8); |
| 249 | |
| 250 | buf++; |
| 251 | chunk++; |
| 252 | len--; |
| 253 | |
| 254 | if ((chunk == 4) || ((len == 0) && (blksize == 0))) { |
| 255 | writel(scratch, host->ioaddr + SDHCI_BUFFER); |
| 256 | chunk = 0; |
| 257 | scratch = 0; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 258 | } |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 259 | } |
| 260 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 261 | |
| 262 | sg_miter_stop(&host->sg_miter); |
| 263 | |
| 264 | local_irq_restore(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void sdhci_transfer_pio(struct sdhci_host *host) |
| 268 | { |
| 269 | u32 mask; |
| 270 | |
| 271 | BUG_ON(!host->data); |
| 272 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 273 | if (host->blocks == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 274 | return; |
| 275 | |
| 276 | if (host->data->flags & MMC_DATA_READ) |
| 277 | mask = SDHCI_DATA_AVAILABLE; |
| 278 | else |
| 279 | mask = SDHCI_SPACE_AVAILABLE; |
| 280 | |
| 281 | while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) { |
| 282 | if (host->data->flags & MMC_DATA_READ) |
| 283 | sdhci_read_block_pio(host); |
| 284 | else |
| 285 | sdhci_write_block_pio(host); |
| 286 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 287 | host->blocks--; |
| 288 | if (host->blocks == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 289 | break; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | DBG("PIO transfer complete.\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 295 | static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags) |
| 296 | { |
| 297 | local_irq_save(*flags); |
| 298 | return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset; |
| 299 | } |
| 300 | |
| 301 | static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags) |
| 302 | { |
| 303 | kunmap_atomic(buffer, KM_BIO_SRC_IRQ); |
| 304 | local_irq_restore(*flags); |
| 305 | } |
| 306 | |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 307 | static int sdhci_adma_table_pre(struct sdhci_host *host, |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 308 | struct mmc_data *data) |
| 309 | { |
| 310 | int direction; |
| 311 | |
| 312 | u8 *desc; |
| 313 | u8 *align; |
| 314 | dma_addr_t addr; |
| 315 | dma_addr_t align_addr; |
| 316 | int len, offset; |
| 317 | |
| 318 | struct scatterlist *sg; |
| 319 | int i; |
| 320 | char *buffer; |
| 321 | unsigned long flags; |
| 322 | |
| 323 | /* |
| 324 | * The spec does not specify endianness of descriptor table. |
| 325 | * We currently guess that it is LE. |
| 326 | */ |
| 327 | |
| 328 | if (data->flags & MMC_DATA_READ) |
| 329 | direction = DMA_FROM_DEVICE; |
| 330 | else |
| 331 | direction = DMA_TO_DEVICE; |
| 332 | |
| 333 | /* |
| 334 | * The ADMA descriptor table is mapped further down as we |
| 335 | * need to fill it with data first. |
| 336 | */ |
| 337 | |
| 338 | host->align_addr = dma_map_single(mmc_dev(host->mmc), |
| 339 | host->align_buffer, 128 * 4, direction); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame^] | 340 | if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr)) |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 341 | goto fail; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 342 | BUG_ON(host->align_addr & 0x3); |
| 343 | |
| 344 | host->sg_count = dma_map_sg(mmc_dev(host->mmc), |
| 345 | data->sg, data->sg_len, direction); |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 346 | if (host->sg_count == 0) |
| 347 | goto unmap_align; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 348 | |
| 349 | desc = host->adma_desc; |
| 350 | align = host->align_buffer; |
| 351 | |
| 352 | align_addr = host->align_addr; |
| 353 | |
| 354 | for_each_sg(data->sg, sg, host->sg_count, i) { |
| 355 | addr = sg_dma_address(sg); |
| 356 | len = sg_dma_len(sg); |
| 357 | |
| 358 | /* |
| 359 | * The SDHCI specification states that ADMA |
| 360 | * addresses must be 32-bit aligned. If they |
| 361 | * aren't, then we use a bounce buffer for |
| 362 | * the (up to three) bytes that screw up the |
| 363 | * alignment. |
| 364 | */ |
| 365 | offset = (4 - (addr & 0x3)) & 0x3; |
| 366 | if (offset) { |
| 367 | if (data->flags & MMC_DATA_WRITE) { |
| 368 | buffer = sdhci_kmap_atomic(sg, &flags); |
Pierre Ossman | 6cefd05 | 2008-07-21 00:45:15 +0200 | [diff] [blame] | 369 | WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3)); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 370 | memcpy(align, buffer, offset); |
| 371 | sdhci_kunmap_atomic(buffer, &flags); |
| 372 | } |
| 373 | |
| 374 | desc[7] = (align_addr >> 24) & 0xff; |
| 375 | desc[6] = (align_addr >> 16) & 0xff; |
| 376 | desc[5] = (align_addr >> 8) & 0xff; |
| 377 | desc[4] = (align_addr >> 0) & 0xff; |
| 378 | |
| 379 | BUG_ON(offset > 65536); |
| 380 | |
| 381 | desc[3] = (offset >> 8) & 0xff; |
| 382 | desc[2] = (offset >> 0) & 0xff; |
| 383 | |
| 384 | desc[1] = 0x00; |
| 385 | desc[0] = 0x21; /* tran, valid */ |
| 386 | |
| 387 | align += 4; |
| 388 | align_addr += 4; |
| 389 | |
| 390 | desc += 8; |
| 391 | |
| 392 | addr += offset; |
| 393 | len -= offset; |
| 394 | } |
| 395 | |
| 396 | desc[7] = (addr >> 24) & 0xff; |
| 397 | desc[6] = (addr >> 16) & 0xff; |
| 398 | desc[5] = (addr >> 8) & 0xff; |
| 399 | desc[4] = (addr >> 0) & 0xff; |
| 400 | |
| 401 | BUG_ON(len > 65536); |
| 402 | |
| 403 | desc[3] = (len >> 8) & 0xff; |
| 404 | desc[2] = (len >> 0) & 0xff; |
| 405 | |
| 406 | desc[1] = 0x00; |
| 407 | desc[0] = 0x21; /* tran, valid */ |
| 408 | |
| 409 | desc += 8; |
| 410 | |
| 411 | /* |
| 412 | * If this triggers then we have a calculation bug |
| 413 | * somewhere. :/ |
| 414 | */ |
| 415 | WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4); |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Add a terminating entry. |
| 420 | */ |
| 421 | desc[7] = 0; |
| 422 | desc[6] = 0; |
| 423 | desc[5] = 0; |
| 424 | desc[4] = 0; |
| 425 | |
| 426 | desc[3] = 0; |
| 427 | desc[2] = 0; |
| 428 | |
| 429 | desc[1] = 0x00; |
| 430 | desc[0] = 0x03; /* nop, end, valid */ |
| 431 | |
| 432 | /* |
| 433 | * Resync align buffer as we might have changed it. |
| 434 | */ |
| 435 | if (data->flags & MMC_DATA_WRITE) { |
| 436 | dma_sync_single_for_device(mmc_dev(host->mmc), |
| 437 | host->align_addr, 128 * 4, direction); |
| 438 | } |
| 439 | |
| 440 | host->adma_addr = dma_map_single(mmc_dev(host->mmc), |
| 441 | host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame^] | 442 | if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr)) |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 443 | goto unmap_entries; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 444 | BUG_ON(host->adma_addr & 0x3); |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 445 | |
| 446 | return 0; |
| 447 | |
| 448 | unmap_entries: |
| 449 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
| 450 | data->sg_len, direction); |
| 451 | unmap_align: |
| 452 | dma_unmap_single(mmc_dev(host->mmc), host->align_addr, |
| 453 | 128 * 4, direction); |
| 454 | fail: |
| 455 | return -EINVAL; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static void sdhci_adma_table_post(struct sdhci_host *host, |
| 459 | struct mmc_data *data) |
| 460 | { |
| 461 | int direction; |
| 462 | |
| 463 | struct scatterlist *sg; |
| 464 | int i, size; |
| 465 | u8 *align; |
| 466 | char *buffer; |
| 467 | unsigned long flags; |
| 468 | |
| 469 | if (data->flags & MMC_DATA_READ) |
| 470 | direction = DMA_FROM_DEVICE; |
| 471 | else |
| 472 | direction = DMA_TO_DEVICE; |
| 473 | |
| 474 | dma_unmap_single(mmc_dev(host->mmc), host->adma_addr, |
| 475 | (128 * 2 + 1) * 4, DMA_TO_DEVICE); |
| 476 | |
| 477 | dma_unmap_single(mmc_dev(host->mmc), host->align_addr, |
| 478 | 128 * 4, direction); |
| 479 | |
| 480 | if (data->flags & MMC_DATA_READ) { |
| 481 | dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg, |
| 482 | data->sg_len, direction); |
| 483 | |
| 484 | align = host->align_buffer; |
| 485 | |
| 486 | for_each_sg(data->sg, sg, host->sg_count, i) { |
| 487 | if (sg_dma_address(sg) & 0x3) { |
| 488 | size = 4 - (sg_dma_address(sg) & 0x3); |
| 489 | |
| 490 | buffer = sdhci_kmap_atomic(sg, &flags); |
Pierre Ossman | 6cefd05 | 2008-07-21 00:45:15 +0200 | [diff] [blame] | 491 | WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3)); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 492 | memcpy(buffer, align, size); |
| 493 | sdhci_kunmap_atomic(buffer, &flags); |
| 494 | |
| 495 | align += 4; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
| 501 | data->sg_len, direction); |
| 502 | } |
| 503 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 504 | static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 505 | { |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 506 | u8 count; |
| 507 | unsigned target_timeout, current_timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 508 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 509 | /* |
| 510 | * If the host controller provides us with an incorrect timeout |
| 511 | * value, just skip the check and use 0xE. The hardware may take |
| 512 | * longer to time out, but that's much better than having a too-short |
| 513 | * timeout value. |
| 514 | */ |
| 515 | if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)) |
| 516 | return 0xE; |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 517 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 518 | /* timeout in us */ |
| 519 | target_timeout = data->timeout_ns / 1000 + |
| 520 | data->timeout_clks / host->clock; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 521 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 522 | /* |
| 523 | * Figure out needed cycles. |
| 524 | * We do this in steps in order to fit inside a 32 bit int. |
| 525 | * The first step is the minimum timeout, which will have a |
| 526 | * minimum resolution of 6 bits: |
| 527 | * (1) 2^13*1000 > 2^22, |
| 528 | * (2) host->timeout_clk < 2^16 |
| 529 | * => |
| 530 | * (1) / (2) > 2^6 |
| 531 | */ |
| 532 | count = 0; |
| 533 | current_timeout = (1 << 13) * 1000 / host->timeout_clk; |
| 534 | while (current_timeout < target_timeout) { |
| 535 | count++; |
| 536 | current_timeout <<= 1; |
| 537 | if (count >= 0xF) |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | if (count >= 0xF) { |
| 542 | printk(KERN_WARNING "%s: Too large timeout requested!\n", |
| 543 | mmc_hostname(host->mmc)); |
| 544 | count = 0xE; |
| 545 | } |
| 546 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 547 | return count; |
| 548 | } |
| 549 | |
| 550 | static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data) |
| 551 | { |
| 552 | u8 count; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 553 | u8 ctrl; |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 554 | int ret; |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 555 | |
| 556 | WARN_ON(host->data); |
| 557 | |
| 558 | if (data == NULL) |
| 559 | return; |
| 560 | |
| 561 | /* Sanity checks */ |
| 562 | BUG_ON(data->blksz * data->blocks > 524288); |
| 563 | BUG_ON(data->blksz > host->mmc->max_blk_size); |
| 564 | BUG_ON(data->blocks > 65535); |
| 565 | |
| 566 | host->data = data; |
| 567 | host->data_early = 0; |
| 568 | |
| 569 | count = sdhci_calc_timeout(host, data); |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 570 | writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 571 | |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 572 | if (host->flags & SDHCI_USE_DMA) |
| 573 | host->flags |= SDHCI_REQ_USE_DMA; |
| 574 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 575 | /* |
| 576 | * FIXME: This doesn't account for merging when mapping the |
| 577 | * scatterlist. |
| 578 | */ |
| 579 | if (host->flags & SDHCI_REQ_USE_DMA) { |
| 580 | int broken, i; |
| 581 | struct scatterlist *sg; |
| 582 | |
| 583 | broken = 0; |
| 584 | if (host->flags & SDHCI_USE_ADMA) { |
| 585 | if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) |
| 586 | broken = 1; |
| 587 | } else { |
| 588 | if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) |
| 589 | broken = 1; |
| 590 | } |
| 591 | |
| 592 | if (unlikely(broken)) { |
| 593 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 594 | if (sg->length & 0x3) { |
| 595 | DBG("Reverting to PIO because of " |
| 596 | "transfer size (%d)\n", |
| 597 | sg->length); |
| 598 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | } |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
| 606 | * The assumption here being that alignment is the same after |
| 607 | * translation to device address space. |
| 608 | */ |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 609 | if (host->flags & SDHCI_REQ_USE_DMA) { |
| 610 | int broken, i; |
| 611 | struct scatterlist *sg; |
| 612 | |
| 613 | broken = 0; |
| 614 | if (host->flags & SDHCI_USE_ADMA) { |
| 615 | /* |
| 616 | * As we use 3 byte chunks to work around |
| 617 | * alignment problems, we need to check this |
| 618 | * quirk. |
| 619 | */ |
| 620 | if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) |
| 621 | broken = 1; |
| 622 | } else { |
| 623 | if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) |
| 624 | broken = 1; |
| 625 | } |
| 626 | |
| 627 | if (unlikely(broken)) { |
| 628 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 629 | if (sg->offset & 0x3) { |
| 630 | DBG("Reverting to PIO because of " |
| 631 | "bad alignment\n"); |
| 632 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 639 | if (host->flags & SDHCI_REQ_USE_DMA) { |
| 640 | if (host->flags & SDHCI_USE_ADMA) { |
| 641 | ret = sdhci_adma_table_pre(host, data); |
| 642 | if (ret) { |
| 643 | /* |
| 644 | * This only happens when someone fed |
| 645 | * us an invalid request. |
| 646 | */ |
| 647 | WARN_ON(1); |
| 648 | host->flags &= ~SDHCI_USE_DMA; |
| 649 | } else { |
| 650 | writel(host->adma_addr, |
| 651 | host->ioaddr + SDHCI_ADMA_ADDRESS); |
| 652 | } |
| 653 | } else { |
Tomas Winkler | c8b3e02 | 2008-07-05 19:52:04 +0300 | [diff] [blame] | 654 | int sg_cnt; |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 655 | |
Tomas Winkler | c8b3e02 | 2008-07-05 19:52:04 +0300 | [diff] [blame] | 656 | sg_cnt = dma_map_sg(mmc_dev(host->mmc), |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 657 | data->sg, data->sg_len, |
| 658 | (data->flags & MMC_DATA_READ) ? |
| 659 | DMA_FROM_DEVICE : |
| 660 | DMA_TO_DEVICE); |
Tomas Winkler | c8b3e02 | 2008-07-05 19:52:04 +0300 | [diff] [blame] | 661 | if (sg_cnt == 0) { |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 662 | /* |
| 663 | * This only happens when someone fed |
| 664 | * us an invalid request. |
| 665 | */ |
| 666 | WARN_ON(1); |
| 667 | host->flags &= ~SDHCI_USE_DMA; |
| 668 | } else { |
Pierre Ossman | 719a61b | 2008-07-22 13:23:23 +0200 | [diff] [blame] | 669 | WARN_ON(sg_cnt != 1); |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 670 | writel(sg_dma_address(data->sg), |
| 671 | host->ioaddr + SDHCI_DMA_ADDRESS); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 676 | /* |
| 677 | * Always adjust the DMA selection as some controllers |
| 678 | * (e.g. JMicron) can't do PIO properly when the selection |
| 679 | * is ADMA. |
| 680 | */ |
| 681 | if (host->version >= SDHCI_SPEC_200) { |
| 682 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 683 | ctrl &= ~SDHCI_CTRL_DMA_MASK; |
| 684 | if ((host->flags & SDHCI_REQ_USE_DMA) && |
| 685 | (host->flags & SDHCI_USE_ADMA)) |
| 686 | ctrl |= SDHCI_CTRL_ADMA32; |
| 687 | else |
| 688 | ctrl |= SDHCI_CTRL_SDMA; |
| 689 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 692 | if (!(host->flags & SDHCI_REQ_USE_DMA)) { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 693 | sg_miter_start(&host->sg_miter, |
| 694 | data->sg, data->sg_len, SG_MITER_ATOMIC); |
| 695 | host->blocks = data->blocks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 696 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 697 | |
Pierre Ossman | bab7696 | 2006-07-02 16:51:35 +0100 | [diff] [blame] | 698 | /* We do not handle DMA boundaries, so set it to max (512 KiB) */ |
| 699 | writew(SDHCI_MAKE_BLKSZ(7, data->blksz), |
| 700 | host->ioaddr + SDHCI_BLOCK_SIZE); |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 701 | writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT); |
| 702 | } |
| 703 | |
| 704 | static void sdhci_set_transfer_mode(struct sdhci_host *host, |
| 705 | struct mmc_data *data) |
| 706 | { |
| 707 | u16 mode; |
| 708 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 709 | if (data == NULL) |
| 710 | return; |
| 711 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 712 | WARN_ON(!host->data); |
| 713 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 714 | mode = SDHCI_TRNS_BLK_CNT_EN; |
| 715 | if (data->blocks > 1) |
| 716 | mode |= SDHCI_TRNS_MULTI; |
| 717 | if (data->flags & MMC_DATA_READ) |
| 718 | mode |= SDHCI_TRNS_READ; |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 719 | if (host->flags & SDHCI_REQ_USE_DMA) |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 720 | mode |= SDHCI_TRNS_DMA; |
| 721 | |
| 722 | writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static void sdhci_finish_data(struct sdhci_host *host) |
| 726 | { |
| 727 | struct mmc_data *data; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 728 | |
| 729 | BUG_ON(!host->data); |
| 730 | |
| 731 | data = host->data; |
| 732 | host->data = NULL; |
| 733 | |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 734 | if (host->flags & SDHCI_REQ_USE_DMA) { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 735 | if (host->flags & SDHCI_USE_ADMA) |
| 736 | sdhci_adma_table_post(host, data); |
| 737 | else { |
| 738 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
| 739 | data->sg_len, (data->flags & MMC_DATA_READ) ? |
| 740 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 741 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /* |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 745 | * The specification states that the block count register must |
| 746 | * be updated, but it does not specify at what point in the |
| 747 | * data flow. That makes the register entirely useless to read |
| 748 | * back so we have to assume that nothing made it to the card |
| 749 | * in the event of an error. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 750 | */ |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 751 | if (data->error) |
| 752 | data->bytes_xfered = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 753 | else |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 754 | data->bytes_xfered = data->blksz * data->blocks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 755 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 756 | if (data->stop) { |
| 757 | /* |
| 758 | * The controller needs a reset of internal state machines |
| 759 | * upon error conditions. |
| 760 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 761 | if (data->error) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 762 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 763 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 764 | } |
| 765 | |
| 766 | sdhci_send_command(host, data->stop); |
| 767 | } else |
| 768 | tasklet_schedule(&host->finish_tasklet); |
| 769 | } |
| 770 | |
| 771 | static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd) |
| 772 | { |
| 773 | int flags; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 774 | u32 mask; |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 775 | unsigned long timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 776 | |
| 777 | WARN_ON(host->cmd); |
| 778 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 779 | /* Wait max 10 ms */ |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 780 | timeout = 10; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 781 | |
| 782 | mask = SDHCI_CMD_INHIBIT; |
| 783 | if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY)) |
| 784 | mask |= SDHCI_DATA_INHIBIT; |
| 785 | |
| 786 | /* We shouldn't wait for data inihibit for stop commands, even |
| 787 | though they might use busy signaling */ |
| 788 | if (host->mrq->data && (cmd == host->mrq->data->stop)) |
| 789 | mask &= ~SDHCI_DATA_INHIBIT; |
| 790 | |
| 791 | while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) { |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 792 | if (timeout == 0) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 793 | printk(KERN_ERR "%s: Controller never released " |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 794 | "inhibit bit(s).\n", mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 795 | sdhci_dumpregs(host); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 796 | cmd->error = -EIO; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 797 | tasklet_schedule(&host->finish_tasklet); |
| 798 | return; |
| 799 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 800 | timeout--; |
| 801 | mdelay(1); |
| 802 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 803 | |
| 804 | mod_timer(&host->timer, jiffies + 10 * HZ); |
| 805 | |
| 806 | host->cmd = cmd; |
| 807 | |
| 808 | sdhci_prepare_data(host, cmd->data); |
| 809 | |
| 810 | writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT); |
| 811 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 812 | sdhci_set_transfer_mode(host, cmd->data); |
| 813 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 814 | if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 815 | printk(KERN_ERR "%s: Unsupported response type!\n", |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 816 | mmc_hostname(host->mmc)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 817 | cmd->error = -EINVAL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 818 | tasklet_schedule(&host->finish_tasklet); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | if (!(cmd->flags & MMC_RSP_PRESENT)) |
| 823 | flags = SDHCI_CMD_RESP_NONE; |
| 824 | else if (cmd->flags & MMC_RSP_136) |
| 825 | flags = SDHCI_CMD_RESP_LONG; |
| 826 | else if (cmd->flags & MMC_RSP_BUSY) |
| 827 | flags = SDHCI_CMD_RESP_SHORT_BUSY; |
| 828 | else |
| 829 | flags = SDHCI_CMD_RESP_SHORT; |
| 830 | |
| 831 | if (cmd->flags & MMC_RSP_CRC) |
| 832 | flags |= SDHCI_CMD_CRC; |
| 833 | if (cmd->flags & MMC_RSP_OPCODE) |
| 834 | flags |= SDHCI_CMD_INDEX; |
| 835 | if (cmd->data) |
| 836 | flags |= SDHCI_CMD_DATA; |
| 837 | |
Pierre Ossman | fb61e28 | 2006-07-11 21:06:48 +0200 | [diff] [blame] | 838 | writew(SDHCI_MAKE_CMD(cmd->opcode, flags), |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 839 | host->ioaddr + SDHCI_COMMAND); |
| 840 | } |
| 841 | |
| 842 | static void sdhci_finish_command(struct sdhci_host *host) |
| 843 | { |
| 844 | int i; |
| 845 | |
| 846 | BUG_ON(host->cmd == NULL); |
| 847 | |
| 848 | if (host->cmd->flags & MMC_RSP_PRESENT) { |
| 849 | if (host->cmd->flags & MMC_RSP_136) { |
| 850 | /* CRC is stripped so we need to do some shifting. */ |
| 851 | for (i = 0;i < 4;i++) { |
| 852 | host->cmd->resp[i] = readl(host->ioaddr + |
| 853 | SDHCI_RESPONSE + (3-i)*4) << 8; |
| 854 | if (i != 3) |
| 855 | host->cmd->resp[i] |= |
| 856 | readb(host->ioaddr + |
| 857 | SDHCI_RESPONSE + (3-i)*4-1); |
| 858 | } |
| 859 | } else { |
| 860 | host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE); |
| 861 | } |
| 862 | } |
| 863 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 864 | host->cmd->error = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 865 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 866 | if (host->data && host->data_early) |
| 867 | sdhci_finish_data(host); |
| 868 | |
| 869 | if (!host->cmd->data) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 870 | tasklet_schedule(&host->finish_tasklet); |
| 871 | |
| 872 | host->cmd = NULL; |
| 873 | } |
| 874 | |
| 875 | static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock) |
| 876 | { |
| 877 | int div; |
| 878 | u16 clk; |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 879 | unsigned long timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 880 | |
| 881 | if (clock == host->clock) |
| 882 | return; |
| 883 | |
| 884 | writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 885 | |
| 886 | if (clock == 0) |
| 887 | goto out; |
| 888 | |
| 889 | for (div = 1;div < 256;div *= 2) { |
| 890 | if ((host->max_clk / div) <= clock) |
| 891 | break; |
| 892 | } |
| 893 | div >>= 1; |
| 894 | |
| 895 | clk = div << SDHCI_DIVIDER_SHIFT; |
| 896 | clk |= SDHCI_CLOCK_INT_EN; |
| 897 | writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 898 | |
| 899 | /* Wait max 10 ms */ |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 900 | timeout = 10; |
| 901 | while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL)) |
| 902 | & SDHCI_CLOCK_INT_STABLE)) { |
| 903 | if (timeout == 0) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 904 | printk(KERN_ERR "%s: Internal clock never " |
| 905 | "stabilised.\n", mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 906 | sdhci_dumpregs(host); |
| 907 | return; |
| 908 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 909 | timeout--; |
| 910 | mdelay(1); |
| 911 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 912 | |
| 913 | clk |= SDHCI_CLOCK_CARD_EN; |
| 914 | writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 915 | |
| 916 | out: |
| 917 | host->clock = clock; |
| 918 | } |
| 919 | |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 920 | static void sdhci_set_power(struct sdhci_host *host, unsigned short power) |
| 921 | { |
| 922 | u8 pwr; |
| 923 | |
| 924 | if (host->power == power) |
| 925 | return; |
| 926 | |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 927 | if (power == (unsigned short)-1) { |
| 928 | writeb(0, host->ioaddr + SDHCI_POWER_CONTROL); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 929 | goto out; |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Spec says that we should clear the power reg before setting |
| 934 | * a new value. Some controllers don't seem to like this though. |
| 935 | */ |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 936 | if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 937 | writeb(0, host->ioaddr + SDHCI_POWER_CONTROL); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 938 | |
| 939 | pwr = SDHCI_POWER_ON; |
| 940 | |
Philip Langdale | 4be34c9 | 2007-03-11 17:15:15 -0700 | [diff] [blame] | 941 | switch (1 << power) { |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 942 | case MMC_VDD_165_195: |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 943 | pwr |= SDHCI_POWER_180; |
| 944 | break; |
Philip Langdale | 4be34c9 | 2007-03-11 17:15:15 -0700 | [diff] [blame] | 945 | case MMC_VDD_29_30: |
| 946 | case MMC_VDD_30_31: |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 947 | pwr |= SDHCI_POWER_300; |
| 948 | break; |
Philip Langdale | 4be34c9 | 2007-03-11 17:15:15 -0700 | [diff] [blame] | 949 | case MMC_VDD_32_33: |
| 950 | case MMC_VDD_33_34: |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 951 | pwr |= SDHCI_POWER_330; |
| 952 | break; |
| 953 | default: |
| 954 | BUG(); |
| 955 | } |
| 956 | |
Andres Salomon | e08c169 | 2008-07-04 10:00:03 -0700 | [diff] [blame] | 957 | /* |
Andres Salomon | c71f651 | 2008-07-07 17:25:56 -0400 | [diff] [blame] | 958 | * At least the Marvell CaFe chip gets confused if we set the voltage |
Andres Salomon | e08c169 | 2008-07-04 10:00:03 -0700 | [diff] [blame] | 959 | * and set turn on power at the same time, so set the voltage first. |
| 960 | */ |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 961 | if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)) |
Andres Salomon | e08c169 | 2008-07-04 10:00:03 -0700 | [diff] [blame] | 962 | writeb(pwr & ~SDHCI_POWER_ON, |
| 963 | host->ioaddr + SDHCI_POWER_CONTROL); |
| 964 | |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 965 | writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL); |
| 966 | |
| 967 | out: |
| 968 | host->power = power; |
| 969 | } |
| 970 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 971 | /*****************************************************************************\ |
| 972 | * * |
| 973 | * MMC callbacks * |
| 974 | * * |
| 975 | \*****************************************************************************/ |
| 976 | |
| 977 | static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 978 | { |
| 979 | struct sdhci_host *host; |
| 980 | unsigned long flags; |
| 981 | |
| 982 | host = mmc_priv(mmc); |
| 983 | |
| 984 | spin_lock_irqsave(&host->lock, flags); |
| 985 | |
| 986 | WARN_ON(host->mrq != NULL); |
| 987 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 988 | #ifndef CONFIG_LEDS_CLASS |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 989 | sdhci_activate_led(host); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 990 | #endif |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 991 | |
| 992 | host->mrq = mrq; |
| 993 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 994 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT) |
| 995 | || (host->flags & SDHCI_DEVICE_DEAD)) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 996 | host->mrq->cmd->error = -ENOMEDIUM; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 997 | tasklet_schedule(&host->finish_tasklet); |
| 998 | } else |
| 999 | sdhci_send_command(host, mrq->cmd); |
| 1000 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1001 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1002 | spin_unlock_irqrestore(&host->lock, flags); |
| 1003 | } |
| 1004 | |
| 1005 | static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1006 | { |
| 1007 | struct sdhci_host *host; |
| 1008 | unsigned long flags; |
| 1009 | u8 ctrl; |
| 1010 | |
| 1011 | host = mmc_priv(mmc); |
| 1012 | |
| 1013 | spin_lock_irqsave(&host->lock, flags); |
| 1014 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1015 | if (host->flags & SDHCI_DEVICE_DEAD) |
| 1016 | goto out; |
| 1017 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1018 | /* |
| 1019 | * Reset the chip on each power off. |
| 1020 | * Should clear out any weird states. |
| 1021 | */ |
| 1022 | if (ios->power_mode == MMC_POWER_OFF) { |
| 1023 | writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1024 | sdhci_init(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | sdhci_set_clock(host, ios->clock); |
| 1028 | |
| 1029 | if (ios->power_mode == MMC_POWER_OFF) |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1030 | sdhci_set_power(host, -1); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1031 | else |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1032 | sdhci_set_power(host, ios->vdd); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1033 | |
| 1034 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1035 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1036 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 1037 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 1038 | else |
| 1039 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1040 | |
| 1041 | if (ios->timing == MMC_TIMING_SD_HS) |
| 1042 | ctrl |= SDHCI_CTRL_HISPD; |
| 1043 | else |
| 1044 | ctrl &= ~SDHCI_CTRL_HISPD; |
| 1045 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1046 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 1047 | |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 1048 | /* |
| 1049 | * Some (ENE) controllers go apeshit on some ios operation, |
| 1050 | * signalling timeout and CRC errors even on CMD0. Resetting |
| 1051 | * it on each ios seems to solve the problem. |
| 1052 | */ |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1053 | if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS) |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 1054 | sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
| 1055 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1056 | out: |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1057 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1058 | spin_unlock_irqrestore(&host->lock, flags); |
| 1059 | } |
| 1060 | |
| 1061 | static int sdhci_get_ro(struct mmc_host *mmc) |
| 1062 | { |
| 1063 | struct sdhci_host *host; |
| 1064 | unsigned long flags; |
| 1065 | int present; |
| 1066 | |
| 1067 | host = mmc_priv(mmc); |
| 1068 | |
| 1069 | spin_lock_irqsave(&host->lock, flags); |
| 1070 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1071 | if (host->flags & SDHCI_DEVICE_DEAD) |
| 1072 | present = 0; |
| 1073 | else |
| 1074 | present = readl(host->ioaddr + SDHCI_PRESENT_STATE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1075 | |
| 1076 | spin_unlock_irqrestore(&host->lock, flags); |
| 1077 | |
| 1078 | return !(present & SDHCI_WRITE_PROTECT); |
| 1079 | } |
| 1080 | |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1081 | static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 1082 | { |
| 1083 | struct sdhci_host *host; |
| 1084 | unsigned long flags; |
| 1085 | u32 ier; |
| 1086 | |
| 1087 | host = mmc_priv(mmc); |
| 1088 | |
| 1089 | spin_lock_irqsave(&host->lock, flags); |
| 1090 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1091 | if (host->flags & SDHCI_DEVICE_DEAD) |
| 1092 | goto out; |
| 1093 | |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1094 | ier = readl(host->ioaddr + SDHCI_INT_ENABLE); |
| 1095 | |
| 1096 | ier &= ~SDHCI_INT_CARD_INT; |
| 1097 | if (enable) |
| 1098 | ier |= SDHCI_INT_CARD_INT; |
| 1099 | |
| 1100 | writel(ier, host->ioaddr + SDHCI_INT_ENABLE); |
| 1101 | writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 1102 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1103 | out: |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1104 | mmiowb(); |
| 1105 | |
| 1106 | spin_unlock_irqrestore(&host->lock, flags); |
| 1107 | } |
| 1108 | |
David Brownell | ab7aefd | 2006-11-12 17:55:30 -0800 | [diff] [blame] | 1109 | static const struct mmc_host_ops sdhci_ops = { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1110 | .request = sdhci_request, |
| 1111 | .set_ios = sdhci_set_ios, |
| 1112 | .get_ro = sdhci_get_ro, |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1113 | .enable_sdio_irq = sdhci_enable_sdio_irq, |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1114 | }; |
| 1115 | |
| 1116 | /*****************************************************************************\ |
| 1117 | * * |
| 1118 | * Tasklets * |
| 1119 | * * |
| 1120 | \*****************************************************************************/ |
| 1121 | |
| 1122 | static void sdhci_tasklet_card(unsigned long param) |
| 1123 | { |
| 1124 | struct sdhci_host *host; |
| 1125 | unsigned long flags; |
| 1126 | |
| 1127 | host = (struct sdhci_host*)param; |
| 1128 | |
| 1129 | spin_lock_irqsave(&host->lock, flags); |
| 1130 | |
| 1131 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { |
| 1132 | if (host->mrq) { |
| 1133 | printk(KERN_ERR "%s: Card removed during transfer!\n", |
| 1134 | mmc_hostname(host->mmc)); |
| 1135 | printk(KERN_ERR "%s: Resetting controller.\n", |
| 1136 | mmc_hostname(host->mmc)); |
| 1137 | |
| 1138 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 1139 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 1140 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1141 | host->mrq->cmd->error = -ENOMEDIUM; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1142 | tasklet_schedule(&host->finish_tasklet); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | spin_unlock_irqrestore(&host->lock, flags); |
| 1147 | |
| 1148 | mmc_detect_change(host->mmc, msecs_to_jiffies(500)); |
| 1149 | } |
| 1150 | |
| 1151 | static void sdhci_tasklet_finish(unsigned long param) |
| 1152 | { |
| 1153 | struct sdhci_host *host; |
| 1154 | unsigned long flags; |
| 1155 | struct mmc_request *mrq; |
| 1156 | |
| 1157 | host = (struct sdhci_host*)param; |
| 1158 | |
| 1159 | spin_lock_irqsave(&host->lock, flags); |
| 1160 | |
| 1161 | del_timer(&host->timer); |
| 1162 | |
| 1163 | mrq = host->mrq; |
| 1164 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1165 | /* |
| 1166 | * The controller needs a reset of internal state machines |
| 1167 | * upon error conditions. |
| 1168 | */ |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1169 | if (!(host->flags & SDHCI_DEVICE_DEAD) && |
| 1170 | (mrq->cmd->error || |
| 1171 | (mrq->data && (mrq->data->error || |
| 1172 | (mrq->data->stop && mrq->data->stop->error))) || |
| 1173 | (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 1174 | |
| 1175 | /* Some controllers need this kick or reset won't work here */ |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1176 | if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) { |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 1177 | unsigned int clock; |
| 1178 | |
| 1179 | /* This is to force an update */ |
| 1180 | clock = host->clock; |
| 1181 | host->clock = 0; |
| 1182 | sdhci_set_clock(host, clock); |
| 1183 | } |
| 1184 | |
| 1185 | /* Spec says we should do both at the same time, but Ricoh |
| 1186 | controllers do not like that. */ |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1187 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 1188 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 1189 | } |
| 1190 | |
| 1191 | host->mrq = NULL; |
| 1192 | host->cmd = NULL; |
| 1193 | host->data = NULL; |
| 1194 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 1195 | #ifndef CONFIG_LEDS_CLASS |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1196 | sdhci_deactivate_led(host); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 1197 | #endif |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1198 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1199 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1200 | spin_unlock_irqrestore(&host->lock, flags); |
| 1201 | |
| 1202 | mmc_request_done(host->mmc, mrq); |
| 1203 | } |
| 1204 | |
| 1205 | static void sdhci_timeout_timer(unsigned long data) |
| 1206 | { |
| 1207 | struct sdhci_host *host; |
| 1208 | unsigned long flags; |
| 1209 | |
| 1210 | host = (struct sdhci_host*)data; |
| 1211 | |
| 1212 | spin_lock_irqsave(&host->lock, flags); |
| 1213 | |
| 1214 | if (host->mrq) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 1215 | printk(KERN_ERR "%s: Timeout waiting for hardware " |
| 1216 | "interrupt.\n", mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1217 | sdhci_dumpregs(host); |
| 1218 | |
| 1219 | if (host->data) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1220 | host->data->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1221 | sdhci_finish_data(host); |
| 1222 | } else { |
| 1223 | if (host->cmd) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1224 | host->cmd->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1225 | else |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1226 | host->mrq->cmd->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1227 | |
| 1228 | tasklet_schedule(&host->finish_tasklet); |
| 1229 | } |
| 1230 | } |
| 1231 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1232 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1233 | spin_unlock_irqrestore(&host->lock, flags); |
| 1234 | } |
| 1235 | |
| 1236 | /*****************************************************************************\ |
| 1237 | * * |
| 1238 | * Interrupt handling * |
| 1239 | * * |
| 1240 | \*****************************************************************************/ |
| 1241 | |
| 1242 | static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask) |
| 1243 | { |
| 1244 | BUG_ON(intmask == 0); |
| 1245 | |
| 1246 | if (!host->cmd) { |
Pierre Ossman | b67ac3f | 2007-08-12 17:29:47 +0200 | [diff] [blame] | 1247 | printk(KERN_ERR "%s: Got command interrupt 0x%08x even " |
| 1248 | "though no command operation was in progress.\n", |
| 1249 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1250 | sdhci_dumpregs(host); |
| 1251 | return; |
| 1252 | } |
| 1253 | |
Pierre Ossman | 43b58b3 | 2007-07-25 23:15:27 +0200 | [diff] [blame] | 1254 | if (intmask & SDHCI_INT_TIMEOUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1255 | host->cmd->error = -ETIMEDOUT; |
| 1256 | else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT | |
| 1257 | SDHCI_INT_INDEX)) |
| 1258 | host->cmd->error = -EILSEQ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1259 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1260 | if (host->cmd->error) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1261 | tasklet_schedule(&host->finish_tasklet); |
Pierre Ossman | 43b58b3 | 2007-07-25 23:15:27 +0200 | [diff] [blame] | 1262 | else if (intmask & SDHCI_INT_RESPONSE) |
| 1263 | sdhci_finish_command(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static void sdhci_data_irq(struct sdhci_host *host, u32 intmask) |
| 1267 | { |
| 1268 | BUG_ON(intmask == 0); |
| 1269 | |
| 1270 | if (!host->data) { |
| 1271 | /* |
| 1272 | * A data end interrupt is sent together with the response |
| 1273 | * for the stop command. |
| 1274 | */ |
| 1275 | if (intmask & SDHCI_INT_DATA_END) |
| 1276 | return; |
| 1277 | |
Pierre Ossman | b67ac3f | 2007-08-12 17:29:47 +0200 | [diff] [blame] | 1278 | printk(KERN_ERR "%s: Got data interrupt 0x%08x even " |
| 1279 | "though no data operation was in progress.\n", |
| 1280 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1281 | sdhci_dumpregs(host); |
| 1282 | |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | if (intmask & SDHCI_INT_DATA_TIMEOUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1287 | host->data->error = -ETIMEDOUT; |
| 1288 | else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) |
| 1289 | host->data->error = -EILSEQ; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1290 | else if (intmask & SDHCI_INT_ADMA_ERROR) |
| 1291 | host->data->error = -EIO; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1292 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1293 | if (host->data->error) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1294 | sdhci_finish_data(host); |
| 1295 | else { |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 1296 | if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1297 | sdhci_transfer_pio(host); |
| 1298 | |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 1299 | /* |
| 1300 | * We currently don't do anything fancy with DMA |
| 1301 | * boundaries, but as we can't disable the feature |
| 1302 | * we need to at least restart the transfer. |
| 1303 | */ |
| 1304 | if (intmask & SDHCI_INT_DMA_END) |
| 1305 | writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS), |
| 1306 | host->ioaddr + SDHCI_DMA_ADDRESS); |
| 1307 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 1308 | if (intmask & SDHCI_INT_DATA_END) { |
| 1309 | if (host->cmd) { |
| 1310 | /* |
| 1311 | * Data managed to finish before the |
| 1312 | * command completed. Make sure we do |
| 1313 | * things in the proper order. |
| 1314 | */ |
| 1315 | host->data_early = 1; |
| 1316 | } else { |
| 1317 | sdhci_finish_data(host); |
| 1318 | } |
| 1319 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1323 | static irqreturn_t sdhci_irq(int irq, void *dev_id) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1324 | { |
| 1325 | irqreturn_t result; |
| 1326 | struct sdhci_host* host = dev_id; |
| 1327 | u32 intmask; |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1328 | int cardint = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1329 | |
| 1330 | spin_lock(&host->lock); |
| 1331 | |
| 1332 | intmask = readl(host->ioaddr + SDHCI_INT_STATUS); |
| 1333 | |
Mark Lord | 62df67a5 | 2007-03-06 13:30:13 +0100 | [diff] [blame] | 1334 | if (!intmask || intmask == 0xffffffff) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1335 | result = IRQ_NONE; |
| 1336 | goto out; |
| 1337 | } |
| 1338 | |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 1339 | DBG("*** %s got interrupt: 0x%08x\n", |
| 1340 | mmc_hostname(host->mmc), intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1341 | |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1342 | if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { |
| 1343 | writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE), |
| 1344 | host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1345 | tasklet_schedule(&host->card_tasklet); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1349 | |
| 1350 | if (intmask & SDHCI_INT_CMD_MASK) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1351 | writel(intmask & SDHCI_INT_CMD_MASK, |
| 1352 | host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1353 | sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | if (intmask & SDHCI_INT_DATA_MASK) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1357 | writel(intmask & SDHCI_INT_DATA_MASK, |
| 1358 | host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1359 | sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); |
| 1363 | |
Pierre Ossman | 964f9ce | 2007-07-20 18:20:36 +0200 | [diff] [blame] | 1364 | intmask &= ~SDHCI_INT_ERROR; |
| 1365 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1366 | if (intmask & SDHCI_INT_BUS_POWER) { |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1367 | printk(KERN_ERR "%s: Card is consuming too much power!\n", |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1368 | mmc_hostname(host->mmc)); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1369 | writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Rolf Eike Beer | 9d26a5d | 2007-06-26 13:31:16 +0200 | [diff] [blame] | 1372 | intmask &= ~SDHCI_INT_BUS_POWER; |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1373 | |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1374 | if (intmask & SDHCI_INT_CARD_INT) |
| 1375 | cardint = 1; |
| 1376 | |
| 1377 | intmask &= ~SDHCI_INT_CARD_INT; |
| 1378 | |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1379 | if (intmask) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 1380 | printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n", |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1381 | mmc_hostname(host->mmc), intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1382 | sdhci_dumpregs(host); |
| 1383 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1384 | writel(intmask, host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1385 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1386 | |
| 1387 | result = IRQ_HANDLED; |
| 1388 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1389 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1390 | out: |
| 1391 | spin_unlock(&host->lock); |
| 1392 | |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1393 | /* |
| 1394 | * We have to delay this as it calls back into the driver. |
| 1395 | */ |
| 1396 | if (cardint) |
| 1397 | mmc_signal_sdio_irq(host->mmc); |
| 1398 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1399 | return result; |
| 1400 | } |
| 1401 | |
| 1402 | /*****************************************************************************\ |
| 1403 | * * |
| 1404 | * Suspend/resume * |
| 1405 | * * |
| 1406 | \*****************************************************************************/ |
| 1407 | |
| 1408 | #ifdef CONFIG_PM |
| 1409 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1410 | int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1411 | { |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1412 | int ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1413 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1414 | ret = mmc_suspend_host(host->mmc, state); |
Pierre Ossman | df1c4b7 | 2007-01-30 07:55:15 +0100 | [diff] [blame] | 1415 | if (ret) |
| 1416 | return ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1417 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1418 | free_irq(host->irq, host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1423 | EXPORT_SYMBOL_GPL(sdhci_suspend_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1424 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1425 | int sdhci_resume_host(struct sdhci_host *host) |
| 1426 | { |
| 1427 | int ret; |
| 1428 | |
| 1429 | if (host->flags & SDHCI_USE_DMA) { |
| 1430 | if (host->ops->enable_dma) |
| 1431 | host->ops->enable_dma(host); |
| 1432 | } |
| 1433 | |
| 1434 | ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED, |
| 1435 | mmc_hostname(host->mmc), host); |
| 1436 | if (ret) |
| 1437 | return ret; |
| 1438 | |
| 1439 | sdhci_init(host); |
| 1440 | mmiowb(); |
| 1441 | |
| 1442 | ret = mmc_resume_host(host->mmc); |
| 1443 | if (ret) |
| 1444 | return ret; |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | EXPORT_SYMBOL_GPL(sdhci_resume_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1450 | |
| 1451 | #endif /* CONFIG_PM */ |
| 1452 | |
| 1453 | /*****************************************************************************\ |
| 1454 | * * |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1455 | * Device allocation/registration * |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1456 | * * |
| 1457 | \*****************************************************************************/ |
| 1458 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1459 | struct sdhci_host *sdhci_alloc_host(struct device *dev, |
| 1460 | size_t priv_size) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1461 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1462 | struct mmc_host *mmc; |
| 1463 | struct sdhci_host *host; |
| 1464 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1465 | WARN_ON(dev == NULL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1466 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1467 | mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1468 | if (!mmc) |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1469 | return ERR_PTR(-ENOMEM); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1470 | |
| 1471 | host = mmc_priv(mmc); |
| 1472 | host->mmc = mmc; |
| 1473 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1474 | return host; |
| 1475 | } |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 1476 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1477 | EXPORT_SYMBOL_GPL(sdhci_alloc_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1478 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1479 | int sdhci_add_host(struct sdhci_host *host) |
| 1480 | { |
| 1481 | struct mmc_host *mmc; |
| 1482 | unsigned int caps; |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1483 | int ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1484 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1485 | WARN_ON(host == NULL); |
| 1486 | if (host == NULL) |
| 1487 | return -EINVAL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1488 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1489 | mmc = host->mmc; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1490 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1491 | if (debug_quirks) |
| 1492 | host->quirks = debug_quirks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1493 | |
Pierre Ossman | d96649e | 2006-06-30 02:22:30 -0700 | [diff] [blame] | 1494 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 1495 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1496 | host->version = readw(host->ioaddr + SDHCI_HOST_VERSION); |
| 1497 | host->version = (host->version & SDHCI_SPEC_VER_MASK) |
| 1498 | >> SDHCI_SPEC_VER_SHIFT; |
| 1499 | if (host->version > SDHCI_SPEC_200) { |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 1500 | printk(KERN_ERR "%s: Unknown controller version (%d). " |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 1501 | "You may experience problems.\n", mmc_hostname(mmc), |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1502 | host->version); |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1505 | caps = readl(host->ioaddr + SDHCI_CAPABILITIES); |
| 1506 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1507 | if (host->quirks & SDHCI_QUIRK_FORCE_DMA) |
Pierre Ossman | 9860807 | 2006-06-30 02:22:34 -0700 | [diff] [blame] | 1508 | host->flags |= SDHCI_USE_DMA; |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1509 | else if (!(caps & SDHCI_CAN_DO_DMA)) |
| 1510 | DBG("Controller doesn't have DMA capability\n"); |
| 1511 | else |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1512 | host->flags |= SDHCI_USE_DMA; |
| 1513 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1514 | if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) && |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 1515 | (host->flags & SDHCI_USE_DMA)) { |
Rolf Eike Beer | cee687c | 2007-11-02 15:22:30 +0100 | [diff] [blame] | 1516 | DBG("Disabling DMA as it is marked broken\n"); |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 1517 | host->flags &= ~SDHCI_USE_DMA; |
| 1518 | } |
| 1519 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1520 | if (host->flags & SDHCI_USE_DMA) { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1521 | if ((host->version >= SDHCI_SPEC_200) && |
| 1522 | (caps & SDHCI_CAN_DO_ADMA2)) |
| 1523 | host->flags |= SDHCI_USE_ADMA; |
| 1524 | } |
| 1525 | |
| 1526 | if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) && |
| 1527 | (host->flags & SDHCI_USE_ADMA)) { |
| 1528 | DBG("Disabling ADMA as it is marked broken\n"); |
| 1529 | host->flags &= ~SDHCI_USE_ADMA; |
| 1530 | } |
| 1531 | |
| 1532 | if (host->flags & SDHCI_USE_DMA) { |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1533 | if (host->ops->enable_dma) { |
| 1534 | if (host->ops->enable_dma(host)) { |
| 1535 | printk(KERN_WARNING "%s: No suitable DMA " |
| 1536 | "available. Falling back to PIO.\n", |
| 1537 | mmc_hostname(mmc)); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1538 | host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA); |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1539 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1543 | if (host->flags & SDHCI_USE_ADMA) { |
| 1544 | /* |
| 1545 | * We need to allocate descriptors for all sg entries |
| 1546 | * (128) and potentially one alignment transfer for |
| 1547 | * each of those entries. |
| 1548 | */ |
| 1549 | host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL); |
| 1550 | host->align_buffer = kmalloc(128 * 4, GFP_KERNEL); |
| 1551 | if (!host->adma_desc || !host->align_buffer) { |
| 1552 | kfree(host->adma_desc); |
| 1553 | kfree(host->align_buffer); |
| 1554 | printk(KERN_WARNING "%s: Unable to allocate ADMA " |
| 1555 | "buffers. Falling back to standard DMA.\n", |
| 1556 | mmc_hostname(mmc)); |
| 1557 | host->flags &= ~SDHCI_USE_ADMA; |
| 1558 | } |
| 1559 | } |
| 1560 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 1561 | /* |
| 1562 | * If we use DMA, then it's up to the caller to set the DMA |
| 1563 | * mask, but PIO does not need the hw shim so we set a new |
| 1564 | * mask here in that case. |
| 1565 | */ |
| 1566 | if (!(host->flags & SDHCI_USE_DMA)) { |
| 1567 | host->dma_mask = DMA_BIT_MASK(64); |
| 1568 | mmc_dev(host->mmc)->dma_mask = &host->dma_mask; |
| 1569 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1570 | |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1571 | host->max_clk = |
| 1572 | (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; |
| 1573 | if (host->max_clk == 0) { |
| 1574 | printk(KERN_ERR "%s: Hardware doesn't specify base clock " |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 1575 | "frequency.\n", mmc_hostname(mmc)); |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1576 | return -ENODEV; |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1577 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1578 | host->max_clk *= 1000000; |
| 1579 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1580 | host->timeout_clk = |
| 1581 | (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT; |
| 1582 | if (host->timeout_clk == 0) { |
| 1583 | printk(KERN_ERR "%s: Hardware doesn't specify timeout clock " |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 1584 | "frequency.\n", mmc_hostname(mmc)); |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1585 | return -ENODEV; |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1586 | } |
| 1587 | if (caps & SDHCI_TIMEOUT_CLK_UNIT) |
| 1588 | host->timeout_clk *= 1000; |
| 1589 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1590 | /* |
| 1591 | * Set host parameters. |
| 1592 | */ |
| 1593 | mmc->ops = &sdhci_ops; |
| 1594 | mmc->f_min = host->max_clk / 256; |
| 1595 | mmc->f_max = host->max_clk; |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 1596 | mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1597 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1598 | if (caps & SDHCI_CAN_DO_HISPD) |
| 1599 | mmc->caps |= MMC_CAP_SD_HIGHSPEED; |
| 1600 | |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1601 | mmc->ocr_avail = 0; |
| 1602 | if (caps & SDHCI_CAN_VDD_330) |
| 1603 | mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34; |
Pierre Ossman | c70840e | 2007-02-02 22:41:41 +0100 | [diff] [blame] | 1604 | if (caps & SDHCI_CAN_VDD_300) |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1605 | mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31; |
Pierre Ossman | c70840e | 2007-02-02 22:41:41 +0100 | [diff] [blame] | 1606 | if (caps & SDHCI_CAN_VDD_180) |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 1607 | mmc->ocr_avail |= MMC_VDD_165_195; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1608 | |
| 1609 | if (mmc->ocr_avail == 0) { |
| 1610 | printk(KERN_ERR "%s: Hardware doesn't report any " |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 1611 | "support voltages.\n", mmc_hostname(mmc)); |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1612 | return -ENODEV; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1615 | spin_lock_init(&host->lock); |
| 1616 | |
| 1617 | /* |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1618 | * Maximum number of segments. Depends on if the hardware |
| 1619 | * can do scatter/gather or not. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1620 | */ |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1621 | if (host->flags & SDHCI_USE_ADMA) |
| 1622 | mmc->max_hw_segs = 128; |
| 1623 | else if (host->flags & SDHCI_USE_DMA) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1624 | mmc->max_hw_segs = 1; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1625 | else /* PIO */ |
| 1626 | mmc->max_hw_segs = 128; |
| 1627 | mmc->max_phys_segs = 128; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1628 | |
| 1629 | /* |
Pierre Ossman | bab7696 | 2006-07-02 16:51:35 +0100 | [diff] [blame] | 1630 | * Maximum number of sectors in one transfer. Limited by DMA boundary |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1631 | * size (512KiB). |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1632 | */ |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1633 | mmc->max_req_size = 524288; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1634 | |
| 1635 | /* |
| 1636 | * Maximum segment size. Could be one segment with the maximum number |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1637 | * of bytes. When doing hardware scatter/gather, each entry cannot |
| 1638 | * be larger than 64 KiB though. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1639 | */ |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1640 | if (host->flags & SDHCI_USE_ADMA) |
| 1641 | mmc->max_seg_size = 65536; |
| 1642 | else |
| 1643 | mmc->max_seg_size = mmc->max_req_size; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1644 | |
| 1645 | /* |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 1646 | * Maximum block size. This varies from controller to controller and |
| 1647 | * is specified in the capabilities register. |
| 1648 | */ |
| 1649 | mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT; |
| 1650 | if (mmc->max_blk_size >= 3) { |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 1651 | printk(KERN_WARNING "%s: Invalid maximum block size, " |
| 1652 | "assuming 512 bytes\n", mmc_hostname(mmc)); |
David Vrabel | 03f8590 | 2007-08-10 13:25:03 +0100 | [diff] [blame] | 1653 | mmc->max_blk_size = 512; |
| 1654 | } else |
| 1655 | mmc->max_blk_size = 512 << mmc->max_blk_size; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 1656 | |
| 1657 | /* |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1658 | * Maximum block count. |
| 1659 | */ |
| 1660 | mmc->max_blk_count = 65535; |
| 1661 | |
| 1662 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1663 | * Init tasklets. |
| 1664 | */ |
| 1665 | tasklet_init(&host->card_tasklet, |
| 1666 | sdhci_tasklet_card, (unsigned long)host); |
| 1667 | tasklet_init(&host->finish_tasklet, |
| 1668 | sdhci_tasklet_finish, (unsigned long)host); |
| 1669 | |
Al Viro | e4cad1b | 2006-10-10 22:47:07 +0100 | [diff] [blame] | 1670 | setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1671 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 1672 | ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED, |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 1673 | mmc_hostname(mmc), host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1674 | if (ret) |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1675 | goto untasklet; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1676 | |
| 1677 | sdhci_init(host); |
| 1678 | |
| 1679 | #ifdef CONFIG_MMC_DEBUG |
| 1680 | sdhci_dumpregs(host); |
| 1681 | #endif |
| 1682 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 1683 | #ifdef CONFIG_LEDS_CLASS |
| 1684 | host->led.name = mmc_hostname(mmc); |
| 1685 | host->led.brightness = LED_OFF; |
| 1686 | host->led.default_trigger = mmc_hostname(mmc); |
| 1687 | host->led.brightness_set = sdhci_led_control; |
| 1688 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1689 | ret = led_classdev_register(mmc_dev(mmc), &host->led); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 1690 | if (ret) |
| 1691 | goto reset; |
| 1692 | #endif |
| 1693 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1694 | mmiowb(); |
| 1695 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1696 | mmc_add_host(mmc); |
| 1697 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1698 | printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n", |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1699 | mmc_hostname(mmc), host->hw_name, mmc_dev(mmc)->bus_id, |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1700 | (host->flags & SDHCI_USE_ADMA)?"A":"", |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1701 | (host->flags & SDHCI_USE_DMA)?"DMA":"PIO"); |
| 1702 | |
| 1703 | return 0; |
| 1704 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 1705 | #ifdef CONFIG_LEDS_CLASS |
| 1706 | reset: |
| 1707 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 1708 | free_irq(host->irq, host); |
| 1709 | #endif |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1710 | untasklet: |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1711 | tasklet_kill(&host->card_tasklet); |
| 1712 | tasklet_kill(&host->finish_tasklet); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1713 | |
| 1714 | return ret; |
| 1715 | } |
| 1716 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1717 | EXPORT_SYMBOL_GPL(sdhci_add_host); |
| 1718 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1719 | void sdhci_remove_host(struct sdhci_host *host, int dead) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1720 | { |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1721 | unsigned long flags; |
| 1722 | |
| 1723 | if (dead) { |
| 1724 | spin_lock_irqsave(&host->lock, flags); |
| 1725 | |
| 1726 | host->flags |= SDHCI_DEVICE_DEAD; |
| 1727 | |
| 1728 | if (host->mrq) { |
| 1729 | printk(KERN_ERR "%s: Controller removed during " |
| 1730 | " transfer!\n", mmc_hostname(host->mmc)); |
| 1731 | |
| 1732 | host->mrq->cmd->error = -ENOMEDIUM; |
| 1733 | tasklet_schedule(&host->finish_tasklet); |
| 1734 | } |
| 1735 | |
| 1736 | spin_unlock_irqrestore(&host->lock, flags); |
| 1737 | } |
| 1738 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1739 | mmc_remove_host(host->mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1740 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 1741 | #ifdef CONFIG_LEDS_CLASS |
| 1742 | led_classdev_unregister(&host->led); |
| 1743 | #endif |
| 1744 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1745 | if (!dead) |
| 1746 | sdhci_reset(host, SDHCI_RESET_ALL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1747 | |
| 1748 | free_irq(host->irq, host); |
| 1749 | |
| 1750 | del_timer_sync(&host->timer); |
| 1751 | |
| 1752 | tasklet_kill(&host->card_tasklet); |
| 1753 | tasklet_kill(&host->finish_tasklet); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 1754 | |
| 1755 | kfree(host->adma_desc); |
| 1756 | kfree(host->align_buffer); |
| 1757 | |
| 1758 | host->adma_desc = NULL; |
| 1759 | host->align_buffer = NULL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1760 | } |
| 1761 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1762 | EXPORT_SYMBOL_GPL(sdhci_remove_host); |
| 1763 | |
| 1764 | void sdhci_free_host(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1765 | { |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1766 | mmc_free_host(host->mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1769 | EXPORT_SYMBOL_GPL(sdhci_free_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1770 | |
| 1771 | /*****************************************************************************\ |
| 1772 | * * |
| 1773 | * Driver init/exit * |
| 1774 | * * |
| 1775 | \*****************************************************************************/ |
| 1776 | |
| 1777 | static int __init sdhci_drv_init(void) |
| 1778 | { |
| 1779 | printk(KERN_INFO DRIVER_NAME |
Pierre Ossman | 52fbf9c | 2007-02-09 08:23:41 +0100 | [diff] [blame] | 1780 | ": Secure Digital Host Controller Interface driver\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1781 | printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n"); |
| 1782 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1783 | return 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | static void __exit sdhci_drv_exit(void) |
| 1787 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | module_init(sdhci_drv_init); |
| 1791 | module_exit(sdhci_drv_exit); |
| 1792 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1793 | module_param(debug_quirks, uint, 0444); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1794 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1795 | MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>"); |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1796 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1797 | MODULE_LICENSE("GPL"); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1798 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1799 | MODULE_PARM_DESC(debug_quirks, "Force certain quirks."); |