1 /* Code to support devices on the DIO (and eventually DIO-II) bus
2  * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
3  *
4  * This code has basically these routines at the moment:
5  * int dio_find(u_int deviceid)
6  *    Search the list of DIO devices and return the select code
7  *    of the next unconfigured device found that matches the given device ID.
8  *    Note that the deviceid parameter should be the encoded ID.
9  *    This means that framebuffers should pass it as
10  *    DIO_ENCODE_ID(DIO_ID_FBUFFER,DIO_ID2_TOPCAT)
11  *    (or whatever); everybody else just uses DIO_ID_FOOBAR.
12  * void *dio_scodetoviraddr(int scode)
13  *    Return the virtual address corresponding to the given select code.
14  *    NB: DIO-II devices will have to be mapped in in this routine!
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/config.h>
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/dio.h>
31 #include <linux/slab.h>                         /* kmalloc() */
32 #include <linux/init.h>
33 #include <asm/hwtest.h>                           /* hwreg_present() */
34 #include <asm/io.h>
35 /* not a real config option yet! */
36 #define CONFIG_DIO_CONSTANTS
37 
38 #ifdef CONFIG_DIO_CONSTANTS
39 /* We associate each numeric ID with an appropriate descriptive string
40  * using a constant array of these structs.
41  * FIXME: we should be able to arrange to throw away most of the strings
42  * using the initdata stuff. Then we wouldn't need to worry about
43  * carrying them around...
44  * I think we do this by copying them into newly kmalloc()ed memory and
45  * marking the names[] array as .initdata ?
46  */
47 struct dioname
48 {
49         int id;
50         const char *name;
51 };
52 
53 /* useful macro */
54 #define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x }
55 #define DIOFBNAME(x) { DIO_ENCODE_ID( DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x }
56 
57 static struct dioname names[] =
58 {
59         DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
60         DIONAME(DCM), DIONAME(DCMREM),
61         DIONAME(LAN),
62         DIONAME(FHPIB), DIONAME(NHPIB), DIONAME(IHPIB),
63         DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
64         DIONAME(FBUFFER),
65         DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
66         DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
67         DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
68         DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
69         DIONAME(MISC12), DIONAME(MISC13),
70         DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
71         DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
72         DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
73         DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
74 };
75 
76 #undef DIONAME
77 #undef DIOFBNAME
78 
79 #define NUMNAMES (sizeof(names) / sizeof(struct dioname))
80 
81 static const char *unknowndioname
82         = "unknown DIO board -- please email <pmaydell@chiark.greenend.org.uk>!";
83 
dio_getname(int id)84 static const char *dio_getname(int id)
85 {
86         /* return pointer to a constant string describing the board with given ID */
87 	unsigned int i;
88         for (i = 0; i < NUMNAMES; i++)
89                 if (names[i].id == id)
90                         return names[i].name;
91 
92         return unknowndioname;
93 }
94 
95 #else
96 
97 static char dio_no_name[] = { 0 };
98 #define dio_getname(_id)	(dio_no_name)
99 
100 #endif /* CONFIG_DIO_CONSTANTS */
101 
102 /* We represent all the DIO boards in the system with a linked list of these structs. */
103 struct dioboard
104 {
105         struct dioboard *next;                    /* link to next struct in list */
106         int ipl;                                  /* IPL of this board */
107         int configured;                           /* has this board been configured? */
108         int scode;                                /* select code of this board */
109         int id;                                   /* encoded ID */
110         const char *name;
111 };
112 
113 static struct dioboard *blist = NULL;
114 
dio_find_slow(int deviceid)115 static int __init dio_find_slow(int deviceid)
116 {
117 	/* Called to find a DIO device before the full bus scan has run.  Basically
118          * only used by the console driver.
119          * We don't do the primary+secondary ID encoding thing here. Maybe we should.
120          * (that would break the topcat detection, though. I need to think about
121          * the whole primary/secondary ID thing.)
122          */
123 	int scode;
124         u_char prid;
125 
126 	for (scode = 0; scode < DIO_SCMAX; scode++)
127 	{
128 		void *va;
129 
130                 if (DIO_SCINHOLE(scode))
131                         continue;
132 
133                 va = dio_scodetoviraddr(scode);
134                 if (!va || !hwreg_present(va + DIO_IDOFF))
135                         continue;             /* no board present at that select code */
136 
137                 /* We aren't very likely to want to use this to get at the IHPIB,
138                  * but maybe it's returning the same ID as the card we do want...
139                  */
140                 if (!DIO_ISIHPIB(scode))
141                         prid = DIO_ID(va);
142                 else
143                         prid = DIO_ID_IHPIB;
144 
145 		if (prid == deviceid)
146 			return scode;
147 	}
148 	return 0;
149 }
150 
151 /* Aargh: we use 0 for an error return code, but select code 0 exists!
152  * FIXME (trivial, use -1, but requires changes to all the drivers :-< )
153  */
dio_find(int deviceid)154 int dio_find(int deviceid)
155 {
156 	if (blist)
157 	{
158 		/* fast way */
159 		struct dioboard *b;
160 		for (b = blist; b; b = b->next)
161 			if (b->id == deviceid && b->configured == 0)
162 				return b->scode;
163 		return 0;
164 	}
165 	return dio_find_slow(deviceid);
166 }
167 
168 /* This is the function that scans the DIO space and works out what
169  * hardware is actually present.
170  */
dio_init(void)171 void __init dio_init(void)
172 {
173         int scode;
174         struct dioboard *b, *bprev = NULL;
175 
176         printk("Scanning for DIO devices...\n");
177 
178         for (scode = 0; scode < DIO_SCMAX; ++scode)
179         {
180                 u_char prid, secid = 0;        /* primary, secondary ID bytes */
181                 u_char *va;
182 
183                 if (DIO_SCINHOLE(scode))
184                         continue;
185 
186                 va = dio_scodetoviraddr(scode);
187                 if (!va || !hwreg_present(va + DIO_IDOFF))
188                         continue;              /* no board present at that select code */
189 
190                 /* Found a board, allocate it an entry in the list */
191                 b = kmalloc(sizeof(struct dioboard), GFP_KERNEL);
192 
193                 /* read the ID byte(s) and encode if necessary. Note workaround
194                  * for broken internal HPIB devices...
195                  */
196                 if (!DIO_ISIHPIB(scode))
197                         prid = DIO_ID(va);
198                 else
199                         prid = DIO_ID_IHPIB;
200 
201                 if (DIO_NEEDSSECID(prid))
202                 {
203                         secid = DIO_SECID(va);
204                         b->id = DIO_ENCODE_ID(prid, secid);
205                 }
206                 else
207                         b->id = prid;
208 
209                 b->configured = 0;
210                 b->scode = scode;
211                 b->ipl = DIO_IPL(va);
212                 b->name = dio_getname(b->id);
213                 printk("select code %3d: ipl %d: ID %02X", scode, b->ipl, prid);
214                 if (DIO_NEEDSSECID(b->id))
215                         printk(":%02X", secid);
216                 printk(": %s\n", b->name);
217 
218                 b->next = NULL;
219 
220                 if (bprev)
221                         bprev->next = b;
222                 else
223                         blist = b;
224                 bprev = b;
225         }
226 }
227 
228 /* Bear in mind that this is called in the very early stages of initialisation
229  * in order to get the virtual address of the serial port for the console...
230  */
dio_scodetoviraddr(int scode)231 void *dio_scodetoviraddr(int scode)
232 {
233         if (scode > DIOII_SCBASE)
234         {
235                 printk("dio_scodetoviraddr: don't support DIO-II yet!\n");
236                 return 0;
237         }
238         else if (scode > DIO_SCMAX || scode < 0)
239                 return 0;
240         else if (DIO_SCINHOLE(scode))
241                 return 0;
242         else if (DIO_ISIHPIB(scode))
243                 return (void*)DIO_IHPIBADDR;
244 
245         return (void*)(DIO_VIRADDRBASE + DIO_BASE + scode * 0x10000);
246 }
247 
dio_scodetoipl(int scode)248 int dio_scodetoipl(int scode)
249 {
250         struct dioboard *b;
251         for (b = blist; b; b = b->next)
252                 if (b->scode == scode)
253                         break;
254 
255         if (!b)
256         {
257                 printk("dio_scodetoipl: bad select code %d\n", scode);
258                 return 0;
259         }
260         else
261                 return b->ipl;
262 }
263 
dio_scodetoname(int scode)264 const char *dio_scodetoname(int scode)
265 {
266         struct dioboard *b;
267         for (b = blist; b; b = b->next)
268                 if (b->scode == scode)
269                         break;
270 
271         if (!b)
272         {
273                 printk("dio_scodetoname: bad select code %d\n", scode);
274                 return NULL;
275         }
276         else
277                 return b->name;
278 }
279 
dio_config_board(int scode)280 void dio_config_board(int scode)
281 {
282         struct dioboard *b;
283         for (b = blist; b; b = b->next)
284                 if (b->scode == scode)
285                         break;
286 
287         if (!b)
288                 printk("dio_config_board: bad select code %d\n", scode);
289         else if (b->configured)
290                 printk("dio_config_board: board at select code %d already configured\n", scode);
291         else
292                 b->configured = 1;
293 }
294 
dio_unconfig_board(int scode)295 void dio_unconfig_board(int scode)
296 {
297         struct dioboard *b;
298         for (b = blist; b; b = b->next)
299                 if (b->scode == scode)
300                         break;
301 
302         if (!b)
303                 printk("dio_unconfig_board: bad select code %d\n", scode);
304         else if (!b->configured)
305                 printk("dio_unconfig_board: board at select code %d not configured\n",
306 		       scode);
307         else
308                 b->configured = 0;
309 }
310