blob: 066b9f301ed5a76e7de363ec083dc7cf0686a36b [file] [log] [blame]
Alan Cox025621f2007-10-04 21:32:58 +01001/*
2 * ACPI PATA driver
3 *
Alan Coxab771632008-10-27 15:09:10 +00004 * (c) 2007 Red Hat
Alan Cox025621f2007-10-04 21:32:58 +01005 */
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 Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Alan Cox025621f2007-10-04 21:32:58 +010015#include <scsi/scsi_host.h>
16#include <acpi/acpi_bus.h>
Alan Cox025621f2007-10-04 21:32:58 +010017
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
24struct 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
38static 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;
42 if (ap->acpi_handle == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0)
43 return -ENODEV;
44
Tejun Heo9363c382008-04-07 22:47:16 +090045 return ata_sff_prereset(link, deadline);
Alan Cox025621f2007-10-04 21:32:58 +010046}
47
48/**
49 * pacpi_cable_detect - cable type detection
50 * @ap: port to detect
51 *
52 * Perform device specific cable detection
53 */
54
55static 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 Cox025621f2007-10-04 21:32:58 +010066 * 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
74static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
75{
Alan Cox025621f2007-10-04 21:32:58 +010076 struct pata_acpi *acpi = ap->private_data;
Alan Cox025621f2007-10-04 21:32:58 +010077 struct ata_acpi_gtm probe;
Tejun Heo7c77fa42007-12-18 16:33:03 +090078 unsigned int xfer_mask;
Alan Cox025621f2007-10-04 21:32:58 +010079
80 probe = acpi->gtm;
81
Alan Cox025621f2007-10-04 21:32:58 +010082 ata_acpi_gtm(ap, &probe);
83
Tejun Heo7c77fa42007-12-18 16:33:03 +090084 xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
Alan Cox025621f2007-10-04 21:32:58 +010085
Tejun Heo7c77fa42007-12-18 16:33:03 +090086 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
Alan Cox025621f2007-10-04 21:32:58 +010087 ap->cbl = ATA_CBL_PATA80;
Tejun Heo7c77fa42007-12-18 16:33:03 +090088
89 return xfer_mask;
Alan Cox025621f2007-10-04 21:32:58 +010090}
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
101static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask)
102{
103 struct pata_acpi *acpi = adev->link->ap->private_data;
Tejun Heoc7087652010-05-10 21:41:34 +0200104 return mask & acpi->mask[adev->devno];
Alan Cox025621f2007-10-04 21:32:58 +0100105}
106
107/**
108 * pacpi_set_piomode - set initial PIO mode data
109 * @ap: ATA interface
110 * @adev: ATA device
111 */
112
113static 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 Heoa0f79b92007-12-18 16:33:05 +0900117 const struct ata_timing *t;
Alan Cox025621f2007-10-04 21:32:58 +0100118
Jeff Garzikb4479162007-10-25 20:47:30 -0400119 if (!(acpi->gtm.flags & 0x10))
Alan Cox025621f2007-10-04 21:32:58 +0100120 unit = 0;
121
122 /* Now stuff the nS values into the structure */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900123 t = ata_timing_find_mode(adev->pio_mode);
124 acpi->gtm.drive[unit].pio = t->cycle;
Alan Cox025621f2007-10-04 21:32:58 +0100125 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
136static 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 Heoa0f79b92007-12-18 16:33:05 +0900140 const struct ata_timing *t;
Alan Cox025621f2007-10-04 21:32:58 +0100141
Jeff Garzikb4479162007-10-25 20:47:30 -0400142 if (!(acpi->gtm.flags & 0x10))
Alan Cox025621f2007-10-04 21:32:58 +0100143 unit = 0;
144
145 /* Now stuff the nS values into the structure */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900146 t = ata_timing_find_mode(adev->dma_mode);
Alan Cox025621f2007-10-04 21:32:58 +0100147 if (adev->dma_mode >= XFER_UDMA_0) {
Tejun Heoa0f79b92007-12-18 16:33:05 +0900148 acpi->gtm.drive[unit].dma = t->udma;
Alan Cox025621f2007-10-04 21:32:58 +0100149 acpi->gtm.flags |= (1 << (2 * unit));
150 } else {
Tejun Heoa0f79b92007-12-18 16:33:05 +0900151 acpi->gtm.drive[unit].dma = t->cycle;
Alan Cox025621f2007-10-04 21:32:58 +0100152 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 Heo9363c382008-04-07 22:47:16 +0900160 * pacpi_qc_issue - command issue
Alan Cox025621f2007-10-04 21:32:58 +0100161 * @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 Mack3ad2f3f2010-02-03 08:01:28 +0800165 * necessary.
Alan Cox025621f2007-10-04 21:32:58 +0100166 */
167
Tejun Heo9363c382008-04-07 22:47:16 +0900168static unsigned int pacpi_qc_issue(struct ata_queued_cmd *qc)
Alan Cox025621f2007-10-04 21:32:58 +0100169{
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 Heo360ff782010-05-10 21:41:42 +0200175 return ata_bmdma_qc_issue(qc);
Alan Cox025621f2007-10-04 21:32:58 +0100176
177 if (adev != acpi->last) {
178 pacpi_set_piomode(ap, adev);
Alan Coxb15b3eb2008-08-01 09:18:34 +0100179 if (ata_dma_enabled(adev))
Alan Cox025621f2007-10-04 21:32:58 +0100180 pacpi_set_dmamode(ap, adev);
181 acpi->last = adev;
182 }
Tejun Heo360ff782010-05-10 21:41:42 +0200183 return ata_bmdma_qc_issue(qc);
Alan Cox025621f2007-10-04 21:32:58 +0100184}
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
193static 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
198 int ret;
199
200 if (ap->acpi_handle == NULL)
201 return -ENODEV;
202
203 acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
204 if (ap->private_data == NULL)
205 return -ENOMEM;
206 acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
207 acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
Tejun Heoc7087652010-05-10 21:41:34 +0200208 ret = ata_bmdma_port_start(ap);
Alan Cox025621f2007-10-04 21:32:58 +0100209 if (ret < 0)
210 return ret;
211
212 return ret;
213}
214
215static struct scsi_host_template pacpi_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900216 ATA_BMDMA_SHT(DRV_NAME),
Alan Cox025621f2007-10-04 21:32:58 +0100217};
218
Tejun Heo029cfd62008-03-25 12:22:49 +0900219static struct ata_port_operations pacpi_ops = {
220 .inherits = &ata_bmdma_port_ops,
Tejun Heo9363c382008-04-07 22:47:16 +0900221 .qc_issue = pacpi_qc_issue,
Tejun Heo029cfd62008-03-25 12:22:49 +0900222 .cable_detect = pacpi_cable_detect,
223 .mode_filter = pacpi_mode_filter,
Alan Cox025621f2007-10-04 21:32:58 +0100224 .set_piomode = pacpi_set_piomode,
225 .set_dmamode = pacpi_set_dmamode,
Tejun Heo887125e2008-03-25 12:22:49 +0900226 .prereset = pacpi_pre_reset,
Alan Cox025621f2007-10-04 21:32:58 +0100227 .port_start = pacpi_port_start,
228};
229
230
231/**
232 * pacpi_init_one - Register ACPI ATA PCI device with kernel services
233 * @pdev: PCI device to register
234 * @ent: Entry in pacpi_pci_tbl matching with @pdev
235 *
236 * Called from kernel PCI layer.
237 *
238 * LOCKING:
239 * Inherited from PCI layer (may sleep).
240 *
241 * RETURNS:
242 * Zero on success, or -ERRNO value.
243 */
244
245static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
246{
247 static const struct ata_port_info info = {
Alan Cox025621f2007-10-04 21:32:58 +0100248 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
249
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100250 .pio_mask = ATA_PIO4,
251 .mwdma_mask = ATA_MWDMA2,
252 .udma_mask = ATA_UDMA6,
Alan Cox025621f2007-10-04 21:32:58 +0100253
254 .port_ops = &pacpi_ops,
255 };
256 const struct ata_port_info *ppi[] = { &info, NULL };
Alan Cox05177f12008-05-02 15:13:39 -0700257 if (pdev->vendor == PCI_VENDOR_ID_ATI) {
258 int rc = pcim_enable_device(pdev);
259 if (rc < 0)
260 return rc;
261 pcim_pin_device(pdev);
262 }
Alan Cox16ea0fc2010-02-23 02:26:06 -0500263 return ata_pci_sff_init_one(pdev, ppi, &pacpi_sht, NULL, 0);
Alan Cox025621f2007-10-04 21:32:58 +0100264}
265
266static const struct pci_device_id pacpi_pci_tbl[] = {
267 { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
268 { } /* terminate list */
269};
270
271static struct pci_driver pacpi_pci_driver = {
272 .name = DRV_NAME,
273 .id_table = pacpi_pci_tbl,
274 .probe = pacpi_init_one,
275 .remove = ata_pci_remove_one,
Tejun Heo8e2840e2007-10-17 15:24:16 +0900276#ifdef CONFIG_PM
Alan Cox025621f2007-10-04 21:32:58 +0100277 .suspend = ata_pci_device_suspend,
278 .resume = ata_pci_device_resume,
Tejun Heo8e2840e2007-10-17 15:24:16 +0900279#endif
Alan Cox025621f2007-10-04 21:32:58 +0100280};
281
282static int __init pacpi_init(void)
283{
284 return pci_register_driver(&pacpi_pci_driver);
285}
286
287static void __exit pacpi_exit(void)
288{
289 pci_unregister_driver(&pacpi_pci_driver);
290}
291
292module_init(pacpi_init);
293module_exit(pacpi_exit);
294
295MODULE_AUTHOR("Alan Cox");
296MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
297MODULE_LICENSE("GPL");
298MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
299MODULE_VERSION(DRV_VERSION);
300