xref: /linux/drivers/ata/sata_uli.c (revision 8c13415c8a4383447c21ec832b20b3b283f0e01a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  sata_uli.c - ULi Electronics SATA
4  *
5  *  libata documentation is available via 'make {ps|pdf}docs',
6  *  as Documentation/driver-api/libata.rst
7  *
8  *  Hardware documentation available under NDA.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/gfp.h>
14 #include <linux/pci.h>
15 #include <linux/blkdev.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <scsi/scsi_host.h>
20 #include <linux/libata.h>
21 
22 #define DRV_NAME	"sata_uli"
23 #define DRV_VERSION	"1.3"
24 
25 enum {
26 	uli_5289		= 0,
27 	uli_5287		= 1,
28 	uli_5281		= 2,
29 
30 	uli_max_ports		= 4,
31 
32 	/* PCI configuration registers */
33 	ULI5287_BASE		= 0x90, /* sata0 phy SCR registers */
34 	ULI5287_OFFS		= 0x10, /* offset from sata0->sata1 phy regs */
35 	ULI5281_BASE		= 0x60, /* sata0 phy SCR  registers */
36 	ULI5281_OFFS		= 0x60, /* offset from sata0->sata1 phy regs */
37 };
38 
39 struct uli_priv {
40 	unsigned int		scr_cfg_addr[uli_max_ports];
41 };
42 
43 static int uli_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
44 static int uli_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
45 static int uli_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
46 
47 static const struct pci_device_id uli_pci_tbl[] = {
48 	{ PCI_VDEVICE(AL, 0x5289), .driver_data = uli_5289 },
49 	{ PCI_VDEVICE(AL, 0x5287), .driver_data = uli_5287 },
50 	{ PCI_VDEVICE(AL, 0x5281), .driver_data = uli_5281 },
51 	{ }	/* terminate list */
52 };
53 
54 static struct pci_driver uli_pci_driver = {
55 	.name			= DRV_NAME,
56 	.id_table		= uli_pci_tbl,
57 	.probe			= uli_init_one,
58 	.remove			= ata_pci_remove_one,
59 };
60 
61 static const struct scsi_host_template uli_sht = {
62 	ATA_BMDMA_SHT(DRV_NAME),
63 };
64 
65 static struct ata_port_operations uli_ops = {
66 	.inherits		= &ata_bmdma_port_ops,
67 	.scr_read		= uli_scr_read,
68 	.scr_write		= uli_scr_write,
69 	.reset.hardreset	= ATA_OP_NULL,
70 };
71 
72 static const struct ata_port_info uli_port_info = {
73 	.flags		= ATA_FLAG_SATA | ATA_FLAG_IGN_SIMPLEX,
74 	.pio_mask       = ATA_PIO4,
75 	.udma_mask      = ATA_UDMA6,
76 	.port_ops       = &uli_ops,
77 };
78 
79 
80 MODULE_AUTHOR("Peer Chen");
81 MODULE_DESCRIPTION("low-level driver for ULi Electronics SATA controller");
82 MODULE_LICENSE("GPL");
83 MODULE_DEVICE_TABLE(pci, uli_pci_tbl);
84 MODULE_VERSION(DRV_VERSION);
85 
86 static unsigned int get_scr_cfg_addr(struct ata_port *ap, unsigned int sc_reg)
87 {
88 	struct uli_priv *hpriv = ap->host->private_data;
89 	return hpriv->scr_cfg_addr[ap->port_no] + (4 * sc_reg);
90 }
91 
92 static u32 uli_scr_cfg_read(struct ata_link *link, unsigned int sc_reg)
93 {
94 	struct pci_dev *pdev = to_pci_dev(link->ap->host->dev);
95 	unsigned int cfg_addr = get_scr_cfg_addr(link->ap, sc_reg);
96 	u32 val;
97 
98 	pci_read_config_dword(pdev, cfg_addr, &val);
99 	return val;
100 }
101 
102 static void uli_scr_cfg_write(struct ata_link *link, unsigned int scr, u32 val)
103 {
104 	struct pci_dev *pdev = to_pci_dev(link->ap->host->dev);
105 	unsigned int cfg_addr = get_scr_cfg_addr(link->ap, scr);
106 
107 	pci_write_config_dword(pdev, cfg_addr, val);
108 }
109 
110 static int uli_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
111 {
112 	if (sc_reg > SCR_CONTROL)
113 		return -EINVAL;
114 
115 	*val = uli_scr_cfg_read(link, sc_reg);
116 	return 0;
117 }
118 
119 static int uli_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
120 {
121 	if (sc_reg > SCR_CONTROL) //SCR_CONTROL=2, SCR_ERROR=1, SCR_STATUS=0
122 		return -EINVAL;
123 
124 	uli_scr_cfg_write(link, sc_reg, val);
125 	return 0;
126 }
127 
128 static int uli_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
129 {
130 	const struct ata_port_info *ppi[] = { &uli_port_info, NULL };
131 	unsigned int board_idx = (unsigned int) ent->driver_data;
132 	struct ata_host *host;
133 	struct uli_priv *hpriv;
134 	void __iomem * const *iomap;
135 	struct ata_ioports *ioaddr;
136 	int n_ports, rc;
137 
138 	ata_print_version_once(&pdev->dev, DRV_VERSION);
139 
140 	rc = pcim_enable_device(pdev);
141 	if (rc)
142 		return rc;
143 
144 	n_ports = 2;
145 	if (board_idx == uli_5287)
146 		n_ports = 4;
147 
148 	/* allocate the host */
149 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
150 	if (!host)
151 		return -ENOMEM;
152 
153 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
154 	if (!hpriv)
155 		return -ENOMEM;
156 	host->private_data = hpriv;
157 
158 	/* the first two ports are standard SFF */
159 	rc = ata_pci_sff_init_host(host);
160 	if (rc)
161 		return rc;
162 
163 	ata_pci_bmdma_init(host);
164 
165 	iomap = host->iomap;
166 
167 	switch (board_idx) {
168 	case uli_5287:
169 		/* If there are four, the last two live right after
170 		 * the standard SFF ports.
171 		 */
172 		hpriv->scr_cfg_addr[0] = ULI5287_BASE;
173 		hpriv->scr_cfg_addr[1] = ULI5287_BASE + ULI5287_OFFS;
174 
175 		ioaddr = &host->ports[2]->ioaddr;
176 		ioaddr->cmd_addr = iomap[0] + 8;
177 		ioaddr->altstatus_addr =
178 		ioaddr->ctl_addr = (void __iomem *)
179 			((unsigned long)iomap[1] | ATA_PCI_CTL_OFS) + 4;
180 		ioaddr->bmdma_addr = iomap[4] + 16;
181 		hpriv->scr_cfg_addr[2] = ULI5287_BASE + ULI5287_OFFS*4;
182 		ata_sff_std_ports(ioaddr);
183 
184 		ata_port_desc(host->ports[2],
185 			"cmd 0x%llx ctl 0x%llx bmdma 0x%llx",
186 			(unsigned long long)pci_resource_start(pdev, 0) + 8,
187 			((unsigned long long)pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS) + 4,
188 			(unsigned long long)pci_resource_start(pdev, 4) + 16);
189 
190 		ioaddr = &host->ports[3]->ioaddr;
191 		ioaddr->cmd_addr = iomap[2] + 8;
192 		ioaddr->altstatus_addr =
193 		ioaddr->ctl_addr = (void __iomem *)
194 			((unsigned long)iomap[3] | ATA_PCI_CTL_OFS) + 4;
195 		ioaddr->bmdma_addr = iomap[4] + 24;
196 		hpriv->scr_cfg_addr[3] = ULI5287_BASE + ULI5287_OFFS*5;
197 		ata_sff_std_ports(ioaddr);
198 
199 		ata_port_desc(host->ports[2],
200 			"cmd 0x%llx ctl 0x%llx bmdma 0x%llx",
201 			(unsigned long long)pci_resource_start(pdev, 2) + 9,
202 			((unsigned long long)pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS) + 4,
203 			(unsigned long long)pci_resource_start(pdev, 4) + 24);
204 
205 		break;
206 
207 	case uli_5289:
208 		hpriv->scr_cfg_addr[0] = ULI5287_BASE;
209 		hpriv->scr_cfg_addr[1] = ULI5287_BASE + ULI5287_OFFS;
210 		break;
211 
212 	case uli_5281:
213 		hpriv->scr_cfg_addr[0] = ULI5281_BASE;
214 		hpriv->scr_cfg_addr[1] = ULI5281_BASE + ULI5281_OFFS;
215 		break;
216 
217 	default:
218 		BUG();
219 		break;
220 	}
221 
222 	pci_set_master(pdev);
223 	pcim_intx(pdev, 1);
224 	return ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
225 				 IRQF_SHARED, &uli_sht);
226 }
227 
228 module_pci_driver(uli_pci_driver);
229