eisa_enumerator.c 11 KB

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