1/* Code to support devices on the DIO and DIO-II bus
2 * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
3 * Copyright (C) 2004 Jochen Friedrich <jochen@scram.de>
4 *
5 * This code has basically these routines at the moment:
6 * int dio_find(u_int deviceid)
7 *    Search the list of DIO devices and return the select code
8 *    of the next unconfigured device found that matches the given device ID.
9 *    Note that the deviceid parameter should be the encoded ID.
10 *    This means that framebuffers should pass it as
11 *    DIO_ENCODE_ID(DIO_ID_FBUFFER,DIO_ID2_TOPCAT)
12 *    (or whatever); everybody else just uses DIO_ID_FOOBAR.
13 * unsigned long dio_scodetophysaddr(int scode)
14 *    Return the physical address corresponding to the given select code.
15 * int dio_scodetoipl(int scode)
16 *    Every DIO card has a fixed interrupt priority level. This function
17 *    returns it, whatever it is.
18 * const char *dio_scodetoname(int scode)
19 *    Return a character string describing this board [might be "" if
20 *    not CONFIG_DIO_CONSTANTS]
21 * void dio_config_board(int scode)     mark board as configured in the list
22 * void dio_unconfig_board(int scode)   mark board as no longer configured
23 *
24 * This file is based on the way the Amiga port handles Zorro II cards,
25 * although we aren't so complicated...
26 */
27#include <linux/module.h>
28#include <linux/string.h>
29#include <linux/types.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/dio.h>
33#include <linux/slab.h>                         /* kmalloc() */
34#include <asm/uaccess.h>
35#include <asm/io.h>                             /* readb() */
36
37struct dio_bus dio_bus = {
38	.resources = {
39		/* DIO range */
40		{ .name = "DIO mem", .start = 0x00600000, .end = 0x007fffff },
41		/* DIO-II range */
42		{ .name = "DIO-II mem", .start = 0x01000000, .end = 0x1fffffff }
43	},
44	.name = "DIO bus"
45};
46
47/* not a real config option yet! */
48#define CONFIG_DIO_CONSTANTS
49
50#ifdef CONFIG_DIO_CONSTANTS
51struct dioname
52{
53        int id;
54        const char *name;
55};
56
57/* useful macro */
58#define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x }
59#define DIOFBNAME(x) { DIO_ENCODE_ID( DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x }
60
61static struct dioname names[] =
62{
63        DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
64        DIONAME(DCM), DIONAME(DCMREM),
65        DIONAME(LAN),
66        DIONAME(FHPIB), DIONAME(NHPIB),
67        DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
68        DIONAME(FBUFFER),
69        DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
70        DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
71        DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
72        DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
73        DIONAME(MISC12), DIONAME(MISC13),
74        DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
75        DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
76        DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
77        DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
78};
79
80#undef DIONAME
81#undef DIOFBNAME
82
83#define NUMNAMES (sizeof(names) / sizeof(struct dioname))
84
85static const char *unknowndioname
86        = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!";
87
88static const char *dio_getname(int id)
89{
90        /* return pointer to a constant string describing the board with given ID */
91	unsigned int i;
92        for (i = 0; i < NUMNAMES; i++)
93                if (names[i].id == id)
94                        return names[i].name;
95
96        return unknowndioname;
97}
98
99#else
100
101static char dio_no_name[] = { 0 };
102#define dio_getname(_id)	(dio_no_name)
103
104#endif /* CONFIG_DIO_CONSTANTS */
105
106int __init dio_find(int deviceid)
107{
108	/* Called to find a DIO device before the full bus scan has run.
109	 * Only used by the console driver.
110	 */
111	int scode, id;
112	u_char prid, secid, i;
113	mm_segment_t fs;
114
115	for (scode = 0; scode < DIO_SCMAX; scode++) {
116		void *va;
117		unsigned long pa;
118
119                if (DIO_SCINHOLE(scode))
120                        continue;
121
122                pa = dio_scodetophysaddr(scode);
123
124		if (!pa)
125			continue;
126
127		if (scode < DIOII_SCBASE)
128			va = (void *)(pa + DIO_VIRADDRBASE);
129		else
130			va = ioremap(pa, PAGE_SIZE);
131
132		fs = get_fs();
133		set_fs(KERNEL_DS);
134
135                if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
136			set_fs(fs);
137			if (scode >= DIOII_SCBASE)
138				iounmap(va);
139                        continue;             /* no board present at that select code */
140		}
141
142		set_fs(fs);
143		prid = DIO_ID(va);
144
145                if (DIO_NEEDSSECID(prid)) {
146                        secid = DIO_SECID(va);
147                        id = DIO_ENCODE_ID(prid, secid);
148                } else
149			id = prid;
150
151		if (id == deviceid) {
152			if (scode >= DIOII_SCBASE)
153				iounmap(va);
154			return scode;
155		}
156	}
157
158	return -1;
159}
160
161/* This is the function that scans the DIO space and works out what
162 * hardware is actually present.
163 */
164static int __init dio_init(void)
165{
166	int scode;
167	mm_segment_t fs;
168	int i;
169	struct dio_dev *dev;
170
171	if (!MACH_IS_HP300)
172		return 0;
173
174        printk(KERN_INFO "Scanning for DIO devices...\n");
175
176	/* Initialize the DIO bus */
177	INIT_LIST_HEAD(&dio_bus.devices);
178	strcpy(dio_bus.dev.bus_id, "dio");
179	device_register(&dio_bus.dev);
180
181	/* Request all resources */
182	dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
183	for (i = 0; i < dio_bus.num_resources; i++)
184		request_resource(&iomem_resource, &dio_bus.resources[i]);
185
186	/* Register all devices */
187        for (scode = 0; scode < DIO_SCMAX; ++scode)
188        {
189                u_char prid, secid = 0;        /* primary, secondary ID bytes */
190                u_char *va;
191		unsigned long pa;
192
193                if (DIO_SCINHOLE(scode))
194                        continue;
195
196		pa = dio_scodetophysaddr(scode);
197
198		if (!pa)
199			continue;
200
201		if (scode < DIOII_SCBASE)
202			va = (void *)(pa + DIO_VIRADDRBASE);
203		else
204			va = ioremap(pa, PAGE_SIZE);
205
206		fs = get_fs();
207		set_fs(KERNEL_DS);
208
209                if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
210			set_fs(fs);
211			if (scode >= DIOII_SCBASE)
212				iounmap(va);
213                        continue;              /* no board present at that select code */
214		}
215
216		set_fs(fs);
217
218                /* Found a board, allocate it an entry in the list */
219		dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
220		if (!dev)
221			return 0;
222
223		dev->bus = &dio_bus;
224		dev->dev.parent = &dio_bus.dev;
225		dev->dev.bus = &dio_bus_type;
226		dev->scode = scode;
227		dev->resource.start = pa;
228		dev->resource.end = pa + DIO_SIZE(scode, va);
229		sprintf(dev->dev.bus_id,"%02x", scode);
230
231                /* read the ID byte(s) and encode if necessary. */
232		prid = DIO_ID(va);
233
234                if (DIO_NEEDSSECID(prid)) {
235                        secid = DIO_SECID(va);
236                        dev->id = DIO_ENCODE_ID(prid, secid);
237                } else
238                        dev->id = prid;
239
240                dev->ipl = DIO_IPL(va);
241                strcpy(dev->name,dio_getname(dev->id));
242                printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
243                if (DIO_NEEDSSECID(prid))
244                        printk(":%02X", secid);
245                printk(": %s\n", dev->name);
246
247		if (scode >= DIOII_SCBASE)
248			iounmap(va);
249		device_register(&dev->dev);
250		dio_create_sysfs_dev_files(dev);
251        }
252	return 0;
253}
254
255subsys_initcall(dio_init);
256
257/* Bear in mind that this is called in the very early stages of initialisation
258 * in order to get the address of the serial port for the console...
259 */
260unsigned long dio_scodetophysaddr(int scode)
261{
262        if (scode >= DIOII_SCBASE) {
263                return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
264        } else if (scode > DIO_SCMAX || scode < 0)
265                return 0;
266        else if (DIO_SCINHOLE(scode))
267                return 0;
268
269        return (DIO_BASE + scode * DIO_DEVSIZE);
270}
271