xref: /linux/drivers/dio/dio.c (revision e66860cbda4a1880fa379bee17d6ab926e5cc58e)
11da177e4SLinus Torvalds /* Code to support devices on the DIO and DIO-II bus
21da177e4SLinus Torvalds  * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
31da177e4SLinus Torvalds  * Copyright (C) 2004 Jochen Friedrich <jochen@scram.de>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This code has basically these routines at the moment:
61da177e4SLinus Torvalds  * int dio_find(u_int deviceid)
71da177e4SLinus Torvalds  *    Search the list of DIO devices and return the select code
81da177e4SLinus Torvalds  *    of the next unconfigured device found that matches the given device ID.
91da177e4SLinus Torvalds  *    Note that the deviceid parameter should be the encoded ID.
101da177e4SLinus Torvalds  *    This means that framebuffers should pass it as
111da177e4SLinus Torvalds  *    DIO_ENCODE_ID(DIO_ID_FBUFFER,DIO_ID2_TOPCAT)
121da177e4SLinus Torvalds  *    (or whatever); everybody else just uses DIO_ID_FOOBAR.
131da177e4SLinus Torvalds  * unsigned long dio_scodetophysaddr(int scode)
141da177e4SLinus Torvalds  *    Return the physical address corresponding to the given select code.
151da177e4SLinus Torvalds  * int dio_scodetoipl(int scode)
161da177e4SLinus Torvalds  *    Every DIO card has a fixed interrupt priority level. This function
171da177e4SLinus Torvalds  *    returns it, whatever it is.
181da177e4SLinus Torvalds  * const char *dio_scodetoname(int scode)
191da177e4SLinus Torvalds  *    Return a character string describing this board [might be "" if
201da177e4SLinus Torvalds  *    not CONFIG_DIO_CONSTANTS]
211da177e4SLinus Torvalds  * void dio_config_board(int scode)     mark board as configured in the list
221da177e4SLinus Torvalds  * void dio_unconfig_board(int scode)   mark board as no longer configured
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * This file is based on the way the Amiga port handles Zorro II cards,
251da177e4SLinus Torvalds  * although we aren't so complicated...
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds #include <linux/module.h>
281da177e4SLinus Torvalds #include <linux/string.h>
291da177e4SLinus Torvalds #include <linux/types.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/init.h>
321da177e4SLinus Torvalds #include <linux/dio.h>
331da177e4SLinus Torvalds #include <linux/slab.h>                         /* kmalloc() */
341da177e4SLinus Torvalds #include <asm/uaccess.h>
351da177e4SLinus Torvalds #include <asm/io.h>                             /* readb() */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds struct dio_bus dio_bus = {
381da177e4SLinus Torvalds 	.resources = {
391da177e4SLinus Torvalds 		/* DIO range */
401da177e4SLinus Torvalds 		{ .name = "DIO mem", .start = 0x00600000, .end = 0x007fffff },
411da177e4SLinus Torvalds 		/* DIO-II range */
421da177e4SLinus Torvalds 		{ .name = "DIO-II mem", .start = 0x01000000, .end = 0x1fffffff }
431da177e4SLinus Torvalds 	},
441da177e4SLinus Torvalds 	.name = "DIO bus"
451da177e4SLinus Torvalds };
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /* not a real config option yet! */
481da177e4SLinus Torvalds #define CONFIG_DIO_CONSTANTS
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #ifdef CONFIG_DIO_CONSTANTS
511da177e4SLinus Torvalds /* We associate each numeric ID with an appropriate descriptive string
521da177e4SLinus Torvalds  * using a constant array of these structs.
531da177e4SLinus Torvalds  * FIXME: we should be able to arrange to throw away most of the strings
541da177e4SLinus Torvalds  * using the initdata stuff. Then we wouldn't need to worry about
551da177e4SLinus Torvalds  * carrying them around...
561da177e4SLinus Torvalds  * I think we do this by copying them into newly kmalloc()ed memory and
571da177e4SLinus Torvalds  * marking the names[] array as .initdata ?
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds struct dioname
601da177e4SLinus Torvalds {
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 
691da177e4SLinus Torvalds static struct dioname names[] =
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds         DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
721da177e4SLinus Torvalds         DIONAME(DCM), DIONAME(DCMREM),
731da177e4SLinus Torvalds         DIONAME(LAN),
741da177e4SLinus Torvalds         DIONAME(FHPIB), DIONAME(NHPIB),
751da177e4SLinus Torvalds         DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
761da177e4SLinus Torvalds         DIONAME(FBUFFER),
771da177e4SLinus Torvalds         DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
781da177e4SLinus Torvalds         DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
791da177e4SLinus Torvalds         DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
801da177e4SLinus Torvalds         DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
811da177e4SLinus Torvalds         DIONAME(MISC12), DIONAME(MISC13),
821da177e4SLinus Torvalds         DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
831da177e4SLinus Torvalds         DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
841da177e4SLinus Torvalds         DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
851da177e4SLinus Torvalds         DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #undef DIONAME
891da177e4SLinus Torvalds #undef DIOFBNAME
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #define NUMNAMES (sizeof(names) / sizeof(struct dioname))
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static const char *unknowndioname
941da177e4SLinus Torvalds         = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!";
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static const char *dio_getname(int id)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds         /* return pointer to a constant string describing the board with given ID */
991da177e4SLinus Torvalds 	unsigned int i;
1001da177e4SLinus Torvalds         for (i = 0; i < NUMNAMES; i++)
1011da177e4SLinus Torvalds                 if (names[i].id == id)
1021da177e4SLinus Torvalds                         return names[i].name;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds         return unknowndioname;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #else
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds static char dio_no_name[] = { 0 };
1101da177e4SLinus Torvalds #define dio_getname(_id)	(dio_no_name)
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #endif /* CONFIG_DIO_CONSTANTS */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds int __init dio_find(int deviceid)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	/* Called to find a DIO device before the full bus scan has run.
1171da177e4SLinus Torvalds 	 * Only used by the console driver.
1181da177e4SLinus Torvalds 	 */
1191da177e4SLinus Torvalds 	int scode, id;
1201da177e4SLinus Torvalds 	u_char prid, secid, i;
1211da177e4SLinus Torvalds 	mm_segment_t fs;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	for (scode = 0; scode < DIO_SCMAX; scode++) {
1241da177e4SLinus Torvalds 		void *va;
1251da177e4SLinus Torvalds 		unsigned long pa;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds                 if (DIO_SCINHOLE(scode))
1281da177e4SLinus Torvalds                         continue;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds                 pa = dio_scodetophysaddr(scode);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 		if (!pa)
1331da177e4SLinus Torvalds 			continue;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 		if (scode < DIOII_SCBASE)
1361da177e4SLinus Torvalds 			va = (void *)(pa + DIO_VIRADDRBASE);
1371da177e4SLinus Torvalds 		else
1381da177e4SLinus Torvalds 			va = ioremap(pa, PAGE_SIZE);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 		fs = get_fs();
1411da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
1441da177e4SLinus Torvalds 			set_fs(fs);
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 		set_fs(fs);
1511da177e4SLinus Torvalds 		prid = DIO_ID(va);
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds                 if (DIO_NEEDSSECID(prid)) {
1541da177e4SLinus Torvalds                         secid = DIO_SECID(va);
1551da177e4SLinus Torvalds                         id = DIO_ENCODE_ID(prid, secid);
1561da177e4SLinus Torvalds                 } else
1571da177e4SLinus Torvalds 			id = prid;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 		if (id == deviceid) {
1601da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
1611da177e4SLinus Torvalds 				iounmap(va);
1621da177e4SLinus Torvalds 			return scode;
1631da177e4SLinus Torvalds 		}
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	return -1;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /* This is the function that scans the DIO space and works out what
1701da177e4SLinus Torvalds  * hardware is actually present.
1711da177e4SLinus Torvalds  */
1721da177e4SLinus Torvalds static int __init dio_init(void)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	int scode;
1751da177e4SLinus Torvalds 	mm_segment_t fs;
1761da177e4SLinus Torvalds 	int i;
1771da177e4SLinus Torvalds 	struct dio_dev *dev;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	if (!MACH_IS_HP300)
1801da177e4SLinus Torvalds 		return 0;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds         printk(KERN_INFO "Scanning for DIO devices...\n");
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	/* Initialize the DIO bus */
1851da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dio_bus.devices);
1861da177e4SLinus Torvalds 	strcpy(dio_bus.dev.bus_id, "dio");
1871da177e4SLinus Torvalds 	device_register(&dio_bus.dev);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/* Request all resources */
1901da177e4SLinus Torvalds 	dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
1911da177e4SLinus Torvalds 	for (i = 0; i < dio_bus.num_resources; i++)
1921da177e4SLinus Torvalds 		request_resource(&iomem_resource, &dio_bus.resources[i]);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/* Register all devices */
1951da177e4SLinus Torvalds         for (scode = 0; scode < DIO_SCMAX; ++scode)
1961da177e4SLinus Torvalds         {
1971da177e4SLinus Torvalds                 u_char prid, secid = 0;        /* primary, secondary ID bytes */
1981da177e4SLinus Torvalds                 u_char *va;
1991da177e4SLinus Torvalds 		unsigned long pa;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds                 if (DIO_SCINHOLE(scode))
2021da177e4SLinus Torvalds                         continue;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 		pa = dio_scodetophysaddr(scode);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 		if (!pa)
2071da177e4SLinus Torvalds 			continue;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 		if (scode < DIOII_SCBASE)
2101da177e4SLinus Torvalds 			va = (void *)(pa + DIO_VIRADDRBASE);
2111da177e4SLinus Torvalds 		else
2121da177e4SLinus Torvalds 			va = ioremap(pa, PAGE_SIZE);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 		fs = get_fs();
2151da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
2181da177e4SLinus Torvalds 			set_fs(fs);
2191da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
2201da177e4SLinus Torvalds 				iounmap(va);
2211da177e4SLinus Torvalds                         continue;              /* no board present at that select code */
2221da177e4SLinus Torvalds 		}
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 		set_fs(fs);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds                 /* Found a board, allocate it an entry in the list */
227*e66860cbSDeepak Saxena 		dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
2281da177e4SLinus Torvalds 		if (!dev)
2291da177e4SLinus Torvalds 			return 0;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		dev->bus = &dio_bus;
2321da177e4SLinus Torvalds 		dev->dev.parent = &dio_bus.dev;
2331da177e4SLinus Torvalds 		dev->dev.bus = &dio_bus_type;
2341da177e4SLinus Torvalds 		dev->scode = scode;
2351da177e4SLinus Torvalds 		dev->resource.start = pa;
2361da177e4SLinus Torvalds 		dev->resource.end = pa + DIO_SIZE(scode, va);
2371da177e4SLinus Torvalds 		sprintf(dev->dev.bus_id,"%02x", scode);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds                 /* read the ID byte(s) and encode if necessary. */
2401da177e4SLinus Torvalds 		prid = DIO_ID(va);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds                 if (DIO_NEEDSSECID(prid)) {
2431da177e4SLinus Torvalds                         secid = DIO_SECID(va);
2441da177e4SLinus Torvalds                         dev->id = DIO_ENCODE_ID(prid, secid);
2451da177e4SLinus Torvalds                 } else
2461da177e4SLinus Torvalds                         dev->id = prid;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds                 dev->ipl = DIO_IPL(va);
2491da177e4SLinus Torvalds                 strcpy(dev->name,dio_getname(dev->id));
2501da177e4SLinus Torvalds                 printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
2511da177e4SLinus Torvalds                 if (DIO_NEEDSSECID(prid))
2521da177e4SLinus Torvalds                         printk(":%02X", secid);
2531da177e4SLinus Torvalds                 printk(": %s\n", dev->name);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		if (scode >= DIOII_SCBASE)
2561da177e4SLinus Torvalds 			iounmap(va);
2571da177e4SLinus Torvalds 		device_register(&dev->dev);
2581da177e4SLinus Torvalds 		dio_create_sysfs_dev_files(dev);
2591da177e4SLinus Torvalds         }
2601da177e4SLinus Torvalds 	return 0;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds subsys_initcall(dio_init);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds /* Bear in mind that this is called in the very early stages of initialisation
2661da177e4SLinus Torvalds  * in order to get the address of the serial port for the console...
2671da177e4SLinus Torvalds  */
2681da177e4SLinus Torvalds unsigned long dio_scodetophysaddr(int scode)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds         if (scode >= DIOII_SCBASE) {
2711da177e4SLinus Torvalds                 return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
2721da177e4SLinus Torvalds         } else if (scode > DIO_SCMAX || scode < 0)
2731da177e4SLinus Torvalds                 return 0;
2741da177e4SLinus Torvalds         else if (DIO_SCINHOLE(scode))
2751da177e4SLinus Torvalds                 return 0;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds         return (DIO_BASE + scode * DIO_DEVSIZE);
2781da177e4SLinus Torvalds }
279