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