1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * eisa_enumerator.c - provide support for EISA adapters in PA-RISC machines
5 * Copyright (c) 2002 Daniel Engstrom <5116@telia.com>
8 #include <linux/ioport.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <asm/byteorder.h>
16 #include <asm/eisa_bus.h>
17 #include <asm/eisa_eeprom.h>
23 * PORT init with MASK attr and other size than byte
24 * MEMORY with other decode than 20 bit
31 #define SLOT2PORT(x) (x<<12)
34 /* macros to handle unaligned accesses and
35 * byte swapping. The data in the EEPROM is
36 * little-endian on the big-endian PAROSC */
37 #define get_8(x) (*(u_int8_t*)(x))
39 static inline u_int16_t get_16(const unsigned char *x)
41 return (x[1] << 8) | x[0];
44 static inline u_int32_t get_32(const unsigned char *x)
46 return (x[3] << 24) | (x[2] << 16) | (x[1] << 8) | x[0];
49 static inline u_int32_t get_24(const unsigned char *x)
51 return (x[2] << 24) | (x[1] << 16) | (x[0] << 8);
54 static void print_eisa_id(char *s, u_int32_t id)
65 vendor[2] = '@' + (id & 0x1f);
67 vendor[1] = '@' + (id & 0x1f);
69 vendor[0] = '@' + (id & 0x1f);
72 sprintf(s, "%s%02X%02X", vendor, device, rev);
75 static int configure_memory(const unsigned char *buf,
76 struct resource *mem_parent,
86 for (i=0;i<HPEE_MEMORY_MAX_ENT;i++) {
89 if (NULL != (res = kzalloc(sizeof(struct resource), GFP_KERNEL))) {
93 res->start = mem_parent->start + get_24(buf+len+2);
94 res->end = res->start + get_16(buf+len+5)*1024;
95 res->flags = IORESOURCE_MEM;
96 pr_cont("memory %pR ", res);
97 result = request_resource(mem_parent, res);
99 printk(KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
106 if (!(c & HPEE_MEMORY_MORE)) {
115 static int configure_irq(const unsigned char *buf)
123 for (i=0;i<HPEE_IRQ_MAX_ENT;i++) {
126 pr_cont("IRQ %d ", c & HPEE_IRQ_CHANNEL_MASK);
127 if (c & HPEE_IRQ_TRIG_LEVEL) {
128 eisa_make_irq_level(c & HPEE_IRQ_CHANNEL_MASK);
130 eisa_make_irq_edge(c & HPEE_IRQ_CHANNEL_MASK);
134 /* hpux seems to allow for
135 * two bytes of irq data but only defines one of
137 if (!(c & HPEE_IRQ_MORE)) {
146 static int configure_dma(const unsigned char *buf)
154 for (i=0;i<HPEE_DMA_MAX_ENT;i++) {
156 pr_cont("DMA %d ", c&HPEE_DMA_CHANNEL_MASK);
157 /* fixme: maybe initialize the dma channel withthe timing ? */
159 if (!(c & HPEE_DMA_MORE)) {
167 static int configure_port(const unsigned char *buf, struct resource *io_parent,
173 struct resource *res;
178 for (i=0;i<HPEE_PORT_MAX_ENT;i++) {
181 if (NULL != (res = kzalloc(sizeof(struct resource), GFP_KERNEL))) {
183 res->start = get_16(buf+len+1);
184 res->end = get_16(buf+len+1)+(c&HPEE_PORT_SIZE_MASK)+1;
185 res->flags = IORESOURCE_IO;
186 pr_cont("ioports %pR ", res);
187 result = request_resource(io_parent, res);
189 printk(KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
195 if (!(c & HPEE_PORT_MORE)) {
204 /* byte 1 and 2 is the port number to write
205 * and at byte 3 the value to write starts.
206 * I assume that there are and- and or- masks
207 * here when HPEE_PORT_INIT_MASK is set but I have
208 * not yet encountered this. */
209 static int configure_port_init(const unsigned char *buf)
214 while (len<HPEE_PORT_INIT_MAX_LEN) {
218 switch (c & HPEE_PORT_INIT_WIDTH_MASK) {
219 case HPEE_PORT_INIT_WIDTH_BYTE:
221 if (c & HPEE_PORT_INIT_MASK) {
222 printk(KERN_WARNING "port_init: unverified mask attribute\n");
223 outb((inb(get_16(buf+len+1) &
225 get_8(buf+len+4)), get_16(buf+len+1));
228 outb(get_8(buf+len+3), get_16(buf+len+1));
232 case HPEE_PORT_INIT_WIDTH_WORD:
234 if (c & HPEE_PORT_INIT_MASK) {
235 printk(KERN_WARNING "port_init: unverified mask attribute\n");
236 outw((inw(get_16(buf+len+1)) &
241 outw(cpu_to_le16(get_16(buf+len+3)), get_16(buf+len+1));
244 case HPEE_PORT_INIT_WIDTH_DWORD:
246 if (c & HPEE_PORT_INIT_MASK) {
247 printk(KERN_WARNING "port_init: unverified mask attribute\n");
248 outl((inl(get_16(buf+len+1) &
250 get_32(buf+len+7)), get_16(buf+len+1));
252 outl(cpu_to_le32(get_32(buf+len+3)), get_16(buf+len+1));
257 printk(KERN_ERR "Invalid port init word %02x\n", c);
261 if (c & HPEE_PORT_INIT_MASK) {
266 if (!(c & HPEE_PORT_INIT_MORE)) {
274 static int configure_choise(const unsigned char *buf, u_int8_t *info)
278 /* theis record contain the value of the functions
279 * configuration choises and an info byte which
280 * describes which other records to expect in this
283 *info=get_8(buf+len+1);
288 static int configure_type_string(const unsigned char *buf)
292 /* just skip past the type field */
295 printk(KERN_ERR "eisa_enumerator: type info field too long (%d, max is 80)\n", len);
301 static int configure_function(const unsigned char *buf, int *more)
303 /* the init field seems to be a two-byte field
304 * which is non-zero if there are an other function following
305 * I think it is the length of the function def
312 static int parse_slot_config(int slot,
313 const unsigned char *buf,
314 struct eeprom_eisa_slot_info *es,
315 struct resource *io_parent,
316 struct resource *mem_parent)
327 int id_string_used=0;
329 if (NULL == (board = kmalloc(8, GFP_KERNEL))) {
332 print_eisa_id(board, es->eisa_slot_id);
333 printk(KERN_INFO "EISA slot %d: %s %s ",
334 slot, board, es->flags&HPEE_FLAG_BOARD_IS_ISA ? "ISA" : "EISA");
336 maxlen = es->config_data_length < HPEE_MAX_LENGTH ?
337 es->config_data_length : HPEE_MAX_LENGTH;
338 while ((pos < maxlen) && (num_func <= es->num_functions)) {
339 pos+=configure_function(buf+pos, &function_len);
346 pos += configure_choise(buf+pos, &flags);
348 if (flags & HPEE_FUNCTION_INFO_F_DISABLED) {
349 /* function disabled, skip silently */
350 pos = p0 + function_len;
353 if (flags & HPEE_FUNCTION_INFO_CFG_FREE_FORM) {
354 /* I have no idea how to handle this */
355 printk("function %d have free-form configuration, skipping ",
357 pos = p0 + function_len;
361 /* the ordering of the sections need
362 * more investigation.
363 * Currently I think that memory comaed before IRQ
364 * I assume the order is LSB to MSB in the
366 * eg type, memory, irq, dma, port, HPEE_PORT_init
369 if (flags & HPEE_FUNCTION_INFO_HAVE_TYPE) {
370 pos += configure_type_string(buf+pos);
373 if (flags & HPEE_FUNCTION_INFO_HAVE_MEMORY) {
375 pos += configure_memory(buf+pos, mem_parent, board);
378 if (flags & HPEE_FUNCTION_INFO_HAVE_IRQ) {
379 pos += configure_irq(buf+pos);
382 if (flags & HPEE_FUNCTION_INFO_HAVE_DMA) {
383 pos += configure_dma(buf+pos);
386 if (flags & HPEE_FUNCTION_INFO_HAVE_PORT) {
388 pos += configure_port(buf+pos, io_parent, board);
391 if (flags & HPEE_FUNCTION_INFO_HAVE_PORT_INIT) {
392 pos += configure_port_init(buf+pos);
395 if (p0 + function_len < pos) {
396 printk(KERN_ERR "eisa_enumerator: function %d length mis-match "
397 "got %d, expected %d\n",
398 num_func, pos-p0, function_len);
402 pos = p0 + function_len;
405 if (!id_string_used) {
409 if (pos != es->config_data_length) {
410 printk(KERN_ERR "eisa_enumerator: config data length mis-match got %d, expected %d\n",
411 pos, es->config_data_length);
415 if (num_func != es->num_functions) {
416 printk(KERN_ERR "eisa_enumerator: number of functions mis-match got %d, expected %d\n",
417 num_func, es->num_functions);
425 static int init_slot(int slot, struct eeprom_eisa_slot_info *es)
431 if (!(es->slot_info&HPEE_SLOT_INFO_NO_READID)) {
432 /* try to read the id of the board in the slot */
433 id = le32_to_cpu(inl(SLOT2PORT(slot)+EPI));
435 if (0xffffffff == id) {
436 /* Maybe we didn't expect a card to be here... */
437 if (es->eisa_slot_id == 0xffffffff)
440 /* this board is not here or it does not
443 printk(KERN_ERR "EISA slot %d a configured board was not detected (",
446 print_eisa_id(id_string, es->eisa_slot_id);
447 printk(" expected %s)\n", id_string);
452 if (es->eisa_slot_id != id) {
453 print_eisa_id(id_string, id);
454 printk(KERN_ERR "EISA slot %d id mis-match: got %s",
457 print_eisa_id(id_string, es->eisa_slot_id);
458 printk(" expected %s\n", id_string);
465 /* now: we need to enable the board if
466 * it supports enabling and run through
467 * the port init sction if present
468 * and finally record any interrupt polarity
470 if (es->slot_features & HPEE_SLOT_FEATURES_ENABLE) {
472 outb(0x01| inb(SLOT2PORT(slot)+EPI+4),
473 SLOT2PORT(slot)+EPI+4);
480 int eisa_enumerator(unsigned long eeprom_addr,
481 struct resource *io_parent, struct resource *mem_parent)
484 struct eeprom_header *eh;
485 static char eeprom_buf[HPEE_MAX_LENGTH];
487 for (i=0; i < HPEE_MAX_LENGTH; i++) {
488 eeprom_buf[i] = gsc_readb(eeprom_addr+i);
491 printk(KERN_INFO "Enumerating EISA bus\n");
493 eh = (struct eeprom_header*)(eeprom_buf);
494 for (i=0;i<eh->num_slots;i++) {
495 struct eeprom_eisa_slot_info *es;
497 es = (struct eeprom_eisa_slot_info*)
498 (&eeprom_buf[HPEE_SLOT_INFO(i)]);
500 if (-1==init_slot(i+1, es)) {
504 if (es->config_data_offset < HPEE_MAX_LENGTH) {
505 if (parse_slot_config(i+1, &eeprom_buf[es->config_data_offset],
506 es, io_parent, mem_parent)) {
510 printk (KERN_WARNING "EISA EEPROM offset 0x%x out of range\n",es->config_data_offset);
514 return eh->num_slots;