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