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