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