eisa_enumerator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. #include <linux/ioport.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <asm/io.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/eisa_bus.h>
  20. #include <asm/eisa_eeprom.h>
  21. /*
  22. * Todo:
  23. *
  24. * PORT init with MASK attr and other size than byte
  25. * MEMORY with other decode than 20 bit
  26. * CRC stuff
  27. * FREEFORM stuff
  28. */
  29. #define EPI 0xc80
  30. #define NUM_SLOT 16
  31. #define SLOT2PORT(x) (x<<12)
  32. /* macros to handle unaligned accesses and
  33. * byte swapping. The data in the EEPROM is
  34. * little-endian on the big-endian PAROSC */
  35. #define get_8(x) (*(u_int8_t*)(x))
  36. static inline u_int16_t get_16(const unsigned char *x)
  37. {
  38. return (x[1] << 8) | x[0];
  39. }
  40. static inline u_int32_t get_32(const unsigned char *x)
  41. {
  42. return (x[3] << 24) | (x[2] << 16) | (x[1] << 8) | x[0];
  43. }
  44. static inline u_int32_t get_24(const unsigned char *x)
  45. {
  46. return (x[2] << 24) | (x[1] << 16) | (x[0] << 8);
  47. }
  48. static void print_eisa_id(char *s, u_int32_t id)
  49. {
  50. char vendor[4];
  51. int rev;
  52. int device;
  53. rev = id & 0xff;
  54. id >>= 8;
  55. device = id & 0xff;
  56. id >>= 8;
  57. vendor[3] = '\0';
  58. vendor[2] = '@' + (id & 0x1f);
  59. id >>= 5;
  60. vendor[1] = '@' + (id & 0x1f);
  61. id >>= 5;
  62. vendor[0] = '@' + (id & 0x1f);
  63. id >>= 5;
  64. sprintf(s, "%s%02X%02X", vendor, device, rev);
  65. }
  66. static int configure_memory(const unsigned char *buf,
  67. struct resource *mem_parent,
  68. char *name)
  69. {
  70. int len;
  71. u_int8_t c;
  72. int i;
  73. struct resource *res;
  74. len=0;
  75. for (i=0;i<HPEE_MEMORY_MAX_ENT;i++) {
  76. c = get_8(buf+len);
  77. if (NULL != (res = kmalloc(sizeof(struct resource), GFP_KERNEL))) {
  78. int result;
  79. res->name = name;
  80. res->start = mem_parent->start + get_24(buf+len+2);
  81. res->end = res->start + get_16(buf+len+5)*1024;
  82. res->flags = IORESOURCE_MEM;
  83. printk("memory %lx-%lx ", (unsigned long)res->start, (unsigned long)res->end);
  84. result = request_resource(mem_parent, res);
  85. if (result < 0) {
  86. printk(KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
  87. return result;
  88. }
  89. }
  90. len+=7;
  91. if (!(c & HPEE_MEMORY_MORE)) {
  92. break;
  93. }
  94. }
  95. return len;
  96. }
  97. static int configure_irq(const unsigned char *buf)
  98. {
  99. int len;
  100. u_int8_t c;
  101. int i;
  102. len=0;
  103. for (i=0;i<HPEE_IRQ_MAX_ENT;i++) {
  104. c = get_8(buf+len);
  105. printk("IRQ %d ", c & HPEE_IRQ_CHANNEL_MASK);
  106. if (c & HPEE_IRQ_TRIG_LEVEL) {
  107. eisa_make_irq_level(c & HPEE_IRQ_CHANNEL_MASK);
  108. } else {
  109. eisa_make_irq_edge(c & HPEE_IRQ_CHANNEL_MASK);
  110. }
  111. len+=2;
  112. /* hpux seems to allow for
  113. * two bytes of irq data but only defines one of
  114. * them, I think */
  115. if (!(c & HPEE_IRQ_MORE)) {
  116. break;
  117. }
  118. }
  119. return len;
  120. }
  121. static int configure_dma(const unsigned char *buf)
  122. {
  123. int len;
  124. u_int8_t c;
  125. int i;
  126. len=0;
  127. for (i=0;i<HPEE_DMA_MAX_ENT;i++) {
  128. c = get_8(buf+len);
  129. printk("DMA %d ", c&HPEE_DMA_CHANNEL_MASK);
  130. /* fixme: maybe initialize the dma channel withthe timing ? */
  131. len+=2;
  132. if (!(c & HPEE_DMA_MORE)) {
  133. break;
  134. }
  135. }
  136. return len;
  137. }
  138. static int configure_port(const unsigned char *buf, struct resource *io_parent,
  139. char *board)
  140. {
  141. int len;
  142. u_int8_t c;
  143. int i;
  144. struct resource *res;
  145. int result;
  146. len=0;
  147. for (i=0;i<HPEE_PORT_MAX_ENT;i++) {
  148. c = get_8(buf+len);
  149. if (NULL != (res = kmalloc(sizeof(struct resource), GFP_KERNEL))) {
  150. res->name = board;
  151. res->start = get_16(buf+len+1);
  152. res->end = get_16(buf+len+1)+(c&HPEE_PORT_SIZE_MASK)+1;
  153. res->flags = IORESOURCE_IO;
  154. printk("ioports %lx-%lx ", (unsigned long)res->start, (unsigned long)res->end);
  155. result = request_resource(io_parent, res);
  156. if (result < 0) {
  157. printk(KERN_ERR "EISA Enumerator: failed to claim EISA Bus address space!\n");
  158. return result;
  159. }
  160. }
  161. len+=3;
  162. if (!(c & HPEE_PORT_MORE)) {
  163. break;
  164. }
  165. }
  166. return len;
  167. }
  168. /* byte 1 and 2 is the port number to write
  169. * and at byte 3 the value to write starts.
  170. * I assume that there are and- and or- masks
  171. * here when HPEE_PORT_INIT_MASK is set but I have
  172. * not yet encountered this. */
  173. static int configure_port_init(const unsigned char *buf)
  174. {
  175. int len=0;
  176. u_int8_t c;
  177. while (len<HPEE_PORT_INIT_MAX_LEN) {
  178. int s=0;
  179. c = get_8(buf+len);
  180. switch (c & HPEE_PORT_INIT_WIDTH_MASK) {
  181. case HPEE_PORT_INIT_WIDTH_BYTE:
  182. s=1;
  183. if (c & HPEE_PORT_INIT_MASK) {
  184. printk(KERN_WARNING "port_init: unverified mask attribute\n");
  185. outb((inb(get_16(buf+len+1) &
  186. get_8(buf+len+3)) |
  187. get_8(buf+len+4)), get_16(buf+len+1));
  188. } else {
  189. outb(get_8(buf+len+3), get_16(buf+len+1));
  190. }
  191. break;
  192. case HPEE_PORT_INIT_WIDTH_WORD:
  193. s=2;
  194. if (c & HPEE_PORT_INIT_MASK) {
  195. printk(KERN_WARNING "port_init: unverified mask attribute\n");
  196. outw((inw(get_16(buf+len+1)) &
  197. get_16(buf+len+3)) |
  198. get_16(buf+len+5),
  199. get_16(buf+len+1));
  200. } else {
  201. outw(cpu_to_le16(get_16(buf+len+3)), get_16(buf+len+1));
  202. }
  203. break;
  204. case HPEE_PORT_INIT_WIDTH_DWORD:
  205. s=4;
  206. if (c & HPEE_PORT_INIT_MASK) {
  207. printk(KERN_WARNING "port_init: unverified mask attribute\n");
  208. outl((inl(get_16(buf+len+1) &
  209. get_32(buf+len+3)) |
  210. get_32(buf+len+7)), get_16(buf+len+1));
  211. } else {
  212. outl(cpu_to_le32(get_32(buf+len+3)), get_16(buf+len+1));
  213. }
  214. break;
  215. default:
  216. printk(KERN_ERR "Invalid port init word %02x\n", c);
  217. return 0;
  218. }
  219. if (c & HPEE_PORT_INIT_MASK) {
  220. s*=2;
  221. }
  222. len+=s+3;
  223. if (!(c & HPEE_PORT_INIT_MORE)) {
  224. break;
  225. }
  226. }
  227. return len;
  228. }
  229. static int configure_choise(const unsigned char *buf, u_int8_t *info)
  230. {
  231. int len;
  232. /* theis record contain the value of the functions
  233. * configuration choises and an info byte which
  234. * describes which other records to expect in this
  235. * function */
  236. len = get_8(buf);
  237. *info=get_8(buf+len+1);
  238. return len+2;
  239. }
  240. static int configure_type_string(const unsigned char *buf)
  241. {
  242. int len;
  243. /* just skip past the type field */
  244. len = get_8(buf);
  245. if (len > 80) {
  246. printk(KERN_ERR "eisa_enumerator: type info field too long (%d, max is 80)\n", len);
  247. }
  248. return 1+len;
  249. }
  250. static int configure_function(const unsigned char *buf, int *more)
  251. {
  252. /* the init field seems to be a two-byte field
  253. * which is non-zero if there are an other function following
  254. * I think it is the length of the function def
  255. */
  256. *more = get_16(buf);
  257. return 2;
  258. }
  259. static int parse_slot_config(int slot,
  260. const unsigned char *buf,
  261. struct eeprom_eisa_slot_info *es,
  262. struct resource *io_parent,
  263. struct resource *mem_parent)
  264. {
  265. int res=0;
  266. int function_len;
  267. unsigned int pos=0;
  268. unsigned int maxlen;
  269. int num_func=0;
  270. u_int8_t flags;
  271. int p0;
  272. char *board;
  273. int id_string_used=0;
  274. if (NULL == (board = kmalloc(8, GFP_KERNEL))) {
  275. return -1;
  276. }
  277. print_eisa_id(board, es->eisa_slot_id);
  278. printk(KERN_INFO "EISA slot %d: %s %s ",
  279. slot, board, es->flags&HPEE_FLAG_BOARD_IS_ISA ? "ISA" : "EISA");
  280. maxlen = es->config_data_length < HPEE_MAX_LENGTH ?
  281. es->config_data_length : HPEE_MAX_LENGTH;
  282. while ((pos < maxlen) && (num_func <= es->num_functions)) {
  283. pos+=configure_function(buf+pos, &function_len);
  284. if (!function_len) {
  285. break;
  286. }
  287. num_func++;
  288. p0 = pos;
  289. pos += configure_choise(buf+pos, &flags);
  290. if (flags & HPEE_FUNCTION_INFO_F_DISABLED) {
  291. /* function disabled, skip silently */
  292. pos = p0 + function_len;
  293. continue;
  294. }
  295. if (flags & HPEE_FUNCTION_INFO_CFG_FREE_FORM) {
  296. /* I have no idea how to handle this */
  297. printk("function %d have free-form confgiuration, skipping ",
  298. num_func);
  299. pos = p0 + function_len;
  300. continue;
  301. }
  302. /* the ordering of the sections need
  303. * more investigation.
  304. * Currently I think that memory comaed before IRQ
  305. * I assume the order is LSB to MSB in the
  306. * info flags
  307. * eg type, memory, irq, dma, port, HPEE_PORT_init
  308. */
  309. if (flags & HPEE_FUNCTION_INFO_HAVE_TYPE) {
  310. pos += configure_type_string(buf+pos);
  311. }
  312. if (flags & HPEE_FUNCTION_INFO_HAVE_MEMORY) {
  313. id_string_used=1;
  314. pos += configure_memory(buf+pos, mem_parent, board);
  315. }
  316. if (flags & HPEE_FUNCTION_INFO_HAVE_IRQ) {
  317. pos += configure_irq(buf+pos);
  318. }
  319. if (flags & HPEE_FUNCTION_INFO_HAVE_DMA) {
  320. pos += configure_dma(buf+pos);
  321. }
  322. if (flags & HPEE_FUNCTION_INFO_HAVE_PORT) {
  323. id_string_used=1;
  324. pos += configure_port(buf+pos, io_parent, board);
  325. }
  326. if (flags & HPEE_FUNCTION_INFO_HAVE_PORT_INIT) {
  327. pos += configure_port_init(buf+pos);
  328. }
  329. if (p0 + function_len < pos) {
  330. printk(KERN_ERR "eisa_enumerator: function %d length mis-match "
  331. "got %d, expected %d\n",
  332. num_func, pos-p0, function_len);
  333. res=-1;
  334. break;
  335. }
  336. pos = p0 + function_len;
  337. }
  338. printk("\n");
  339. if (!id_string_used) {
  340. kfree(board);
  341. }
  342. if (pos != es->config_data_length) {
  343. printk(KERN_ERR "eisa_enumerator: config data length mis-match got %d, expected %d\n",
  344. pos, es->config_data_length);
  345. res=-1;
  346. }
  347. if (num_func != es->num_functions) {
  348. printk(KERN_ERR "eisa_enumerator: number of functions mis-match got %d, expected %d\n",
  349. num_func, es->num_functions);
  350. res=-2;
  351. }
  352. return res;
  353. }
  354. static int init_slot(int slot, struct eeprom_eisa_slot_info *es)
  355. {
  356. unsigned int id;
  357. char id_string[8];
  358. if (!(es->slot_info&HPEE_SLOT_INFO_NO_READID)) {
  359. /* try to read the id of the board in the slot */
  360. id = le32_to_cpu(inl(SLOT2PORT(slot)+EPI));
  361. if (0xffffffff == id) {
  362. /* Maybe we didn't expect a card to be here... */
  363. if (es->eisa_slot_id == 0xffffffff)
  364. return -1;
  365. /* this board is not here or it does not
  366. * support readid
  367. */
  368. printk(KERN_ERR "EISA slot %d a configured board was not detected (",
  369. slot);
  370. print_eisa_id(id_string, es->eisa_slot_id);
  371. printk(" expected %s)\n", id_string);
  372. return -1;
  373. }
  374. if (es->eisa_slot_id != id) {
  375. print_eisa_id(id_string, id);
  376. printk(KERN_ERR "EISA slot %d id mis-match: got %s",
  377. slot, id_string);
  378. print_eisa_id(id_string, es->eisa_slot_id);
  379. printk(" expected %s\n", id_string);
  380. return -1;
  381. }
  382. }
  383. /* now: we need to enable the board if
  384. * it supports enabling and run through
  385. * the port init sction if present
  386. * and finally record any interrupt polarity
  387. */
  388. if (es->slot_features & HPEE_SLOT_FEATURES_ENABLE) {
  389. /* enable board */
  390. outb(0x01| inb(SLOT2PORT(slot)+EPI+4),
  391. SLOT2PORT(slot)+EPI+4);
  392. }
  393. return 0;
  394. }
  395. int eisa_enumerator(unsigned long eeprom_addr,
  396. struct resource *io_parent, struct resource *mem_parent)
  397. {
  398. int i;
  399. struct eeprom_header *eh;
  400. static char eeprom_buf[HPEE_MAX_LENGTH];
  401. for (i=0; i < HPEE_MAX_LENGTH; i++) {
  402. eeprom_buf[i] = gsc_readb(eeprom_addr+i);
  403. }
  404. printk(KERN_INFO "Enumerating EISA bus\n");
  405. eh = (struct eeprom_header*)(eeprom_buf);
  406. for (i=0;i<eh->num_slots;i++) {
  407. struct eeprom_eisa_slot_info *es;
  408. es = (struct eeprom_eisa_slot_info*)
  409. (&eeprom_buf[HPEE_SLOT_INFO(i)]);
  410. if (-1==init_slot(i+1, es)) {
  411. continue;
  412. }
  413. if (es->config_data_offset < HPEE_MAX_LENGTH) {
  414. if (parse_slot_config(i+1, &eeprom_buf[es->config_data_offset],
  415. es, io_parent, mem_parent)) {
  416. return -1;
  417. }
  418. } else {
  419. printk (KERN_WARNING "EISA EEPROM offset 0x%x out of range\n",es->config_data_offset);
  420. return -1;
  421. }
  422. }
  423. return eh->num_slots;
  424. }