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