blob: 71e020d6718db2329f2154a2b90bb86fbd7ef06f [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * 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 Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
18#include <linux/pci.h>
19#include <linux/dma-mapping.h>
Ralf Baechle11763602007-10-23 20:42:11 +020020#include <linux/scatterlist.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021
22#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080023
Pierre Ossmand129bce2006-03-24 03:18:17 -080024#include "sdhci.h"
25
26#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080027
Pierre Ossmand129bce2006-03-24 03:18:17 -080028#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010029 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmandf673b22006-06-30 02:22:31 -070031static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070032
Pierre Ossmandc934412007-12-02 19:45:19 +010033/*
34 * Different quirks to handle when the hardware deviates from a strict
35 * interpretation of the SDHCI specification.
36 */
37
38/* Controller doesn't honor resets unless we touch the clock register */
Pierre Ossman645289d2006-06-30 02:22:33 -070039#define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
Pierre Ossmandc934412007-12-02 19:45:19 +010040/* Controller has bad caps bits, but really supports DMA */
Pierre Ossman98608072006-06-30 02:22:34 -070041#define SDHCI_QUIRK_FORCE_DMA (1<<1)
Pierre Ossman8a4da142006-10-04 02:15:40 -070042/* Controller doesn't like some resets when there is no card inserted. */
43#define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
Pierre Ossmandc934412007-12-02 19:45:19 +010044/* Controller doesn't like clearing the power reg before a change */
Darren Salt9e9dc5f2007-01-27 15:32:31 +010045#define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
Pierre Ossmandc934412007-12-02 19:45:19 +010046/* Controller has flaky internal state so reset it on each ios change */
Leandro Dorileob8352262007-07-25 23:47:04 +020047#define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4)
Pierre Ossmandc934412007-12-02 19:45:19 +010048/* Controller has an unusable DMA engine */
Feng Tang7c168e32007-09-30 12:44:18 +020049#define SDHCI_QUIRK_BROKEN_DMA (1<<5)
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +010050/* Controller can only DMA from 32-bit aligned addresses */
51#define SDHCI_QUIRK_32BIT_DMA_ADDR (1<<6)
52/* Controller can only DMA chunk sizes that are a multiple of 32 bits */
53#define SDHCI_QUIRK_32BIT_DMA_SIZE (1<<7)
Pierre Ossman84c46a52007-12-02 19:58:16 +010054/* Controller needs to be reset after each request to stay stable */
55#define SDHCI_QUIRK_RESET_AFTER_REQUEST (1<<8)
Pierre Ossman645289d2006-06-30 02:22:33 -070056
Pierre Ossmand129bce2006-03-24 03:18:17 -080057static const struct pci_device_id pci_ids[] __devinitdata = {
Pierre Ossman645289d2006-06-30 02:22:33 -070058 {
59 .vendor = PCI_VENDOR_ID_RICOH,
60 .device = PCI_DEVICE_ID_RICOH_R5C822,
61 .subvendor = PCI_VENDOR_ID_IBM,
62 .subdevice = PCI_ANY_ID,
Pierre Ossman98608072006-06-30 02:22:34 -070063 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
64 SDHCI_QUIRK_FORCE_DMA,
65 },
66
67 {
68 .vendor = PCI_VENDOR_ID_RICOH,
69 .device = PCI_DEVICE_ID_RICOH_R5C822,
70 .subvendor = PCI_ANY_ID,
71 .subdevice = PCI_ANY_ID,
Pierre Ossman8a4da142006-10-04 02:15:40 -070072 .driver_data = SDHCI_QUIRK_FORCE_DMA |
73 SDHCI_QUIRK_NO_CARD_NO_RESET,
Pierre Ossman98608072006-06-30 02:22:34 -070074 },
75
76 {
77 .vendor = PCI_VENDOR_ID_TI,
78 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
79 .subvendor = PCI_ANY_ID,
80 .subdevice = PCI_ANY_ID,
81 .driver_data = SDHCI_QUIRK_FORCE_DMA,
Pierre Ossman645289d2006-06-30 02:22:33 -070082 },
83
Darren Salt9e9dc5f2007-01-27 15:32:31 +010084 {
85 .vendor = PCI_VENDOR_ID_ENE,
86 .device = PCI_DEVICE_ID_ENE_CB712_SD,
87 .subvendor = PCI_ANY_ID,
88 .subdevice = PCI_ANY_ID,
Feng Tang7c168e32007-09-30 12:44:18 +020089 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
90 SDHCI_QUIRK_BROKEN_DMA,
Darren Salt9e9dc5f2007-01-27 15:32:31 +010091 },
92
Milko Krachounov7de064e2007-05-19 01:18:03 +020093 {
94 .vendor = PCI_VENDOR_ID_ENE,
95 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
96 .subvendor = PCI_ANY_ID,
97 .subdevice = PCI_ANY_ID,
Feng Tang7c168e32007-09-30 12:44:18 +020098 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
99 SDHCI_QUIRK_BROKEN_DMA,
Milko Krachounov7de064e2007-05-19 01:18:03 +0200100 },
101
Leandro Dorileob8352262007-07-25 23:47:04 +0200102 {
103 .vendor = PCI_VENDOR_ID_ENE,
104 .device = PCI_DEVICE_ID_ENE_CB714_SD,
105 .subvendor = PCI_ANY_ID,
106 .subdevice = PCI_ANY_ID,
107 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
108 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
109 },
110
111 {
112 .vendor = PCI_VENDOR_ID_ENE,
113 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
114 .subvendor = PCI_ANY_ID,
115 .subdevice = PCI_ANY_ID,
116 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
117 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
118 },
119
Pierre Ossman84c46a52007-12-02 19:58:16 +0100120 {
121 .vendor = PCI_VENDOR_ID_JMICRON,
122 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
123 .subvendor = PCI_ANY_ID,
124 .subdevice = PCI_ANY_ID,
125 .driver_data = SDHCI_QUIRK_32BIT_DMA_ADDR |
126 SDHCI_QUIRK_32BIT_DMA_SIZE |
127 SDHCI_QUIRK_RESET_AFTER_REQUEST,
128 },
129
Pierre Ossman645289d2006-06-30 02:22:33 -0700130 { /* Generic SD host controller */
131 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
132 },
133
Pierre Ossmand129bce2006-03-24 03:18:17 -0800134 { /* end: all zeroes */ },
135};
136
137MODULE_DEVICE_TABLE(pci, pci_ids);
138
139static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
140static void sdhci_finish_data(struct sdhci_host *);
141
142static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
143static void sdhci_finish_command(struct sdhci_host *);
144
145static void sdhci_dumpregs(struct sdhci_host *host)
146{
147 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
148
149 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
150 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
151 readw(host->ioaddr + SDHCI_HOST_VERSION));
152 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
153 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
154 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
155 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
156 readl(host->ioaddr + SDHCI_ARGUMENT),
157 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
158 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
159 readl(host->ioaddr + SDHCI_PRESENT_STATE),
160 readb(host->ioaddr + SDHCI_HOST_CONTROL));
161 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
162 readb(host->ioaddr + SDHCI_POWER_CONTROL),
163 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
164 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Nicolas Pitre2df3b712007-09-29 10:46:20 -0400165 readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800166 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
167 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
168 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
169 readl(host->ioaddr + SDHCI_INT_STATUS));
170 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
171 readl(host->ioaddr + SDHCI_INT_ENABLE),
172 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
173 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
174 readw(host->ioaddr + SDHCI_ACMD12_ERR),
175 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
176 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
177 readl(host->ioaddr + SDHCI_CAPABILITIES),
178 readl(host->ioaddr + SDHCI_MAX_CURRENT));
179
180 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
181}
182
183/*****************************************************************************\
184 * *
185 * Low level functions *
186 * *
187\*****************************************************************************/
188
189static void sdhci_reset(struct sdhci_host *host, u8 mask)
190{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700191 unsigned long timeout;
192
Pierre Ossman8a4da142006-10-04 02:15:40 -0700193 if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
194 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
195 SDHCI_CARD_PRESENT))
196 return;
197 }
198
Pierre Ossmand129bce2006-03-24 03:18:17 -0800199 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
200
Pierre Ossmane16514d82006-06-30 02:22:24 -0700201 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800202 host->clock = 0;
203
Pierre Ossmane16514d82006-06-30 02:22:24 -0700204 /* Wait max 100 ms */
205 timeout = 100;
206
207 /* hw clears the bit when it's done */
208 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
209 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100210 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700211 mmc_hostname(host->mmc), (int)mask);
212 sdhci_dumpregs(host);
213 return;
214 }
215 timeout--;
216 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800217 }
218}
219
220static void sdhci_init(struct sdhci_host *host)
221{
222 u32 intmask;
223
224 sdhci_reset(host, SDHCI_RESET_ALL);
225
Pierre Ossman3192a282006-06-30 02:22:26 -0700226 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
227 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
228 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
229 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100230 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
Pierre Ossman3192a282006-06-30 02:22:26 -0700231 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
233 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
234 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800235}
236
237static void sdhci_activate_led(struct sdhci_host *host)
238{
239 u8 ctrl;
240
241 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
242 ctrl |= SDHCI_CTRL_LED;
243 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
244}
245
246static void sdhci_deactivate_led(struct sdhci_host *host)
247{
248 u8 ctrl;
249
250 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
251 ctrl &= ~SDHCI_CTRL_LED;
252 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
253}
254
255/*****************************************************************************\
256 * *
257 * Core functions *
258 * *
259\*****************************************************************************/
260
Pierre Ossman2a22b142007-02-02 18:27:42 +0100261static inline char* sdhci_sg_to_buffer(struct sdhci_host* host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262{
Jens Axboe45711f12007-10-22 21:19:53 +0200263 return sg_virt(host->cur_sg);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264}
265
266static inline int sdhci_next_sg(struct sdhci_host* host)
267{
268 /*
269 * Skip to next SG entry.
270 */
271 host->cur_sg++;
272 host->num_sg--;
273
274 /*
275 * Any entries left?
276 */
277 if (host->num_sg > 0) {
278 host->offset = 0;
279 host->remain = host->cur_sg->length;
280 }
281
282 return host->num_sg;
283}
284
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100285static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286{
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100287 int blksize, chunk_remain;
288 u32 data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800289 char *buffer;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100290 int size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800291
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100292 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800293
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100294 blksize = host->data->blksz;
295 chunk_remain = 0;
296 data = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800297
Pierre Ossman2a22b142007-02-02 18:27:42 +0100298 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800299
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100300 while (blksize) {
301 if (chunk_remain == 0) {
302 data = readl(host->ioaddr + SDHCI_BUFFER);
303 chunk_remain = min(blksize, 4);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800304 }
305
Alex Dubov14d836e2007-04-13 19:04:38 +0200306 size = min(host->remain, chunk_remain);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308 chunk_remain -= size;
309 blksize -= size;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310 host->offset += size;
311 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 while (size) {
314 *buffer = data & 0xFF;
315 buffer++;
316 data >>= 8;
317 size--;
318 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
320 if (host->remain == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321 if (sdhci_next_sg(host) == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 BUG_ON(blksize != 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323 return;
324 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100325 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326 }
327 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100328}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800329
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100330static void sdhci_write_block_pio(struct sdhci_host *host)
331{
332 int blksize, chunk_remain;
333 u32 data;
334 char *buffer;
335 int bytes, size;
336
337 DBG("PIO writing\n");
338
339 blksize = host->data->blksz;
340 chunk_remain = 4;
341 data = 0;
342
343 bytes = 0;
Pierre Ossman2a22b142007-02-02 18:27:42 +0100344 buffer = sdhci_sg_to_buffer(host) + host->offset;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100345
346 while (blksize) {
Alex Dubov14d836e2007-04-13 19:04:38 +0200347 size = min(host->remain, chunk_remain);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348
349 chunk_remain -= size;
350 blksize -= size;
351 host->offset += size;
352 host->remain -= size;
Alex Dubov14d836e2007-04-13 19:04:38 +0200353
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354 while (size) {
355 data >>= 8;
356 data |= (u32)*buffer << 24;
357 buffer++;
358 size--;
359 }
360
361 if (chunk_remain == 0) {
362 writel(data, host->ioaddr + SDHCI_BUFFER);
363 chunk_remain = min(blksize, 4);
364 }
365
366 if (host->remain == 0) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367 if (sdhci_next_sg(host) == 0) {
368 BUG_ON(blksize != 0);
369 return;
370 }
Pierre Ossman2a22b142007-02-02 18:27:42 +0100371 buffer = sdhci_sg_to_buffer(host);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372 }
373 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374}
375
376static void sdhci_transfer_pio(struct sdhci_host *host)
377{
378 u32 mask;
379
380 BUG_ON(!host->data);
381
Alex Dubov14d836e2007-04-13 19:04:38 +0200382 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383 return;
384
385 if (host->data->flags & MMC_DATA_READ)
386 mask = SDHCI_DATA_AVAILABLE;
387 else
388 mask = SDHCI_SPACE_AVAILABLE;
389
390 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
391 if (host->data->flags & MMC_DATA_READ)
392 sdhci_read_block_pio(host);
393 else
394 sdhci_write_block_pio(host);
395
Alex Dubov14d836e2007-04-13 19:04:38 +0200396 if (host->num_sg == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100398 }
399
400 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800401}
402
403static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
404{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700405 u8 count;
406 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800407
408 WARN_ON(host->data);
409
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700410 if (data == NULL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800411 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800412
Pierre Ossmanbab76962006-07-02 16:51:35 +0100413 /* Sanity checks */
414 BUG_ON(data->blksz * data->blocks > 524288);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100415 BUG_ON(data->blksz > host->mmc->max_blk_size);
Pierre Ossman1d676e02006-07-02 16:52:10 +0100416 BUG_ON(data->blocks > 65535);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800417
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200418 host->data = data;
419 host->data_early = 0;
420
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700421 /* timeout in us */
422 target_timeout = data->timeout_ns / 1000 +
423 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800424
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700425 /*
426 * Figure out needed cycles.
427 * We do this in steps in order to fit inside a 32 bit int.
428 * The first step is the minimum timeout, which will have a
429 * minimum resolution of 6 bits:
430 * (1) 2^13*1000 > 2^22,
431 * (2) host->timeout_clk < 2^16
432 * =>
433 * (1) / (2) > 2^6
434 */
435 count = 0;
436 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
437 while (current_timeout < target_timeout) {
438 count++;
439 current_timeout <<= 1;
440 if (count >= 0xF)
441 break;
442 }
443
444 if (count >= 0xF) {
445 printk(KERN_WARNING "%s: Too large timeout requested!\n",
446 mmc_hostname(host->mmc));
447 count = 0xE;
448 }
449
450 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800451
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100452 if (host->flags & SDHCI_USE_DMA)
453 host->flags |= SDHCI_REQ_USE_DMA;
454
455 if (unlikely((host->flags & SDHCI_REQ_USE_DMA) &&
456 (host->chip->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
457 ((data->blksz * data->blocks) & 0x3))) {
458 DBG("Reverting to PIO because of transfer size (%d)\n",
459 data->blksz * data->blocks);
460 host->flags &= ~SDHCI_REQ_USE_DMA;
461 }
462
463 /*
464 * The assumption here being that alignment is the same after
465 * translation to device address space.
466 */
467 if (unlikely((host->flags & SDHCI_REQ_USE_DMA) &&
468 (host->chip->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
469 (data->sg->offset & 0x3))) {
470 DBG("Reverting to PIO because of bad alignment\n");
471 host->flags &= ~SDHCI_REQ_USE_DMA;
472 }
473
474 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800475 int count;
476
477 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
478 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
479 BUG_ON(count != 1);
480
481 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
482 } else {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800483 host->cur_sg = data->sg;
484 host->num_sg = data->sg_len;
485
486 host->offset = 0;
487 host->remain = host->cur_sg->length;
488 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700489
Pierre Ossmanbab76962006-07-02 16:51:35 +0100490 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
491 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
492 host->ioaddr + SDHCI_BLOCK_SIZE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700493 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
494}
495
496static void sdhci_set_transfer_mode(struct sdhci_host *host,
497 struct mmc_data *data)
498{
499 u16 mode;
500
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700501 if (data == NULL)
502 return;
503
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200504 WARN_ON(!host->data);
505
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700506 mode = SDHCI_TRNS_BLK_CNT_EN;
507 if (data->blocks > 1)
508 mode |= SDHCI_TRNS_MULTI;
509 if (data->flags & MMC_DATA_READ)
510 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100511 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700512 mode |= SDHCI_TRNS_DMA;
513
514 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800515}
516
517static void sdhci_finish_data(struct sdhci_host *host)
518{
519 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800520 u16 blocks;
521
522 BUG_ON(!host->data);
523
524 data = host->data;
525 host->data = NULL;
526
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100527 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800528 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
529 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800530 }
531
532 /*
533 * Controller doesn't count down when in single block mode.
534 */
Pierre Ossman2b061972007-08-12 13:13:24 +0200535 if (data->blocks == 1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200536 blocks = (data->error == 0) ? 0 : 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800537 else
538 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
Russell Kinga3fd4a12006-06-04 17:51:15 +0100539 data->bytes_xfered = data->blksz * (data->blocks - blocks);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800540
Pierre Ossman17b04292007-07-22 22:18:46 +0200541 if (!data->error && blocks) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800542 printk(KERN_ERR "%s: Controller signalled completion even "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100543 "though there were blocks left.\n",
544 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200545 data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800546 }
547
Pierre Ossmand129bce2006-03-24 03:18:17 -0800548 if (data->stop) {
549 /*
550 * The controller needs a reset of internal state machines
551 * upon error conditions.
552 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200553 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800554 sdhci_reset(host, SDHCI_RESET_CMD);
555 sdhci_reset(host, SDHCI_RESET_DATA);
556 }
557
558 sdhci_send_command(host, data->stop);
559 } else
560 tasklet_schedule(&host->finish_tasklet);
561}
562
563static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
564{
565 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700566 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700567 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800568
569 WARN_ON(host->cmd);
570
Pierre Ossmand129bce2006-03-24 03:18:17 -0800571 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700572 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700573
574 mask = SDHCI_CMD_INHIBIT;
575 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
576 mask |= SDHCI_DATA_INHIBIT;
577
578 /* We shouldn't wait for data inihibit for stop commands, even
579 though they might use busy signaling */
580 if (host->mrq->data && (cmd == host->mrq->data->stop))
581 mask &= ~SDHCI_DATA_INHIBIT;
582
583 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700584 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800585 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100586 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800587 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200588 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800589 tasklet_schedule(&host->finish_tasklet);
590 return;
591 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700592 timeout--;
593 mdelay(1);
594 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800595
596 mod_timer(&host->timer, jiffies + 10 * HZ);
597
598 host->cmd = cmd;
599
600 sdhci_prepare_data(host, cmd->data);
601
602 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
603
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700604 sdhci_set_transfer_mode(host, cmd->data);
605
Pierre Ossmand129bce2006-03-24 03:18:17 -0800606 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100607 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800608 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200609 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800610 tasklet_schedule(&host->finish_tasklet);
611 return;
612 }
613
614 if (!(cmd->flags & MMC_RSP_PRESENT))
615 flags = SDHCI_CMD_RESP_NONE;
616 else if (cmd->flags & MMC_RSP_136)
617 flags = SDHCI_CMD_RESP_LONG;
618 else if (cmd->flags & MMC_RSP_BUSY)
619 flags = SDHCI_CMD_RESP_SHORT_BUSY;
620 else
621 flags = SDHCI_CMD_RESP_SHORT;
622
623 if (cmd->flags & MMC_RSP_CRC)
624 flags |= SDHCI_CMD_CRC;
625 if (cmd->flags & MMC_RSP_OPCODE)
626 flags |= SDHCI_CMD_INDEX;
627 if (cmd->data)
628 flags |= SDHCI_CMD_DATA;
629
Pierre Ossmanfb61e282006-07-11 21:06:48 +0200630 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
Pierre Ossmand129bce2006-03-24 03:18:17 -0800631 host->ioaddr + SDHCI_COMMAND);
632}
633
634static void sdhci_finish_command(struct sdhci_host *host)
635{
636 int i;
637
638 BUG_ON(host->cmd == NULL);
639
640 if (host->cmd->flags & MMC_RSP_PRESENT) {
641 if (host->cmd->flags & MMC_RSP_136) {
642 /* CRC is stripped so we need to do some shifting. */
643 for (i = 0;i < 4;i++) {
644 host->cmd->resp[i] = readl(host->ioaddr +
645 SDHCI_RESPONSE + (3-i)*4) << 8;
646 if (i != 3)
647 host->cmd->resp[i] |=
648 readb(host->ioaddr +
649 SDHCI_RESPONSE + (3-i)*4-1);
650 }
651 } else {
652 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
653 }
654 }
655
Pierre Ossman17b04292007-07-22 22:18:46 +0200656 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800657
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200658 if (host->data && host->data_early)
659 sdhci_finish_data(host);
660
661 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800662 tasklet_schedule(&host->finish_tasklet);
663
664 host->cmd = NULL;
665}
666
667static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
668{
669 int div;
670 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700671 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800672
673 if (clock == host->clock)
674 return;
675
676 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
677
678 if (clock == 0)
679 goto out;
680
681 for (div = 1;div < 256;div *= 2) {
682 if ((host->max_clk / div) <= clock)
683 break;
684 }
685 div >>= 1;
686
687 clk = div << SDHCI_DIVIDER_SHIFT;
688 clk |= SDHCI_CLOCK_INT_EN;
689 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
690
691 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700692 timeout = 10;
693 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
694 & SDHCI_CLOCK_INT_STABLE)) {
695 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100696 printk(KERN_ERR "%s: Internal clock never "
697 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800698 sdhci_dumpregs(host);
699 return;
700 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700701 timeout--;
702 mdelay(1);
703 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800704
705 clk |= SDHCI_CLOCK_CARD_EN;
706 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
707
708out:
709 host->clock = clock;
710}
711
Pierre Ossman146ad662006-06-30 02:22:23 -0700712static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
713{
714 u8 pwr;
715
716 if (host->power == power)
717 return;
718
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100719 if (power == (unsigned short)-1) {
720 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700721 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100722 }
723
724 /*
725 * Spec says that we should clear the power reg before setting
726 * a new value. Some controllers don't seem to like this though.
727 */
728 if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
729 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700730
731 pwr = SDHCI_POWER_ON;
732
Philip Langdale4be34c92007-03-11 17:15:15 -0700733 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -0700734 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -0700735 pwr |= SDHCI_POWER_180;
736 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700737 case MMC_VDD_29_30:
738 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -0700739 pwr |= SDHCI_POWER_300;
740 break;
Philip Langdale4be34c92007-03-11 17:15:15 -0700741 case MMC_VDD_32_33:
742 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -0700743 pwr |= SDHCI_POWER_330;
744 break;
745 default:
746 BUG();
747 }
748
749 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
750
751out:
752 host->power = power;
753}
754
Pierre Ossmand129bce2006-03-24 03:18:17 -0800755/*****************************************************************************\
756 * *
757 * MMC callbacks *
758 * *
759\*****************************************************************************/
760
761static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
762{
763 struct sdhci_host *host;
764 unsigned long flags;
765
766 host = mmc_priv(mmc);
767
768 spin_lock_irqsave(&host->lock, flags);
769
770 WARN_ON(host->mrq != NULL);
771
772 sdhci_activate_led(host);
773
774 host->mrq = mrq;
775
776 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200777 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800778 tasklet_schedule(&host->finish_tasklet);
779 } else
780 sdhci_send_command(host, mrq->cmd);
781
Pierre Ossman5f25a662006-10-04 02:15:39 -0700782 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800783 spin_unlock_irqrestore(&host->lock, flags);
784}
785
786static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
787{
788 struct sdhci_host *host;
789 unsigned long flags;
790 u8 ctrl;
791
792 host = mmc_priv(mmc);
793
794 spin_lock_irqsave(&host->lock, flags);
795
Pierre Ossmand129bce2006-03-24 03:18:17 -0800796 /*
797 * Reset the chip on each power off.
798 * Should clear out any weird states.
799 */
800 if (ios->power_mode == MMC_POWER_OFF) {
801 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800802 sdhci_init(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800803 }
804
805 sdhci_set_clock(host, ios->clock);
806
807 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -0700808 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800809 else
Pierre Ossman146ad662006-06-30 02:22:23 -0700810 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800811
812 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100813
Pierre Ossmand129bce2006-03-24 03:18:17 -0800814 if (ios->bus_width == MMC_BUS_WIDTH_4)
815 ctrl |= SDHCI_CTRL_4BITBUS;
816 else
817 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100818
819 if (ios->timing == MMC_TIMING_SD_HS)
820 ctrl |= SDHCI_CTRL_HISPD;
821 else
822 ctrl &= ~SDHCI_CTRL_HISPD;
823
Pierre Ossmand129bce2006-03-24 03:18:17 -0800824 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
825
Leandro Dorileob8352262007-07-25 23:47:04 +0200826 /*
827 * Some (ENE) controllers go apeshit on some ios operation,
828 * signalling timeout and CRC errors even on CMD0. Resetting
829 * it on each ios seems to solve the problem.
830 */
831 if(host->chip->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
832 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
833
Pierre Ossman5f25a662006-10-04 02:15:39 -0700834 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800835 spin_unlock_irqrestore(&host->lock, flags);
836}
837
838static int sdhci_get_ro(struct mmc_host *mmc)
839{
840 struct sdhci_host *host;
841 unsigned long flags;
842 int present;
843
844 host = mmc_priv(mmc);
845
846 spin_lock_irqsave(&host->lock, flags);
847
848 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
849
850 spin_unlock_irqrestore(&host->lock, flags);
851
852 return !(present & SDHCI_WRITE_PROTECT);
853}
854
Pierre Ossmanf75979b2007-09-04 07:59:18 +0200855static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
856{
857 struct sdhci_host *host;
858 unsigned long flags;
859 u32 ier;
860
861 host = mmc_priv(mmc);
862
863 spin_lock_irqsave(&host->lock, flags);
864
865 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
866
867 ier &= ~SDHCI_INT_CARD_INT;
868 if (enable)
869 ier |= SDHCI_INT_CARD_INT;
870
871 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
872 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
873
874 mmiowb();
875
876 spin_unlock_irqrestore(&host->lock, flags);
877}
878
David Brownellab7aefd2006-11-12 17:55:30 -0800879static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800880 .request = sdhci_request,
881 .set_ios = sdhci_set_ios,
882 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +0200883 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800884};
885
886/*****************************************************************************\
887 * *
888 * Tasklets *
889 * *
890\*****************************************************************************/
891
892static void sdhci_tasklet_card(unsigned long param)
893{
894 struct sdhci_host *host;
895 unsigned long flags;
896
897 host = (struct sdhci_host*)param;
898
899 spin_lock_irqsave(&host->lock, flags);
900
901 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
902 if (host->mrq) {
903 printk(KERN_ERR "%s: Card removed during transfer!\n",
904 mmc_hostname(host->mmc));
905 printk(KERN_ERR "%s: Resetting controller.\n",
906 mmc_hostname(host->mmc));
907
908 sdhci_reset(host, SDHCI_RESET_CMD);
909 sdhci_reset(host, SDHCI_RESET_DATA);
910
Pierre Ossman17b04292007-07-22 22:18:46 +0200911 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800912 tasklet_schedule(&host->finish_tasklet);
913 }
914 }
915
916 spin_unlock_irqrestore(&host->lock, flags);
917
918 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
919}
920
921static void sdhci_tasklet_finish(unsigned long param)
922{
923 struct sdhci_host *host;
924 unsigned long flags;
925 struct mmc_request *mrq;
926
927 host = (struct sdhci_host*)param;
928
929 spin_lock_irqsave(&host->lock, flags);
930
931 del_timer(&host->timer);
932
933 mrq = host->mrq;
934
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935 /*
936 * The controller needs a reset of internal state machines
937 * upon error conditions.
938 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200939 if (mrq->cmd->error ||
940 (mrq->data && (mrq->data->error ||
Pierre Ossman84c46a52007-12-02 19:58:16 +0100941 (mrq->data->stop && mrq->data->stop->error))) ||
942 (host->chip->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
Pierre Ossman645289d2006-06-30 02:22:33 -0700943
944 /* Some controllers need this kick or reset won't work here */
945 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
946 unsigned int clock;
947
948 /* This is to force an update */
949 clock = host->clock;
950 host->clock = 0;
951 sdhci_set_clock(host, clock);
952 }
953
954 /* Spec says we should do both at the same time, but Ricoh
955 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800956 sdhci_reset(host, SDHCI_RESET_CMD);
957 sdhci_reset(host, SDHCI_RESET_DATA);
958 }
959
960 host->mrq = NULL;
961 host->cmd = NULL;
962 host->data = NULL;
963
964 sdhci_deactivate_led(host);
965
Pierre Ossman5f25a662006-10-04 02:15:39 -0700966 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967 spin_unlock_irqrestore(&host->lock, flags);
968
969 mmc_request_done(host->mmc, mrq);
970}
971
972static void sdhci_timeout_timer(unsigned long data)
973{
974 struct sdhci_host *host;
975 unsigned long flags;
976
977 host = (struct sdhci_host*)data;
978
979 spin_lock_irqsave(&host->lock, flags);
980
981 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100982 printk(KERN_ERR "%s: Timeout waiting for hardware "
983 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 sdhci_dumpregs(host);
985
986 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200987 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 sdhci_finish_data(host);
989 } else {
990 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +0200991 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200993 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994
995 tasklet_schedule(&host->finish_tasklet);
996 }
997 }
998
Pierre Ossman5f25a662006-10-04 02:15:39 -0700999 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000 spin_unlock_irqrestore(&host->lock, flags);
1001}
1002
1003/*****************************************************************************\
1004 * *
1005 * Interrupt handling *
1006 * *
1007\*****************************************************************************/
1008
1009static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1010{
1011 BUG_ON(intmask == 0);
1012
1013 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001014 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1015 "though no command operation was in progress.\n",
1016 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 sdhci_dumpregs(host);
1018 return;
1019 }
1020
Pierre Ossman43b58b32007-07-25 23:15:27 +02001021 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001022 host->cmd->error = -ETIMEDOUT;
1023 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1024 SDHCI_INT_INDEX))
1025 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
Pierre Ossman17b04292007-07-22 22:18:46 +02001027 if (host->cmd->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028 tasklet_schedule(&host->finish_tasklet);
Pierre Ossman43b58b32007-07-25 23:15:27 +02001029 else if (intmask & SDHCI_INT_RESPONSE)
1030 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031}
1032
1033static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1034{
1035 BUG_ON(intmask == 0);
1036
1037 if (!host->data) {
1038 /*
1039 * A data end interrupt is sent together with the response
1040 * for the stop command.
1041 */
1042 if (intmask & SDHCI_INT_DATA_END)
1043 return;
1044
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001045 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1046 "though no data operation was in progress.\n",
1047 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048 sdhci_dumpregs(host);
1049
1050 return;
1051 }
1052
1053 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001054 host->data->error = -ETIMEDOUT;
1055 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1056 host->data->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057
Pierre Ossman17b04292007-07-22 22:18:46 +02001058 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059 sdhci_finish_data(host);
1060 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001061 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062 sdhci_transfer_pio(host);
1063
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001064 /*
1065 * We currently don't do anything fancy with DMA
1066 * boundaries, but as we can't disable the feature
1067 * we need to at least restart the transfer.
1068 */
1069 if (intmask & SDHCI_INT_DMA_END)
1070 writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
1071 host->ioaddr + SDHCI_DMA_ADDRESS);
1072
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001073 if (intmask & SDHCI_INT_DATA_END) {
1074 if (host->cmd) {
1075 /*
1076 * Data managed to finish before the
1077 * command completed. Make sure we do
1078 * things in the proper order.
1079 */
1080 host->data_early = 1;
1081 } else {
1082 sdhci_finish_data(host);
1083 }
1084 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085 }
1086}
1087
David Howells7d12e782006-10-05 14:55:46 +01001088static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089{
1090 irqreturn_t result;
1091 struct sdhci_host* host = dev_id;
1092 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001093 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001094
1095 spin_lock(&host->lock);
1096
1097 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
1098
Mark Lord62df67a52007-03-06 13:30:13 +01001099 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100 result = IRQ_NONE;
1101 goto out;
1102 }
1103
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001104 DBG("*** %s got interrupt: 0x%08x\n",
1105 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001106
Pierre Ossman3192a282006-06-30 02:22:26 -07001107 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1108 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
1109 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001111 }
1112
1113 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001114
1115 if (intmask & SDHCI_INT_CMD_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001116 writel(intmask & SDHCI_INT_CMD_MASK,
1117 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001118 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119 }
1120
1121 if (intmask & SDHCI_INT_DATA_MASK) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122 writel(intmask & SDHCI_INT_DATA_MASK,
1123 host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001124 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125 }
1126
1127 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1128
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001129 intmask &= ~SDHCI_INT_ERROR;
1130
Pierre Ossmand129bce2006-03-24 03:18:17 -08001131 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001132 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133 mmc_hostname(host->mmc));
Pierre Ossman3192a282006-06-30 02:22:26 -07001134 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135 }
1136
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001137 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001138
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001139 if (intmask & SDHCI_INT_CARD_INT)
1140 cardint = 1;
1141
1142 intmask &= ~SDHCI_INT_CARD_INT;
1143
Pierre Ossman3192a282006-06-30 02:22:26 -07001144 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001145 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001146 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147 sdhci_dumpregs(host);
1148
Pierre Ossmand129bce2006-03-24 03:18:17 -08001149 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001150 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151
1152 result = IRQ_HANDLED;
1153
Pierre Ossman5f25a662006-10-04 02:15:39 -07001154 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001155out:
1156 spin_unlock(&host->lock);
1157
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001158 /*
1159 * We have to delay this as it calls back into the driver.
1160 */
1161 if (cardint)
1162 mmc_signal_sdio_irq(host->mmc);
1163
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164 return result;
1165}
1166
1167/*****************************************************************************\
1168 * *
1169 * Suspend/resume *
1170 * *
1171\*****************************************************************************/
1172
1173#ifdef CONFIG_PM
1174
1175static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1176{
1177 struct sdhci_chip *chip;
1178 int i, ret;
1179
1180 chip = pci_get_drvdata(pdev);
1181 if (!chip)
1182 return 0;
1183
1184 DBG("Suspending...\n");
1185
1186 for (i = 0;i < chip->num_slots;i++) {
1187 if (!chip->hosts[i])
1188 continue;
1189 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1190 if (ret) {
1191 for (i--;i >= 0;i--)
1192 mmc_resume_host(chip->hosts[i]->mmc);
1193 return ret;
1194 }
1195 }
1196
1197 pci_save_state(pdev);
1198 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001199
1200 for (i = 0;i < chip->num_slots;i++) {
1201 if (!chip->hosts[i])
1202 continue;
1203 free_irq(chip->hosts[i]->irq, chip->hosts[i]);
1204 }
1205
Pierre Ossmand129bce2006-03-24 03:18:17 -08001206 pci_disable_device(pdev);
1207 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1208
1209 return 0;
1210}
1211
1212static int sdhci_resume (struct pci_dev *pdev)
1213{
1214 struct sdhci_chip *chip;
1215 int i, ret;
1216
1217 chip = pci_get_drvdata(pdev);
1218 if (!chip)
1219 return 0;
1220
1221 DBG("Resuming...\n");
1222
1223 pci_set_power_state(pdev, PCI_D0);
1224 pci_restore_state(pdev);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001225 ret = pci_enable_device(pdev);
1226 if (ret)
1227 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228
1229 for (i = 0;i < chip->num_slots;i++) {
1230 if (!chip->hosts[i])
1231 continue;
1232 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1233 pci_set_master(pdev);
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001234 ret = request_irq(chip->hosts[i]->irq, sdhci_irq,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001235 IRQF_SHARED, mmc_hostname(chip->hosts[i]->mmc),
Pierre Ossmana715dfc2007-03-06 13:38:49 +01001236 chip->hosts[i]);
1237 if (ret)
1238 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239 sdhci_init(chip->hosts[i]);
Pierre Ossman5f25a662006-10-04 02:15:39 -07001240 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241 ret = mmc_resume_host(chip->hosts[i]->mmc);
1242 if (ret)
1243 return ret;
1244 }
1245
1246 return 0;
1247}
1248
1249#else /* CONFIG_PM */
1250
1251#define sdhci_suspend NULL
1252#define sdhci_resume NULL
1253
1254#endif /* CONFIG_PM */
1255
1256/*****************************************************************************\
1257 * *
1258 * Device probing/removal *
1259 * *
1260\*****************************************************************************/
1261
1262static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1263{
1264 int ret;
Pierre Ossman4a965502006-06-30 02:22:29 -07001265 unsigned int version;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266 struct sdhci_chip *chip;
1267 struct mmc_host *mmc;
1268 struct sdhci_host *host;
1269
1270 u8 first_bar;
1271 unsigned int caps;
1272
1273 chip = pci_get_drvdata(pdev);
1274 BUG_ON(!chip);
1275
1276 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1277 if (ret)
1278 return ret;
1279
1280 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1281
1282 if (first_bar > 5) {
1283 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1284 return -ENODEV;
1285 }
1286
1287 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1288 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1289 return -ENODEV;
1290 }
1291
1292 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
Pierre Ossmana98087c2006-12-07 19:17:20 +01001293 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
1294 "You may experience problems.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001295 }
1296
Pierre Ossman67435272006-06-30 02:22:31 -07001297 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1298 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1299 return -ENODEV;
1300 }
1301
1302 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1303 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1304 return -ENODEV;
1305 }
1306
Pierre Ossmand129bce2006-03-24 03:18:17 -08001307 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1308 if (!mmc)
1309 return -ENOMEM;
1310
1311 host = mmc_priv(mmc);
1312 host->mmc = mmc;
1313
Pierre Ossman8a4da142006-10-04 02:15:40 -07001314 host->chip = chip;
1315 chip->hosts[slot] = host;
1316
Pierre Ossmand129bce2006-03-24 03:18:17 -08001317 host->bar = first_bar + slot;
1318
1319 host->addr = pci_resource_start(pdev, host->bar);
1320 host->irq = pdev->irq;
1321
1322 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1323
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001324 ret = pci_request_region(pdev, host->bar, mmc_hostname(mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001325 if (ret)
1326 goto free;
1327
1328 host->ioaddr = ioremap_nocache(host->addr,
1329 pci_resource_len(pdev, host->bar));
1330 if (!host->ioaddr) {
1331 ret = -ENOMEM;
1332 goto release;
1333 }
1334
Pierre Ossmand96649e2006-06-30 02:22:30 -07001335 sdhci_reset(host, SDHCI_RESET_ALL);
1336
Pierre Ossman4a965502006-06-30 02:22:29 -07001337 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1338 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
Pierre Ossmanc6573c92007-12-02 19:46:49 +01001339 if (version > 1) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001340 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001341 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman4a965502006-06-30 02:22:29 -07001342 version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001343 }
1344
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1346
Pierre Ossmand6f8dee2007-09-30 12:47:05 +02001347 if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001348 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001349 else if (!(caps & SDHCI_CAN_DO_DMA))
1350 DBG("Controller doesn't have DMA capability\n");
1351 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352 host->flags |= SDHCI_USE_DMA;
1353
Feng Tang7c168e32007-09-30 12:44:18 +02001354 if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1355 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001356 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001357 host->flags &= ~SDHCI_USE_DMA;
1358 }
1359
Feng Tang56e71ef2007-09-29 14:15:05 +08001360 if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1361 (host->flags & SDHCI_USE_DMA)) {
1362 printk(KERN_WARNING "%s: Will use DMA "
1363 "mode even though HW doesn't fully "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001364 "claim to support it.\n", mmc_hostname(mmc));
Feng Tang56e71ef2007-09-29 14:15:05 +08001365 }
1366
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367 if (host->flags & SDHCI_USE_DMA) {
1368 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1369 printk(KERN_WARNING "%s: No suitable DMA available. "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001370 "Falling back to PIO.\n", mmc_hostname(mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371 host->flags &= ~SDHCI_USE_DMA;
1372 }
1373 }
1374
1375 if (host->flags & SDHCI_USE_DMA)
1376 pci_set_master(pdev);
1377 else /* XXX: Hack to get MMC layer to avoid highmem */
1378 pdev->dma_mask = 0;
1379
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001380 host->max_clk =
1381 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1382 if (host->max_clk == 0) {
1383 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001384 "frequency.\n", mmc_hostname(mmc));
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001385 ret = -ENODEV;
1386 goto unmap;
1387 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001388 host->max_clk *= 1000000;
1389
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001390 host->timeout_clk =
1391 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1392 if (host->timeout_clk == 0) {
1393 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001394 "frequency.\n", mmc_hostname(mmc));
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001395 ret = -ENODEV;
1396 goto unmap;
1397 }
1398 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1399 host->timeout_clk *= 1000;
1400
Pierre Ossmand129bce2006-03-24 03:18:17 -08001401 /*
1402 * Set host parameters.
1403 */
1404 mmc->ops = &sdhci_ops;
1405 mmc->f_min = host->max_clk / 256;
1406 mmc->f_max = host->max_clk;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001407 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001408
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001409 if (caps & SDHCI_CAN_DO_HISPD)
1410 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1411
Pierre Ossman146ad662006-06-30 02:22:23 -07001412 mmc->ocr_avail = 0;
1413 if (caps & SDHCI_CAN_VDD_330)
1414 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001415 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001416 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001417 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001418 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001419
1420 if (mmc->ocr_avail == 0) {
1421 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001422 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossman146ad662006-06-30 02:22:23 -07001423 ret = -ENODEV;
1424 goto unmap;
1425 }
1426
Pierre Ossmand129bce2006-03-24 03:18:17 -08001427 spin_lock_init(&host->lock);
1428
1429 /*
1430 * Maximum number of segments. Hardware cannot do scatter lists.
1431 */
1432 if (host->flags & SDHCI_USE_DMA)
1433 mmc->max_hw_segs = 1;
1434 else
1435 mmc->max_hw_segs = 16;
1436 mmc->max_phys_segs = 16;
1437
1438 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001439 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001440 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001441 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001442 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001443
1444 /*
1445 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman55db8902006-11-21 17:55:45 +01001446 * of bytes.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001448 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449
1450 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001451 * Maximum block size. This varies from controller to controller and
1452 * is specified in the capabilities register.
1453 */
1454 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1455 if (mmc->max_blk_size >= 3) {
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001456 printk(KERN_WARNING "%s: Invalid maximum block size, "
1457 "assuming 512 bytes\n", mmc_hostname(mmc));
David Vrabel03f85902007-08-10 13:25:03 +01001458 mmc->max_blk_size = 512;
1459 } else
1460 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001461
1462 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001463 * Maximum block count.
1464 */
1465 mmc->max_blk_count = 65535;
1466
1467 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001468 * Init tasklets.
1469 */
1470 tasklet_init(&host->card_tasklet,
1471 sdhci_tasklet_card, (unsigned long)host);
1472 tasklet_init(&host->finish_tasklet,
1473 sdhci_tasklet_finish, (unsigned long)host);
1474
Al Viroe4cad1b2006-10-10 22:47:07 +01001475 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001477 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001478 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001479 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001480 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001481
1482 sdhci_init(host);
1483
1484#ifdef CONFIG_MMC_DEBUG
1485 sdhci_dumpregs(host);
1486#endif
1487
Pierre Ossman5f25a662006-10-04 02:15:39 -07001488 mmiowb();
1489
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490 mmc_add_host(mmc);
1491
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001492 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n",
1493 mmc_hostname(mmc), host->addr, host->irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1495
1496 return 0;
1497
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001498untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499 tasklet_kill(&host->card_tasklet);
1500 tasklet_kill(&host->finish_tasklet);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001501unmap:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502 iounmap(host->ioaddr);
1503release:
1504 pci_release_region(pdev, host->bar);
1505free:
1506 mmc_free_host(mmc);
1507
1508 return ret;
1509}
1510
1511static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1512{
1513 struct sdhci_chip *chip;
1514 struct mmc_host *mmc;
1515 struct sdhci_host *host;
1516
1517 chip = pci_get_drvdata(pdev);
1518 host = chip->hosts[slot];
1519 mmc = host->mmc;
1520
1521 chip->hosts[slot] = NULL;
1522
1523 mmc_remove_host(mmc);
1524
1525 sdhci_reset(host, SDHCI_RESET_ALL);
1526
1527 free_irq(host->irq, host);
1528
1529 del_timer_sync(&host->timer);
1530
1531 tasklet_kill(&host->card_tasklet);
1532 tasklet_kill(&host->finish_tasklet);
1533
1534 iounmap(host->ioaddr);
1535
1536 pci_release_region(pdev, host->bar);
1537
1538 mmc_free_host(mmc);
1539}
1540
1541static int __devinit sdhci_probe(struct pci_dev *pdev,
1542 const struct pci_device_id *ent)
1543{
1544 int ret, i;
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001545 u8 slots, rev;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001546 struct sdhci_chip *chip;
1547
1548 BUG_ON(pdev == NULL);
1549 BUG_ON(ent == NULL);
1550
Pierre Ossman51f82bc2006-06-30 02:22:22 -07001551 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1552
1553 printk(KERN_INFO DRIVER_NAME
1554 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1555 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1556 (int)rev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001557
1558 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1559 if (ret)
1560 return ret;
1561
1562 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1563 DBG("found %d slot(s)\n", slots);
1564 if (slots == 0)
1565 return -ENODEV;
1566
1567 ret = pci_enable_device(pdev);
1568 if (ret)
1569 return ret;
1570
1571 chip = kzalloc(sizeof(struct sdhci_chip) +
1572 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1573 if (!chip) {
1574 ret = -ENOMEM;
1575 goto err;
1576 }
1577
1578 chip->pdev = pdev;
Pierre Ossmandf673b22006-06-30 02:22:31 -07001579 chip->quirks = ent->driver_data;
1580
1581 if (debug_quirks)
1582 chip->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583
1584 chip->num_slots = slots;
1585 pci_set_drvdata(pdev, chip);
1586
1587 for (i = 0;i < slots;i++) {
1588 ret = sdhci_probe_slot(pdev, i);
1589 if (ret) {
1590 for (i--;i >= 0;i--)
1591 sdhci_remove_slot(pdev, i);
1592 goto free;
1593 }
1594 }
1595
1596 return 0;
1597
1598free:
1599 pci_set_drvdata(pdev, NULL);
1600 kfree(chip);
1601
1602err:
1603 pci_disable_device(pdev);
1604 return ret;
1605}
1606
1607static void __devexit sdhci_remove(struct pci_dev *pdev)
1608{
1609 int i;
1610 struct sdhci_chip *chip;
1611
1612 chip = pci_get_drvdata(pdev);
1613
1614 if (chip) {
1615 for (i = 0;i < chip->num_slots;i++)
1616 sdhci_remove_slot(pdev, i);
1617
1618 pci_set_drvdata(pdev, NULL);
1619
1620 kfree(chip);
1621 }
1622
1623 pci_disable_device(pdev);
1624}
1625
1626static struct pci_driver sdhci_driver = {
1627 .name = DRIVER_NAME,
1628 .id_table = pci_ids,
1629 .probe = sdhci_probe,
1630 .remove = __devexit_p(sdhci_remove),
1631 .suspend = sdhci_suspend,
1632 .resume = sdhci_resume,
1633};
1634
1635/*****************************************************************************\
1636 * *
1637 * Driver init/exit *
1638 * *
1639\*****************************************************************************/
1640
1641static int __init sdhci_drv_init(void)
1642{
1643 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001644 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001645 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1646
1647 return pci_register_driver(&sdhci_driver);
1648}
1649
1650static void __exit sdhci_drv_exit(void)
1651{
1652 DBG("Exiting\n");
1653
1654 pci_unregister_driver(&sdhci_driver);
1655}
1656
1657module_init(sdhci_drv_init);
1658module_exit(sdhci_drv_exit);
1659
Pierre Ossmandf673b22006-06-30 02:22:31 -07001660module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001661
Pierre Ossmand129bce2006-03-24 03:18:17 -08001662MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1663MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001664MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001665
Pierre Ossmandf673b22006-06-30 02:22:31 -07001666MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");