blob: ec4b81dd493f0be13a3bb66ae92782f97859a0be [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Maxim Levitskyccc92c22010-08-10 18:01:42 -070020#include <linux/device.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010021
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
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010043struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020044struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010045
Pierre Ossman22606402008-03-23 19:33:23 +010046struct sdhci_pci_fixes {
47 unsigned int quirks;
48
49 int (*probe)(struct sdhci_pci_chip*);
Pierre Ossman45211e22008-03-24 13:09:09 +010050
Pierre Ossman44894282008-04-04 19:36:59 +020051 int (*probe_slot)(struct sdhci_pci_slot*);
Pierre Ossman1e728592008-04-16 19:13:13 +020052 void (*remove_slot)(struct sdhci_pci_slot*, int);
Pierre Ossman44894282008-04-04 19:36:59 +020053
54 int (*suspend)(struct sdhci_pci_chip*,
55 pm_message_t);
Pierre Ossman45211e22008-03-24 13:09:09 +010056 int (*resume)(struct sdhci_pci_chip*);
Pierre Ossman22606402008-03-23 19:33:23 +010057};
58
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010059struct sdhci_pci_slot {
60 struct sdhci_pci_chip *chip;
61 struct sdhci_host *host;
62
63 int pci_bar;
64};
65
66struct sdhci_pci_chip {
67 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010068
69 unsigned int quirks;
70 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010071
72 int num_slots; /* Slots on controller */
73 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
74};
75
Pierre Ossman22606402008-03-23 19:33:23 +010076
77/*****************************************************************************\
78 * *
79 * Hardware specific quirk handling *
80 * *
81\*****************************************************************************/
82
83static int ricoh_probe(struct sdhci_pci_chip *chip)
84{
Chris Ballc99436f2009-09-22 16:45:22 -070085 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
86 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010087 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070088 return 0;
89}
Pierre Ossman22606402008-03-23 19:33:23 +010090
Maxim Levitskyccc92c22010-08-10 18:01:42 -070091static 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
106static 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);
Pierre Ossman22606402008-03-23 19:33:23 +0100112 return 0;
113}
114
115static const struct sdhci_pci_fixes sdhci_ricoh = {
116 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800117 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
118 SDHCI_QUIRK_FORCE_DMA |
119 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100120};
121
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700122static 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
Pierre Ossman22606402008-03-23 19:33:23 +0100131static const struct sdhci_pci_fixes sdhci_ene_712 = {
132 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
133 SDHCI_QUIRK_BROKEN_DMA,
134};
135
136static 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
142static const struct sdhci_pci_fixes sdhci_cafe = {
143 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100144 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200145 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100146};
147
Xiaochen Shen29229052010-10-04 15:24:52 +0100148static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
149 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
150};
151
152static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc_sdio = {
153 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
154};
155
Pierre Ossman45211e22008-03-24 13:09:09 +0100156static 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
181static int jmicron_probe(struct sdhci_pci_chip *chip)
182{
183 int ret;
184
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200185 if (chip->pdev->revision == 0) {
186 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
187 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200188 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200189 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100190 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200191 }
192
Pierre Ossman45211e22008-03-24 13:09:09 +0100193 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200194 * 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 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100226 * 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
Pierre Ossman44894282008-04-04 19:36:59 +0200238static 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
252static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
253{
Pierre Ossman2134a922008-06-28 18:28:51 +0200254 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
Pierre Ossman44894282008-04-04 19:36:59 +0200270 /*
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
Pierre Ossman1e728592008-04-16 19:13:13 +0200280static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200281{
Pierre Ossman1e728592008-04-16 19:13:13 +0200282 if (dead)
283 return;
284
Pierre Ossman44894282008-04-04 19:36:59 +0200285 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
286 jmicron_enable_mmc(slot->host, 0);
287}
288
289static 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
Pierre Ossman45211e22008-03-24 13:09:09 +0100301static int jmicron_resume(struct sdhci_pci_chip *chip)
302{
Pierre Ossman44894282008-04-04 19:36:59 +0200303 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 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100309
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
Pierre Ossman22606402008-03-23 19:33:23 +0100319static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100320 .probe = jmicron_probe,
321
Pierre Ossman44894282008-04-04 19:36:59 +0200322 .probe_slot = jmicron_probe_slot,
323 .remove_slot = jmicron_remove_slot,
324
325 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100326 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100327};
328
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800329/* 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
348static 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
357static 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
390static 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
Harald Welte557b0692009-06-18 16:53:38 +0200396static 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
404static const struct sdhci_pci_fixes sdhci_via = {
405 .probe = via_probe,
406};
407
Pierre Ossman22606402008-03-23 19:33:23 +0100408static 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 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700418 .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 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700426 .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 {
Pierre Ossman22606402008-03-23 19:33:23 +0100434 .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,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100467 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100468 .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
Pierre Ossman44894282008-04-04 19:36:59 +0200481 {
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
Harald Welte557b0692009-06-18 16:53:38 +0200489 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800490 .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 {
Harald Welte557b0692009-06-18 16:53:38 +0200498 .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
Xiaochen Shen29229052010-10-04 15:24:52 +0100505 {
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
Pierre Ossman22606402008-03-23 19:33:23 +0100545 { /* Generic SD host controller */
546 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
547 },
548
549 { /* end: all zeroes */ },
550};
551
552MODULE_DEVICE_TABLE(pci, pci_ids);
553
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100554/*****************************************************************************\
555 * *
556 * SDHCI core callbacks *
557 * *
558\*****************************************************************************/
559
560static 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) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700571 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100572 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
573 "doesn't fully claim to support it.\n");
574 }
575
Yang Hongyang284901a2009-04-06 19:01:15 -0700576 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100577 if (ret)
578 return ret;
579
580 pci_set_master(pdev);
581
582 return 0;
583}
584
585static 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
597static 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;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800601 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100602 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 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800620
621 pm_flags |= slot->host->mmc->pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100622 }
623
Pierre Ossman44894282008-04-04 19:36:59 +0200624 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
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100633 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800634 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 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100643
644 return 0;
645}
646
647static 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
Pierre Ossman45211e22008-03-24 13:09:09 +0100663 if (chip->fixes && chip->fixes->resume) {
664 ret = chip->fixes->resume(chip);
665 if (ret)
666 return ret;
667 }
668
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100669 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
695static 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)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200727 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -0700728 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100729 }
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");
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200746 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100747 }
748
749 addr = pci_resource_start(pdev, bar);
Arjan van de Ven092f82e2008-09-28 16:15:56 -0700750 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100751 if (!host->ioaddr) {
752 dev_err(&pdev->dev, "failed to remap registers\n");
753 goto release;
754 }
755
Pierre Ossman44894282008-04-04 19:36:59 +0200756 if (chip->fixes && chip->fixes->probe_slot) {
757 ret = chip->fixes->probe_slot(slot);
758 if (ret)
759 goto unmap;
760 }
761
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800762 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
763
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100764 ret = sdhci_add_host(host);
765 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +0200766 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100767
768 return slot;
769
Pierre Ossman44894282008-04-04 19:36:59 +0200770remove:
771 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +0200772 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +0200773
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100774unmap:
775 iounmap(host->ioaddr);
776
777release:
778 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200779
780free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100781 sdhci_free_host(host);
782
783 return ERR_PTR(ret);
784}
785
786static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
787{
Pierre Ossman1e728592008-04-16 19:13:13 +0200788 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);
Pierre Ossman44894282008-04-04 19:36:59 +0200797
798 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +0200799 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +0200800
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100801 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +0200802
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100803 sdhci_free_host(slot->host);
804}
805
806static 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;
Pierre Ossman22606402008-03-23 19:33:23 +0100856 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
857 if (chip->fixes)
858 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100859 chip->num_slots = slots;
860
861 pci_set_drvdata(pdev, chip);
862
Pierre Ossman22606402008-03-23 19:33:23 +0100863 if (chip->fixes && chip->fixes->probe) {
864 ret = chip->fixes->probe(chip);
865 if (ret)
866 goto free;
867 }
868
Alan Cox225d85f2010-10-04 15:24:21 +0100869 slots = chip->num_slots; /* Quirk may have changed this */
870
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100871 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
885free:
886 pci_set_drvdata(pdev, NULL);
887 kfree(chip);
888
889err:
890 pci_disable_device(pdev);
891 return ret;
892}
893
894static 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
912static 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
927static int __init sdhci_drv_init(void)
928{
929 return pci_register_driver(&sdhci_driver);
930}
931
932static void __exit sdhci_drv_exit(void)
933{
934 pci_unregister_driver(&sdhci_driver);
935}
936
937module_init(sdhci_drv_init);
938module_exit(sdhci_drv_exit);
939
Pierre Ossman32710e82009-04-08 20:14:54 +0200940MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100941MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
942MODULE_LICENSE("GPL");