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