Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / 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                 1 << data->blksz_bits, 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(1 << data->blksz_bits, 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 = (1 << data->blksz_bits) * 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 = (1 << data->blksz_bits) * (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         u32 present;
375         unsigned long max_jiffies;
376
377         WARN_ON(host->cmd);
378
379         DBG("Sending cmd (%x)\n", cmd->opcode);
380
381         /* Wait max 10 ms */
382         max_jiffies = jiffies + (HZ + 99)/100;
383         do {
384                 if (time_after(jiffies, max_jiffies)) {
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                 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
394         } while (present & (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT));
395
396         mod_timer(&host->timer, jiffies + 10 * HZ);
397
398         host->cmd = cmd;
399
400         sdhci_prepare_data(host, cmd->data);
401
402         writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
403
404         if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
405                 printk(KERN_ERR "%s: Unsupported response type! "
406                         "Please report this to " BUGMAIL ".\n",
407                         mmc_hostname(host->mmc));
408                 cmd->error = MMC_ERR_INVALID;
409                 tasklet_schedule(&host->finish_tasklet);
410                 return;
411         }
412
413         if (!(cmd->flags & MMC_RSP_PRESENT))
414                 flags = SDHCI_CMD_RESP_NONE;
415         else if (cmd->flags & MMC_RSP_136)
416                 flags = SDHCI_CMD_RESP_LONG;
417         else if (cmd->flags & MMC_RSP_BUSY)
418                 flags = SDHCI_CMD_RESP_SHORT_BUSY;
419         else
420                 flags = SDHCI_CMD_RESP_SHORT;
421
422         if (cmd->flags & MMC_RSP_CRC)
423                 flags |= SDHCI_CMD_CRC;
424         if (cmd->flags & MMC_RSP_OPCODE)
425                 flags |= SDHCI_CMD_INDEX;
426         if (cmd->data)
427                 flags |= SDHCI_CMD_DATA;
428
429         writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
430                 host->ioaddr + SDHCI_COMMAND);
431 }
432
433 static void sdhci_finish_command(struct sdhci_host *host)
434 {
435         int i;
436
437         BUG_ON(host->cmd == NULL);
438
439         if (host->cmd->flags & MMC_RSP_PRESENT) {
440                 if (host->cmd->flags & MMC_RSP_136) {
441                         /* CRC is stripped so we need to do some shifting. */
442                         for (i = 0;i < 4;i++) {
443                                 host->cmd->resp[i] = readl(host->ioaddr +
444                                         SDHCI_RESPONSE + (3-i)*4) << 8;
445                                 if (i != 3)
446                                         host->cmd->resp[i] |=
447                                                 readb(host->ioaddr +
448                                                 SDHCI_RESPONSE + (3-i)*4-1);
449                         }
450                 } else {
451                         host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
452                 }
453         }
454
455         host->cmd->error = MMC_ERR_NONE;
456
457         DBG("Ending cmd (%x)\n", host->cmd->opcode);
458
459         if (host->cmd->data) {
460                 u32 intmask;
461
462                 host->data = host->cmd->data;
463
464                 if (!(host->flags & SDHCI_USE_DMA)) {
465                         /*
466                          * Don't enable the interrupts until now to make sure we
467                          * get stable handling of the FIFO.
468                          */
469                         intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
470                         intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
471                         writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
472
473                         intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
474                         intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
475                         writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
476
477                         /*
478                          * The buffer interrupts are to unreliable so we
479                          * start the transfer immediatly.
480                          */
481                         sdhci_transfer_pio(host);
482                 }
483         } else
484                 tasklet_schedule(&host->finish_tasklet);
485
486         host->cmd = NULL;
487 }
488
489 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
490 {
491         int div;
492         u16 clk;
493         unsigned long max_jiffies;
494
495         if (clock == host->clock)
496                 return;
497
498         writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
499
500         if (clock == 0)
501                 goto out;
502
503         for (div = 1;div < 256;div *= 2) {
504                 if ((host->max_clk / div) <= clock)
505                         break;
506         }
507         div >>= 1;
508
509         clk = div << SDHCI_DIVIDER_SHIFT;
510         clk |= SDHCI_CLOCK_INT_EN;
511         writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
512
513         /* Wait max 10 ms */
514         max_jiffies = jiffies + (HZ + 99)/100;
515         do {
516                 if (time_after(jiffies, max_jiffies)) {
517                         printk(KERN_ERR "%s: Internal clock never stabilised. "
518                                 "Please report this to " BUGMAIL ".\n",
519                                 mmc_hostname(host->mmc));
520                         sdhci_dumpregs(host);
521                         return;
522                 }
523                 clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL);
524         } while (!(clk & SDHCI_CLOCK_INT_STABLE));
525
526         clk |= SDHCI_CLOCK_CARD_EN;
527         writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
528
529 out:
530         host->clock = clock;
531 }
532
533 /*****************************************************************************\
534  *                                                                           *
535  * MMC callbacks                                                             *
536  *                                                                           *
537 \*****************************************************************************/
538
539 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
540 {
541         struct sdhci_host *host;
542         unsigned long flags;
543
544         host = mmc_priv(mmc);
545
546         spin_lock_irqsave(&host->lock, flags);
547
548         WARN_ON(host->mrq != NULL);
549
550         sdhci_activate_led(host);
551
552         host->mrq = mrq;
553
554         if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
555                 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
556                 tasklet_schedule(&host->finish_tasklet);
557         } else
558                 sdhci_send_command(host, mrq->cmd);
559
560         spin_unlock_irqrestore(&host->lock, flags);
561 }
562
563 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
564 {
565         struct sdhci_host *host;
566         unsigned long flags;
567         u8 ctrl;
568
569         host = mmc_priv(mmc);
570
571         spin_lock_irqsave(&host->lock, flags);
572
573         DBG("clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
574              ios->clock, ios->bus_mode, ios->power_mode, ios->chip_select,
575              ios->vdd, ios->bus_width);
576
577         /*
578          * Reset the chip on each power off.
579          * Should clear out any weird states.
580          */
581         if (ios->power_mode == MMC_POWER_OFF) {
582                 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
583                 spin_unlock_irqrestore(&host->lock, flags);
584                 sdhci_init(host);
585                 spin_lock_irqsave(&host->lock, flags);
586         }
587
588         sdhci_set_clock(host, ios->clock);
589
590         if (ios->power_mode == MMC_POWER_OFF)
591                 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
592         else
593                 writeb(0xFF, host->ioaddr + SDHCI_POWER_CONTROL);
594
595         ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
596         if (ios->bus_width == MMC_BUS_WIDTH_4)
597                 ctrl |= SDHCI_CTRL_4BITBUS;
598         else
599                 ctrl &= ~SDHCI_CTRL_4BITBUS;
600         writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
601
602         spin_unlock_irqrestore(&host->lock, flags);
603 }
604
605 static int sdhci_get_ro(struct mmc_host *mmc)
606 {
607         struct sdhci_host *host;
608         unsigned long flags;
609         int present;
610
611         host = mmc_priv(mmc);
612
613         spin_lock_irqsave(&host->lock, flags);
614
615         present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
616
617         spin_unlock_irqrestore(&host->lock, flags);
618
619         return !(present & SDHCI_WRITE_PROTECT);
620 }
621
622 static struct mmc_host_ops sdhci_ops = {
623         .request        = sdhci_request,
624         .set_ios        = sdhci_set_ios,
625         .get_ro         = sdhci_get_ro,
626 };
627
628 /*****************************************************************************\
629  *                                                                           *
630  * Tasklets                                                                  *
631  *                                                                           *
632 \*****************************************************************************/
633
634 static void sdhci_tasklet_card(unsigned long param)
635 {
636         struct sdhci_host *host;
637         unsigned long flags;
638
639         host = (struct sdhci_host*)param;
640
641         spin_lock_irqsave(&host->lock, flags);
642
643         if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
644                 if (host->mrq) {
645                         printk(KERN_ERR "%s: Card removed during transfer!\n",
646                                 mmc_hostname(host->mmc));
647                         printk(KERN_ERR "%s: Resetting controller.\n",
648                                 mmc_hostname(host->mmc));
649
650                         sdhci_reset(host, SDHCI_RESET_CMD);
651                         sdhci_reset(host, SDHCI_RESET_DATA);
652
653                         host->mrq->cmd->error = MMC_ERR_FAILED;
654                         tasklet_schedule(&host->finish_tasklet);
655                 }
656         }
657
658         spin_unlock_irqrestore(&host->lock, flags);
659
660         mmc_detect_change(host->mmc, msecs_to_jiffies(500));
661 }
662
663 static void sdhci_tasklet_finish(unsigned long param)
664 {
665         struct sdhci_host *host;
666         unsigned long flags;
667         struct mmc_request *mrq;
668
669         host = (struct sdhci_host*)param;
670
671         spin_lock_irqsave(&host->lock, flags);
672
673         del_timer(&host->timer);
674
675         mrq = host->mrq;
676
677         DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
678
679         /*
680          * The controller needs a reset of internal state machines
681          * upon error conditions.
682          */
683         if ((mrq->cmd->error != MMC_ERR_NONE) ||
684                 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
685                 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
686                 sdhci_reset(host, SDHCI_RESET_CMD);
687                 sdhci_reset(host, SDHCI_RESET_DATA);
688         }
689
690         host->mrq = NULL;
691         host->cmd = NULL;
692         host->data = NULL;
693
694         sdhci_deactivate_led(host);
695
696         spin_unlock_irqrestore(&host->lock, flags);
697
698         mmc_request_done(host->mmc, mrq);
699 }
700
701 static void sdhci_timeout_timer(unsigned long data)
702 {
703         struct sdhci_host *host;
704         unsigned long flags;
705
706         host = (struct sdhci_host*)data;
707
708         spin_lock_irqsave(&host->lock, flags);
709
710         if (host->mrq) {
711                 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
712                         "Please report this to " BUGMAIL ".\n",
713                         mmc_hostname(host->mmc));
714                 sdhci_dumpregs(host);
715
716                 if (host->data) {
717                         host->data->error = MMC_ERR_TIMEOUT;
718                         sdhci_finish_data(host);
719                 } else {
720                         if (host->cmd)
721                                 host->cmd->error = MMC_ERR_TIMEOUT;
722                         else
723                                 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
724
725                         tasklet_schedule(&host->finish_tasklet);
726                 }
727         }
728
729         spin_unlock_irqrestore(&host->lock, flags);
730 }
731
732 /*****************************************************************************\
733  *                                                                           *
734  * Interrupt handling                                                        *
735  *                                                                           *
736 \*****************************************************************************/
737
738 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
739 {
740         BUG_ON(intmask == 0);
741
742         if (!host->cmd) {
743                 printk(KERN_ERR "%s: Got command interrupt even though no "
744                         "command operation was in progress.\n",
745                         mmc_hostname(host->mmc));
746                 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
747                         mmc_hostname(host->mmc));
748                 sdhci_dumpregs(host);
749                 return;
750         }
751
752         if (intmask & SDHCI_INT_RESPONSE)
753                 sdhci_finish_command(host);
754         else {
755                 if (intmask & SDHCI_INT_TIMEOUT)
756                         host->cmd->error = MMC_ERR_TIMEOUT;
757                 else if (intmask & SDHCI_INT_CRC)
758                         host->cmd->error = MMC_ERR_BADCRC;
759                 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
760                         host->cmd->error = MMC_ERR_FAILED;
761                 else
762                         host->cmd->error = MMC_ERR_INVALID;
763
764                 tasklet_schedule(&host->finish_tasklet);
765         }
766 }
767
768 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
769 {
770         BUG_ON(intmask == 0);
771
772         if (!host->data) {
773                 /*
774                  * A data end interrupt is sent together with the response
775                  * for the stop command.
776                  */
777                 if (intmask & SDHCI_INT_DATA_END)
778                         return;
779
780                 printk(KERN_ERR "%s: Got data interrupt even though no "
781                         "data operation was in progress.\n",
782                         mmc_hostname(host->mmc));
783                 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
784                         mmc_hostname(host->mmc));
785                 sdhci_dumpregs(host);
786
787                 return;
788         }
789
790         if (intmask & SDHCI_INT_DATA_TIMEOUT)
791                 host->data->error = MMC_ERR_TIMEOUT;
792         else if (intmask & SDHCI_INT_DATA_CRC)
793                 host->data->error = MMC_ERR_BADCRC;
794         else if (intmask & SDHCI_INT_DATA_END_BIT)
795                 host->data->error = MMC_ERR_FAILED;
796
797         if (host->data->error != MMC_ERR_NONE)
798                 sdhci_finish_data(host);
799         else {
800                 if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY))
801                         sdhci_transfer_pio(host);
802
803                 if (intmask & SDHCI_INT_DATA_END)
804                         sdhci_finish_data(host);
805         }
806 }
807
808 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
809 {
810         irqreturn_t result;
811         struct sdhci_host* host = dev_id;
812         u32 intmask;
813
814         spin_lock(&host->lock);
815
816         intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
817
818         if (!intmask) {
819                 result = IRQ_NONE;
820                 goto out;
821         }
822
823         DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
824
825         if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE))
826                 tasklet_schedule(&host->card_tasklet);
827
828         if (intmask & SDHCI_INT_CMD_MASK) {
829                 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
830
831                 writel(intmask & SDHCI_INT_CMD_MASK,
832                         host->ioaddr + SDHCI_INT_STATUS);
833         }
834
835         if (intmask & SDHCI_INT_DATA_MASK) {
836                 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
837
838                 writel(intmask & SDHCI_INT_DATA_MASK,
839                         host->ioaddr + SDHCI_INT_STATUS);
840         }
841
842         intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
843
844         if (intmask & SDHCI_INT_CARD_INT) {
845                 printk(KERN_ERR "%s: Unexpected card interrupt. Please "
846                         "report this to " BUGMAIL ".\n",
847                         mmc_hostname(host->mmc));
848                 sdhci_dumpregs(host);
849         }
850
851         if (intmask & SDHCI_INT_BUS_POWER) {
852                 printk(KERN_ERR "%s: Unexpected bus power interrupt. Please "
853                         "report this to " BUGMAIL ".\n",
854                         mmc_hostname(host->mmc));
855                 sdhci_dumpregs(host);
856         }
857
858         if (intmask & SDHCI_INT_ACMD12ERR) {
859                 printk(KERN_ERR "%s: Unexpected auto CMD12 error. Please "
860                         "report this to " BUGMAIL ".\n",
861                         mmc_hostname(host->mmc));
862                 sdhci_dumpregs(host);
863
864                 writew(~0, host->ioaddr + SDHCI_ACMD12_ERR);
865         }
866
867         if (intmask)
868                 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
869
870         result = IRQ_HANDLED;
871
872 out:
873         spin_unlock(&host->lock);
874
875         return result;
876 }
877
878 /*****************************************************************************\
879  *                                                                           *
880  * Suspend/resume                                                            *
881  *                                                                           *
882 \*****************************************************************************/
883
884 #ifdef CONFIG_PM
885
886 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
887 {
888         struct sdhci_chip *chip;
889         int i, ret;
890
891         chip = pci_get_drvdata(pdev);
892         if (!chip)
893                 return 0;
894
895         DBG("Suspending...\n");
896
897         for (i = 0;i < chip->num_slots;i++) {
898                 if (!chip->hosts[i])
899                         continue;
900                 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
901                 if (ret) {
902                         for (i--;i >= 0;i--)
903                                 mmc_resume_host(chip->hosts[i]->mmc);
904                         return ret;
905                 }
906         }
907
908         pci_save_state(pdev);
909         pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
910         pci_disable_device(pdev);
911         pci_set_power_state(pdev, pci_choose_state(pdev, state));
912
913         return 0;
914 }
915
916 static int sdhci_resume (struct pci_dev *pdev)
917 {
918         struct sdhci_chip *chip;
919         int i, ret;
920
921         chip = pci_get_drvdata(pdev);
922         if (!chip)
923                 return 0;
924
925         DBG("Resuming...\n");
926
927         pci_set_power_state(pdev, PCI_D0);
928         pci_restore_state(pdev);
929         pci_enable_device(pdev);
930
931         for (i = 0;i < chip->num_slots;i++) {
932                 if (!chip->hosts[i])
933                         continue;
934                 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
935                         pci_set_master(pdev);
936                 sdhci_init(chip->hosts[i]);
937                 ret = mmc_resume_host(chip->hosts[i]->mmc);
938                 if (ret)
939                         return ret;
940         }
941
942         return 0;
943 }
944
945 #else /* CONFIG_PM */
946
947 #define sdhci_suspend NULL
948 #define sdhci_resume NULL
949
950 #endif /* CONFIG_PM */
951
952 /*****************************************************************************\
953  *                                                                           *
954  * Device probing/removal                                                    *
955  *                                                                           *
956 \*****************************************************************************/
957
958 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
959 {
960         int ret;
961         struct sdhci_chip *chip;
962         struct mmc_host *mmc;
963         struct sdhci_host *host;
964
965         u8 first_bar;
966         unsigned int caps;
967
968         chip = pci_get_drvdata(pdev);
969         BUG_ON(!chip);
970
971         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
972         if (ret)
973                 return ret;
974
975         first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
976
977         if (first_bar > 5) {
978                 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
979                 return -ENODEV;
980         }
981
982         if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
983                 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
984                 return -ENODEV;
985         }
986
987         if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
988                 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
989                 return -ENODEV;
990         }
991
992         mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
993         if (!mmc)
994                 return -ENOMEM;
995
996         host = mmc_priv(mmc);
997         host->mmc = mmc;
998
999         host->bar = first_bar + slot;
1000
1001         host->addr = pci_resource_start(pdev, host->bar);
1002         host->irq = pdev->irq;
1003
1004         DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1005
1006         snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1007
1008         ret = pci_request_region(pdev, host->bar, host->slot_descr);
1009         if (ret)
1010                 goto free;
1011
1012         host->ioaddr = ioremap_nocache(host->addr,
1013                 pci_resource_len(pdev, host->bar));
1014         if (!host->ioaddr) {
1015                 ret = -ENOMEM;
1016                 goto release;
1017         }
1018
1019         caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1020
1021         if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1022                 host->flags |= SDHCI_USE_DMA;
1023
1024         if (host->flags & SDHCI_USE_DMA) {
1025                 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1026                         printk(KERN_WARNING "%s: No suitable DMA available. "
1027                                 "Falling back to PIO.\n", host->slot_descr);
1028                         host->flags &= ~SDHCI_USE_DMA;
1029                 }
1030         }
1031
1032         if (host->flags & SDHCI_USE_DMA)
1033                 pci_set_master(pdev);
1034         else /* XXX: Hack to get MMC layer to avoid highmem */
1035                 pdev->dma_mask = 0;
1036
1037         host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1038         host->max_clk *= 1000000;
1039
1040         /*
1041          * Set host parameters.
1042          */
1043         mmc->ops = &sdhci_ops;
1044         mmc->f_min = host->max_clk / 256;
1045         mmc->f_max = host->max_clk;
1046         mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1047         mmc->caps = MMC_CAP_4_BIT_DATA;
1048
1049         spin_lock_init(&host->lock);
1050
1051         /*
1052          * Maximum number of segments. Hardware cannot do scatter lists.
1053          */
1054         if (host->flags & SDHCI_USE_DMA)
1055                 mmc->max_hw_segs = 1;
1056         else
1057                 mmc->max_hw_segs = 16;
1058         mmc->max_phys_segs = 16;
1059
1060         /*
1061          * Maximum number of sectors in one transfer. Limited by sector
1062          * count register.
1063          */
1064         mmc->max_sectors = 0x3FFF;
1065
1066         /*
1067          * Maximum segment size. Could be one segment with the maximum number
1068          * of sectors.
1069          */
1070         mmc->max_seg_size = mmc->max_sectors * 512;
1071
1072         /*
1073          * Init tasklets.
1074          */
1075         tasklet_init(&host->card_tasklet,
1076                 sdhci_tasklet_card, (unsigned long)host);
1077         tasklet_init(&host->finish_tasklet,
1078                 sdhci_tasklet_finish, (unsigned long)host);
1079
1080         setup_timer(&host->timer, sdhci_timeout_timer, (int)host);
1081
1082         ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1083                 host->slot_descr, host);
1084         if (ret)
1085                 goto unmap;
1086
1087         sdhci_init(host);
1088
1089 #ifdef CONFIG_MMC_DEBUG
1090         sdhci_dumpregs(host);
1091 #endif
1092
1093         host->chip = chip;
1094         chip->hosts[slot] = host;
1095
1096         mmc_add_host(mmc);
1097
1098         printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1099                 host->addr, host->irq,
1100                 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1101
1102         return 0;
1103
1104 unmap:
1105         tasklet_kill(&host->card_tasklet);
1106         tasklet_kill(&host->finish_tasklet);
1107
1108         iounmap(host->ioaddr);
1109 release:
1110         pci_release_region(pdev, host->bar);
1111 free:
1112         mmc_free_host(mmc);
1113
1114         return ret;
1115 }
1116
1117 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1118 {
1119         struct sdhci_chip *chip;
1120         struct mmc_host *mmc;
1121         struct sdhci_host *host;
1122
1123         chip = pci_get_drvdata(pdev);
1124         host = chip->hosts[slot];
1125         mmc = host->mmc;
1126
1127         chip->hosts[slot] = NULL;
1128
1129         mmc_remove_host(mmc);
1130
1131         sdhci_reset(host, SDHCI_RESET_ALL);
1132
1133         free_irq(host->irq, host);
1134
1135         del_timer_sync(&host->timer);
1136
1137         tasklet_kill(&host->card_tasklet);
1138         tasklet_kill(&host->finish_tasklet);
1139
1140         iounmap(host->ioaddr);
1141
1142         pci_release_region(pdev, host->bar);
1143
1144         mmc_free_host(mmc);
1145 }
1146
1147 static int __devinit sdhci_probe(struct pci_dev *pdev,
1148         const struct pci_device_id *ent)
1149 {
1150         int ret, i;
1151         u8 slots;
1152         struct sdhci_chip *chip;
1153
1154         BUG_ON(pdev == NULL);
1155         BUG_ON(ent == NULL);
1156
1157         DBG("found at %s\n", pci_name(pdev));
1158
1159         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1160         if (ret)
1161                 return ret;
1162
1163         slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1164         DBG("found %d slot(s)\n", slots);
1165         if (slots == 0)
1166                 return -ENODEV;
1167
1168         ret = pci_enable_device(pdev);
1169         if (ret)
1170                 return ret;
1171
1172         chip = kzalloc(sizeof(struct sdhci_chip) +
1173                 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1174         if (!chip) {
1175                 ret = -ENOMEM;
1176                 goto err;
1177         }
1178
1179         chip->pdev = pdev;
1180
1181         chip->num_slots = slots;
1182         pci_set_drvdata(pdev, chip);
1183
1184         for (i = 0;i < slots;i++) {
1185                 ret = sdhci_probe_slot(pdev, i);
1186                 if (ret) {
1187                         for (i--;i >= 0;i--)
1188                                 sdhci_remove_slot(pdev, i);
1189                         goto free;
1190                 }
1191         }
1192
1193         return 0;
1194
1195 free:
1196         pci_set_drvdata(pdev, NULL);
1197         kfree(chip);
1198
1199 err:
1200         pci_disable_device(pdev);
1201         return ret;
1202 }
1203
1204 static void __devexit sdhci_remove(struct pci_dev *pdev)
1205 {
1206         int i;
1207         struct sdhci_chip *chip;
1208
1209         chip = pci_get_drvdata(pdev);
1210
1211         if (chip) {
1212                 for (i = 0;i < chip->num_slots;i++)
1213                         sdhci_remove_slot(pdev, i);
1214
1215                 pci_set_drvdata(pdev, NULL);
1216
1217                 kfree(chip);
1218         }
1219
1220         pci_disable_device(pdev);
1221 }
1222
1223 static struct pci_driver sdhci_driver = {
1224         .name =         DRIVER_NAME,
1225         .id_table =     pci_ids,
1226         .probe =        sdhci_probe,
1227         .remove =       __devexit_p(sdhci_remove),
1228         .suspend =      sdhci_suspend,
1229         .resume =       sdhci_resume,
1230 };
1231
1232 /*****************************************************************************\
1233  *                                                                           *
1234  * Driver init/exit                                                          *
1235  *                                                                           *
1236 \*****************************************************************************/
1237
1238 static int __init sdhci_drv_init(void)
1239 {
1240         printk(KERN_INFO DRIVER_NAME
1241                 ": Secure Digital Host Controller Interface driver, "
1242                 DRIVER_VERSION "\n");
1243         printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1244
1245         return pci_register_driver(&sdhci_driver);
1246 }
1247
1248 static void __exit sdhci_drv_exit(void)
1249 {
1250         DBG("Exiting\n");
1251
1252         pci_unregister_driver(&sdhci_driver);
1253 }
1254
1255 module_init(sdhci_drv_init);
1256 module_exit(sdhci_drv_exit);
1257
1258 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1259 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1260 MODULE_VERSION(DRIVER_VERSION);
1261 MODULE_LICENSE("GPL");