GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / parisc / eisa_enumerator.c
blob6c01d3bfcb42bbc20ea58dbc7ac8ebedc65f3989
1 /*
2 * eisa_enumerator.c - provide support for EISA adapters in PA-RISC machines
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Copyright (c) 2002 Daniel Engstrom <5116@telia.com>
13 #include <linux/ioport.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <asm/io.h>
18 #include <asm/uaccess.h>
19 #include <asm/byteorder.h>
21 #include <asm/eisa_bus.h>
22 #include <asm/eisa_eeprom.h>
26 * Todo:
28 * PORT init with MASK attr and other size than byte
29 * MEMORY with other decode than 20 bit
30 * CRC stuff
31 * FREEFORM stuff
34 #define EPI 0xc80
35 #define NUM_SLOT 16
36 #define SLOT2PORT(x) (x<<12)
39 /* macros to handle unaligned accesses and
40 * byte swapping. The data in the EEPROM is
41 * little-endian on the big-endian PAROSC */
42 #define get_8(x) (*(u_int8_t*)(x))
44 static inline u_int16_t get_16(const unsigned char *x)
46 return (x[1] << 8) | x[0];
49 static inline u_int32_t get_32(const unsigned char *x)
51 return (x[3] << 24) | (x[2] << 16) | (x[1] << 8) | x[0];
54 static inline u_int32_t get_24(const unsigned char *x)
56 return (x[2] << 24) | (x[1] << 16) | (x[0] << 8);
59 static void print_eisa_id(char *s, u_int32_t id)
61 char vendor[4];
62 int rev;
63 int device;
65 rev = id & 0xff;
66 id >>= 8;
67 device = id & 0xff;
68 id >>= 8;
69 vendor[3] = '\0';
70 vendor[2] = '@' + (id & 0x1f);
71 id >>= 5;
72 vendor[1] = '@' + (id & 0x1f);
73 id >>= 5;
74 vendor[0] = '@' + (id & 0x1f);
75 id >>= 5;
77 sprintf(s, "%s%02X%02X", vendor, device, rev);
80 static int configure_memory(const unsigned char *buf,
81 struct resource *mem_parent,
82 char *name)
84 int len;
85 u_int8_t c;
86 int i;
87 struct resource *res;
89 len=0;
91 for (i=0;i<HPEE_MEMORY_MAX_ENT;i++) {
92 c = get_8(buf+len);
94 if (NULL != (res = kmalloc(sizeof(struct resource), GFP_KERNEL))) {
95 int result;
97 res->name = name;
98 res->start = mem_parent->start + get_24(buf+len+2);
99 res->end = res->start + get_16(buf+len+5)*1024;
100 res->flags = IORESOURCE_MEM;
101 printk("memory %lx-%lx ", (unsigned long)res->start, (unsigned long)res->end);
102 result = request_resource(mem_parent, res);
103 if (result < 0) {
104 printk(KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
105 return result;
109 len+=7;
111 if (!(c & HPEE_MEMORY_MORE)) {
112 break;
116 return len;
120 static int configure_irq(const unsigned char *buf)
122 int len;
123 u_int8_t c;
124 int i;
126 len=0;
128 for (i=0;i<HPEE_IRQ_MAX_ENT;i++) {
129 c = get_8(buf+len);
131 printk("IRQ %d ", c & HPEE_IRQ_CHANNEL_MASK);
132 if (c & HPEE_IRQ_TRIG_LEVEL) {
133 eisa_make_irq_level(c & HPEE_IRQ_CHANNEL_MASK);
134 } else {
135 eisa_make_irq_edge(c & HPEE_IRQ_CHANNEL_MASK);
138 len+=2;
139 /* hpux seems to allow for
140 * two bytes of irq data but only defines one of
141 * them, I think */
142 if (!(c & HPEE_IRQ_MORE)) {
143 break;
147 return len;
151 static int configure_dma(const unsigned char *buf)
153 int len;
154 u_int8_t c;
155 int i;
157 len=0;
159 for (i=0;i<HPEE_DMA_MAX_ENT;i++) {
160 c = get_8(buf+len);
161 printk("DMA %d ", c&HPEE_DMA_CHANNEL_MASK);
162 len+=2;
163 if (!(c & HPEE_DMA_MORE)) {
164 break;
168 return len;
171 static int configure_port(const unsigned char *buf, struct resource *io_parent,
172 char *board)
174 int len;
175 u_int8_t c;
176 int i;
177 struct resource *res;
178 int result;
180 len=0;
182 for (i=0;i<HPEE_PORT_MAX_ENT;i++) {
183 c = get_8(buf+len);
185 if (NULL != (res = kmalloc(sizeof(struct resource), GFP_KERNEL))) {
186 res->name = board;
187 res->start = get_16(buf+len+1);
188 res->end = get_16(buf+len+1)+(c&HPEE_PORT_SIZE_MASK)+1;
189 res->flags = IORESOURCE_IO;
190 printk("ioports %lx-%lx ", (unsigned long)res->start, (unsigned long)res->end);
191 result = request_resource(io_parent, res);
192 if (result < 0) {
193 printk(KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
194 return result;
198 len+=3;
199 if (!(c & HPEE_PORT_MORE)) {
200 break;
204 return len;
208 /* byte 1 and 2 is the port number to write
209 * and at byte 3 the value to write starts.
210 * I assume that there are and- and or- masks
211 * here when HPEE_PORT_INIT_MASK is set but I have
212 * not yet encountered this. */
213 static int configure_port_init(const unsigned char *buf)
215 int len=0;
216 u_int8_t c;
218 while (len<HPEE_PORT_INIT_MAX_LEN) {
219 int s=0;
220 c = get_8(buf+len);
222 switch (c & HPEE_PORT_INIT_WIDTH_MASK) {
223 case HPEE_PORT_INIT_WIDTH_BYTE:
224 s=1;
225 if (c & HPEE_PORT_INIT_MASK) {
226 printk(KERN_WARNING "port_init: unverified mask attribute\n");
227 outb((inb(get_16(buf+len+1) &
228 get_8(buf+len+3)) |
229 get_8(buf+len+4)), get_16(buf+len+1));
231 } else {
232 outb(get_8(buf+len+3), get_16(buf+len+1));
235 break;
236 case HPEE_PORT_INIT_WIDTH_WORD:
237 s=2;
238 if (c & HPEE_PORT_INIT_MASK) {
239 printk(KERN_WARNING "port_init: unverified mask attribute\n");
240 outw((inw(get_16(buf+len+1)) &
241 get_16(buf+len+3)) |
242 get_16(buf+len+5),
243 get_16(buf+len+1));
244 } else {
245 outw(cpu_to_le16(get_16(buf+len+3)), get_16(buf+len+1));
247 break;
248 case HPEE_PORT_INIT_WIDTH_DWORD:
249 s=4;
250 if (c & HPEE_PORT_INIT_MASK) {
251 printk(KERN_WARNING "port_init: unverified mask attribute\n");
252 outl((inl(get_16(buf+len+1) &
253 get_32(buf+len+3)) |
254 get_32(buf+len+7)), get_16(buf+len+1));
255 } else {
256 outl(cpu_to_le32(get_32(buf+len+3)), get_16(buf+len+1));
259 break;
260 default:
261 printk(KERN_ERR "Invalid port init word %02x\n", c);
262 return 0;
265 if (c & HPEE_PORT_INIT_MASK) {
266 s*=2;
269 len+=s+3;
270 if (!(c & HPEE_PORT_INIT_MORE)) {
271 break;
275 return len;
278 static int configure_choise(const unsigned char *buf, u_int8_t *info)
280 int len;
282 /* theis record contain the value of the functions
283 * configuration choises and an info byte which
284 * describes which other records to expect in this
285 * function */
286 len = get_8(buf);
287 *info=get_8(buf+len+1);
289 return len+2;
292 static int configure_type_string(const unsigned char *buf)
294 int len;
296 /* just skip past the type field */
297 len = get_8(buf);
298 if (len > 80) {
299 printk(KERN_ERR "eisa_enumerator: type info field too long (%d, max is 80)\n", len);
302 return 1+len;
305 static int configure_function(const unsigned char *buf, int *more)
307 /* the init field seems to be a two-byte field
308 * which is non-zero if there are an other function following
309 * I think it is the length of the function def
311 *more = get_16(buf);
313 return 2;
316 static int parse_slot_config(int slot,
317 const unsigned char *buf,
318 struct eeprom_eisa_slot_info *es,
319 struct resource *io_parent,
320 struct resource *mem_parent)
322 int res=0;
323 int function_len;
324 unsigned int pos=0;
325 unsigned int maxlen;
326 int num_func=0;
327 u_int8_t flags;
328 int p0;
330 char *board;
331 int id_string_used=0;
333 if (NULL == (board = kmalloc(8, GFP_KERNEL))) {
334 return -1;
336 print_eisa_id(board, es->eisa_slot_id);
337 printk(KERN_INFO "EISA slot %d: %s %s ",
338 slot, board, es->flags&HPEE_FLAG_BOARD_IS_ISA ? "ISA" : "EISA");
340 maxlen = es->config_data_length < HPEE_MAX_LENGTH ?
341 es->config_data_length : HPEE_MAX_LENGTH;
342 while ((pos < maxlen) && (num_func <= es->num_functions)) {
343 pos+=configure_function(buf+pos, &function_len);
345 if (!function_len) {
346 break;
348 num_func++;
349 p0 = pos;
350 pos += configure_choise(buf+pos, &flags);
352 if (flags & HPEE_FUNCTION_INFO_F_DISABLED) {
353 /* function disabled, skip silently */
354 pos = p0 + function_len;
355 continue;
357 if (flags & HPEE_FUNCTION_INFO_CFG_FREE_FORM) {
358 /* I have no idea how to handle this */
359 printk("function %d have free-form confgiuration, skipping ",
360 num_func);
361 pos = p0 + function_len;
362 continue;
365 /* the ordering of the sections need
366 * more investigation.
367 * Currently I think that memory comaed before IRQ
368 * I assume the order is LSB to MSB in the
369 * info flags
370 * eg type, memory, irq, dma, port, HPEE_PORT_init
373 if (flags & HPEE_FUNCTION_INFO_HAVE_TYPE) {
374 pos += configure_type_string(buf+pos);
377 if (flags & HPEE_FUNCTION_INFO_HAVE_MEMORY) {
378 id_string_used=1;
379 pos += configure_memory(buf+pos, mem_parent, board);
382 if (flags & HPEE_FUNCTION_INFO_HAVE_IRQ) {
383 pos += configure_irq(buf+pos);
386 if (flags & HPEE_FUNCTION_INFO_HAVE_DMA) {
387 pos += configure_dma(buf+pos);
390 if (flags & HPEE_FUNCTION_INFO_HAVE_PORT) {
391 id_string_used=1;
392 pos += configure_port(buf+pos, io_parent, board);
395 if (flags & HPEE_FUNCTION_INFO_HAVE_PORT_INIT) {
396 pos += configure_port_init(buf+pos);
399 if (p0 + function_len < pos) {
400 printk(KERN_ERR "eisa_enumerator: function %d length mis-match "
401 "got %d, expected %d\n",
402 num_func, pos-p0, function_len);
403 res=-1;
404 break;
406 pos = p0 + function_len;
408 printk("\n");
409 if (!id_string_used) {
410 kfree(board);
413 if (pos != es->config_data_length) {
414 printk(KERN_ERR "eisa_enumerator: config data length mis-match got %d, expected %d\n",
415 pos, es->config_data_length);
416 res=-1;
419 if (num_func != es->num_functions) {
420 printk(KERN_ERR "eisa_enumerator: number of functions mis-match got %d, expected %d\n",
421 num_func, es->num_functions);
422 res=-2;
425 return res;
429 static int init_slot(int slot, struct eeprom_eisa_slot_info *es)
431 unsigned int id;
433 char id_string[8];
435 if (!(es->slot_info&HPEE_SLOT_INFO_NO_READID)) {
436 /* try to read the id of the board in the slot */
437 id = le32_to_cpu(inl(SLOT2PORT(slot)+EPI));
439 if (0xffffffff == id) {
440 /* Maybe we didn't expect a card to be here... */
441 if (es->eisa_slot_id == 0xffffffff)
442 return -1;
444 /* this board is not here or it does not
445 * support readid
447 printk(KERN_ERR "EISA slot %d a configured board was not detected (",
448 slot);
450 print_eisa_id(id_string, es->eisa_slot_id);
451 printk(" expected %s)\n", id_string);
453 return -1;
456 if (es->eisa_slot_id != id) {
457 print_eisa_id(id_string, id);
458 printk(KERN_ERR "EISA slot %d id mis-match: got %s",
459 slot, id_string);
461 print_eisa_id(id_string, es->eisa_slot_id);
462 printk(" expected %s\n", id_string);
464 return -1;
469 /* now: we need to enable the board if
470 * it supports enabling and run through
471 * the port init sction if present
472 * and finally record any interrupt polarity
474 if (es->slot_features & HPEE_SLOT_FEATURES_ENABLE) {
475 /* enable board */
476 outb(0x01| inb(SLOT2PORT(slot)+EPI+4),
477 SLOT2PORT(slot)+EPI+4);
480 return 0;
484 int eisa_enumerator(unsigned long eeprom_addr,
485 struct resource *io_parent, struct resource *mem_parent)
487 int i;
488 struct eeprom_header *eh;
489 static char eeprom_buf[HPEE_MAX_LENGTH];
491 for (i=0; i < HPEE_MAX_LENGTH; i++) {
492 eeprom_buf[i] = gsc_readb(eeprom_addr+i);
495 printk(KERN_INFO "Enumerating EISA bus\n");
497 eh = (struct eeprom_header*)(eeprom_buf);
498 for (i=0;i<eh->num_slots;i++) {
499 struct eeprom_eisa_slot_info *es;
501 es = (struct eeprom_eisa_slot_info*)
502 (&eeprom_buf[HPEE_SLOT_INFO(i)]);
504 if (-1==init_slot(i+1, es)) {
505 continue;
508 if (es->config_data_offset < HPEE_MAX_LENGTH) {
509 if (parse_slot_config(i+1, &eeprom_buf[es->config_data_offset],
510 es, io_parent, mem_parent)) {
511 return -1;
513 } else {
514 printk (KERN_WARNING "EISA EEPROM offset 0x%x out of range\n",es->config_data_offset);
515 return -1;
518 return eh->num_slots;