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