xref: /linux/drivers/dio/dio.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /* Code to support devices on the DIO and DIO-II bus
31da177e4SLinus Torvalds  * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
41da177e4SLinus Torvalds  * Copyright (C) 2004 Jochen Friedrich <jochen@scram.de>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This code has basically these routines at the moment:
71da177e4SLinus Torvalds  * int dio_find(u_int deviceid)
81da177e4SLinus Torvalds  *    Search the list of DIO devices and return the select code
91da177e4SLinus Torvalds  *    of the next unconfigured device found that matches the given device ID.
101da177e4SLinus Torvalds  *    Note that the deviceid parameter should be the encoded ID.
111da177e4SLinus Torvalds  *    This means that framebuffers should pass it as
121da177e4SLinus Torvalds  *    DIO_ENCODE_ID(DIO_ID_FBUFFER,DIO_ID2_TOPCAT)
131da177e4SLinus Torvalds  *    (or whatever); everybody else just uses DIO_ID_FOOBAR.
141da177e4SLinus Torvalds  * unsigned long dio_scodetophysaddr(int scode)
151da177e4SLinus Torvalds  *    Return the physical address corresponding to the given select code.
161da177e4SLinus Torvalds  * int dio_scodetoipl(int scode)
171da177e4SLinus Torvalds  *    Every DIO card has a fixed interrupt priority level. This function
181da177e4SLinus Torvalds  *    returns it, whatever it is.
191da177e4SLinus Torvalds  * const char *dio_scodetoname(int scode)
201da177e4SLinus Torvalds  *    Return a character string describing this board [might be "" if
211da177e4SLinus Torvalds  *    not CONFIG_DIO_CONSTANTS]
221da177e4SLinus Torvalds  * void dio_config_board(int scode)     mark board as configured in the list
231da177e4SLinus Torvalds  * void dio_unconfig_board(int scode)   mark board as no longer configured
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * This file is based on the way the Amiga port handles Zorro II cards,
261da177e4SLinus Torvalds  * although we aren't so complicated...
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds #include <linux/module.h>
291da177e4SLinus Torvalds #include <linux/string.h>
301da177e4SLinus Torvalds #include <linux/types.h>
311da177e4SLinus Torvalds #include <linux/kernel.h>
321da177e4SLinus Torvalds #include <linux/init.h>
331da177e4SLinus Torvalds #include <linux/dio.h>
341da177e4SLinus Torvalds #include <linux/slab.h>                         /* kmalloc() */
357c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3610f4629fSAleksa Vučković #include <linux/io.h>                           /* readb() */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds struct dio_bus dio_bus = {
391da177e4SLinus Torvalds 	.resources = {
401da177e4SLinus Torvalds 		/* DIO range */
411da177e4SLinus Torvalds 		{ .name = "DIO mem", .start = 0x00600000, .end = 0x007fffff },
421da177e4SLinus Torvalds 		/* DIO-II range */
431da177e4SLinus Torvalds 		{ .name = "DIO-II mem", .start = 0x01000000, .end = 0x1fffffff }
441da177e4SLinus Torvalds 	},
451da177e4SLinus Torvalds 	.name = "DIO bus"
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* not a real config option yet! */
491da177e4SLinus Torvalds #define CONFIG_DIO_CONSTANTS
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #ifdef CONFIG_DIO_CONSTANTS
521da177e4SLinus Torvalds /* We associate each numeric ID with an appropriate descriptive string
531da177e4SLinus Torvalds  * using a constant array of these structs.
541da177e4SLinus Torvalds  * FIXME: we should be able to arrange to throw away most of the strings
551da177e4SLinus Torvalds  * using the initdata stuff. Then we wouldn't need to worry about
561da177e4SLinus Torvalds  * carrying them around...
571da177e4SLinus Torvalds  * I think we do this by copying them into newly kmalloc()ed memory and
581da177e4SLinus Torvalds  * marking the names[] array as .initdata ?
591da177e4SLinus Torvalds  */
608a4606c6SAleksa Vučković struct dioname {
611da177e4SLinus Torvalds 	int id;
621da177e4SLinus Torvalds 	const char *name;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* useful macro */
661da177e4SLinus Torvalds #define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x }
671da177e4SLinus Torvalds #define DIOFBNAME(x) { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x }
681da177e4SLinus Torvalds 
698a4606c6SAleksa Vučković static struct dioname names[] = {
701da177e4SLinus Torvalds 	DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
711da177e4SLinus Torvalds 	DIONAME(DCM), DIONAME(DCMREM),
721da177e4SLinus Torvalds 	DIONAME(LAN),
731da177e4SLinus Torvalds 	DIONAME(FHPIB), DIONAME(NHPIB),
741da177e4SLinus Torvalds 	DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
751da177e4SLinus Torvalds 	DIONAME(FBUFFER),
761da177e4SLinus Torvalds 	DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
771da177e4SLinus Torvalds 	DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
781da177e4SLinus Torvalds 	DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
791da177e4SLinus Torvalds 	DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
801da177e4SLinus Torvalds 	DIONAME(MISC12), DIONAME(MISC13),
811da177e4SLinus Torvalds 	DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
821da177e4SLinus Torvalds 	DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
831da177e4SLinus Torvalds 	DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
841da177e4SLinus Torvalds 	DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #undef DIONAME
881da177e4SLinus Torvalds #undef DIOFBNAME
891da177e4SLinus Torvalds 
90627f192dSGeert Uytterhoeven static const char unknowndioname[]
91627f192dSGeert Uytterhoeven 	= "unknown DIO board, please email linux-m68k@lists.linux-m68k.org";
921da177e4SLinus Torvalds 
dio_getname(int id)931da177e4SLinus Torvalds static const char *dio_getname(int id)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	/* return pointer to a constant string describing the board with given ID */
961da177e4SLinus Torvalds 	unsigned int i;
97aafce7bcSAleksa Vučković 
980a8320b0SAlejandro Martinez Ruiz 	for (i = 0; i < ARRAY_SIZE(names); i++)
991da177e4SLinus Torvalds 		if (names[i].id == id)
1001da177e4SLinus Torvalds 			return names[i].name;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	return unknowndioname;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #else
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static char dio_no_name[] = { 0 };
1081da177e4SLinus Torvalds #define dio_getname(_id)	(dio_no_name)
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds #endif /* CONFIG_DIO_CONSTANTS */
1111da177e4SLinus Torvalds 
dio_dev_release(struct device * dev)112*e63e9939SYang Yingliang static void dio_dev_release(struct device *dev)
113*e63e9939SYang Yingliang {
114*e63e9939SYang Yingliang 	struct dio_dev *ddev = container_of(dev, typeof(struct dio_dev), dev);
115*e63e9939SYang Yingliang 	kfree(ddev);
116*e63e9939SYang Yingliang }
117*e63e9939SYang Yingliang 
dio_find(int deviceid)1181da177e4SLinus Torvalds int __init dio_find(int deviceid)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	/* Called to find a DIO device before the full bus scan has run.
1211da177e4SLinus Torvalds 	 * Only used by the console driver.
1221da177e4SLinus Torvalds 	 */
1231da177e4SLinus Torvalds 	int scode, id;
1241da177e4SLinus Torvalds 	u_char prid, secid, i;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	for (scode = 0; scode < DIO_SCMAX; scode++) {
1271da177e4SLinus Torvalds 		void *va;
1281da177e4SLinus Torvalds 		unsigned long pa;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 		if (DIO_SCINHOLE(scode))
1311da177e4SLinus Torvalds 			continue;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 		pa = dio_scodetophysaddr(scode);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 		if (!pa)
1361da177e4SLinus Torvalds 			continue;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 		if (scode < DIOII_SCBASE)
1391da177e4SLinus Torvalds 			va = (void *)(pa + DIO_VIRADDRBASE);
1401da177e4SLinus Torvalds 		else
1411da177e4SLinus Torvalds 			va = ioremap(pa, PAGE_SIZE);
1421da177e4SLinus Torvalds 
143fe557319SChristoph Hellwig 		if (copy_from_kernel_nofault(&i,
144fe557319SChristoph Hellwig 				(unsigned char *)va + DIO_IDOFF, 1)) {
1451da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
1461da177e4SLinus Torvalds 				iounmap(va);
1471da177e4SLinus Torvalds 			continue;	     /* no board present at that select code */
1481da177e4SLinus Torvalds 		}
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 		prid = DIO_ID(va);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 		if (DIO_NEEDSSECID(prid)) {
1531da177e4SLinus Torvalds 			secid = DIO_SECID(va);
1541da177e4SLinus Torvalds 			id = DIO_ENCODE_ID(prid, secid);
1551da177e4SLinus Torvalds 		} else
1561da177e4SLinus Torvalds 			id = prid;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 		if (id == deviceid) {
1591da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
1601da177e4SLinus Torvalds 				iounmap(va);
1611da177e4SLinus Torvalds 			return scode;
1621da177e4SLinus Torvalds 		}
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	return -1;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /* This is the function that scans the DIO space and works out what
1691da177e4SLinus Torvalds  * hardware is actually present.
1701da177e4SLinus Torvalds  */
dio_init(void)1711da177e4SLinus Torvalds static int __init dio_init(void)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	int scode;
1741da177e4SLinus Torvalds 	int i;
1751da177e4SLinus Torvalds 	struct dio_dev *dev;
1762e4c77beSGeert Uytterhoeven 	int error;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (!MACH_IS_HP300)
1791da177e4SLinus Torvalds 		return 0;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds         printk(KERN_INFO "Scanning for DIO devices...\n");
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* Initialize the DIO bus */
1841da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dio_bus.devices);
1859591463aSKay Sievers 	dev_set_name(&dio_bus.dev, "dio");
1862e4c77beSGeert Uytterhoeven 	error = device_register(&dio_bus.dev);
1872e4c77beSGeert Uytterhoeven 	if (error) {
1882e4c77beSGeert Uytterhoeven 		pr_err("DIO: Error registering dio_bus\n");
1892e4c77beSGeert Uytterhoeven 		return error;
1902e4c77beSGeert Uytterhoeven 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* Request all resources */
1931da177e4SLinus Torvalds 	dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
1941da177e4SLinus Torvalds 	for (i = 0; i < dio_bus.num_resources; i++)
1951da177e4SLinus Torvalds 		request_resource(&iomem_resource, &dio_bus.resources[i]);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	/* Register all devices */
1988a4606c6SAleksa Vučković 	for (scode = 0; scode < DIO_SCMAX; ++scode) {
1991da177e4SLinus Torvalds 		u_char prid, secid = 0;	/* primary, secondary ID bytes */
2001da177e4SLinus Torvalds 		u_char *va;
2011da177e4SLinus Torvalds 		unsigned long pa;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 		if (DIO_SCINHOLE(scode))
2041da177e4SLinus Torvalds 			continue;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 		pa = dio_scodetophysaddr(scode);
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 		if (!pa)
2091da177e4SLinus Torvalds 			continue;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 		if (scode < DIOII_SCBASE)
2121da177e4SLinus Torvalds 			va = (void *)(pa + DIO_VIRADDRBASE);
2131da177e4SLinus Torvalds 		else
2141da177e4SLinus Torvalds 			va = ioremap(pa, PAGE_SIZE);
2151da177e4SLinus Torvalds 
216fe557319SChristoph Hellwig 		if (copy_from_kernel_nofault(&i,
217fe557319SChristoph Hellwig 				(unsigned char *)va + DIO_IDOFF, 1)) {
2181da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
2191da177e4SLinus Torvalds 				iounmap(va);
2201da177e4SLinus Torvalds 			continue;	      /* no board present at that select code */
2211da177e4SLinus Torvalds 		}
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 		/* Found a board, allocate it an entry in the list */
224e66860cbSDeepak Saxena 		dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
225077f5d90SYang Yingliang 		if (!dev) {
226077f5d90SYang Yingliang 			if (scode >= DIOII_SCBASE)
227077f5d90SYang Yingliang 				iounmap(va);
228d1d26f40SSalah Triki 			return -ENOMEM;
229077f5d90SYang Yingliang 		}
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		dev->bus = &dio_bus;
2321da177e4SLinus Torvalds 		dev->dev.parent = &dio_bus.dev;
2331da177e4SLinus Torvalds 		dev->dev.bus = &dio_bus_type;
234*e63e9939SYang Yingliang 		dev->dev.release = dio_dev_release;
2351da177e4SLinus Torvalds 		dev->scode = scode;
2361da177e4SLinus Torvalds 		dev->resource.start = pa;
2371da177e4SLinus Torvalds 		dev->resource.end = pa + DIO_SIZE(scode, va);
2389591463aSKay Sievers 		dev_set_name(&dev->dev, "%02x", scode);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 		/* read the ID byte(s) and encode if necessary. */
2411da177e4SLinus Torvalds 		prid = DIO_ID(va);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 		if (DIO_NEEDSSECID(prid)) {
2441da177e4SLinus Torvalds 			secid = DIO_SECID(va);
2451da177e4SLinus Torvalds 			dev->id = DIO_ENCODE_ID(prid, secid);
2461da177e4SLinus Torvalds 		} else
2471da177e4SLinus Torvalds 			dev->id = prid;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 		dev->ipl = DIO_IPL(va);
2501da177e4SLinus Torvalds 		strcpy(dev->name, dio_getname(dev->id));
2511da177e4SLinus Torvalds                 printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
2521da177e4SLinus Torvalds 		if (DIO_NEEDSSECID(prid))
2531da177e4SLinus Torvalds                         printk(":%02X", secid);
2541da177e4SLinus Torvalds                 printk(": %s\n", dev->name);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 		if (scode >= DIOII_SCBASE)
2571da177e4SLinus Torvalds 			iounmap(va);
2582e4c77beSGeert Uytterhoeven 		error = device_register(&dev->dev);
2592e4c77beSGeert Uytterhoeven 		if (error) {
2602e4c77beSGeert Uytterhoeven 			pr_err("DIO: Error registering device %s\n",
2612e4c77beSGeert Uytterhoeven 			       dev->name);
262*e63e9939SYang Yingliang 			put_device(&dev->dev);
2632e4c77beSGeert Uytterhoeven 			continue;
2642e4c77beSGeert Uytterhoeven 		}
2652e4c77beSGeert Uytterhoeven 		error = dio_create_sysfs_dev_files(dev);
2662e4c77beSGeert Uytterhoeven 		if (error)
2672e4c77beSGeert Uytterhoeven 			dev_err(&dev->dev, "Error creating sysfs files\n");
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 	return 0;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds subsys_initcall(dio_init);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds /* Bear in mind that this is called in the very early stages of initialisation
2751da177e4SLinus Torvalds  * in order to get the address of the serial port for the console...
2761da177e4SLinus Torvalds  */
dio_scodetophysaddr(int scode)2771da177e4SLinus Torvalds unsigned long dio_scodetophysaddr(int scode)
2781da177e4SLinus Torvalds {
2798a4606c6SAleksa Vučković 	if (scode >= DIOII_SCBASE)
2801da177e4SLinus Torvalds 		return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
2818a4606c6SAleksa Vučković 	else if (scode > DIO_SCMAX || scode < 0)
2821da177e4SLinus Torvalds 		return 0;
2831da177e4SLinus Torvalds 	else if (DIO_SCINHOLE(scode))
2841da177e4SLinus Torvalds 		return 0;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	return (DIO_BASE + scode * DIO_DEVSIZE);
2871da177e4SLinus Torvalds }
288