Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
025621f9 AC |
2 | /* |
3 | * ACPI PATA driver | |
4 | * | |
ab771630 | 5 | * (c) 2007 Red Hat |
025621f9 AC |
6 | */ |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/pci.h> | |
025621f9 AC |
11 | #include <linux/blkdev.h> |
12 | #include <linux/delay.h> | |
13 | #include <linux/device.h> | |
5a0e3ad6 | 14 | #include <linux/gfp.h> |
8b48463f | 15 | #include <linux/acpi.h> |
025621f9 AC |
16 | #include <linux/libata.h> |
17 | #include <linux/ata.h> | |
8b48463f | 18 | #include <scsi/scsi_host.h> |
025621f9 AC |
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; | |
f1bc1e4c | 41 | if (ACPI_HANDLE(&ap->tdev) == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0) |
025621f9 AC |
42 | return -ENODEV; |
43 | ||
9363c382 | 44 | return ata_sff_prereset(link, deadline); |
025621f9 AC |
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 | ||
025621f9 AC |
64 | /** |
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 | { | |
025621f9 | 75 | struct pata_acpi *acpi = ap->private_data; |
025621f9 | 76 | struct ata_acpi_gtm probe; |
7c77fa4d | 77 | unsigned int xfer_mask; |
025621f9 AC |
78 | |
79 | probe = acpi->gtm; | |
80 | ||
025621f9 AC |
81 | ata_acpi_gtm(ap, &probe); |
82 | ||
7c77fa4d | 83 | xfer_mask = ata_acpi_gtm_xfermask(adev, &probe); |
025621f9 | 84 | |
7c77fa4d | 85 | if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA)) |
025621f9 | 86 | ap->cbl = ATA_CBL_PATA80; |
7c77fa4d TH |
87 | |
88 | return xfer_mask; | |
025621f9 AC |
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; | |
c7087652 | 103 | return mask & acpi->mask[adev->devno]; |
025621f9 AC |
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; | |
a0f79b92 | 116 | const struct ata_timing *t; |
025621f9 | 117 | |
b447916e | 118 | if (!(acpi->gtm.flags & 0x10)) |
025621f9 AC |
119 | unit = 0; |
120 | ||
121 | /* Now stuff the nS values into the structure */ | |
a0f79b92 TH |
122 | t = ata_timing_find_mode(adev->pio_mode); |
123 | acpi->gtm.drive[unit].pio = t->cycle; | |
025621f9 AC |
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; | |
a0f79b92 | 139 | const struct ata_timing *t; |
025621f9 | 140 | |
b447916e | 141 | if (!(acpi->gtm.flags & 0x10)) |
025621f9 AC |
142 | unit = 0; |
143 | ||
144 | /* Now stuff the nS values into the structure */ | |
a0f79b92 | 145 | t = ata_timing_find_mode(adev->dma_mode); |
025621f9 | 146 | if (adev->dma_mode >= XFER_UDMA_0) { |
a0f79b92 | 147 | acpi->gtm.drive[unit].dma = t->udma; |
025621f9 AC |
148 | acpi->gtm.flags |= (1 << (2 * unit)); |
149 | } else { | |
a0f79b92 | 150 | acpi->gtm.drive[unit].dma = t->cycle; |
025621f9 AC |
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 | /** | |
9363c382 | 159 | * pacpi_qc_issue - command issue |
025621f9 AC |
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 | |
3ad2f3fb | 164 | * necessary. |
025621f9 AC |
165 | */ |
166 | ||
9363c382 | 167 | static unsigned int pacpi_qc_issue(struct ata_queued_cmd *qc) |
025621f9 AC |
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) | |
360ff783 | 174 | return ata_bmdma_qc_issue(qc); |
025621f9 AC |
175 | |
176 | if (adev != acpi->last) { | |
177 | pacpi_set_piomode(ap, adev); | |
b15b3eba | 178 | if (ata_dma_enabled(adev)) |
025621f9 AC |
179 | pacpi_set_dmamode(ap, adev); |
180 | acpi->last = adev; | |
181 | } | |
360ff783 | 182 | return ata_bmdma_qc_issue(qc); |
025621f9 AC |
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 | ||
f1bc1e4c | 197 | if (ACPI_HANDLE(&ap->tdev) == NULL) |
025621f9 AC |
198 | return -ENODEV; |
199 | ||
200 | acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL); | |
201 | if (ap->private_data == NULL) | |
202 | return -ENOMEM; | |
203 | acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]); | |
204 | acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]); | |
47db477e | 205 | return ata_bmdma_port_start(ap); |
025621f9 AC |
206 | } |
207 | ||
208 | static struct scsi_host_template pacpi_sht = { | |
68d1d07b | 209 | ATA_BMDMA_SHT(DRV_NAME), |
025621f9 AC |
210 | }; |
211 | ||
029cfd6b TH |
212 | static struct ata_port_operations pacpi_ops = { |
213 | .inherits = &ata_bmdma_port_ops, | |
9363c382 | 214 | .qc_issue = pacpi_qc_issue, |
029cfd6b TH |
215 | .cable_detect = pacpi_cable_detect, |
216 | .mode_filter = pacpi_mode_filter, | |
025621f9 AC |
217 | .set_piomode = pacpi_set_piomode, |
218 | .set_dmamode = pacpi_set_dmamode, | |
887125e3 | 219 | .prereset = pacpi_pre_reset, |
025621f9 AC |
220 | .port_start = pacpi_port_start, |
221 | }; | |
222 | ||
223 | ||
224 | /** | |
225 | * pacpi_init_one - Register ACPI ATA PCI device with kernel services | |
226 | * @pdev: PCI device to register | |
227 | * @ent: Entry in pacpi_pci_tbl matching with @pdev | |
228 | * | |
229 | * Called from kernel PCI layer. | |
230 | * | |
231 | * LOCKING: | |
232 | * Inherited from PCI layer (may sleep). | |
233 | * | |
234 | * RETURNS: | |
235 | * Zero on success, or -ERRNO value. | |
236 | */ | |
237 | ||
238 | static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id) | |
239 | { | |
240 | static const struct ata_port_info info = { | |
c10f97b9 | 241 | .flags = ATA_FLAG_SLAVE_POSS, |
025621f9 | 242 | |
14bdef98 EIB |
243 | .pio_mask = ATA_PIO4, |
244 | .mwdma_mask = ATA_MWDMA2, | |
245 | .udma_mask = ATA_UDMA6, | |
025621f9 AC |
246 | |
247 | .port_ops = &pacpi_ops, | |
248 | }; | |
249 | const struct ata_port_info *ppi[] = { &info, NULL }; | |
05177f17 AC |
250 | if (pdev->vendor == PCI_VENDOR_ID_ATI) { |
251 | int rc = pcim_enable_device(pdev); | |
252 | if (rc < 0) | |
253 | return rc; | |
254 | pcim_pin_device(pdev); | |
255 | } | |
1c5afdf7 | 256 | return ata_pci_bmdma_init_one(pdev, ppi, &pacpi_sht, NULL, 0); |
025621f9 AC |
257 | } |
258 | ||
259 | static const struct pci_device_id pacpi_pci_tbl[] = { | |
260 | { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1}, | |
261 | { } /* terminate list */ | |
262 | }; | |
263 | ||
264 | static struct pci_driver pacpi_pci_driver = { | |
265 | .name = DRV_NAME, | |
266 | .id_table = pacpi_pci_tbl, | |
267 | .probe = pacpi_init_one, | |
268 | .remove = ata_pci_remove_one, | |
58eb8cd5 | 269 | #ifdef CONFIG_PM_SLEEP |
025621f9 AC |
270 | .suspend = ata_pci_device_suspend, |
271 | .resume = ata_pci_device_resume, | |
8e2840e0 | 272 | #endif |
025621f9 AC |
273 | }; |
274 | ||
2fc75da0 | 275 | module_pci_driver(pacpi_pci_driver); |
025621f9 AC |
276 | |
277 | MODULE_AUTHOR("Alan Cox"); | |
278 | MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode"); | |
279 | MODULE_LICENSE("GPL"); | |
280 | MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl); | |
281 | MODULE_VERSION(DRV_VERSION); |