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 | * |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 4 | * Copyright (C) 2005-2007 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 | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 12 | #include <linux/delay.h> |
| 13 | #include <linux/highmem.h> |
| 14 | #include <linux/pci.h> |
| 15 | #include <linux/dma-mapping.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame^] | 16 | #include <linux/scatterlist.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 17 | |
| 18 | #include <linux/mmc/host.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 19 | |
| 20 | #include <asm/scatterlist.h> |
| 21 | |
| 22 | #include "sdhci.h" |
| 23 | |
| 24 | #define DRIVER_NAME "sdhci" |
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 | #define DBG(f, x...) \ |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 27 | pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 28 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 29 | static unsigned int debug_quirks = 0; |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 30 | |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 31 | #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0) |
Pierre Ossman | 9860807 | 2006-06-30 02:22:34 -0700 | [diff] [blame] | 32 | #define SDHCI_QUIRK_FORCE_DMA (1<<1) |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 33 | /* Controller doesn't like some resets when there is no card inserted. */ |
| 34 | #define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2) |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 35 | #define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3) |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 36 | #define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4) |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 37 | #define SDHCI_QUIRK_BROKEN_DMA (1<<5) |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 38 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 39 | static const struct pci_device_id pci_ids[] __devinitdata = { |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 40 | { |
| 41 | .vendor = PCI_VENDOR_ID_RICOH, |
| 42 | .device = PCI_DEVICE_ID_RICOH_R5C822, |
| 43 | .subvendor = PCI_VENDOR_ID_IBM, |
| 44 | .subdevice = PCI_ANY_ID, |
Pierre Ossman | 9860807 | 2006-06-30 02:22:34 -0700 | [diff] [blame] | 45 | .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET | |
| 46 | SDHCI_QUIRK_FORCE_DMA, |
| 47 | }, |
| 48 | |
| 49 | { |
| 50 | .vendor = PCI_VENDOR_ID_RICOH, |
| 51 | .device = PCI_DEVICE_ID_RICOH_R5C822, |
| 52 | .subvendor = PCI_ANY_ID, |
| 53 | .subdevice = PCI_ANY_ID, |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 54 | .driver_data = SDHCI_QUIRK_FORCE_DMA | |
| 55 | SDHCI_QUIRK_NO_CARD_NO_RESET, |
Pierre Ossman | 9860807 | 2006-06-30 02:22:34 -0700 | [diff] [blame] | 56 | }, |
| 57 | |
| 58 | { |
| 59 | .vendor = PCI_VENDOR_ID_TI, |
| 60 | .device = PCI_DEVICE_ID_TI_XX21_XX11_SD, |
| 61 | .subvendor = PCI_ANY_ID, |
| 62 | .subdevice = PCI_ANY_ID, |
| 63 | .driver_data = SDHCI_QUIRK_FORCE_DMA, |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 64 | }, |
| 65 | |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 66 | { |
| 67 | .vendor = PCI_VENDOR_ID_ENE, |
| 68 | .device = PCI_DEVICE_ID_ENE_CB712_SD, |
| 69 | .subvendor = PCI_ANY_ID, |
| 70 | .subdevice = PCI_ANY_ID, |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 71 | .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE | |
| 72 | SDHCI_QUIRK_BROKEN_DMA, |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 73 | }, |
| 74 | |
Milko Krachounov | 7de064e | 2007-05-19 01:18:03 +0200 | [diff] [blame] | 75 | { |
| 76 | .vendor = PCI_VENDOR_ID_ENE, |
| 77 | .device = PCI_DEVICE_ID_ENE_CB712_SD_2, |
| 78 | .subvendor = PCI_ANY_ID, |
| 79 | .subdevice = PCI_ANY_ID, |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 80 | .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE | |
| 81 | SDHCI_QUIRK_BROKEN_DMA, |
Milko Krachounov | 7de064e | 2007-05-19 01:18:03 +0200 | [diff] [blame] | 82 | }, |
| 83 | |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 84 | { |
| 85 | .vendor = PCI_VENDOR_ID_ENE, |
| 86 | .device = PCI_DEVICE_ID_ENE_CB714_SD, |
| 87 | .subvendor = PCI_ANY_ID, |
| 88 | .subdevice = PCI_ANY_ID, |
| 89 | .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE | |
| 90 | SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS, |
| 91 | }, |
| 92 | |
| 93 | { |
| 94 | .vendor = PCI_VENDOR_ID_ENE, |
| 95 | .device = PCI_DEVICE_ID_ENE_CB714_SD_2, |
| 96 | .subvendor = PCI_ANY_ID, |
| 97 | .subdevice = PCI_ANY_ID, |
| 98 | .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE | |
| 99 | SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS, |
| 100 | }, |
| 101 | |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 102 | { /* Generic SD host controller */ |
| 103 | PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00) |
| 104 | }, |
| 105 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 106 | { /* end: all zeroes */ }, |
| 107 | }; |
| 108 | |
| 109 | MODULE_DEVICE_TABLE(pci, pci_ids); |
| 110 | |
| 111 | static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *); |
| 112 | static void sdhci_finish_data(struct sdhci_host *); |
| 113 | |
| 114 | static void sdhci_send_command(struct sdhci_host *, struct mmc_command *); |
| 115 | static void sdhci_finish_command(struct sdhci_host *); |
| 116 | |
| 117 | static void sdhci_dumpregs(struct sdhci_host *host) |
| 118 | { |
| 119 | printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n"); |
| 120 | |
| 121 | printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n", |
| 122 | readl(host->ioaddr + SDHCI_DMA_ADDRESS), |
| 123 | readw(host->ioaddr + SDHCI_HOST_VERSION)); |
| 124 | printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n", |
| 125 | readw(host->ioaddr + SDHCI_BLOCK_SIZE), |
| 126 | readw(host->ioaddr + SDHCI_BLOCK_COUNT)); |
| 127 | printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n", |
| 128 | readl(host->ioaddr + SDHCI_ARGUMENT), |
| 129 | readw(host->ioaddr + SDHCI_TRANSFER_MODE)); |
| 130 | printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n", |
| 131 | readl(host->ioaddr + SDHCI_PRESENT_STATE), |
| 132 | readb(host->ioaddr + SDHCI_HOST_CONTROL)); |
| 133 | printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n", |
| 134 | readb(host->ioaddr + SDHCI_POWER_CONTROL), |
| 135 | readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL)); |
| 136 | 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] | 137 | readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL), |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 138 | readw(host->ioaddr + SDHCI_CLOCK_CONTROL)); |
| 139 | printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n", |
| 140 | readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL), |
| 141 | readl(host->ioaddr + SDHCI_INT_STATUS)); |
| 142 | printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n", |
| 143 | readl(host->ioaddr + SDHCI_INT_ENABLE), |
| 144 | readl(host->ioaddr + SDHCI_SIGNAL_ENABLE)); |
| 145 | printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n", |
| 146 | readw(host->ioaddr + SDHCI_ACMD12_ERR), |
| 147 | readw(host->ioaddr + SDHCI_SLOT_INT_STATUS)); |
| 148 | printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n", |
| 149 | readl(host->ioaddr + SDHCI_CAPABILITIES), |
| 150 | readl(host->ioaddr + SDHCI_MAX_CURRENT)); |
| 151 | |
| 152 | printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n"); |
| 153 | } |
| 154 | |
| 155 | /*****************************************************************************\ |
| 156 | * * |
| 157 | * Low level functions * |
| 158 | * * |
| 159 | \*****************************************************************************/ |
| 160 | |
| 161 | static void sdhci_reset(struct sdhci_host *host, u8 mask) |
| 162 | { |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 163 | unsigned long timeout; |
| 164 | |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 165 | if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { |
| 166 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & |
| 167 | SDHCI_CARD_PRESENT)) |
| 168 | return; |
| 169 | } |
| 170 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 171 | writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET); |
| 172 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 173 | if (mask & SDHCI_RESET_ALL) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 174 | host->clock = 0; |
| 175 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 176 | /* Wait max 100 ms */ |
| 177 | timeout = 100; |
| 178 | |
| 179 | /* hw clears the bit when it's done */ |
| 180 | while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) { |
| 181 | if (timeout == 0) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 182 | printk(KERN_ERR "%s: Reset 0x%x never completed.\n", |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 183 | mmc_hostname(host->mmc), (int)mask); |
| 184 | sdhci_dumpregs(host); |
| 185 | return; |
| 186 | } |
| 187 | timeout--; |
| 188 | mdelay(1); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | static void sdhci_init(struct sdhci_host *host) |
| 193 | { |
| 194 | u32 intmask; |
| 195 | |
| 196 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 197 | |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 198 | intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | |
| 199 | SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | |
| 200 | SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | |
| 201 | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 202 | SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 203 | SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 204 | |
| 205 | writel(intmask, host->ioaddr + SDHCI_INT_ENABLE); |
| 206 | writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void sdhci_activate_led(struct sdhci_host *host) |
| 210 | { |
| 211 | u8 ctrl; |
| 212 | |
| 213 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 214 | ctrl |= SDHCI_CTRL_LED; |
| 215 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 216 | } |
| 217 | |
| 218 | static void sdhci_deactivate_led(struct sdhci_host *host) |
| 219 | { |
| 220 | u8 ctrl; |
| 221 | |
| 222 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 223 | ctrl &= ~SDHCI_CTRL_LED; |
| 224 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 225 | } |
| 226 | |
| 227 | /*****************************************************************************\ |
| 228 | * * |
| 229 | * Core functions * |
| 230 | * * |
| 231 | \*****************************************************************************/ |
| 232 | |
Pierre Ossman | 2a22b14 | 2007-02-02 18:27:42 +0100 | [diff] [blame] | 233 | static inline char* sdhci_sg_to_buffer(struct sdhci_host* host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 234 | { |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 235 | return sg_virt(host->cur_sg); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static inline int sdhci_next_sg(struct sdhci_host* host) |
| 239 | { |
| 240 | /* |
| 241 | * Skip to next SG entry. |
| 242 | */ |
| 243 | host->cur_sg++; |
| 244 | host->num_sg--; |
| 245 | |
| 246 | /* |
| 247 | * Any entries left? |
| 248 | */ |
| 249 | if (host->num_sg > 0) { |
| 250 | host->offset = 0; |
| 251 | host->remain = host->cur_sg->length; |
| 252 | } |
| 253 | |
| 254 | return host->num_sg; |
| 255 | } |
| 256 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 257 | static void sdhci_read_block_pio(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 258 | { |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 259 | int blksize, chunk_remain; |
| 260 | u32 data; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 261 | char *buffer; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 262 | int size; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 263 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 264 | DBG("PIO reading\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 265 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 266 | blksize = host->data->blksz; |
| 267 | chunk_remain = 0; |
| 268 | data = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 269 | |
Pierre Ossman | 2a22b14 | 2007-02-02 18:27:42 +0100 | [diff] [blame] | 270 | buffer = sdhci_sg_to_buffer(host) + host->offset; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 271 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 272 | while (blksize) { |
| 273 | if (chunk_remain == 0) { |
| 274 | data = readl(host->ioaddr + SDHCI_BUFFER); |
| 275 | chunk_remain = min(blksize, 4); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 278 | size = min(host->remain, chunk_remain); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 279 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 280 | chunk_remain -= size; |
| 281 | blksize -= size; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 282 | host->offset += size; |
| 283 | host->remain -= size; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 284 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 285 | while (size) { |
| 286 | *buffer = data & 0xFF; |
| 287 | buffer++; |
| 288 | data >>= 8; |
| 289 | size--; |
| 290 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 291 | |
| 292 | if (host->remain == 0) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 293 | if (sdhci_next_sg(host) == 0) { |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 294 | BUG_ON(blksize != 0); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 295 | return; |
| 296 | } |
Pierre Ossman | 2a22b14 | 2007-02-02 18:27:42 +0100 | [diff] [blame] | 297 | buffer = sdhci_sg_to_buffer(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 300 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 301 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 302 | static void sdhci_write_block_pio(struct sdhci_host *host) |
| 303 | { |
| 304 | int blksize, chunk_remain; |
| 305 | u32 data; |
| 306 | char *buffer; |
| 307 | int bytes, size; |
| 308 | |
| 309 | DBG("PIO writing\n"); |
| 310 | |
| 311 | blksize = host->data->blksz; |
| 312 | chunk_remain = 4; |
| 313 | data = 0; |
| 314 | |
| 315 | bytes = 0; |
Pierre Ossman | 2a22b14 | 2007-02-02 18:27:42 +0100 | [diff] [blame] | 316 | buffer = sdhci_sg_to_buffer(host) + host->offset; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 317 | |
| 318 | while (blksize) { |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 319 | size = min(host->remain, chunk_remain); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 320 | |
| 321 | chunk_remain -= size; |
| 322 | blksize -= size; |
| 323 | host->offset += size; |
| 324 | host->remain -= size; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 325 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 326 | while (size) { |
| 327 | data >>= 8; |
| 328 | data |= (u32)*buffer << 24; |
| 329 | buffer++; |
| 330 | size--; |
| 331 | } |
| 332 | |
| 333 | if (chunk_remain == 0) { |
| 334 | writel(data, host->ioaddr + SDHCI_BUFFER); |
| 335 | chunk_remain = min(blksize, 4); |
| 336 | } |
| 337 | |
| 338 | if (host->remain == 0) { |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 339 | if (sdhci_next_sg(host) == 0) { |
| 340 | BUG_ON(blksize != 0); |
| 341 | return; |
| 342 | } |
Pierre Ossman | 2a22b14 | 2007-02-02 18:27:42 +0100 | [diff] [blame] | 343 | buffer = sdhci_sg_to_buffer(host); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 344 | } |
| 345 | } |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static void sdhci_transfer_pio(struct sdhci_host *host) |
| 349 | { |
| 350 | u32 mask; |
| 351 | |
| 352 | BUG_ON(!host->data); |
| 353 | |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 354 | if (host->num_sg == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 355 | return; |
| 356 | |
| 357 | if (host->data->flags & MMC_DATA_READ) |
| 358 | mask = SDHCI_DATA_AVAILABLE; |
| 359 | else |
| 360 | mask = SDHCI_SPACE_AVAILABLE; |
| 361 | |
| 362 | while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) { |
| 363 | if (host->data->flags & MMC_DATA_READ) |
| 364 | sdhci_read_block_pio(host); |
| 365 | else |
| 366 | sdhci_write_block_pio(host); |
| 367 | |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 368 | if (host->num_sg == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 369 | break; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | DBG("PIO transfer complete.\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data) |
| 376 | { |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 377 | u8 count; |
| 378 | unsigned target_timeout, current_timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 379 | |
| 380 | WARN_ON(host->data); |
| 381 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 382 | if (data == NULL) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 383 | return; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 384 | |
Pierre Ossman | bab7696 | 2006-07-02 16:51:35 +0100 | [diff] [blame] | 385 | /* Sanity checks */ |
| 386 | BUG_ON(data->blksz * data->blocks > 524288); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 387 | BUG_ON(data->blksz > host->mmc->max_blk_size); |
Pierre Ossman | 1d676e0 | 2006-07-02 16:52:10 +0100 | [diff] [blame] | 388 | BUG_ON(data->blocks > 65535); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 389 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 390 | host->data = data; |
| 391 | host->data_early = 0; |
| 392 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 393 | /* timeout in us */ |
| 394 | target_timeout = data->timeout_ns / 1000 + |
| 395 | data->timeout_clks / host->clock; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 396 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 397 | /* |
| 398 | * Figure out needed cycles. |
| 399 | * We do this in steps in order to fit inside a 32 bit int. |
| 400 | * The first step is the minimum timeout, which will have a |
| 401 | * minimum resolution of 6 bits: |
| 402 | * (1) 2^13*1000 > 2^22, |
| 403 | * (2) host->timeout_clk < 2^16 |
| 404 | * => |
| 405 | * (1) / (2) > 2^6 |
| 406 | */ |
| 407 | count = 0; |
| 408 | current_timeout = (1 << 13) * 1000 / host->timeout_clk; |
| 409 | while (current_timeout < target_timeout) { |
| 410 | count++; |
| 411 | current_timeout <<= 1; |
| 412 | if (count >= 0xF) |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | if (count >= 0xF) { |
| 417 | printk(KERN_WARNING "%s: Too large timeout requested!\n", |
| 418 | mmc_hostname(host->mmc)); |
| 419 | count = 0xE; |
| 420 | } |
| 421 | |
| 422 | writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 423 | |
| 424 | if (host->flags & SDHCI_USE_DMA) { |
| 425 | int count; |
| 426 | |
| 427 | count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len, |
| 428 | (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE); |
| 429 | BUG_ON(count != 1); |
| 430 | |
| 431 | writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS); |
| 432 | } else { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 433 | host->cur_sg = data->sg; |
| 434 | host->num_sg = data->sg_len; |
| 435 | |
| 436 | host->offset = 0; |
| 437 | host->remain = host->cur_sg->length; |
| 438 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 439 | |
Pierre Ossman | bab7696 | 2006-07-02 16:51:35 +0100 | [diff] [blame] | 440 | /* We do not handle DMA boundaries, so set it to max (512 KiB) */ |
| 441 | writew(SDHCI_MAKE_BLKSZ(7, data->blksz), |
| 442 | host->ioaddr + SDHCI_BLOCK_SIZE); |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 443 | writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT); |
| 444 | } |
| 445 | |
| 446 | static void sdhci_set_transfer_mode(struct sdhci_host *host, |
| 447 | struct mmc_data *data) |
| 448 | { |
| 449 | u16 mode; |
| 450 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 451 | if (data == NULL) |
| 452 | return; |
| 453 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 454 | WARN_ON(!host->data); |
| 455 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 456 | mode = SDHCI_TRNS_BLK_CNT_EN; |
| 457 | if (data->blocks > 1) |
| 458 | mode |= SDHCI_TRNS_MULTI; |
| 459 | if (data->flags & MMC_DATA_READ) |
| 460 | mode |= SDHCI_TRNS_READ; |
| 461 | if (host->flags & SDHCI_USE_DMA) |
| 462 | mode |= SDHCI_TRNS_DMA; |
| 463 | |
| 464 | writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static void sdhci_finish_data(struct sdhci_host *host) |
| 468 | { |
| 469 | struct mmc_data *data; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 470 | u16 blocks; |
| 471 | |
| 472 | BUG_ON(!host->data); |
| 473 | |
| 474 | data = host->data; |
| 475 | host->data = NULL; |
| 476 | |
| 477 | if (host->flags & SDHCI_USE_DMA) { |
| 478 | pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len, |
| 479 | (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Controller doesn't count down when in single block mode. |
| 484 | */ |
Pierre Ossman | 2b06197 | 2007-08-12 13:13:24 +0200 | [diff] [blame] | 485 | if (data->blocks == 1) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 486 | blocks = (data->error == 0) ? 0 : 1; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 487 | else |
| 488 | blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT); |
Russell King | a3fd4a1 | 2006-06-04 17:51:15 +0100 | [diff] [blame] | 489 | data->bytes_xfered = data->blksz * (data->blocks - blocks); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 490 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 491 | if (!data->error && blocks) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 492 | printk(KERN_ERR "%s: Controller signalled completion even " |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 493 | "though there were blocks left.\n", |
| 494 | mmc_hostname(host->mmc)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 495 | data->error = -EIO; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 498 | if (data->stop) { |
| 499 | /* |
| 500 | * The controller needs a reset of internal state machines |
| 501 | * upon error conditions. |
| 502 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 503 | if (data->error) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 504 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 505 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 506 | } |
| 507 | |
| 508 | sdhci_send_command(host, data->stop); |
| 509 | } else |
| 510 | tasklet_schedule(&host->finish_tasklet); |
| 511 | } |
| 512 | |
| 513 | static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd) |
| 514 | { |
| 515 | int flags; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 516 | u32 mask; |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 517 | unsigned long timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 518 | |
| 519 | WARN_ON(host->cmd); |
| 520 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 521 | /* Wait max 10 ms */ |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 522 | timeout = 10; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 523 | |
| 524 | mask = SDHCI_CMD_INHIBIT; |
| 525 | if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY)) |
| 526 | mask |= SDHCI_DATA_INHIBIT; |
| 527 | |
| 528 | /* We shouldn't wait for data inihibit for stop commands, even |
| 529 | though they might use busy signaling */ |
| 530 | if (host->mrq->data && (cmd == host->mrq->data->stop)) |
| 531 | mask &= ~SDHCI_DATA_INHIBIT; |
| 532 | |
| 533 | while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) { |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 534 | if (timeout == 0) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 535 | printk(KERN_ERR "%s: Controller never released " |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 536 | "inhibit bit(s).\n", mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 537 | sdhci_dumpregs(host); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 538 | cmd->error = -EIO; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 539 | tasklet_schedule(&host->finish_tasklet); |
| 540 | return; |
| 541 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 542 | timeout--; |
| 543 | mdelay(1); |
| 544 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 545 | |
| 546 | mod_timer(&host->timer, jiffies + 10 * HZ); |
| 547 | |
| 548 | host->cmd = cmd; |
| 549 | |
| 550 | sdhci_prepare_data(host, cmd->data); |
| 551 | |
| 552 | writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT); |
| 553 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 554 | sdhci_set_transfer_mode(host, cmd->data); |
| 555 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 556 | if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 557 | printk(KERN_ERR "%s: Unsupported response type!\n", |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 558 | mmc_hostname(host->mmc)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 559 | cmd->error = -EINVAL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 560 | tasklet_schedule(&host->finish_tasklet); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | if (!(cmd->flags & MMC_RSP_PRESENT)) |
| 565 | flags = SDHCI_CMD_RESP_NONE; |
| 566 | else if (cmd->flags & MMC_RSP_136) |
| 567 | flags = SDHCI_CMD_RESP_LONG; |
| 568 | else if (cmd->flags & MMC_RSP_BUSY) |
| 569 | flags = SDHCI_CMD_RESP_SHORT_BUSY; |
| 570 | else |
| 571 | flags = SDHCI_CMD_RESP_SHORT; |
| 572 | |
| 573 | if (cmd->flags & MMC_RSP_CRC) |
| 574 | flags |= SDHCI_CMD_CRC; |
| 575 | if (cmd->flags & MMC_RSP_OPCODE) |
| 576 | flags |= SDHCI_CMD_INDEX; |
| 577 | if (cmd->data) |
| 578 | flags |= SDHCI_CMD_DATA; |
| 579 | |
Pierre Ossman | fb61e28 | 2006-07-11 21:06:48 +0200 | [diff] [blame] | 580 | writew(SDHCI_MAKE_CMD(cmd->opcode, flags), |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 581 | host->ioaddr + SDHCI_COMMAND); |
| 582 | } |
| 583 | |
| 584 | static void sdhci_finish_command(struct sdhci_host *host) |
| 585 | { |
| 586 | int i; |
| 587 | |
| 588 | BUG_ON(host->cmd == NULL); |
| 589 | |
| 590 | if (host->cmd->flags & MMC_RSP_PRESENT) { |
| 591 | if (host->cmd->flags & MMC_RSP_136) { |
| 592 | /* CRC is stripped so we need to do some shifting. */ |
| 593 | for (i = 0;i < 4;i++) { |
| 594 | host->cmd->resp[i] = readl(host->ioaddr + |
| 595 | SDHCI_RESPONSE + (3-i)*4) << 8; |
| 596 | if (i != 3) |
| 597 | host->cmd->resp[i] |= |
| 598 | readb(host->ioaddr + |
| 599 | SDHCI_RESPONSE + (3-i)*4-1); |
| 600 | } |
| 601 | } else { |
| 602 | host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE); |
| 603 | } |
| 604 | } |
| 605 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 606 | host->cmd->error = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 607 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 608 | if (host->data && host->data_early) |
| 609 | sdhci_finish_data(host); |
| 610 | |
| 611 | if (!host->cmd->data) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 612 | tasklet_schedule(&host->finish_tasklet); |
| 613 | |
| 614 | host->cmd = NULL; |
| 615 | } |
| 616 | |
| 617 | static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock) |
| 618 | { |
| 619 | int div; |
| 620 | u16 clk; |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 621 | unsigned long timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 622 | |
| 623 | if (clock == host->clock) |
| 624 | return; |
| 625 | |
| 626 | writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 627 | |
| 628 | if (clock == 0) |
| 629 | goto out; |
| 630 | |
| 631 | for (div = 1;div < 256;div *= 2) { |
| 632 | if ((host->max_clk / div) <= clock) |
| 633 | break; |
| 634 | } |
| 635 | div >>= 1; |
| 636 | |
| 637 | clk = div << SDHCI_DIVIDER_SHIFT; |
| 638 | clk |= SDHCI_CLOCK_INT_EN; |
| 639 | writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 640 | |
| 641 | /* Wait max 10 ms */ |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 642 | timeout = 10; |
| 643 | while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL)) |
| 644 | & SDHCI_CLOCK_INT_STABLE)) { |
| 645 | if (timeout == 0) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 646 | printk(KERN_ERR "%s: Internal clock never " |
| 647 | "stabilised.\n", mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 648 | sdhci_dumpregs(host); |
| 649 | return; |
| 650 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 651 | timeout--; |
| 652 | mdelay(1); |
| 653 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 654 | |
| 655 | clk |= SDHCI_CLOCK_CARD_EN; |
| 656 | writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 657 | |
| 658 | out: |
| 659 | host->clock = clock; |
| 660 | } |
| 661 | |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 662 | static void sdhci_set_power(struct sdhci_host *host, unsigned short power) |
| 663 | { |
| 664 | u8 pwr; |
| 665 | |
| 666 | if (host->power == power) |
| 667 | return; |
| 668 | |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 669 | if (power == (unsigned short)-1) { |
| 670 | writeb(0, host->ioaddr + SDHCI_POWER_CONTROL); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 671 | goto out; |
Darren Salt | 9e9dc5f | 2007-01-27 15:32:31 +0100 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Spec says that we should clear the power reg before setting |
| 676 | * a new value. Some controllers don't seem to like this though. |
| 677 | */ |
| 678 | if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) |
| 679 | writeb(0, host->ioaddr + SDHCI_POWER_CONTROL); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 680 | |
| 681 | pwr = SDHCI_POWER_ON; |
| 682 | |
Philip Langdale | 4be34c9 | 2007-03-11 17:15:15 -0700 | [diff] [blame] | 683 | switch (1 << power) { |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 684 | case MMC_VDD_165_195: |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 685 | pwr |= SDHCI_POWER_180; |
| 686 | break; |
Philip Langdale | 4be34c9 | 2007-03-11 17:15:15 -0700 | [diff] [blame] | 687 | case MMC_VDD_29_30: |
| 688 | case MMC_VDD_30_31: |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 689 | pwr |= SDHCI_POWER_300; |
| 690 | break; |
Philip Langdale | 4be34c9 | 2007-03-11 17:15:15 -0700 | [diff] [blame] | 691 | case MMC_VDD_32_33: |
| 692 | case MMC_VDD_33_34: |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 693 | pwr |= SDHCI_POWER_330; |
| 694 | break; |
| 695 | default: |
| 696 | BUG(); |
| 697 | } |
| 698 | |
| 699 | writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL); |
| 700 | |
| 701 | out: |
| 702 | host->power = power; |
| 703 | } |
| 704 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 705 | /*****************************************************************************\ |
| 706 | * * |
| 707 | * MMC callbacks * |
| 708 | * * |
| 709 | \*****************************************************************************/ |
| 710 | |
| 711 | static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 712 | { |
| 713 | struct sdhci_host *host; |
| 714 | unsigned long flags; |
| 715 | |
| 716 | host = mmc_priv(mmc); |
| 717 | |
| 718 | spin_lock_irqsave(&host->lock, flags); |
| 719 | |
| 720 | WARN_ON(host->mrq != NULL); |
| 721 | |
| 722 | sdhci_activate_led(host); |
| 723 | |
| 724 | host->mrq = mrq; |
| 725 | |
| 726 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 727 | host->mrq->cmd->error = -ENOMEDIUM; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 728 | tasklet_schedule(&host->finish_tasklet); |
| 729 | } else |
| 730 | sdhci_send_command(host, mrq->cmd); |
| 731 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 732 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 733 | spin_unlock_irqrestore(&host->lock, flags); |
| 734 | } |
| 735 | |
| 736 | static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 737 | { |
| 738 | struct sdhci_host *host; |
| 739 | unsigned long flags; |
| 740 | u8 ctrl; |
| 741 | |
| 742 | host = mmc_priv(mmc); |
| 743 | |
| 744 | spin_lock_irqsave(&host->lock, flags); |
| 745 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 746 | /* |
| 747 | * Reset the chip on each power off. |
| 748 | * Should clear out any weird states. |
| 749 | */ |
| 750 | if (ios->power_mode == MMC_POWER_OFF) { |
| 751 | writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 752 | sdhci_init(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | sdhci_set_clock(host, ios->clock); |
| 756 | |
| 757 | if (ios->power_mode == MMC_POWER_OFF) |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 758 | sdhci_set_power(host, -1); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 759 | else |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 760 | sdhci_set_power(host, ios->vdd); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 761 | |
| 762 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 763 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 764 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 765 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 766 | else |
| 767 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 768 | |
| 769 | if (ios->timing == MMC_TIMING_SD_HS) |
| 770 | ctrl |= SDHCI_CTRL_HISPD; |
| 771 | else |
| 772 | ctrl &= ~SDHCI_CTRL_HISPD; |
| 773 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 774 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 775 | |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 776 | /* |
| 777 | * Some (ENE) controllers go apeshit on some ios operation, |
| 778 | * signalling timeout and CRC errors even on CMD0. Resetting |
| 779 | * it on each ios seems to solve the problem. |
| 780 | */ |
| 781 | if(host->chip->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS) |
| 782 | sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
| 783 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 784 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 785 | spin_unlock_irqrestore(&host->lock, flags); |
| 786 | } |
| 787 | |
| 788 | static int sdhci_get_ro(struct mmc_host *mmc) |
| 789 | { |
| 790 | struct sdhci_host *host; |
| 791 | unsigned long flags; |
| 792 | int present; |
| 793 | |
| 794 | host = mmc_priv(mmc); |
| 795 | |
| 796 | spin_lock_irqsave(&host->lock, flags); |
| 797 | |
| 798 | present = readl(host->ioaddr + SDHCI_PRESENT_STATE); |
| 799 | |
| 800 | spin_unlock_irqrestore(&host->lock, flags); |
| 801 | |
| 802 | return !(present & SDHCI_WRITE_PROTECT); |
| 803 | } |
| 804 | |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 805 | static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 806 | { |
| 807 | struct sdhci_host *host; |
| 808 | unsigned long flags; |
| 809 | u32 ier; |
| 810 | |
| 811 | host = mmc_priv(mmc); |
| 812 | |
| 813 | spin_lock_irqsave(&host->lock, flags); |
| 814 | |
| 815 | ier = readl(host->ioaddr + SDHCI_INT_ENABLE); |
| 816 | |
| 817 | ier &= ~SDHCI_INT_CARD_INT; |
| 818 | if (enable) |
| 819 | ier |= SDHCI_INT_CARD_INT; |
| 820 | |
| 821 | writel(ier, host->ioaddr + SDHCI_INT_ENABLE); |
| 822 | writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 823 | |
| 824 | mmiowb(); |
| 825 | |
| 826 | spin_unlock_irqrestore(&host->lock, flags); |
| 827 | } |
| 828 | |
David Brownell | ab7aefd | 2006-11-12 17:55:30 -0800 | [diff] [blame] | 829 | static const struct mmc_host_ops sdhci_ops = { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 830 | .request = sdhci_request, |
| 831 | .set_ios = sdhci_set_ios, |
| 832 | .get_ro = sdhci_get_ro, |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 833 | .enable_sdio_irq = sdhci_enable_sdio_irq, |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 834 | }; |
| 835 | |
| 836 | /*****************************************************************************\ |
| 837 | * * |
| 838 | * Tasklets * |
| 839 | * * |
| 840 | \*****************************************************************************/ |
| 841 | |
| 842 | static void sdhci_tasklet_card(unsigned long param) |
| 843 | { |
| 844 | struct sdhci_host *host; |
| 845 | unsigned long flags; |
| 846 | |
| 847 | host = (struct sdhci_host*)param; |
| 848 | |
| 849 | spin_lock_irqsave(&host->lock, flags); |
| 850 | |
| 851 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { |
| 852 | if (host->mrq) { |
| 853 | printk(KERN_ERR "%s: Card removed during transfer!\n", |
| 854 | mmc_hostname(host->mmc)); |
| 855 | printk(KERN_ERR "%s: Resetting controller.\n", |
| 856 | mmc_hostname(host->mmc)); |
| 857 | |
| 858 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 859 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 860 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 861 | host->mrq->cmd->error = -ENOMEDIUM; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 862 | tasklet_schedule(&host->finish_tasklet); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | spin_unlock_irqrestore(&host->lock, flags); |
| 867 | |
| 868 | mmc_detect_change(host->mmc, msecs_to_jiffies(500)); |
| 869 | } |
| 870 | |
| 871 | static void sdhci_tasklet_finish(unsigned long param) |
| 872 | { |
| 873 | struct sdhci_host *host; |
| 874 | unsigned long flags; |
| 875 | struct mmc_request *mrq; |
| 876 | |
| 877 | host = (struct sdhci_host*)param; |
| 878 | |
| 879 | spin_lock_irqsave(&host->lock, flags); |
| 880 | |
| 881 | del_timer(&host->timer); |
| 882 | |
| 883 | mrq = host->mrq; |
| 884 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 885 | /* |
| 886 | * The controller needs a reset of internal state machines |
| 887 | * upon error conditions. |
| 888 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 889 | if (mrq->cmd->error || |
| 890 | (mrq->data && (mrq->data->error || |
| 891 | (mrq->data->stop && mrq->data->stop->error)))) { |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 892 | |
| 893 | /* Some controllers need this kick or reset won't work here */ |
| 894 | if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) { |
| 895 | unsigned int clock; |
| 896 | |
| 897 | /* This is to force an update */ |
| 898 | clock = host->clock; |
| 899 | host->clock = 0; |
| 900 | sdhci_set_clock(host, clock); |
| 901 | } |
| 902 | |
| 903 | /* Spec says we should do both at the same time, but Ricoh |
| 904 | controllers do not like that. */ |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 905 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 906 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 907 | } |
| 908 | |
| 909 | host->mrq = NULL; |
| 910 | host->cmd = NULL; |
| 911 | host->data = NULL; |
| 912 | |
| 913 | sdhci_deactivate_led(host); |
| 914 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 915 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 916 | spin_unlock_irqrestore(&host->lock, flags); |
| 917 | |
| 918 | mmc_request_done(host->mmc, mrq); |
| 919 | } |
| 920 | |
| 921 | static void sdhci_timeout_timer(unsigned long data) |
| 922 | { |
| 923 | struct sdhci_host *host; |
| 924 | unsigned long flags; |
| 925 | |
| 926 | host = (struct sdhci_host*)data; |
| 927 | |
| 928 | spin_lock_irqsave(&host->lock, flags); |
| 929 | |
| 930 | if (host->mrq) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 931 | printk(KERN_ERR "%s: Timeout waiting for hardware " |
| 932 | "interrupt.\n", mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 933 | sdhci_dumpregs(host); |
| 934 | |
| 935 | if (host->data) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 936 | host->data->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 937 | sdhci_finish_data(host); |
| 938 | } else { |
| 939 | if (host->cmd) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 940 | host->cmd->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 941 | else |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 942 | host->mrq->cmd->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 943 | |
| 944 | tasklet_schedule(&host->finish_tasklet); |
| 945 | } |
| 946 | } |
| 947 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 948 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 949 | spin_unlock_irqrestore(&host->lock, flags); |
| 950 | } |
| 951 | |
| 952 | /*****************************************************************************\ |
| 953 | * * |
| 954 | * Interrupt handling * |
| 955 | * * |
| 956 | \*****************************************************************************/ |
| 957 | |
| 958 | static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask) |
| 959 | { |
| 960 | BUG_ON(intmask == 0); |
| 961 | |
| 962 | if (!host->cmd) { |
Pierre Ossman | b67ac3f | 2007-08-12 17:29:47 +0200 | [diff] [blame] | 963 | printk(KERN_ERR "%s: Got command interrupt 0x%08x even " |
| 964 | "though no command operation was in progress.\n", |
| 965 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 966 | sdhci_dumpregs(host); |
| 967 | return; |
| 968 | } |
| 969 | |
Pierre Ossman | 43b58b3 | 2007-07-25 23:15:27 +0200 | [diff] [blame] | 970 | if (intmask & SDHCI_INT_TIMEOUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 971 | host->cmd->error = -ETIMEDOUT; |
| 972 | else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT | |
| 973 | SDHCI_INT_INDEX)) |
| 974 | host->cmd->error = -EILSEQ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 975 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 976 | if (host->cmd->error) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 977 | tasklet_schedule(&host->finish_tasklet); |
Pierre Ossman | 43b58b3 | 2007-07-25 23:15:27 +0200 | [diff] [blame] | 978 | else if (intmask & SDHCI_INT_RESPONSE) |
| 979 | sdhci_finish_command(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static void sdhci_data_irq(struct sdhci_host *host, u32 intmask) |
| 983 | { |
| 984 | BUG_ON(intmask == 0); |
| 985 | |
| 986 | if (!host->data) { |
| 987 | /* |
| 988 | * A data end interrupt is sent together with the response |
| 989 | * for the stop command. |
| 990 | */ |
| 991 | if (intmask & SDHCI_INT_DATA_END) |
| 992 | return; |
| 993 | |
Pierre Ossman | b67ac3f | 2007-08-12 17:29:47 +0200 | [diff] [blame] | 994 | printk(KERN_ERR "%s: Got data interrupt 0x%08x even " |
| 995 | "though no data operation was in progress.\n", |
| 996 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 997 | sdhci_dumpregs(host); |
| 998 | |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | if (intmask & SDHCI_INT_DATA_TIMEOUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1003 | host->data->error = -ETIMEDOUT; |
| 1004 | else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) |
| 1005 | host->data->error = -EILSEQ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1006 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1007 | if (host->data->error) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1008 | sdhci_finish_data(host); |
| 1009 | else { |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 1010 | if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1011 | sdhci_transfer_pio(host); |
| 1012 | |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 1013 | /* |
| 1014 | * We currently don't do anything fancy with DMA |
| 1015 | * boundaries, but as we can't disable the feature |
| 1016 | * we need to at least restart the transfer. |
| 1017 | */ |
| 1018 | if (intmask & SDHCI_INT_DMA_END) |
| 1019 | writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS), |
| 1020 | host->ioaddr + SDHCI_DMA_ADDRESS); |
| 1021 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 1022 | if (intmask & SDHCI_INT_DATA_END) { |
| 1023 | if (host->cmd) { |
| 1024 | /* |
| 1025 | * Data managed to finish before the |
| 1026 | * command completed. Make sure we do |
| 1027 | * things in the proper order. |
| 1028 | */ |
| 1029 | host->data_early = 1; |
| 1030 | } else { |
| 1031 | sdhci_finish_data(host); |
| 1032 | } |
| 1033 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1037 | static irqreturn_t sdhci_irq(int irq, void *dev_id) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1038 | { |
| 1039 | irqreturn_t result; |
| 1040 | struct sdhci_host* host = dev_id; |
| 1041 | u32 intmask; |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1042 | int cardint = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1043 | |
| 1044 | spin_lock(&host->lock); |
| 1045 | |
| 1046 | intmask = readl(host->ioaddr + SDHCI_INT_STATUS); |
| 1047 | |
Mark Lord | 62df67a5 | 2007-03-06 13:30:13 +0100 | [diff] [blame] | 1048 | if (!intmask || intmask == 0xffffffff) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1049 | result = IRQ_NONE; |
| 1050 | goto out; |
| 1051 | } |
| 1052 | |
| 1053 | DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask); |
| 1054 | |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1055 | if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { |
| 1056 | writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE), |
| 1057 | host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1058 | tasklet_schedule(&host->card_tasklet); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1062 | |
| 1063 | if (intmask & SDHCI_INT_CMD_MASK) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1064 | writel(intmask & SDHCI_INT_CMD_MASK, |
| 1065 | host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1066 | sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | if (intmask & SDHCI_INT_DATA_MASK) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1070 | writel(intmask & SDHCI_INT_DATA_MASK, |
| 1071 | host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1072 | sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); |
| 1076 | |
Pierre Ossman | 964f9ce | 2007-07-20 18:20:36 +0200 | [diff] [blame] | 1077 | intmask &= ~SDHCI_INT_ERROR; |
| 1078 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1079 | if (intmask & SDHCI_INT_BUS_POWER) { |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1080 | printk(KERN_ERR "%s: Card is consuming too much power!\n", |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1081 | mmc_hostname(host->mmc)); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1082 | writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Rolf Eike Beer | 9d26a5d | 2007-06-26 13:31:16 +0200 | [diff] [blame] | 1085 | intmask &= ~SDHCI_INT_BUS_POWER; |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1086 | |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1087 | if (intmask & SDHCI_INT_CARD_INT) |
| 1088 | cardint = 1; |
| 1089 | |
| 1090 | intmask &= ~SDHCI_INT_CARD_INT; |
| 1091 | |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1092 | if (intmask) { |
Pierre Ossman | acf1da4 | 2007-02-09 08:29:19 +0100 | [diff] [blame] | 1093 | printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n", |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1094 | mmc_hostname(host->mmc), intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1095 | sdhci_dumpregs(host); |
| 1096 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1097 | writel(intmask, host->ioaddr + SDHCI_INT_STATUS); |
Pierre Ossman | 3192a28 | 2006-06-30 02:22:26 -0700 | [diff] [blame] | 1098 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1099 | |
| 1100 | result = IRQ_HANDLED; |
| 1101 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1102 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1103 | out: |
| 1104 | spin_unlock(&host->lock); |
| 1105 | |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1106 | /* |
| 1107 | * We have to delay this as it calls back into the driver. |
| 1108 | */ |
| 1109 | if (cardint) |
| 1110 | mmc_signal_sdio_irq(host->mmc); |
| 1111 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1112 | return result; |
| 1113 | } |
| 1114 | |
| 1115 | /*****************************************************************************\ |
| 1116 | * * |
| 1117 | * Suspend/resume * |
| 1118 | * * |
| 1119 | \*****************************************************************************/ |
| 1120 | |
| 1121 | #ifdef CONFIG_PM |
| 1122 | |
| 1123 | static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state) |
| 1124 | { |
| 1125 | struct sdhci_chip *chip; |
| 1126 | int i, ret; |
| 1127 | |
| 1128 | chip = pci_get_drvdata(pdev); |
| 1129 | if (!chip) |
| 1130 | return 0; |
| 1131 | |
| 1132 | DBG("Suspending...\n"); |
| 1133 | |
| 1134 | for (i = 0;i < chip->num_slots;i++) { |
| 1135 | if (!chip->hosts[i]) |
| 1136 | continue; |
| 1137 | ret = mmc_suspend_host(chip->hosts[i]->mmc, state); |
| 1138 | if (ret) { |
| 1139 | for (i--;i >= 0;i--) |
| 1140 | mmc_resume_host(chip->hosts[i]->mmc); |
| 1141 | return ret; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | pci_save_state(pdev); |
| 1146 | pci_enable_wake(pdev, pci_choose_state(pdev, state), 0); |
Pierre Ossman | a715dfc | 2007-03-06 13:38:49 +0100 | [diff] [blame] | 1147 | |
| 1148 | for (i = 0;i < chip->num_slots;i++) { |
| 1149 | if (!chip->hosts[i]) |
| 1150 | continue; |
| 1151 | free_irq(chip->hosts[i]->irq, chip->hosts[i]); |
| 1152 | } |
| 1153 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1154 | pci_disable_device(pdev); |
| 1155 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); |
| 1156 | |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | static int sdhci_resume (struct pci_dev *pdev) |
| 1161 | { |
| 1162 | struct sdhci_chip *chip; |
| 1163 | int i, ret; |
| 1164 | |
| 1165 | chip = pci_get_drvdata(pdev); |
| 1166 | if (!chip) |
| 1167 | return 0; |
| 1168 | |
| 1169 | DBG("Resuming...\n"); |
| 1170 | |
| 1171 | pci_set_power_state(pdev, PCI_D0); |
| 1172 | pci_restore_state(pdev); |
Pierre Ossman | df1c4b7 | 2007-01-30 07:55:15 +0100 | [diff] [blame] | 1173 | ret = pci_enable_device(pdev); |
| 1174 | if (ret) |
| 1175 | return ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1176 | |
| 1177 | for (i = 0;i < chip->num_slots;i++) { |
| 1178 | if (!chip->hosts[i]) |
| 1179 | continue; |
| 1180 | if (chip->hosts[i]->flags & SDHCI_USE_DMA) |
| 1181 | pci_set_master(pdev); |
Pierre Ossman | a715dfc | 2007-03-06 13:38:49 +0100 | [diff] [blame] | 1182 | ret = request_irq(chip->hosts[i]->irq, sdhci_irq, |
| 1183 | IRQF_SHARED, chip->hosts[i]->slot_descr, |
| 1184 | chip->hosts[i]); |
| 1185 | if (ret) |
| 1186 | return ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1187 | sdhci_init(chip->hosts[i]); |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1188 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1189 | ret = mmc_resume_host(chip->hosts[i]->mmc); |
| 1190 | if (ret) |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
| 1197 | #else /* CONFIG_PM */ |
| 1198 | |
| 1199 | #define sdhci_suspend NULL |
| 1200 | #define sdhci_resume NULL |
| 1201 | |
| 1202 | #endif /* CONFIG_PM */ |
| 1203 | |
| 1204 | /*****************************************************************************\ |
| 1205 | * * |
| 1206 | * Device probing/removal * |
| 1207 | * * |
| 1208 | \*****************************************************************************/ |
| 1209 | |
| 1210 | static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot) |
| 1211 | { |
| 1212 | int ret; |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 1213 | unsigned int version; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1214 | struct sdhci_chip *chip; |
| 1215 | struct mmc_host *mmc; |
| 1216 | struct sdhci_host *host; |
| 1217 | |
| 1218 | u8 first_bar; |
| 1219 | unsigned int caps; |
| 1220 | |
| 1221 | chip = pci_get_drvdata(pdev); |
| 1222 | BUG_ON(!chip); |
| 1223 | |
| 1224 | ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar); |
| 1225 | if (ret) |
| 1226 | return ret; |
| 1227 | |
| 1228 | first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK; |
| 1229 | |
| 1230 | if (first_bar > 5) { |
| 1231 | printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n"); |
| 1232 | return -ENODEV; |
| 1233 | } |
| 1234 | |
| 1235 | if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) { |
| 1236 | printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n"); |
| 1237 | return -ENODEV; |
| 1238 | } |
| 1239 | |
| 1240 | if (pci_resource_len(pdev, first_bar + slot) != 0x100) { |
Pierre Ossman | a98087c | 2006-12-07 19:17:20 +0100 | [diff] [blame] | 1241 | printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. " |
| 1242 | "You may experience problems.\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1245 | if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) { |
| 1246 | printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n"); |
| 1247 | return -ENODEV; |
| 1248 | } |
| 1249 | |
| 1250 | if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) { |
| 1251 | printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n"); |
| 1252 | return -ENODEV; |
| 1253 | } |
| 1254 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1255 | mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev); |
| 1256 | if (!mmc) |
| 1257 | return -ENOMEM; |
| 1258 | |
| 1259 | host = mmc_priv(mmc); |
| 1260 | host->mmc = mmc; |
| 1261 | |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 1262 | host->chip = chip; |
| 1263 | chip->hosts[slot] = host; |
| 1264 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1265 | host->bar = first_bar + slot; |
| 1266 | |
| 1267 | host->addr = pci_resource_start(pdev, host->bar); |
| 1268 | host->irq = pdev->irq; |
| 1269 | |
| 1270 | DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq); |
| 1271 | |
| 1272 | snprintf(host->slot_descr, 20, "sdhci:slot%d", slot); |
| 1273 | |
| 1274 | ret = pci_request_region(pdev, host->bar, host->slot_descr); |
| 1275 | if (ret) |
| 1276 | goto free; |
| 1277 | |
| 1278 | host->ioaddr = ioremap_nocache(host->addr, |
| 1279 | pci_resource_len(pdev, host->bar)); |
| 1280 | if (!host->ioaddr) { |
| 1281 | ret = -ENOMEM; |
| 1282 | goto release; |
| 1283 | } |
| 1284 | |
Pierre Ossman | d96649e | 2006-06-30 02:22:30 -0700 | [diff] [blame] | 1285 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 1286 | |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 1287 | version = readw(host->ioaddr + SDHCI_HOST_VERSION); |
| 1288 | version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT; |
| 1289 | if (version != 0) { |
| 1290 | printk(KERN_ERR "%s: Unknown controller version (%d). " |
Pierre Ossman | 8b1b218 | 2006-07-11 21:07:10 +0200 | [diff] [blame] | 1291 | "You may experience problems.\n", host->slot_descr, |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 1292 | version); |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1295 | caps = readl(host->ioaddr + SDHCI_CAPABILITIES); |
| 1296 | |
Pierre Ossman | d6f8dee | 2007-09-30 12:47:05 +0200 | [diff] [blame] | 1297 | if (chip->quirks & SDHCI_QUIRK_FORCE_DMA) |
Pierre Ossman | 9860807 | 2006-06-30 02:22:34 -0700 | [diff] [blame] | 1298 | host->flags |= SDHCI_USE_DMA; |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1299 | else if (!(caps & SDHCI_CAN_DO_DMA)) |
| 1300 | DBG("Controller doesn't have DMA capability\n"); |
| 1301 | else |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1302 | host->flags |= SDHCI_USE_DMA; |
| 1303 | |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 1304 | if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) && |
| 1305 | (host->flags & SDHCI_USE_DMA)) { |
| 1306 | DBG("Disabling DMA as it is marked broken"); |
| 1307 | host->flags &= ~SDHCI_USE_DMA; |
| 1308 | } |
| 1309 | |
Feng Tang | 56e71ef | 2007-09-29 14:15:05 +0800 | [diff] [blame] | 1310 | if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) && |
| 1311 | (host->flags & SDHCI_USE_DMA)) { |
| 1312 | printk(KERN_WARNING "%s: Will use DMA " |
| 1313 | "mode even though HW doesn't fully " |
| 1314 | "claim to support it.\n", host->slot_descr); |
| 1315 | } |
| 1316 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1317 | if (host->flags & SDHCI_USE_DMA) { |
| 1318 | if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) { |
| 1319 | printk(KERN_WARNING "%s: No suitable DMA available. " |
| 1320 | "Falling back to PIO.\n", host->slot_descr); |
| 1321 | host->flags &= ~SDHCI_USE_DMA; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | if (host->flags & SDHCI_USE_DMA) |
| 1326 | pci_set_master(pdev); |
| 1327 | else /* XXX: Hack to get MMC layer to avoid highmem */ |
| 1328 | pdev->dma_mask = 0; |
| 1329 | |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1330 | host->max_clk = |
| 1331 | (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; |
| 1332 | if (host->max_clk == 0) { |
| 1333 | printk(KERN_ERR "%s: Hardware doesn't specify base clock " |
| 1334 | "frequency.\n", host->slot_descr); |
| 1335 | ret = -ENODEV; |
| 1336 | goto unmap; |
| 1337 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1338 | host->max_clk *= 1000000; |
| 1339 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1340 | host->timeout_clk = |
| 1341 | (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT; |
| 1342 | if (host->timeout_clk == 0) { |
| 1343 | printk(KERN_ERR "%s: Hardware doesn't specify timeout clock " |
| 1344 | "frequency.\n", host->slot_descr); |
| 1345 | ret = -ENODEV; |
| 1346 | goto unmap; |
| 1347 | } |
| 1348 | if (caps & SDHCI_TIMEOUT_CLK_UNIT) |
| 1349 | host->timeout_clk *= 1000; |
| 1350 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1351 | /* |
| 1352 | * Set host parameters. |
| 1353 | */ |
| 1354 | mmc->ops = &sdhci_ops; |
| 1355 | mmc->f_min = host->max_clk / 256; |
| 1356 | mmc->f_max = host->max_clk; |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 1357 | mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1358 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1359 | if (caps & SDHCI_CAN_DO_HISPD) |
| 1360 | mmc->caps |= MMC_CAP_SD_HIGHSPEED; |
| 1361 | |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1362 | mmc->ocr_avail = 0; |
| 1363 | if (caps & SDHCI_CAN_VDD_330) |
| 1364 | mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34; |
Pierre Ossman | c70840e | 2007-02-02 22:41:41 +0100 | [diff] [blame] | 1365 | if (caps & SDHCI_CAN_VDD_300) |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1366 | mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31; |
Pierre Ossman | c70840e | 2007-02-02 22:41:41 +0100 | [diff] [blame] | 1367 | if (caps & SDHCI_CAN_VDD_180) |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 1368 | mmc->ocr_avail |= MMC_VDD_165_195; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1369 | |
| 1370 | if (mmc->ocr_avail == 0) { |
| 1371 | printk(KERN_ERR "%s: Hardware doesn't report any " |
| 1372 | "support voltages.\n", host->slot_descr); |
| 1373 | ret = -ENODEV; |
| 1374 | goto unmap; |
| 1375 | } |
| 1376 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1377 | spin_lock_init(&host->lock); |
| 1378 | |
| 1379 | /* |
| 1380 | * Maximum number of segments. Hardware cannot do scatter lists. |
| 1381 | */ |
| 1382 | if (host->flags & SDHCI_USE_DMA) |
| 1383 | mmc->max_hw_segs = 1; |
| 1384 | else |
| 1385 | mmc->max_hw_segs = 16; |
| 1386 | mmc->max_phys_segs = 16; |
| 1387 | |
| 1388 | /* |
Pierre Ossman | bab7696 | 2006-07-02 16:51:35 +0100 | [diff] [blame] | 1389 | * Maximum number of sectors in one transfer. Limited by DMA boundary |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1390 | * size (512KiB). |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1391 | */ |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1392 | mmc->max_req_size = 524288; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1393 | |
| 1394 | /* |
| 1395 | * Maximum segment size. Could be one segment with the maximum number |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1396 | * of bytes. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1397 | */ |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1398 | mmc->max_seg_size = mmc->max_req_size; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1399 | |
| 1400 | /* |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 1401 | * Maximum block size. This varies from controller to controller and |
| 1402 | * is specified in the capabilities register. |
| 1403 | */ |
| 1404 | mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT; |
| 1405 | if (mmc->max_blk_size >= 3) { |
David Vrabel | 03f8590 | 2007-08-10 13:25:03 +0100 | [diff] [blame] | 1406 | printk(KERN_WARNING "%s: Invalid maximum block size, assuming 512\n", |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 1407 | host->slot_descr); |
David Vrabel | 03f8590 | 2007-08-10 13:25:03 +0100 | [diff] [blame] | 1408 | mmc->max_blk_size = 512; |
| 1409 | } else |
| 1410 | mmc->max_blk_size = 512 << mmc->max_blk_size; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 1411 | |
| 1412 | /* |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1413 | * Maximum block count. |
| 1414 | */ |
| 1415 | mmc->max_blk_count = 65535; |
| 1416 | |
| 1417 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1418 | * Init tasklets. |
| 1419 | */ |
| 1420 | tasklet_init(&host->card_tasklet, |
| 1421 | sdhci_tasklet_card, (unsigned long)host); |
| 1422 | tasklet_init(&host->finish_tasklet, |
| 1423 | sdhci_tasklet_finish, (unsigned long)host); |
| 1424 | |
Al Viro | e4cad1b | 2006-10-10 22:47:07 +0100 | [diff] [blame] | 1425 | setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1426 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 1427 | ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED, |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1428 | host->slot_descr, host); |
| 1429 | if (ret) |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1430 | goto untasklet; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1431 | |
| 1432 | sdhci_init(host); |
| 1433 | |
| 1434 | #ifdef CONFIG_MMC_DEBUG |
| 1435 | sdhci_dumpregs(host); |
| 1436 | #endif |
| 1437 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1438 | mmiowb(); |
| 1439 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1440 | mmc_add_host(mmc); |
| 1441 | |
| 1442 | printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc), |
| 1443 | host->addr, host->irq, |
| 1444 | (host->flags & SDHCI_USE_DMA)?"DMA":"PIO"); |
| 1445 | |
| 1446 | return 0; |
| 1447 | |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1448 | untasklet: |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1449 | tasklet_kill(&host->card_tasklet); |
| 1450 | tasklet_kill(&host->finish_tasklet); |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1451 | unmap: |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1452 | iounmap(host->ioaddr); |
| 1453 | release: |
| 1454 | pci_release_region(pdev, host->bar); |
| 1455 | free: |
| 1456 | mmc_free_host(mmc); |
| 1457 | |
| 1458 | return ret; |
| 1459 | } |
| 1460 | |
| 1461 | static void sdhci_remove_slot(struct pci_dev *pdev, int slot) |
| 1462 | { |
| 1463 | struct sdhci_chip *chip; |
| 1464 | struct mmc_host *mmc; |
| 1465 | struct sdhci_host *host; |
| 1466 | |
| 1467 | chip = pci_get_drvdata(pdev); |
| 1468 | host = chip->hosts[slot]; |
| 1469 | mmc = host->mmc; |
| 1470 | |
| 1471 | chip->hosts[slot] = NULL; |
| 1472 | |
| 1473 | mmc_remove_host(mmc); |
| 1474 | |
| 1475 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 1476 | |
| 1477 | free_irq(host->irq, host); |
| 1478 | |
| 1479 | del_timer_sync(&host->timer); |
| 1480 | |
| 1481 | tasklet_kill(&host->card_tasklet); |
| 1482 | tasklet_kill(&host->finish_tasklet); |
| 1483 | |
| 1484 | iounmap(host->ioaddr); |
| 1485 | |
| 1486 | pci_release_region(pdev, host->bar); |
| 1487 | |
| 1488 | mmc_free_host(mmc); |
| 1489 | } |
| 1490 | |
| 1491 | static int __devinit sdhci_probe(struct pci_dev *pdev, |
| 1492 | const struct pci_device_id *ent) |
| 1493 | { |
| 1494 | int ret, i; |
Pierre Ossman | 51f82bc | 2006-06-30 02:22:22 -0700 | [diff] [blame] | 1495 | u8 slots, rev; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1496 | struct sdhci_chip *chip; |
| 1497 | |
| 1498 | BUG_ON(pdev == NULL); |
| 1499 | BUG_ON(ent == NULL); |
| 1500 | |
Pierre Ossman | 51f82bc | 2006-06-30 02:22:22 -0700 | [diff] [blame] | 1501 | pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev); |
| 1502 | |
| 1503 | printk(KERN_INFO DRIVER_NAME |
| 1504 | ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n", |
| 1505 | pci_name(pdev), (int)pdev->vendor, (int)pdev->device, |
| 1506 | (int)rev); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1507 | |
| 1508 | ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots); |
| 1509 | if (ret) |
| 1510 | return ret; |
| 1511 | |
| 1512 | slots = PCI_SLOT_INFO_SLOTS(slots) + 1; |
| 1513 | DBG("found %d slot(s)\n", slots); |
| 1514 | if (slots == 0) |
| 1515 | return -ENODEV; |
| 1516 | |
| 1517 | ret = pci_enable_device(pdev); |
| 1518 | if (ret) |
| 1519 | return ret; |
| 1520 | |
| 1521 | chip = kzalloc(sizeof(struct sdhci_chip) + |
| 1522 | sizeof(struct sdhci_host*) * slots, GFP_KERNEL); |
| 1523 | if (!chip) { |
| 1524 | ret = -ENOMEM; |
| 1525 | goto err; |
| 1526 | } |
| 1527 | |
| 1528 | chip->pdev = pdev; |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1529 | chip->quirks = ent->driver_data; |
| 1530 | |
| 1531 | if (debug_quirks) |
| 1532 | chip->quirks = debug_quirks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1533 | |
| 1534 | chip->num_slots = slots; |
| 1535 | pci_set_drvdata(pdev, chip); |
| 1536 | |
| 1537 | for (i = 0;i < slots;i++) { |
| 1538 | ret = sdhci_probe_slot(pdev, i); |
| 1539 | if (ret) { |
| 1540 | for (i--;i >= 0;i--) |
| 1541 | sdhci_remove_slot(pdev, i); |
| 1542 | goto free; |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | return 0; |
| 1547 | |
| 1548 | free: |
| 1549 | pci_set_drvdata(pdev, NULL); |
| 1550 | kfree(chip); |
| 1551 | |
| 1552 | err: |
| 1553 | pci_disable_device(pdev); |
| 1554 | return ret; |
| 1555 | } |
| 1556 | |
| 1557 | static void __devexit sdhci_remove(struct pci_dev *pdev) |
| 1558 | { |
| 1559 | int i; |
| 1560 | struct sdhci_chip *chip; |
| 1561 | |
| 1562 | chip = pci_get_drvdata(pdev); |
| 1563 | |
| 1564 | if (chip) { |
| 1565 | for (i = 0;i < chip->num_slots;i++) |
| 1566 | sdhci_remove_slot(pdev, i); |
| 1567 | |
| 1568 | pci_set_drvdata(pdev, NULL); |
| 1569 | |
| 1570 | kfree(chip); |
| 1571 | } |
| 1572 | |
| 1573 | pci_disable_device(pdev); |
| 1574 | } |
| 1575 | |
| 1576 | static struct pci_driver sdhci_driver = { |
| 1577 | .name = DRIVER_NAME, |
| 1578 | .id_table = pci_ids, |
| 1579 | .probe = sdhci_probe, |
| 1580 | .remove = __devexit_p(sdhci_remove), |
| 1581 | .suspend = sdhci_suspend, |
| 1582 | .resume = sdhci_resume, |
| 1583 | }; |
| 1584 | |
| 1585 | /*****************************************************************************\ |
| 1586 | * * |
| 1587 | * Driver init/exit * |
| 1588 | * * |
| 1589 | \*****************************************************************************/ |
| 1590 | |
| 1591 | static int __init sdhci_drv_init(void) |
| 1592 | { |
| 1593 | printk(KERN_INFO DRIVER_NAME |
Pierre Ossman | 52fbf9c | 2007-02-09 08:23:41 +0100 | [diff] [blame] | 1594 | ": Secure Digital Host Controller Interface driver\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1595 | printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n"); |
| 1596 | |
| 1597 | return pci_register_driver(&sdhci_driver); |
| 1598 | } |
| 1599 | |
| 1600 | static void __exit sdhci_drv_exit(void) |
| 1601 | { |
| 1602 | DBG("Exiting\n"); |
| 1603 | |
| 1604 | pci_unregister_driver(&sdhci_driver); |
| 1605 | } |
| 1606 | |
| 1607 | module_init(sdhci_drv_init); |
| 1608 | module_exit(sdhci_drv_exit); |
| 1609 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1610 | module_param(debug_quirks, uint, 0444); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1611 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1612 | MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>"); |
| 1613 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1614 | MODULE_LICENSE("GPL"); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1615 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 1616 | MODULE_PARM_DESC(debug_quirks, "Force certain quirks."); |