xref: /openbmc/linux/drivers/ata/pata_acpi.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2025621f9SAlan Cox /*
3025621f9SAlan Cox  *	ACPI PATA driver
4025621f9SAlan Cox  *
5ab771630SAlan Cox  *	(c) 2007 Red Hat
6025621f9SAlan Cox  */
7025621f9SAlan Cox 
8025621f9SAlan Cox #include <linux/kernel.h>
9025621f9SAlan Cox #include <linux/module.h>
10025621f9SAlan Cox #include <linux/pci.h>
11025621f9SAlan Cox #include <linux/blkdev.h>
12025621f9SAlan Cox #include <linux/delay.h>
13025621f9SAlan Cox #include <linux/device.h>
145a0e3ad6STejun Heo #include <linux/gfp.h>
158b48463fSLv Zheng #include <linux/acpi.h>
16025621f9SAlan Cox #include <linux/libata.h>
17025621f9SAlan Cox #include <linux/ata.h>
188b48463fSLv Zheng #include <scsi/scsi_host.h>
19025621f9SAlan Cox 
20025621f9SAlan Cox #define DRV_NAME	"pata_acpi"
21025621f9SAlan Cox #define DRV_VERSION	"0.2.3"
22025621f9SAlan Cox 
23025621f9SAlan Cox struct pata_acpi {
24025621f9SAlan Cox 	struct ata_acpi_gtm gtm;
25025621f9SAlan Cox 	void *last;
26025621f9SAlan Cox 	unsigned long mask[2];
27025621f9SAlan Cox };
28025621f9SAlan Cox 
29025621f9SAlan Cox /**
30025621f9SAlan Cox  *	pacpi_pre_reset	-	check for 40/80 pin
31ff237990SLee Jones  *	@link: ATA link
32025621f9SAlan Cox  *	@deadline: deadline jiffies for the operation
33025621f9SAlan Cox  *
34025621f9SAlan Cox  *	Perform the PATA port setup we need.
35025621f9SAlan Cox  */
36025621f9SAlan Cox 
pacpi_pre_reset(struct ata_link * link,unsigned long deadline)37025621f9SAlan Cox static int pacpi_pre_reset(struct ata_link *link, unsigned long deadline)
38025621f9SAlan Cox {
39025621f9SAlan Cox 	struct ata_port *ap = link->ap;
40025621f9SAlan Cox 	struct pata_acpi *acpi = ap->private_data;
41f1bc1e4cSAaron Lu 	if (ACPI_HANDLE(&ap->tdev) == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0)
42025621f9SAlan Cox 		return -ENODEV;
43025621f9SAlan Cox 
449363c382STejun Heo 	return ata_sff_prereset(link, deadline);
45025621f9SAlan Cox }
46025621f9SAlan Cox 
47025621f9SAlan Cox /**
48025621f9SAlan Cox  *	pacpi_cable_detect	-	cable type detection
49025621f9SAlan Cox  *	@ap: port to detect
50025621f9SAlan Cox  *
51025621f9SAlan Cox  *	Perform device specific cable detection
52025621f9SAlan Cox  */
53025621f9SAlan Cox 
pacpi_cable_detect(struct ata_port * ap)54025621f9SAlan Cox static int pacpi_cable_detect(struct ata_port *ap)
55025621f9SAlan Cox {
56025621f9SAlan Cox 	struct pata_acpi *acpi = ap->private_data;
57025621f9SAlan Cox 
58025621f9SAlan Cox 	if ((acpi->mask[0] | acpi->mask[1]) & (0xF8 << ATA_SHIFT_UDMA))
59025621f9SAlan Cox 		return ATA_CBL_PATA80;
60025621f9SAlan Cox 	else
61025621f9SAlan Cox 		return ATA_CBL_PATA40;
62025621f9SAlan Cox }
63025621f9SAlan Cox 
64025621f9SAlan Cox /**
65025621f9SAlan Cox  *	pacpi_discover_modes	-	filter non ACPI modes
66ff237990SLee Jones  *	@ap: ATA port
67025621f9SAlan Cox  *	@adev: ATA device
68025621f9SAlan Cox  *
69025621f9SAlan Cox  *	Try the modes available and see which ones the ACPI method will
70025621f9SAlan Cox  *	set up sensibly. From this we get a mask of ACPI modes we can use
71025621f9SAlan Cox  */
72025621f9SAlan Cox 
pacpi_discover_modes(struct ata_port * ap,struct ata_device * adev)73025621f9SAlan Cox static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
74025621f9SAlan Cox {
75025621f9SAlan Cox 	struct pata_acpi *acpi = ap->private_data;
76025621f9SAlan Cox 	struct ata_acpi_gtm probe;
777c77fa4dSTejun Heo 	unsigned int xfer_mask;
78025621f9SAlan Cox 
79025621f9SAlan Cox 	probe = acpi->gtm;
80025621f9SAlan Cox 
81025621f9SAlan Cox 	ata_acpi_gtm(ap, &probe);
82025621f9SAlan Cox 
837c77fa4dSTejun Heo 	xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
84025621f9SAlan Cox 
857c77fa4dSTejun Heo 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
86025621f9SAlan Cox 		ap->cbl = ATA_CBL_PATA80;
877c77fa4dSTejun Heo 
887c77fa4dSTejun Heo 	return xfer_mask;
89025621f9SAlan Cox }
90025621f9SAlan Cox 
91025621f9SAlan Cox /**
92025621f9SAlan Cox  *	pacpi_mode_filter	-	mode filter for ACPI
93025621f9SAlan Cox  *	@adev: device
94025621f9SAlan Cox  *	@mask: mask of valid modes
95025621f9SAlan Cox  *
96025621f9SAlan Cox  *	Filter the valid mode list according to our own specific rules, in
97025621f9SAlan Cox  *	this case the list of discovered valid modes obtained by ACPI probing
98025621f9SAlan Cox  */
99025621f9SAlan Cox 
pacpi_mode_filter(struct ata_device * adev,unsigned int mask)100f0a6d77bSSergey Shtylyov static unsigned int pacpi_mode_filter(struct ata_device *adev, unsigned int mask)
101025621f9SAlan Cox {
102025621f9SAlan Cox 	struct pata_acpi *acpi = adev->link->ap->private_data;
103c7087652STejun Heo 	return mask & acpi->mask[adev->devno];
104025621f9SAlan Cox }
105025621f9SAlan Cox 
106025621f9SAlan Cox /**
107025621f9SAlan Cox  *	pacpi_set_piomode	-	set initial PIO mode data
108025621f9SAlan Cox  *	@ap: ATA interface
109025621f9SAlan Cox  *	@adev: ATA device
110025621f9SAlan Cox  */
111025621f9SAlan Cox 
pacpi_set_piomode(struct ata_port * ap,struct ata_device * adev)112025621f9SAlan Cox static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
113025621f9SAlan Cox {
114025621f9SAlan Cox 	int unit = adev->devno;
115025621f9SAlan Cox 	struct pata_acpi *acpi = ap->private_data;
116a0f79b92STejun Heo 	const struct ata_timing *t;
117025621f9SAlan Cox 
118025621f9SAlan Cox 	if (!(acpi->gtm.flags & 0x10))
119025621f9SAlan Cox 		unit = 0;
120025621f9SAlan Cox 
121025621f9SAlan Cox 	/* Now stuff the nS values into the structure */
122a0f79b92STejun Heo 	t = ata_timing_find_mode(adev->pio_mode);
123a0f79b92STejun Heo 	acpi->gtm.drive[unit].pio = t->cycle;
124025621f9SAlan Cox 	ata_acpi_stm(ap, &acpi->gtm);
125025621f9SAlan Cox 	/* See what mode we actually got */
126025621f9SAlan Cox 	ata_acpi_gtm(ap, &acpi->gtm);
127025621f9SAlan Cox }
128025621f9SAlan Cox 
129025621f9SAlan Cox /**
130025621f9SAlan Cox  *	pacpi_set_dmamode	-	set initial DMA mode data
131025621f9SAlan Cox  *	@ap: ATA interface
132025621f9SAlan Cox  *	@adev: ATA device
133025621f9SAlan Cox  */
134025621f9SAlan Cox 
pacpi_set_dmamode(struct ata_port * ap,struct ata_device * adev)135025621f9SAlan Cox static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
136025621f9SAlan Cox {
137025621f9SAlan Cox 	int unit = adev->devno;
138025621f9SAlan Cox 	struct pata_acpi *acpi = ap->private_data;
139a0f79b92STejun Heo 	const struct ata_timing *t;
140025621f9SAlan Cox 
141025621f9SAlan Cox 	if (!(acpi->gtm.flags & 0x10))
142025621f9SAlan Cox 		unit = 0;
143025621f9SAlan Cox 
144025621f9SAlan Cox 	/* Now stuff the nS values into the structure */
145a0f79b92STejun Heo 	t = ata_timing_find_mode(adev->dma_mode);
146025621f9SAlan Cox 	if (adev->dma_mode >= XFER_UDMA_0) {
147a0f79b92STejun Heo 		acpi->gtm.drive[unit].dma = t->udma;
148025621f9SAlan Cox 		acpi->gtm.flags |= (1 << (2 * unit));
149025621f9SAlan Cox 	} else {
150a0f79b92STejun Heo 		acpi->gtm.drive[unit].dma = t->cycle;
151025621f9SAlan Cox 		acpi->gtm.flags &= ~(1 << (2 * unit));
152025621f9SAlan Cox 	}
153025621f9SAlan Cox 	ata_acpi_stm(ap, &acpi->gtm);
154025621f9SAlan Cox 	/* See what mode we actually got */
155025621f9SAlan Cox 	ata_acpi_gtm(ap, &acpi->gtm);
156025621f9SAlan Cox }
157025621f9SAlan Cox 
158025621f9SAlan Cox /**
1599363c382STejun Heo  *	pacpi_qc_issue	-	command issue
160025621f9SAlan Cox  *	@qc: command pending
161025621f9SAlan Cox  *
162025621f9SAlan Cox  *	Called when the libata layer is about to issue a command. We wrap
163025621f9SAlan Cox  *	this interface so that we can load the correct ATA timings if
1643ad2f3fbSDaniel Mack  *	necessary.
165025621f9SAlan Cox  */
166025621f9SAlan Cox 
pacpi_qc_issue(struct ata_queued_cmd * qc)1679363c382STejun Heo static unsigned int pacpi_qc_issue(struct ata_queued_cmd *qc)
168025621f9SAlan Cox {
169025621f9SAlan Cox 	struct ata_port *ap = qc->ap;
170025621f9SAlan Cox 	struct ata_device *adev = qc->dev;
171025621f9SAlan Cox 	struct pata_acpi *acpi = ap->private_data;
172025621f9SAlan Cox 
173025621f9SAlan Cox 	if (acpi->gtm.flags & 0x10)
174360ff783STejun Heo 		return ata_bmdma_qc_issue(qc);
175025621f9SAlan Cox 
176025621f9SAlan Cox 	if (adev != acpi->last) {
177025621f9SAlan Cox 		pacpi_set_piomode(ap, adev);
178b15b3ebaSAlan Cox 		if (ata_dma_enabled(adev))
179025621f9SAlan Cox 			pacpi_set_dmamode(ap, adev);
180025621f9SAlan Cox 		acpi->last = adev;
181025621f9SAlan Cox 	}
182360ff783STejun Heo 	return ata_bmdma_qc_issue(qc);
183025621f9SAlan Cox }
184025621f9SAlan Cox 
185025621f9SAlan Cox /**
186025621f9SAlan Cox  *	pacpi_port_start	-	port setup
187025621f9SAlan Cox  *	@ap: ATA port being set up
188025621f9SAlan Cox  *
189025621f9SAlan Cox  *	Use the port_start hook to maintain private control structures
190025621f9SAlan Cox  */
191025621f9SAlan Cox 
pacpi_port_start(struct ata_port * ap)192025621f9SAlan Cox static int pacpi_port_start(struct ata_port *ap)
193025621f9SAlan Cox {
194025621f9SAlan Cox 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
195025621f9SAlan Cox 	struct pata_acpi *acpi;
196025621f9SAlan Cox 
197f1bc1e4cSAaron Lu 	if (ACPI_HANDLE(&ap->tdev) == NULL)
198025621f9SAlan Cox 		return -ENODEV;
199025621f9SAlan Cox 
200025621f9SAlan Cox 	acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
201025621f9SAlan Cox 	if (ap->private_data == NULL)
202025621f9SAlan Cox 		return -ENOMEM;
203025621f9SAlan Cox 	acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
204025621f9SAlan Cox 	acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
20547db477eSGreg Dietsche 	return ata_bmdma_port_start(ap);
206025621f9SAlan Cox }
207025621f9SAlan Cox 
208*25df73d9SBart Van Assche static const struct scsi_host_template pacpi_sht = {
20968d1d07bSTejun Heo 	ATA_BMDMA_SHT(DRV_NAME),
210025621f9SAlan Cox };
211025621f9SAlan Cox 
212029cfd6bSTejun Heo static struct ata_port_operations pacpi_ops = {
213029cfd6bSTejun Heo 	.inherits		= &ata_bmdma_port_ops,
2149363c382STejun Heo 	.qc_issue		= pacpi_qc_issue,
215029cfd6bSTejun Heo 	.cable_detect		= pacpi_cable_detect,
216029cfd6bSTejun Heo 	.mode_filter		= pacpi_mode_filter,
217025621f9SAlan Cox 	.set_piomode		= pacpi_set_piomode,
218025621f9SAlan Cox 	.set_dmamode		= pacpi_set_dmamode,
219887125e3STejun Heo 	.prereset		= pacpi_pre_reset,
220025621f9SAlan Cox 	.port_start		= pacpi_port_start,
221025621f9SAlan Cox };
222025621f9SAlan Cox 
223025621f9SAlan Cox 
224025621f9SAlan Cox /**
225025621f9SAlan Cox  *	pacpi_init_one - Register ACPI ATA PCI device with kernel services
226025621f9SAlan Cox  *	@pdev: PCI device to register
227ff237990SLee Jones  *	@id: PCI device ID
228025621f9SAlan Cox  *
229025621f9SAlan Cox  *	Called from kernel PCI layer.
230025621f9SAlan Cox  *
231025621f9SAlan Cox  *	LOCKING:
232025621f9SAlan Cox  *	Inherited from PCI layer (may sleep).
233025621f9SAlan Cox  *
234025621f9SAlan Cox  *	RETURNS:
235025621f9SAlan Cox  *	Zero on success, or -ERRNO value.
236025621f9SAlan Cox  */
237025621f9SAlan Cox 
pacpi_init_one(struct pci_dev * pdev,const struct pci_device_id * id)238025621f9SAlan Cox static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
239025621f9SAlan Cox {
240025621f9SAlan Cox 	static const struct ata_port_info info = {
241c10f97b9SSergei Shtylyov 		.flags		= ATA_FLAG_SLAVE_POSS,
242025621f9SAlan Cox 
24314bdef98SErik Inge Bolsø 		.pio_mask	= ATA_PIO4,
24414bdef98SErik Inge Bolsø 		.mwdma_mask	= ATA_MWDMA2,
24514bdef98SErik Inge Bolsø 		.udma_mask 	= ATA_UDMA6,
246025621f9SAlan Cox 
247025621f9SAlan Cox 		.port_ops	= &pacpi_ops,
248025621f9SAlan Cox 	};
249025621f9SAlan Cox 	const struct ata_port_info *ppi[] = { &info, NULL };
25005177f17SAlan Cox 	if (pdev->vendor == PCI_VENDOR_ID_ATI) {
25105177f17SAlan Cox 		int rc = pcim_enable_device(pdev);
25205177f17SAlan Cox 		if (rc < 0)
25305177f17SAlan Cox 			return rc;
25405177f17SAlan Cox 		pcim_pin_device(pdev);
25505177f17SAlan Cox 	}
2561c5afdf7STejun Heo 	return ata_pci_bmdma_init_one(pdev, ppi, &pacpi_sht, NULL, 0);
257025621f9SAlan Cox }
258025621f9SAlan Cox 
259025621f9SAlan Cox static const struct pci_device_id pacpi_pci_tbl[] = {
260025621f9SAlan Cox 	{ PCI_ANY_ID,		PCI_ANY_ID,			   PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
261025621f9SAlan Cox 	{ }	/* terminate list */
262025621f9SAlan Cox };
263025621f9SAlan Cox 
264025621f9SAlan Cox static struct pci_driver pacpi_pci_driver = {
265025621f9SAlan Cox 	.name			= DRV_NAME,
266025621f9SAlan Cox 	.id_table		= pacpi_pci_tbl,
267025621f9SAlan Cox 	.probe			= pacpi_init_one,
268025621f9SAlan Cox 	.remove			= ata_pci_remove_one,
26958eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
270025621f9SAlan Cox 	.suspend		= ata_pci_device_suspend,
271025621f9SAlan Cox 	.resume			= ata_pci_device_resume,
2728e2840e0STejun Heo #endif
273025621f9SAlan Cox };
274025621f9SAlan Cox 
2752fc75da0SAxel Lin module_pci_driver(pacpi_pci_driver);
276025621f9SAlan Cox 
277025621f9SAlan Cox MODULE_AUTHOR("Alan Cox");
278025621f9SAlan Cox MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
279025621f9SAlan Cox MODULE_LICENSE("GPL");
280025621f9SAlan Cox MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
281025621f9SAlan Cox MODULE_VERSION(DRV_VERSION);
282