ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / wan / cycx_drv.c
1 /*
2 * cycx_drv.c    Cyclom 2X Support Module.
3 *
4 *               This module is a library of common hardware specific
5 *               functions used by the Cyclades Cyclom 2X sync card.
6 *
7 * Author:       Arnaldo Carvalho de Melo <acme@conectiva.com.br>
8 *
9 * Copyright:    (c) 1998-2003 Arnaldo Carvalho de Melo
10 *
11 * Based on sdladrv.c by Gene Kozin <genek@compuserve.com>
12 *
13 *               This program is free software; you can redistribute it and/or
14 *               modify it under the terms of the GNU General Public License
15 *               as published by the Free Software Foundation; either version
16 *               2 of the License, or (at your option) any later version.
17 * ============================================================================
18 * 1999/11/11    acme            set_current_state(TASK_INTERRUPTIBLE), code
19 *                               cleanup
20 * 1999/11/08    acme            init_cyc2x deleted, doing nothing
21 * 1999/11/06    acme            back to read[bw], write[bw] and memcpy_to and
22 *                               fromio to use dpmbase ioremaped
23 * 1999/10/26    acme            use isa_read[bw], isa_write[bw] & isa_memcpy_to
24 *                               & fromio
25 * 1999/10/23    acme            cleanup to only supports cyclom2x: all the other
26 *                               boards are no longer manufactured by cyclades,
27 *                               if someone wants to support them... be my guest!
28 * 1999/05/28    acme            cycx_intack & cycx_intde gone for good
29 * 1999/05/18    acme            lots of unlogged work, submitting to Linus...
30 * 1999/01/03    acme            more judicious use of data types
31 * 1999/01/03    acme            judicious use of data types :>
32 *                               cycx_inten trying to reset pending interrupts
33 *                               from cyclom 2x - I think this isn't the way to
34 *                               go, but for now...
35 * 1999/01/02    acme            cycx_intack ok, I think there's nothing to do
36 *                               to ack an int in cycx_drv.c, only handle it in
37 *                               cyx_isr (or in the other protocols: cyp_isr,
38 *                               cyf_isr, when they get implemented.
39 * Dec 31, 1998  acme            cycx_data_boot & cycx_code_boot fixed, crossing
40 *                               fingers to see x25_configure in cycx_x25.c
41 *                               work... :)
42 * Dec 26, 1998  acme            load implementation fixed, seems to work! :)
43 *                               cycx_2x_dpmbase_options with all the possible
44 *                               DPM addresses (20).
45 *                               cycx_intr implemented (test this!)
46 *                               general code cleanup
47 * Dec  8, 1998  Ivan Passos     Cyclom-2X firmware load implementation.
48 * Aug  8, 1998  acme            Initial version.
49 */
50
51 #include <linux/init.h>         /* __init */
52 #include <linux/module.h>
53 #include <linux/kernel.h>       /* printk(), and other useful stuff */
54 #include <linux/stddef.h>       /* offsetof(), etc. */
55 #include <linux/errno.h>        /* return codes */
56 #include <linux/sched.h>        /* for jiffies, HZ, etc. */
57 #include <linux/cycx_drv.h>     /* API definitions */
58 #include <linux/cycx_cfm.h>     /* CYCX firmware module definitions */
59 #include <linux/delay.h>        /* udelay */
60 #include <asm/io.h>             /* read[wl], write[wl], ioremap, iounmap */
61
62 #define MOD_VERSION     0
63 #define MOD_RELEASE     6
64
65 MODULE_AUTHOR("Arnaldo Carvalho de Melo");
66 MODULE_DESCRIPTION("Cyclom 2x Sync Card Driver");
67 MODULE_LICENSE("GPL");
68
69 /* Hardware-specific functions */
70 static int load_cyc2x(struct cycx_hw *hw, struct cycx_firmware *cfm, u32 len);
71 static void cycx_bootcfg(struct cycx_hw *hw);
72
73 static int reset_cyc2x(void *addr);
74 static int detect_cyc2x(void *addr);
75
76 /* Miscellaneous functions */
77 static void delay_cycx(int sec);
78 static int get_option_index(long *optlist, long optval);
79 static u16 checksum(u8 *buf, u32 len);
80
81 #define wait_cyc(addr) cycx_exec(addr + CMD_OFFSET)
82
83 /* Global Data */
84
85 /* private data */
86 static char modname[] = "cycx_drv";
87 static char fullname[] = "Cyclom 2X Support Module";
88 static char copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
89                           "<acme@conectiva.com.br>";
90
91 /* Hardware configuration options.
92  * These are arrays of configuration options used by verification routines.
93  * The first element of each array is its size (i.e. number of options).
94  */
95 static long cyc2x_dpmbase_options[] = {
96         20,
97         0xA0000, 0xA4000, 0xA8000, 0xAC000, 0xB0000, 0xB4000, 0xB8000,
98         0xBC000, 0xC0000, 0xC4000, 0xC8000, 0xCC000, 0xD0000, 0xD4000,
99         0xD8000, 0xDC000, 0xE0000, 0xE4000, 0xE8000, 0xEC000
100 };
101
102 static long cycx_2x_irq_options[]  = { 7, 3, 5, 9, 10, 11, 12, 15 };
103
104 /* Kernel Loadable Module Entry Points */
105 /* Module 'insert' entry point.
106  * o print announcement
107  * o initialize static data
108  *
109  * Return:      0       Ok
110  *              < 0     error.
111  * Context:     process */
112
113 int __init cycx_drv_init(void)
114 {
115         printk(KERN_INFO "%s v%u.%u %s\n", fullname, MOD_VERSION, MOD_RELEASE,
116                          copyright);
117
118         return 0;
119 }
120
121 /* Module 'remove' entry point.
122  * o release all remaining system resources */
123 void cycx_drv_cleanup(void)
124 {
125 }
126
127 /* Kernel APIs */
128 /* Set up adapter.
129  * o detect adapter type
130  * o verify hardware configuration options
131  * o check for hardware conflicts
132  * o set up adapter shared memory
133  * o test adapter memory
134  * o load firmware
135  * Return:      0       ok.
136  *              < 0     error */
137 EXPORT_SYMBOL(cycx_setup);
138 int cycx_setup(struct cycx_hw *hw, void *cfm, u32 len)
139 {
140         long dpmbase = (long)hw->dpmbase;
141         int err;
142
143         /* Verify IRQ configuration options */
144         if (!get_option_index(cycx_2x_irq_options, hw->irq)) {
145                 printk(KERN_ERR "%s: IRQ %d is invalid!\n", modname, hw->irq);
146                 return -EINVAL;
147         }
148
149         /* Setup adapter dual-port memory window and test memory */
150         if (!dpmbase) {
151                 printk(KERN_ERR "%s: you must specify the dpm address!\n",
152                                 modname);
153                 return -EINVAL;
154         } else if (!get_option_index(cyc2x_dpmbase_options, dpmbase)) {
155                 printk(KERN_ERR "%s: memory address 0x%lX is invalid!\n",
156                                 modname, dpmbase);
157                 return -EINVAL;
158         }
159
160         hw->dpmbase = ioremap(dpmbase, CYCX_WINDOWSIZE);
161         hw->dpmsize = CYCX_WINDOWSIZE;
162
163         if (!detect_cyc2x(hw->dpmbase)) {
164                 printk(KERN_ERR "%s: adapter Cyclom 2X not found at "
165                                 "address 0x%lX!\n", modname, dpmbase);
166                 return -EINVAL;
167         }
168
169         printk(KERN_INFO "%s: found Cyclom 2X card at address 0x%lX.\n",
170                          modname, dpmbase);
171
172         /* Load firmware. If loader fails then shut down adapter */
173         err = load_cyc2x(hw, cfm, len);
174
175         if (err)
176                 cycx_down(hw);         /* shutdown adapter */
177
178         return err;
179 }
180
181 EXPORT_SYMBOL(cycx_down);
182 int cycx_down(struct cycx_hw *hw)
183 {
184         iounmap(hw->dpmbase);
185         return 0;
186 }
187
188 /* Enable interrupt generation.  */
189 EXPORT_SYMBOL(cycx_inten);
190 void cycx_inten(struct cycx_hw *hw)
191 {
192         writeb(0, hw->dpmbase);
193 }
194
195 /* Generate an interrupt to adapter's CPU. */
196 EXPORT_SYMBOL(cycx_intr);
197 void cycx_intr(struct cycx_hw *hw)
198 {
199         writew(0, hw->dpmbase + GEN_CYCX_INTR);
200 }
201
202 /* Execute Adapter Command.
203  * o Set exec flag.
204  * o Busy-wait until flag is reset. */
205 EXPORT_SYMBOL(cycx_exec);
206 int cycx_exec(void *addr)
207 {
208         u16 i = 0;
209         /* wait till addr content is zeroed */
210
211         while (readw(addr)) {
212                 udelay(1000);
213
214                 if (++i > 50)
215                         return -1;
216         }
217
218         return 0;
219 }
220
221 /* Read absolute adapter memory.
222  * Transfer data from adapter's memory to data buffer. */
223 EXPORT_SYMBOL(cycx_peek);
224 int cycx_peek(struct cycx_hw *hw, u32 addr, void *buf, u32 len)
225 {
226         if (len == 1)
227                 *(u8*)buf = readb(hw->dpmbase + addr);
228         else
229                 memcpy_fromio(buf, hw->dpmbase + addr, len);
230
231         return 0;
232 }
233
234 /* Write Absolute Adapter Memory.
235  * Transfer data from data buffer to adapter's memory. */
236 EXPORT_SYMBOL(cycx_poke);
237 int cycx_poke(struct cycx_hw *hw, u32 addr, void *buf, u32 len)
238 {
239         if (len == 1)
240                 writeb(*(u8*)buf, hw->dpmbase + addr);
241         else
242                 memcpy_toio(hw->dpmbase + addr, buf, len);
243
244         return 0;
245 }
246
247 /* Hardware-Specific Functions */
248
249 /* Load Aux Routines */
250 /* Reset board hardware.
251    return 1 if memory exists at addr and 0 if not. */
252 static int memory_exists(void *addr)
253 {
254         int tries = 0;
255
256         for (; tries < 3 ; tries++) {
257                 writew(TEST_PATTERN, addr + 0x10);
258
259                 if (readw(addr + 0x10) == TEST_PATTERN)
260                         if (readw(addr + 0x10) == TEST_PATTERN)
261                                 return 1;
262
263                 delay_cycx(1);
264         }
265
266         return 0;
267 }
268
269 /* Load reset code. */
270 static void reset_load(void *addr, u8 *buffer, u32 cnt)
271 {
272         void *pt_code = addr + RESET_OFFSET;
273         u16 i; /*, j; */
274
275         for (i = 0 ; i < cnt ; i++) {
276 /*              for (j = 0 ; j < 50 ; j++); Delay - FIXME busy waiting... */
277                 writeb(*buffer++, pt_code++);
278         }
279 }
280
281 /* Load buffer using boot interface.
282  * o copy data from buffer to Cyclom-X memory
283  * o wait for reset code to copy it to right portion of memory */
284 static int buffer_load(void *addr, u8 *buffer, u32 cnt)
285 {
286         memcpy_toio(addr + DATA_OFFSET, buffer, cnt);
287         writew(GEN_BOOT_DAT, addr + CMD_OFFSET);
288
289         return wait_cyc(addr);
290 }
291
292 /* Set up entry point and kick start Cyclom-X CPU. */
293 static void cycx_start(void *addr)
294 {
295         /* put in 0x30 offset the jump instruction to the code entry point */
296         writeb(0xea, addr + 0x30);
297         writeb(0x00, addr + 0x31);
298         writeb(0xc4, addr + 0x32);
299         writeb(0x00, addr + 0x33);
300         writeb(0x00, addr + 0x34);
301
302         /* cmd to start executing code */
303         writew(GEN_START, addr + CMD_OFFSET);
304 }
305
306 /* Load and boot reset code. */
307 static void cycx_reset_boot(void *addr, u8 *code, u32 len)
308 {
309         void *pt_start = addr + START_OFFSET;
310
311         writeb(0xea, pt_start++); /* jmp to f000:3f00 */
312         writeb(0x00, pt_start++);
313         writeb(0xfc, pt_start++);
314         writeb(0x00, pt_start++);
315         writeb(0xf0, pt_start);
316         reset_load(addr, code, len);
317
318         /* 80186 was in hold, go */
319         writeb(0, addr + START_CPU);
320         delay_cycx(1);
321 }
322
323 /* Load data.bin file through boot (reset) interface. */
324 static int cycx_data_boot(void *addr, u8 *code, u32 len)
325 {
326         void *pt_boot_cmd = addr + CMD_OFFSET;
327         u32 i;
328
329         /* boot buffer lenght */
330         writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
331         writew(GEN_DEFPAR, pt_boot_cmd);
332
333         if (wait_cyc(addr) < 0)
334                 return -1;
335
336         writew(0, pt_boot_cmd + sizeof(u16));
337         writew(0x4000, pt_boot_cmd + 2 * sizeof(u16));
338         writew(GEN_SET_SEG, pt_boot_cmd);
339
340         if (wait_cyc(addr) < 0)
341                 return -1;
342
343         for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
344                 if (buffer_load(addr, code + i,
345                                 min_t(u32, CFM_LOAD_BUFSZ, (len - i))) < 0) {
346                         printk(KERN_ERR "%s: Error !!\n", modname);
347                         return -1;
348                 }
349
350         return 0;
351 }
352
353
354 /* Load code.bin file through boot (reset) interface. */
355 static int cycx_code_boot(void *addr, u8 *code, u32 len)
356 {
357         void *pt_boot_cmd = addr + CMD_OFFSET;
358         u32 i;
359
360         /* boot buffer lenght */
361         writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
362         writew(GEN_DEFPAR, pt_boot_cmd);
363
364         if (wait_cyc(addr) < 0)
365                 return -1;
366
367         writew(0x0000, pt_boot_cmd + sizeof(u16));
368         writew(0xc400, pt_boot_cmd + 2 * sizeof(u16));
369         writew(GEN_SET_SEG, pt_boot_cmd);
370
371         if (wait_cyc(addr) < 0)
372                 return -1;
373
374         for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
375                 if (buffer_load(addr, code + i,
376                                 min_t(u32, CFM_LOAD_BUFSZ, (len - i)))) {
377                         printk(KERN_ERR "%s: Error !!\n", modname);
378                         return -1;
379                 }
380
381         return 0;
382 }
383
384 /* Load adapter from the memory image of the CYCX firmware module.
385  * o verify firmware integrity and compatibility
386  * o start adapter up */
387 static int load_cyc2x(struct cycx_hw *hw, struct cycx_firmware *cfm, u32 len)
388 {
389         int i, j;
390         struct cycx_fw_header *img_hdr;
391         u8 *reset_image,
392            *data_image,
393            *code_image;
394         void *pt_cycld = hw->dpmbase + 0x400;
395         u16 cksum;
396
397         /* Announce */
398         printk(KERN_INFO "%s: firmware signature=\"%s\"\n", modname,
399                                                             cfm->signature);
400
401         /* Verify firmware signature */
402         if (strcmp(cfm->signature, CFM_SIGNATURE)) {
403                 printk(KERN_ERR "%s:load_cyc2x: not Cyclom-2X firmware!\n",
404                                 modname);
405                 return -EINVAL;
406         }
407
408         printk(KERN_INFO "%s: firmware version=%u\n", modname, cfm->version);
409
410         /* Verify firmware module format version */
411         if (cfm->version != CFM_VERSION) {
412                 printk(KERN_ERR "%s:%s: firmware format %u rejected! "
413                                 "Expecting %u.\n",
414                                 modname, __FUNCTION__, cfm->version, CFM_VERSION);
415                 return -EINVAL;
416         }
417
418         /* Verify firmware module length and checksum */
419         cksum = checksum((u8*)&cfm->info, sizeof(struct cycx_fw_info) +
420                                           cfm->info.codesize);
421 /*
422         FIXME cfm->info.codesize is off by 2
423         if (((len - sizeof(struct cycx_firmware) - 1) != cfm->info.codesize) ||
424 */
425         if (cksum != cfm->checksum) {
426                 printk(KERN_ERR "%s:%s: firmware corrupted!\n",
427                                 modname, __FUNCTION__);
428                 printk(KERN_ERR " cdsize = 0x%x (expected 0x%lx)\n",
429                                 len - (int)sizeof(struct cycx_firmware) - 1,
430                                 cfm->info.codesize);
431                 printk(KERN_ERR " chksum = 0x%x (expected 0x%x)\n",
432                                 cksum, cfm->checksum);
433                 return -EINVAL;
434         }
435
436         /* If everything is ok, set reset, data and code pointers */
437         img_hdr = (struct cycx_fw_header *)&cfm->image;
438 #ifdef FIRMWARE_DEBUG
439         printk(KERN_INFO "%s:%s: image sizes\n", __FUNCTION__, modname);
440         printk(KERN_INFO " reset=%lu\n", img_hdr->reset_size);
441         printk(KERN_INFO "  data=%lu\n", img_hdr->data_size);
442         printk(KERN_INFO "  code=%lu\n", img_hdr->code_size);
443 #endif
444         reset_image = ((u8 *)img_hdr) + sizeof(struct cycx_fw_header);
445         data_image = reset_image + img_hdr->reset_size;
446         code_image = data_image + img_hdr->data_size;
447
448         /*---- Start load ----*/
449         /* Announce */
450         printk(KERN_INFO "%s: loading firmware %s (ID=%u)...\n", modname,
451                          cfm->descr[0] ? cfm->descr : "unknown firmware",
452                          cfm->info.codeid);
453
454         for (i = 0 ; i < 5 ; i++) {
455                 /* Reset Cyclom hardware */
456                 if (!reset_cyc2x(hw->dpmbase)) {
457                         printk(KERN_ERR "%s: dpm problem or board not found\n",
458                                         modname);
459                         return -EINVAL;
460                 }
461
462                 /* Load reset.bin */
463                 cycx_reset_boot(hw->dpmbase, reset_image, img_hdr->reset_size);
464                 /* reset is waiting for boot */
465                 writew(GEN_POWER_ON, pt_cycld);
466                 delay_cycx(1);
467
468                 for (j = 0 ; j < 3 ; j++)
469                         if (!readw(pt_cycld))
470                                 goto reset_loaded;
471                         else
472                                 delay_cycx(1);
473         }
474
475         printk(KERN_ERR "%s: reset not started.\n", modname);
476         return -EINVAL;
477
478 reset_loaded:
479         /* Load data.bin */
480         if (cycx_data_boot(hw->dpmbase, data_image, img_hdr->data_size)) {
481                 printk(KERN_ERR "%s: cannot load data file.\n", modname);
482                 return -EINVAL;
483         }
484
485         /* Load code.bin */
486         if (cycx_code_boot(hw->dpmbase, code_image, img_hdr->code_size)) {
487                 printk(KERN_ERR "%s: cannot load code file.\n", modname);
488                 return -EINVAL;
489         }
490
491         /* Prepare boot-time configuration data */
492         cycx_bootcfg(hw);
493
494         /* kick-off CPU */
495         cycx_start(hw->dpmbase);
496
497         /* Arthur Ganzert's tip: wait a while after the firmware loading...
498            seg abr 26 17:17:12 EST 1999 - acme */
499         delay_cycx(7);
500         printk(KERN_INFO "%s: firmware loaded!\n", modname);
501
502         /* enable interrupts */
503         cycx_inten(hw);
504
505         return 0;
506 }
507
508 /* Prepare boot-time firmware configuration data.
509  * o initialize configuration data area
510    From async.doc - V_3.4.0 - 07/18/1994
511    - As of now, only static buffers are available to the user.
512      So, the bit VD_RXDIRC must be set in 'valid'. That means that user
513      wants to use the static transmission and reception buffers. */
514 static void cycx_bootcfg(struct cycx_hw *hw)
515 {
516         /* use fixed buffers */
517         writeb(FIXED_BUFFERS, hw->dpmbase + CONF_OFFSET);
518 }
519
520 /* Detect Cyclom 2x adapter.
521  *      Following tests are used to detect Cyclom 2x adapter:
522  *       to be completed based on the tests done below
523  *      Return 1 if detected o.k. or 0 if failed.
524  *      Note:   This test is destructive! Adapter will be left in shutdown
525  *              state after the test. */
526 static int detect_cyc2x(void *addr)
527 {
528         reset_cyc2x(addr);
529
530         return memory_exists(addr);
531 }
532
533 /* Miscellaneous */
534 /* Get option's index into the options list.
535  *      Return option's index (1 .. N) or zero if option is invalid. */
536 static int get_option_index(long *optlist, long optval)
537 {
538         int i = 1;
539
540         for (; i <= optlist[0]; ++i)
541                 if (optlist[i] == optval)
542                         return i;
543
544         return 0;
545 }
546
547 /* Reset adapter's CPU. */
548 static int reset_cyc2x(void *addr)
549 {
550         writeb(0, addr + RST_ENABLE);
551         delay_cycx(2);
552         writeb(0, addr + RST_DISABLE);
553         delay_cycx(2);
554
555         return memory_exists(addr);
556 }
557
558 /* Delay */
559 static void delay_cycx(int sec)
560 {
561         set_current_state(TASK_INTERRUPTIBLE);
562         schedule_timeout(sec * HZ);
563 }
564
565 /* Calculate 16-bit CRC using CCITT polynomial. */
566 static u16 checksum(u8 *buf, u32 len)
567 {
568         u16 crc = 0;
569         u16 mask, flag;
570
571         for (; len; --len, ++buf)
572                 for (mask = 0x80; mask; mask >>= 1) {
573                         flag = (crc & 0x8000);
574                         crc <<= 1;
575                         crc |= ((*buf & mask) ? 1 : 0);
576
577                         if (flag)
578                                 crc ^= 0x1021;
579                 }
580
581         return crc;
582 }
583
584 module_init(cycx_drv_init);
585 module_exit(cycx_drv_cleanup);
586
587 /* End */