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