1/*
2 * eisa_enumerator.c - provide support for EISA adapters in PA-RISC machines
3 *
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.
8 *
9 * Copyright (c) 2002 Daniel Engstrom <5116@telia.com>
10 *
11 */
12
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>
20
21#include <asm/eisa_bus.h>
22#include <asm/eisa_eeprom.h>
23
24
25/*
26 * Todo:
27 *
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
32 */
33
34#define EPI 0xc80
35#define NUM_SLOT 16
36#define SLOT2PORT(x) (x<<12)
37
38
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))
43
44static inline u_int16_t get_16(const unsigned char *x)
45{
46	return (x[1] << 8) | x[0];
47}
48
49static inline u_int32_t get_32(const unsigned char *x)
50{
51	return (x[3] << 24) | (x[2] << 16) | (x[1] << 8) | x[0];
52}
53
54static inline u_int32_t get_24(const unsigned char *x)
55{
56	return (x[2] << 24) | (x[1] << 16) | (x[0] << 8);
57}
58
59static void print_eisa_id(char *s, u_int32_t id)
60{
61	char vendor[4];
62	int rev;
63	int device;
64
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;
76
77	sprintf(s, "%s%02X%02X", vendor, device, rev);
78}
79
80static int configure_memory(const unsigned char *buf,
81		       struct resource *mem_parent,
82		       char *name)
83{
84	int len;
85	u_int8_t c;
86	int i;
87	struct resource *res;
88
89	len=0;
90
91	for (i=0;i<HPEE_MEMORY_MAX_ENT;i++) {
92		c = get_8(buf+len);
93
94		if (NULL != (res = kmalloc(sizeof(struct resource), GFP_KERNEL))) {
95			int result;
96
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 ", res->start, res->end);
102			result = request_resource(mem_parent, res);
103			if (result < 0) {
104				printk("\n" KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
105				return result;
106			}
107		}
108
109		len+=7;
110
111		if (!(c & HPEE_MEMORY_MORE)) {
112			break;
113		}
114	}
115
116	return len;
117}
118
119
120static int configure_irq(const unsigned char *buf)
121{
122	int len;
123	u_int8_t c;
124	int i;
125
126	len=0;
127
128	for (i=0;i<HPEE_IRQ_MAX_ENT;i++) {
129		c = get_8(buf+len);
130
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);
136		}
137
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;
144		}
145	}
146
147	return len;
148}
149
150
151static int configure_dma(const unsigned char *buf)
152{
153	int len;
154	u_int8_t c;
155	int i;
156
157	len=0;
158
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;
165		}
166	}
167
168	return len;
169}
170
171static int configure_port(const unsigned char *buf, struct resource *io_parent,
172		     char *board)
173{
174	int len;
175	u_int8_t c;
176	int i;
177	struct resource *res;
178	int result;
179
180	len=0;
181
182	for (i=0;i<HPEE_PORT_MAX_ENT;i++) {
183		c = get_8(buf+len);
184
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 ", res->start, res->end);
191			result = request_resource(io_parent, res);
192			if (result < 0) {
193				printk("\n" KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
194				return result;
195			}
196		}
197
198		len+=3;
199		if (!(c & HPEE_PORT_MORE)) {
200			break;
201		}
202	}
203
204	return len;
205}
206
207
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. */
213static int configure_port_init(const unsigned char *buf)
214{
215	int len=0;
216	u_int8_t c;
217
218	while (len<HPEE_PORT_INIT_MAX_LEN) {
219		int s=0;
220		c = get_8(buf+len);
221
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("\n" 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));
230
231			} else {
232				outb(get_8(buf+len+3), get_16(buf+len+1));
233
234			}
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));
246			}
247			break;
248		 case HPEE_PORT_INIT_WIDTH_DWORD:
249			s=4;
250			if (c & HPEE_PORT_INIT_MASK) {
251 				printk("\n" 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));
257			}
258
259			break;
260		 default:
261			printk("\n" KERN_ERR "Invalid port init word %02x\n", c);
262			return 0;
263		}
264
265		if (c & HPEE_PORT_INIT_MASK) {
266			s*=2;
267		}
268
269		len+=s+3;
270		if (!(c & HPEE_PORT_INIT_MORE)) {
271			break;
272		}
273	}
274
275	return len;
276}
277
278static int configure_choise(const unsigned char *buf, u_int8_t *info)
279{
280	int len;
281
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);
288
289	return len+2;
290}
291
292static int configure_type_string(const unsigned char *buf)
293{
294	int len;
295
296	/* just skip past the type field */
297	len = get_8(buf);
298	if (len > 80) {
299		printk("\n" KERN_ERR "eisa_enumerator: type info field too long (%d, max is 80)\n", len);
300	}
301
302	return 1+len;
303}
304
305static int configure_function(const unsigned char *buf, int *more)
306{
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
310	 */
311	*more = get_16(buf);
312
313	return 2;
314}
315
316static 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)
321{
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;
329
330	char *board;
331	int id_string_used=0;
332
333	if (NULL == (board = kmalloc(8, GFP_KERNEL))) {
334		return -1;
335	}
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");
339
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);
344
345		if (!function_len) {
346			break;
347		}
348		num_func++;
349		p0 = pos;
350		pos += configure_choise(buf+pos, &flags);
351
352		if (flags & HPEE_FUNCTION_INFO_F_DISABLED) {
353			/* function disabled, skip silently */
354			pos = p0 + function_len;
355			continue;
356		}
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;
363		}
364
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
371		 */
372
373		if (flags & HPEE_FUNCTION_INFO_HAVE_TYPE) {
374			pos += configure_type_string(buf+pos);
375		}
376
377		if (flags & HPEE_FUNCTION_INFO_HAVE_MEMORY) {
378			id_string_used=1;
379			pos += configure_memory(buf+pos, mem_parent, board);
380		}
381
382		if (flags & HPEE_FUNCTION_INFO_HAVE_IRQ) {
383			pos += configure_irq(buf+pos);
384		}
385
386		if (flags & HPEE_FUNCTION_INFO_HAVE_DMA) {
387			pos += configure_dma(buf+pos);
388		}
389
390		if (flags & HPEE_FUNCTION_INFO_HAVE_PORT) {
391			id_string_used=1;
392			pos += configure_port(buf+pos, io_parent, board);
393		}
394
395		if (flags &  HPEE_FUNCTION_INFO_HAVE_PORT_INIT) {
396			pos += configure_port_init(buf+pos);
397		}
398
399		if (p0 + function_len < pos) {
400			printk("\n" 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;
405		}
406		pos = p0 + function_len;
407	}
408	printk("\n");
409	if (!id_string_used) {
410		kfree(board);
411	}
412
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;
417	}
418
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;
423	}
424
425	return res;
426
427}
428
429static int init_slot(int slot, struct eeprom_eisa_slot_info *es)
430{
431	unsigned int id;
432
433	char id_string[8];
434
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));
438
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;
443
444			/* this board is not here or it does not
445			 * support readid
446			 */
447			printk(KERN_ERR "EISA slot %d a configured board was not detected (",
448			       slot);
449
450			print_eisa_id(id_string, es->eisa_slot_id);
451			printk(" expected %s)\n", id_string);
452
453			return -1;
454
455		}
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);
460
461			print_eisa_id(id_string, es->eisa_slot_id);
462			printk(" expected %s \n", id_string);
463
464			return -1;
465
466		}
467	}
468
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
473	 */
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);
478	}
479
480	return 0;
481}
482
483
484int eisa_enumerator(unsigned long eeprom_addr,
485		    struct resource *io_parent, struct resource *mem_parent)
486{
487	int i;
488	struct eeprom_header *eh;
489	static char eeprom_buf[HPEE_MAX_LENGTH];
490
491	for (i=0; i < HPEE_MAX_LENGTH; i++) {
492		eeprom_buf[i] = gsc_readb(eeprom_addr+i);
493	}
494
495	printk(KERN_INFO "Enumerating EISA bus\n");
496
497	eh = (struct eeprom_header*)(eeprom_buf);
498	for (i=0;i<eh->num_slots;i++) {
499		struct eeprom_eisa_slot_info *es;
500
501		es = (struct eeprom_eisa_slot_info*)
502			(&eeprom_buf[HPEE_SLOT_INFO(i)]);
503
504		if (-1==init_slot(i+1, es)) {
505			continue;
506		}
507
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;
512			}
513		} else {
514			printk (KERN_WARNING "EISA EEPROM offset 0x%x out of range\n",es->config_data_offset);
515			return -1;
516		}
517	}
518	return eh->num_slots;
519}
520