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>
18 #include <asm/uaccess.h>
19 #include <asm/byteorder.h>
21 #include <asm/eisa_bus.h>
22 #include <asm/eisa_eeprom.h>
28 * PORT init with MASK attr and other size than byte
29 * MEMORY with other decode than 20 bit
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
)
70 vendor
[2] = '@' + (id
& 0x1f);
72 vendor
[1] = '@' + (id
& 0x1f);
74 vendor
[0] = '@' + (id
& 0x1f);
77 sprintf(s
, "%s%02X%02X", vendor
, device
, rev
);
80 static int configure_memory(const unsigned char *buf
,
81 struct resource
*mem_parent
,
91 for (i
=0;i
<HPEE_MEMORY_MAX_ENT
;i
++) {
94 if (NULL
!= (res
= kmalloc(sizeof(struct resource
), GFP_KERNEL
))) {
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
);
104 printk(KERN_ERR
"EISA Enumerator: failed to claim EISA Bus address space!\n");
111 if (!(c
& HPEE_MEMORY_MORE
)) {
120 static int configure_irq(const unsigned char *buf
)
128 for (i
=0;i
<HPEE_IRQ_MAX_ENT
;i
++) {
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
);
135 eisa_make_irq_edge(c
& HPEE_IRQ_CHANNEL_MASK
);
139 /* hpux seems to allow for
140 * two bytes of irq data but only defines one of
142 if (!(c
& HPEE_IRQ_MORE
)) {
151 static int configure_dma(const unsigned char *buf
)
159 for (i
=0;i
<HPEE_DMA_MAX_ENT
;i
++) {
161 printk("DMA %d ", c
&HPEE_DMA_CHANNEL_MASK
);
163 if (!(c
& HPEE_DMA_MORE
)) {
171 static int configure_port(const unsigned char *buf
, struct resource
*io_parent
,
177 struct resource
*res
;
182 for (i
=0;i
<HPEE_PORT_MAX_ENT
;i
++) {
185 if (NULL
!= (res
= kmalloc(sizeof(struct resource
), GFP_KERNEL
))) {
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
);
193 printk(KERN_ERR
"EISA Enumerator: failed to claim EISA Bus address space!\n");
199 if (!(c
& HPEE_PORT_MORE
)) {
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
)
218 while (len
<HPEE_PORT_INIT_MAX_LEN
) {
222 switch (c
& HPEE_PORT_INIT_WIDTH_MASK
) {
223 case HPEE_PORT_INIT_WIDTH_BYTE
:
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) &
229 get_8(buf
+len
+4)), get_16(buf
+len
+1));
232 outb(get_8(buf
+len
+3), get_16(buf
+len
+1));
236 case HPEE_PORT_INIT_WIDTH_WORD
:
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)) &
245 outw(cpu_to_le16(get_16(buf
+len
+3)), get_16(buf
+len
+1));
248 case HPEE_PORT_INIT_WIDTH_DWORD
:
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) &
254 get_32(buf
+len
+7)), get_16(buf
+len
+1));
256 outl(cpu_to_le32(get_32(buf
+len
+3)), get_16(buf
+len
+1));
261 printk(KERN_ERR
"Invalid port init word %02x\n", c
);
265 if (c
& HPEE_PORT_INIT_MASK
) {
270 if (!(c
& HPEE_PORT_INIT_MORE
)) {
278 static int configure_choise(const unsigned char *buf
, u_int8_t
*info
)
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
287 *info
=get_8(buf
+len
+1);
292 static int configure_type_string(const unsigned char *buf
)
296 /* just skip past the type field */
299 printk(KERN_ERR
"eisa_enumerator: type info field too long (%d, max is 80)\n", 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
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
)
331 int id_string_used
=0;
333 if (NULL
== (board
= kmalloc(8, GFP_KERNEL
))) {
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
);
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
;
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 ",
361 pos
= p0
+ function_len
;
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
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
) {
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
) {
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
);
406 pos
= p0
+ function_len
;
409 if (!id_string_used
) {
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
);
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
);
429 static int init_slot(int slot
, struct eeprom_eisa_slot_info
*es
)
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)
444 /* this board is not here or it does not
447 printk(KERN_ERR
"EISA slot %d a configured board was not detected (",
450 print_eisa_id(id_string
, es
->eisa_slot_id
);
451 printk(" expected %s)\n", id_string
);
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",
461 print_eisa_id(id_string
, es
->eisa_slot_id
);
462 printk(" expected %s\n", id_string
);
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
) {
476 outb(0x01| inb(SLOT2PORT(slot
)+EPI
+4),
477 SLOT2PORT(slot
)+EPI
+4);
484 int eisa_enumerator(unsigned long eeprom_addr
,
485 struct resource
*io_parent
, struct resource
*mem_parent
)
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
)) {
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
)) {
514 printk (KERN_WARNING
"EISA EEPROM offset 0x%x out of range\n",es
->config_data_offset
);
518 return eh
->num_slots
;