xref: /OK3568_Linux_fs/kernel/drivers/ata/pata_marvell.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	Marvell PATA driver.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	For the moment we drive the PATA port in legacy mode. That
6*4882a593Smuzhiyun  *	isn't making full use of the device functionality but it is
7*4882a593Smuzhiyun  *	easy to get working.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *	(c) 2006 Red Hat
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/pci.h>
15*4882a593Smuzhiyun #include <linux/blkdev.h>
16*4882a593Smuzhiyun #include <linux/delay.h>
17*4882a593Smuzhiyun #include <linux/device.h>
18*4882a593Smuzhiyun #include <scsi/scsi_host.h>
19*4882a593Smuzhiyun #include <linux/libata.h>
20*4882a593Smuzhiyun #include <linux/ata.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define DRV_NAME	"pata_marvell"
23*4882a593Smuzhiyun #define DRV_VERSION	"0.1.6"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  *	marvell_pata_active	-	check if PATA is active
27*4882a593Smuzhiyun  *	@pdev: PCI device
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  *	Returns 1 if the PATA port may be active. We know how to check this
30*4882a593Smuzhiyun  *	for the 6145 but not the other devices
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
marvell_pata_active(struct pci_dev * pdev)33*4882a593Smuzhiyun static int marvell_pata_active(struct pci_dev *pdev)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	int i;
36*4882a593Smuzhiyun 	u32 devices;
37*4882a593Smuzhiyun 	void __iomem *barp;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	/* We don't yet know how to do this for other devices */
40*4882a593Smuzhiyun 	if (pdev->device != 0x6145)
41*4882a593Smuzhiyun 		return 1;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	barp = pci_iomap(pdev, 5, 0x10);
44*4882a593Smuzhiyun 	if (barp == NULL)
45*4882a593Smuzhiyun 		return -ENOMEM;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	printk("BAR5:");
48*4882a593Smuzhiyun 	for(i = 0; i <= 0x0F; i++)
49*4882a593Smuzhiyun 		printk("%02X:%02X ", i, ioread8(barp + i));
50*4882a593Smuzhiyun 	printk("\n");
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	devices = ioread32(barp + 0x0C);
53*4882a593Smuzhiyun 	pci_iounmap(pdev, barp);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	if (devices & 0x10)
56*4882a593Smuzhiyun 		return 1;
57*4882a593Smuzhiyun 	return 0;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun  *	marvell_pre_reset	-	probe begin
62*4882a593Smuzhiyun  *	@link: link
63*4882a593Smuzhiyun  *	@deadline: deadline jiffies for the operation
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  *	Perform the PATA port setup we need.
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun 
marvell_pre_reset(struct ata_link * link,unsigned long deadline)68*4882a593Smuzhiyun static int marvell_pre_reset(struct ata_link *link, unsigned long deadline)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct ata_port *ap = link->ap;
71*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	if (pdev->device == 0x6145 && ap->port_no == 0 &&
74*4882a593Smuzhiyun 		!marvell_pata_active(pdev))	/* PATA enable ? */
75*4882a593Smuzhiyun 			return -ENOENT;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return ata_sff_prereset(link, deadline);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
marvell_cable_detect(struct ata_port * ap)80*4882a593Smuzhiyun static int marvell_cable_detect(struct ata_port *ap)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	/* Cable type */
83*4882a593Smuzhiyun 	switch(ap->port_no)
84*4882a593Smuzhiyun 	{
85*4882a593Smuzhiyun 	case 0:
86*4882a593Smuzhiyun 		if (!ap->ioaddr.bmdma_addr)
87*4882a593Smuzhiyun 			return ATA_CBL_PATA_UNK;
88*4882a593Smuzhiyun 		if (ioread8(ap->ioaddr.bmdma_addr + 1) & 1)
89*4882a593Smuzhiyun 			return ATA_CBL_PATA40;
90*4882a593Smuzhiyun 		return ATA_CBL_PATA80;
91*4882a593Smuzhiyun 	case 1: /* Legacy SATA port */
92*4882a593Smuzhiyun 		return ATA_CBL_SATA;
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	BUG();
96*4882a593Smuzhiyun 	return 0;	/* Our BUG macro needs the right markup */
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /* No PIO or DMA methods needed for this device */
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun static struct scsi_host_template marvell_sht = {
102*4882a593Smuzhiyun 	ATA_BMDMA_SHT(DRV_NAME),
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun static struct ata_port_operations marvell_ops = {
106*4882a593Smuzhiyun 	.inherits		= &ata_bmdma_port_ops,
107*4882a593Smuzhiyun 	.cable_detect		= marvell_cable_detect,
108*4882a593Smuzhiyun 	.prereset		= marvell_pre_reset,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /**
113*4882a593Smuzhiyun  *	marvell_init_one - Register Marvell ATA PCI device with kernel services
114*4882a593Smuzhiyun  *	@pdev: PCI device to register
115*4882a593Smuzhiyun  *	@ent: Entry in marvell_pci_tbl matching with @pdev
116*4882a593Smuzhiyun  *
117*4882a593Smuzhiyun  *	Called from kernel PCI layer.
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  *	LOCKING:
120*4882a593Smuzhiyun  *	Inherited from PCI layer (may sleep).
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  *	RETURNS:
123*4882a593Smuzhiyun  *	Zero on success, or -ERRNO value.
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun 
marvell_init_one(struct pci_dev * pdev,const struct pci_device_id * id)126*4882a593Smuzhiyun static int marvell_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	static const struct ata_port_info info = {
129*4882a593Smuzhiyun 		.flags		= ATA_FLAG_SLAVE_POSS,
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		.pio_mask	= ATA_PIO4,
132*4882a593Smuzhiyun 		.mwdma_mask	= ATA_MWDMA2,
133*4882a593Smuzhiyun 		.udma_mask 	= ATA_UDMA5,
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 		.port_ops	= &marvell_ops,
136*4882a593Smuzhiyun 	};
137*4882a593Smuzhiyun 	static const struct ata_port_info info_sata = {
138*4882a593Smuzhiyun 		/* Slave possible as its magically mapped not real */
139*4882a593Smuzhiyun 		.flags		= ATA_FLAG_SLAVE_POSS,
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 		.pio_mask	= ATA_PIO4,
142*4882a593Smuzhiyun 		.mwdma_mask	= ATA_MWDMA2,
143*4882a593Smuzhiyun 		.udma_mask 	= ATA_UDMA6,
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		.port_ops	= &marvell_ops,
146*4882a593Smuzhiyun 	};
147*4882a593Smuzhiyun 	const struct ata_port_info *ppi[] = { &info, &info_sata };
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if (pdev->device == 0x6101)
150*4882a593Smuzhiyun 		ppi[1] = &ata_dummy_port_info;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SATA_AHCI)
153*4882a593Smuzhiyun 	if (!marvell_pata_active(pdev)) {
154*4882a593Smuzhiyun 		printk(KERN_INFO DRV_NAME ": PATA port not active, deferring to AHCI driver.\n");
155*4882a593Smuzhiyun 		return -ENODEV;
156*4882a593Smuzhiyun 	}
157*4882a593Smuzhiyun #endif
158*4882a593Smuzhiyun 	return ata_pci_bmdma_init_one(pdev, ppi, &marvell_sht, NULL, 0);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun static const struct pci_device_id marvell_pci_tbl[] = {
162*4882a593Smuzhiyun 	{ PCI_DEVICE(0x11AB, 0x6101), },
163*4882a593Smuzhiyun 	{ PCI_DEVICE(0x11AB, 0x6121), },
164*4882a593Smuzhiyun 	{ PCI_DEVICE(0x11AB, 0x6123), },
165*4882a593Smuzhiyun 	{ PCI_DEVICE(0x11AB, 0x6145), },
166*4882a593Smuzhiyun 	{ PCI_DEVICE(0x1B4B, 0x91A0), },
167*4882a593Smuzhiyun 	{ PCI_DEVICE(0x1B4B, 0x91A4), },
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	{ }	/* terminate list */
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun static struct pci_driver marvell_pci_driver = {
173*4882a593Smuzhiyun 	.name			= DRV_NAME,
174*4882a593Smuzhiyun 	.id_table		= marvell_pci_tbl,
175*4882a593Smuzhiyun 	.probe			= marvell_init_one,
176*4882a593Smuzhiyun 	.remove			= ata_pci_remove_one,
177*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
178*4882a593Smuzhiyun 	.suspend		= ata_pci_device_suspend,
179*4882a593Smuzhiyun 	.resume			= ata_pci_device_resume,
180*4882a593Smuzhiyun #endif
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun module_pci_driver(marvell_pci_driver);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun MODULE_AUTHOR("Alan Cox");
186*4882a593Smuzhiyun MODULE_DESCRIPTION("SCSI low-level driver for Marvell ATA in legacy mode");
187*4882a593Smuzhiyun MODULE_LICENSE("GPL");
188*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, marvell_pci_tbl);
189*4882a593Smuzhiyun MODULE_VERSION(DRV_VERSION);
190