Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ACPI PATA driver |
| 3 | * |
| 4 | * (c) 2007 Red Hat <alan@redhat.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/pci.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <scsi/scsi_host.h> |
| 15 | #include <acpi/acpi_bus.h> |
| 16 | #include <acpi/acnames.h> |
| 17 | #include <acpi/acnamesp.h> |
| 18 | #include <acpi/acparser.h> |
| 19 | #include <acpi/acexcep.h> |
| 20 | #include <acpi/acmacros.h> |
| 21 | #include <acpi/actypes.h> |
| 22 | |
| 23 | #include <linux/libata.h> |
| 24 | #include <linux/ata.h> |
| 25 | |
| 26 | #define DRV_NAME "pata_acpi" |
| 27 | #define DRV_VERSION "0.2.3" |
| 28 | |
| 29 | struct pata_acpi { |
| 30 | struct ata_acpi_gtm gtm; |
| 31 | void *last; |
| 32 | unsigned long mask[2]; |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * pacpi_pre_reset - check for 40/80 pin |
| 37 | * @ap: Port |
| 38 | * @deadline: deadline jiffies for the operation |
| 39 | * |
| 40 | * Perform the PATA port setup we need. |
| 41 | */ |
| 42 | |
| 43 | static int pacpi_pre_reset(struct ata_link *link, unsigned long deadline) |
| 44 | { |
| 45 | struct ata_port *ap = link->ap; |
| 46 | struct pata_acpi *acpi = ap->private_data; |
| 47 | if (ap->acpi_handle == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0) |
| 48 | return -ENODEV; |
| 49 | |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 50 | return ata_sff_prereset(link, deadline); |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * pacpi_cable_detect - cable type detection |
| 55 | * @ap: port to detect |
| 56 | * |
| 57 | * Perform device specific cable detection |
| 58 | */ |
| 59 | |
| 60 | static int pacpi_cable_detect(struct ata_port *ap) |
| 61 | { |
| 62 | struct pata_acpi *acpi = ap->private_data; |
| 63 | |
| 64 | if ((acpi->mask[0] | acpi->mask[1]) & (0xF8 << ATA_SHIFT_UDMA)) |
| 65 | return ATA_CBL_PATA80; |
| 66 | else |
| 67 | return ATA_CBL_PATA40; |
| 68 | } |
| 69 | |
| 70 | /** |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 71 | * pacpi_discover_modes - filter non ACPI modes |
| 72 | * @adev: ATA device |
| 73 | * @mask: proposed modes |
| 74 | * |
| 75 | * Try the modes available and see which ones the ACPI method will |
| 76 | * set up sensibly. From this we get a mask of ACPI modes we can use |
| 77 | */ |
| 78 | |
| 79 | static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev) |
| 80 | { |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 81 | struct pata_acpi *acpi = ap->private_data; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 82 | struct ata_acpi_gtm probe; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 83 | unsigned int xfer_mask; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 84 | |
| 85 | probe = acpi->gtm; |
| 86 | |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 87 | ata_acpi_gtm(ap, &probe); |
| 88 | |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 89 | xfer_mask = ata_acpi_gtm_xfermask(adev, &probe); |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 90 | |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 91 | if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA)) |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 92 | ap->cbl = ATA_CBL_PATA80; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 93 | |
| 94 | return xfer_mask; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * pacpi_mode_filter - mode filter for ACPI |
| 99 | * @adev: device |
| 100 | * @mask: mask of valid modes |
| 101 | * |
| 102 | * Filter the valid mode list according to our own specific rules, in |
| 103 | * this case the list of discovered valid modes obtained by ACPI probing |
| 104 | */ |
| 105 | |
| 106 | static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask) |
| 107 | { |
| 108 | struct pata_acpi *acpi = adev->link->ap->private_data; |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 109 | return ata_bmdma_mode_filter(adev, mask & acpi->mask[adev->devno]); |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * pacpi_set_piomode - set initial PIO mode data |
| 114 | * @ap: ATA interface |
| 115 | * @adev: ATA device |
| 116 | */ |
| 117 | |
| 118 | static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev) |
| 119 | { |
| 120 | int unit = adev->devno; |
| 121 | struct pata_acpi *acpi = ap->private_data; |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 122 | const struct ata_timing *t; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 123 | |
Jeff Garzik | b447916 | 2007-10-25 20:47:30 -0400 | [diff] [blame] | 124 | if (!(acpi->gtm.flags & 0x10)) |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 125 | unit = 0; |
| 126 | |
| 127 | /* Now stuff the nS values into the structure */ |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 128 | t = ata_timing_find_mode(adev->pio_mode); |
| 129 | acpi->gtm.drive[unit].pio = t->cycle; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 130 | ata_acpi_stm(ap, &acpi->gtm); |
| 131 | /* See what mode we actually got */ |
| 132 | ata_acpi_gtm(ap, &acpi->gtm); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * pacpi_set_dmamode - set initial DMA mode data |
| 137 | * @ap: ATA interface |
| 138 | * @adev: ATA device |
| 139 | */ |
| 140 | |
| 141 | static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev) |
| 142 | { |
| 143 | int unit = adev->devno; |
| 144 | struct pata_acpi *acpi = ap->private_data; |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 145 | const struct ata_timing *t; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 146 | |
Jeff Garzik | b447916 | 2007-10-25 20:47:30 -0400 | [diff] [blame] | 147 | if (!(acpi->gtm.flags & 0x10)) |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 148 | unit = 0; |
| 149 | |
| 150 | /* Now stuff the nS values into the structure */ |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 151 | t = ata_timing_find_mode(adev->dma_mode); |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 152 | if (adev->dma_mode >= XFER_UDMA_0) { |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 153 | acpi->gtm.drive[unit].dma = t->udma; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 154 | acpi->gtm.flags |= (1 << (2 * unit)); |
| 155 | } else { |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 156 | acpi->gtm.drive[unit].dma = t->cycle; |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 157 | acpi->gtm.flags &= ~(1 << (2 * unit)); |
| 158 | } |
| 159 | ata_acpi_stm(ap, &acpi->gtm); |
| 160 | /* See what mode we actually got */ |
| 161 | ata_acpi_gtm(ap, &acpi->gtm); |
| 162 | } |
| 163 | |
| 164 | /** |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 165 | * pacpi_qc_issue - command issue |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 166 | * @qc: command pending |
| 167 | * |
| 168 | * Called when the libata layer is about to issue a command. We wrap |
| 169 | * this interface so that we can load the correct ATA timings if |
| 170 | * neccessary. |
| 171 | */ |
| 172 | |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 173 | static unsigned int pacpi_qc_issue(struct ata_queued_cmd *qc) |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 174 | { |
| 175 | struct ata_port *ap = qc->ap; |
| 176 | struct ata_device *adev = qc->dev; |
| 177 | struct pata_acpi *acpi = ap->private_data; |
| 178 | |
| 179 | if (acpi->gtm.flags & 0x10) |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 180 | return ata_sff_qc_issue(qc); |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 181 | |
| 182 | if (adev != acpi->last) { |
| 183 | pacpi_set_piomode(ap, adev); |
Alan Cox | b15b3eb | 2008-08-01 09:18:34 +0100 | [diff] [blame] | 184 | if (ata_dma_enabled(adev)) |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 185 | pacpi_set_dmamode(ap, adev); |
| 186 | acpi->last = adev; |
| 187 | } |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 188 | return ata_sff_qc_issue(qc); |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * pacpi_port_start - port setup |
| 193 | * @ap: ATA port being set up |
| 194 | * |
| 195 | * Use the port_start hook to maintain private control structures |
| 196 | */ |
| 197 | |
| 198 | static int pacpi_port_start(struct ata_port *ap) |
| 199 | { |
| 200 | struct pci_dev *pdev = to_pci_dev(ap->host->dev); |
| 201 | struct pata_acpi *acpi; |
| 202 | |
| 203 | int ret; |
| 204 | |
| 205 | if (ap->acpi_handle == NULL) |
| 206 | return -ENODEV; |
| 207 | |
| 208 | acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL); |
| 209 | if (ap->private_data == NULL) |
| 210 | return -ENOMEM; |
| 211 | acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]); |
| 212 | acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]); |
| 213 | ret = ata_sff_port_start(ap); |
| 214 | if (ret < 0) |
| 215 | return ret; |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static struct scsi_host_template pacpi_sht = { |
Tejun Heo | 68d1d07 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 221 | ATA_BMDMA_SHT(DRV_NAME), |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 222 | }; |
| 223 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 224 | static struct ata_port_operations pacpi_ops = { |
| 225 | .inherits = &ata_bmdma_port_ops, |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 226 | .qc_issue = pacpi_qc_issue, |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 227 | .cable_detect = pacpi_cable_detect, |
| 228 | .mode_filter = pacpi_mode_filter, |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 229 | .set_piomode = pacpi_set_piomode, |
| 230 | .set_dmamode = pacpi_set_dmamode, |
Tejun Heo | 887125e | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 231 | .prereset = pacpi_pre_reset, |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 232 | .port_start = pacpi_port_start, |
| 233 | }; |
| 234 | |
| 235 | |
| 236 | /** |
| 237 | * pacpi_init_one - Register ACPI ATA PCI device with kernel services |
| 238 | * @pdev: PCI device to register |
| 239 | * @ent: Entry in pacpi_pci_tbl matching with @pdev |
| 240 | * |
| 241 | * Called from kernel PCI layer. |
| 242 | * |
| 243 | * LOCKING: |
| 244 | * Inherited from PCI layer (may sleep). |
| 245 | * |
| 246 | * RETURNS: |
| 247 | * Zero on success, or -ERRNO value. |
| 248 | */ |
| 249 | |
| 250 | static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id) |
| 251 | { |
| 252 | static const struct ata_port_info info = { |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 253 | .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST, |
| 254 | |
| 255 | .pio_mask = 0x1f, |
| 256 | .mwdma_mask = 0x07, |
| 257 | .udma_mask = 0x7f, |
| 258 | |
| 259 | .port_ops = &pacpi_ops, |
| 260 | }; |
| 261 | const struct ata_port_info *ppi[] = { &info, NULL }; |
Alan Cox | 05177f1 | 2008-05-02 15:13:39 -0700 | [diff] [blame] | 262 | if (pdev->vendor == PCI_VENDOR_ID_ATI) { |
| 263 | int rc = pcim_enable_device(pdev); |
| 264 | if (rc < 0) |
| 265 | return rc; |
| 266 | pcim_pin_device(pdev); |
| 267 | } |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 268 | return ata_pci_sff_init_one(pdev, ppi, &pacpi_sht, NULL); |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static const struct pci_device_id pacpi_pci_tbl[] = { |
| 272 | { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1}, |
| 273 | { } /* terminate list */ |
| 274 | }; |
| 275 | |
| 276 | static struct pci_driver pacpi_pci_driver = { |
| 277 | .name = DRV_NAME, |
| 278 | .id_table = pacpi_pci_tbl, |
| 279 | .probe = pacpi_init_one, |
| 280 | .remove = ata_pci_remove_one, |
Tejun Heo | 8e2840e | 2007-10-17 15:24:16 +0900 | [diff] [blame] | 281 | #ifdef CONFIG_PM |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 282 | .suspend = ata_pci_device_suspend, |
| 283 | .resume = ata_pci_device_resume, |
Tejun Heo | 8e2840e | 2007-10-17 15:24:16 +0900 | [diff] [blame] | 284 | #endif |
Alan Cox | 025621f | 2007-10-04 21:32:58 +0100 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | static int __init pacpi_init(void) |
| 288 | { |
| 289 | return pci_register_driver(&pacpi_pci_driver); |
| 290 | } |
| 291 | |
| 292 | static void __exit pacpi_exit(void) |
| 293 | { |
| 294 | pci_unregister_driver(&pacpi_pci_driver); |
| 295 | } |
| 296 | |
| 297 | module_init(pacpi_init); |
| 298 | module_exit(pacpi_exit); |
| 299 | |
| 300 | MODULE_AUTHOR("Alan Cox"); |
| 301 | MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode"); |
| 302 | MODULE_LICENSE("GPL"); |
| 303 | MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl); |
| 304 | MODULE_VERSION(DRV_VERSION); |
| 305 | |