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