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-2000 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
70 /* Function Prototypes */
71 /* Module entry points. These are called by the OS and must be public. */
72 int init_module(void);
73 void cleanup_module(void);
74
75 /* Hardware-specific functions */
76 static int load_cyc2x(cycxhw_t *hw, cfm_t *cfm, u32 len);
77 static void cycx_bootcfg(cycxhw_t *hw);
78
79 static int reset_cyc2x(u32 addr);
80 static int detect_cyc2x(u32 addr);
81
82 /* Miscellaneous functions */
83 static void delay_cycx(int sec);
84 static int get_option_index(u32 *optlist, u32 optval);
85 static u16 checksum(u8 *buf, u32 len);
86
87 #define wait_cyc(addr) cycx_exec(addr + CMD_OFFSET)
88
89 #define cyc2x_readb(b) readb(b)
90 #define cyc2x_readw(b) readw(b)
91 #define cyc2x_writeb(b, addr) writeb(b, addr)
92 #define cyc2x_writew(w, addr) writew(w, addr)
93 #define cyc2x_memcpy_toio(addr, buf, len) memcpy_toio((addr), buf, len)
94 #define cyc2x_memcpy_fromio(buf, addr, len) memcpy_fromio(buf, (addr), len)
95
96 /* Global Data */
97
98 /* private data */
99 static char modname[] = "cycx_drv";
100 static char fullname[] = "Cyclom 2X Support Module";
101 static char copyright[] = "(c) 1998-2000 Arnaldo Carvalho de Melo "
102 "<acme@conectiva.com.br>";
103
104 /* Hardware configuration options.
105 * These are arrays of configuration options used by verification routines.
106 * The first element of each array is its size (i.e. number of options).
107 */
108 static u32 cyc2x_dpmbase_options[] =
109 {
110 20,
111 0xA0000, 0xA4000, 0xA8000, 0xAC000, 0xB0000, 0xB4000, 0xB8000,
112 0xBC000, 0xC0000, 0xC4000, 0xC8000, 0xCC000, 0xD0000, 0xD4000,
113 0xD8000, 0xDC000, 0xE0000, 0xE4000, 0xE8000, 0xEC000
114 };
115
116 static u32 cycx_2x_irq_options[] = { 7, 3, 5, 9, 10, 11, 12, 15 };
117
118 /* Kernel Loadable Module Entry Points */
119 /* Module 'insert' entry point.
120 * o print announcement
121 * o initialize static data
122 *
123 * Return: 0 Ok
124 * < 0 error.
125 * Context: process */
126
cycx_drv_init(void)127 int __init cycx_drv_init(void)
128 {
129 printk(KERN_INFO "%s v%u.%u %s\n", fullname, MOD_VERSION, MOD_RELEASE,
130 copyright);
131
132 return 0;
133 }
134
135 /* Module 'remove' entry point.
136 * o release all remaining system resources */
cycx_drv_cleanup(void)137 void cycx_drv_cleanup(void)
138 {
139 }
140
141 /* Kernel APIs */
142 /* Set up adapter.
143 * o detect adapter type
144 * o verify hardware configuration options
145 * o check for hardware conflicts
146 * o set up adapter shared memory
147 * o test adapter memory
148 * o load firmware
149 * Return: 0 ok.
150 * < 0 error */
151 EXPORT_SYMBOL(cycx_setup);
cycx_setup(cycxhw_t * hw,void * cfm,u32 len)152 int cycx_setup(cycxhw_t *hw, void *cfm, u32 len)
153 {
154 unsigned long dpmbase = hw->dpmbase;
155 int err;
156
157 /* Verify IRQ configuration options */
158 if (!get_option_index(cycx_2x_irq_options, hw->irq)) {
159 printk(KERN_ERR "%s: IRQ %d is illegal!\n", modname, hw->irq);
160 return -EINVAL;
161 }
162
163 /* Setup adapter dual-port memory window and test memory */
164 if (!hw->dpmbase) {
165 printk(KERN_ERR "%s: you must specify the dpm address!\n",
166 modname);
167 return -EINVAL;
168 } else if (!get_option_index(cyc2x_dpmbase_options, hw->dpmbase)) {
169 printk(KERN_ERR "%s: memory address 0x%lX is illegal!\n",
170 modname, dpmbase);
171 return -EINVAL;
172 }
173
174 hw->dpmbase = (u32)ioremap(dpmbase, CYCX_WINDOWSIZE);
175 hw->dpmsize = CYCX_WINDOWSIZE;
176
177 if (!detect_cyc2x(hw->dpmbase)) {
178 printk(KERN_ERR "%s: adapter Cyclom 2X not found at "
179 "address 0x%lX!\n", modname, dpmbase);
180 return -EINVAL;
181 }
182
183 printk(KERN_INFO "%s: found Cyclom 2X card at address 0x%lX.\n",
184 modname, dpmbase);
185
186 /* Load firmware. If loader fails then shut down adapter */
187 err = load_cyc2x(hw, cfm, len);
188
189 if (err)
190 cycx_down(hw); /* shutdown adapter */
191
192 return err;
193 }
194
195 EXPORT_SYMBOL(cycx_down);
cycx_down(cycxhw_t * hw)196 int cycx_down(cycxhw_t *hw)
197 {
198 iounmap((u32 *)hw->dpmbase);
199
200 return 0;
201 }
202
203 /* Enable interrupt generation. */
204 EXPORT_SYMBOL(cycx_inten);
cycx_inten(cycxhw_t * hw)205 void cycx_inten(cycxhw_t *hw)
206 {
207 cyc2x_writeb(0, hw->dpmbase);
208 }
209
210 /* Generate an interrupt to adapter's CPU. */
211 EXPORT_SYMBOL(cycx_intr);
cycx_intr(cycxhw_t * hw)212 void cycx_intr(cycxhw_t *hw)
213 {
214 cyc2x_writew(0, hw->dpmbase + GEN_CYCX_INTR);
215 }
216
217 /* Execute Adapter Command.
218 * o Set exec flag.
219 * o Busy-wait until flag is reset. */
220 EXPORT_SYMBOL(cycx_exec);
cycx_exec(u32 addr)221 int cycx_exec(u32 addr)
222 {
223 u16 i = 0;
224 /* wait till addr content is zeroed */
225
226 while (cyc2x_readw(addr)) {
227 udelay(1000);
228
229 if (++i > 50)
230 return -1;
231 }
232
233 return 0;
234 }
235
236 /* Read absolute adapter memory.
237 * Transfer data from adapter's memory to data buffer. */
238 EXPORT_SYMBOL(cycx_peek);
cycx_peek(cycxhw_t * hw,u32 addr,void * buf,u32 len)239 int cycx_peek(cycxhw_t *hw, u32 addr, void *buf, u32 len)
240 {
241 if (len == 1)
242 *(u8*)buf = cyc2x_readb(hw->dpmbase + addr);
243 else
244 cyc2x_memcpy_fromio(buf, hw->dpmbase + addr, len);
245
246 return 0;
247 }
248
249 /* Write Absolute Adapter Memory.
250 * Transfer data from data buffer to adapter's memory. */
251 EXPORT_SYMBOL(cycx_poke);
cycx_poke(cycxhw_t * hw,u32 addr,void * buf,u32 len)252 int cycx_poke(cycxhw_t *hw, u32 addr, void *buf, u32 len)
253 {
254 if (len == 1)
255 cyc2x_writeb(*(u8*)buf, hw->dpmbase + addr);
256 else
257 cyc2x_memcpy_toio(hw->dpmbase + addr, buf, len);
258
259 return 0;
260 }
261
262 /* Hardware-Specific Functions */
263
264 /* Load Aux Routines */
265 /* Reset board hardware.
266 return 1 if memory exists at addr and 0 if not. */
memory_exists(u32 addr)267 static int memory_exists(u32 addr)
268 {
269 int tries = 0;
270
271 for (; tries < 3 ; tries++) {
272 cyc2x_writew(TEST_PATTERN, addr + 0x10);
273
274 if (cyc2x_readw(addr + 0x10) == TEST_PATTERN)
275 if (cyc2x_readw(addr + 0x10) == TEST_PATTERN)
276 return 1;
277
278 delay_cycx(1);
279 }
280
281 return 0;
282 }
283
284 /* Load reset code. */
reset_load(u32 addr,u8 * buffer,u32 cnt)285 static void reset_load(u32 addr, u8 *buffer, u32 cnt)
286 {
287 u32 pt_code = addr + RESET_OFFSET;
288 u16 i; /*, j; */
289
290 for (i = 0 ; i < cnt ; i++) {
291 /* for (j = 0 ; j < 50 ; j++); Delay - FIXME busy waiting... */
292 cyc2x_writeb(*buffer++, pt_code++);
293 }
294 }
295
296 /* Load buffer using boot interface.
297 * o copy data from buffer to Cyclom-X memory
298 * o wait for reset code to copy it to right portion of memory */
buffer_load(u32 addr,u8 * buffer,u32 cnt)299 static int buffer_load(u32 addr, u8 *buffer, u32 cnt)
300 {
301 cyc2x_memcpy_toio(addr + DATA_OFFSET, buffer, cnt);
302 cyc2x_writew(GEN_BOOT_DAT, addr + CMD_OFFSET);
303
304 return wait_cyc(addr);
305 }
306
307 /* Set up entry point and kick start Cyclom-X CPU. */
cycx_start(u32 addr)308 static void cycx_start(u32 addr)
309 {
310 /* put in 0x30 offset the jump instruction to the code entry point */
311 cyc2x_writeb(0xea, addr + 0x30);
312 cyc2x_writeb(0x00, addr + 0x31);
313 cyc2x_writeb(0xc4, addr + 0x32);
314 cyc2x_writeb(0x00, addr + 0x33);
315 cyc2x_writeb(0x00, addr + 0x34);
316
317 /* cmd to start executing code */
318 cyc2x_writew(GEN_START, addr + CMD_OFFSET);
319 }
320
321 /* Load and boot reset code. */
cycx_reset_boot(u32 addr,u8 * code,u32 len)322 static void cycx_reset_boot(u32 addr, u8 *code, u32 len)
323 {
324 u32 pt_start = addr + START_OFFSET;
325
326 cyc2x_writeb(0xea, pt_start++); /* jmp to f000:3f00 */
327 cyc2x_writeb(0x00, pt_start++);
328 cyc2x_writeb(0xfc, pt_start++);
329 cyc2x_writeb(0x00, pt_start++);
330 cyc2x_writeb(0xf0, pt_start);
331 reset_load(addr, code, len);
332
333 /* 80186 was in hold, go */
334 cyc2x_writeb(0, addr + START_CPU);
335 delay_cycx(1);
336 }
337
338 /* Load data.bin file through boot (reset) interface. */
cycx_data_boot(u32 addr,u8 * code,u32 len)339 static int cycx_data_boot(u32 addr, u8 *code, u32 len)
340 {
341 u32 pt_boot_cmd = addr + CMD_OFFSET;
342 u32 i;
343
344 /* boot buffer lenght */
345 cyc2x_writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
346 cyc2x_writew(GEN_DEFPAR, pt_boot_cmd);
347
348 if (wait_cyc(addr) < 0)
349 return -1;
350
351 cyc2x_writew(0, pt_boot_cmd + sizeof(u16));
352 cyc2x_writew(0x4000, pt_boot_cmd + 2 * sizeof(u16));
353 cyc2x_writew(GEN_SET_SEG, pt_boot_cmd);
354
355 if (wait_cyc(addr) < 0)
356 return -1;
357
358 for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
359 if (buffer_load(addr, code + i,
360 MIN(CFM_LOAD_BUFSZ, (len - i))) < 0) {
361 printk(KERN_ERR "%s: Error !!\n", modname);
362 return -1;
363 }
364
365 return 0;
366 }
367
368
369 /* Load code.bin file through boot (reset) interface. */
cycx_code_boot(u32 addr,u8 * code,u32 len)370 static int cycx_code_boot(u32 addr, u8 *code, u32 len)
371 {
372 u32 pt_boot_cmd = addr + CMD_OFFSET;
373 u32 i;
374
375 /* boot buffer lenght */
376 cyc2x_writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
377 cyc2x_writew(GEN_DEFPAR, pt_boot_cmd);
378
379 if (wait_cyc(addr) < 0)
380 return -1;
381
382 cyc2x_writew(0x0000, pt_boot_cmd + sizeof(u16));
383 cyc2x_writew(0xc400, pt_boot_cmd + 2 * sizeof(u16));
384 cyc2x_writew(GEN_SET_SEG, pt_boot_cmd);
385
386 if (wait_cyc(addr) < 0)
387 return -1;
388
389 for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
390 if (buffer_load(addr, code + i,MIN(CFM_LOAD_BUFSZ,(len - i)))) {
391 printk(KERN_ERR "%s: Error !!\n", modname);
392 return -1;
393 }
394
395 return 0;
396 }
397
398 /* Load adapter from the memory image of the CYCX firmware module.
399 * o verify firmware integrity and compatibility
400 * o start adapter up */
load_cyc2x(cycxhw_t * hw,cfm_t * cfm,u32 len)401 static int load_cyc2x(cycxhw_t *hw, cfm_t *cfm, u32 len)
402 {
403 int i, j;
404 cycx_header_t *img_hdr;
405 u8 *reset_image,
406 *data_image,
407 *code_image;
408 u32 pt_cycld = hw->dpmbase + 0x400;
409 u16 cksum;
410
411 /* Announce */
412 printk(KERN_INFO "%s: firmware signature=\"%s\"\n", modname,
413 cfm->signature);
414
415 /* Verify firmware signature */
416 if (strcmp(cfm->signature, CFM_SIGNATURE)) {
417 printk(KERN_ERR "%s:load_cyc2x: not Cyclom-2X firmware!\n",
418 modname);
419 return -EINVAL;
420 }
421
422 printk(KERN_INFO "%s: firmware version=%u\n", modname, cfm->version);
423
424 /* Verify firmware module format version */
425 if (cfm->version != CFM_VERSION) {
426 printk(KERN_ERR "%s:%s: firmware format %u rejected! "
427 "Expecting %u.\n",
428 modname, __FUNCTION__, cfm->version, CFM_VERSION);
429 return -EINVAL;
430 }
431
432 /* Verify firmware module length and checksum */
433 cksum = checksum((u8*)&cfm->info, sizeof(cfm_info_t) +
434 cfm->info.codesize);
435 /*
436 FIXME cfm->info.codesize is off by 2
437 if (((len - sizeof(cfm_t) - 1) != cfm->info.codesize) ||
438 */
439 if (cksum != cfm->checksum) {
440 printk(KERN_ERR "%s:%s: firmware corrupted!\n",
441 modname, __FUNCTION__);
442 printk(KERN_ERR " cdsize = 0x%x (expected 0x%lx)\n",
443 len - sizeof(cfm_t) - 1, cfm->info.codesize);
444 printk(KERN_ERR " chksum = 0x%x (expected 0x%x)\n",
445 cksum, cfm->checksum);
446 return -EINVAL;
447 }
448
449 /* If everything is ok, set reset, data and code pointers */
450
451 img_hdr = (cycx_header_t*)(((u8*)cfm) + sizeof(cfm_t) - 1);
452 #ifdef FIRMWARE_DEBUG
453 printk(KERN_INFO "%s:%s: image sizes\n", __FUNCTION__, modname);
454 printk(KERN_INFO " reset=%lu\n", img_hdr->reset_size);
455 printk(KERN_INFO " data=%lu\n", img_hdr->data_size);
456 printk(KERN_INFO " code=%lu\n", img_hdr->code_size);
457 #endif
458 reset_image = ((u8 *)img_hdr) + sizeof(cycx_header_t);
459 data_image = reset_image + img_hdr->reset_size;
460 code_image = data_image + img_hdr->data_size;
461
462 /*---- Start load ----*/
463 /* Announce */
464 printk(KERN_INFO "%s: loading firmware %s (ID=%u)...\n", modname,
465 cfm->descr[0] ? cfm->descr : "unknown firmware",
466 cfm->info.codeid);
467
468 for (i = 0 ; i < 5 ; i++) {
469 /* Reset Cyclom hardware */
470 if (!reset_cyc2x(hw->dpmbase)) {
471 printk(KERN_ERR "%s: dpm problem or board not found\n",
472 modname);
473 return -EINVAL;
474 }
475
476 /* Load reset.bin */
477 cycx_reset_boot(hw->dpmbase, reset_image, img_hdr->reset_size);
478 /* reset is waiting for boot */
479 cyc2x_writew(GEN_POWER_ON, pt_cycld);
480 delay_cycx(1);
481
482 for (j = 0 ; j < 3 ; j++)
483 if (!cyc2x_readw(pt_cycld))
484 goto reset_loaded;
485 else
486 delay_cycx(1);
487 }
488
489 printk(KERN_ERR "%s: reset not started.\n", modname);
490 return -EINVAL;
491
492 reset_loaded:
493 /* Load data.bin */
494 if (cycx_data_boot(hw->dpmbase, data_image, img_hdr->data_size)) {
495 printk(KERN_ERR "%s: cannot load data file.\n", modname);
496 return -EINVAL;
497 }
498
499 /* Load code.bin */
500 if (cycx_code_boot(hw->dpmbase, code_image, img_hdr->code_size)) {
501 printk(KERN_ERR "%s: cannot load code file.\n", modname);
502 return -EINVAL;
503 }
504
505 /* Prepare boot-time configuration data */
506 cycx_bootcfg(hw);
507
508 /* kick-off CPU */
509 cycx_start(hw->dpmbase);
510
511 /* Arthur Ganzert's tip: wait a while after the firmware loading...
512 seg abr 26 17:17:12 EST 1999 - acme */
513 delay_cycx(7);
514 printk(KERN_INFO "%s: firmware loaded!\n", modname);
515
516 /* enable interrupts */
517 cycx_inten(hw);
518
519 return 0;
520 }
521
522 /* Prepare boot-time firmware configuration data.
523 * o initialize configuration data area
524 From async.doc - V_3.4.0 - 07/18/1994
525 - As of now, only static buffers are available to the user.
526 So, the bit VD_RXDIRC must be set in 'valid'. That means that user
527 wants to use the static transmission and reception buffers. */
cycx_bootcfg(cycxhw_t * hw)528 static void cycx_bootcfg(cycxhw_t *hw)
529 {
530 /* use fixed buffers */
531 cyc2x_writeb(FIXED_BUFFERS, hw->dpmbase + CONF_OFFSET);
532 }
533
534 /* Detect Cyclom 2x adapter.
535 * Following tests are used to detect Cyclom 2x adapter:
536 * to be completed based on the tests done below
537 * Return 1 if detected o.k. or 0 if failed.
538 * Note: This test is destructive! Adapter will be left in shutdown
539 * state after the test. */
detect_cyc2x(u32 addr)540 static int detect_cyc2x(u32 addr)
541 {
542 reset_cyc2x(addr);
543
544 return memory_exists(addr);
545 }
546
547 /* Miscellaneous */
548 /* Get option's index into the options list.
549 * Return option's index (1 .. N) or zero if option is invalid. */
get_option_index(u32 * optlist,u32 optval)550 static int get_option_index(u32 *optlist, u32 optval)
551 {
552 int i = 1;
553
554 for (; i <= optlist[0]; ++i)
555 if (optlist[i] == optval)
556 return i;
557
558 return 0;
559 }
560
561 /* Reset adapter's CPU. */
reset_cyc2x(u32 addr)562 static int reset_cyc2x(u32 addr)
563 {
564 cyc2x_writeb(0, addr + RST_ENABLE);
565 delay_cycx(2);
566 cyc2x_writeb(0, addr + RST_DISABLE);
567 delay_cycx(2);
568
569 return memory_exists(addr);
570 }
571
572 /* Delay */
delay_cycx(int sec)573 static void delay_cycx(int sec)
574 {
575 set_current_state(TASK_INTERRUPTIBLE);
576 schedule_timeout(sec*HZ);
577 }
578
579 /* Calculate 16-bit CRC using CCITT polynomial. */
checksum(u8 * buf,u32 len)580 static u16 checksum(u8 *buf, u32 len)
581 {
582 u16 crc = 0;
583 u16 mask, flag;
584
585 for (; len; --len, ++buf)
586 for (mask = 0x80; mask; mask >>= 1) {
587 flag = (crc & 0x8000);
588 crc <<= 1;
589 crc |= ((*buf & mask) ? 1 : 0);
590
591 if (flag)
592 crc ^= 0x1021;
593 }
594
595 return crc;
596 }
597
598 module_init(cycx_drv_init);
599 module_exit(cycx_drv_cleanup);
600
601 /* End */
602