Import 2.3.18pre1
[davej-history.git] / drivers / net / cycx_drv.c
blob1629bdcf07fedba4c38011410b2a3633eacc6c39
1 /*
2 * cycx_drv.c cycx Support Module.
4 * This module is a library of common hardware-specific
5 * functions used by all Cyclades sync and some async (8x & 16x)
6 * drivers.
8 * Copyright: (c) 1998, 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
10 * Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
12 * Based on sdladrv.c by Gene Kozin <genek@compuserve.com>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 * ============================================================================
19 * 1999/05/28 acme cycx_intack & cycx_intde gone for good
20 * 1999/05/18 acme lots of unlogged work, submitting to Linus...
21 * 1999/01/03 acme more judicious use of data types
22 * 1999/01/03 acme judicious use of data types :>
23 * cycx_inten trying to reset pending interrupts
24 * from cyclom 2x - I think this isn't the way to
25 * go, but for now...
26 * 1999/01/02 acme cycx_intack ok, I think there's nothing to do
27 * to ack an int in cycx_drv.c, only handle it in
28 * cyx_isr (or in the other protocols: cyp_isr,
29 * cyf_isr, when they get implemented.
30 * Dec 31, 1998 Arnaldo cycx_data_boot & cycx_code_boot fixed, crossing
31 * fingers to see x25_configure in cycx_x25.c
32 * work... :)
33 * Dec 26, 1998 Arnaldo load implementation fixed, seems to work! :)
34 * cycx_2x_dpmbase_options with all the possible
35 * DPM addresses (20).
36 * cycx_intr implemented (test this!)
37 * general code cleanup
38 * Dec 8, 1998 Ivan Passos Cyclom-2X firmware load implementation.
39 * Aug 8, 1998 Arnaldo Initial version.
42 #ifdef MODULE
43 #ifdef MODVERSIONS
44 #include <linux/modversions.h>
45 #endif
46 #include <linux/module.h>
47 #else
48 #define EXPORT_SYMBOL(function)
49 #endif
50 #include <linux/kernel.h> /* printk(), and other useful stuff */
51 #include <linux/stddef.h> /* offsetof(), etc. */
52 #include <linux/errno.h> /* return codes */
53 #include <linux/sched.h> /* for jiffies, HZ, etc. */
54 #include <linux/cycx_drv.h> /* API definitions */
55 #include <linux/cycx_cfm.h> /* CYCX firmware module definitions */
56 #include <linux/delay.h> /* udelay */
57 #include <asm/io.h> /* for inb(), outb(), etc. */
59 #define MOD_VERSION 0
60 #define MOD_RELEASE 2
62 #ifdef MODULE
63 MODULE_AUTHOR("Arnaldo Carvalho de Melo");
64 MODULE_DESCRIPTION("Cyclades Sync Cards Driver.");
65 #endif
67 /* Function Prototypes */
68 /* Module entry points. These are called by the OS and must be public. */
69 int init_module (void);
70 void cleanup_module (void);
72 /* Hardware-specific functions */
73 static int cycx_detect (cycxhw_t *hw);
74 static int cycx_load (cycxhw_t *hw, cfm_t *cfm, u32 len);
75 static int cycx_init (cycxhw_t *hw);
76 static int cycx_reset (cycxhw_t *hw);
77 static void cycx_bootcfg (cycxhw_t *hw);
79 static int init_cycx_2x (cycxhw_t *hw);
80 static int reset_cycx_2x (u32 addr);
81 static int detect_cycx_2x (u32 addr);
83 /* Miscellaneous functions */
84 static void delay_cycx (int sec);
85 static int get_option_index (u32 *optlist, u32 optval);
86 static u16 checksum (u8 *buf, u32 len);
88 #define wait_cyc(addr) cycx_exec(addr + CMD_OFFSET)
90 /* Global Data
91 * Note: All data must be explicitly initialized!!! */
93 /* private data */
94 static char modname[] = "cycx_drv";
95 static char fullname[] = "Cyclom X Support Module";
96 static char copyright[] = "(c) 1998, 1999 Arnaldo Carvalho de Melo";
98 /* Hardware configuration options.
99 * These are arrays of configuration options used by verification routines.
100 * The first element of each array is its size (i.e. number of options).
102 static u32 cycx_2x_dpmbase_options[] =
105 0xA0000, 0xA4000, 0xA8000, 0xAC000, 0xB0000, 0xB4000, 0xB8000,
106 0xBC000, 0xC0000, 0xC4000, 0xC8000, 0xCC000, 0xD0000, 0xD4000,
107 0xD8000, 0xDC000, 0xE0000, 0xE4000, 0xE8000, 0xEC000
110 static u32 cycx_2x_irq_options[] = { 7, 3, 5, 9, 10, 11, 12, 15 };
112 /* Kernel Loadable Module Entry Points */
113 /* Module 'insert' entry point.
114 * o print announcement
115 * o initialize static data
117 * Return: 0 Ok
118 * < 0 error.
119 * Context: process */
120 #ifdef MODULE
121 int init_module (void)
123 printk(KERN_INFO "%s v%u.%u %s\n",
124 fullname, MOD_VERSION, MOD_RELEASE, copyright);
125 return 0;
127 /* Module 'remove' entry point.
128 * o release all remaining system resources */
129 void cleanup_module (void)
132 #endif
133 /* Kernel APIs */
134 /* Set up adapter.
135 * o detect adapter type
136 * o verify hardware configuration options
137 * o check for hardware conflicts
138 * o set up adapter shared memory
139 * o test adapter memory
140 * o load firmware
141 * Return: 0 ok.
142 * < 0 error */
143 EXPORT_SYMBOL(cycx_setup);
144 int cycx_setup (cycxhw_t *hw, void *cfm, u32 len)
146 u32 *irq_opt = NULL; /* IRQ options */
147 u32 *dpmbase_opt = NULL;/* DPM window base options */
148 int err = 0;
150 if (cycx_detect(hw)) {
151 printk(KERN_ERR "%s: adapter Cyclom %uX not found at "
152 "address 0x%lX!\n",
153 modname, hw->type, (unsigned long) hw->dpmbase);
154 return -EINVAL;
157 printk(KERN_INFO "%s: found Cyclom %uX card at address 0x%lx.\n",
158 modname, hw->type, (unsigned long) hw->dpmbase);
160 switch (hw->type) {
161 case CYCX_2X:
162 irq_opt = cycx_2x_irq_options;
163 dpmbase_opt = cycx_2x_dpmbase_options;
164 break;
165 default:
166 printk(KERN_ERR "%s: unknown card.\n", modname);
167 return -EINVAL;
170 /* Verify IRQ configuration options */
171 if (!get_option_index(irq_opt, hw->irq)) {
172 printk (KERN_ERR "%s: IRQ %d is illegal!\n", modname, hw->irq);
173 return -EINVAL;
176 /* Setup adapter dual-port memory window and test memory */
177 if (!hw->dpmbase) {
178 printk(KERN_ERR "%s: you must specify the dpm address!\n",
179 modname);
180 return -EINVAL;
182 else if (!get_option_index(dpmbase_opt, hw->dpmbase)) {
183 printk(KERN_ERR "%s: memory address 0x%lX is illegal!\n",
184 modname, (unsigned long) hw->dpmbase);
185 return -EINVAL;
188 hw->dpmsize = CYCX_WINDOWSIZE;
189 /* FIXME! Is this the only amount ever available? */
190 hw->memory = 0x40000;
192 cycx_init(hw);
194 printk(KERN_INFO "%s: dual-port memory window is set at 0x%lX.\n",
195 modname, (unsigned long) hw->dpmbase);
196 printk(KERN_INFO "%s: found %luK bytes of on-board memory.\n",
197 modname, (unsigned long) hw->memory / 1024);
199 /* Load firmware. If loader fails then shut down adapter */
200 err = cycx_load(hw, cfm, len);
201 if (err) cycx_down(hw); /* shutdown adapter */
202 return err;
205 /* Shut down CYCX: disable shared memory access and interrupts, stop CPU,etc.*/
206 EXPORT_SYMBOL(cycx_down);
207 int cycx_down (cycxhw_t *hw)
209 return 0; /* FIXME: anything needed here? */
212 /* Enable interrupt generation. */
213 EXPORT_SYMBOL(cycx_inten);
214 int cycx_inten (cycxhw_t *hw)
216 switch (hw->type) {
217 case CYCX_2X: writeb (0, hw->dpmbase); break;
218 default: return -EINVAL;
221 return 0;
224 /* Generate an interrupt to adapter's CPU. */
225 EXPORT_SYMBOL(cycx_intr);
226 int cycx_intr (cycxhw_t *hw)
228 switch (hw->type) {
229 case CYCX_2X:
230 writew(0, hw->dpmbase + GEN_CYCX_INTR);
231 return 0;
232 default: return -EINVAL;
235 return 0;
238 /* Execute Adapter Command.
239 * o Set exec flag.
240 * o Busy-wait until flag is reset. */
241 EXPORT_SYMBOL(cycx_exec);
242 int cycx_exec (u32 addr)
244 u16 i = 0;
245 /* wait till addr content is zeroed */
247 while (readw(addr) != 0) {
248 udelay(1000);
249 if (++i > 50) return -1;
252 return 0;
255 /* Read absolute adapter memory.
256 * Transfer data from adapter's memory to data buffer. */
257 EXPORT_SYMBOL(cycx_peek);
258 int cycx_peek (cycxhw_t *hw, u32 addr, void *buf, u32 len)
260 if (len == 1) *(u8*)buf = readb (hw->dpmbase + addr);
261 else memcpy_fromio(buf, hw->dpmbase + addr, len);
263 return 0;
266 /* Write Absolute Adapter Memory.
267 * Transfer data from data buffer to adapter's memory. */
268 EXPORT_SYMBOL(cycx_poke);
269 int cycx_poke (cycxhw_t *hw, u32 addr, void *buf, u32 len)
271 if (len == 1) writeb (*(u8*)buf, hw->dpmbase + addr);
272 else memcpy_toio(hw->dpmbase + addr, buf, len);
274 return 0;
277 /* Hardware-Specific Functions */
278 /* Detect adapter type.
279 * o if adapter type is specified then call detection routine for that adapter
280 * type. Otherwise call detection routines for every adapter types until
281 * adapter is detected.
283 * Notes:
284 * 1) Detection tests are destructive! Adapter will be left in shutdown state
285 * after the test. */
286 static int cycx_detect (cycxhw_t *hw)
288 int err = 0;
290 if (!hw->dpmbase) return -EFAULT;
292 switch (hw->type) {
293 case CYCX_2X:
294 if (!detect_cycx_2x(hw->dpmbase)) err = -ENODEV;
295 break;
296 default:
297 if (detect_cycx_2x(hw->dpmbase)) hw->type = CYCX_2X;
298 else err = -ENODEV;
301 return err;
304 /* Load Aux Routines */
305 /* Reset board hardware.
306 return 1 if memory exists at addr and 0 if not. */
307 static int memory_exists(u32 addr)
309 int timeout = 0;
311 for (; timeout < 3 ; timeout++) {
312 writew (TEST_PATTERN, addr + 0x10);
314 if (readw (addr + 0x10) == TEST_PATTERN)
315 if (readw (addr + 0x10) == TEST_PATTERN) return 1;
317 delay_cycx(1);
320 return 0;
323 /* Reset board hardware. */
324 static int cycx_reset(cycxhw_t *hw)
326 /* Reset board */
327 switch (hw->type) {
328 case CYCX_2X: return reset_cycx_2x(hw->dpmbase);
331 return -EINVAL;
334 /* Load reset code. */
335 static void reset_load(u32 addr, u8 *buffer, u32 cnt)
337 u32 pt_code = addr + RESET_OFFSET;
338 u16 i, j;
340 for ( i = 0 ; i < cnt ; i++) {
341 for (j = 0 ; j < 50 ; j++); /* Delay - FIXME busy waiting... */
342 writeb(*buffer++, pt_code++);
346 /* Load buffer using boot interface.
347 * o copy data from buffer to Cyclom-X memory
348 * o wait for reset code to copy it to right portion of memory */
349 static int buffer_load(u32 addr, u8 *buffer, u32 cnt)
351 memcpy_toio(addr + DATA_OFFSET, buffer, cnt);
352 writew(GEN_BOOT_DAT, addr + CMD_OFFSET);
353 return wait_cyc(addr);
356 /* Set up entry point and kick start Cyclom-X CPU. */
357 static void cycx_start (u32 addr)
359 /* put in 0x30 offset the jump instruction to the code entry point */
360 writeb(0xea, addr + 0x30);
361 writeb(0x00, addr + 0x31);
362 writeb(0xc4, addr + 0x32);
363 writeb(0x00, addr + 0x33);
364 writeb(0x00, addr + 0x34);
366 /* cmd to start executing code */
367 writew(GEN_START, addr + CMD_OFFSET);
370 /* Load and boot reset code. */
371 static void cycx_reset_boot(u32 addr, u8 *code, u32 len)
373 u32 pt_start = addr + START_OFFSET;
375 writeb(0xea, pt_start++); /* jmp to f000:3f00 */
376 writeb(0x00, pt_start++);
377 writeb(0xfc, pt_start++);
378 writeb(0x00, pt_start++);
379 writeb(0xf0, pt_start);
380 reset_load(addr, code, len);
382 /* 80186 was in hold, go */
383 writeb(0, addr + START_CPU);
384 delay_cycx(1);
387 /* Load data.bin file through boot (reset) interface. */
388 static int cycx_data_boot(u32 addr, u8 *code, u32 len)
390 u32 pt_boot_cmd = addr + CMD_OFFSET;
391 u32 i;
393 /* boot buffer lenght */
394 writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
395 writew(GEN_DEFPAR, pt_boot_cmd);
397 if (wait_cyc(addr) < 0) return 2;
399 writew(0, pt_boot_cmd + sizeof(u16));
400 writew(0x4000, pt_boot_cmd + 2 * sizeof(u16));
401 writew(GEN_SET_SEG, pt_boot_cmd);
403 if (wait_cyc(addr) < 0) return 2;
405 for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
406 if (buffer_load(addr, code + i,
407 MIN(CFM_LOAD_BUFSZ, (len - i))) < 0) {
408 printk(KERN_ERR "%s: Error !!\n", modname);
409 return 4;
412 return 0;
416 /* Load code.bin file through boot (reset) interface. */
417 static int cycx_code_boot(u32 addr, u8 *code, u32 len)
419 u32 pt_boot_cmd = addr + CMD_OFFSET;
420 u32 i;
422 /* boot buffer lenght */
423 writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
424 writew(GEN_DEFPAR, pt_boot_cmd);
426 if (wait_cyc(addr) == -1) return 2;
428 writew(0x0000, pt_boot_cmd + sizeof(u16));
429 writew(0xc400, pt_boot_cmd + 2 * sizeof(u16));
430 writew(GEN_SET_SEG, pt_boot_cmd);
432 if (wait_cyc(addr) == -1) return 1;
434 for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
435 if (buffer_load(addr, code + i,MIN(CFM_LOAD_BUFSZ,(len - i)))) {
436 printk(KERN_ERR "%s: Error !!\n", modname);
437 return 4;
440 return 0;
443 /* Initialize CYCX hardware: setup memory window, IRQ, etc. */
444 static int cycx_init (cycxhw_t *hw)
446 switch (hw->type) {
447 case CYCX_2X: return init_cycx_2x(hw);
450 return -EINVAL;
453 /* Load adapter from the memory image of the CYCX firmware module.
454 * o verify firmware integrity and compatibility
455 * o start adapter up */
456 static int cycx_load (cycxhw_t *hw, cfm_t *cfm, u32 len)
458 int i, j, status;
459 cycx_header_t *img_hdr;
460 u8 *reset_image,
461 *data_image,
462 *code_image;
463 u32 pt_cycld = hw->dpmbase + 0x400;
464 u16 cksum;
466 /* Announce */
467 printk(KERN_INFO "%s: firmware signature=\"%s\"\n",
468 modname, cfm->signature);
470 /* Verify firmware signature */
471 if (strcmp(cfm->signature, CFM_SIGNATURE)) {
472 printk(KERN_ERR "%s:cycx_load: not Cyclom-2X firmware!\n",
473 modname);
474 return -EINVAL;
477 printk(KERN_INFO "%s: firmware version=%u\n", modname, cfm->version);
479 /* Verify firmware module format version */
480 if (cfm->version != CFM_VERSION) {
481 printk(KERN_ERR "%s:cycx_load: firmware format %u rejected! "
482 "Expecting %u.\n",
483 modname, cfm->version, CFM_VERSION);
484 return -EINVAL;
487 /* Verify firmware module length and checksum */
488 cksum = checksum((u8*)&cfm->info, sizeof(cfm_info_t) +
489 cfm->info.codesize);
491 FIXME cfm->info.codesize is off by 2
492 if (((len - sizeof(cfm_t) - 1) != cfm->info.codesize) ||
494 if (cksum != cfm->checksum) {
495 printk(KERN_ERR "%s:cycx_load: firmware corrupted!\n", modname);
496 printk(KERN_ERR " cdsize = 0x%x (expected 0x%lx)\n",
497 len - sizeof(cfm_t) - 1, cfm->info.codesize);
498 printk(KERN_ERR " chksum = 0x%x (expected 0x%x)\n",
499 cksum, cfm->checksum);
500 return -EINVAL;
503 /* If everything is ok, set reset, data and code pointers */
505 img_hdr = (cycx_header_t*)(((u8*) cfm) + sizeof(cfm_t) - 1);
506 #ifdef FIRMWARE_DEBUG
507 printk(KERN_INFO "%s:cycx_load: image sizes\n", modname);
508 printk(KERN_INFO " reset=%lu\n", img_hdr->reset_size);
509 printk(KERN_INFO " data=%lu\n", img_hdr->data_size);
510 printk(KERN_INFO " code=%lu\n", img_hdr->code_size);
511 #endif
512 reset_image = ((u8 *) img_hdr) + sizeof(cycx_header_t);
513 data_image = reset_image + img_hdr->reset_size;
514 code_image = data_image + img_hdr->data_size;
516 /*---- Start load ----*/
517 /* Announce */
518 printk(KERN_INFO "%s: loading firmware %s (ID=%u)...\n", modname,
519 (cfm->descr[0] != '\0') ? cfm->descr : "unknown firmware",
520 cfm->info.codeid);
522 for (i = 0 ; i < 5 ; i++) {
523 /* Reset Cyclom hardware */
524 if ((status = cycx_reset(hw)) != 0) {
525 printk(KERN_ERR "%s: dpm problem or board not "
526 "found (%d).\n", modname, status);
527 return -EINVAL;
530 /* Load reset.bin */
531 cycx_reset_boot(hw->dpmbase, reset_image, img_hdr->reset_size);
532 /* reset is waiting for boot */
533 writew(GEN_POWER_ON, pt_cycld);
534 delay_cycx(1);
536 for (j = 0 ; j < 3 ; j++)
537 if (!readw(pt_cycld)) goto reset_loaded;
538 else delay_cycx(1);
541 printk(KERN_ERR "%s: reset not started.\n", modname);
542 return -EINVAL;
543 reset_loaded:
544 /* Load data.bin */
545 if((status = cycx_data_boot(hw->dpmbase, data_image,
546 img_hdr->data_size)) != 0) {
547 printk(KERN_ERR "%s: cannot load data file (%d).\n",
548 modname, status);
549 return -EINVAL;
552 /* Load code.bin */
553 if((status = cycx_code_boot(hw->dpmbase, code_image,
554 img_hdr->code_size)) != 0) {
555 printk(KERN_ERR "%s: cannot load code file (%d).\n",
556 modname, status);
557 return -EINVAL;
560 /* Prepare boot-time configuration data */
561 cycx_bootcfg(hw);
563 /* kick-off CPU */
564 cycx_start(hw->dpmbase);
566 /* Arthur Ganzert's tip: wait a while after the firmware loading...
567 seg abr 26 17:17:12 EST 1999 - acme */
568 delay_cycx(7);
569 printk(KERN_INFO "%s: firmware loaded!\n", modname);
571 /* enable interrupts */
572 if (cycx_inten(hw)) {
573 printk(KERN_ERR "%s: adapter hardware failure!\n", modname);
574 return -EIO;
577 return 0;
580 /* Prepare boot-time firmware configuration data.
581 * o initialize configuration data area
582 From async.doc - V_3.4.0 - 07/18/1994
583 - As of now, only static buffers are available to the user.
584 So, the bit VD_RXDIRC must be set in 'valid'. That means that user
585 wants to use the static transmission and reception buffers. */
586 static void cycx_bootcfg (cycxhw_t *hw)
588 /* use fixed buffers */
589 writeb(FIXED_BUFFERS, hw->dpmbase + CONF_OFFSET);
592 /* Initialize CYCX_2X adapter. */
593 static int init_cycx_2x (cycxhw_t *hw)
595 if (!detect_cycx_2x(hw->dpmbase)) return -ENODEV;
596 return 0;
599 /* Detect Cyclom 2x adapter.
600 * Following tests are used to detect Cyclom 2x adapter:
601 * to be completed based on the tests done below
602 * Return 1 if detected o.k. or 0 if failed.
603 * Note: This test is destructive! Adapter will be left in shutdown
604 * state after the test. */
605 static int detect_cycx_2x (u32 addr)
607 printk(KERN_INFO "%s: looking for a cyclom 2x at 0x%lX...\n",
608 modname, (unsigned long) addr);
610 reset_cycx_2x(addr);
611 return memory_exists(addr);
614 /* Miscellaneous */
615 /* Get option's index into the options list.
616 * Return option's index (1 .. N) or zero if option is invalid. */
617 static int get_option_index (u32 *optlist, u32 optval)
619 int i = 1;
620 for (; i <= optlist[0]; ++i) if (optlist[i] == optval) return i;
621 return 0;
624 /* Reset adapter's CPU. */
625 static int reset_cycx_2x (u32 addr)
627 writeb (0, addr + RST_ENABLE); delay_cycx (2);
628 writeb (0, addr + RST_DISABLE); delay_cycx (2);
629 return memory_exists(addr) ? 0 : 1;
632 /* Delay */
633 static void delay_cycx (int sec)
635 /* acme
636 Thu dez 31 21:45:16 EDT 1998
637 FIXME I'll keep this comment here just in case, as of now I don't
638 know it all the contexts where this routine is used are interruptible... */
640 current->state = TASK_INTERRUPTIBLE;
641 schedule_timeout(sec*HZ);
644 /* Calculate 16-bit CRC using CCITT polynomial. */
645 static u16 checksum (u8 *buf, u32 len)
647 u16 crc = 0;
648 u16 mask, flag;
650 for (; len; --len, ++buf)
651 for (mask = 0x80; mask; mask >>= 1) {
652 flag = (crc & 0x8000);
653 crc <<= 1;
654 crc |= ((*buf & mask) ? 1 : 0);
655 if (flag) crc ^= 0x1021;
658 return crc;
660 /* End */