1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78#undef A2232_SPEEDHACK
79
80
81
82#define A2232_SUPPRESS_RTSCTS_WARNING
83
84
85
86#include <linux/module.h>
87
88#include <linux/types.h>
89#include <linux/interrupt.h>
90#include <linux/kernel.h>
91#include <linux/errno.h>
92#include <linux/sched.h>
93#include <linux/tty.h>
94
95#include <asm/setup.h>
96#include <asm/amigaints.h>
97#include <asm/amigahw.h>
98#include <linux/zorro.h>
99#include <asm/irq.h>
100#include <linux/mutex.h>
101
102#include <linux/delay.h>
103
104#include <linux/serial.h>
105#include <linux/generic_serial.h>
106#include <linux/tty_flip.h>
107
108#include "ser_a2232.h"
109#include "ser_a2232fw.h"
110
111
112
113
114static irqreturn_t a2232_vbl_inter(int irq, void *data);
115
116static void a2232_init_portstructs(void);
117
118
119static int a2232_init_drivers(void);
120
121
122static void a2232_disable_tx_interrupts(void *ptr);
123static void a2232_enable_tx_interrupts(void *ptr);
124static void a2232_disable_rx_interrupts(void *ptr);
125static void a2232_enable_rx_interrupts(void *ptr);
126static int a2232_carrier_raised(struct tty_port *port);
127static void a2232_shutdown_port(void *ptr);
128static int a2232_set_real_termios(void *ptr);
129static int a2232_chars_in_buffer(void *ptr);
130static void a2232_close(void *ptr);
131static void a2232_hungup(void *ptr);
132
133
134
135
136static int a2232_ioctl(struct tty_struct *tty, struct file *file,
137 unsigned int cmd, unsigned long arg);
138static void a2232_throttle(struct tty_struct *tty);
139static void a2232_unthrottle(struct tty_struct *tty);
140static int a2232_open(struct tty_struct * tty, struct file * filp);
141
142
143
144
145
146
147static struct real_driver a2232_real_driver = {
148 a2232_disable_tx_interrupts,
149 a2232_enable_tx_interrupts,
150 a2232_disable_rx_interrupts,
151 a2232_enable_rx_interrupts,
152 a2232_shutdown_port,
153 a2232_set_real_termios,
154 a2232_chars_in_buffer,
155 a2232_close,
156 a2232_hungup,
157 NULL
158};
159
160static void *a2232_driver_ID = &a2232_driver_ID;
161
162
163static struct a2232_port a2232_ports[MAX_A2232_BOARDS*NUMLINES];
164
165
166static struct tty_driver *a2232_driver;
167
168
169static int nr_a2232;
170
171
172static struct zorro_dev *zd_a2232[MAX_A2232_BOARDS];
173
174
175
176
177static inline volatile struct a2232memory *a2232mem(unsigned int board)
178{
179 return (volatile struct a2232memory *)ZTWO_VADDR(zd_a2232[board]->resource.start);
180}
181
182static inline volatile struct a2232status *a2232stat(unsigned int board,
183 unsigned int portonboard)
184{
185 volatile struct a2232memory *mem = a2232mem(board);
186 return &(mem->Status[portonboard]);
187}
188
189static inline void a2232_receive_char(struct a2232_port *port, int ch, int err)
190{
191
192
193
194
195 struct tty_struct *tty = port->gs.port.tty;
196
197#if 0
198 switch(err) {
199 case TTY_BREAK:
200 break;
201 case TTY_PARITY:
202 break;
203 case TTY_OVERRUN:
204 break;
205 case TTY_FRAME:
206 break;
207 }
208#endif
209
210 tty_insert_flip_char(tty, ch, err);
211 tty_flip_buffer_push(tty);
212}
213
214
215
216
217static void a2232_disable_tx_interrupts(void *ptr)
218{
219 struct a2232_port *port;
220 volatile struct a2232status *stat;
221 unsigned long flags;
222
223 port = ptr;
224 stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
225 stat->OutDisable = -1;
226
227
228 local_irq_save(flags);
229 port->gs.port.flags &= ~GS_TX_INTEN;
230 local_irq_restore(flags);
231}
232
233static void a2232_enable_tx_interrupts(void *ptr)
234{
235 struct a2232_port *port;
236 volatile struct a2232status *stat;
237 unsigned long flags;
238
239 port = ptr;
240 stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
241 stat->OutDisable = 0;
242
243
244 local_irq_save(flags);
245 port->gs.port.flags |= GS_TX_INTEN;
246 local_irq_restore(flags);
247}
248
249static void a2232_disable_rx_interrupts(void *ptr)
250{
251 struct a2232_port *port;
252 port = ptr;
253 port->disable_rx = -1;
254}
255
256static void a2232_enable_rx_interrupts(void *ptr)
257{
258 struct a2232_port *port;
259 port = ptr;
260 port->disable_rx = 0;
261}
262
263static int a2232_carrier_raised(struct tty_port *port)
264{
265 struct a2232_port *ap = container_of(port, struct a2232_port, gs.port);
266 return ap->cd_status;
267}
268
269static void a2232_shutdown_port(void *ptr)
270{
271 struct a2232_port *port;
272 volatile struct a2232status *stat;
273 unsigned long flags;
274
275 port = ptr;
276 stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
277
278 local_irq_save(flags);
279
280 port->gs.port.flags &= ~GS_ACTIVE;
281
282 if (port->gs.port.tty && port->gs.port.tty->termios->c_cflag & HUPCL) {
283
284
285 stat->Command = ( (stat->Command & ~A2232CMD_CMask) |
286 A2232CMD_Close );
287 stat->OutFlush = -1;
288 stat->Setup = -1;
289 }
290
291 local_irq_restore(flags);
292
293
294
295
296
297
298
299
300}
301
302static int a2232_set_real_termios(void *ptr)
303{
304 unsigned int cflag, baud, chsize, stopb, parity, softflow;
305 int rate;
306 int a2232_param, a2232_cmd;
307 unsigned long flags;
308 unsigned int i;
309 struct a2232_port *port = ptr;
310 volatile struct a2232status *status;
311 volatile struct a2232memory *mem;
312
313 if (!port->gs.port.tty || !port->gs.port.tty->termios) return 0;
314
315 status = a2232stat(port->which_a2232, port->which_port_on_a2232);
316 mem = a2232mem(port->which_a2232);
317
318 a2232_param = a2232_cmd = 0;
319
320
321 baud = port->gs.baud;
322 if (baud == 0) {
323
324 local_irq_save(flags);
325
326 status->Command = ( (status->Command & ~A2232CMD_CMask) |
327 A2232CMD_Close );
328 status->OutFlush = -1;
329 status->Setup = -1;
330
331 local_irq_restore(flags);
332 return 0;
333 }
334
335 rate = A2232_BAUD_TABLE_NOAVAIL;
336 for (i=0; i < A2232_BAUD_TABLE_NUM_RATES * 3; i += 3){
337 if (a2232_baud_table[i] == baud){
338 if (mem->Common.Crystal == A2232_TURBO) rate = a2232_baud_table[i+2];
339 else rate = a2232_baud_table[i+1];
340 }
341 }
342 if (rate == A2232_BAUD_TABLE_NOAVAIL){
343 printk("a2232: Board %d Port %d unsupported baud rate: %d baud. Using another.\n",port->which_a2232,port->which_port_on_a2232,baud);
344
345 rate = A2232PARAM_B9600;
346 }
347 a2232_param |= rate;
348
349 cflag = port->gs.port.tty->termios->c_cflag;
350
351
352 chsize = cflag & CSIZE;
353 switch (chsize){
354 case CS8: a2232_param |= A2232PARAM_8Bit; break;
355 case CS7: a2232_param |= A2232PARAM_7Bit; break;
356 case CS6: a2232_param |= A2232PARAM_6Bit; break;
357 case CS5: a2232_param |= A2232PARAM_5Bit; break;
358 default: printk("a2232: Board %d Port %d unsupported character size: %d. Using 8 data bits.\n",
359 port->which_a2232,port->which_port_on_a2232,chsize);
360 a2232_param |= A2232PARAM_8Bit; break;
361 }
362
363
364 stopb = cflag & CSTOPB;
365 if (stopb){
366 printk("a2232: Board %d Port %d 2 stop bits unsupported. Using 1 stop bit.\n",
367 port->which_a2232,port->which_port_on_a2232);
368 }
369
370
371 if (!(cflag & CRTSCTS)){
372#ifndef A2232_SUPPRESS_RTSCTS_WARNING
373 printk("a2232: Board %d Port %d cannot switch off firmware-implemented RTS/CTS hardware flow control.\n",
374 port->which_a2232,port->which_port_on_a2232);
375#endif
376 }
377
378
379
380
381
382
383
384
385
386 softflow = !!( port->gs.port.tty->termios->c_iflag & IXOFF );
387
388
389 parity = cflag & (PARENB | PARODD);
390 if (parity & PARENB){
391 if (parity & PARODD){
392 a2232_cmd |= A2232CMD_OddParity;
393 }
394 else{
395 a2232_cmd |= A2232CMD_EvenParity;
396 }
397 }
398 else a2232_cmd |= A2232CMD_NoParity;
399
400
401
402
403 if (cflag & CLOCAL)
404 port->gs.port.flags &= ~ASYNC_CHECK_CD;
405 else
406 port->gs.port.flags |= ASYNC_CHECK_CD;
407
408
409
410 local_irq_save(flags);
411
412 status->Param = a2232_param | A2232PARAM_RcvBaud;
413 status->Command = a2232_cmd | A2232CMD_Open | A2232CMD_Enable;
414 status->SoftFlow = softflow;
415 status->OutDisable = 0;
416 status->Setup = -1;
417
418 local_irq_restore(flags);
419 return 0;
420}
421
422static int a2232_chars_in_buffer(void *ptr)
423{
424 struct a2232_port *port;
425 volatile struct a2232status *status;
426 unsigned char ret;
427 port = ptr;
428 status = a2232stat(port->which_a2232, port->which_port_on_a2232);
429#if A2232_IOBUFLEN != 256
430#error "Re-Implement a2232_chars_in_buffer()!"
431#endif
432 ret = (status->OutHead - status->OutTail);
433 return ret;
434}
435
436static void a2232_close(void *ptr)
437{
438 a2232_disable_tx_interrupts(ptr);
439 a2232_disable_rx_interrupts(ptr);
440
441}
442
443static void a2232_hungup(void *ptr)
444{
445 a2232_close(ptr);
446}
447
448
449
450static int a2232_ioctl( struct tty_struct *tty, struct file *file,
451 unsigned int cmd, unsigned long arg)
452{
453 return -ENOIOCTLCMD;
454}
455
456static void a2232_throttle(struct tty_struct *tty)
457{
458
459
460
461
462
463
464 struct a2232_port *port = tty->driver_data;
465 port->throttle_input = -1;
466}
467
468static void a2232_unthrottle(struct tty_struct *tty)
469{
470
471 struct a2232_port *port = tty->driver_data;
472 port->throttle_input = 0;
473}
474
475static int a2232_open(struct tty_struct * tty, struct file * filp)
476{
477
478 int line;
479 int retval;
480 struct a2232_port *port;
481
482 line = tty->index;
483 port = &a2232_ports[line];
484
485 tty->driver_data = port;
486 port->gs.port.tty = tty;
487 port->gs.port.count++;
488 retval = gs_init_port(&port->gs);
489 if (retval) {
490 port->gs.port.count--;
491 return retval;
492 }
493 port->gs.port.flags |= GS_ACTIVE;
494 retval = gs_block_til_ready(port, filp);
495
496 if (retval) {
497 port->gs.port.count--;
498 return retval;
499 }
500
501 a2232_enable_rx_interrupts(port);
502
503 return 0;
504}
505
506
507static irqreturn_t a2232_vbl_inter(int irq, void *data)
508{
509#if A2232_IOBUFLEN != 256
510#error "Re-Implement a2232_vbl_inter()!"
511#endif
512
513struct a2232_port *port;
514volatile struct a2232memory *mem;
515volatile struct a2232status *status;
516unsigned char newhead;
517unsigned char bufpos;
518unsigned char ncd, ocd, ccd;
519volatile u_char *ibuf, *cbuf, *obuf;
520int ch, err, n, p;
521 for (n = 0; n < nr_a2232; n++){
522 mem = a2232mem(n);
523 for (p = 0; p < NUMLINES; p++){
524 err = 0;
525 port = &a2232_ports[n*NUMLINES+p];
526 if ( port->gs.port.flags & GS_ACTIVE ){
527
528 status = a2232stat(n,p);
529
530 if (!port->disable_rx && !port->throttle_input){
531 newhead = status->InHead;
532 bufpos = status->InTail;
533
534
535 if (newhead != bufpos) {
536
537 ibuf = mem->InBuf[p];
538
539
540 cbuf = mem->InCtl[p];
541
542
543 while (bufpos != newhead) {
544
545 switch (cbuf[bufpos]) {
546
547 case A2232INCTL_EVENT:
548 switch (ibuf[bufpos++]) {
549 case A2232EVENT_Break:
550
551 break;
552
553
554 case A2232EVENT_Sync:
555 printk("A2232: 65EC02 software sent SYNC event, don't know what to do. Ignoring.");
556 break;
557 default:
558 printk("A2232: 65EC02 software broken, unknown event type %d occurred.\n",ibuf[bufpos-1]);
559 }
560 break;
561 case A2232INCTL_CHAR:
562
563 a2232_receive_char(port, ibuf[bufpos], err);
564 bufpos++;
565 break;
566 default:
567 printk("A2232: 65EC02 software broken, unknown data type %d occurred.\n",cbuf[bufpos]);
568 bufpos++;
569 }
570 }
571
572 status->InTail = bufpos;
573
574 }
575 }
576
577
578 obuf = mem->OutBuf[p];
579 bufpos = status->OutHead;
580 while ( (port->gs.xmit_cnt > 0) &&
581 (!port->gs.port.tty->stopped) &&
582 (!port->gs.port.tty->hw_stopped) ){
583 if (((bufpos+1) & A2232_IOBUFLENMASK) != status->OutTail) {
584 ch = port->gs.xmit_buf[port->gs.xmit_tail];
585 port->gs.xmit_tail = (port->gs.xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
586 obuf[bufpos++] = ch;
587 port->gs.xmit_cnt--;
588 }
589 else{
590 break;
591 }
592 }
593 status->OutHead = bufpos;
594
595
596 if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.port.tty) {
597 tty_wakeup(port->gs.port.tty);
598 }
599 }
600 }
601
602
603 newhead = mem->Common.CDHead;
604 bufpos = mem->Common.CDTail;
605 if (newhead != bufpos){
606 ocd = mem->Common.CDStatus;
607 while (newhead != bufpos){
608 ncd = mem->CDBuf[bufpos++];
609 ccd = ncd ^ ocd;
610 ocd = ncd;
611 for(p=0; p < NUMLINES; p++){
612 if (ccd & 1){
613
614 struct a2232_port *port = &a2232_ports[n*7+p];
615 port->cd_status = !(ncd & 1);
616
617 if (!(port->gs.port.flags & ASYNC_CHECK_CD))
618 ;
619 else if (port->cd_status) {
620
621
622 wake_up_interruptible(&port->gs.port.open_wait);
623 }
624 else {
625 if (port->gs.port.tty)
626 tty_hangup (port->gs.port.tty);
627 }
628
629 }
630 ccd >>= 1;
631 ncd >>= 1;
632 }
633 }
634 mem->Common.CDStatus = ocd;
635 mem->Common.CDTail = bufpos;
636 }
637
638 }
639 return IRQ_HANDLED;
640}
641
642static const struct tty_port_operations a2232_port_ops = {
643 .carrier_raised = a2232_carrier_raised,
644};
645
646static void a2232_init_portstructs(void)
647{
648 struct a2232_port *port;
649 int i;
650
651 for (i = 0; i < MAX_A2232_BOARDS*NUMLINES; i++) {
652 port = a2232_ports + i;
653 tty_port_init(&port->gs.port);
654 port->gs.port.ops = &a2232_port_ops;
655 port->which_a2232 = i/NUMLINES;
656 port->which_port_on_a2232 = i%NUMLINES;
657 port->disable_rx = port->throttle_input = port->cd_status = 0;
658 port->gs.magic = A2232_MAGIC;
659 port->gs.close_delay = HZ/2;
660 port->gs.closing_wait = 30 * HZ;
661 port->gs.rd = &a2232_real_driver;
662 }
663}
664
665static const struct tty_operations a2232_ops = {
666 .open = a2232_open,
667 .close = gs_close,
668 .write = gs_write,
669 .put_char = gs_put_char,
670 .flush_chars = gs_flush_chars,
671 .write_room = gs_write_room,
672 .chars_in_buffer = gs_chars_in_buffer,
673 .flush_buffer = gs_flush_buffer,
674 .ioctl = a2232_ioctl,
675 .throttle = a2232_throttle,
676 .unthrottle = a2232_unthrottle,
677 .set_termios = gs_set_termios,
678 .stop = gs_stop,
679 .start = gs_start,
680 .hangup = gs_hangup,
681};
682
683static int a2232_init_drivers(void)
684{
685 int error;
686
687 a2232_driver = alloc_tty_driver(NUMLINES * nr_a2232);
688 if (!a2232_driver)
689 return -ENOMEM;
690 a2232_driver->owner = THIS_MODULE;
691 a2232_driver->driver_name = "commodore_a2232";
692 a2232_driver->name = "ttyY";
693 a2232_driver->major = A2232_NORMAL_MAJOR;
694 a2232_driver->type = TTY_DRIVER_TYPE_SERIAL;
695 a2232_driver->subtype = SERIAL_TYPE_NORMAL;
696 a2232_driver->init_termios = tty_std_termios;
697 a2232_driver->init_termios.c_cflag =
698 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
699 a2232_driver->init_termios.c_ispeed = 9600;
700 a2232_driver->init_termios.c_ospeed = 9600;
701 a2232_driver->flags = TTY_DRIVER_REAL_RAW;
702 tty_set_operations(a2232_driver, &a2232_ops);
703 if ((error = tty_register_driver(a2232_driver))) {
704 printk(KERN_ERR "A2232: Couldn't register A2232 driver, error = %d\n",
705 error);
706 put_tty_driver(a2232_driver);
707 return 1;
708 }
709 return 0;
710}
711
712static int __init a2232board_init(void)
713{
714 struct zorro_dev *z;
715
716 unsigned int boardaddr;
717 int bcount;
718 short start;
719 u_char *from;
720 volatile u_char *to;
721 volatile struct a2232memory *mem;
722 int error, i;
723
724#ifdef CONFIG_SMP
725 return -ENODEV;
726#endif
727
728 if (!MACH_IS_AMIGA){
729 return -ENODEV;
730 }
731
732 printk("Commodore A2232 driver initializing.\n");
733
734 z = NULL;
735 nr_a2232 = 0;
736 while ( (z = zorro_find_device(ZORRO_WILDCARD, z)) ){
737 if ( (z->id != ZORRO_PROD_CBM_A2232_PROTOTYPE) &&
738 (z->id != ZORRO_PROD_CBM_A2232) ){
739 continue;
740 }
741 if (!zorro_request_device(z,"A2232 driver"))
742 continue;
743
744 printk("Commodore A2232 found (#%d).\n",nr_a2232);
745
746 zd_a2232[nr_a2232] = z;
747
748 boardaddr = ZTWO_VADDR( z->resource.start );
749 printk("Board is located at address 0x%x, size is 0x%x.\n", boardaddr, (unsigned int) ((z->resource.end+1) - (z->resource.start)));
750
751 mem = (volatile struct a2232memory *) boardaddr;
752
753 (void) mem->Enable6502Reset;
754 to = (u_char *)mem; from = a2232_65EC02code; bcount = sizeof(a2232_65EC02code) - 2;
755 start = *(short *)from;
756 from += sizeof(start);
757 to += start;
758 while(bcount--) *to++ = *from++;
759 printk("65EC02 software uploaded to the A2232 memory.\n");
760
761 mem->Common.Crystal = A2232_UNKNOWN;
762
763
764 (void) mem->ResetBoard;
765 printk("A2232's 65EC02 CPU up and running.\n");
766
767
768 for (bcount = 0; bcount < 2000; bcount++) {
769 udelay(1000);
770 if (mem->Common.Crystal)
771 break;
772 }
773 printk((mem->Common.Crystal?"A2232 oscillator crystal detected by 65EC02 software: ":"65EC02 software could not determine A2232 oscillator crystal: "));
774 switch (mem->Common.Crystal){
775 case A2232_UNKNOWN:
776 printk("Unknown crystal.\n");
777 break;
778 case A2232_NORMAL:
779 printk ("Normal crystal.\n");
780 break;
781 case A2232_TURBO:
782 printk ("Turbo crystal.\n");
783 break;
784 default:
785 printk ("0x%x. Huh?\n",mem->Common.Crystal);
786 }
787
788 nr_a2232++;
789
790 }
791
792 printk("Total: %d A2232 boards initialized.\n", nr_a2232);
793
794 a2232_init_portstructs();
795
796
797
798
799
800 if (a2232_init_drivers()) return -ENODEV;
801
802 error = request_irq(IRQ_AMIGA_VERTB, a2232_vbl_inter, 0,
803 "A2232 serial VBL", a2232_driver_ID);
804 if (error) {
805 for (i = 0; i < nr_a2232; i++)
806 zorro_release_device(zd_a2232[i]);
807 tty_unregister_driver(a2232_driver);
808 put_tty_driver(a2232_driver);
809 }
810 return error;
811}
812
813static void __exit a2232board_exit(void)
814{
815 int i;
816
817 for (i = 0; i < nr_a2232; i++) {
818 zorro_release_device(zd_a2232[i]);
819 }
820
821 tty_unregister_driver(a2232_driver);
822 put_tty_driver(a2232_driver);
823 free_irq(IRQ_AMIGA_VERTB, a2232_driver_ID);
824}
825
826module_init(a2232board_init);
827module_exit(a2232board_exit);
828
829MODULE_AUTHOR("Enver Haase");
830MODULE_DESCRIPTION("Amiga A2232 multi-serial board driver");
831MODULE_LICENSE("GPL");
832