ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / dio / dio.c
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>                               /* readb() */
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
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
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  */
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  */
171 static int __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         return 0;
227 }
228
229 subsys_initcall(dio_init);
230
231 /* Bear in mind that this is called in the very early stages of initialisation
232  * in order to get the virtual address of the serial port for the console...
233  */
234 void *dio_scodetoviraddr(int scode)
235 {
236         if (scode > DIOII_SCBASE)
237         {
238                 printk("dio_scodetoviraddr: don't support DIO-II yet!\n");
239                 return 0;
240         }
241         else if (scode > DIO_SCMAX || scode < 0)
242                 return 0;
243         else if (DIO_SCINHOLE(scode))
244                 return 0;
245         else if (DIO_ISIHPIB(scode))
246                 return (void*)DIO_IHPIBADDR;
247
248         return (void*)(DIO_VIRADDRBASE + DIO_BASE + scode * 0x10000);
249 }
250
251 int dio_scodetoipl(int scode)
252 {
253         struct dioboard *b;
254         for (b = blist; b; b = b->next)
255                 if (b->scode == scode) 
256                         break;
257         
258         if (!b)
259         {
260                 printk("dio_scodetoipl: bad select code %d\n", scode);
261                 return 0;
262         }
263         else
264                 return b->ipl;
265 }
266
267 const char *dio_scodetoname(int scode)
268 {
269         struct dioboard *b;
270         for (b = blist; b; b = b->next)
271                 if (b->scode == scode) 
272                         break;
273         
274         if (!b)
275         {
276                 printk("dio_scodetoname: bad select code %d\n", scode);
277                 return NULL;
278         }
279         else
280                 return b->name;
281 }
282
283 void dio_config_board(int scode)
284 {
285         struct dioboard *b;
286         for (b = blist; b; b = b->next)
287                 if (b->scode == scode)
288                         break;
289    
290         if (!b) 
291                 printk("dio_config_board: bad select code %d\n", scode);
292         else if (b->configured)
293                 printk("dio_config_board: board at select code %d already configured\n", scode);
294         else
295                 b->configured = 1;
296 }
297
298 void dio_unconfig_board(int scode)
299 {
300         struct dioboard *b;
301         for (b = blist; b; b = b->next)
302                 if (b->scode == scode) 
303                         break;
304    
305         if (!b) 
306                 printk("dio_unconfig_board: bad select code %d\n", scode);
307         else if (!b->configured)
308                 printk("dio_unconfig_board: board at select code %d not configured\n", 
309                        scode);
310         else 
311                 b->configured = 0;
312 }