blob: ef77ed1bd114b63c584cab8642c738423a3e2198 [file] [log] [blame]
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001/* 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
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010041struct sdhci_pci_chip;
42
Pierre Ossman22606402008-03-23 19:33:23 +010043struct sdhci_pci_fixes {
44 unsigned int quirks;
45
46 int (*probe)(struct sdhci_pci_chip*);
Pierre Ossman45211e22008-03-24 13:09:09 +010047
48 int (*resume)(struct sdhci_pci_chip*);
Pierre Ossman22606402008-03-23 19:33:23 +010049};
50
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010051struct sdhci_pci_slot {
52 struct sdhci_pci_chip *chip;
53 struct sdhci_host *host;
54
55 int pci_bar;
56};
57
58struct sdhci_pci_chip {
59 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010060
61 unsigned int quirks;
62 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010063
64 int num_slots; /* Slots on controller */
65 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
66};
67
Pierre Ossman22606402008-03-23 19:33:23 +010068
69/*****************************************************************************\
70 * *
71 * Hardware specific quirk handling *
72 * *
73\*****************************************************************************/
74
75static int ricoh_probe(struct sdhci_pci_chip *chip)
76{
77 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
78 chip->quirks |= SDHCI_QUIRK_CLOCK_BEFORE_RESET;
79
80 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG)
81 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
82
83 return 0;
84}
85
86static const struct sdhci_pci_fixes sdhci_ricoh = {
87 .probe = ricoh_probe,
88};
89
90static const struct sdhci_pci_fixes sdhci_ene_712 = {
91 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
92 SDHCI_QUIRK_BROKEN_DMA,
93};
94
95static const struct sdhci_pci_fixes sdhci_ene_714 = {
96 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
97 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
98 SDHCI_QUIRK_BROKEN_DMA,
99};
100
101static const struct sdhci_pci_fixes sdhci_cafe = {
102 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200103 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100104};
105
Pierre Ossman45211e22008-03-24 13:09:09 +0100106static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
107{
108 u8 scratch;
109 int ret;
110
111 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
112 if (ret)
113 return ret;
114
115 /*
116 * Turn PMOS on [bit 0], set over current detection to 2.4 V
117 * [bit 1:2] and enable over current debouncing [bit 6].
118 */
119 if (on)
120 scratch |= 0x47;
121 else
122 scratch &= ~0x47;
123
124 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
125 if (ret)
126 return ret;
127
128 return 0;
129}
130
131static int jmicron_probe(struct sdhci_pci_chip *chip)
132{
133 int ret;
134
135 /*
136 * JMicron chips need a bit of a nudge to enable the power
137 * output pins.
138 */
139 ret = jmicron_pmos(chip, 1);
140 if (ret) {
141 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
142 return ret;
143 }
144
145 return 0;
146}
147
148static int jmicron_resume(struct sdhci_pci_chip *chip)
149{
150 int ret;
151
152 ret = jmicron_pmos(chip, 1);
153 if (ret) {
154 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
155 return ret;
156 }
157
158 return 0;
159}
160
Pierre Ossman22606402008-03-23 19:33:23 +0100161static const struct sdhci_pci_fixes sdhci_jmicron = {
162 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
163 SDHCI_QUIRK_32BIT_DMA_SIZE |
164 SDHCI_QUIRK_RESET_AFTER_REQUEST,
Pierre Ossman45211e22008-03-24 13:09:09 +0100165
166 .probe = jmicron_probe,
167
168 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100169};
170
171static const struct pci_device_id pci_ids[] __devinitdata = {
172 {
173 .vendor = PCI_VENDOR_ID_RICOH,
174 .device = PCI_DEVICE_ID_RICOH_R5C822,
175 .subvendor = PCI_ANY_ID,
176 .subdevice = PCI_ANY_ID,
177 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
178 },
179
180 {
181 .vendor = PCI_VENDOR_ID_ENE,
182 .device = PCI_DEVICE_ID_ENE_CB712_SD,
183 .subvendor = PCI_ANY_ID,
184 .subdevice = PCI_ANY_ID,
185 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
186 },
187
188 {
189 .vendor = PCI_VENDOR_ID_ENE,
190 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
191 .subvendor = PCI_ANY_ID,
192 .subdevice = PCI_ANY_ID,
193 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
194 },
195
196 {
197 .vendor = PCI_VENDOR_ID_ENE,
198 .device = PCI_DEVICE_ID_ENE_CB714_SD,
199 .subvendor = PCI_ANY_ID,
200 .subdevice = PCI_ANY_ID,
201 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
202 },
203
204 {
205 .vendor = PCI_VENDOR_ID_ENE,
206 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
207 .subvendor = PCI_ANY_ID,
208 .subdevice = PCI_ANY_ID,
209 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
210 },
211
212 {
213 .vendor = PCI_VENDOR_ID_MARVELL,
214 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
215 .subvendor = PCI_ANY_ID,
216 .subdevice = PCI_ANY_ID,
217 .driver_data = (kernel_ulong_t)&sdhci_cafe,
218 },
219
220 {
221 .vendor = PCI_VENDOR_ID_JMICRON,
222 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
223 .subvendor = PCI_ANY_ID,
224 .subdevice = PCI_ANY_ID,
225 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
226 },
227
228 { /* Generic SD host controller */
229 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
230 },
231
232 { /* end: all zeroes */ },
233};
234
235MODULE_DEVICE_TABLE(pci, pci_ids);
236
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100237/*****************************************************************************\
238 * *
239 * SDHCI core callbacks *
240 * *
241\*****************************************************************************/
242
243static int sdhci_pci_enable_dma(struct sdhci_host *host)
244{
245 struct sdhci_pci_slot *slot;
246 struct pci_dev *pdev;
247 int ret;
248
249 slot = sdhci_priv(host);
250 pdev = slot->chip->pdev;
251
252 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
253 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
254 (host->flags & SDHCI_USE_DMA)) {
255 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
256 "doesn't fully claim to support it.\n");
257 }
258
259 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
260 if (ret)
261 return ret;
262
263 pci_set_master(pdev);
264
265 return 0;
266}
267
268static struct sdhci_ops sdhci_pci_ops = {
269 .enable_dma = sdhci_pci_enable_dma,
270};
271
272/*****************************************************************************\
273 * *
274 * Suspend/resume *
275 * *
276\*****************************************************************************/
277
278#ifdef CONFIG_PM
279
280static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
281{
282 struct sdhci_pci_chip *chip;
283 struct sdhci_pci_slot *slot;
284 int i, ret;
285
286 chip = pci_get_drvdata(pdev);
287 if (!chip)
288 return 0;
289
290 for (i = 0;i < chip->num_slots;i++) {
291 slot = chip->slots[i];
292 if (!slot)
293 continue;
294
295 ret = sdhci_suspend_host(slot->host, state);
296
297 if (ret) {
298 for (i--;i >= 0;i--)
299 sdhci_resume_host(chip->slots[i]->host);
300 return ret;
301 }
302 }
303
304 pci_save_state(pdev);
305 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
306 pci_disable_device(pdev);
307 pci_set_power_state(pdev, pci_choose_state(pdev, state));
308
309 return 0;
310}
311
312static int sdhci_pci_resume (struct pci_dev *pdev)
313{
314 struct sdhci_pci_chip *chip;
315 struct sdhci_pci_slot *slot;
316 int i, ret;
317
318 chip = pci_get_drvdata(pdev);
319 if (!chip)
320 return 0;
321
322 pci_set_power_state(pdev, PCI_D0);
323 pci_restore_state(pdev);
324 ret = pci_enable_device(pdev);
325 if (ret)
326 return ret;
327
Pierre Ossman45211e22008-03-24 13:09:09 +0100328 if (chip->fixes && chip->fixes->resume) {
329 ret = chip->fixes->resume(chip);
330 if (ret)
331 return ret;
332 }
333
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100334 for (i = 0;i < chip->num_slots;i++) {
335 slot = chip->slots[i];
336 if (!slot)
337 continue;
338
339 ret = sdhci_resume_host(slot->host);
340 if (ret)
341 return ret;
342 }
343
344 return 0;
345}
346
347#else /* CONFIG_PM */
348
349#define sdhci_pci_suspend NULL
350#define sdhci_pci_resume NULL
351
352#endif /* CONFIG_PM */
353
354/*****************************************************************************\
355 * *
356 * Device probing/removal *
357 * *
358\*****************************************************************************/
359
360static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
361 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
362{
363 struct sdhci_pci_slot *slot;
364 struct sdhci_host *host;
365
366 resource_size_t addr;
367
368 int ret;
369
370 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
371 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
372 return ERR_PTR(-ENODEV);
373 }
374
375 if (pci_resource_len(pdev, bar) != 0x100) {
376 dev_err(&pdev->dev, "Invalid iomem size. You may "
377 "experience problems.\n");
378 }
379
380 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
381 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
382 return ERR_PTR(-ENODEV);
383 }
384
385 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
386 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
387 return ERR_PTR(-ENODEV);
388 }
389
390 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
391 if (IS_ERR(host)) {
392 ret = PTR_ERR(host);
393 goto unmap;
394 }
395
396 slot = sdhci_priv(host);
397
398 slot->chip = chip;
399 slot->host = host;
400 slot->pci_bar = bar;
401
402 host->hw_name = "PCI";
403 host->ops = &sdhci_pci_ops;
404 host->quirks = chip->quirks;
405
406 host->irq = pdev->irq;
407
408 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
409 if (ret) {
410 dev_err(&pdev->dev, "cannot request region\n");
411 return ERR_PTR(ret);
412 }
413
414 addr = pci_resource_start(pdev, bar);
415 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
416 if (!host->ioaddr) {
417 dev_err(&pdev->dev, "failed to remap registers\n");
418 goto release;
419 }
420
421 ret = sdhci_add_host(host);
422 if (ret)
423 goto unmap;
424
425 return slot;
426
427unmap:
428 iounmap(host->ioaddr);
429
430release:
431 pci_release_region(pdev, bar);
432 sdhci_free_host(host);
433
434 return ERR_PTR(ret);
435}
436
437static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
438{
439 sdhci_remove_host(slot->host);
440 pci_release_region(slot->chip->pdev, slot->pci_bar);
441 sdhci_free_host(slot->host);
442}
443
444static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
445 const struct pci_device_id *ent)
446{
447 struct sdhci_pci_chip *chip;
448 struct sdhci_pci_slot *slot;
449
450 u8 slots, rev, first_bar;
451 int ret, i;
452
453 BUG_ON(pdev == NULL);
454 BUG_ON(ent == NULL);
455
456 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
457
458 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
459 (int)pdev->vendor, (int)pdev->device, (int)rev);
460
461 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
462 if (ret)
463 return ret;
464
465 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
466 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
467 if (slots == 0)
468 return -ENODEV;
469
470 BUG_ON(slots > MAX_SLOTS);
471
472 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
473 if (ret)
474 return ret;
475
476 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
477
478 if (first_bar > 5) {
479 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
480 return -ENODEV;
481 }
482
483 ret = pci_enable_device(pdev);
484 if (ret)
485 return ret;
486
487 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
488 if (!chip) {
489 ret = -ENOMEM;
490 goto err;
491 }
492
493 chip->pdev = pdev;
Pierre Ossman22606402008-03-23 19:33:23 +0100494 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
495 if (chip->fixes)
496 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100497 chip->num_slots = slots;
498
499 pci_set_drvdata(pdev, chip);
500
Pierre Ossman22606402008-03-23 19:33:23 +0100501 if (chip->fixes && chip->fixes->probe) {
502 ret = chip->fixes->probe(chip);
503 if (ret)
504 goto free;
505 }
506
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100507 for (i = 0;i < slots;i++) {
508 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
509 if (IS_ERR(slot)) {
510 for (i--;i >= 0;i--)
511 sdhci_pci_remove_slot(chip->slots[i]);
512 ret = PTR_ERR(slot);
513 goto free;
514 }
515
516 chip->slots[i] = slot;
517 }
518
519 return 0;
520
521free:
522 pci_set_drvdata(pdev, NULL);
523 kfree(chip);
524
525err:
526 pci_disable_device(pdev);
527 return ret;
528}
529
530static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
531{
532 int i;
533 struct sdhci_pci_chip *chip;
534
535 chip = pci_get_drvdata(pdev);
536
537 if (chip) {
538 for (i = 0;i < chip->num_slots; i++)
539 sdhci_pci_remove_slot(chip->slots[i]);
540
541 pci_set_drvdata(pdev, NULL);
542 kfree(chip);
543 }
544
545 pci_disable_device(pdev);
546}
547
548static struct pci_driver sdhci_driver = {
549 .name = "sdhci-pci",
550 .id_table = pci_ids,
551 .probe = sdhci_pci_probe,
552 .remove = __devexit_p(sdhci_pci_remove),
553 .suspend = sdhci_pci_suspend,
554 .resume = sdhci_pci_resume,
555};
556
557/*****************************************************************************\
558 * *
559 * Driver init/exit *
560 * *
561\*****************************************************************************/
562
563static int __init sdhci_drv_init(void)
564{
565 return pci_register_driver(&sdhci_driver);
566}
567
568static void __exit sdhci_drv_exit(void)
569{
570 pci_unregister_driver(&sdhci_driver);
571}
572
573module_init(sdhci_drv_init);
574module_exit(sdhci_drv_exit);
575
576MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
577MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
578MODULE_LICENSE("GPL");