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