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