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