blob: 35ad488db6ed0af9c5d3c5beb74edfce5760ba4d [file] [log] [blame]
Alan Cox025621f2007-10-04 21:32:58 +01001/*
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
29struct 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
43static 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
50 return ata_std_prereset(link, deadline);
51}
52
53/**
54 * pacpi_cable_detect - cable type detection
55 * @ap: port to detect
56 *
57 * Perform device specific cable detection
58 */
59
60static 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/**
71 * pacpi_error_handler - Setup and error handler
72 * @ap: Port to handle
73 *
74 * LOCKING:
75 * None (inherited from caller).
76 */
77
78static void pacpi_error_handler(struct ata_port *ap)
79{
Harvey Harrison5410f722008-02-13 21:14:31 -080080 ata_bmdma_drive_eh(ap, pacpi_pre_reset, ata_std_softreset, NULL,
81 ata_std_postreset);
Alan Cox025621f2007-10-04 21:32:58 +010082}
83
Alan Cox025621f2007-10-04 21:32:58 +010084/**
85 * pacpi_discover_modes - filter non ACPI modes
86 * @adev: ATA device
87 * @mask: proposed modes
88 *
89 * Try the modes available and see which ones the ACPI method will
90 * set up sensibly. From this we get a mask of ACPI modes we can use
91 */
92
93static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
94{
Alan Cox025621f2007-10-04 21:32:58 +010095 struct pata_acpi *acpi = ap->private_data;
Alan Cox025621f2007-10-04 21:32:58 +010096 struct ata_acpi_gtm probe;
Tejun Heo7c77fa42007-12-18 16:33:03 +090097 unsigned int xfer_mask;
Alan Cox025621f2007-10-04 21:32:58 +010098
99 probe = acpi->gtm;
100
Alan Cox025621f2007-10-04 21:32:58 +0100101 ata_acpi_gtm(ap, &probe);
102
Tejun Heo7c77fa42007-12-18 16:33:03 +0900103 xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
Alan Cox025621f2007-10-04 21:32:58 +0100104
Tejun Heo7c77fa42007-12-18 16:33:03 +0900105 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
Alan Cox025621f2007-10-04 21:32:58 +0100106 ap->cbl = ATA_CBL_PATA80;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900107
108 return xfer_mask;
Alan Cox025621f2007-10-04 21:32:58 +0100109}
110
111/**
112 * pacpi_mode_filter - mode filter for ACPI
113 * @adev: device
114 * @mask: mask of valid modes
115 *
116 * Filter the valid mode list according to our own specific rules, in
117 * this case the list of discovered valid modes obtained by ACPI probing
118 */
119
120static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask)
121{
122 struct pata_acpi *acpi = adev->link->ap->private_data;
123 return ata_pci_default_filter(adev, mask & acpi->mask[adev->devno]);
124}
125
126/**
127 * pacpi_set_piomode - set initial PIO mode data
128 * @ap: ATA interface
129 * @adev: ATA device
130 */
131
132static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
133{
134 int unit = adev->devno;
135 struct pata_acpi *acpi = ap->private_data;
Tejun Heoa0f79b92007-12-18 16:33:05 +0900136 const struct ata_timing *t;
Alan Cox025621f2007-10-04 21:32:58 +0100137
Jeff Garzikb4479162007-10-25 20:47:30 -0400138 if (!(acpi->gtm.flags & 0x10))
Alan Cox025621f2007-10-04 21:32:58 +0100139 unit = 0;
140
141 /* Now stuff the nS values into the structure */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900142 t = ata_timing_find_mode(adev->pio_mode);
143 acpi->gtm.drive[unit].pio = t->cycle;
Alan Cox025621f2007-10-04 21:32:58 +0100144 ata_acpi_stm(ap, &acpi->gtm);
145 /* See what mode we actually got */
146 ata_acpi_gtm(ap, &acpi->gtm);
147}
148
149/**
150 * pacpi_set_dmamode - set initial DMA mode data
151 * @ap: ATA interface
152 * @adev: ATA device
153 */
154
155static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
156{
157 int unit = adev->devno;
158 struct pata_acpi *acpi = ap->private_data;
Tejun Heoa0f79b92007-12-18 16:33:05 +0900159 const struct ata_timing *t;
Alan Cox025621f2007-10-04 21:32:58 +0100160
Jeff Garzikb4479162007-10-25 20:47:30 -0400161 if (!(acpi->gtm.flags & 0x10))
Alan Cox025621f2007-10-04 21:32:58 +0100162 unit = 0;
163
164 /* Now stuff the nS values into the structure */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900165 t = ata_timing_find_mode(adev->dma_mode);
Alan Cox025621f2007-10-04 21:32:58 +0100166 if (adev->dma_mode >= XFER_UDMA_0) {
Tejun Heoa0f79b92007-12-18 16:33:05 +0900167 acpi->gtm.drive[unit].dma = t->udma;
Alan Cox025621f2007-10-04 21:32:58 +0100168 acpi->gtm.flags |= (1 << (2 * unit));
169 } else {
Tejun Heoa0f79b92007-12-18 16:33:05 +0900170 acpi->gtm.drive[unit].dma = t->cycle;
Alan Cox025621f2007-10-04 21:32:58 +0100171 acpi->gtm.flags &= ~(1 << (2 * unit));
172 }
173 ata_acpi_stm(ap, &acpi->gtm);
174 /* See what mode we actually got */
175 ata_acpi_gtm(ap, &acpi->gtm);
176}
177
178/**
179 * pacpi_qc_issue_prot - command issue
180 * @qc: command pending
181 *
182 * Called when the libata layer is about to issue a command. We wrap
183 * this interface so that we can load the correct ATA timings if
184 * neccessary.
185 */
186
187static unsigned int pacpi_qc_issue_prot(struct ata_queued_cmd *qc)
188{
189 struct ata_port *ap = qc->ap;
190 struct ata_device *adev = qc->dev;
191 struct pata_acpi *acpi = ap->private_data;
192
193 if (acpi->gtm.flags & 0x10)
194 return ata_qc_issue_prot(qc);
195
196 if (adev != acpi->last) {
197 pacpi_set_piomode(ap, adev);
198 if (adev->dma_mode)
199 pacpi_set_dmamode(ap, adev);
200 acpi->last = adev;
201 }
202 return ata_qc_issue_prot(qc);
203}
204
205/**
206 * pacpi_port_start - port setup
207 * @ap: ATA port being set up
208 *
209 * Use the port_start hook to maintain private control structures
210 */
211
212static int pacpi_port_start(struct ata_port *ap)
213{
214 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
215 struct pata_acpi *acpi;
216
217 int ret;
218
219 if (ap->acpi_handle == NULL)
220 return -ENODEV;
221
222 acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
223 if (ap->private_data == NULL)
224 return -ENOMEM;
225 acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
226 acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
227 ret = ata_sff_port_start(ap);
228 if (ret < 0)
229 return ret;
230
231 return ret;
232}
233
234static struct scsi_host_template pacpi_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900235 ATA_BMDMA_SHT(DRV_NAME),
Alan Cox025621f2007-10-04 21:32:58 +0100236};
237
Tejun Heo029cfd62008-03-25 12:22:49 +0900238static struct ata_port_operations pacpi_ops = {
239 .inherits = &ata_bmdma_port_ops,
240 .qc_issue = pacpi_qc_issue_prot,
241 .cable_detect = pacpi_cable_detect,
242 .mode_filter = pacpi_mode_filter,
Alan Cox025621f2007-10-04 21:32:58 +0100243 .set_piomode = pacpi_set_piomode,
244 .set_dmamode = pacpi_set_dmamode,
Alan Cox025621f2007-10-04 21:32:58 +0100245 .error_handler = pacpi_error_handler,
Alan Cox025621f2007-10-04 21:32:58 +0100246 .port_start = pacpi_port_start,
247};
248
249
250/**
251 * pacpi_init_one - Register ACPI ATA PCI device with kernel services
252 * @pdev: PCI device to register
253 * @ent: Entry in pacpi_pci_tbl matching with @pdev
254 *
255 * Called from kernel PCI layer.
256 *
257 * LOCKING:
258 * Inherited from PCI layer (may sleep).
259 *
260 * RETURNS:
261 * Zero on success, or -ERRNO value.
262 */
263
264static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
265{
266 static const struct ata_port_info info = {
267 .sht = &pacpi_sht,
268 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
269
270 .pio_mask = 0x1f,
271 .mwdma_mask = 0x07,
272 .udma_mask = 0x7f,
273
274 .port_ops = &pacpi_ops,
275 };
276 const struct ata_port_info *ppi[] = { &info, NULL };
277 return ata_pci_init_one(pdev, ppi);
278}
279
280static const struct pci_device_id pacpi_pci_tbl[] = {
281 { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
282 { } /* terminate list */
283};
284
285static struct pci_driver pacpi_pci_driver = {
286 .name = DRV_NAME,
287 .id_table = pacpi_pci_tbl,
288 .probe = pacpi_init_one,
289 .remove = ata_pci_remove_one,
Tejun Heo8e2840e2007-10-17 15:24:16 +0900290#ifdef CONFIG_PM
Alan Cox025621f2007-10-04 21:32:58 +0100291 .suspend = ata_pci_device_suspend,
292 .resume = ata_pci_device_resume,
Tejun Heo8e2840e2007-10-17 15:24:16 +0900293#endif
Alan Cox025621f2007-10-04 21:32:58 +0100294};
295
296static int __init pacpi_init(void)
297{
298 return pci_register_driver(&pacpi_pci_driver);
299}
300
301static void __exit pacpi_exit(void)
302{
303 pci_unregister_driver(&pacpi_pci_driver);
304}
305
306module_init(pacpi_init);
307module_exit(pacpi_exit);
308
309MODULE_AUTHOR("Alan Cox");
310MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
311MODULE_LICENSE("GPL");
312MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
313MODULE_VERSION(DRV_VERSION);
314