Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/sdhci.c - Secure Digital Host Controller Interface driver |
| 3 | * |
| 4 | * Copyright (C) 2005-2006 Pierre Ossman, All Rights Reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Note that PIO transfer is rather crappy atm. The buffer full/empty |
| 13 | * interrupts aren't reliable so we currently transfer the entire buffer |
| 14 | * directly. Patches to solve the problem are welcome. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/highmem.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/dma-mapping.h> |
| 21 | |
| 22 | #include <linux/mmc/host.h> |
| 23 | #include <linux/mmc/protocol.h> |
| 24 | |
| 25 | #include <asm/scatterlist.h> |
| 26 | |
| 27 | #include "sdhci.h" |
| 28 | |
| 29 | #define DRIVER_NAME "sdhci" |
| 30 | #define DRIVER_VERSION "0.11" |
| 31 | |
| 32 | #define BUGMAIL "<sdhci-devel@list.drzeus.cx>" |
| 33 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 34 | #define DBG(f, x...) \ |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 35 | pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 36 | |
| 37 | static const struct pci_device_id pci_ids[] __devinitdata = { |
| 38 | /* handle any SD host controller */ |
| 39 | {PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)}, |
| 40 | { /* end: all zeroes */ }, |
| 41 | }; |
| 42 | |
| 43 | MODULE_DEVICE_TABLE(pci, pci_ids); |
| 44 | |
| 45 | static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *); |
| 46 | static void sdhci_finish_data(struct sdhci_host *); |
| 47 | |
| 48 | static void sdhci_send_command(struct sdhci_host *, struct mmc_command *); |
| 49 | static void sdhci_finish_command(struct sdhci_host *); |
| 50 | |
| 51 | static void sdhci_dumpregs(struct sdhci_host *host) |
| 52 | { |
| 53 | printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n"); |
| 54 | |
| 55 | printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n", |
| 56 | readl(host->ioaddr + SDHCI_DMA_ADDRESS), |
| 57 | readw(host->ioaddr + SDHCI_HOST_VERSION)); |
| 58 | printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n", |
| 59 | readw(host->ioaddr + SDHCI_BLOCK_SIZE), |
| 60 | readw(host->ioaddr + SDHCI_BLOCK_COUNT)); |
| 61 | printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n", |
| 62 | readl(host->ioaddr + SDHCI_ARGUMENT), |
| 63 | readw(host->ioaddr + SDHCI_TRANSFER_MODE)); |
| 64 | printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n", |
| 65 | readl(host->ioaddr + SDHCI_PRESENT_STATE), |
| 66 | readb(host->ioaddr + SDHCI_HOST_CONTROL)); |
| 67 | printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n", |
| 68 | readb(host->ioaddr + SDHCI_POWER_CONTROL), |
| 69 | readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL)); |
| 70 | printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n", |
| 71 | readb(host->ioaddr + SDHCI_WALK_UP_CONTROL), |
| 72 | readw(host->ioaddr + SDHCI_CLOCK_CONTROL)); |
| 73 | printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n", |
| 74 | readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL), |
| 75 | readl(host->ioaddr + SDHCI_INT_STATUS)); |
| 76 | printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n", |
| 77 | readl(host->ioaddr + SDHCI_INT_ENABLE), |
| 78 | readl(host->ioaddr + SDHCI_SIGNAL_ENABLE)); |
| 79 | printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n", |
| 80 | readw(host->ioaddr + SDHCI_ACMD12_ERR), |
| 81 | readw(host->ioaddr + SDHCI_SLOT_INT_STATUS)); |
| 82 | printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n", |
| 83 | readl(host->ioaddr + SDHCI_CAPABILITIES), |
| 84 | readl(host->ioaddr + SDHCI_MAX_CURRENT)); |
| 85 | |
| 86 | printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n"); |
| 87 | } |
| 88 | |
| 89 | /*****************************************************************************\ |
| 90 | * * |
| 91 | * Low level functions * |
| 92 | * * |
| 93 | \*****************************************************************************/ |
| 94 | |
| 95 | static void sdhci_reset(struct sdhci_host *host, u8 mask) |
| 96 | { |
| 97 | writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET); |
| 98 | |
| 99 | if (mask & SDHCI_RESET_ALL) { |
| 100 | host->clock = 0; |
| 101 | |
| 102 | mdelay(50); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void sdhci_init(struct sdhci_host *host) |
| 107 | { |
| 108 | u32 intmask; |
| 109 | |
| 110 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 111 | |
| 112 | intmask = ~(SDHCI_INT_CARD_INT | SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL); |
| 113 | |
| 114 | writel(intmask, host->ioaddr + SDHCI_INT_ENABLE); |
| 115 | writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 116 | |
| 117 | /* This is unknown magic. */ |
| 118 | writeb(0xE, host->ioaddr + SDHCI_TIMEOUT_CONTROL); |
| 119 | } |
| 120 | |
| 121 | static void sdhci_activate_led(struct sdhci_host *host) |
| 122 | { |
| 123 | u8 ctrl; |
| 124 | |
| 125 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 126 | ctrl |= SDHCI_CTRL_LED; |
| 127 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 128 | } |
| 129 | |
| 130 | static void sdhci_deactivate_led(struct sdhci_host *host) |
| 131 | { |
| 132 | u8 ctrl; |
| 133 | |
| 134 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 135 | ctrl &= ~SDHCI_CTRL_LED; |
| 136 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 137 | } |
| 138 | |
| 139 | /*****************************************************************************\ |
| 140 | * * |
| 141 | * Core functions * |
| 142 | * * |
| 143 | \*****************************************************************************/ |
| 144 | |
| 145 | static inline char* sdhci_kmap_sg(struct sdhci_host* host) |
| 146 | { |
| 147 | host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ); |
| 148 | return host->mapped_sg + host->cur_sg->offset; |
| 149 | } |
| 150 | |
| 151 | static inline void sdhci_kunmap_sg(struct sdhci_host* host) |
| 152 | { |
| 153 | kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ); |
| 154 | } |
| 155 | |
| 156 | static inline int sdhci_next_sg(struct sdhci_host* host) |
| 157 | { |
| 158 | /* |
| 159 | * Skip to next SG entry. |
| 160 | */ |
| 161 | host->cur_sg++; |
| 162 | host->num_sg--; |
| 163 | |
| 164 | /* |
| 165 | * Any entries left? |
| 166 | */ |
| 167 | if (host->num_sg > 0) { |
| 168 | host->offset = 0; |
| 169 | host->remain = host->cur_sg->length; |
| 170 | } |
| 171 | |
| 172 | return host->num_sg; |
| 173 | } |
| 174 | |
| 175 | static void sdhci_transfer_pio(struct sdhci_host *host) |
| 176 | { |
| 177 | char *buffer; |
| 178 | u32 mask; |
| 179 | int bytes, size; |
| 180 | unsigned long max_jiffies; |
| 181 | |
| 182 | BUG_ON(!host->data); |
| 183 | |
| 184 | if (host->num_sg == 0) |
| 185 | return; |
| 186 | |
| 187 | bytes = 0; |
| 188 | if (host->data->flags & MMC_DATA_READ) |
| 189 | mask = SDHCI_DATA_AVAILABLE; |
| 190 | else |
| 191 | mask = SDHCI_SPACE_AVAILABLE; |
| 192 | |
| 193 | buffer = sdhci_kmap_sg(host) + host->offset; |
| 194 | |
| 195 | /* Transfer shouldn't take more than 5 s */ |
| 196 | max_jiffies = jiffies + HZ * 5; |
| 197 | |
| 198 | while (host->size > 0) { |
| 199 | if (time_after(jiffies, max_jiffies)) { |
| 200 | printk(KERN_ERR "%s: PIO transfer stalled. " |
| 201 | "Please report this to " |
| 202 | BUGMAIL ".\n", mmc_hostname(host->mmc)); |
| 203 | sdhci_dumpregs(host); |
| 204 | |
| 205 | sdhci_kunmap_sg(host); |
| 206 | |
| 207 | host->data->error = MMC_ERR_FAILED; |
| 208 | sdhci_finish_data(host); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask)) |
| 213 | continue; |
| 214 | |
| 215 | size = min(host->size, host->remain); |
| 216 | |
| 217 | if (size >= 4) { |
| 218 | if (host->data->flags & MMC_DATA_READ) |
| 219 | *(u32*)buffer = readl(host->ioaddr + SDHCI_BUFFER); |
| 220 | else |
| 221 | writel(*(u32*)buffer, host->ioaddr + SDHCI_BUFFER); |
| 222 | size = 4; |
| 223 | } else if (size >= 2) { |
| 224 | if (host->data->flags & MMC_DATA_READ) |
| 225 | *(u16*)buffer = readw(host->ioaddr + SDHCI_BUFFER); |
| 226 | else |
| 227 | writew(*(u16*)buffer, host->ioaddr + SDHCI_BUFFER); |
| 228 | size = 2; |
| 229 | } else { |
| 230 | if (host->data->flags & MMC_DATA_READ) |
| 231 | *(u8*)buffer = readb(host->ioaddr + SDHCI_BUFFER); |
| 232 | else |
| 233 | writeb(*(u8*)buffer, host->ioaddr + SDHCI_BUFFER); |
| 234 | size = 1; |
| 235 | } |
| 236 | |
| 237 | buffer += size; |
| 238 | host->offset += size; |
| 239 | host->remain -= size; |
| 240 | |
| 241 | bytes += size; |
| 242 | host->size -= size; |
| 243 | |
| 244 | if (host->remain == 0) { |
| 245 | sdhci_kunmap_sg(host); |
| 246 | if (sdhci_next_sg(host) == 0) { |
| 247 | DBG("PIO transfer: %d bytes\n", bytes); |
| 248 | return; |
| 249 | } |
| 250 | buffer = sdhci_kmap_sg(host); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | sdhci_kunmap_sg(host); |
| 255 | |
| 256 | DBG("PIO transfer: %d bytes\n", bytes); |
| 257 | } |
| 258 | |
| 259 | static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data) |
| 260 | { |
| 261 | u16 mode; |
| 262 | |
| 263 | WARN_ON(host->data); |
| 264 | |
| 265 | if (data == NULL) { |
| 266 | writew(0, host->ioaddr + SDHCI_TRANSFER_MODE); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | DBG("blksz %04x blks %04x flags %08x\n", |
Russell King | a3fd4a1 | 2006-06-04 17:51:15 +0100 | [diff] [blame] | 271 | data->blksz, data->blocks, data->flags); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 272 | DBG("tsac %d ms nsac %d clk\n", |
| 273 | data->timeout_ns / 1000000, data->timeout_clks); |
| 274 | |
| 275 | mode = SDHCI_TRNS_BLK_CNT_EN; |
| 276 | if (data->blocks > 1) |
| 277 | mode |= SDHCI_TRNS_MULTI; |
| 278 | if (data->flags & MMC_DATA_READ) |
| 279 | mode |= SDHCI_TRNS_READ; |
| 280 | if (host->flags & SDHCI_USE_DMA) |
| 281 | mode |= SDHCI_TRNS_DMA; |
| 282 | |
| 283 | writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE); |
| 284 | |
Russell King | a3fd4a1 | 2006-06-04 17:51:15 +0100 | [diff] [blame] | 285 | writew(data->blksz, host->ioaddr + SDHCI_BLOCK_SIZE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 286 | writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT); |
| 287 | |
| 288 | if (host->flags & SDHCI_USE_DMA) { |
| 289 | int count; |
| 290 | |
| 291 | count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len, |
| 292 | (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE); |
| 293 | BUG_ON(count != 1); |
| 294 | |
| 295 | writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS); |
| 296 | } else { |
Russell King | a3fd4a1 | 2006-06-04 17:51:15 +0100 | [diff] [blame] | 297 | host->size = data->blksz * data->blocks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 298 | |
| 299 | host->cur_sg = data->sg; |
| 300 | host->num_sg = data->sg_len; |
| 301 | |
| 302 | host->offset = 0; |
| 303 | host->remain = host->cur_sg->length; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | static void sdhci_finish_data(struct sdhci_host *host) |
| 308 | { |
| 309 | struct mmc_data *data; |
| 310 | u32 intmask; |
| 311 | u16 blocks; |
| 312 | |
| 313 | BUG_ON(!host->data); |
| 314 | |
| 315 | data = host->data; |
| 316 | host->data = NULL; |
| 317 | |
| 318 | if (host->flags & SDHCI_USE_DMA) { |
| 319 | pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len, |
| 320 | (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE); |
| 321 | } else { |
| 322 | intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 323 | intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL); |
| 324 | writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 325 | |
| 326 | intmask = readl(host->ioaddr + SDHCI_INT_ENABLE); |
| 327 | intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL); |
| 328 | writel(intmask, host->ioaddr + SDHCI_INT_ENABLE); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Controller doesn't count down when in single block mode. |
| 333 | */ |
| 334 | if ((data->blocks == 1) && (data->error == MMC_ERR_NONE)) |
| 335 | blocks = 0; |
| 336 | else |
| 337 | blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT); |
Russell King | a3fd4a1 | 2006-06-04 17:51:15 +0100 | [diff] [blame] | 338 | data->bytes_xfered = data->blksz * (data->blocks - blocks); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 339 | |
| 340 | if ((data->error == MMC_ERR_NONE) && blocks) { |
| 341 | printk(KERN_ERR "%s: Controller signalled completion even " |
| 342 | "though there were blocks left. Please report this " |
| 343 | "to " BUGMAIL ".\n", mmc_hostname(host->mmc)); |
| 344 | data->error = MMC_ERR_FAILED; |
| 345 | } |
| 346 | |
| 347 | if (host->size != 0) { |
| 348 | printk(KERN_ERR "%s: %d bytes were left untransferred. " |
| 349 | "Please report this to " BUGMAIL ".\n", |
| 350 | mmc_hostname(host->mmc), host->size); |
| 351 | data->error = MMC_ERR_FAILED; |
| 352 | } |
| 353 | |
| 354 | DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered); |
| 355 | |
| 356 | if (data->stop) { |
| 357 | /* |
| 358 | * The controller needs a reset of internal state machines |
| 359 | * upon error conditions. |
| 360 | */ |
| 361 | if (data->error != MMC_ERR_NONE) { |
| 362 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 363 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 364 | } |
| 365 | |
| 366 | sdhci_send_command(host, data->stop); |
| 367 | } else |
| 368 | tasklet_schedule(&host->finish_tasklet); |
| 369 | } |
| 370 | |
| 371 | static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd) |
| 372 | { |
| 373 | int flags; |
| 374 | u32 present; |
| 375 | unsigned long max_jiffies; |
| 376 | |
| 377 | WARN_ON(host->cmd); |
| 378 | |
| 379 | DBG("Sending cmd (%x)\n", cmd->opcode); |
| 380 | |
| 381 | /* Wait max 10 ms */ |
| 382 | max_jiffies = jiffies + (HZ + 99)/100; |
| 383 | do { |
| 384 | if (time_after(jiffies, max_jiffies)) { |
| 385 | printk(KERN_ERR "%s: Controller never released " |
| 386 | "inhibit bits. Please report this to " |
| 387 | BUGMAIL ".\n", mmc_hostname(host->mmc)); |
| 388 | sdhci_dumpregs(host); |
| 389 | cmd->error = MMC_ERR_FAILED; |
| 390 | tasklet_schedule(&host->finish_tasklet); |
| 391 | return; |
| 392 | } |
| 393 | present = readl(host->ioaddr + SDHCI_PRESENT_STATE); |
| 394 | } while (present & (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)); |
| 395 | |
| 396 | mod_timer(&host->timer, jiffies + 10 * HZ); |
| 397 | |
| 398 | host->cmd = cmd; |
| 399 | |
| 400 | sdhci_prepare_data(host, cmd->data); |
| 401 | |
| 402 | writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT); |
| 403 | |
| 404 | if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { |
| 405 | printk(KERN_ERR "%s: Unsupported response type! " |
| 406 | "Please report this to " BUGMAIL ".\n", |
| 407 | mmc_hostname(host->mmc)); |
| 408 | cmd->error = MMC_ERR_INVALID; |
| 409 | tasklet_schedule(&host->finish_tasklet); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | if (!(cmd->flags & MMC_RSP_PRESENT)) |
| 414 | flags = SDHCI_CMD_RESP_NONE; |
| 415 | else if (cmd->flags & MMC_RSP_136) |
| 416 | flags = SDHCI_CMD_RESP_LONG; |
| 417 | else if (cmd->flags & MMC_RSP_BUSY) |
| 418 | flags = SDHCI_CMD_RESP_SHORT_BUSY; |
| 419 | else |
| 420 | flags = SDHCI_CMD_RESP_SHORT; |
| 421 | |
| 422 | if (cmd->flags & MMC_RSP_CRC) |
| 423 | flags |= SDHCI_CMD_CRC; |
| 424 | if (cmd->flags & MMC_RSP_OPCODE) |
| 425 | flags |= SDHCI_CMD_INDEX; |
| 426 | if (cmd->data) |
| 427 | flags |= SDHCI_CMD_DATA; |
| 428 | |
| 429 | writel(SDHCI_MAKE_CMD(cmd->opcode, flags), |
| 430 | host->ioaddr + SDHCI_COMMAND); |
| 431 | } |
| 432 | |
| 433 | static void sdhci_finish_command(struct sdhci_host *host) |
| 434 | { |
| 435 | int i; |
| 436 | |
| 437 | BUG_ON(host->cmd == NULL); |
| 438 | |
| 439 | if (host->cmd->flags & MMC_RSP_PRESENT) { |
| 440 | if (host->cmd->flags & MMC_RSP_136) { |
| 441 | /* CRC is stripped so we need to do some shifting. */ |
| 442 | for (i = 0;i < 4;i++) { |
| 443 | host->cmd->resp[i] = readl(host->ioaddr + |
| 444 | SDHCI_RESPONSE + (3-i)*4) << 8; |
| 445 | if (i != 3) |
| 446 | host->cmd->resp[i] |= |
| 447 | readb(host->ioaddr + |
| 448 | SDHCI_RESPONSE + (3-i)*4-1); |
| 449 | } |
| 450 | } else { |
| 451 | host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | host->cmd->error = MMC_ERR_NONE; |
| 456 | |
| 457 | DBG("Ending cmd (%x)\n", host->cmd->opcode); |
| 458 | |
| 459 | if (host->cmd->data) { |
| 460 | u32 intmask; |
| 461 | |
| 462 | host->data = host->cmd->data; |
| 463 | |
| 464 | if (!(host->flags & SDHCI_USE_DMA)) { |
| 465 | /* |
| 466 | * Don't enable the interrupts until now to make sure we |
| 467 | * get stable handling of the FIFO. |
| 468 | */ |
| 469 | intmask = readl(host->ioaddr + SDHCI_INT_ENABLE); |
| 470 | intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL; |
| 471 | writel(intmask, host->ioaddr + SDHCI_INT_ENABLE); |
| 472 | |
| 473 | intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 474 | intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL; |
| 475 | writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 476 | |
| 477 | /* |
| 478 | * The buffer interrupts are to unreliable so we |
| 479 | * start the transfer immediatly. |
| 480 | */ |
| 481 | sdhci_transfer_pio(host); |
| 482 | } |
| 483 | } else |
| 484 | tasklet_schedule(&host->finish_tasklet); |
| 485 | |
| 486 | host->cmd = NULL; |
| 487 | } |
| 488 | |
| 489 | static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock) |
| 490 | { |
| 491 | int div; |
| 492 | u16 clk; |
| 493 | unsigned long max_jiffies; |
| 494 | |
| 495 | if (clock == host->clock) |
| 496 | return; |
| 497 | |
| 498 | writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 499 | |
| 500 | if (clock == 0) |
| 501 | goto out; |
| 502 | |
| 503 | for (div = 1;div < 256;div *= 2) { |
| 504 | if ((host->max_clk / div) <= clock) |
| 505 | break; |
| 506 | } |
| 507 | div >>= 1; |
| 508 | |
| 509 | clk = div << SDHCI_DIVIDER_SHIFT; |
| 510 | clk |= SDHCI_CLOCK_INT_EN; |
| 511 | writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 512 | |
| 513 | /* Wait max 10 ms */ |
| 514 | max_jiffies = jiffies + (HZ + 99)/100; |
| 515 | do { |
| 516 | if (time_after(jiffies, max_jiffies)) { |
| 517 | printk(KERN_ERR "%s: Internal clock never stabilised. " |
| 518 | "Please report this to " BUGMAIL ".\n", |
| 519 | mmc_hostname(host->mmc)); |
| 520 | sdhci_dumpregs(host); |
| 521 | return; |
| 522 | } |
| 523 | clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 524 | } while (!(clk & SDHCI_CLOCK_INT_STABLE)); |
| 525 | |
| 526 | clk |= SDHCI_CLOCK_CARD_EN; |
| 527 | writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL); |
| 528 | |
| 529 | out: |
| 530 | host->clock = clock; |
| 531 | } |
| 532 | |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame^] | 533 | static void sdhci_set_power(struct sdhci_host *host, unsigned short power) |
| 534 | { |
| 535 | u8 pwr; |
| 536 | |
| 537 | if (host->power == power) |
| 538 | return; |
| 539 | |
| 540 | writeb(0, host->ioaddr + SDHCI_POWER_CONTROL); |
| 541 | |
| 542 | if (power == (unsigned short)-1) |
| 543 | goto out; |
| 544 | |
| 545 | pwr = SDHCI_POWER_ON; |
| 546 | |
| 547 | switch (power) { |
| 548 | case MMC_VDD_170: |
| 549 | case MMC_VDD_180: |
| 550 | case MMC_VDD_190: |
| 551 | pwr |= SDHCI_POWER_180; |
| 552 | break; |
| 553 | case MMC_VDD_290: |
| 554 | case MMC_VDD_300: |
| 555 | case MMC_VDD_310: |
| 556 | pwr |= SDHCI_POWER_300; |
| 557 | break; |
| 558 | case MMC_VDD_320: |
| 559 | case MMC_VDD_330: |
| 560 | case MMC_VDD_340: |
| 561 | pwr |= SDHCI_POWER_330; |
| 562 | break; |
| 563 | default: |
| 564 | BUG(); |
| 565 | } |
| 566 | |
| 567 | writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL); |
| 568 | |
| 569 | out: |
| 570 | host->power = power; |
| 571 | } |
| 572 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 573 | /*****************************************************************************\ |
| 574 | * * |
| 575 | * MMC callbacks * |
| 576 | * * |
| 577 | \*****************************************************************************/ |
| 578 | |
| 579 | static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 580 | { |
| 581 | struct sdhci_host *host; |
| 582 | unsigned long flags; |
| 583 | |
| 584 | host = mmc_priv(mmc); |
| 585 | |
| 586 | spin_lock_irqsave(&host->lock, flags); |
| 587 | |
| 588 | WARN_ON(host->mrq != NULL); |
| 589 | |
| 590 | sdhci_activate_led(host); |
| 591 | |
| 592 | host->mrq = mrq; |
| 593 | |
| 594 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { |
| 595 | host->mrq->cmd->error = MMC_ERR_TIMEOUT; |
| 596 | tasklet_schedule(&host->finish_tasklet); |
| 597 | } else |
| 598 | sdhci_send_command(host, mrq->cmd); |
| 599 | |
| 600 | spin_unlock_irqrestore(&host->lock, flags); |
| 601 | } |
| 602 | |
| 603 | static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 604 | { |
| 605 | struct sdhci_host *host; |
| 606 | unsigned long flags; |
| 607 | u8 ctrl; |
| 608 | |
| 609 | host = mmc_priv(mmc); |
| 610 | |
| 611 | spin_lock_irqsave(&host->lock, flags); |
| 612 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 613 | /* |
| 614 | * Reset the chip on each power off. |
| 615 | * Should clear out any weird states. |
| 616 | */ |
| 617 | if (ios->power_mode == MMC_POWER_OFF) { |
| 618 | writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE); |
| 619 | spin_unlock_irqrestore(&host->lock, flags); |
| 620 | sdhci_init(host); |
| 621 | spin_lock_irqsave(&host->lock, flags); |
| 622 | } |
| 623 | |
| 624 | sdhci_set_clock(host, ios->clock); |
| 625 | |
| 626 | if (ios->power_mode == MMC_POWER_OFF) |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame^] | 627 | sdhci_set_power(host, -1); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 628 | else |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame^] | 629 | sdhci_set_power(host, ios->vdd); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 630 | |
| 631 | ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL); |
| 632 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 633 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 634 | else |
| 635 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 636 | writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL); |
| 637 | |
| 638 | spin_unlock_irqrestore(&host->lock, flags); |
| 639 | } |
| 640 | |
| 641 | static int sdhci_get_ro(struct mmc_host *mmc) |
| 642 | { |
| 643 | struct sdhci_host *host; |
| 644 | unsigned long flags; |
| 645 | int present; |
| 646 | |
| 647 | host = mmc_priv(mmc); |
| 648 | |
| 649 | spin_lock_irqsave(&host->lock, flags); |
| 650 | |
| 651 | present = readl(host->ioaddr + SDHCI_PRESENT_STATE); |
| 652 | |
| 653 | spin_unlock_irqrestore(&host->lock, flags); |
| 654 | |
| 655 | return !(present & SDHCI_WRITE_PROTECT); |
| 656 | } |
| 657 | |
| 658 | static struct mmc_host_ops sdhci_ops = { |
| 659 | .request = sdhci_request, |
| 660 | .set_ios = sdhci_set_ios, |
| 661 | .get_ro = sdhci_get_ro, |
| 662 | }; |
| 663 | |
| 664 | /*****************************************************************************\ |
| 665 | * * |
| 666 | * Tasklets * |
| 667 | * * |
| 668 | \*****************************************************************************/ |
| 669 | |
| 670 | static void sdhci_tasklet_card(unsigned long param) |
| 671 | { |
| 672 | struct sdhci_host *host; |
| 673 | unsigned long flags; |
| 674 | |
| 675 | host = (struct sdhci_host*)param; |
| 676 | |
| 677 | spin_lock_irqsave(&host->lock, flags); |
| 678 | |
| 679 | if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { |
| 680 | if (host->mrq) { |
| 681 | printk(KERN_ERR "%s: Card removed during transfer!\n", |
| 682 | mmc_hostname(host->mmc)); |
| 683 | printk(KERN_ERR "%s: Resetting controller.\n", |
| 684 | mmc_hostname(host->mmc)); |
| 685 | |
| 686 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 687 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 688 | |
| 689 | host->mrq->cmd->error = MMC_ERR_FAILED; |
| 690 | tasklet_schedule(&host->finish_tasklet); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | spin_unlock_irqrestore(&host->lock, flags); |
| 695 | |
| 696 | mmc_detect_change(host->mmc, msecs_to_jiffies(500)); |
| 697 | } |
| 698 | |
| 699 | static void sdhci_tasklet_finish(unsigned long param) |
| 700 | { |
| 701 | struct sdhci_host *host; |
| 702 | unsigned long flags; |
| 703 | struct mmc_request *mrq; |
| 704 | |
| 705 | host = (struct sdhci_host*)param; |
| 706 | |
| 707 | spin_lock_irqsave(&host->lock, flags); |
| 708 | |
| 709 | del_timer(&host->timer); |
| 710 | |
| 711 | mrq = host->mrq; |
| 712 | |
| 713 | DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode); |
| 714 | |
| 715 | /* |
| 716 | * The controller needs a reset of internal state machines |
| 717 | * upon error conditions. |
| 718 | */ |
| 719 | if ((mrq->cmd->error != MMC_ERR_NONE) || |
| 720 | (mrq->data && ((mrq->data->error != MMC_ERR_NONE) || |
| 721 | (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) { |
| 722 | sdhci_reset(host, SDHCI_RESET_CMD); |
| 723 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 724 | } |
| 725 | |
| 726 | host->mrq = NULL; |
| 727 | host->cmd = NULL; |
| 728 | host->data = NULL; |
| 729 | |
| 730 | sdhci_deactivate_led(host); |
| 731 | |
| 732 | spin_unlock_irqrestore(&host->lock, flags); |
| 733 | |
| 734 | mmc_request_done(host->mmc, mrq); |
| 735 | } |
| 736 | |
| 737 | static void sdhci_timeout_timer(unsigned long data) |
| 738 | { |
| 739 | struct sdhci_host *host; |
| 740 | unsigned long flags; |
| 741 | |
| 742 | host = (struct sdhci_host*)data; |
| 743 | |
| 744 | spin_lock_irqsave(&host->lock, flags); |
| 745 | |
| 746 | if (host->mrq) { |
| 747 | printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. " |
| 748 | "Please report this to " BUGMAIL ".\n", |
| 749 | mmc_hostname(host->mmc)); |
| 750 | sdhci_dumpregs(host); |
| 751 | |
| 752 | if (host->data) { |
| 753 | host->data->error = MMC_ERR_TIMEOUT; |
| 754 | sdhci_finish_data(host); |
| 755 | } else { |
| 756 | if (host->cmd) |
| 757 | host->cmd->error = MMC_ERR_TIMEOUT; |
| 758 | else |
| 759 | host->mrq->cmd->error = MMC_ERR_TIMEOUT; |
| 760 | |
| 761 | tasklet_schedule(&host->finish_tasklet); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | spin_unlock_irqrestore(&host->lock, flags); |
| 766 | } |
| 767 | |
| 768 | /*****************************************************************************\ |
| 769 | * * |
| 770 | * Interrupt handling * |
| 771 | * * |
| 772 | \*****************************************************************************/ |
| 773 | |
| 774 | static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask) |
| 775 | { |
| 776 | BUG_ON(intmask == 0); |
| 777 | |
| 778 | if (!host->cmd) { |
| 779 | printk(KERN_ERR "%s: Got command interrupt even though no " |
| 780 | "command operation was in progress.\n", |
| 781 | mmc_hostname(host->mmc)); |
| 782 | printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n", |
| 783 | mmc_hostname(host->mmc)); |
| 784 | sdhci_dumpregs(host); |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | if (intmask & SDHCI_INT_RESPONSE) |
| 789 | sdhci_finish_command(host); |
| 790 | else { |
| 791 | if (intmask & SDHCI_INT_TIMEOUT) |
| 792 | host->cmd->error = MMC_ERR_TIMEOUT; |
| 793 | else if (intmask & SDHCI_INT_CRC) |
| 794 | host->cmd->error = MMC_ERR_BADCRC; |
| 795 | else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) |
| 796 | host->cmd->error = MMC_ERR_FAILED; |
| 797 | else |
| 798 | host->cmd->error = MMC_ERR_INVALID; |
| 799 | |
| 800 | tasklet_schedule(&host->finish_tasklet); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | static void sdhci_data_irq(struct sdhci_host *host, u32 intmask) |
| 805 | { |
| 806 | BUG_ON(intmask == 0); |
| 807 | |
| 808 | if (!host->data) { |
| 809 | /* |
| 810 | * A data end interrupt is sent together with the response |
| 811 | * for the stop command. |
| 812 | */ |
| 813 | if (intmask & SDHCI_INT_DATA_END) |
| 814 | return; |
| 815 | |
| 816 | printk(KERN_ERR "%s: Got data interrupt even though no " |
| 817 | "data operation was in progress.\n", |
| 818 | mmc_hostname(host->mmc)); |
| 819 | printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n", |
| 820 | mmc_hostname(host->mmc)); |
| 821 | sdhci_dumpregs(host); |
| 822 | |
| 823 | return; |
| 824 | } |
| 825 | |
| 826 | if (intmask & SDHCI_INT_DATA_TIMEOUT) |
| 827 | host->data->error = MMC_ERR_TIMEOUT; |
| 828 | else if (intmask & SDHCI_INT_DATA_CRC) |
| 829 | host->data->error = MMC_ERR_BADCRC; |
| 830 | else if (intmask & SDHCI_INT_DATA_END_BIT) |
| 831 | host->data->error = MMC_ERR_FAILED; |
| 832 | |
| 833 | if (host->data->error != MMC_ERR_NONE) |
| 834 | sdhci_finish_data(host); |
| 835 | else { |
| 836 | if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY)) |
| 837 | sdhci_transfer_pio(host); |
| 838 | |
| 839 | if (intmask & SDHCI_INT_DATA_END) |
| 840 | sdhci_finish_data(host); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs) |
| 845 | { |
| 846 | irqreturn_t result; |
| 847 | struct sdhci_host* host = dev_id; |
| 848 | u32 intmask; |
| 849 | |
| 850 | spin_lock(&host->lock); |
| 851 | |
| 852 | intmask = readl(host->ioaddr + SDHCI_INT_STATUS); |
| 853 | |
| 854 | if (!intmask) { |
| 855 | result = IRQ_NONE; |
| 856 | goto out; |
| 857 | } |
| 858 | |
| 859 | DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask); |
| 860 | |
| 861 | if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) |
| 862 | tasklet_schedule(&host->card_tasklet); |
| 863 | |
| 864 | if (intmask & SDHCI_INT_CMD_MASK) { |
| 865 | sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK); |
| 866 | |
| 867 | writel(intmask & SDHCI_INT_CMD_MASK, |
| 868 | host->ioaddr + SDHCI_INT_STATUS); |
| 869 | } |
| 870 | |
| 871 | if (intmask & SDHCI_INT_DATA_MASK) { |
| 872 | sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK); |
| 873 | |
| 874 | writel(intmask & SDHCI_INT_DATA_MASK, |
| 875 | host->ioaddr + SDHCI_INT_STATUS); |
| 876 | } |
| 877 | |
| 878 | intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); |
| 879 | |
| 880 | if (intmask & SDHCI_INT_CARD_INT) { |
| 881 | printk(KERN_ERR "%s: Unexpected card interrupt. Please " |
| 882 | "report this to " BUGMAIL ".\n", |
| 883 | mmc_hostname(host->mmc)); |
| 884 | sdhci_dumpregs(host); |
| 885 | } |
| 886 | |
| 887 | if (intmask & SDHCI_INT_BUS_POWER) { |
| 888 | printk(KERN_ERR "%s: Unexpected bus power interrupt. Please " |
| 889 | "report this to " BUGMAIL ".\n", |
| 890 | mmc_hostname(host->mmc)); |
| 891 | sdhci_dumpregs(host); |
| 892 | } |
| 893 | |
| 894 | if (intmask & SDHCI_INT_ACMD12ERR) { |
| 895 | printk(KERN_ERR "%s: Unexpected auto CMD12 error. Please " |
| 896 | "report this to " BUGMAIL ".\n", |
| 897 | mmc_hostname(host->mmc)); |
| 898 | sdhci_dumpregs(host); |
| 899 | |
| 900 | writew(~0, host->ioaddr + SDHCI_ACMD12_ERR); |
| 901 | } |
| 902 | |
| 903 | if (intmask) |
| 904 | writel(intmask, host->ioaddr + SDHCI_INT_STATUS); |
| 905 | |
| 906 | result = IRQ_HANDLED; |
| 907 | |
| 908 | out: |
| 909 | spin_unlock(&host->lock); |
| 910 | |
| 911 | return result; |
| 912 | } |
| 913 | |
| 914 | /*****************************************************************************\ |
| 915 | * * |
| 916 | * Suspend/resume * |
| 917 | * * |
| 918 | \*****************************************************************************/ |
| 919 | |
| 920 | #ifdef CONFIG_PM |
| 921 | |
| 922 | static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state) |
| 923 | { |
| 924 | struct sdhci_chip *chip; |
| 925 | int i, ret; |
| 926 | |
| 927 | chip = pci_get_drvdata(pdev); |
| 928 | if (!chip) |
| 929 | return 0; |
| 930 | |
| 931 | DBG("Suspending...\n"); |
| 932 | |
| 933 | for (i = 0;i < chip->num_slots;i++) { |
| 934 | if (!chip->hosts[i]) |
| 935 | continue; |
| 936 | ret = mmc_suspend_host(chip->hosts[i]->mmc, state); |
| 937 | if (ret) { |
| 938 | for (i--;i >= 0;i--) |
| 939 | mmc_resume_host(chip->hosts[i]->mmc); |
| 940 | return ret; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | pci_save_state(pdev); |
| 945 | pci_enable_wake(pdev, pci_choose_state(pdev, state), 0); |
| 946 | pci_disable_device(pdev); |
| 947 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); |
| 948 | |
| 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | static int sdhci_resume (struct pci_dev *pdev) |
| 953 | { |
| 954 | struct sdhci_chip *chip; |
| 955 | int i, ret; |
| 956 | |
| 957 | chip = pci_get_drvdata(pdev); |
| 958 | if (!chip) |
| 959 | return 0; |
| 960 | |
| 961 | DBG("Resuming...\n"); |
| 962 | |
| 963 | pci_set_power_state(pdev, PCI_D0); |
| 964 | pci_restore_state(pdev); |
| 965 | pci_enable_device(pdev); |
| 966 | |
| 967 | for (i = 0;i < chip->num_slots;i++) { |
| 968 | if (!chip->hosts[i]) |
| 969 | continue; |
| 970 | if (chip->hosts[i]->flags & SDHCI_USE_DMA) |
| 971 | pci_set_master(pdev); |
| 972 | sdhci_init(chip->hosts[i]); |
| 973 | ret = mmc_resume_host(chip->hosts[i]->mmc); |
| 974 | if (ret) |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | #else /* CONFIG_PM */ |
| 982 | |
| 983 | #define sdhci_suspend NULL |
| 984 | #define sdhci_resume NULL |
| 985 | |
| 986 | #endif /* CONFIG_PM */ |
| 987 | |
| 988 | /*****************************************************************************\ |
| 989 | * * |
| 990 | * Device probing/removal * |
| 991 | * * |
| 992 | \*****************************************************************************/ |
| 993 | |
| 994 | static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot) |
| 995 | { |
| 996 | int ret; |
| 997 | struct sdhci_chip *chip; |
| 998 | struct mmc_host *mmc; |
| 999 | struct sdhci_host *host; |
| 1000 | |
| 1001 | u8 first_bar; |
| 1002 | unsigned int caps; |
| 1003 | |
| 1004 | chip = pci_get_drvdata(pdev); |
| 1005 | BUG_ON(!chip); |
| 1006 | |
| 1007 | ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar); |
| 1008 | if (ret) |
| 1009 | return ret; |
| 1010 | |
| 1011 | first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK; |
| 1012 | |
| 1013 | if (first_bar > 5) { |
| 1014 | printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n"); |
| 1015 | return -ENODEV; |
| 1016 | } |
| 1017 | |
| 1018 | if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) { |
| 1019 | printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n"); |
| 1020 | return -ENODEV; |
| 1021 | } |
| 1022 | |
| 1023 | if (pci_resource_len(pdev, first_bar + slot) != 0x100) { |
| 1024 | printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n"); |
| 1025 | return -ENODEV; |
| 1026 | } |
| 1027 | |
| 1028 | mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev); |
| 1029 | if (!mmc) |
| 1030 | return -ENOMEM; |
| 1031 | |
| 1032 | host = mmc_priv(mmc); |
| 1033 | host->mmc = mmc; |
| 1034 | |
| 1035 | host->bar = first_bar + slot; |
| 1036 | |
| 1037 | host->addr = pci_resource_start(pdev, host->bar); |
| 1038 | host->irq = pdev->irq; |
| 1039 | |
| 1040 | DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq); |
| 1041 | |
| 1042 | snprintf(host->slot_descr, 20, "sdhci:slot%d", slot); |
| 1043 | |
| 1044 | ret = pci_request_region(pdev, host->bar, host->slot_descr); |
| 1045 | if (ret) |
| 1046 | goto free; |
| 1047 | |
| 1048 | host->ioaddr = ioremap_nocache(host->addr, |
| 1049 | pci_resource_len(pdev, host->bar)); |
| 1050 | if (!host->ioaddr) { |
| 1051 | ret = -ENOMEM; |
| 1052 | goto release; |
| 1053 | } |
| 1054 | |
| 1055 | caps = readl(host->ioaddr + SDHCI_CAPABILITIES); |
| 1056 | |
| 1057 | if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01)) |
| 1058 | host->flags |= SDHCI_USE_DMA; |
| 1059 | |
| 1060 | if (host->flags & SDHCI_USE_DMA) { |
| 1061 | if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) { |
| 1062 | printk(KERN_WARNING "%s: No suitable DMA available. " |
| 1063 | "Falling back to PIO.\n", host->slot_descr); |
| 1064 | host->flags &= ~SDHCI_USE_DMA; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | if (host->flags & SDHCI_USE_DMA) |
| 1069 | pci_set_master(pdev); |
| 1070 | else /* XXX: Hack to get MMC layer to avoid highmem */ |
| 1071 | pdev->dma_mask = 0; |
| 1072 | |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1073 | host->max_clk = |
| 1074 | (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; |
| 1075 | if (host->max_clk == 0) { |
| 1076 | printk(KERN_ERR "%s: Hardware doesn't specify base clock " |
| 1077 | "frequency.\n", host->slot_descr); |
| 1078 | ret = -ENODEV; |
| 1079 | goto unmap; |
| 1080 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1081 | host->max_clk *= 1000000; |
| 1082 | |
| 1083 | /* |
| 1084 | * Set host parameters. |
| 1085 | */ |
| 1086 | mmc->ops = &sdhci_ops; |
| 1087 | mmc->f_min = host->max_clk / 256; |
| 1088 | mmc->f_max = host->max_clk; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1089 | mmc->caps = MMC_CAP_4_BIT_DATA; |
| 1090 | |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame^] | 1091 | mmc->ocr_avail = 0; |
| 1092 | if (caps & SDHCI_CAN_VDD_330) |
| 1093 | mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34; |
| 1094 | else if (caps & SDHCI_CAN_VDD_300) |
| 1095 | mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31; |
| 1096 | else if (caps & SDHCI_CAN_VDD_180) |
| 1097 | mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19; |
| 1098 | |
| 1099 | if (mmc->ocr_avail == 0) { |
| 1100 | printk(KERN_ERR "%s: Hardware doesn't report any " |
| 1101 | "support voltages.\n", host->slot_descr); |
| 1102 | ret = -ENODEV; |
| 1103 | goto unmap; |
| 1104 | } |
| 1105 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1106 | spin_lock_init(&host->lock); |
| 1107 | |
| 1108 | /* |
| 1109 | * Maximum number of segments. Hardware cannot do scatter lists. |
| 1110 | */ |
| 1111 | if (host->flags & SDHCI_USE_DMA) |
| 1112 | mmc->max_hw_segs = 1; |
| 1113 | else |
| 1114 | mmc->max_hw_segs = 16; |
| 1115 | mmc->max_phys_segs = 16; |
| 1116 | |
| 1117 | /* |
| 1118 | * Maximum number of sectors in one transfer. Limited by sector |
| 1119 | * count register. |
| 1120 | */ |
| 1121 | mmc->max_sectors = 0x3FFF; |
| 1122 | |
| 1123 | /* |
| 1124 | * Maximum segment size. Could be one segment with the maximum number |
| 1125 | * of sectors. |
| 1126 | */ |
| 1127 | mmc->max_seg_size = mmc->max_sectors * 512; |
| 1128 | |
| 1129 | /* |
| 1130 | * Init tasklets. |
| 1131 | */ |
| 1132 | tasklet_init(&host->card_tasklet, |
| 1133 | sdhci_tasklet_card, (unsigned long)host); |
| 1134 | tasklet_init(&host->finish_tasklet, |
| 1135 | sdhci_tasklet_finish, (unsigned long)host); |
| 1136 | |
Andrew Morton | e474c66 | 2006-06-12 22:10:22 +0100 | [diff] [blame] | 1137 | setup_timer(&host->timer, sdhci_timeout_timer, (long)host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1138 | |
| 1139 | ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ, |
| 1140 | host->slot_descr, host); |
| 1141 | if (ret) |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1142 | goto untasklet; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1143 | |
| 1144 | sdhci_init(host); |
| 1145 | |
| 1146 | #ifdef CONFIG_MMC_DEBUG |
| 1147 | sdhci_dumpregs(host); |
| 1148 | #endif |
| 1149 | |
| 1150 | host->chip = chip; |
| 1151 | chip->hosts[slot] = host; |
| 1152 | |
| 1153 | mmc_add_host(mmc); |
| 1154 | |
| 1155 | printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc), |
| 1156 | host->addr, host->irq, |
| 1157 | (host->flags & SDHCI_USE_DMA)?"DMA":"PIO"); |
| 1158 | |
| 1159 | return 0; |
| 1160 | |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1161 | untasklet: |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1162 | tasklet_kill(&host->card_tasklet); |
| 1163 | tasklet_kill(&host->finish_tasklet); |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 1164 | unmap: |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1165 | iounmap(host->ioaddr); |
| 1166 | release: |
| 1167 | pci_release_region(pdev, host->bar); |
| 1168 | free: |
| 1169 | mmc_free_host(mmc); |
| 1170 | |
| 1171 | return ret; |
| 1172 | } |
| 1173 | |
| 1174 | static void sdhci_remove_slot(struct pci_dev *pdev, int slot) |
| 1175 | { |
| 1176 | struct sdhci_chip *chip; |
| 1177 | struct mmc_host *mmc; |
| 1178 | struct sdhci_host *host; |
| 1179 | |
| 1180 | chip = pci_get_drvdata(pdev); |
| 1181 | host = chip->hosts[slot]; |
| 1182 | mmc = host->mmc; |
| 1183 | |
| 1184 | chip->hosts[slot] = NULL; |
| 1185 | |
| 1186 | mmc_remove_host(mmc); |
| 1187 | |
| 1188 | sdhci_reset(host, SDHCI_RESET_ALL); |
| 1189 | |
| 1190 | free_irq(host->irq, host); |
| 1191 | |
| 1192 | del_timer_sync(&host->timer); |
| 1193 | |
| 1194 | tasklet_kill(&host->card_tasklet); |
| 1195 | tasklet_kill(&host->finish_tasklet); |
| 1196 | |
| 1197 | iounmap(host->ioaddr); |
| 1198 | |
| 1199 | pci_release_region(pdev, host->bar); |
| 1200 | |
| 1201 | mmc_free_host(mmc); |
| 1202 | } |
| 1203 | |
| 1204 | static int __devinit sdhci_probe(struct pci_dev *pdev, |
| 1205 | const struct pci_device_id *ent) |
| 1206 | { |
| 1207 | int ret, i; |
Pierre Ossman | 51f82bc | 2006-06-30 02:22:22 -0700 | [diff] [blame] | 1208 | u8 slots, rev; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1209 | struct sdhci_chip *chip; |
| 1210 | |
| 1211 | BUG_ON(pdev == NULL); |
| 1212 | BUG_ON(ent == NULL); |
| 1213 | |
Pierre Ossman | 51f82bc | 2006-06-30 02:22:22 -0700 | [diff] [blame] | 1214 | pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev); |
| 1215 | |
| 1216 | printk(KERN_INFO DRIVER_NAME |
| 1217 | ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n", |
| 1218 | pci_name(pdev), (int)pdev->vendor, (int)pdev->device, |
| 1219 | (int)rev); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1220 | |
| 1221 | ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots); |
| 1222 | if (ret) |
| 1223 | return ret; |
| 1224 | |
| 1225 | slots = PCI_SLOT_INFO_SLOTS(slots) + 1; |
| 1226 | DBG("found %d slot(s)\n", slots); |
| 1227 | if (slots == 0) |
| 1228 | return -ENODEV; |
| 1229 | |
| 1230 | ret = pci_enable_device(pdev); |
| 1231 | if (ret) |
| 1232 | return ret; |
| 1233 | |
| 1234 | chip = kzalloc(sizeof(struct sdhci_chip) + |
| 1235 | sizeof(struct sdhci_host*) * slots, GFP_KERNEL); |
| 1236 | if (!chip) { |
| 1237 | ret = -ENOMEM; |
| 1238 | goto err; |
| 1239 | } |
| 1240 | |
| 1241 | chip->pdev = pdev; |
| 1242 | |
| 1243 | chip->num_slots = slots; |
| 1244 | pci_set_drvdata(pdev, chip); |
| 1245 | |
| 1246 | for (i = 0;i < slots;i++) { |
| 1247 | ret = sdhci_probe_slot(pdev, i); |
| 1248 | if (ret) { |
| 1249 | for (i--;i >= 0;i--) |
| 1250 | sdhci_remove_slot(pdev, i); |
| 1251 | goto free; |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | return 0; |
| 1256 | |
| 1257 | free: |
| 1258 | pci_set_drvdata(pdev, NULL); |
| 1259 | kfree(chip); |
| 1260 | |
| 1261 | err: |
| 1262 | pci_disable_device(pdev); |
| 1263 | return ret; |
| 1264 | } |
| 1265 | |
| 1266 | static void __devexit sdhci_remove(struct pci_dev *pdev) |
| 1267 | { |
| 1268 | int i; |
| 1269 | struct sdhci_chip *chip; |
| 1270 | |
| 1271 | chip = pci_get_drvdata(pdev); |
| 1272 | |
| 1273 | if (chip) { |
| 1274 | for (i = 0;i < chip->num_slots;i++) |
| 1275 | sdhci_remove_slot(pdev, i); |
| 1276 | |
| 1277 | pci_set_drvdata(pdev, NULL); |
| 1278 | |
| 1279 | kfree(chip); |
| 1280 | } |
| 1281 | |
| 1282 | pci_disable_device(pdev); |
| 1283 | } |
| 1284 | |
| 1285 | static struct pci_driver sdhci_driver = { |
| 1286 | .name = DRIVER_NAME, |
| 1287 | .id_table = pci_ids, |
| 1288 | .probe = sdhci_probe, |
| 1289 | .remove = __devexit_p(sdhci_remove), |
| 1290 | .suspend = sdhci_suspend, |
| 1291 | .resume = sdhci_resume, |
| 1292 | }; |
| 1293 | |
| 1294 | /*****************************************************************************\ |
| 1295 | * * |
| 1296 | * Driver init/exit * |
| 1297 | * * |
| 1298 | \*****************************************************************************/ |
| 1299 | |
| 1300 | static int __init sdhci_drv_init(void) |
| 1301 | { |
| 1302 | printk(KERN_INFO DRIVER_NAME |
| 1303 | ": Secure Digital Host Controller Interface driver, " |
| 1304 | DRIVER_VERSION "\n"); |
| 1305 | printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n"); |
| 1306 | |
| 1307 | return pci_register_driver(&sdhci_driver); |
| 1308 | } |
| 1309 | |
| 1310 | static void __exit sdhci_drv_exit(void) |
| 1311 | { |
| 1312 | DBG("Exiting\n"); |
| 1313 | |
| 1314 | pci_unregister_driver(&sdhci_driver); |
| 1315 | } |
| 1316 | |
| 1317 | module_init(sdhci_drv_init); |
| 1318 | module_exit(sdhci_drv_exit); |
| 1319 | |
| 1320 | MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>"); |
| 1321 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver"); |
| 1322 | MODULE_VERSION(DRIVER_VERSION); |
| 1323 | MODULE_LICENSE("GPL"); |