forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path68328serial.c
1472 lines (1244 loc) · 34.4 KB
/
68328serial.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 68328serial.c: Serial port driver for 68328 microcontroller
*
* Copyright (C) 1995 David S. Miller <[email protected]>
* Copyright (C) 1998 Kenneth Albanowski <[email protected]>
* Copyright (C) 1998, 1999 D. Jeff Dionne <[email protected]>
* Copyright (C) 1999 Vladimir Gurevich <[email protected]>
* Copyright (C) 2002-2003 David McCullough <[email protected]>
* Copyright (C) 2002 Greg Ungerer <[email protected]>
*
* VZ Support/Fixes Evan Stawnyczy <[email protected]>
* Multiple UART support Daniel Potts <[email protected]>
* Power management support Daniel Potts <[email protected]>
* VZ Second Serial Port enable Phil Wilshire
* 2.4/2.5 port David McCullough
*/
#include <asm/dbg.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/console.h>
#include <linux/reboot.h>
#include <linux/keyboard.h>
#include <linux/init.h>
#include <linux/pm.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/gfp.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/system.h>
#include <asm/delay.h>
#include <asm/uaccess.h>
/* (es) */
/* note: perhaps we can murge these files, so that you can just
* define 1 of them, and they can sort that out for themselves
*/
#if defined(CONFIG_M68EZ328)
#include <asm/MC68EZ328.h>
#else
#if defined(CONFIG_M68VZ328)
#include <asm/MC68VZ328.h>
#else
#include <asm/MC68328.h>
#endif /* CONFIG_M68VZ328 */
#endif /* CONFIG_M68EZ328 */
#include "68328serial.h"
/* Turn off usage of real serial interrupt code, to "support" Copilot */
#ifdef CONFIG_XCOPILOT_BUGS
#undef USE_INTS
#else
#define USE_INTS
#endif
static struct m68k_serial m68k_soft[NR_PORTS];
static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
/* multiple ports are contiguous in memory */
m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
struct tty_struct m68k_ttys;
struct m68k_serial *m68k_consinfo = 0;
#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
struct tty_driver *serial_driver;
/* number of characters left in xmit buffer before we ask for more */
#define WAKEUP_CHARS 256
/* Debugging... DEBUG_INTR is bad to use when one of the zs
* lines is your console ;(
*/
#undef SERIAL_DEBUG_INTR
#undef SERIAL_DEBUG_OPEN
#undef SERIAL_DEBUG_FLOW
#define RS_ISR_PASS_LIMIT 256
static void change_speed(struct m68k_serial *info);
/*
* Setup for console. Argument comes from the boot command line.
*/
/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
#ifdef CONFIG_M68VZ328
#define CONSOLE_BAUD_RATE 19200
#define DEFAULT_CBAUD B19200
#endif
#ifndef CONSOLE_BAUD_RATE
#define CONSOLE_BAUD_RATE 9600
#define DEFAULT_CBAUD B9600
#endif
static int m68328_console_initted = 0;
static int m68328_console_baud = CONSOLE_BAUD_RATE;
static int m68328_console_cbaud = DEFAULT_CBAUD;
static inline int serial_paranoia_check(struct m68k_serial *info,
char *name, const char *routine)
{
#ifdef SERIAL_PARANOIA_CHECK
static const char *badmagic =
"Warning: bad magic number for serial struct %s in %s\n";
static const char *badinfo =
"Warning: null m68k_serial for %s in %s\n";
if (!info) {
printk(badinfo, name, routine);
return 1;
}
if (info->magic != SERIAL_MAGIC) {
printk(badmagic, name, routine);
return 1;
}
#endif
return 0;
}
/*
* This is used to figure out the divisor speeds and the timeouts
*/
static int baud_table[] = {
0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
9600, 19200, 38400, 57600, 115200, 0 };
/* Sets or clears DTR/RTS on the requested line */
static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
{
if (set) {
/* set the RTS/CTS line */
} else {
/* clear it */
}
return;
}
/* Utility routines */
static inline int get_baud(struct m68k_serial *ss)
{
unsigned long result = 115200;
unsigned short int baud = uart_addr[ss->line].ubaud;
if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
result >>= GET_FIELD(baud, UBAUD_DIVIDE);
return result;
}
/*
* ------------------------------------------------------------
* rs_stop() and rs_start()
*
* This routines are called before setting or resetting tty->stopped.
* They enable or disable transmitter interrupts, as necessary.
* ------------------------------------------------------------
*/
static void rs_stop(struct tty_struct *tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
m68328_uart *uart = &uart_addr[info->line];
unsigned long flags;
if (serial_paranoia_check(info, tty->name, "rs_stop"))
return;
local_irq_save(flags);
uart->ustcnt &= ~USTCNT_TXEN;
local_irq_restore(flags);
}
static int rs_put_char(char ch)
{
int flags, loops = 0;
local_irq_save(flags);
while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
loops++;
udelay(5);
}
UTX_TXDATA = ch;
udelay(5);
local_irq_restore(flags);
return 1;
}
static void rs_start(struct tty_struct *tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
m68328_uart *uart = &uart_addr[info->line];
unsigned long flags;
if (serial_paranoia_check(info, tty->name, "rs_start"))
return;
local_irq_save(flags);
if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
#ifdef USE_INTS
uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
#else
uart->ustcnt |= USTCNT_TXEN;
#endif
}
local_irq_restore(flags);
}
/* Drop into either the boot monitor or kadb upon receiving a break
* from keyboard/console input.
*/
static void batten_down_hatches(void)
{
/* Drop into the debugger */
}
static void status_handle(struct m68k_serial *info, unsigned short status)
{
#if 0
if(status & DCD) {
if((info->port.tty->termios->c_cflag & CRTSCTS) &&
((info->curregs[3] & AUTO_ENAB)==0)) {
info->curregs[3] |= AUTO_ENAB;
info->pendregs[3] |= AUTO_ENAB;
write_zsreg(info->m68k_channel, 3, info->curregs[3]);
}
} else {
if((info->curregs[3] & AUTO_ENAB)) {
info->curregs[3] &= ~AUTO_ENAB;
info->pendregs[3] &= ~AUTO_ENAB;
write_zsreg(info->m68k_channel, 3, info->curregs[3]);
}
}
#endif
/* If this is console input and this is a
* 'break asserted' status change interrupt
* see if we can drop into the debugger
*/
if((status & URX_BREAK) && info->break_abort)
batten_down_hatches();
return;
}
static void receive_chars(struct m68k_serial *info, unsigned short rx)
{
struct tty_struct *tty = info->port.tty;
m68328_uart *uart = &uart_addr[info->line];
unsigned char ch, flag;
/*
* This do { } while() loop will get ALL chars out of Rx FIFO
*/
#ifndef CONFIG_XCOPILOT_BUGS
do {
#endif
ch = GET_FIELD(rx, URX_RXDATA);
if(info->is_cons) {
if(URX_BREAK & rx) { /* whee, break received */
status_handle(info, rx);
return;
#ifdef CONFIG_MAGIC_SYSRQ
} else if (ch == 0x10) { /* ^P */
show_state();
show_free_areas();
show_buffers();
/* show_net_buffers(); */
return;
} else if (ch == 0x12) { /* ^R */
emergency_restart();
return;
#endif /* CONFIG_MAGIC_SYSRQ */
}
}
if(!tty)
goto clear_and_exit;
flag = TTY_NORMAL;
if(rx & URX_PARITY_ERROR) {
flag = TTY_PARITY;
status_handle(info, rx);
} else if(rx & URX_OVRUN) {
flag = TTY_OVERRUN;
status_handle(info, rx);
} else if(rx & URX_FRAME_ERROR) {
flag = TTY_FRAME;
status_handle(info, rx);
}
tty_insert_flip_char(tty, ch, flag);
#ifndef CONFIG_XCOPILOT_BUGS
} while((rx = uart->urx.w) & URX_DATA_READY);
#endif
tty_schedule_flip(tty);
clear_and_exit:
return;
}
static void transmit_chars(struct m68k_serial *info)
{
m68328_uart *uart = &uart_addr[info->line];
if (info->x_char) {
/* Send next char */
uart->utx.b.txdata = info->x_char;
info->x_char = 0;
goto clear_and_return;
}
if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
/* That's peculiar... TX ints off */
uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
goto clear_and_return;
}
/* Send char */
uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
info->xmit_cnt--;
if (info->xmit_cnt < WAKEUP_CHARS)
schedule_work(&info->tqueue);
if(info->xmit_cnt <= 0) {
/* All done for now... TX ints off */
uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
goto clear_and_return;
}
clear_and_return:
/* Clear interrupt (should be auto)*/
return;
}
/*
* This is the serial driver's generic interrupt routine
*/
irqreturn_t rs_interrupt(int irq, void *dev_id)
{
struct m68k_serial *info = dev_id;
m68328_uart *uart;
unsigned short rx;
unsigned short tx;
uart = &uart_addr[info->line];
rx = uart->urx.w;
#ifdef USE_INTS
tx = uart->utx.w;
if (rx & URX_DATA_READY) receive_chars(info, rx);
if (tx & UTX_TX_AVAIL) transmit_chars(info);
#else
receive_chars(info, rx);
#endif
return IRQ_HANDLED;
}
static void do_softint(struct work_struct *work)
{
struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
struct tty_struct *tty;
tty = info->port.tty;
if (!tty)
return;
#if 0
if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
tty_wakeup(tty);
}
#endif
}
/*
* This routine is called from the scheduler tqueue when the interrupt
* routine has signalled that a hangup has occurred. The path of
* hangup processing is:
*
* serial interrupt routine -> (scheduler tqueue) ->
* do_serial_hangup() -> tty->hangup() -> rs_hangup()
*
*/
static void do_serial_hangup(struct work_struct *work)
{
struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
struct tty_struct *tty;
tty = info->port.tty;
if (!tty)
return;
tty_hangup(tty);
}
static int startup(struct m68k_serial * info)
{
m68328_uart *uart = &uart_addr[info->line];
unsigned long flags;
if (info->flags & S_INITIALIZED)
return 0;
if (!info->xmit_buf) {
info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
if (!info->xmit_buf)
return -ENOMEM;
}
local_irq_save(flags);
/*
* Clear the FIFO buffers and disable them
* (they will be reenabled in change_speed())
*/
uart->ustcnt = USTCNT_UEN;
info->xmit_fifo_size = 1;
uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
(void)uart->urx.w;
/*
* Finally, enable sequencing and interrupts
*/
#ifdef USE_INTS
uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
#else
uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
#endif
if (info->port.tty)
clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
/*
* and set the speed of the serial port
*/
change_speed(info);
info->flags |= S_INITIALIZED;
local_irq_restore(flags);
return 0;
}
/*
* This routine will shutdown a serial port; interrupts are disabled, and
* DTR is dropped if the hangup on close termio flag is on.
*/
static void shutdown(struct m68k_serial * info)
{
m68328_uart *uart = &uart_addr[info->line];
unsigned long flags;
uart->ustcnt = 0; /* All off! */
if (!(info->flags & S_INITIALIZED))
return;
local_irq_save(flags);
if (info->xmit_buf) {
free_page((unsigned long) info->xmit_buf);
info->xmit_buf = 0;
}
if (info->port.tty)
set_bit(TTY_IO_ERROR, &info->port.tty->flags);
info->flags &= ~S_INITIALIZED;
local_irq_restore(flags);
}
struct {
int divisor, prescale;
}
#ifndef CONFIG_M68VZ328
hw_baud_table[18] = {
{0,0}, /* 0 */
{0,0}, /* 50 */
{0,0}, /* 75 */
{0,0}, /* 110 */
{0,0}, /* 134 */
{0,0}, /* 150 */
{0,0}, /* 200 */
{7,0x26}, /* 300 */
{6,0x26}, /* 600 */
{5,0x26}, /* 1200 */
{0,0}, /* 1800 */
{4,0x26}, /* 2400 */
{3,0x26}, /* 4800 */
{2,0x26}, /* 9600 */
{1,0x26}, /* 19200 */
{0,0x26}, /* 38400 */
{1,0x38}, /* 57600 */
{0,0x38}, /* 115200 */
};
#else
hw_baud_table[18] = {
{0,0}, /* 0 */
{0,0}, /* 50 */
{0,0}, /* 75 */
{0,0}, /* 110 */
{0,0}, /* 134 */
{0,0}, /* 150 */
{0,0}, /* 200 */
{0,0}, /* 300 */
{7,0x26}, /* 600 */
{6,0x26}, /* 1200 */
{0,0}, /* 1800 */
{5,0x26}, /* 2400 */
{4,0x26}, /* 4800 */
{3,0x26}, /* 9600 */
{2,0x26}, /* 19200 */
{1,0x26}, /* 38400 */
{0,0x26}, /* 57600 */
{1,0x38}, /* 115200 */
};
#endif
/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
/*
* This routine is called to set the UART divisor registers to match
* the specified baud rate for a serial port.
*/
static void change_speed(struct m68k_serial *info)
{
m68328_uart *uart = &uart_addr[info->line];
unsigned short port;
unsigned short ustcnt;
unsigned cflag;
int i;
if (!info->port.tty || !info->port.tty->termios)
return;
cflag = info->port.tty->termios->c_cflag;
if (!(port = info->port))
return;
ustcnt = uart->ustcnt;
uart->ustcnt = ustcnt & ~USTCNT_TXEN;
i = cflag & CBAUD;
if (i & CBAUDEX) {
i = (i & ~CBAUDEX) + B38400;
}
info->baud = baud_table[i];
uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
if ((cflag & CSIZE) == CS8)
ustcnt |= USTCNT_8_7;
if (cflag & CSTOPB)
ustcnt |= USTCNT_STOP;
if (cflag & PARENB)
ustcnt |= USTCNT_PARITYEN;
if (cflag & PARODD)
ustcnt |= USTCNT_ODD_EVEN;
#ifdef CONFIG_SERIAL_68328_RTS_CTS
if (cflag & CRTSCTS) {
uart->utx.w &= ~ UTX_NOCTS;
} else {
uart->utx.w |= UTX_NOCTS;
}
#endif
ustcnt |= USTCNT_TXEN;
uart->ustcnt = ustcnt;
return;
}
/*
* Fair output driver allows a process to speak.
*/
static void rs_fair_output(void)
{
int left; /* Output no more than that */
unsigned long flags;
struct m68k_serial *info = &m68k_soft[0];
char c;
if (info == 0) return;
if (info->xmit_buf == 0) return;
local_irq_save(flags);
left = info->xmit_cnt;
while (left != 0) {
c = info->xmit_buf[info->xmit_tail];
info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
info->xmit_cnt--;
local_irq_restore(flags);
rs_put_char(c);
local_irq_save(flags);
left = min(info->xmit_cnt, left-1);
}
/* Last character is being transmitted now (hopefully). */
udelay(5);
local_irq_restore(flags);
return;
}
/*
* m68k_console_print is registered for printk.
*/
void console_print_68328(const char *p)
{
char c;
while((c=*(p++)) != 0) {
if(c == '\n')
rs_put_char('\r');
rs_put_char(c);
}
/* Comment this if you want to have a strict interrupt-driven output */
rs_fair_output();
return;
}
static void rs_set_ldisc(struct tty_struct *tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
return;
info->is_cons = (tty->termios->c_line == N_TTY);
printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
}
static void rs_flush_chars(struct tty_struct *tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
m68328_uart *uart = &uart_addr[info->line];
unsigned long flags;
if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
return;
#ifndef USE_INTS
for(;;) {
#endif
/* Enable transmitter */
local_irq_save(flags);
if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
!info->xmit_buf) {
local_irq_restore(flags);
return;
}
#ifdef USE_INTS
uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
#else
uart->ustcnt |= USTCNT_TXEN;
#endif
#ifdef USE_INTS
if (uart->utx.w & UTX_TX_AVAIL) {
#else
if (1) {
#endif
/* Send char */
uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
info->xmit_cnt--;
}
#ifndef USE_INTS
while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
}
#endif
local_irq_restore(flags);
}
extern void console_printn(const char * b, int count);
static int rs_write(struct tty_struct * tty,
const unsigned char *buf, int count)
{
int c, total = 0;
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
m68328_uart *uart = &uart_addr[info->line];
unsigned long flags;
if (serial_paranoia_check(info, tty->name, "rs_write"))
return 0;
if (!tty || !info->xmit_buf)
return 0;
local_save_flags(flags);
while (1) {
local_irq_disable();
c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
SERIAL_XMIT_SIZE - info->xmit_head));
local_irq_restore(flags);
if (c <= 0)
break;
memcpy(info->xmit_buf + info->xmit_head, buf, c);
local_irq_disable();
info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
info->xmit_cnt += c;
local_irq_restore(flags);
buf += c;
count -= c;
total += c;
}
if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
/* Enable transmitter */
local_irq_disable();
#ifndef USE_INTS
while(info->xmit_cnt) {
#endif
uart->ustcnt |= USTCNT_TXEN;
#ifdef USE_INTS
uart->ustcnt |= USTCNT_TX_INTR_MASK;
#else
while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
#endif
if (uart->utx.w & UTX_TX_AVAIL) {
uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
info->xmit_cnt--;
}
#ifndef USE_INTS
}
#endif
local_irq_restore(flags);
}
return total;
}
static int rs_write_room(struct tty_struct *tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
int ret;
if (serial_paranoia_check(info, tty->name, "rs_write_room"))
return 0;
ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
if (ret < 0)
ret = 0;
return ret;
}
static int rs_chars_in_buffer(struct tty_struct *tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
return 0;
return info->xmit_cnt;
}
static void rs_flush_buffer(struct tty_struct *tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
unsigned long flags;
if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
return;
local_irq_save(flags);
info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
local_irq_restore(flags);
tty_wakeup(tty);
}
/*
* ------------------------------------------------------------
* rs_throttle()
*
* This routine is called by the upper-layer tty layer to signal that
* incoming characters should be throttled.
* ------------------------------------------------------------
*/
static void rs_throttle(struct tty_struct * tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
if (serial_paranoia_check(info, tty->name, "rs_throttle"))
return;
if (I_IXOFF(tty))
info->x_char = STOP_CHAR(tty);
/* Turn off RTS line (do this atomic) */
}
static void rs_unthrottle(struct tty_struct * tty)
{
struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
return;
if (I_IXOFF(tty)) {
if (info->x_char)
info->x_char = 0;
else
info->x_char = START_CHAR(tty);
}
/* Assert RTS line (do this atomic) */
}
/*
* ------------------------------------------------------------
* rs_ioctl() and friends
* ------------------------------------------------------------
*/
static int get_serial_info(struct m68k_serial * info,
struct serial_struct * retinfo)
{
struct serial_struct tmp;
if (!retinfo)
return -EFAULT;
memset(&tmp, 0, sizeof(tmp));
tmp.type = info->type;
tmp.line = info->line;
tmp.port = info->port;
tmp.irq = info->irq;
tmp.flags = info->flags;
tmp.baud_base = info->baud_base;
tmp.close_delay = info->close_delay;
tmp.closing_wait = info->closing_wait;
tmp.custom_divisor = info->custom_divisor;
if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
return -EFAULT;
return 0;
}
static int set_serial_info(struct m68k_serial * info,
struct serial_struct * new_info)
{
struct serial_struct new_serial;
struct m68k_serial old_info;
int retval = 0;
if (!new_info)
return -EFAULT;
if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
return -EFAULT;
old_info = *info;
if (!capable(CAP_SYS_ADMIN)) {
if ((new_serial.baud_base != info->baud_base) ||
(new_serial.type != info->type) ||
(new_serial.close_delay != info->close_delay) ||
((new_serial.flags & ~S_USR_MASK) !=
(info->flags & ~S_USR_MASK)))
return -EPERM;
info->flags = ((info->flags & ~S_USR_MASK) |
(new_serial.flags & S_USR_MASK));
info->custom_divisor = new_serial.custom_divisor;
goto check_and_exit;
}
if (info->count > 1)
return -EBUSY;
/*
* OK, past this point, all the error checking has been done.
* At this point, we start making changes.....
*/
info->baud_base = new_serial.baud_base;
info->flags = ((info->flags & ~S_FLAGS) |
(new_serial.flags & S_FLAGS));
info->type = new_serial.type;
info->close_delay = new_serial.close_delay;
info->closing_wait = new_serial.closing_wait;
check_and_exit:
retval = startup(info);
return retval;
}
/*
* get_lsr_info - get line status register info
*
* Purpose: Let user call ioctl() to get info when the UART physically
* is emptied. On bus types like RS485, the transmitter must
* release the bus after transmitting. This must be done when
* the transmit shift register is empty, not be done when the
* transmit holding register is empty. This functionality
* allows an RS485 driver to be written in user space.
*/
static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
{
#ifdef CONFIG_SERIAL_68328_RTS_CTS
m68328_uart *uart = &uart_addr[info->line];
#endif
unsigned char status;
unsigned long flags;
local_irq_save(flags);
#ifdef CONFIG_SERIAL_68328_RTS_CTS
status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
#else
status = 0;
#endif
local_irq_restore(flags);
return put_user(status, value);
}
/*
* This routine sends a break character out the serial port.
*/
static void send_break(struct m68k_serial * info, unsigned int duration)
{
m68328_uart *uart = &uart_addr[info->line];
unsigned long flags;
if (!info->port)
return;
local_irq_save(flags);
#ifdef USE_INTS
uart->utx.w |= UTX_SEND_BREAK;
msleep_interruptible(duration);
uart->utx.w &= ~UTX_SEND_BREAK;
#endif
local_irq_restore(flags);
}
static int rs_ioctl(struct tty_struct *tty, struct file * file,
unsigned int cmd, unsigned long arg)
{
int error;
struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
int retval;
if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
return -ENODEV;
if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
(cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
(cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
if (tty->flags & (1 << TTY_IO_ERROR))
return -EIO;
}
switch (cmd) {
case TCSBRK: /* SVID version: non-zero arg --> no break */
retval = tty_check_change(tty);
if (retval)
return retval;
tty_wait_until_sent(tty, 0);
if (!arg)
send_break(info, 250); /* 1/4 second */
return 0;
case TCSBRKP: /* support for POSIX tcsendbreak() */
retval = tty_check_change(tty);
if (retval)
return retval;
tty_wait_until_sent(tty, 0);