xref: /linux/drivers/ata/pata_legacy.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   pata-legacy.c - Legacy port PATA/SATA controller driver.
4  *   Copyright 2005/2006 Red Hat, all rights reserved.
5  *
6  *   An ATA driver for the legacy ATA ports.
7  *
8  *  This driver handles legacy (that is "ISA side") IDE ports found
9  *  on PC class systems. There are three hybrid devices that are exceptions:
10  *  The Cyrix 5510/5520 where a pre SFF ATA device is on the bridge and
11  *  the MPIIX where the tuning is PCI side but the IDE is "ISA side".
12  */
13 
14 #include <linux/async.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/blkdev.h>
20 #include <linux/delay.h>
21 #include <scsi/scsi_host.h>
22 #include <linux/ata.h>
23 #include <linux/libata.h>
24 #include <linux/platform_device.h>
25 
26 #define DRV_NAME "pata_legacy"
27 #define DRV_VERSION "0.6.5"
28 
29 #define NR_HOST 6
30 
31 static int all;
32 module_param(all, int, 0444);
33 MODULE_PARM_DESC(all,
34 		 "Set to probe unclaimed pri/sec ISA port ranges even if PCI");
35 
36 static int probe_all;
37 module_param(probe_all, int, 0);
38 MODULE_PARM_DESC(probe_all,
39 		 "Set to probe tertiary+ ISA port ranges even if PCI");
40 
41 static int probe_mask = ~0;
42 module_param(probe_mask, int, 0);
43 MODULE_PARM_DESC(probe_mask, "Probe mask for legacy ISA PATA ports");
44 
45 static int autospeed;
46 module_param(autospeed, int, 0);
47 MODULE_PARM_DESC(autospeed, "Chip present that snoops speed changes");
48 
49 static int pio_mask = ATA_PIO4;
50 module_param(pio_mask, int, 0);
51 MODULE_PARM_DESC(pio_mask, "PIO range for autospeed devices");
52 
53 static int iordy_mask = 0xFFFFFFFF;
54 module_param(iordy_mask, int, 0);
55 MODULE_PARM_DESC(iordy_mask, "Use IORDY if available");
56 
57 enum controller {
58 	BIOS = 0,
59 	SNOOP = 1,
60 
61 	UNKNOWN = -1
62 };
63 
64 struct legacy_data {
65 	unsigned long timing;
66 	u8 clock[2];
67 	u8 last;
68 	int fast;
69 	enum controller type;
70 	struct platform_device *platform_dev;
71 };
72 
73 struct legacy_probe {
74 	unsigned char *name;
75 	unsigned long port;
76 	unsigned int irq;
77 	unsigned int slot;
78 	enum controller type;
79 	unsigned long private;
80 };
81 
82 struct legacy_controller {
83 	const char *name;
84 	struct ata_port_operations *ops;
85 	unsigned int pio_mask;
86 	unsigned int flags;
87 	unsigned int pflags;
88 	int (*setup)(struct platform_device *, struct legacy_probe *probe,
89 		struct legacy_data *data);
90 };
91 
92 static int legacy_port[NR_HOST] = { 0x1f0, 0x170, 0x1e8, 0x168, 0x1e0, 0x160 };
93 
94 static struct legacy_probe probe_list[NR_HOST];
95 static struct legacy_data legacy_data[NR_HOST];
96 static struct ata_host *legacy_host[NR_HOST];
97 
98 /**
99  *	legacy_probe_add	-	Add interface to probe list
100  *	@port: Controller port
101  *	@irq: IRQ number
102  *	@type: Controller type
103  *	@private: Controller specific info
104  *
105  *	Add an entry into the probe list for ATA controllers. This is used
106  *	to add the default ISA slots and then to build up the table
107  *	further according to other ISA/Weird device scans
108  *
109  *	An I/O port list is used to keep ordering stable and sane, as we
110  *	don't have any good way to talk about ordering otherwise
111  */
112 
113 static int legacy_probe_add(unsigned long port, unsigned int irq,
114 				enum controller type, unsigned long private)
115 {
116 	struct legacy_probe *lp = &probe_list[0];
117 	int i;
118 	struct legacy_probe *free = NULL;
119 
120 	for (i = 0; i < NR_HOST; i++) {
121 		if (lp->port == 0 && free == NULL)
122 			free = lp;
123 		/* Matching port, or the correct slot for ordering */
124 		if (lp->port == port || legacy_port[i] == port) {
125 			if (!(probe_mask & 1 << i))
126 				return -1;
127 			free = lp;
128 			break;
129 		}
130 		lp++;
131 	}
132 	if (free == NULL) {
133 		printk(KERN_ERR "pata_legacy: Too many interfaces.\n");
134 		return -1;
135 	}
136 	/* Fill in the entry for later probing */
137 	free->port = port;
138 	free->irq = irq;
139 	free->type = type;
140 	free->private = private;
141 	return 0;
142 }
143 
144 
145 /**
146  *	legacy_set_mode		-	mode setting
147  *	@link: IDE link
148  *	@unused: Device that failed when error is returned
149  *
150  *	Use a non standard set_mode function. We don't want to be tuned.
151  *
152  *	The BIOS configured everything. Our job is not to fiddle. Just use
153  *	whatever PIO the hardware is using and leave it at that. When we
154  *	get some kind of nice user driven API for control then we can
155  *	expand on this as per hdparm in the base kernel.
156  */
157 
158 static int legacy_set_mode(struct ata_link *link, struct ata_device **unused)
159 {
160 	struct ata_device *dev;
161 
162 	ata_for_each_dev(dev, link, ENABLED) {
163 		ata_dev_info(dev, "configured for PIO\n");
164 		dev->pio_mode = XFER_PIO_0;
165 		dev->xfer_mode = XFER_PIO_0;
166 		dev->xfer_shift = ATA_SHIFT_PIO;
167 		dev->flags |= ATA_DFLAG_PIO;
168 	}
169 	return 0;
170 }
171 
172 static const struct scsi_host_template legacy_sht = {
173 	ATA_PIO_SHT(DRV_NAME),
174 };
175 
176 static const struct ata_port_operations legacy_base_port_ops = {
177 	.inherits	= &ata_sff_port_ops,
178 	.cable_detect	= ata_cable_40wire,
179 };
180 
181 /*
182  *	These ops are used if the user indicates the hardware
183  *	snoops the commands to decide on the mode and handles the
184  *	mode selection "magically" itself. Several legacy controllers
185  *	do this. The mode range can be set if it is not 0x1F by setting
186  *	pio_mask as well.
187  */
188 
189 static struct ata_port_operations simple_port_ops = {
190 	.inherits	= &legacy_base_port_ops,
191 	.sff_data_xfer	= ata_sff_data_xfer32,
192 };
193 
194 static struct ata_port_operations legacy_port_ops = {
195 	.inherits	= &legacy_base_port_ops,
196 	.sff_data_xfer	= ata_sff_data_xfer32,
197 	.set_mode	= legacy_set_mode,
198 };
199 
200 static struct legacy_controller controllers[] = {
201 	{"BIOS",	&legacy_port_ops, 	ATA_PIO4,
202 			ATA_FLAG_NO_IORDY,	0,			NULL },
203 	{"Snooping", 	&simple_port_ops, 	ATA_PIO4,
204 			0,			0,			NULL },
205 };
206 
207 /**
208  *	probe_chip_type		-	Discover controller
209  *	@probe: Probe entry to check
210  *
211  *	Probe an ATA port and identify the type of controller. We don't
212  *	check if the controller appears to be driveless at this point.
213  */
214 
215 static __init int probe_chip_type(struct legacy_probe *probe)
216 {
217 	int mask = 1 << probe->slot;
218 
219 	if (autospeed & mask)
220 		return SNOOP;
221 	return BIOS;
222 }
223 
224 
225 /**
226  *	legacy_init_one		-	attach a legacy interface
227  *	@probe: probe record
228  *
229  *	Register an ISA bus IDE interface. Such interfaces are PIO and we
230  *	assume do not support IRQ sharing.
231  */
232 
233 static __init int legacy_init_one(struct legacy_probe *probe)
234 {
235 	struct legacy_controller *controller = &controllers[probe->type];
236 	int pio_modes = controller->pio_mask;
237 	unsigned long io = probe->port;
238 	u32 mask = (1 << probe->slot);
239 	struct ata_port_operations *ops = controller->ops;
240 	struct legacy_data *ld = &legacy_data[probe->slot];
241 	struct ata_host *host = NULL;
242 	struct ata_port *ap;
243 	struct platform_device *pdev;
244 	struct ata_device *dev;
245 	void __iomem *io_addr, *ctrl_addr;
246 	u32 iordy = (iordy_mask & mask) ? 0: ATA_FLAG_NO_IORDY;
247 	int ret;
248 
249 	iordy |= controller->flags;
250 
251 	pdev = platform_device_register_simple(DRV_NAME, probe->slot, NULL, 0);
252 	if (IS_ERR(pdev))
253 		return PTR_ERR(pdev);
254 
255 	ret = -EBUSY;
256 	if (devm_request_region(&pdev->dev, io, 8, "pata_legacy") == NULL ||
257 	    devm_request_region(&pdev->dev, io + 0x0206, 1,
258 							"pata_legacy") == NULL)
259 		goto fail;
260 
261 	ret = -ENOMEM;
262 	io_addr = devm_ioport_map(&pdev->dev, io, 8);
263 	ctrl_addr = devm_ioport_map(&pdev->dev, io + 0x0206, 1);
264 	if (!io_addr || !ctrl_addr)
265 		goto fail;
266 	ld->type = probe->type;
267 	if (controller->setup)
268 		if (controller->setup(pdev, probe, ld) < 0)
269 			goto fail;
270 	host = ata_host_alloc(&pdev->dev, 1);
271 	if (!host)
272 		goto fail;
273 	ap = host->ports[0];
274 
275 	ap->ops = ops;
276 	ap->pio_mask = pio_modes;
277 	ap->flags |= ATA_FLAG_SLAVE_POSS | iordy;
278 	ap->pflags |= controller->pflags;
279 	ap->ioaddr.cmd_addr = io_addr;
280 	ap->ioaddr.altstatus_addr = ctrl_addr;
281 	ap->ioaddr.ctl_addr = ctrl_addr;
282 	ata_sff_std_ports(&ap->ioaddr);
283 	ap->host->private_data = ld;
284 
285 	ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io, io + 0x0206);
286 
287 	ret = ata_host_activate(host, probe->irq, ata_sff_interrupt, 0,
288 				&legacy_sht);
289 	if (ret)
290 		goto fail;
291 	async_synchronize_full();
292 	ld->platform_dev = pdev;
293 
294 	/* Nothing found means we drop the port as its probably not there */
295 
296 	ret = -ENODEV;
297 	ata_for_each_dev(dev, &ap->link, ALL) {
298 		if (!ata_dev_absent(dev)) {
299 			legacy_host[probe->slot] = host;
300 			ld->platform_dev = pdev;
301 			return 0;
302 		}
303 	}
304 	ata_host_detach(host);
305 fail:
306 	platform_device_unregister(pdev);
307 	return ret;
308 }
309 
310 /**
311  *	legacy_check_special_cases	-	ATA special cases
312  *	@p: PCI device to check
313  *	@primary: set this if we find an ATA master
314  *	@secondary: set this if we find an ATA secondary
315  *
316  *	A small number of vendors implemented early PCI ATA interfaces
317  *	on bridge logic without the ATA interface being PCI visible.
318  *	Where we have a matching PCI driver we must skip the relevant
319  *	device here. If we don't know about it then the legacy driver
320  *	is the right driver anyway.
321  */
322 
323 static void __init legacy_check_special_cases(struct pci_dev *p, int *primary,
324 								int *secondary)
325 {
326 	/* Cyrix CS5510 pre SFF MWDMA ATA on the bridge */
327 	if (p->vendor == 0x1078 && p->device == 0x0000) {
328 		*primary = *secondary = 1;
329 		return;
330 	}
331 	/* Cyrix CS5520 pre SFF MWDMA ATA on the bridge */
332 	if (p->vendor == 0x1078 && p->device == 0x0002) {
333 		*primary = *secondary = 1;
334 		return;
335 	}
336 	/* Intel MPIIX - PIO ATA on non PCI side of bridge */
337 	if (p->vendor == 0x8086 && p->device == 0x1234) {
338 		u16 r;
339 		pci_read_config_word(p, 0x6C, &r);
340 		if (r & 0x8000) {
341 			/* ATA port enabled */
342 			if (r & 0x4000)
343 				*secondary = 1;
344 			else
345 				*primary = 1;
346 		}
347 		return;
348 	}
349 }
350 
351 /**
352  *	legacy_init		-	attach legacy interfaces
353  *
354  *	Attach legacy IDE interfaces by scanning the usual IRQ/port suspects.
355  *	Right now we do not scan the ide0 and ide1 address but should do so
356  *	for non PCI systems or systems with no PCI IDE legacy mode devices.
357  *	If you fix that note there are special cases to consider like CS5510/20.
358  */
359 
360 static __init int legacy_init(void)
361 {
362 	int i;
363 	int ct = 0;
364 	int primary = 0;
365 	int secondary = 0;
366 	int pci_present = 0;
367 	struct legacy_probe *pl = &probe_list[0];
368 	int slot = 0;
369 
370 	struct pci_dev *p = NULL;
371 
372 	for_each_pci_dev(p) {
373 		int r;
374 		/* Check for any overlap of the system ATA mappings. Native
375 		   mode controllers stuck on these addresses or some devices
376 		   in 'raid' mode won't be found by the storage class test */
377 		for (r = 0; r < 6; r++) {
378 			if (pci_resource_start(p, r) == 0x1f0)
379 				primary = 1;
380 			if (pci_resource_start(p, r) == 0x170)
381 				secondary = 1;
382 		}
383 		/* Check for special cases */
384 		legacy_check_special_cases(p, &primary, &secondary);
385 
386 		/* If PCI bus is present then don't probe for tertiary
387 		   legacy ports */
388 		pci_present = 1;
389 	}
390 
391 	if (primary == 0 || all)
392 		legacy_probe_add(0x1F0, 14, UNKNOWN, 0);
393 	if (secondary == 0 || all)
394 		legacy_probe_add(0x170, 15, UNKNOWN, 0);
395 
396 	if (probe_all || !pci_present) {
397 		/* ISA extra ports */
398 		legacy_probe_add(0x1E8, 11, UNKNOWN, 0);
399 		legacy_probe_add(0x168, 10, UNKNOWN, 0);
400 		legacy_probe_add(0x1E0, 8, UNKNOWN, 0);
401 		legacy_probe_add(0x160, 12, UNKNOWN, 0);
402 	}
403 
404 	for (i = 0; i < NR_HOST; i++, pl++) {
405 		if (pl->port == 0)
406 			continue;
407 		if (pl->type == UNKNOWN)
408 			pl->type = probe_chip_type(pl);
409 		pl->slot = slot++;
410 		if (legacy_init_one(pl) == 0)
411 			ct++;
412 	}
413 	if (ct != 0)
414 		return 0;
415 	return -ENODEV;
416 }
417 
418 static __exit void legacy_exit(void)
419 {
420 	int i;
421 
422 	for (i = 0; i < NR_HOST; i++) {
423 		struct legacy_data *ld = &legacy_data[i];
424 
425 		if (legacy_host[i])
426 			ata_host_detach(legacy_host[i]);
427 		platform_device_unregister(ld->platform_dev);
428 	}
429 }
430 
431 MODULE_AUTHOR("Alan Cox");
432 MODULE_DESCRIPTION("low-level driver for legacy ATA");
433 MODULE_LICENSE("GPL");
434 MODULE_VERSION(DRV_VERSION);
435 
436 module_init(legacy_init);
437 module_exit(legacy_exit);
438