[MMC] sdhci: fix timeout loops in sdhci
[deliverable/linux.git] / drivers / mmc / sdhci.c
1 /*
2 * linux/drivers/mmc/sdhci.c - Secure Digital Host Controller Interface driver
3 *
4 * Copyright (C) 2005-2006 Pierre Ossman, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 /*
12 * Note that PIO transfer is rather crappy atm. The buffer full/empty
13 * interrupts aren't reliable so we currently transfer the entire buffer
14 * directly. Patches to solve the problem are welcome.
15 */
16
17 #include <linux/delay.h>
18 #include <linux/highmem.h>
19 #include <linux/pci.h>
20 #include <linux/dma-mapping.h>
21
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/protocol.h>
24
25 #include <asm/scatterlist.h>
26
27 #include "sdhci.h"
28
29 #define DRIVER_NAME "sdhci"
30 #define DRIVER_VERSION "0.11"
31
32 #define BUGMAIL "<sdhci-devel@list.drzeus.cx>"
33
34 #define DBG(f, x...) \
35 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
36
37 static const struct pci_device_id pci_ids[] __devinitdata = {
38 /* handle any SD host controller */
39 {PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)},
40 { /* end: all zeroes */ },
41 };
42
43 MODULE_DEVICE_TABLE(pci, pci_ids);
44
45 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
46 static void sdhci_finish_data(struct sdhci_host *);
47
48 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
49 static void sdhci_finish_command(struct sdhci_host *);
50
51 static void sdhci_dumpregs(struct sdhci_host *host)
52 {
53 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
54
55 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
56 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
57 readw(host->ioaddr + SDHCI_HOST_VERSION));
58 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
59 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
60 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
61 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
62 readl(host->ioaddr + SDHCI_ARGUMENT),
63 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
64 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
65 readl(host->ioaddr + SDHCI_PRESENT_STATE),
66 readb(host->ioaddr + SDHCI_HOST_CONTROL));
67 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
68 readb(host->ioaddr + SDHCI_POWER_CONTROL),
69 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
70 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
71 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
72 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
73 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
74 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
75 readl(host->ioaddr + SDHCI_INT_STATUS));
76 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
77 readl(host->ioaddr + SDHCI_INT_ENABLE),
78 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
79 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
80 readw(host->ioaddr + SDHCI_ACMD12_ERR),
81 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
82 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
83 readl(host->ioaddr + SDHCI_CAPABILITIES),
84 readl(host->ioaddr + SDHCI_MAX_CURRENT));
85
86 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
87 }
88
89 /*****************************************************************************\
90 * *
91 * Low level functions *
92 * *
93 \*****************************************************************************/
94
95 static void sdhci_reset(struct sdhci_host *host, u8 mask)
96 {
97 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
98
99 if (mask & SDHCI_RESET_ALL) {
100 host->clock = 0;
101
102 mdelay(50);
103 }
104 }
105
106 static void sdhci_init(struct sdhci_host *host)
107 {
108 u32 intmask;
109
110 sdhci_reset(host, SDHCI_RESET_ALL);
111
112 intmask = ~(SDHCI_INT_CARD_INT | SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
113
114 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
115 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
116
117 /* This is unknown magic. */
118 writeb(0xE, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
119 }
120
121 static void sdhci_activate_led(struct sdhci_host *host)
122 {
123 u8 ctrl;
124
125 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
126 ctrl |= SDHCI_CTRL_LED;
127 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
128 }
129
130 static void sdhci_deactivate_led(struct sdhci_host *host)
131 {
132 u8 ctrl;
133
134 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
135 ctrl &= ~SDHCI_CTRL_LED;
136 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
137 }
138
139 /*****************************************************************************\
140 * *
141 * Core functions *
142 * *
143 \*****************************************************************************/
144
145 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
146 {
147 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
148 return host->mapped_sg + host->cur_sg->offset;
149 }
150
151 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
152 {
153 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
154 }
155
156 static inline int sdhci_next_sg(struct sdhci_host* host)
157 {
158 /*
159 * Skip to next SG entry.
160 */
161 host->cur_sg++;
162 host->num_sg--;
163
164 /*
165 * Any entries left?
166 */
167 if (host->num_sg > 0) {
168 host->offset = 0;
169 host->remain = host->cur_sg->length;
170 }
171
172 return host->num_sg;
173 }
174
175 static void sdhci_transfer_pio(struct sdhci_host *host)
176 {
177 char *buffer;
178 u32 mask;
179 int bytes, size;
180 unsigned long max_jiffies;
181
182 BUG_ON(!host->data);
183
184 if (host->num_sg == 0)
185 return;
186
187 bytes = 0;
188 if (host->data->flags & MMC_DATA_READ)
189 mask = SDHCI_DATA_AVAILABLE;
190 else
191 mask = SDHCI_SPACE_AVAILABLE;
192
193 buffer = sdhci_kmap_sg(host) + host->offset;
194
195 /* Transfer shouldn't take more than 5 s */
196 max_jiffies = jiffies + HZ * 5;
197
198 while (host->size > 0) {
199 if (time_after(jiffies, max_jiffies)) {
200 printk(KERN_ERR "%s: PIO transfer stalled. "
201 "Please report this to "
202 BUGMAIL ".\n", mmc_hostname(host->mmc));
203 sdhci_dumpregs(host);
204
205 sdhci_kunmap_sg(host);
206
207 host->data->error = MMC_ERR_FAILED;
208 sdhci_finish_data(host);
209 return;
210 }
211
212 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask))
213 continue;
214
215 size = min(host->size, host->remain);
216
217 if (size >= 4) {
218 if (host->data->flags & MMC_DATA_READ)
219 *(u32*)buffer = readl(host->ioaddr + SDHCI_BUFFER);
220 else
221 writel(*(u32*)buffer, host->ioaddr + SDHCI_BUFFER);
222 size = 4;
223 } else if (size >= 2) {
224 if (host->data->flags & MMC_DATA_READ)
225 *(u16*)buffer = readw(host->ioaddr + SDHCI_BUFFER);
226 else
227 writew(*(u16*)buffer, host->ioaddr + SDHCI_BUFFER);
228 size = 2;
229 } else {
230 if (host->data->flags & MMC_DATA_READ)
231 *(u8*)buffer = readb(host->ioaddr + SDHCI_BUFFER);
232 else
233 writeb(*(u8*)buffer, host->ioaddr + SDHCI_BUFFER);
234 size = 1;
235 }
236
237 buffer += size;
238 host->offset += size;
239 host->remain -= size;
240
241 bytes += size;
242 host->size -= size;
243
244 if (host->remain == 0) {
245 sdhci_kunmap_sg(host);
246 if (sdhci_next_sg(host) == 0) {
247 DBG("PIO transfer: %d bytes\n", bytes);
248 return;
249 }
250 buffer = sdhci_kmap_sg(host);
251 }
252 }
253
254 sdhci_kunmap_sg(host);
255
256 DBG("PIO transfer: %d bytes\n", bytes);
257 }
258
259 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
260 {
261 u16 mode;
262
263 WARN_ON(host->data);
264
265 if (data == NULL) {
266 writew(0, host->ioaddr + SDHCI_TRANSFER_MODE);
267 return;
268 }
269
270 DBG("blksz %04x blks %04x flags %08x\n",
271 data->blksz, data->blocks, data->flags);
272 DBG("tsac %d ms nsac %d clk\n",
273 data->timeout_ns / 1000000, data->timeout_clks);
274
275 mode = SDHCI_TRNS_BLK_CNT_EN;
276 if (data->blocks > 1)
277 mode |= SDHCI_TRNS_MULTI;
278 if (data->flags & MMC_DATA_READ)
279 mode |= SDHCI_TRNS_READ;
280 if (host->flags & SDHCI_USE_DMA)
281 mode |= SDHCI_TRNS_DMA;
282
283 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
284
285 writew(data->blksz, host->ioaddr + SDHCI_BLOCK_SIZE);
286 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
287
288 if (host->flags & SDHCI_USE_DMA) {
289 int count;
290
291 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
292 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
293 BUG_ON(count != 1);
294
295 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
296 } else {
297 host->size = data->blksz * data->blocks;
298
299 host->cur_sg = data->sg;
300 host->num_sg = data->sg_len;
301
302 host->offset = 0;
303 host->remain = host->cur_sg->length;
304 }
305 }
306
307 static void sdhci_finish_data(struct sdhci_host *host)
308 {
309 struct mmc_data *data;
310 u32 intmask;
311 u16 blocks;
312
313 BUG_ON(!host->data);
314
315 data = host->data;
316 host->data = NULL;
317
318 if (host->flags & SDHCI_USE_DMA) {
319 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
320 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
321 } else {
322 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
323 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
324 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
325
326 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
327 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
328 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
329 }
330
331 /*
332 * Controller doesn't count down when in single block mode.
333 */
334 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
335 blocks = 0;
336 else
337 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
338 data->bytes_xfered = data->blksz * (data->blocks - blocks);
339
340 if ((data->error == MMC_ERR_NONE) && blocks) {
341 printk(KERN_ERR "%s: Controller signalled completion even "
342 "though there were blocks left. Please report this "
343 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
344 data->error = MMC_ERR_FAILED;
345 }
346
347 if (host->size != 0) {
348 printk(KERN_ERR "%s: %d bytes were left untransferred. "
349 "Please report this to " BUGMAIL ".\n",
350 mmc_hostname(host->mmc), host->size);
351 data->error = MMC_ERR_FAILED;
352 }
353
354 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
355
356 if (data->stop) {
357 /*
358 * The controller needs a reset of internal state machines
359 * upon error conditions.
360 */
361 if (data->error != MMC_ERR_NONE) {
362 sdhci_reset(host, SDHCI_RESET_CMD);
363 sdhci_reset(host, SDHCI_RESET_DATA);
364 }
365
366 sdhci_send_command(host, data->stop);
367 } else
368 tasklet_schedule(&host->finish_tasklet);
369 }
370
371 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
372 {
373 int flags;
374 unsigned long timeout;
375
376 WARN_ON(host->cmd);
377
378 DBG("Sending cmd (%x)\n", cmd->opcode);
379
380 /* Wait max 10 ms */
381 timeout = 10;
382 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) &
383 (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
384 if (timeout == 0) {
385 printk(KERN_ERR "%s: Controller never released "
386 "inhibit bits. Please report this to "
387 BUGMAIL ".\n", mmc_hostname(host->mmc));
388 sdhci_dumpregs(host);
389 cmd->error = MMC_ERR_FAILED;
390 tasklet_schedule(&host->finish_tasklet);
391 return;
392 }
393 timeout--;
394 mdelay(1);
395 }
396
397 mod_timer(&host->timer, jiffies + 10 * HZ);
398
399 host->cmd = cmd;
400
401 sdhci_prepare_data(host, cmd->data);
402
403 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
404
405 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
406 printk(KERN_ERR "%s: Unsupported response type! "
407 "Please report this to " BUGMAIL ".\n",
408 mmc_hostname(host->mmc));
409 cmd->error = MMC_ERR_INVALID;
410 tasklet_schedule(&host->finish_tasklet);
411 return;
412 }
413
414 if (!(cmd->flags & MMC_RSP_PRESENT))
415 flags = SDHCI_CMD_RESP_NONE;
416 else if (cmd->flags & MMC_RSP_136)
417 flags = SDHCI_CMD_RESP_LONG;
418 else if (cmd->flags & MMC_RSP_BUSY)
419 flags = SDHCI_CMD_RESP_SHORT_BUSY;
420 else
421 flags = SDHCI_CMD_RESP_SHORT;
422
423 if (cmd->flags & MMC_RSP_CRC)
424 flags |= SDHCI_CMD_CRC;
425 if (cmd->flags & MMC_RSP_OPCODE)
426 flags |= SDHCI_CMD_INDEX;
427 if (cmd->data)
428 flags |= SDHCI_CMD_DATA;
429
430 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
431 host->ioaddr + SDHCI_COMMAND);
432 }
433
434 static void sdhci_finish_command(struct sdhci_host *host)
435 {
436 int i;
437
438 BUG_ON(host->cmd == NULL);
439
440 if (host->cmd->flags & MMC_RSP_PRESENT) {
441 if (host->cmd->flags & MMC_RSP_136) {
442 /* CRC is stripped so we need to do some shifting. */
443 for (i = 0;i < 4;i++) {
444 host->cmd->resp[i] = readl(host->ioaddr +
445 SDHCI_RESPONSE + (3-i)*4) << 8;
446 if (i != 3)
447 host->cmd->resp[i] |=
448 readb(host->ioaddr +
449 SDHCI_RESPONSE + (3-i)*4-1);
450 }
451 } else {
452 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
453 }
454 }
455
456 host->cmd->error = MMC_ERR_NONE;
457
458 DBG("Ending cmd (%x)\n", host->cmd->opcode);
459
460 if (host->cmd->data) {
461 u32 intmask;
462
463 host->data = host->cmd->data;
464
465 if (!(host->flags & SDHCI_USE_DMA)) {
466 /*
467 * Don't enable the interrupts until now to make sure we
468 * get stable handling of the FIFO.
469 */
470 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
471 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
472 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
473
474 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
475 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
476 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
477
478 /*
479 * The buffer interrupts are to unreliable so we
480 * start the transfer immediatly.
481 */
482 sdhci_transfer_pio(host);
483 }
484 } else
485 tasklet_schedule(&host->finish_tasklet);
486
487 host->cmd = NULL;
488 }
489
490 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
491 {
492 int div;
493 u16 clk;
494 unsigned long timeout;
495
496 if (clock == host->clock)
497 return;
498
499 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
500
501 if (clock == 0)
502 goto out;
503
504 for (div = 1;div < 256;div *= 2) {
505 if ((host->max_clk / div) <= clock)
506 break;
507 }
508 div >>= 1;
509
510 clk = div << SDHCI_DIVIDER_SHIFT;
511 clk |= SDHCI_CLOCK_INT_EN;
512 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
513
514 /* Wait max 10 ms */
515 timeout = 10;
516 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
517 & SDHCI_CLOCK_INT_STABLE)) {
518 if (timeout == 0) {
519 printk(KERN_ERR "%s: Internal clock never stabilised. "
520 "Please report this to " BUGMAIL ".\n",
521 mmc_hostname(host->mmc));
522 sdhci_dumpregs(host);
523 return;
524 }
525 timeout--;
526 mdelay(1);
527 }
528
529 clk |= SDHCI_CLOCK_CARD_EN;
530 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
531
532 out:
533 host->clock = clock;
534 }
535
536 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
537 {
538 u8 pwr;
539
540 if (host->power == power)
541 return;
542
543 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
544
545 if (power == (unsigned short)-1)
546 goto out;
547
548 pwr = SDHCI_POWER_ON;
549
550 switch (power) {
551 case MMC_VDD_170:
552 case MMC_VDD_180:
553 case MMC_VDD_190:
554 pwr |= SDHCI_POWER_180;
555 break;
556 case MMC_VDD_290:
557 case MMC_VDD_300:
558 case MMC_VDD_310:
559 pwr |= SDHCI_POWER_300;
560 break;
561 case MMC_VDD_320:
562 case MMC_VDD_330:
563 case MMC_VDD_340:
564 pwr |= SDHCI_POWER_330;
565 break;
566 default:
567 BUG();
568 }
569
570 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
571
572 out:
573 host->power = power;
574 }
575
576 /*****************************************************************************\
577 * *
578 * MMC callbacks *
579 * *
580 \*****************************************************************************/
581
582 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
583 {
584 struct sdhci_host *host;
585 unsigned long flags;
586
587 host = mmc_priv(mmc);
588
589 spin_lock_irqsave(&host->lock, flags);
590
591 WARN_ON(host->mrq != NULL);
592
593 sdhci_activate_led(host);
594
595 host->mrq = mrq;
596
597 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
598 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
599 tasklet_schedule(&host->finish_tasklet);
600 } else
601 sdhci_send_command(host, mrq->cmd);
602
603 spin_unlock_irqrestore(&host->lock, flags);
604 }
605
606 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
607 {
608 struct sdhci_host *host;
609 unsigned long flags;
610 u8 ctrl;
611
612 host = mmc_priv(mmc);
613
614 spin_lock_irqsave(&host->lock, flags);
615
616 /*
617 * Reset the chip on each power off.
618 * Should clear out any weird states.
619 */
620 if (ios->power_mode == MMC_POWER_OFF) {
621 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
622 spin_unlock_irqrestore(&host->lock, flags);
623 sdhci_init(host);
624 spin_lock_irqsave(&host->lock, flags);
625 }
626
627 sdhci_set_clock(host, ios->clock);
628
629 if (ios->power_mode == MMC_POWER_OFF)
630 sdhci_set_power(host, -1);
631 else
632 sdhci_set_power(host, ios->vdd);
633
634 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
635 if (ios->bus_width == MMC_BUS_WIDTH_4)
636 ctrl |= SDHCI_CTRL_4BITBUS;
637 else
638 ctrl &= ~SDHCI_CTRL_4BITBUS;
639 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
640
641 spin_unlock_irqrestore(&host->lock, flags);
642 }
643
644 static int sdhci_get_ro(struct mmc_host *mmc)
645 {
646 struct sdhci_host *host;
647 unsigned long flags;
648 int present;
649
650 host = mmc_priv(mmc);
651
652 spin_lock_irqsave(&host->lock, flags);
653
654 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
655
656 spin_unlock_irqrestore(&host->lock, flags);
657
658 return !(present & SDHCI_WRITE_PROTECT);
659 }
660
661 static struct mmc_host_ops sdhci_ops = {
662 .request = sdhci_request,
663 .set_ios = sdhci_set_ios,
664 .get_ro = sdhci_get_ro,
665 };
666
667 /*****************************************************************************\
668 * *
669 * Tasklets *
670 * *
671 \*****************************************************************************/
672
673 static void sdhci_tasklet_card(unsigned long param)
674 {
675 struct sdhci_host *host;
676 unsigned long flags;
677
678 host = (struct sdhci_host*)param;
679
680 spin_lock_irqsave(&host->lock, flags);
681
682 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
683 if (host->mrq) {
684 printk(KERN_ERR "%s: Card removed during transfer!\n",
685 mmc_hostname(host->mmc));
686 printk(KERN_ERR "%s: Resetting controller.\n",
687 mmc_hostname(host->mmc));
688
689 sdhci_reset(host, SDHCI_RESET_CMD);
690 sdhci_reset(host, SDHCI_RESET_DATA);
691
692 host->mrq->cmd->error = MMC_ERR_FAILED;
693 tasklet_schedule(&host->finish_tasklet);
694 }
695 }
696
697 spin_unlock_irqrestore(&host->lock, flags);
698
699 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
700 }
701
702 static void sdhci_tasklet_finish(unsigned long param)
703 {
704 struct sdhci_host *host;
705 unsigned long flags;
706 struct mmc_request *mrq;
707
708 host = (struct sdhci_host*)param;
709
710 spin_lock_irqsave(&host->lock, flags);
711
712 del_timer(&host->timer);
713
714 mrq = host->mrq;
715
716 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
717
718 /*
719 * The controller needs a reset of internal state machines
720 * upon error conditions.
721 */
722 if ((mrq->cmd->error != MMC_ERR_NONE) ||
723 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
724 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
725 sdhci_reset(host, SDHCI_RESET_CMD);
726 sdhci_reset(host, SDHCI_RESET_DATA);
727 }
728
729 host->mrq = NULL;
730 host->cmd = NULL;
731 host->data = NULL;
732
733 sdhci_deactivate_led(host);
734
735 spin_unlock_irqrestore(&host->lock, flags);
736
737 mmc_request_done(host->mmc, mrq);
738 }
739
740 static void sdhci_timeout_timer(unsigned long data)
741 {
742 struct sdhci_host *host;
743 unsigned long flags;
744
745 host = (struct sdhci_host*)data;
746
747 spin_lock_irqsave(&host->lock, flags);
748
749 if (host->mrq) {
750 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
751 "Please report this to " BUGMAIL ".\n",
752 mmc_hostname(host->mmc));
753 sdhci_dumpregs(host);
754
755 if (host->data) {
756 host->data->error = MMC_ERR_TIMEOUT;
757 sdhci_finish_data(host);
758 } else {
759 if (host->cmd)
760 host->cmd->error = MMC_ERR_TIMEOUT;
761 else
762 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
763
764 tasklet_schedule(&host->finish_tasklet);
765 }
766 }
767
768 spin_unlock_irqrestore(&host->lock, flags);
769 }
770
771 /*****************************************************************************\
772 * *
773 * Interrupt handling *
774 * *
775 \*****************************************************************************/
776
777 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
778 {
779 BUG_ON(intmask == 0);
780
781 if (!host->cmd) {
782 printk(KERN_ERR "%s: Got command interrupt even though no "
783 "command operation was in progress.\n",
784 mmc_hostname(host->mmc));
785 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
786 mmc_hostname(host->mmc));
787 sdhci_dumpregs(host);
788 return;
789 }
790
791 if (intmask & SDHCI_INT_RESPONSE)
792 sdhci_finish_command(host);
793 else {
794 if (intmask & SDHCI_INT_TIMEOUT)
795 host->cmd->error = MMC_ERR_TIMEOUT;
796 else if (intmask & SDHCI_INT_CRC)
797 host->cmd->error = MMC_ERR_BADCRC;
798 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
799 host->cmd->error = MMC_ERR_FAILED;
800 else
801 host->cmd->error = MMC_ERR_INVALID;
802
803 tasklet_schedule(&host->finish_tasklet);
804 }
805 }
806
807 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
808 {
809 BUG_ON(intmask == 0);
810
811 if (!host->data) {
812 /*
813 * A data end interrupt is sent together with the response
814 * for the stop command.
815 */
816 if (intmask & SDHCI_INT_DATA_END)
817 return;
818
819 printk(KERN_ERR "%s: Got data interrupt even though no "
820 "data operation was in progress.\n",
821 mmc_hostname(host->mmc));
822 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
823 mmc_hostname(host->mmc));
824 sdhci_dumpregs(host);
825
826 return;
827 }
828
829 if (intmask & SDHCI_INT_DATA_TIMEOUT)
830 host->data->error = MMC_ERR_TIMEOUT;
831 else if (intmask & SDHCI_INT_DATA_CRC)
832 host->data->error = MMC_ERR_BADCRC;
833 else if (intmask & SDHCI_INT_DATA_END_BIT)
834 host->data->error = MMC_ERR_FAILED;
835
836 if (host->data->error != MMC_ERR_NONE)
837 sdhci_finish_data(host);
838 else {
839 if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY))
840 sdhci_transfer_pio(host);
841
842 if (intmask & SDHCI_INT_DATA_END)
843 sdhci_finish_data(host);
844 }
845 }
846
847 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
848 {
849 irqreturn_t result;
850 struct sdhci_host* host = dev_id;
851 u32 intmask;
852
853 spin_lock(&host->lock);
854
855 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
856
857 if (!intmask) {
858 result = IRQ_NONE;
859 goto out;
860 }
861
862 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
863
864 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE))
865 tasklet_schedule(&host->card_tasklet);
866
867 if (intmask & SDHCI_INT_CMD_MASK) {
868 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
869
870 writel(intmask & SDHCI_INT_CMD_MASK,
871 host->ioaddr + SDHCI_INT_STATUS);
872 }
873
874 if (intmask & SDHCI_INT_DATA_MASK) {
875 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
876
877 writel(intmask & SDHCI_INT_DATA_MASK,
878 host->ioaddr + SDHCI_INT_STATUS);
879 }
880
881 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
882
883 if (intmask & SDHCI_INT_CARD_INT) {
884 printk(KERN_ERR "%s: Unexpected card interrupt. Please "
885 "report this to " BUGMAIL ".\n",
886 mmc_hostname(host->mmc));
887 sdhci_dumpregs(host);
888 }
889
890 if (intmask & SDHCI_INT_BUS_POWER) {
891 printk(KERN_ERR "%s: Unexpected bus power interrupt. Please "
892 "report this to " BUGMAIL ".\n",
893 mmc_hostname(host->mmc));
894 sdhci_dumpregs(host);
895 }
896
897 if (intmask & SDHCI_INT_ACMD12ERR) {
898 printk(KERN_ERR "%s: Unexpected auto CMD12 error. Please "
899 "report this to " BUGMAIL ".\n",
900 mmc_hostname(host->mmc));
901 sdhci_dumpregs(host);
902
903 writew(~0, host->ioaddr + SDHCI_ACMD12_ERR);
904 }
905
906 if (intmask)
907 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
908
909 result = IRQ_HANDLED;
910
911 out:
912 spin_unlock(&host->lock);
913
914 return result;
915 }
916
917 /*****************************************************************************\
918 * *
919 * Suspend/resume *
920 * *
921 \*****************************************************************************/
922
923 #ifdef CONFIG_PM
924
925 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
926 {
927 struct sdhci_chip *chip;
928 int i, ret;
929
930 chip = pci_get_drvdata(pdev);
931 if (!chip)
932 return 0;
933
934 DBG("Suspending...\n");
935
936 for (i = 0;i < chip->num_slots;i++) {
937 if (!chip->hosts[i])
938 continue;
939 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
940 if (ret) {
941 for (i--;i >= 0;i--)
942 mmc_resume_host(chip->hosts[i]->mmc);
943 return ret;
944 }
945 }
946
947 pci_save_state(pdev);
948 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
949 pci_disable_device(pdev);
950 pci_set_power_state(pdev, pci_choose_state(pdev, state));
951
952 return 0;
953 }
954
955 static int sdhci_resume (struct pci_dev *pdev)
956 {
957 struct sdhci_chip *chip;
958 int i, ret;
959
960 chip = pci_get_drvdata(pdev);
961 if (!chip)
962 return 0;
963
964 DBG("Resuming...\n");
965
966 pci_set_power_state(pdev, PCI_D0);
967 pci_restore_state(pdev);
968 pci_enable_device(pdev);
969
970 for (i = 0;i < chip->num_slots;i++) {
971 if (!chip->hosts[i])
972 continue;
973 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
974 pci_set_master(pdev);
975 sdhci_init(chip->hosts[i]);
976 ret = mmc_resume_host(chip->hosts[i]->mmc);
977 if (ret)
978 return ret;
979 }
980
981 return 0;
982 }
983
984 #else /* CONFIG_PM */
985
986 #define sdhci_suspend NULL
987 #define sdhci_resume NULL
988
989 #endif /* CONFIG_PM */
990
991 /*****************************************************************************\
992 * *
993 * Device probing/removal *
994 * *
995 \*****************************************************************************/
996
997 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
998 {
999 int ret;
1000 struct sdhci_chip *chip;
1001 struct mmc_host *mmc;
1002 struct sdhci_host *host;
1003
1004 u8 first_bar;
1005 unsigned int caps;
1006
1007 chip = pci_get_drvdata(pdev);
1008 BUG_ON(!chip);
1009
1010 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1011 if (ret)
1012 return ret;
1013
1014 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1015
1016 if (first_bar > 5) {
1017 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1018 return -ENODEV;
1019 }
1020
1021 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1022 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1023 return -ENODEV;
1024 }
1025
1026 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1027 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
1028 return -ENODEV;
1029 }
1030
1031 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1032 if (!mmc)
1033 return -ENOMEM;
1034
1035 host = mmc_priv(mmc);
1036 host->mmc = mmc;
1037
1038 host->bar = first_bar + slot;
1039
1040 host->addr = pci_resource_start(pdev, host->bar);
1041 host->irq = pdev->irq;
1042
1043 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1044
1045 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1046
1047 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1048 if (ret)
1049 goto free;
1050
1051 host->ioaddr = ioremap_nocache(host->addr,
1052 pci_resource_len(pdev, host->bar));
1053 if (!host->ioaddr) {
1054 ret = -ENOMEM;
1055 goto release;
1056 }
1057
1058 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1059
1060 if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1061 host->flags |= SDHCI_USE_DMA;
1062
1063 if (host->flags & SDHCI_USE_DMA) {
1064 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1065 printk(KERN_WARNING "%s: No suitable DMA available. "
1066 "Falling back to PIO.\n", host->slot_descr);
1067 host->flags &= ~SDHCI_USE_DMA;
1068 }
1069 }
1070
1071 if (host->flags & SDHCI_USE_DMA)
1072 pci_set_master(pdev);
1073 else /* XXX: Hack to get MMC layer to avoid highmem */
1074 pdev->dma_mask = 0;
1075
1076 host->max_clk =
1077 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1078 if (host->max_clk == 0) {
1079 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1080 "frequency.\n", host->slot_descr);
1081 ret = -ENODEV;
1082 goto unmap;
1083 }
1084 host->max_clk *= 1000000;
1085
1086 /*
1087 * Set host parameters.
1088 */
1089 mmc->ops = &sdhci_ops;
1090 mmc->f_min = host->max_clk / 256;
1091 mmc->f_max = host->max_clk;
1092 mmc->caps = MMC_CAP_4_BIT_DATA;
1093
1094 mmc->ocr_avail = 0;
1095 if (caps & SDHCI_CAN_VDD_330)
1096 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1097 else if (caps & SDHCI_CAN_VDD_300)
1098 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1099 else if (caps & SDHCI_CAN_VDD_180)
1100 mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
1101
1102 if (mmc->ocr_avail == 0) {
1103 printk(KERN_ERR "%s: Hardware doesn't report any "
1104 "support voltages.\n", host->slot_descr);
1105 ret = -ENODEV;
1106 goto unmap;
1107 }
1108
1109 spin_lock_init(&host->lock);
1110
1111 /*
1112 * Maximum number of segments. Hardware cannot do scatter lists.
1113 */
1114 if (host->flags & SDHCI_USE_DMA)
1115 mmc->max_hw_segs = 1;
1116 else
1117 mmc->max_hw_segs = 16;
1118 mmc->max_phys_segs = 16;
1119
1120 /*
1121 * Maximum number of sectors in one transfer. Limited by sector
1122 * count register.
1123 */
1124 mmc->max_sectors = 0x3FFF;
1125
1126 /*
1127 * Maximum segment size. Could be one segment with the maximum number
1128 * of sectors.
1129 */
1130 mmc->max_seg_size = mmc->max_sectors * 512;
1131
1132 /*
1133 * Init tasklets.
1134 */
1135 tasklet_init(&host->card_tasklet,
1136 sdhci_tasklet_card, (unsigned long)host);
1137 tasklet_init(&host->finish_tasklet,
1138 sdhci_tasklet_finish, (unsigned long)host);
1139
1140 setup_timer(&host->timer, sdhci_timeout_timer, (long)host);
1141
1142 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1143 host->slot_descr, host);
1144 if (ret)
1145 goto untasklet;
1146
1147 sdhci_init(host);
1148
1149 #ifdef CONFIG_MMC_DEBUG
1150 sdhci_dumpregs(host);
1151 #endif
1152
1153 host->chip = chip;
1154 chip->hosts[slot] = host;
1155
1156 mmc_add_host(mmc);
1157
1158 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1159 host->addr, host->irq,
1160 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1161
1162 return 0;
1163
1164 untasklet:
1165 tasklet_kill(&host->card_tasklet);
1166 tasklet_kill(&host->finish_tasklet);
1167 unmap:
1168 iounmap(host->ioaddr);
1169 release:
1170 pci_release_region(pdev, host->bar);
1171 free:
1172 mmc_free_host(mmc);
1173
1174 return ret;
1175 }
1176
1177 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1178 {
1179 struct sdhci_chip *chip;
1180 struct mmc_host *mmc;
1181 struct sdhci_host *host;
1182
1183 chip = pci_get_drvdata(pdev);
1184 host = chip->hosts[slot];
1185 mmc = host->mmc;
1186
1187 chip->hosts[slot] = NULL;
1188
1189 mmc_remove_host(mmc);
1190
1191 sdhci_reset(host, SDHCI_RESET_ALL);
1192
1193 free_irq(host->irq, host);
1194
1195 del_timer_sync(&host->timer);
1196
1197 tasklet_kill(&host->card_tasklet);
1198 tasklet_kill(&host->finish_tasklet);
1199
1200 iounmap(host->ioaddr);
1201
1202 pci_release_region(pdev, host->bar);
1203
1204 mmc_free_host(mmc);
1205 }
1206
1207 static int __devinit sdhci_probe(struct pci_dev *pdev,
1208 const struct pci_device_id *ent)
1209 {
1210 int ret, i;
1211 u8 slots, rev;
1212 struct sdhci_chip *chip;
1213
1214 BUG_ON(pdev == NULL);
1215 BUG_ON(ent == NULL);
1216
1217 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1218
1219 printk(KERN_INFO DRIVER_NAME
1220 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1221 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1222 (int)rev);
1223
1224 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1225 if (ret)
1226 return ret;
1227
1228 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1229 DBG("found %d slot(s)\n", slots);
1230 if (slots == 0)
1231 return -ENODEV;
1232
1233 ret = pci_enable_device(pdev);
1234 if (ret)
1235 return ret;
1236
1237 chip = kzalloc(sizeof(struct sdhci_chip) +
1238 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1239 if (!chip) {
1240 ret = -ENOMEM;
1241 goto err;
1242 }
1243
1244 chip->pdev = pdev;
1245
1246 chip->num_slots = slots;
1247 pci_set_drvdata(pdev, chip);
1248
1249 for (i = 0;i < slots;i++) {
1250 ret = sdhci_probe_slot(pdev, i);
1251 if (ret) {
1252 for (i--;i >= 0;i--)
1253 sdhci_remove_slot(pdev, i);
1254 goto free;
1255 }
1256 }
1257
1258 return 0;
1259
1260 free:
1261 pci_set_drvdata(pdev, NULL);
1262 kfree(chip);
1263
1264 err:
1265 pci_disable_device(pdev);
1266 return ret;
1267 }
1268
1269 static void __devexit sdhci_remove(struct pci_dev *pdev)
1270 {
1271 int i;
1272 struct sdhci_chip *chip;
1273
1274 chip = pci_get_drvdata(pdev);
1275
1276 if (chip) {
1277 for (i = 0;i < chip->num_slots;i++)
1278 sdhci_remove_slot(pdev, i);
1279
1280 pci_set_drvdata(pdev, NULL);
1281
1282 kfree(chip);
1283 }
1284
1285 pci_disable_device(pdev);
1286 }
1287
1288 static struct pci_driver sdhci_driver = {
1289 .name = DRIVER_NAME,
1290 .id_table = pci_ids,
1291 .probe = sdhci_probe,
1292 .remove = __devexit_p(sdhci_remove),
1293 .suspend = sdhci_suspend,
1294 .resume = sdhci_resume,
1295 };
1296
1297 /*****************************************************************************\
1298 * *
1299 * Driver init/exit *
1300 * *
1301 \*****************************************************************************/
1302
1303 static int __init sdhci_drv_init(void)
1304 {
1305 printk(KERN_INFO DRIVER_NAME
1306 ": Secure Digital Host Controller Interface driver, "
1307 DRIVER_VERSION "\n");
1308 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1309
1310 return pci_register_driver(&sdhci_driver);
1311 }
1312
1313 static void __exit sdhci_drv_exit(void)
1314 {
1315 DBG("Exiting\n");
1316
1317 pci_unregister_driver(&sdhci_driver);
1318 }
1319
1320 module_init(sdhci_drv_init);
1321 module_exit(sdhci_drv_exit);
1322
1323 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1324 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1325 MODULE_VERSION(DRIVER_VERSION);
1326 MODULE_LICENSE("GPL");
This page took 0.075727 seconds and 6 git commands to generate.