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