]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/mmc/host/sdhci-pci.c
mmc: sdhci: Intel Medfield support
[mv-sheeva.git] / drivers / mmc / host / sdhci-pci.c
1 /*  linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
2  *
3  *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * Thanks to the following companies for their support:
11  *
12  *     - JMicron (hardware and technical support)
13  */
14
15 #include <linux/delay.h>
16 #include <linux/highmem.h>
17 #include <linux/pci.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21
22 #include <linux/mmc/host.h>
23
24 #include <asm/scatterlist.h>
25 #include <asm/io.h>
26
27 #include "sdhci.h"
28
29 /*
30  * PCI registers
31  */
32
33 #define PCI_SDHCI_IFPIO                 0x00
34 #define PCI_SDHCI_IFDMA                 0x01
35 #define PCI_SDHCI_IFVENDOR              0x02
36
37 #define PCI_SLOT_INFO                   0x40    /* 8 bits */
38 #define  PCI_SLOT_INFO_SLOTS(x)         ((x >> 4) & 7)
39 #define  PCI_SLOT_INFO_FIRST_BAR_MASK   0x07
40
41 #define MAX_SLOTS                       8
42
43 struct sdhci_pci_chip;
44 struct sdhci_pci_slot;
45
46 struct sdhci_pci_fixes {
47         unsigned int            quirks;
48
49         int                     (*probe)(struct sdhci_pci_chip*);
50
51         int                     (*probe_slot)(struct sdhci_pci_slot*);
52         void                    (*remove_slot)(struct sdhci_pci_slot*, int);
53
54         int                     (*suspend)(struct sdhci_pci_chip*,
55                                         pm_message_t);
56         int                     (*resume)(struct sdhci_pci_chip*);
57 };
58
59 struct sdhci_pci_slot {
60         struct sdhci_pci_chip   *chip;
61         struct sdhci_host       *host;
62
63         int                     pci_bar;
64 };
65
66 struct sdhci_pci_chip {
67         struct pci_dev          *pdev;
68
69         unsigned int            quirks;
70         const struct sdhci_pci_fixes *fixes;
71
72         int                     num_slots;      /* Slots on controller */
73         struct sdhci_pci_slot   *slots[MAX_SLOTS]; /* Pointers to host slots */
74 };
75
76
77 /*****************************************************************************\
78  *                                                                           *
79  * Hardware specific quirk handling                                          *
80  *                                                                           *
81 \*****************************************************************************/
82
83 static int ricoh_probe(struct sdhci_pci_chip *chip)
84 {
85         if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
86             chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
87                 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
88         return 0;
89 }
90
91 static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
92 {
93         slot->host->caps =
94                 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
95                         & SDHCI_TIMEOUT_CLK_MASK) |
96
97                 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
98                         & SDHCI_CLOCK_BASE_MASK) |
99
100                 SDHCI_TIMEOUT_CLK_UNIT |
101                 SDHCI_CAN_VDD_330 |
102                 SDHCI_CAN_DO_SDMA;
103         return 0;
104 }
105
106 static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
107 {
108         /* Apply a delay to allow controller to settle */
109         /* Otherwise it becomes confused if card state changed
110                 during suspend */
111         msleep(500);
112         return 0;
113 }
114
115 static const struct sdhci_pci_fixes sdhci_ricoh = {
116         .probe          = ricoh_probe,
117         .quirks         = SDHCI_QUIRK_32BIT_DMA_ADDR |
118                           SDHCI_QUIRK_FORCE_DMA |
119                           SDHCI_QUIRK_CLOCK_BEFORE_RESET,
120 };
121
122 static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
123         .probe_slot     = ricoh_mmc_probe_slot,
124         .resume         = ricoh_mmc_resume,
125         .quirks         = SDHCI_QUIRK_32BIT_DMA_ADDR |
126                           SDHCI_QUIRK_CLOCK_BEFORE_RESET |
127                           SDHCI_QUIRK_NO_CARD_NO_RESET |
128                           SDHCI_QUIRK_MISSING_CAPS
129 };
130
131 static const struct sdhci_pci_fixes sdhci_ene_712 = {
132         .quirks         = SDHCI_QUIRK_SINGLE_POWER_WRITE |
133                           SDHCI_QUIRK_BROKEN_DMA,
134 };
135
136 static const struct sdhci_pci_fixes sdhci_ene_714 = {
137         .quirks         = SDHCI_QUIRK_SINGLE_POWER_WRITE |
138                           SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
139                           SDHCI_QUIRK_BROKEN_DMA,
140 };
141
142 static const struct sdhci_pci_fixes sdhci_cafe = {
143         .quirks         = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
144                           SDHCI_QUIRK_NO_BUSY_IRQ |
145                           SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
146 };
147
148 static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
149         .quirks         = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
150 };
151
152 static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc_sdio = {
153         .quirks         = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
154 };
155
156 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
157 {
158         u8 scratch;
159         int ret;
160
161         ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
162         if (ret)
163                 return ret;
164
165         /*
166          * Turn PMOS on [bit 0], set over current detection to 2.4 V
167          * [bit 1:2] and enable over current debouncing [bit 6].
168          */
169         if (on)
170                 scratch |= 0x47;
171         else
172                 scratch &= ~0x47;
173
174         ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
175         if (ret)
176                 return ret;
177
178         return 0;
179 }
180
181 static int jmicron_probe(struct sdhci_pci_chip *chip)
182 {
183         int ret;
184
185         if (chip->pdev->revision == 0) {
186                 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
187                           SDHCI_QUIRK_32BIT_DMA_SIZE |
188                           SDHCI_QUIRK_32BIT_ADMA_SIZE |
189                           SDHCI_QUIRK_RESET_AFTER_REQUEST |
190                           SDHCI_QUIRK_BROKEN_SMALL_PIO;
191         }
192
193         /*
194          * JMicron chips can have two interfaces to the same hardware
195          * in order to work around limitations in Microsoft's driver.
196          * We need to make sure we only bind to one of them.
197          *
198          * This code assumes two things:
199          *
200          * 1. The PCI code adds subfunctions in order.
201          *
202          * 2. The MMC interface has a lower subfunction number
203          *    than the SD interface.
204          */
205         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
206                 struct pci_dev *sd_dev;
207
208                 sd_dev = NULL;
209                 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
210                         PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
211                         if ((PCI_SLOT(chip->pdev->devfn) ==
212                                 PCI_SLOT(sd_dev->devfn)) &&
213                                 (chip->pdev->bus == sd_dev->bus))
214                                 break;
215                 }
216
217                 if (sd_dev) {
218                         pci_dev_put(sd_dev);
219                         dev_info(&chip->pdev->dev, "Refusing to bind to "
220                                 "secondary interface.\n");
221                         return -ENODEV;
222                 }
223         }
224
225         /*
226          * JMicron chips need a bit of a nudge to enable the power
227          * output pins.
228          */
229         ret = jmicron_pmos(chip, 1);
230         if (ret) {
231                 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
232                 return ret;
233         }
234
235         return 0;
236 }
237
238 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
239 {
240         u8 scratch;
241
242         scratch = readb(host->ioaddr + 0xC0);
243
244         if (on)
245                 scratch |= 0x01;
246         else
247                 scratch &= ~0x01;
248
249         writeb(scratch, host->ioaddr + 0xC0);
250 }
251
252 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
253 {
254         if (slot->chip->pdev->revision == 0) {
255                 u16 version;
256
257                 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
258                 version = (version & SDHCI_VENDOR_VER_MASK) >>
259                         SDHCI_VENDOR_VER_SHIFT;
260
261                 /*
262                  * Older versions of the chip have lots of nasty glitches
263                  * in the ADMA engine. It's best just to avoid it
264                  * completely.
265                  */
266                 if (version < 0xAC)
267                         slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
268         }
269
270         /*
271          * The secondary interface requires a bit set to get the
272          * interrupts.
273          */
274         if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
275                 jmicron_enable_mmc(slot->host, 1);
276
277         return 0;
278 }
279
280 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
281 {
282         if (dead)
283                 return;
284
285         if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
286                 jmicron_enable_mmc(slot->host, 0);
287 }
288
289 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
290 {
291         int i;
292
293         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
294                 for (i = 0;i < chip->num_slots;i++)
295                         jmicron_enable_mmc(chip->slots[i]->host, 0);
296         }
297
298         return 0;
299 }
300
301 static int jmicron_resume(struct sdhci_pci_chip *chip)
302 {
303         int ret, i;
304
305         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
306                 for (i = 0;i < chip->num_slots;i++)
307                         jmicron_enable_mmc(chip->slots[i]->host, 1);
308         }
309
310         ret = jmicron_pmos(chip, 1);
311         if (ret) {
312                 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
313                 return ret;
314         }
315
316         return 0;
317 }
318
319 static const struct sdhci_pci_fixes sdhci_jmicron = {
320         .probe          = jmicron_probe,
321
322         .probe_slot     = jmicron_probe_slot,
323         .remove_slot    = jmicron_remove_slot,
324
325         .suspend        = jmicron_suspend,
326         .resume         = jmicron_resume,
327 };
328
329 /* SysKonnect CardBus2SDIO extra registers */
330 #define SYSKT_CTRL              0x200
331 #define SYSKT_RDFIFO_STAT       0x204
332 #define SYSKT_WRFIFO_STAT       0x208
333 #define SYSKT_POWER_DATA        0x20c
334 #define   SYSKT_POWER_330       0xef
335 #define   SYSKT_POWER_300       0xf8
336 #define   SYSKT_POWER_184       0xcc
337 #define SYSKT_POWER_CMD         0x20d
338 #define   SYSKT_POWER_START     (1 << 7)
339 #define SYSKT_POWER_STATUS      0x20e
340 #define   SYSKT_POWER_STATUS_OK (1 << 0)
341 #define SYSKT_BOARD_REV         0x210
342 #define SYSKT_CHIP_REV          0x211
343 #define SYSKT_CONF_DATA         0x212
344 #define   SYSKT_CONF_DATA_1V8   (1 << 2)
345 #define   SYSKT_CONF_DATA_2V5   (1 << 1)
346 #define   SYSKT_CONF_DATA_3V3   (1 << 0)
347
348 static int syskt_probe(struct sdhci_pci_chip *chip)
349 {
350         if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
351                 chip->pdev->class &= ~0x0000FF;
352                 chip->pdev->class |= PCI_SDHCI_IFDMA;
353         }
354         return 0;
355 }
356
357 static int syskt_probe_slot(struct sdhci_pci_slot *slot)
358 {
359         int tm, ps;
360
361         u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
362         u8  chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
363         dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
364                                          "board rev %d.%d, chip rev %d.%d\n",
365                                          board_rev >> 4, board_rev & 0xf,
366                                          chip_rev >> 4,  chip_rev & 0xf);
367         if (chip_rev >= 0x20)
368                 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
369
370         writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
371         writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
372         udelay(50);
373         tm = 10;  /* Wait max 1 ms */
374         do {
375                 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
376                 if (ps & SYSKT_POWER_STATUS_OK)
377                         break;
378                 udelay(100);
379         } while (--tm);
380         if (!tm) {
381                 dev_err(&slot->chip->pdev->dev,
382                         "power regulator never stabilized");
383                 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
384                 return -ENODEV;
385         }
386
387         return 0;
388 }
389
390 static const struct sdhci_pci_fixes sdhci_syskt = {
391         .quirks         = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
392         .probe          = syskt_probe,
393         .probe_slot     = syskt_probe_slot,
394 };
395
396 static int via_probe(struct sdhci_pci_chip *chip)
397 {
398         if (chip->pdev->revision == 0x10)
399                 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
400
401         return 0;
402 }
403
404 static const struct sdhci_pci_fixes sdhci_via = {
405         .probe          = via_probe,
406 };
407
408 static const struct pci_device_id pci_ids[] __devinitdata = {
409         {
410                 .vendor         = PCI_VENDOR_ID_RICOH,
411                 .device         = PCI_DEVICE_ID_RICOH_R5C822,
412                 .subvendor      = PCI_ANY_ID,
413                 .subdevice      = PCI_ANY_ID,
414                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh,
415         },
416
417         {
418                 .vendor         = PCI_VENDOR_ID_RICOH,
419                 .device         = 0x843,
420                 .subvendor      = PCI_ANY_ID,
421                 .subdevice      = PCI_ANY_ID,
422                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh_mmc,
423         },
424
425         {
426                 .vendor         = PCI_VENDOR_ID_RICOH,
427                 .device         = 0xe822,
428                 .subvendor      = PCI_ANY_ID,
429                 .subdevice      = PCI_ANY_ID,
430                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh_mmc,
431         },
432
433         {
434                 .vendor         = PCI_VENDOR_ID_ENE,
435                 .device         = PCI_DEVICE_ID_ENE_CB712_SD,
436                 .subvendor      = PCI_ANY_ID,
437                 .subdevice      = PCI_ANY_ID,
438                 .driver_data    = (kernel_ulong_t)&sdhci_ene_712,
439         },
440
441         {
442                 .vendor         = PCI_VENDOR_ID_ENE,
443                 .device         = PCI_DEVICE_ID_ENE_CB712_SD_2,
444                 .subvendor      = PCI_ANY_ID,
445                 .subdevice      = PCI_ANY_ID,
446                 .driver_data    = (kernel_ulong_t)&sdhci_ene_712,
447         },
448
449         {
450                 .vendor         = PCI_VENDOR_ID_ENE,
451                 .device         = PCI_DEVICE_ID_ENE_CB714_SD,
452                 .subvendor      = PCI_ANY_ID,
453                 .subdevice      = PCI_ANY_ID,
454                 .driver_data    = (kernel_ulong_t)&sdhci_ene_714,
455         },
456
457         {
458                 .vendor         = PCI_VENDOR_ID_ENE,
459                 .device         = PCI_DEVICE_ID_ENE_CB714_SD_2,
460                 .subvendor      = PCI_ANY_ID,
461                 .subdevice      = PCI_ANY_ID,
462                 .driver_data    = (kernel_ulong_t)&sdhci_ene_714,
463         },
464
465         {
466                 .vendor         = PCI_VENDOR_ID_MARVELL,
467                 .device         = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
468                 .subvendor      = PCI_ANY_ID,
469                 .subdevice      = PCI_ANY_ID,
470                 .driver_data    = (kernel_ulong_t)&sdhci_cafe,
471         },
472
473         {
474                 .vendor         = PCI_VENDOR_ID_JMICRON,
475                 .device         = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
476                 .subvendor      = PCI_ANY_ID,
477                 .subdevice      = PCI_ANY_ID,
478                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
479         },
480
481         {
482                 .vendor         = PCI_VENDOR_ID_JMICRON,
483                 .device         = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
484                 .subvendor      = PCI_ANY_ID,
485                 .subdevice      = PCI_ANY_ID,
486                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
487         },
488
489         {
490                 .vendor         = PCI_VENDOR_ID_SYSKONNECT,
491                 .device         = 0x8000,
492                 .subvendor      = PCI_ANY_ID,
493                 .subdevice      = PCI_ANY_ID,
494                 .driver_data    = (kernel_ulong_t)&sdhci_syskt,
495         },
496
497         {
498                 .vendor         = PCI_VENDOR_ID_VIA,
499                 .device         = 0x95d0,
500                 .subvendor      = PCI_ANY_ID,
501                 .subdevice      = PCI_ANY_ID,
502                 .driver_data    = (kernel_ulong_t)&sdhci_via,
503         },
504
505         {
506                 .vendor         = PCI_VENDOR_ID_INTEL,
507                 .device         = PCI_DEVICE_ID_INTEL_MFD_SD,
508                 .subvendor      = PCI_ANY_ID,
509                 .subdevice      = PCI_ANY_ID,
510                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_sd,
511         },
512
513         {
514                 .vendor         = PCI_VENDOR_ID_INTEL,
515                 .device         = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
516                 .subvendor      = PCI_ANY_ID,
517                 .subdevice      = PCI_ANY_ID,
518                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
519         },
520
521         {
522                 .vendor         = PCI_VENDOR_ID_INTEL,
523                 .device         = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
524                 .subvendor      = PCI_ANY_ID,
525                 .subdevice      = PCI_ANY_ID,
526                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
527         },
528
529         {
530                 .vendor         = PCI_VENDOR_ID_INTEL,
531                 .device         = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
532                 .subvendor      = PCI_ANY_ID,
533                 .subdevice      = PCI_ANY_ID,
534                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
535         },
536
537         {
538                 .vendor         = PCI_VENDOR_ID_INTEL,
539                 .device         = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
540                 .subvendor      = PCI_ANY_ID,
541                 .subdevice      = PCI_ANY_ID,
542                 .driver_data    = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
543         },
544
545         {       /* Generic SD host controller */
546                 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
547         },
548
549         { /* end: all zeroes */ },
550 };
551
552 MODULE_DEVICE_TABLE(pci, pci_ids);
553
554 /*****************************************************************************\
555  *                                                                           *
556  * SDHCI core callbacks                                                      *
557  *                                                                           *
558 \*****************************************************************************/
559
560 static int sdhci_pci_enable_dma(struct sdhci_host *host)
561 {
562         struct sdhci_pci_slot *slot;
563         struct pci_dev *pdev;
564         int ret;
565
566         slot = sdhci_priv(host);
567         pdev = slot->chip->pdev;
568
569         if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
570                 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
571                 (host->flags & SDHCI_USE_SDMA)) {
572                 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
573                         "doesn't fully claim to support it.\n");
574         }
575
576         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
577         if (ret)
578                 return ret;
579
580         pci_set_master(pdev);
581
582         return 0;
583 }
584
585 static struct sdhci_ops sdhci_pci_ops = {
586         .enable_dma     = sdhci_pci_enable_dma,
587 };
588
589 /*****************************************************************************\
590  *                                                                           *
591  * Suspend/resume                                                            *
592  *                                                                           *
593 \*****************************************************************************/
594
595 #ifdef CONFIG_PM
596
597 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
598 {
599         struct sdhci_pci_chip *chip;
600         struct sdhci_pci_slot *slot;
601         mmc_pm_flag_t pm_flags = 0;
602         int i, ret;
603
604         chip = pci_get_drvdata(pdev);
605         if (!chip)
606                 return 0;
607
608         for (i = 0;i < chip->num_slots;i++) {
609                 slot = chip->slots[i];
610                 if (!slot)
611                         continue;
612
613                 ret = sdhci_suspend_host(slot->host, state);
614
615                 if (ret) {
616                         for (i--;i >= 0;i--)
617                                 sdhci_resume_host(chip->slots[i]->host);
618                         return ret;
619                 }
620
621                 pm_flags |= slot->host->mmc->pm_flags;
622         }
623
624         if (chip->fixes && chip->fixes->suspend) {
625                 ret = chip->fixes->suspend(chip, state);
626                 if (ret) {
627                         for (i = chip->num_slots - 1;i >= 0;i--)
628                                 sdhci_resume_host(chip->slots[i]->host);
629                         return ret;
630                 }
631         }
632
633         pci_save_state(pdev);
634         if (pm_flags & MMC_PM_KEEP_POWER) {
635                 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ)
636                         pci_enable_wake(pdev, PCI_D3hot, 1);
637                 pci_set_power_state(pdev, PCI_D3hot);
638         } else {
639                 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
640                 pci_disable_device(pdev);
641                 pci_set_power_state(pdev, pci_choose_state(pdev, state));
642         }
643
644         return 0;
645 }
646
647 static int sdhci_pci_resume (struct pci_dev *pdev)
648 {
649         struct sdhci_pci_chip *chip;
650         struct sdhci_pci_slot *slot;
651         int i, ret;
652
653         chip = pci_get_drvdata(pdev);
654         if (!chip)
655                 return 0;
656
657         pci_set_power_state(pdev, PCI_D0);
658         pci_restore_state(pdev);
659         ret = pci_enable_device(pdev);
660         if (ret)
661                 return ret;
662
663         if (chip->fixes && chip->fixes->resume) {
664                 ret = chip->fixes->resume(chip);
665                 if (ret)
666                         return ret;
667         }
668
669         for (i = 0;i < chip->num_slots;i++) {
670                 slot = chip->slots[i];
671                 if (!slot)
672                         continue;
673
674                 ret = sdhci_resume_host(slot->host);
675                 if (ret)
676                         return ret;
677         }
678
679         return 0;
680 }
681
682 #else /* CONFIG_PM */
683
684 #define sdhci_pci_suspend NULL
685 #define sdhci_pci_resume NULL
686
687 #endif /* CONFIG_PM */
688
689 /*****************************************************************************\
690  *                                                                           *
691  * Device probing/removal                                                    *
692  *                                                                           *
693 \*****************************************************************************/
694
695 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
696         struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
697 {
698         struct sdhci_pci_slot *slot;
699         struct sdhci_host *host;
700
701         resource_size_t addr;
702
703         int ret;
704
705         if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
706                 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
707                 return ERR_PTR(-ENODEV);
708         }
709
710         if (pci_resource_len(pdev, bar) != 0x100) {
711                 dev_err(&pdev->dev, "Invalid iomem size. You may "
712                         "experience problems.\n");
713         }
714
715         if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
716                 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
717                 return ERR_PTR(-ENODEV);
718         }
719
720         if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
721                 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
722                 return ERR_PTR(-ENODEV);
723         }
724
725         host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
726         if (IS_ERR(host)) {
727                 dev_err(&pdev->dev, "cannot allocate host\n");
728                 return ERR_CAST(host);
729         }
730
731         slot = sdhci_priv(host);
732
733         slot->chip = chip;
734         slot->host = host;
735         slot->pci_bar = bar;
736
737         host->hw_name = "PCI";
738         host->ops = &sdhci_pci_ops;
739         host->quirks = chip->quirks;
740
741         host->irq = pdev->irq;
742
743         ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
744         if (ret) {
745                 dev_err(&pdev->dev, "cannot request region\n");
746                 goto free;
747         }
748
749         addr = pci_resource_start(pdev, bar);
750         host->ioaddr = pci_ioremap_bar(pdev, bar);
751         if (!host->ioaddr) {
752                 dev_err(&pdev->dev, "failed to remap registers\n");
753                 goto release;
754         }
755
756         if (chip->fixes && chip->fixes->probe_slot) {
757                 ret = chip->fixes->probe_slot(slot);
758                 if (ret)
759                         goto unmap;
760         }
761
762         host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
763
764         ret = sdhci_add_host(host);
765         if (ret)
766                 goto remove;
767
768         return slot;
769
770 remove:
771         if (chip->fixes && chip->fixes->remove_slot)
772                 chip->fixes->remove_slot(slot, 0);
773
774 unmap:
775         iounmap(host->ioaddr);
776
777 release:
778         pci_release_region(pdev, bar);
779
780 free:
781         sdhci_free_host(host);
782
783         return ERR_PTR(ret);
784 }
785
786 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
787 {
788         int dead;
789         u32 scratch;
790
791         dead = 0;
792         scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
793         if (scratch == (u32)-1)
794                 dead = 1;
795
796         sdhci_remove_host(slot->host, dead);
797
798         if (slot->chip->fixes && slot->chip->fixes->remove_slot)
799                 slot->chip->fixes->remove_slot(slot, dead);
800
801         pci_release_region(slot->chip->pdev, slot->pci_bar);
802
803         sdhci_free_host(slot->host);
804 }
805
806 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
807                                      const struct pci_device_id *ent)
808 {
809         struct sdhci_pci_chip *chip;
810         struct sdhci_pci_slot *slot;
811
812         u8 slots, rev, first_bar;
813         int ret, i;
814
815         BUG_ON(pdev == NULL);
816         BUG_ON(ent == NULL);
817
818         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
819
820         dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
821                  (int)pdev->vendor, (int)pdev->device, (int)rev);
822
823         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
824         if (ret)
825                 return ret;
826
827         slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
828         dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
829         if (slots == 0)
830                 return -ENODEV;
831
832         BUG_ON(slots > MAX_SLOTS);
833
834         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
835         if (ret)
836                 return ret;
837
838         first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
839
840         if (first_bar > 5) {
841                 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
842                 return -ENODEV;
843         }
844
845         ret = pci_enable_device(pdev);
846         if (ret)
847                 return ret;
848
849         chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
850         if (!chip) {
851                 ret = -ENOMEM;
852                 goto err;
853         }
854
855         chip->pdev = pdev;
856         chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
857         if (chip->fixes)
858                 chip->quirks = chip->fixes->quirks;
859         chip->num_slots = slots;
860
861         pci_set_drvdata(pdev, chip);
862
863         if (chip->fixes && chip->fixes->probe) {
864                 ret = chip->fixes->probe(chip);
865                 if (ret)
866                         goto free;
867         }
868
869         slots = chip->num_slots;        /* Quirk may have changed this */
870
871         for (i = 0;i < slots;i++) {
872                 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
873                 if (IS_ERR(slot)) {
874                         for (i--;i >= 0;i--)
875                                 sdhci_pci_remove_slot(chip->slots[i]);
876                         ret = PTR_ERR(slot);
877                         goto free;
878                 }
879
880                 chip->slots[i] = slot;
881         }
882
883         return 0;
884
885 free:
886         pci_set_drvdata(pdev, NULL);
887         kfree(chip);
888
889 err:
890         pci_disable_device(pdev);
891         return ret;
892 }
893
894 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
895 {
896         int i;
897         struct sdhci_pci_chip *chip;
898
899         chip = pci_get_drvdata(pdev);
900
901         if (chip) {
902                 for (i = 0;i < chip->num_slots; i++)
903                         sdhci_pci_remove_slot(chip->slots[i]);
904
905                 pci_set_drvdata(pdev, NULL);
906                 kfree(chip);
907         }
908
909         pci_disable_device(pdev);
910 }
911
912 static struct pci_driver sdhci_driver = {
913         .name =         "sdhci-pci",
914         .id_table =     pci_ids,
915         .probe =        sdhci_pci_probe,
916         .remove =       __devexit_p(sdhci_pci_remove),
917         .suspend =      sdhci_pci_suspend,
918         .resume =       sdhci_pci_resume,
919 };
920
921 /*****************************************************************************\
922  *                                                                           *
923  * Driver init/exit                                                          *
924  *                                                                           *
925 \*****************************************************************************/
926
927 static int __init sdhci_drv_init(void)
928 {
929         return pci_register_driver(&sdhci_driver);
930 }
931
932 static void __exit sdhci_drv_exit(void)
933 {
934         pci_unregister_driver(&sdhci_driver);
935 }
936
937 module_init(sdhci_drv_init);
938 module_exit(sdhci_drv_exit);
939
940 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
941 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
942 MODULE_LICENSE("GPL");