]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/host/sdhci-pci.c
8554466e0f4e3200620313fd3f9d72a64544a3ac
[linux-2.6-omap-h63xx.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
20 #include <linux/mmc/host.h>
21
22 #include <asm/scatterlist.h>
23 #include <asm/io.h>
24
25 #include "sdhci.h"
26
27 /*
28  * PCI registers
29  */
30
31 #define PCI_SDHCI_IFPIO                 0x00
32 #define PCI_SDHCI_IFDMA                 0x01
33 #define PCI_SDHCI_IFVENDOR              0x02
34
35 #define PCI_SLOT_INFO                   0x40    /* 8 bits */
36 #define  PCI_SLOT_INFO_SLOTS(x)         ((x >> 4) & 7)
37 #define  PCI_SLOT_INFO_FIRST_BAR_MASK   0x07
38
39 #define MAX_SLOTS                       8
40
41 struct sdhci_pci_chip;
42 struct sdhci_pci_slot;
43
44 struct sdhci_pci_fixes {
45         unsigned int            quirks;
46
47         int                     (*probe)(struct sdhci_pci_chip*);
48
49         int                     (*probe_slot)(struct sdhci_pci_slot*);
50         void                    (*remove_slot)(struct sdhci_pci_slot*, int);
51
52         int                     (*suspend)(struct sdhci_pci_chip*,
53                                         pm_message_t);
54         int                     (*resume)(struct sdhci_pci_chip*);
55 };
56
57 struct sdhci_pci_slot {
58         struct sdhci_pci_chip   *chip;
59         struct sdhci_host       *host;
60
61         int                     pci_bar;
62 };
63
64 struct sdhci_pci_chip {
65         struct pci_dev          *pdev;
66
67         unsigned int            quirks;
68         const struct sdhci_pci_fixes *fixes;
69
70         int                     num_slots;      /* Slots on controller */
71         struct sdhci_pci_slot   *slots[MAX_SLOTS]; /* Pointers to host slots */
72 };
73
74
75 /*****************************************************************************\
76  *                                                                           *
77  * Hardware specific quirk handling                                          *
78  *                                                                           *
79 \*****************************************************************************/
80
81 static int ricoh_probe(struct sdhci_pci_chip *chip)
82 {
83         if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
84                 chip->quirks |= SDHCI_QUIRK_CLOCK_BEFORE_RESET;
85
86         if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG)
87                 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
88
89         return 0;
90 }
91
92 static const struct sdhci_pci_fixes sdhci_ricoh = {
93         .probe          = ricoh_probe,
94 };
95
96 static const struct sdhci_pci_fixes sdhci_ene_712 = {
97         .quirks         = SDHCI_QUIRK_SINGLE_POWER_WRITE |
98                           SDHCI_QUIRK_BROKEN_DMA,
99 };
100
101 static const struct sdhci_pci_fixes sdhci_ene_714 = {
102         .quirks         = SDHCI_QUIRK_SINGLE_POWER_WRITE |
103                           SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
104                           SDHCI_QUIRK_BROKEN_DMA,
105 };
106
107 static const struct sdhci_pci_fixes sdhci_cafe = {
108         .quirks         = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
109                           SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
110 };
111
112 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
113 {
114         u8 scratch;
115         int ret;
116
117         ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
118         if (ret)
119                 return ret;
120
121         /*
122          * Turn PMOS on [bit 0], set over current detection to 2.4 V
123          * [bit 1:2] and enable over current debouncing [bit 6].
124          */
125         if (on)
126                 scratch |= 0x47;
127         else
128                 scratch &= ~0x47;
129
130         ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
131         if (ret)
132                 return ret;
133
134         return 0;
135 }
136
137 static int jmicron_probe(struct sdhci_pci_chip *chip)
138 {
139         int ret;
140
141         /*
142          * JMicron chips can have two interfaces to the same hardware
143          * in order to work around limitations in Microsoft's driver.
144          * We need to make sure we only bind to one of them.
145          *
146          * This code assumes two things:
147          *
148          * 1. The PCI code adds subfunctions in order.
149          *
150          * 2. The MMC interface has a lower subfunction number
151          *    than the SD interface.
152          */
153         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
154                 struct pci_dev *sd_dev;
155
156                 sd_dev = NULL;
157                 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
158                         PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
159                         if ((PCI_SLOT(chip->pdev->devfn) ==
160                                 PCI_SLOT(sd_dev->devfn)) &&
161                                 (chip->pdev->bus == sd_dev->bus))
162                                 break;
163                 }
164
165                 if (sd_dev) {
166                         pci_dev_put(sd_dev);
167                         dev_info(&chip->pdev->dev, "Refusing to bind to "
168                                 "secondary interface.\n");
169                         return -ENODEV;
170                 }
171         }
172
173         /*
174          * JMicron chips need a bit of a nudge to enable the power
175          * output pins.
176          */
177         ret = jmicron_pmos(chip, 1);
178         if (ret) {
179                 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
180                 return ret;
181         }
182
183         return 0;
184 }
185
186 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
187 {
188         u8 scratch;
189
190         scratch = readb(host->ioaddr + 0xC0);
191
192         if (on)
193                 scratch |= 0x01;
194         else
195                 scratch &= ~0x01;
196
197         writeb(scratch, host->ioaddr + 0xC0);
198 }
199
200 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
201 {
202         /*
203          * The secondary interface requires a bit set to get the
204          * interrupts.
205          */
206         if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
207                 jmicron_enable_mmc(slot->host, 1);
208
209         return 0;
210 }
211
212 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
213 {
214         if (dead)
215                 return;
216
217         if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
218                 jmicron_enable_mmc(slot->host, 0);
219 }
220
221 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
222 {
223         int i;
224
225         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
226                 for (i = 0;i < chip->num_slots;i++)
227                         jmicron_enable_mmc(chip->slots[i]->host, 0);
228         }
229
230         return 0;
231 }
232
233 static int jmicron_resume(struct sdhci_pci_chip *chip)
234 {
235         int ret, i;
236
237         if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
238                 for (i = 0;i < chip->num_slots;i++)
239                         jmicron_enable_mmc(chip->slots[i]->host, 1);
240         }
241
242         ret = jmicron_pmos(chip, 1);
243         if (ret) {
244                 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
245                 return ret;
246         }
247
248         return 0;
249 }
250
251 static const struct sdhci_pci_fixes sdhci_jmicron = {
252         .quirks         = SDHCI_QUIRK_32BIT_DMA_ADDR |
253                           SDHCI_QUIRK_32BIT_DMA_SIZE |
254                           SDHCI_QUIRK_RESET_AFTER_REQUEST,
255
256         .probe          = jmicron_probe,
257
258         .probe_slot     = jmicron_probe_slot,
259         .remove_slot    = jmicron_remove_slot,
260
261         .suspend        = jmicron_suspend,
262         .resume         = jmicron_resume,
263 };
264
265 static const struct pci_device_id pci_ids[] __devinitdata = {
266         {
267                 .vendor         = PCI_VENDOR_ID_RICOH,
268                 .device         = PCI_DEVICE_ID_RICOH_R5C822,
269                 .subvendor      = PCI_ANY_ID,
270                 .subdevice      = PCI_ANY_ID,
271                 .driver_data    = (kernel_ulong_t)&sdhci_ricoh,
272         },
273
274         {
275                 .vendor         = PCI_VENDOR_ID_ENE,
276                 .device         = PCI_DEVICE_ID_ENE_CB712_SD,
277                 .subvendor      = PCI_ANY_ID,
278                 .subdevice      = PCI_ANY_ID,
279                 .driver_data    = (kernel_ulong_t)&sdhci_ene_712,
280         },
281
282         {
283                 .vendor         = PCI_VENDOR_ID_ENE,
284                 .device         = PCI_DEVICE_ID_ENE_CB712_SD_2,
285                 .subvendor      = PCI_ANY_ID,
286                 .subdevice      = PCI_ANY_ID,
287                 .driver_data    = (kernel_ulong_t)&sdhci_ene_712,
288         },
289
290         {
291                 .vendor         = PCI_VENDOR_ID_ENE,
292                 .device         = PCI_DEVICE_ID_ENE_CB714_SD,
293                 .subvendor      = PCI_ANY_ID,
294                 .subdevice      = PCI_ANY_ID,
295                 .driver_data    = (kernel_ulong_t)&sdhci_ene_714,
296         },
297
298         {
299                 .vendor         = PCI_VENDOR_ID_ENE,
300                 .device         = PCI_DEVICE_ID_ENE_CB714_SD_2,
301                 .subvendor      = PCI_ANY_ID,
302                 .subdevice      = PCI_ANY_ID,
303                 .driver_data    = (kernel_ulong_t)&sdhci_ene_714,
304         },
305
306         {
307                 .vendor         = PCI_VENDOR_ID_MARVELL,
308                 .device         = PCI_DEVICE_ID_MARVELL_CAFE_SD,
309                 .subvendor      = PCI_ANY_ID,
310                 .subdevice      = PCI_ANY_ID,
311                 .driver_data    = (kernel_ulong_t)&sdhci_cafe,
312         },
313
314         {
315                 .vendor         = PCI_VENDOR_ID_JMICRON,
316                 .device         = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
317                 .subvendor      = PCI_ANY_ID,
318                 .subdevice      = PCI_ANY_ID,
319                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
320         },
321
322         {
323                 .vendor         = PCI_VENDOR_ID_JMICRON,
324                 .device         = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
325                 .subvendor      = PCI_ANY_ID,
326                 .subdevice      = PCI_ANY_ID,
327                 .driver_data    = (kernel_ulong_t)&sdhci_jmicron,
328         },
329
330         {       /* Generic SD host controller */
331                 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
332         },
333
334         { /* end: all zeroes */ },
335 };
336
337 MODULE_DEVICE_TABLE(pci, pci_ids);
338
339 /*****************************************************************************\
340  *                                                                           *
341  * SDHCI core callbacks                                                      *
342  *                                                                           *
343 \*****************************************************************************/
344
345 static int sdhci_pci_enable_dma(struct sdhci_host *host)
346 {
347         struct sdhci_pci_slot *slot;
348         struct pci_dev *pdev;
349         int ret;
350
351         slot = sdhci_priv(host);
352         pdev = slot->chip->pdev;
353
354         if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
355                 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
356                 (host->flags & SDHCI_USE_DMA)) {
357                 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
358                         "doesn't fully claim to support it.\n");
359         }
360
361         ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
362         if (ret)
363                 return ret;
364
365         pci_set_master(pdev);
366
367         return 0;
368 }
369
370 static struct sdhci_ops sdhci_pci_ops = {
371         .enable_dma     = sdhci_pci_enable_dma,
372 };
373
374 /*****************************************************************************\
375  *                                                                           *
376  * Suspend/resume                                                            *
377  *                                                                           *
378 \*****************************************************************************/
379
380 #ifdef CONFIG_PM
381
382 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
383 {
384         struct sdhci_pci_chip *chip;
385         struct sdhci_pci_slot *slot;
386         int i, ret;
387
388         chip = pci_get_drvdata(pdev);
389         if (!chip)
390                 return 0;
391
392         for (i = 0;i < chip->num_slots;i++) {
393                 slot = chip->slots[i];
394                 if (!slot)
395                         continue;
396
397                 ret = sdhci_suspend_host(slot->host, state);
398
399                 if (ret) {
400                         for (i--;i >= 0;i--)
401                                 sdhci_resume_host(chip->slots[i]->host);
402                         return ret;
403                 }
404         }
405
406         if (chip->fixes && chip->fixes->suspend) {
407                 ret = chip->fixes->suspend(chip, state);
408                 if (ret) {
409                         for (i = chip->num_slots - 1;i >= 0;i--)
410                                 sdhci_resume_host(chip->slots[i]->host);
411                         return ret;
412                 }
413         }
414
415         pci_save_state(pdev);
416         pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
417         pci_disable_device(pdev);
418         pci_set_power_state(pdev, pci_choose_state(pdev, state));
419
420         return 0;
421 }
422
423 static int sdhci_pci_resume (struct pci_dev *pdev)
424 {
425         struct sdhci_pci_chip *chip;
426         struct sdhci_pci_slot *slot;
427         int i, ret;
428
429         chip = pci_get_drvdata(pdev);
430         if (!chip)
431                 return 0;
432
433         pci_set_power_state(pdev, PCI_D0);
434         pci_restore_state(pdev);
435         ret = pci_enable_device(pdev);
436         if (ret)
437                 return ret;
438
439         if (chip->fixes && chip->fixes->resume) {
440                 ret = chip->fixes->resume(chip);
441                 if (ret)
442                         return ret;
443         }
444
445         for (i = 0;i < chip->num_slots;i++) {
446                 slot = chip->slots[i];
447                 if (!slot)
448                         continue;
449
450                 ret = sdhci_resume_host(slot->host);
451                 if (ret)
452                         return ret;
453         }
454
455         return 0;
456 }
457
458 #else /* CONFIG_PM */
459
460 #define sdhci_pci_suspend NULL
461 #define sdhci_pci_resume NULL
462
463 #endif /* CONFIG_PM */
464
465 /*****************************************************************************\
466  *                                                                           *
467  * Device probing/removal                                                    *
468  *                                                                           *
469 \*****************************************************************************/
470
471 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
472         struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
473 {
474         struct sdhci_pci_slot *slot;
475         struct sdhci_host *host;
476
477         resource_size_t addr;
478
479         int ret;
480
481         if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
482                 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
483                 return ERR_PTR(-ENODEV);
484         }
485
486         if (pci_resource_len(pdev, bar) != 0x100) {
487                 dev_err(&pdev->dev, "Invalid iomem size. You may "
488                         "experience problems.\n");
489         }
490
491         if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
492                 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
493                 return ERR_PTR(-ENODEV);
494         }
495
496         if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
497                 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
498                 return ERR_PTR(-ENODEV);
499         }
500
501         host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
502         if (IS_ERR(host)) {
503                 ret = PTR_ERR(host);
504                 goto unmap;
505         }
506
507         slot = sdhci_priv(host);
508
509         slot->chip = chip;
510         slot->host = host;
511         slot->pci_bar = bar;
512
513         host->hw_name = "PCI";
514         host->ops = &sdhci_pci_ops;
515         host->quirks = chip->quirks;
516
517         host->irq = pdev->irq;
518
519         ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
520         if (ret) {
521                 dev_err(&pdev->dev, "cannot request region\n");
522                 return ERR_PTR(ret);
523         }
524
525         addr = pci_resource_start(pdev, bar);
526         host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
527         if (!host->ioaddr) {
528                 dev_err(&pdev->dev, "failed to remap registers\n");
529                 goto release;
530         }
531
532         if (chip->fixes && chip->fixes->probe_slot) {
533                 ret = chip->fixes->probe_slot(slot);
534                 if (ret)
535                         goto unmap;
536         }
537
538         ret = sdhci_add_host(host);
539         if (ret)
540                 goto remove;
541
542         return slot;
543
544 remove:
545         if (chip->fixes && chip->fixes->remove_slot)
546                 chip->fixes->remove_slot(slot, 0);
547
548 unmap:
549         iounmap(host->ioaddr);
550
551 release:
552         pci_release_region(pdev, bar);
553         sdhci_free_host(host);
554
555         return ERR_PTR(ret);
556 }
557
558 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
559 {
560         int dead;
561         u32 scratch;
562
563         dead = 0;
564         scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
565         if (scratch == (u32)-1)
566                 dead = 1;
567
568         sdhci_remove_host(slot->host, dead);
569
570         if (slot->chip->fixes && slot->chip->fixes->remove_slot)
571                 slot->chip->fixes->remove_slot(slot, dead);
572
573         pci_release_region(slot->chip->pdev, slot->pci_bar);
574
575         sdhci_free_host(slot->host);
576 }
577
578 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
579                                      const struct pci_device_id *ent)
580 {
581         struct sdhci_pci_chip *chip;
582         struct sdhci_pci_slot *slot;
583
584         u8 slots, rev, first_bar;
585         int ret, i;
586
587         BUG_ON(pdev == NULL);
588         BUG_ON(ent == NULL);
589
590         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
591
592         dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
593                  (int)pdev->vendor, (int)pdev->device, (int)rev);
594
595         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
596         if (ret)
597                 return ret;
598
599         slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
600         dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
601         if (slots == 0)
602                 return -ENODEV;
603
604         BUG_ON(slots > MAX_SLOTS);
605
606         ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
607         if (ret)
608                 return ret;
609
610         first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
611
612         if (first_bar > 5) {
613                 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
614                 return -ENODEV;
615         }
616
617         ret = pci_enable_device(pdev);
618         if (ret)
619                 return ret;
620
621         chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
622         if (!chip) {
623                 ret = -ENOMEM;
624                 goto err;
625         }
626
627         chip->pdev = pdev;
628         chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
629         if (chip->fixes)
630                 chip->quirks = chip->fixes->quirks;
631         chip->num_slots = slots;
632
633         pci_set_drvdata(pdev, chip);
634
635         if (chip->fixes && chip->fixes->probe) {
636                 ret = chip->fixes->probe(chip);
637                 if (ret)
638                         goto free;
639         }
640
641         for (i = 0;i < slots;i++) {
642                 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
643                 if (IS_ERR(slot)) {
644                         for (i--;i >= 0;i--)
645                                 sdhci_pci_remove_slot(chip->slots[i]);
646                         ret = PTR_ERR(slot);
647                         goto free;
648                 }
649
650                 chip->slots[i] = slot;
651         }
652
653         return 0;
654
655 free:
656         pci_set_drvdata(pdev, NULL);
657         kfree(chip);
658
659 err:
660         pci_disable_device(pdev);
661         return ret;
662 }
663
664 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
665 {
666         int i;
667         struct sdhci_pci_chip *chip;
668
669         chip = pci_get_drvdata(pdev);
670
671         if (chip) {
672                 for (i = 0;i < chip->num_slots; i++)
673                         sdhci_pci_remove_slot(chip->slots[i]);
674
675                 pci_set_drvdata(pdev, NULL);
676                 kfree(chip);
677         }
678
679         pci_disable_device(pdev);
680 }
681
682 static struct pci_driver sdhci_driver = {
683         .name =         "sdhci-pci",
684         .id_table =     pci_ids,
685         .probe =        sdhci_pci_probe,
686         .remove =       __devexit_p(sdhci_pci_remove),
687         .suspend =      sdhci_pci_suspend,
688         .resume =       sdhci_pci_resume,
689 };
690
691 /*****************************************************************************\
692  *                                                                           *
693  * Driver init/exit                                                          *
694  *                                                                           *
695 \*****************************************************************************/
696
697 static int __init sdhci_drv_init(void)
698 {
699         return pci_register_driver(&sdhci_driver);
700 }
701
702 static void __exit sdhci_drv_exit(void)
703 {
704         pci_unregister_driver(&sdhci_driver);
705 }
706
707 module_init(sdhci_drv_init);
708 module_exit(sdhci_drv_exit);
709
710 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
711 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
712 MODULE_LICENSE("GPL");