File: /usr/src/linux/arch/ppc/8260_io/uart.c
1 /*
2 * BK Id: SCCS/s.uart.c 1.6 05/17/01 18:14:20 cort
3 */
4 /*
5 * UART driver for MPC8260 CPM SCC or SMC
6 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
7 * Copyright (c) 2000 MontaVista Software, Inc. (source@mvista.com)
8 * 2.3.99 updates
9 *
10 * I used the 8xx uart.c driver as the framework for this driver.
11 * The original code was written for the EST8260 board. I tried to make
12 * it generic, but there may be some assumptions in the structures that
13 * have to be fixed later.
14 *
15 * The 8xx and 8260 are similar, but not identical. Over time we
16 * could probably merge these two drivers.
17 * To save porting time, I did not bother to change any object names
18 * that are not accessed outside of this file.
19 * It still needs lots of work........When it was easy, I included code
20 * to support the SCCs.
21 * Only the SCCs support modem control, so that is not complete either.
22 *
23 * This module exports the following rs232 io functions:
24 *
25 * int rs_8xx_init(void);
26 */
27
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
34 #include <linux/interrupt.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/serial.h>
38 #include <linux/serialP.h>
39 #include <linux/major.h>
40 #include <linux/string.h>
41 #include <linux/fcntl.h>
42 #include <linux/ptrace.h>
43 #include <linux/mm.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
46 #include <linux/delay.h>
47 #include <asm/uaccess.h>
48 #include <asm/immap_8260.h>
49 #include <asm/mpc8260.h>
50 #include <asm/cpm_8260.h>
51 #include <asm/irq.h>
52
53 #ifdef CONFIG_SERIAL_CONSOLE
54 #include <linux/console.h>
55
56 /* this defines the index into rs_table for the port to use
57 */
58 #ifndef CONFIG_SERIAL_CONSOLE_PORT
59 #define CONFIG_SERIAL_CONSOLE_PORT 0
60 #endif
61 #endif
62
63 #define TX_WAKEUP ASYNC_SHARE_IRQ
64
65 static char *serial_name = "CPM UART driver";
66 static char *serial_version = "0.01";
67
68 static DECLARE_TASK_QUEUE(tq_serial);
69
70 static struct tty_driver serial_driver, callout_driver;
71 static int serial_refcount;
72 static int serial_console_setup(struct console *co, char *options);
73
74 /*
75 * Serial driver configuration section. Here are the various options:
76 */
77 #define SERIAL_PARANOIA_CHECK
78 #define CONFIG_SERIAL_NOPAUSE_IO
79 #define SERIAL_DO_RESTART
80
81 /* Set of debugging defines */
82
83 #undef SERIAL_DEBUG_INTR
84 #undef SERIAL_DEBUG_OPEN
85 #undef SERIAL_DEBUG_FLOW
86 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
87
88 #define _INLINE_ inline
89
90 #define DBG_CNT(s)
91
92 /* We overload some of the items in the data structure to meet our
93 * needs. For example, the port address is the CPM parameter ram
94 * offset for the SCC or SMC. The maximum number of ports is 4 SCCs and
95 * 2 SMCs. The "hub6" field is used to indicate the channel number, with
96 * 0 and 1 indicating the SMCs and 2, 3, 4, and 5 are the SCCs.
97 * Since these ports are so versatile, I don't yet have a strategy for
98 * their management. For example, SCC1 is used for Ethernet. Right
99 * now, just don't put them in the table. Of course, right now I just
100 * want the SMC to work as a uart :-)..
101 * The "type" field is currently set to 0, for PORT_UNKNOWN. It is
102 * not currently used. I should probably use it to indicate the port
103 * type of CMS or SCC.
104 * The SMCs do not support any modem control signals.
105 */
106 #define smc_scc_num hub6
107
108 /* SMC2 is sometimes used for low performance TDM interfaces. Define
109 * this as 1 if you want SMC2 as a serial port UART managed by this driver.
110 * Define this as 0 if you wish to use SMC2 for something else.
111 */
112 #define USE_SMC2 1
113
114 /* Define SCC to ttySx mapping.
115 */
116 #define SCC_NUM_BASE (USE_SMC2 + 1) /* SCC base tty "number" */
117
118 /* Define which SCC is the first one to use for a serial port. These
119 * are 0-based numbers, i.e. this assumes the first SCC (SCC1) is used
120 * for Ethernet, and the first available SCC for serial UART is SCC2.
121 * NOTE: IF YOU CHANGE THIS, you have to change the PROFF_xxx and
122 * interrupt vectors in the table below to match.
123 */
124 #define SCC_IDX_BASE 1 /* table index */
125
126 static struct serial_state rs_table[] = {
127 /* UART CLK PORT IRQ FLAGS NUM */
128 { 0, 0, PROFF_SMC1, SIU_INT_SMC1, 0, 0 }, /* SMC1 ttyS0 */
129 #if USE_SMC2
130 { 0, 0, PROFF_SMC2, SIU_INT_SMC2, 0, 1 }, /* SMC2 ttyS1 */
131 #endif
132 { 0, 0, PROFF_SCC2, SIU_INT_SCC2, 0, SCC_NUM_BASE}, /* SCC2 ttyS2 */
133 { 0, 0, PROFF_SCC3, SIU_INT_SCC3, 0, SCC_NUM_BASE + 1}, /* SCC3 ttyS3 */
134 };
135
136 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
137
138 static struct tty_struct *serial_table[NR_PORTS];
139 static struct termios *serial_termios[NR_PORTS];
140 static struct termios *serial_termios_locked[NR_PORTS];
141
142 /* The number of buffer descriptors and their sizes.
143 */
144 #define RX_NUM_FIFO 4
145 #define RX_BUF_SIZE 32
146 #define TX_NUM_FIFO 4
147 #define TX_BUF_SIZE 32
148
149 #ifndef MIN
150 #define MIN(a,b) ((a) < (b) ? (a) : (b))
151 #endif
152
153 /* The async_struct in serial.h does not really give us what we
154 * need, so define our own here.
155 */
156 typedef struct serial_info {
157 int magic;
158 int flags;
159 struct serial_state *state;
160 struct tty_struct *tty;
161 int read_status_mask;
162 int ignore_status_mask;
163 int timeout;
164 int line;
165 int x_char; /* xon/xoff character */
166 int close_delay;
167 unsigned short closing_wait;
168 unsigned short closing_wait2;
169 unsigned long event;
170 unsigned long last_active;
171 int blocked_open; /* # of blocked opens */
172 long session; /* Session of opening process */
173 long pgrp; /* pgrp of opening process */
174 struct tq_struct tqueue;
175 struct tq_struct tqueue_hangup;
176 wait_queue_head_t open_wait;
177 wait_queue_head_t close_wait;
178
179 /* CPM Buffer Descriptor pointers.
180 */
181 cbd_t *rx_bd_base;
182 cbd_t *rx_cur;
183 cbd_t *tx_bd_base;
184 cbd_t *tx_cur;
185 } ser_info_t;
186
187 static void change_speed(ser_info_t *info);
188 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout);
189
190 static inline int serial_paranoia_check(ser_info_t *info,
191 kdev_t device, const char *routine)
192 {
193 #ifdef SERIAL_PARANOIA_CHECK
194 static const char *badmagic =
195 "Warning: bad magic number for serial struct (%s) in %s\n";
196 static const char *badinfo =
197 "Warning: null async_struct for (%s) in %s\n";
198
199 if (!info) {
200 printk(badinfo, kdevname(device), routine);
201 return 1;
202 }
203 if (info->magic != SERIAL_MAGIC) {
204 printk(badmagic, kdevname(device), routine);
205 return 1;
206 }
207 #endif
208 return 0;
209 }
210
211 /*
212 * This is used to figure out the divisor speeds and the timeouts,
213 * indexed by the termio value. The generic CPM functions are responsible
214 * for setting and assigning baud rate generators for us.
215 */
216 static int baud_table[] = {
217 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
218 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
219
220
221 /*
222 * ------------------------------------------------------------
223 * rs_stop() and rs_start()
224 *
225 * This routines are called before setting or resetting tty->stopped.
226 * They enable or disable transmitter interrupts, as necessary.
227 * ------------------------------------------------------------
228 */
229 static void rs_8xx_stop(struct tty_struct *tty)
230 {
231 ser_info_t *info = (ser_info_t *)tty->driver_data;
232 int idx;
233 unsigned long flags;
234 volatile scc_t *sccp;
235 volatile smc_t *smcp;
236
237 if (serial_paranoia_check(info, tty->device, "rs_stop"))
238 return;
239
240 save_flags(flags); cli();
241 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
242 smcp = &immr->im_smc[idx];
243 smcp->smc_smcm &= ~SMCM_TX;
244 }
245 else {
246 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
247 sccp->scc_sccm &= ~UART_SCCM_TX;
248 }
249 restore_flags(flags);
250 }
251
252 static void rs_8xx_start(struct tty_struct *tty)
253 {
254 ser_info_t *info = (ser_info_t *)tty->driver_data;
255 int idx;
256 unsigned long flags;
257 volatile scc_t *sccp;
258 volatile smc_t *smcp;
259
260 if (serial_paranoia_check(info, tty->device, "rs_stop"))
261 return;
262
263 save_flags(flags); cli();
264 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
265 smcp = &immr->im_smc[idx];
266 smcp->smc_smcm |= SMCM_TX;
267 }
268 else {
269 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
270 sccp->scc_sccm |= UART_SCCM_TX;
271 }
272 restore_flags(flags);
273 }
274
275 /*
276 * ----------------------------------------------------------------------
277 *
278 * Here starts the interrupt handling routines. All of the following
279 * subroutines are declared as inline and are folded into
280 * rs_interrupt(). They were separated out for readability's sake.
281 *
282 * Note: rs_interrupt() is a "fast" interrupt, which means that it
283 * runs with interrupts turned off. People who may want to modify
284 * rs_interrupt() should try to keep the interrupt handler as fast as
285 * possible. After you are done making modifications, it is not a bad
286 * idea to do:
287 *
288 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
289 *
290 * and look at the resulting assemble code in serial.s.
291 *
292 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
293 * -----------------------------------------------------------------------
294 */
295
296 /*
297 * This routine is used by the interrupt handler to schedule
298 * processing in the software interrupt portion of the driver.
299 */
300 static _INLINE_ void rs_sched_event(ser_info_t *info,
301 int event)
302 {
303 info->event |= 1 << event;
304 queue_task(&info->tqueue, &tq_serial);
305 mark_bh(SERIAL_BH);
306 }
307
308 static _INLINE_ void receive_chars(ser_info_t *info)
309 {
310 struct tty_struct *tty = info->tty;
311 unsigned char ch, *cp;
312 /*int ignored = 0;*/
313 int i;
314 ushort status;
315 struct async_icount *icount;
316 volatile cbd_t *bdp;
317
318 icount = &info->state->icount;
319
320 /* Just loop through the closed BDs and copy the characters into
321 * the buffer.
322 */
323 bdp = info->rx_cur;
324 for (;;) {
325 if (bdp->cbd_sc & BD_SC_EMPTY) /* If this one is empty */
326 break; /* we are all done */
327
328 /* The read status mask tell us what we should do with
329 * incoming characters, especially if errors occur.
330 * One special case is the use of BD_SC_EMPTY. If
331 * this is not set, we are supposed to be ignoring
332 * inputs. In this case, just mark the buffer empty and
333 * continue.
334 if (!(info->read_status_mask & BD_SC_EMPTY)) {
335 bdp->cbd_sc |= BD_SC_EMPTY;
336 bdp->cbd_sc &=
337 ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
338
339 if (bdp->cbd_sc & BD_SC_WRAP)
340 bdp = info->rx_bd_base;
341 else
342 bdp++;
343 continue;
344 }
345 */
346
347 /* Get the number of characters and the buffer pointer.
348 */
349 i = bdp->cbd_datlen;
350 cp = (unsigned char *)__va(bdp->cbd_bufaddr);
351 status = bdp->cbd_sc;
352
353 /* Check to see if there is room in the tty buffer for
354 * the characters in our BD buffer. If not, we exit
355 * now, leaving the BD with the characters. We'll pick
356 * them up again on the next receive interrupt (which could
357 * be a timeout).
358 */
359 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)
360 break;
361
362 while (i-- > 0) {
363 ch = *cp++;
364 *tty->flip.char_buf_ptr = ch;
365 icount->rx++;
366
367 #ifdef SERIAL_DEBUG_INTR
368 printk("DR%02x:%02x...", ch, *status);
369 #endif
370 *tty->flip.flag_buf_ptr = 0;
371 if (status & (BD_SC_BR | BD_SC_FR |
372 BD_SC_PR | BD_SC_OV)) {
373 /*
374 * For statistics only
375 */
376 if (status & BD_SC_BR)
377 icount->brk++;
378 else if (status & BD_SC_PR)
379 icount->parity++;
380 else if (status & BD_SC_FR)
381 icount->frame++;
382 if (status & BD_SC_OV)
383 icount->overrun++;
384
385 /*
386 * Now check to see if character should be
387 * ignored, and mask off conditions which
388 * should be ignored.
389 if (status & info->ignore_status_mask) {
390 if (++ignored > 100)
391 break;
392 continue;
393 }
394 */
395 status &= info->read_status_mask;
396
397 if (status & (BD_SC_BR)) {
398 #ifdef SERIAL_DEBUG_INTR
399 printk("handling break....");
400 #endif
401 *tty->flip.flag_buf_ptr = TTY_BREAK;
402 if (info->flags & ASYNC_SAK)
403 do_SAK(tty);
404 } else if (status & BD_SC_PR)
405 *tty->flip.flag_buf_ptr = TTY_PARITY;
406 else if (status & BD_SC_FR)
407 *tty->flip.flag_buf_ptr = TTY_FRAME;
408 if (status & BD_SC_OV) {
409 /*
410 * Overrun is special, since it's
411 * reported immediately, and doesn't
412 * affect the current character
413 */
414 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
415 tty->flip.count++;
416 tty->flip.flag_buf_ptr++;
417 tty->flip.char_buf_ptr++;
418 *tty->flip.flag_buf_ptr =
419 TTY_OVERRUN;
420 }
421 }
422 }
423 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
424 break;
425
426 tty->flip.flag_buf_ptr++;
427 tty->flip.char_buf_ptr++;
428 tty->flip.count++;
429 }
430
431 /* This BD is ready to be used again. Clear status.
432 * Get next BD.
433 */
434 bdp->cbd_sc |= BD_SC_EMPTY;
435 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
436
437 if (bdp->cbd_sc & BD_SC_WRAP)
438 bdp = info->rx_bd_base;
439 else
440 bdp++;
441 }
442
443 info->rx_cur = (cbd_t *)bdp;
444
445 queue_task(&tty->flip.tqueue, &tq_timer);
446 }
447
448 static _INLINE_ void transmit_chars(ser_info_t *info)
449 {
450
451 if (info->flags & TX_WAKEUP) {
452 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
453 }
454
455 #ifdef SERIAL_DEBUG_INTR
456 printk("THRE...");
457 #endif
458 }
459
460 #ifdef notdef
461 /* I need to do this for the SCCs, so it is left as a reminder.
462 */
463 static _INLINE_ void check_modem_status(struct async_struct *info)
464 {
465 int status;
466 struct async_icount *icount;
467
468 status = serial_in(info, UART_MSR);
469
470 if (status & UART_MSR_ANY_DELTA) {
471 icount = &info->state->icount;
472 /* update input line counters */
473 if (status & UART_MSR_TERI)
474 icount->rng++;
475 if (status & UART_MSR_DDSR)
476 icount->dsr++;
477 if (status & UART_MSR_DDCD) {
478 icount->dcd++;
479 #ifdef CONFIG_HARD_PPS
480 if ((info->flags & ASYNC_HARDPPS_CD) &&
481 (status & UART_MSR_DCD))
482 hardpps();
483 #endif
484 }
485 if (status & UART_MSR_DCTS)
486 icount->cts++;
487 wake_up_interruptible(&info->delta_msr_wait);
488 }
489
490 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
491 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
492 printk("ttys%d CD now %s...", info->line,
493 (status & UART_MSR_DCD) ? "on" : "off");
494 #endif
495 if (status & UART_MSR_DCD)
496 wake_up_interruptible(&info->open_wait);
497 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
498 (info->flags & ASYNC_CALLOUT_NOHUP))) {
499 #ifdef SERIAL_DEBUG_OPEN
500 printk("scheduling hangup...");
501 #endif
502 MOD_INC_USE_COUNT;
503 if (schedule_task(&info->tqueue_hangup) == 0)
504 MOD_DEC_USE_COUNT;
505 }
506 }
507 if (info->flags & ASYNC_CTS_FLOW) {
508 if (info->tty->hw_stopped) {
509 if (status & UART_MSR_CTS) {
510 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
511 printk("CTS tx start...");
512 #endif
513 info->tty->hw_stopped = 0;
514 info->IER |= UART_IER_THRI;
515 serial_out(info, UART_IER, info->IER);
516 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
517 return;
518 }
519 } else {
520 if (!(status & UART_MSR_CTS)) {
521 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
522 printk("CTS tx stop...");
523 #endif
524 info->tty->hw_stopped = 1;
525 info->IER &= ~UART_IER_THRI;
526 serial_out(info, UART_IER, info->IER);
527 }
528 }
529 }
530 }
531 #endif
532
533 /*
534 * This is the serial driver's interrupt routine for a single port
535 */
536 static void rs_8xx_interrupt(int irq, void * dev_id, struct pt_regs * regs)
537 {
538 u_char events;
539 int idx;
540 ser_info_t *info;
541 volatile smc_t *smcp;
542 volatile scc_t *sccp;
543
544 info = (ser_info_t *)dev_id;
545
546 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
547 smcp = &immr->im_smc[idx];
548 events = smcp->smc_smce;
549 if (events & SMCM_RX)
550 receive_chars(info);
551 if (events & SMCM_TX)
552 transmit_chars(info);
553 smcp->smc_smce = events;
554 }
555 else {
556 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
557 events = sccp->scc_scce;
558 if (events & SCCM_RX)
559 receive_chars(info);
560 if (events & SCCM_TX)
561 transmit_chars(info);
562 sccp->scc_scce = events;
563 }
564
565 #ifdef SERIAL_DEBUG_INTR
566 printk("rs_interrupt_single(%d, %x)...",
567 info->state->smc_scc_num, events);
568 #endif
569 #ifdef modem_control
570 check_modem_status(info);
571 #endif
572 info->last_active = jiffies;
573 #ifdef SERIAL_DEBUG_INTR
574 printk("end.\n");
575 #endif
576 }
577
578
579 /*
580 * -------------------------------------------------------------------
581 * Here ends the serial interrupt routines.
582 * -------------------------------------------------------------------
583 */
584
585 /*
586 * This routine is used to handle the "bottom half" processing for the
587 * serial driver, known also the "software interrupt" processing.
588 * This processing is done at the kernel interrupt level, after the
589 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
590 * is where time-consuming activities which can not be done in the
591 * interrupt driver proper are done; the interrupt driver schedules
592 * them using rs_sched_event(), and they get done here.
593 */
594 static void do_serial_bh(void)
595 {
596 run_task_queue(&tq_serial);
597 }
598
599 static void do_softint(void *private_)
600 {
601 ser_info_t *info = (ser_info_t *) private_;
602 struct tty_struct *tty;
603
604 tty = info->tty;
605 if (!tty)
606 return;
607
608 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
609 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
610 tty->ldisc.write_wakeup)
611 (tty->ldisc.write_wakeup)(tty);
612 wake_up_interruptible(&tty->write_wait);
613 }
614 }
615
616 /*
617 * This routine is called from the scheduler tqueue when the interrupt
618 * routine has signalled that a hangup has occurred. The path of
619 * hangup processing is:
620 *
621 * serial interrupt routine -> (scheduler tqueue) ->
622 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
623 *
624 */
625 static void do_serial_hangup(void *private_)
626 {
627 struct async_struct *info = (struct async_struct *) private_;
628 struct tty_struct *tty;
629
630 tty = info->tty;
631 if (tty)
632 tty_hangup(tty);
633 MOD_DEC_USE_COUNT;
634 }
635
636 /*static void rs_8xx_timer(void)
637 {
638 printk("rs_8xx_timer\n");
639 }*/
640
641
642 static int startup(ser_info_t *info)
643 {
644 unsigned long flags;
645 int retval=0;
646 int idx;
647 struct serial_state *state= info->state;
648 volatile smc_t *smcp;
649 volatile scc_t *sccp;
650 volatile smc_uart_t *up;
651 volatile scc_uart_t *scup;
652
653
654 save_flags(flags); cli();
655
656 if (info->flags & ASYNC_INITIALIZED) {
657 goto errout;
658 }
659
660 #ifdef maybe
661 if (!state->port || !state->type) {
662 if (info->tty)
663 set_bit(TTY_IO_ERROR, &info->tty->flags);
664 goto errout;
665 }
666 #endif
667
668 #ifdef SERIAL_DEBUG_OPEN
669 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
670 #endif
671
672
673 #ifdef modem_control
674 info->MCR = 0;
675 if (info->tty->termios->c_cflag & CBAUD)
676 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
677 #endif
678
679 if (info->tty)
680 clear_bit(TTY_IO_ERROR, &info->tty->flags);
681
682 /*
683 * and set the speed of the serial port
684 */
685 change_speed(info);
686
687 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
688 smcp = &immr->im_smc[idx];
689
690 /* Enable interrupts and I/O.
691 */
692 smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
693 smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
694
695 /* We can tune the buffer length and idle characters
696 * to take advantage of the entire incoming buffer size.
697 * If mrblr is something other than 1, maxidl has to be
698 * non-zero or we never get an interrupt. The maxidl
699 * is the number of character times we wait after reception
700 * of the last character before we decide no more characters
701 * are coming.
702 */
703 up = (smc_uart_t *)&immr->im_dprambase[state->port];
704 #if 0
705 up->smc_mrblr = 1; /* receive buffer length */
706 up->smc_maxidl = 0; /* wait forever for next char */
707 #else
708 up->smc_mrblr = RX_BUF_SIZE;
709 up->smc_maxidl = RX_BUF_SIZE;
710 #endif
711 up->smc_brkcr = 1; /* number of break chars */
712 }
713 else {
714 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
715 scup = (scc_uart_t *)&immr->im_dprambase[state->port];
716 #if 0
717 scup->scc_genscc.scc_mrblr = 1; /* receive buffer length */
718 scup->scc_maxidl = 0; /* wait forever for next char */
719 #else
720 scup->scc_genscc.scc_mrblr = RX_BUF_SIZE;
721 scup->scc_maxidl = RX_BUF_SIZE;
722 #endif
723
724 sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
725 sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
726 }
727
728 info->flags |= ASYNC_INITIALIZED;
729 restore_flags(flags);
730 return 0;
731
732 errout:
733 restore_flags(flags);
734 return retval;
735 }
736
737 /*
738 * This routine will shutdown a serial port; interrupts are disabled, and
739 * DTR is dropped if the hangup on close termio flag is on.
740 */
741 static void shutdown(ser_info_t * info)
742 {
743 unsigned long flags;
744 struct serial_state *state;
745 int idx;
746 volatile smc_t *smcp;
747 volatile scc_t *sccp;
748
749 if (!(info->flags & ASYNC_INITIALIZED))
750 return;
751
752 state = info->state;
753
754 #ifdef SERIAL_DEBUG_OPEN
755 printk("Shutting down serial port %d (irq %d)....", info->line,
756 state->irq);
757 #endif
758
759 save_flags(flags); cli(); /* Disable interrupts */
760
761 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
762 smcp = &immr->im_smc[idx];
763
764 /* Disable interrupts and I/O.
765 */
766 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
767 #ifdef CONFIG_SERIAL_CONSOLE
768 /* We can't disable the transmitter if this is the
769 * system console.
770 */
771 if (idx != CONFIG_SERIAL_CONSOLE_PORT)
772 #endif
773 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
774 }
775 else {
776 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
777 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
778 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
779 }
780
781 if (info->tty)
782 set_bit(TTY_IO_ERROR, &info->tty->flags);
783
784 info->flags &= ~ASYNC_INITIALIZED;
785 restore_flags(flags);
786 }
787
788 /*
789 * This routine is called to set the UART divisor registers to match
790 * the specified baud rate for a serial port.
791 */
792 static void change_speed(ser_info_t *info)
793 {
794 int baud_rate;
795 unsigned cflag, cval, scval, prev_mode;
796 int i, bits, sbits, idx;
797 unsigned long flags;
798 volatile smc_t *smcp;
799 volatile scc_t *sccp;
800
801 if (!info->tty || !info->tty->termios)
802 return;
803 cflag = info->tty->termios->c_cflag;
804
805 /* Character length programmed into the mode register is the
806 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
807 * 1 or 2 stop bits, minus 1.
808 * The value 'bits' counts this for us.
809 */
810 cval = 0;
811 scval = 0;
812
813 /* byte size and parity */
814 switch (cflag & CSIZE) {
815 case CS5: bits = 5; break;
816 case CS6: bits = 6; break;
817 case CS7: bits = 7; break;
818 case CS8: bits = 8; break;
819 /* Never happens, but GCC is too dumb to figure it out */
820 default: bits = 8; break;
821 }
822 sbits = bits - 5;
823
824 if (cflag & CSTOPB) {
825 cval |= SMCMR_SL; /* Two stops */
826 scval |= SCU_PMSR_SL;
827 bits++;
828 }
829 if (cflag & PARENB) {
830 cval |= SMCMR_PEN;
831 scval |= SCU_PMSR_PEN;
832 bits++;
833 }
834 if (!(cflag & PARODD)) {
835 cval |= SMCMR_PM_EVEN;
836 scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
837 }
838
839 /* Determine divisor based on baud rate */
840 i = cflag & CBAUD;
841 if (i >= (sizeof(baud_table)/sizeof(int)))
842 baud_rate = 9600;
843 else
844 baud_rate = baud_table[i];
845
846 info->timeout = (TX_BUF_SIZE*HZ*bits);
847 info->timeout += HZ/50; /* Add .02 seconds of slop */
848
849 #ifdef modem_control
850 /* CTS flow control flag and modem status interrupts */
851 info->IER &= ~UART_IER_MSI;
852 if (info->flags & ASYNC_HARDPPS_CD)
853 info->IER |= UART_IER_MSI;
854 if (cflag & CRTSCTS) {
855 info->flags |= ASYNC_CTS_FLOW;
856 info->IER |= UART_IER_MSI;
857 } else
858 info->flags &= ~ASYNC_CTS_FLOW;
859 if (cflag & CLOCAL)
860 info->flags &= ~ASYNC_CHECK_CD;
861 else {
862 info->flags |= ASYNC_CHECK_CD;
863 info->IER |= UART_IER_MSI;
864 }
865 serial_out(info, UART_IER, info->IER);
866 #endif
867
868 /*
869 * Set up parity check flag
870 */
871 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
872
873 info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
874 if (I_INPCK(info->tty))
875 info->read_status_mask |= BD_SC_FR | BD_SC_PR;
876 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
877 info->read_status_mask |= BD_SC_BR;
878
879 /*
880 * Characters to ignore
881 */
882 info->ignore_status_mask = 0;
883 if (I_IGNPAR(info->tty))
884 info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
885 if (I_IGNBRK(info->tty)) {
886 info->ignore_status_mask |= BD_SC_BR;
887 /*
888 * If we're ignore parity and break indicators, ignore
889 * overruns too. (For real raw support).
890 */
891 if (I_IGNPAR(info->tty))
892 info->ignore_status_mask |= BD_SC_OV;
893 }
894 /*
895 * !!! ignore all characters if CREAD is not set
896 */
897 if ((cflag & CREAD) == 0)
898 info->read_status_mask &= ~BD_SC_EMPTY;
899 save_flags(flags); cli();
900
901 /* Start bit has not been added (so don't, because we would just
902 * subtract it later), and we need to add one for the number of
903 * stops bits (there is always at least one).
904 */
905 bits++;
906 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
907 smcp = &immr->im_smc[idx];
908
909 /* Set the mode register. We want to keep a copy of the
910 * enables, because we want to put them back if they were
911 * present.
912 */
913 prev_mode = smcp->smc_smcmr;
914 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
915 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
916 }
917 else {
918 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
919 sccp->scc_pmsr = (sbits << 12) | scval;
920 }
921
922 m8260_cpm_setbrg(info->state->smc_scc_num, baud_rate);
923
924 restore_flags(flags);
925 }
926
927 static void rs_8xx_put_char(struct tty_struct *tty, unsigned char ch)
928 {
929 ser_info_t *info = (ser_info_t *)tty->driver_data;
930 volatile cbd_t *bdp;
931
932 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
933 return;
934
935 if (!tty)
936 return;
937
938 bdp = info->tx_cur;
939 while (bdp->cbd_sc & BD_SC_READY);
940
941 *((char *)__va(bdp->cbd_bufaddr)) = ch;
942 bdp->cbd_datlen = 1;
943 bdp->cbd_sc |= BD_SC_READY;
944
945 /* Get next BD.
946 */
947 if (bdp->cbd_sc & BD_SC_WRAP)
948 bdp = info->tx_bd_base;
949 else
950 bdp++;
951
952 info->tx_cur = (cbd_t *)bdp;
953
954 }
955
956 static int rs_8xx_write(struct tty_struct * tty, int from_user,
957 const unsigned char *buf, int count)
958 {
959 int c, ret = 0;
960 ser_info_t *info = (ser_info_t *)tty->driver_data;
961 volatile cbd_t *bdp;
962
963 if (serial_paranoia_check(info, tty->device, "rs_write"))
964 return 0;
965
966 if (!tty)
967 return 0;
968
969 bdp = info->tx_cur;
970
971 while (1) {
972 c = MIN(count, TX_BUF_SIZE);
973
974 if (c <= 0)
975 break;
976
977 if (bdp->cbd_sc & BD_SC_READY) {
978 info->flags |= TX_WAKEUP;
979 break;
980 }
981
982 if (from_user) {
983 if (copy_from_user(__va(bdp->cbd_bufaddr), buf, c)) {
984 if (!ret)
985 ret = -EFAULT;
986 break;
987 }
988 } else {
989 memcpy(__va(bdp->cbd_bufaddr), buf, c);
990 }
991
992 bdp->cbd_datlen = c;
993 bdp->cbd_sc |= BD_SC_READY;
994
995 buf += c;
996 count -= c;
997 ret += c;
998
999 /* Get next BD.
1000 */
1001 if (bdp->cbd_sc & BD_SC_WRAP)
1002 bdp = info->tx_bd_base;
1003 else
1004 bdp++;
1005 info->tx_cur = (cbd_t *)bdp;
1006 }
1007 return ret;
1008 }
1009
1010 static int rs_8xx_write_room(struct tty_struct *tty)
1011 {
1012 ser_info_t *info = (ser_info_t *)tty->driver_data;
1013 int ret;
1014
1015 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1016 return 0;
1017
1018 if ((info->tx_cur->cbd_sc & BD_SC_READY) == 0) {
1019 info->flags &= ~TX_WAKEUP;
1020 ret = TX_BUF_SIZE;
1021 }
1022 else {
1023 info->flags |= TX_WAKEUP;
1024 ret = 0;
1025 }
1026 return ret;
1027 }
1028
1029 /* I could track this with transmit counters....maybe later.
1030 */
1031 static int rs_8xx_chars_in_buffer(struct tty_struct *tty)
1032 {
1033 ser_info_t *info = (ser_info_t *)tty->driver_data;
1034
1035 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1036 return 0;
1037 return 0;
1038 }
1039
1040 static void rs_8xx_flush_buffer(struct tty_struct *tty)
1041 {
1042 ser_info_t *info = (ser_info_t *)tty->driver_data;
1043
1044 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1045 return;
1046
1047 /* There is nothing to "flush", whatever we gave the CPM
1048 * is on its way out.
1049 */
1050 wake_up_interruptible(&tty->write_wait);
1051 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1052 tty->ldisc.write_wakeup)
1053 (tty->ldisc.write_wakeup)(tty);
1054 info->flags &= ~TX_WAKEUP;
1055 }
1056
1057 /*
1058 * This function is used to send a high-priority XON/XOFF character to
1059 * the device
1060 */
1061 static void rs_8xx_send_xchar(struct tty_struct *tty, char ch)
1062 {
1063 volatile cbd_t *bdp;
1064
1065 ser_info_t *info = (ser_info_t *)tty->driver_data;
1066
1067 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1068 return;
1069
1070 bdp = info->tx_cur;
1071 while (bdp->cbd_sc & BD_SC_READY);
1072
1073 *((char *)__va(bdp->cbd_bufaddr)) = ch;
1074 bdp->cbd_datlen = 1;
1075 bdp->cbd_sc |= BD_SC_READY;
1076
1077 /* Get next BD.
1078 */
1079 if (bdp->cbd_sc & BD_SC_WRAP)
1080 bdp = info->tx_bd_base;
1081 else
1082 bdp++;
1083
1084 info->tx_cur = (cbd_t *)bdp;
1085 }
1086
1087 /*
1088 * ------------------------------------------------------------
1089 * rs_throttle()
1090 *
1091 * This routine is called by the upper-layer tty layer to signal that
1092 * incoming characters should be throttled.
1093 * ------------------------------------------------------------
1094 */
1095 static void rs_8xx_throttle(struct tty_struct * tty)
1096 {
1097 ser_info_t *info = (ser_info_t *)tty->driver_data;
1098 #ifdef SERIAL_DEBUG_THROTTLE
1099 char buf[64];
1100
1101 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1102 tty->ldisc.chars_in_buffer(tty));
1103 #endif
1104
1105 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1106 return;
1107
1108 if (I_IXOFF(tty))
1109 rs_8xx_send_xchar(tty, STOP_CHAR(tty));
1110
1111 #ifdef modem_control
1112 if (tty->termios->c_cflag & CRTSCTS)
1113 info->MCR &= ~UART_MCR_RTS;
1114
1115 cli();
1116 serial_out(info, UART_MCR, info->MCR);
1117 sti();
1118 #endif
1119 }
1120
1121 static void rs_8xx_unthrottle(struct tty_struct * tty)
1122 {
1123 ser_info_t *info = (ser_info_t *)tty->driver_data;
1124 #ifdef SERIAL_DEBUG_THROTTLE
1125 char buf[64];
1126
1127 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1128 tty->ldisc.chars_in_buffer(tty));
1129 #endif
1130
1131 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1132 return;
1133
1134 if (I_IXOFF(tty)) {
1135 if (info->x_char)
1136 info->x_char = 0;
1137 else
1138 rs_8xx_send_xchar(tty, START_CHAR(tty));
1139 }
1140 #ifdef modem_control
1141 if (tty->termios->c_cflag & CRTSCTS)
1142 info->MCR |= UART_MCR_RTS;
1143 cli();
1144 serial_out(info, UART_MCR, info->MCR);
1145 sti();
1146 #endif
1147 }
1148
1149 /*
1150 * ------------------------------------------------------------
1151 * rs_ioctl() and friends
1152 * ------------------------------------------------------------
1153 */
1154
1155 #ifdef maybe
1156 /*
1157 * get_lsr_info - get line status register info
1158 *
1159 * Purpose: Let user call ioctl() to get info when the UART physically
1160 * is emptied. On bus types like RS485, the transmitter must
1161 * release the bus after transmitting. This must be done when
1162 * the transmit shift register is empty, not be done when the
1163 * transmit holding register is empty. This functionality
1164 * allows an RS485 driver to be written in user space.
1165 */
1166 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1167 {
1168 unsigned char status;
1169 unsigned int result;
1170
1171 cli();
1172 status = serial_in(info, UART_LSR);
1173 sti();
1174 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1175 return put_user(result,value);
1176 }
1177 #endif
1178
1179 static int get_modem_info(ser_info_t *info, unsigned int *value)
1180 {
1181 unsigned int result = 0;
1182 #ifdef modem_control
1183 unsigned char control, status;
1184
1185 control = info->MCR;
1186 cli();
1187 status = serial_in(info, UART_MSR);
1188 sti();
1189 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1190 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1191 #ifdef TIOCM_OUT1
1192 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1193 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1194 #endif
1195 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1196 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1197 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1198 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1199 #endif
1200 return put_user(result,value);
1201 }
1202
1203 static int set_modem_info(ser_info_t *info, unsigned int cmd,
1204 unsigned int *value)
1205 {
1206 int error;
1207 unsigned int arg;
1208
1209 error = get_user(arg, value);
1210 if (error)
1211 return error;
1212 #ifdef modem_control
1213 switch (cmd) {
1214 case TIOCMBIS:
1215 if (arg & TIOCM_RTS)
1216 info->MCR |= UART_MCR_RTS;
1217 if (arg & TIOCM_DTR)
1218 info->MCR |= UART_MCR_DTR;
1219 #ifdef TIOCM_OUT1
1220 if (arg & TIOCM_OUT1)
1221 info->MCR |= UART_MCR_OUT1;
1222 if (arg & TIOCM_OUT2)
1223 info->MCR |= UART_MCR_OUT2;
1224 #endif
1225 break;
1226 case TIOCMBIC:
1227 if (arg & TIOCM_RTS)
1228 info->MCR &= ~UART_MCR_RTS;
1229 if (arg & TIOCM_DTR)
1230 info->MCR &= ~UART_MCR_DTR;
1231 #ifdef TIOCM_OUT1
1232 if (arg & TIOCM_OUT1)
1233 info->MCR &= ~UART_MCR_OUT1;
1234 if (arg & TIOCM_OUT2)
1235 info->MCR &= ~UART_MCR_OUT2;
1236 #endif
1237 break;
1238 case TIOCMSET:
1239 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1240 #ifdef TIOCM_OUT1
1241 UART_MCR_OUT1 |
1242 UART_MCR_OUT2 |
1243 #endif
1244 UART_MCR_DTR))
1245 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1246 #ifdef TIOCM_OUT1
1247 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1248 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1249 #endif
1250 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1251 break;
1252 default:
1253 return -EINVAL;
1254 }
1255 cli();
1256 serial_out(info, UART_MCR, info->MCR);
1257 sti();
1258 #endif
1259 return 0;
1260 }
1261
1262 /* Sending a break is a two step process on the SMC/SCC. It is accomplished
1263 * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1264 * command. We take advantage of the begin/end functions to make this
1265 * happen.
1266 */
1267 static void begin_break(ser_info_t *info)
1268 {
1269 volatile cpm8260_t *cp;
1270 uint page, sblock;
1271 ushort num;
1272
1273 cp = cpmp;
1274
1275 if ((num = info->state->smc_scc_num) < SCC_NUM_BASE) {
1276 if (num == 0) {
1277 page = CPM_CR_SMC1_PAGE;
1278 sblock = CPM_CR_SMC1_SBLOCK;
1279 }
1280 else {
1281 page = CPM_CR_SMC2_PAGE;
1282 sblock = CPM_CR_SMC2_SBLOCK;
1283 }
1284 }
1285 else {
1286 num -= SCC_NUM_BASE;
1287 switch (num) {
1288 case 0:
1289 page = CPM_CR_SCC1_PAGE;
1290 sblock = CPM_CR_SCC1_SBLOCK;
1291 break;
1292 case 1:
1293 page = CPM_CR_SCC2_PAGE;
1294 sblock = CPM_CR_SCC2_SBLOCK;
1295 break;
1296 case 2:
1297 page = CPM_CR_SCC3_PAGE;
1298 sblock = CPM_CR_SCC3_SBLOCK;
1299 break;
1300 case 3:
1301 page = CPM_CR_SCC4_PAGE;
1302 sblock = CPM_CR_SCC4_SBLOCK;
1303 break;
1304 default: return;
1305 }
1306 }
1307 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0, CPM_CR_STOP_TX) | CPM_CR_FLG;
1308 while (cp->cp_cpcr & CPM_CR_FLG);
1309 }
1310
1311 static void end_break(ser_info_t *info)
1312 {
1313 volatile cpm8260_t *cp;
1314 uint page, sblock;
1315 ushort num;
1316
1317 cp = cpmp;
1318
1319 if ((num = info->state->smc_scc_num) < SCC_NUM_BASE) {
1320 if (num == 0) {
1321 page = CPM_CR_SMC1_PAGE;
1322 sblock = CPM_CR_SMC1_SBLOCK;
1323 }
1324 else {
1325 page = CPM_CR_SMC2_PAGE;
1326 sblock = CPM_CR_SMC2_SBLOCK;
1327 }
1328 }
1329 else {
1330 num -= SCC_NUM_BASE;
1331 switch (num) {
1332 case 0:
1333 page = CPM_CR_SCC1_PAGE;
1334 sblock = CPM_CR_SCC1_SBLOCK;
1335 break;
1336 case 1:
1337 page = CPM_CR_SCC2_PAGE;
1338 sblock = CPM_CR_SCC2_SBLOCK;
1339 break;
1340 case 2:
1341 page = CPM_CR_SCC3_PAGE;
1342 sblock = CPM_CR_SCC3_SBLOCK;
1343 break;
1344 case 3:
1345 page = CPM_CR_SCC4_PAGE;
1346 sblock = CPM_CR_SCC4_SBLOCK;
1347 break;
1348 default: return;
1349 }
1350 }
1351 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1352 while (cp->cp_cpcr & CPM_CR_FLG);
1353 }
1354
1355 /*
1356 * This routine sends a break character out the serial port.
1357 */
1358 static void send_break(ser_info_t *info, int duration)
1359 {
1360 current->state = TASK_INTERRUPTIBLE;
1361 #ifdef SERIAL_DEBUG_SEND_BREAK
1362 printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1363 #endif
1364 begin_break(info);
1365 schedule_timeout(duration);
1366 end_break(info);
1367 #ifdef SERIAL_DEBUG_SEND_BREAK
1368 printk("done jiffies=%lu\n", jiffies);
1369 #endif
1370 }
1371
1372
1373 static int rs_8xx_ioctl(struct tty_struct *tty, struct file * file,
1374 unsigned int cmd, unsigned long arg)
1375 {
1376 int error;
1377 ser_info_t *info = (ser_info_t *)tty->driver_data;
1378 int retval;
1379 struct async_icount cnow; /* kernel counter temps */
1380 struct serial_icounter_struct *p_cuser; /* user space */
1381
1382 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1383 return -ENODEV;
1384
1385 if ((cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1386 if (tty->flags & (1 << TTY_IO_ERROR))
1387 return -EIO;
1388 }
1389
1390 switch (cmd) {
1391 case TCSBRK: /* SVID version: non-zero arg --> no break */
1392 retval = tty_check_change(tty);
1393 if (retval)
1394 return retval;
1395 tty_wait_until_sent(tty, 0);
1396 if (signal_pending(current))
1397 return -EINTR;
1398 if (!arg) {
1399 send_break(info, HZ/4); /* 1/4 second */
1400 if (signal_pending(current))
1401 return -EINTR;
1402 }
1403 return 0;
1404 case TCSBRKP: /* support for POSIX tcsendbreak() */
1405 retval = tty_check_change(tty);
1406 if (retval)
1407 return retval;
1408 tty_wait_until_sent(tty, 0);
1409 if (signal_pending(current))
1410 return -EINTR;
1411 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1412 if (signal_pending(current))
1413 return -EINTR;
1414 return 0;
1415 case TIOCSBRK:
1416 retval = tty_check_change(tty);
1417 if (retval)
1418 return retval;
1419 tty_wait_until_sent(tty, 0);
1420 begin_break(info);
1421 return 0;
1422 case TIOCCBRK:
1423 retval = tty_check_change(tty);
1424 if (retval)
1425 return retval;
1426 end_break(info);
1427 return 0;
1428 case TIOCGSOFTCAR:
1429 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1430 case TIOCSSOFTCAR:
1431 error = get_user(arg, (unsigned int *) arg);
1432 if (error)
1433 return error;
1434 tty->termios->c_cflag =
1435 ((tty->termios->c_cflag & ~CLOCAL) |
1436 (arg ? CLOCAL : 0));
1437 return 0;
1438 case TIOCMGET:
1439 return get_modem_info(info, (unsigned int *) arg);
1440 case TIOCMBIS:
1441 case TIOCMBIC:
1442 case TIOCMSET:
1443 return set_modem_info(info, cmd, (unsigned int *) arg);
1444 #ifdef maybe
1445 case TIOCSERGETLSR: /* Get line status register */
1446 return get_lsr_info(info, (unsigned int *) arg);
1447 #endif
1448 /*
1449 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1450 * - mask passed in arg for lines of interest
1451 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1452 * Caller should use TIOCGICOUNT to see which one it was
1453 */
1454 case TIOCMIWAIT:
1455 #ifdef modem_control
1456 cli();
1457 /* note the counters on entry */
1458 cprev = info->state->icount;
1459 sti();
1460 while (1) {
1461 interruptible_sleep_on(&info->delta_msr_wait);
1462 /* see if a signal did it */
1463 if (signal_pending(current))
1464 return -ERESTARTSYS;
1465 cli();
1466 cnow = info->state->icount; /* atomic copy */
1467 sti();
1468 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1469 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1470 return -EIO; /* no change => error */
1471 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1472 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1473 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1474 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1475 return 0;
1476 }
1477 cprev = cnow;
1478 }
1479 /* NOTREACHED */
1480 #else
1481 return 0;
1482 #endif
1483
1484 /*
1485 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1486 * Return: write counters to the user passed counter struct
1487 * NB: both 1->0 and 0->1 transitions are counted except for
1488 * RI where only 0->1 is counted.
1489 */
1490 case TIOCGICOUNT:
1491 cli();
1492 cnow = info->state->icount;
1493 sti();
1494 p_cuser = (struct serial_icounter_struct *) arg;
1495 error = put_user(cnow.cts, &p_cuser->cts);
1496 if (error) return error;
1497 error = put_user(cnow.dsr, &p_cuser->dsr);
1498 if (error) return error;
1499 error = put_user(cnow.rng, &p_cuser->rng);
1500 if (error) return error;
1501 error = put_user(cnow.dcd, &p_cuser->dcd);
1502 if (error) return error;
1503 return 0;
1504
1505 default:
1506 return -ENOIOCTLCMD;
1507 }
1508 return 0;
1509 }
1510
1511 /* FIX UP modem control here someday......
1512 */
1513 static void rs_8xx_set_termios(struct tty_struct *tty, struct termios *old_termios)
1514 {
1515 ser_info_t *info = (ser_info_t *)tty->driver_data;
1516
1517 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1518 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1519 == RELEVANT_IFLAG(old_termios->c_iflag)))
1520 return;
1521
1522 change_speed(info);
1523
1524 #ifdef modem_control
1525 /* Handle transition to B0 status */
1526 if ((old_termios->c_cflag & CBAUD) &&
1527 !(tty->termios->c_cflag & CBAUD)) {
1528 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1529 cli();
1530 serial_out(info, UART_MCR, info->MCR);
1531 sti();
1532 }
1533
1534 /* Handle transition away from B0 status */
1535 if (!(old_termios->c_cflag & CBAUD) &&
1536 (tty->termios->c_cflag & CBAUD)) {
1537 info->MCR |= UART_MCR_DTR;
1538 if (!tty->hw_stopped ||
1539 !(tty->termios->c_cflag & CRTSCTS)) {
1540 info->MCR |= UART_MCR_RTS;
1541 }
1542 cli();
1543 serial_out(info, UART_MCR, info->MCR);
1544 sti();
1545 }
1546
1547 /* Handle turning off CRTSCTS */
1548 if ((old_termios->c_cflag & CRTSCTS) &&
1549 !(tty->termios->c_cflag & CRTSCTS)) {
1550 tty->hw_stopped = 0;
1551 rs_8xx_start(tty);
1552 }
1553 #endif
1554
1555 #if 0
1556 /*
1557 * No need to wake up processes in open wait, since they
1558 * sample the CLOCAL flag once, and don't recheck it.
1559 * XXX It's not clear whether the current behavior is correct
1560 * or not. Hence, this may change.....
1561 */
1562 if (!(old_termios->c_cflag & CLOCAL) &&
1563 (tty->termios->c_cflag & CLOCAL))
1564 wake_up_interruptible(&info->open_wait);
1565 #endif
1566 }
1567
1568 /*
1569 * ------------------------------------------------------------
1570 * rs_close()
1571 *
1572 * This routine is called when the serial port gets closed. First, we
1573 * wait for the last remaining data to be sent. Then, we unlink its
1574 * async structure from the interrupt chain if necessary, and we free
1575 * that IRQ if nothing is left in the chain.
1576 * ------------------------------------------------------------
1577 */
1578 static void rs_8xx_close(struct tty_struct *tty, struct file * filp)
1579 {
1580 ser_info_t *info = (ser_info_t *)tty->driver_data;
1581 struct serial_state *state;
1582 unsigned long flags;
1583 int idx;
1584 volatile smc_t *smcp;
1585 volatile scc_t *sccp;
1586
1587 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1588 return;
1589
1590 state = info->state;
1591
1592 save_flags(flags); cli();
1593
1594 if (tty_hung_up_p(filp)) {
1595 DBG_CNT("before DEC-hung");
1596 MOD_DEC_USE_COUNT;
1597 restore_flags(flags);
1598 return;
1599 }
1600
1601 #ifdef SERIAL_DEBUG_OPEN
1602 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1603 #endif
1604 if ((tty->count == 1) && (state->count != 1)) {
1605 /*
1606 * Uh, oh. tty->count is 1, which means that the tty
1607 * structure will be freed. state->count should always
1608 * be one in these conditions. If it's greater than
1609 * one, we've got real problems, since it means the
1610 * serial port won't be shutdown.
1611 */
1612 printk("rs_close: bad serial port count; tty->count is 1, "
1613 "state->count is %d\n", state->count);
1614 state->count = 1;
1615 }
1616 if (--state->count < 0) {
1617 printk("rs_close: bad serial port count for ttys%d: %d\n",
1618 info->line, state->count);
1619 state->count = 0;
1620 }
1621 if (state->count) {
1622 DBG_CNT("before DEC-2");
1623 MOD_DEC_USE_COUNT;
1624 restore_flags(flags);
1625 return;
1626 }
1627 info->flags |= ASYNC_CLOSING;
1628 /*
1629 * Save the termios structure, since this port may have
1630 * separate termios for callout and dialin.
1631 */
1632 if (info->flags & ASYNC_NORMAL_ACTIVE)
1633 info->state->normal_termios = *tty->termios;
1634 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1635 info->state->callout_termios = *tty->termios;
1636 /*
1637 * Now we wait for the transmit buffer to clear; and we notify
1638 * the line discipline to only process XON/XOFF characters.
1639 */
1640 tty->closing = 1;
1641 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1642 tty_wait_until_sent(tty, info->closing_wait);
1643 /*
1644 * At this point we stop accepting input. To do this, we
1645 * disable the receive line status interrupts, and tell the
1646 * interrupt driver to stop checking the data ready bit in the
1647 * line status register.
1648 */
1649 info->read_status_mask &= ~BD_SC_EMPTY;
1650 if (info->flags & ASYNC_INITIALIZED) {
1651 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
1652 smcp = &immr->im_smc[idx];
1653 smcp->smc_smcm &= ~SMCM_RX;
1654 smcp->smc_smcmr &= ~SMCMR_REN;
1655 }
1656 else {
1657 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
1658 sccp->scc_sccm &= ~UART_SCCM_RX;
1659 sccp->scc_gsmrl &= ~SCC_GSMRL_ENR;
1660 }
1661 /*
1662 * Before we drop DTR, make sure the UART transmitter
1663 * has completely drained; this is especially
1664 * important if there is a transmit FIFO!
1665 */
1666 rs_8xx_wait_until_sent(tty, info->timeout);
1667 }
1668 shutdown(info);
1669 if (tty->driver.flush_buffer)
1670 tty->driver.flush_buffer(tty);
1671 if (tty->ldisc.flush_buffer)
1672 tty->ldisc.flush_buffer(tty);
1673 tty->closing = 0;
1674 info->event = 0;
1675 info->tty = 0;
1676 if (info->blocked_open) {
1677 if (info->close_delay) {
1678 current->state = TASK_INTERRUPTIBLE;
1679 schedule_timeout(info->close_delay);
1680 }
1681 wake_up_interruptible(&info->open_wait);
1682 }
1683 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1684 ASYNC_CLOSING);
1685 wake_up_interruptible(&info->close_wait);
1686 MOD_DEC_USE_COUNT;
1687 restore_flags(flags);
1688 }
1689
1690 /*
1691 * rs_wait_until_sent() --- wait until the transmitter is empty
1692 */
1693 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout)
1694 {
1695 ser_info_t *info = (ser_info_t *)tty->driver_data;
1696 unsigned long orig_jiffies, char_time;
1697 /*int lsr;*/
1698 volatile cbd_t *bdp;
1699
1700 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1701 return;
1702
1703 #ifdef maybe
1704 if (info->state->type == PORT_UNKNOWN)
1705 return;
1706 #endif
1707
1708 orig_jiffies = jiffies;
1709 /*
1710 * Set the check interval to be 1/5 of the estimated time to
1711 * send a single character, and make it at least 1. The check
1712 * interval should also be less than the timeout.
1713 *
1714 * Note: we have to use pretty tight timings here to satisfy
1715 * the NIST-PCTS.
1716 */
1717 char_time = 1;
1718 if (timeout)
1719 char_time = MIN(char_time, timeout);
1720 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1721 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1722 printk("jiff=%lu...", jiffies);
1723 #endif
1724
1725 /* We go through the loop at least once because we can't tell
1726 * exactly when the last character exits the shifter. There can
1727 * be at least two characters waiting to be sent after the buffers
1728 * are empty.
1729 */
1730 do {
1731 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1732 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1733 #endif
1734 current->state = TASK_INTERRUPTIBLE;
1735 /* current->counter = 0; make us low-priority */
1736 schedule_timeout(char_time);
1737 if (signal_pending(current))
1738 break;
1739 if (timeout && ((orig_jiffies + timeout) < jiffies))
1740 break;
1741 bdp = info->tx_cur;
1742 } while (bdp->cbd_sc & BD_SC_READY);
1743 current->state = TASK_RUNNING;
1744 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1745 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1746 #endif
1747 }
1748
1749 /*
1750 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1751 */
1752 static void rs_8xx_hangup(struct tty_struct *tty)
1753 {
1754 ser_info_t *info = (ser_info_t *)tty->driver_data;
1755 struct serial_state *state = info->state;
1756
1757 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1758 return;
1759
1760 state = info->state;
1761
1762 rs_8xx_flush_buffer(tty);
1763 shutdown(info);
1764 info->event = 0;
1765 state->count = 0;
1766 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1767 info->tty = 0;
1768 wake_up_interruptible(&info->open_wait);
1769 }
1770
1771 /*
1772 * ------------------------------------------------------------
1773 * rs_open() and friends
1774 * ------------------------------------------------------------
1775 */
1776 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1777 ser_info_t *info)
1778 {
1779 #ifdef DO_THIS_LATER
1780 DECLARE_WAITQUEUE(wait, current);
1781 #endif
1782 struct serial_state *state = info->state;
1783 int retval;
1784 int do_clocal = 0;
1785
1786 /*
1787 * If the device is in the middle of being closed, then block
1788 * until it's done, and then try again.
1789 */
1790 if (tty_hung_up_p(filp) ||
1791 (info->flags & ASYNC_CLOSING)) {
1792 if (info->flags & ASYNC_CLOSING)
1793 interruptible_sleep_on(&info->close_wait);
1794 #ifdef SERIAL_DO_RESTART
1795 if (info->flags & ASYNC_HUP_NOTIFY)
1796 return -EAGAIN;
1797 else
1798 return -ERESTARTSYS;
1799 #else
1800 return -EAGAIN;
1801 #endif
1802 }
1803
1804 /*
1805 * If this is a callout device, then just make sure the normal
1806 * device isn't being used.
1807 */
1808 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1809 if (info->flags & ASYNC_NORMAL_ACTIVE)
1810 return -EBUSY;
1811 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1812 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1813 (info->session != current->session))
1814 return -EBUSY;
1815 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1816 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1817 (info->pgrp != current->pgrp))
1818 return -EBUSY;
1819 info->flags |= ASYNC_CALLOUT_ACTIVE;
1820 return 0;
1821 }
1822
1823 /*
1824 * If non-blocking mode is set, or the port is not enabled,
1825 * then make the check up front and then exit.
1826 * If this is an SMC port, we don't have modem control to wait
1827 * for, so just get out here.
1828 */
1829 if ((filp->f_flags & O_NONBLOCK) ||
1830 (tty->flags & (1 << TTY_IO_ERROR)) ||
1831 (info->state->smc_scc_num < SCC_NUM_BASE)) {
1832 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1833 return -EBUSY;
1834 info->flags |= ASYNC_NORMAL_ACTIVE;
1835 return 0;
1836 }
1837
1838 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1839 if (state->normal_termios.c_cflag & CLOCAL)
1840 do_clocal = 1;
1841 } else {
1842 if (tty->termios->c_cflag & CLOCAL)
1843 do_clocal = 1;
1844 }
1845
1846 /*
1847 * Block waiting for the carrier detect and the line to become
1848 * free (i.e., not in use by the callout). While we are in
1849 * this loop, state->count is dropped by one, so that
1850 * rs_close() knows when to free things. We restore it upon
1851 * exit, either normal or abnormal.
1852 */
1853 retval = 0;
1854 #ifdef DO_THIS_LATER
1855 add_wait_queue(&info->open_wait, &wait);
1856 #ifdef SERIAL_DEBUG_OPEN
1857 printk("block_til_ready before block: ttys%d, count = %d\n",
1858 state->line, state->count);
1859 #endif
1860 cli();
1861 if (!tty_hung_up_p(filp))
1862 state->count--;
1863 sti();
1864 info->blocked_open++;
1865 while (1) {
1866 cli();
1867 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1868 (tty->termios->c_cflag & CBAUD))
1869 serial_out(info, UART_MCR,
1870 serial_inp(info, UART_MCR) |
1871 (UART_MCR_DTR | UART_MCR_RTS));
1872 sti();
1873 set_current_state(TASK_INTERRUPTIBLE);
1874 if (tty_hung_up_p(filp) ||
1875 !(info->flags & ASYNC_INITIALIZED)) {
1876 #ifdef SERIAL_DO_RESTART
1877 if (info->flags & ASYNC_HUP_NOTIFY)
1878 retval = -EAGAIN;
1879 else
1880 retval = -ERESTARTSYS;
1881 #else
1882 retval = -EAGAIN;
1883 #endif
1884 break;
1885 }
1886 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1887 !(info->flags & ASYNC_CLOSING) &&
1888 (do_clocal || (serial_in(info, UART_MSR) &
1889 UART_MSR_DCD)))
1890 break;
1891 if (signal_pending(current)) {
1892 retval = -ERESTARTSYS;
1893 break;
1894 }
1895 #ifdef SERIAL_DEBUG_OPEN
1896 printk("block_til_ready blocking: ttys%d, count = %d\n",
1897 info->line, state->count);
1898 #endif
1899 schedule();
1900 }
1901 current->state = TASK_RUNNING;
1902 remove_wait_queue(&info->open_wait, &wait);
1903 if (!tty_hung_up_p(filp))
1904 state->count++;
1905 info->blocked_open--;
1906 #ifdef SERIAL_DEBUG_OPEN
1907 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1908 info->line, state->count);
1909 #endif
1910 #endif /* DO_THIS_LATER */
1911 if (retval)
1912 return retval;
1913 info->flags |= ASYNC_NORMAL_ACTIVE;
1914 return 0;
1915 }
1916
1917 static int get_async_struct(int line, ser_info_t **ret_info)
1918 {
1919 struct serial_state *sstate;
1920
1921 sstate = rs_table + line;
1922 if (sstate->info) {
1923 sstate->count++;
1924 *ret_info = (ser_info_t *)sstate->info;
1925 return 0;
1926 }
1927 else {
1928 return -ENOMEM;
1929 }
1930 }
1931
1932 /*
1933 * This routine is called whenever a serial port is opened. It
1934 * enables interrupts for a serial port, linking in its async structure into
1935 * the IRQ chain. It also performs the serial-specific
1936 * initialization for the tty structure.
1937 */
1938 static int rs_8xx_open(struct tty_struct *tty, struct file * filp)
1939 {
1940 ser_info_t *info;
1941 int retval, line;
1942
1943 line = MINOR(tty->device) - tty->driver.minor_start;
1944 if ((line < 0) || (line >= NR_PORTS))
1945 return -ENODEV;
1946 retval = get_async_struct(line, &info);
1947 if (retval)
1948 return retval;
1949 if (serial_paranoia_check(info, tty->device, "rs_open"))
1950 return -ENODEV;
1951
1952 #ifdef SERIAL_DEBUG_OPEN
1953 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1954 info->state->count);
1955 #endif
1956 tty->driver_data = info;
1957 info->tty = tty;
1958
1959 /*
1960 * Start up serial port
1961 */
1962 retval = startup(info);
1963 if (retval)
1964 return retval;
1965
1966 MOD_INC_USE_COUNT;
1967 retval = block_til_ready(tty, filp, info);
1968 if (retval) {
1969 #ifdef SERIAL_DEBUG_OPEN
1970 printk("rs_open returning after block_til_ready with %d\n",
1971 retval);
1972 #endif
1973 return retval;
1974 }
1975
1976 if ((info->state->count == 1) &&
1977 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1978 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1979 *tty->termios = info->state->normal_termios;
1980 else
1981 *tty->termios = info->state->callout_termios;
1982 change_speed(info);
1983 }
1984
1985 info->session = current->session;
1986 info->pgrp = current->pgrp;
1987
1988 #ifdef SERIAL_DEBUG_OPEN
1989 printk("rs_open ttys%d successful...", info->line);
1990 #endif
1991 return 0;
1992 }
1993
1994 /*
1995 * /proc fs routines....
1996 */
1997
1998 static int inline line_info(char *buf, struct serial_state *state)
1999 {
2000 #ifdef notdef
2001 struct async_struct *info = state->info, scr_info;
2002 char stat_buf[30], control, status;
2003 #endif
2004 int ret;
2005
2006 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2007 state->line,
2008 (state->smc_scc_num < SCC_NUM_BASE) ? "SMC" : "SCC",
2009 state->port, state->irq);
2010
2011 if (!state->port || (state->type == PORT_UNKNOWN)) {
2012 ret += sprintf(buf+ret, "\n");
2013 return ret;
2014 }
2015
2016 #ifdef notdef
2017 /*
2018 * Figure out the current RS-232 lines
2019 */
2020 if (!info) {
2021 info = &scr_info; /* This is just for serial_{in,out} */
2022
2023 info->magic = SERIAL_MAGIC;
2024 info->port = state->port;
2025 info->flags = state->flags;
2026 info->quot = 0;
2027 info->tty = 0;
2028 }
2029 cli();
2030 status = serial_in(info, UART_MSR);
2031 control = info ? info->MCR : serial_in(info, UART_MCR);
2032 sti();
2033
2034 stat_buf[0] = 0;
2035 stat_buf[1] = 0;
2036 if (control & UART_MCR_RTS)
2037 strcat(stat_buf, "|RTS");
2038 if (status & UART_MSR_CTS)
2039 strcat(stat_buf, "|CTS");
2040 if (control & UART_MCR_DTR)
2041 strcat(stat_buf, "|DTR");
2042 if (status & UART_MSR_DSR)
2043 strcat(stat_buf, "|DSR");
2044 if (status & UART_MSR_DCD)
2045 strcat(stat_buf, "|CD");
2046 if (status & UART_MSR_RI)
2047 strcat(stat_buf, "|RI");
2048
2049 if (info->quot) {
2050 ret += sprintf(buf+ret, " baud:%d",
2051 state->baud_base / info->quot);
2052 }
2053
2054 ret += sprintf(buf+ret, " tx:%d rx:%d",
2055 state->icount.tx, state->icount.rx);
2056
2057 if (state->icount.frame)
2058 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2059
2060 if (state->icount.parity)
2061 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2062
2063 if (state->icount.brk)
2064 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2065
2066 if (state->icount.overrun)
2067 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2068
2069 /*
2070 * Last thing is the RS-232 status lines
2071 */
2072 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2073 #endif
2074 return ret;
2075 }
2076
2077 int rs_8xx_read_proc(char *page, char **start, off_t off, int count,
2078 int *eof, void *data)
2079 {
2080 int i, len = 0;
2081 off_t begin = 0;
2082
2083 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2084 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2085 len += line_info(page + len, &rs_table[i]);
2086 if (len+begin > off+count)
2087 goto done;
2088 if (len+begin < off) {
2089 begin += len;
2090 len = 0;
2091 }
2092 }
2093 *eof = 1;
2094 done:
2095 if (off >= len+begin)
2096 return 0;
2097 *start = page + (begin-off);
2098 return ((count < begin+len-off) ? count : begin+len-off);
2099 }
2100
2101 /*
2102 * ---------------------------------------------------------------------
2103 * rs_init() and friends
2104 *
2105 * rs_init() is called at boot-time to initialize the serial driver.
2106 * ---------------------------------------------------------------------
2107 */
2108
2109 /*
2110 * This routine prints out the appropriate serial driver version
2111 * number, and identifies which options were configured into this
2112 * driver.
2113 */
2114 static _INLINE_ void show_serial_version(void)
2115 {
2116 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2117 }
2118
2119
2120 /*
2121 * The serial console driver used during boot. Note that these names
2122 * clash with those found in "serial.c", so we currently can't support
2123 * the 16xxx uarts and these at the same time. I will fix this to become
2124 * an indirect function call from tty_io.c (or something).
2125 */
2126
2127 #ifdef CONFIG_SERIAL_CONSOLE
2128
2129 /*
2130 * Print a string to the serial port trying not to disturb any possible
2131 * real use of the port...
2132 */
2133 static void serial_console_write(struct console *c, const char *s,
2134 unsigned count)
2135 {
2136 struct serial_state *ser;
2137 ser_info_t *info;
2138 unsigned i;
2139 volatile cbd_t *bdp, *bdbase;
2140 volatile smc_uart_t *up;
2141 volatile u_char *cp;
2142
2143 ser = rs_table + c->index;
2144
2145 /* If the port has been initialized for general use, we have
2146 * to use the buffer descriptors allocated there. Otherwise,
2147 * we simply use the single buffer allocated.
2148 */
2149 if ((info = (ser_info_t *)ser->info) != NULL) {
2150 bdp = info->tx_cur;
2151 bdbase = info->tx_bd_base;
2152 }
2153 else {
2154 /* Pointer to UART in parameter ram.
2155 */
2156 up = (smc_uart_t *)&immr->im_dprambase[ser->port];
2157
2158 /* Get the address of the host memory buffer.
2159 */
2160 bdp = bdbase = (cbd_t *)&immr->im_dprambase[up->smc_tbase];
2161 }
2162
2163 /*
2164 * We need to gracefully shut down the transmitter, disable
2165 * interrupts, then send our bytes out.
2166 */
2167
2168 /*
2169 * Now, do each character. This is not as bad as it looks
2170 * since this is a holding FIFO and not a transmitting FIFO.
2171 * We could add the complexity of filling the entire transmit
2172 * buffer, but we would just wait longer between accesses......
2173 */
2174 for (i = 0; i < count; i++, s++) {
2175 /* Wait for transmitter fifo to empty.
2176 * Ready indicates output is ready, and xmt is doing
2177 * that, not that it is ready for us to send.
2178 */
2179 while (bdp->cbd_sc & BD_SC_READY);
2180 /* Send the character out. */
2181 cp = __va(bdp->cbd_bufaddr);
2182 *cp = *s;
2183
2184 bdp->cbd_datlen = 1;
2185 bdp->cbd_sc |= BD_SC_READY;
2186
2187 if (bdp->cbd_sc & BD_SC_WRAP)
2188 bdp = bdbase;
2189 else
2190 bdp++;
2191
2192 /* if a LF, also do CR... */
2193 if (*s == 10) {
2194 while (bdp->cbd_sc & BD_SC_READY);
2195 cp = __va(bdp->cbd_bufaddr);
2196 *cp = 13;
2197 bdp->cbd_datlen = 1;
2198 bdp->cbd_sc |= BD_SC_READY;
2199
2200 if (bdp->cbd_sc & BD_SC_WRAP) {
2201 bdp = bdbase;
2202 }
2203 else {
2204 bdp++;
2205 }
2206 }
2207 }
2208
2209 /*
2210 * Finally, Wait for transmitter & holding register to empty
2211 * and restore the IER
2212 */
2213 while (bdp->cbd_sc & BD_SC_READY);
2214
2215 if (info)
2216 info->tx_cur = (cbd_t *)bdp;
2217 }
2218
2219 /*
2220 * Receive character from the serial port. This only works well
2221 * before the port is initialize for real use.
2222 */
2223 static int serial_console_wait_key(struct console *co)
2224 {
2225 struct serial_state *ser;
2226 u_char c, *cp;
2227 ser_info_t *info;
2228 volatile cbd_t *bdp;
2229 volatile smc_uart_t *up;
2230
2231 ser = rs_table + co->index;
2232
2233 /* Pointer to UART in parameter ram.
2234 */
2235 up = (smc_uart_t *)&immr->im_dprambase[ser->port];
2236
2237 /* Get the address of the host memory buffer.
2238 * If the port has been initialized for general use, we must
2239 * use information from the port structure.
2240 */
2241 if ((info = (ser_info_t *)ser->info))
2242 bdp = info->rx_cur;
2243 else
2244 bdp = (cbd_t *)&immr->im_dprambase[up->smc_rbase];
2245
2246 /*
2247 * We need to gracefully shut down the receiver, disable
2248 * interrupts, then read the input.
2249 */
2250 while (bdp->cbd_sc & BD_SC_EMPTY); /* Wait for a character */
2251 cp = __va(bdp->cbd_bufaddr);
2252
2253 if (info) {
2254 if (bdp->cbd_sc & BD_SC_WRAP) {
2255 bdp = info->rx_bd_base;
2256 }
2257 else {
2258 bdp++;
2259 }
2260 info->rx_cur = (cbd_t *)bdp;
2261 }
2262
2263 c = *cp;
2264 return((int)c);
2265 }
2266
2267 static kdev_t serial_console_device(struct console *c)
2268 {
2269 return MKDEV(TTYAUX_MAJOR, 64 + c->index);
2270 }
2271
2272
2273 static struct console sercons = {
2274 name: "ttyS",
2275 write: serial_console_write,
2276 device: serial_console_device,
2277 wait_key: serial_console_wait_key,
2278 setup: serial_console_setup,
2279 flags: CON_PRINTBUFFER,
2280 index: CONFIG_SERIAL_CONSOLE_PORT,
2281 };
2282
2283 /*
2284 * Register console.
2285 */
2286 long __init console_8xx_init(long kmem_start, long kmem_end)
2287 {
2288 register_console(&sercons);
2289 return kmem_start;
2290 }
2291
2292 #endif
2293
2294 /* Default console baud rate as determined by the board information
2295 * structure.
2296 */
2297 static int baud_idx;
2298
2299 /*
2300 * The serial driver boot-time initialization code!
2301 */
2302 int __init rs_8xx_init(void)
2303 {
2304 struct serial_state * state;
2305 ser_info_t *info;
2306 uint mem_addr, dp_addr;
2307 int i, j, idx;
2308 uint page, sblock;
2309 volatile cbd_t *bdp;
2310 volatile cpm8260_t *cp;
2311 volatile smc_t *sp;
2312 volatile smc_uart_t *up;
2313 volatile scc_t *scp;
2314 volatile scc_uart_t *sup;
2315 volatile immap_t *immap;
2316 volatile iop8260_t *io;
2317
2318 init_bh(SERIAL_BH, do_serial_bh);
2319
2320 show_serial_version();
2321
2322 /* Initialize the tty_driver structure */
2323
2324 /*memset(&serial_driver, 0, sizeof(struct tty_driver));*/
2325 __clear_user(&serial_driver,sizeof(struct tty_driver));
2326 serial_driver.magic = TTY_DRIVER_MAGIC;
2327 serial_driver.driver_name = "serial";
2328 serial_driver.name = "ttyS";
2329 serial_driver.major = TTY_MAJOR;
2330 serial_driver.minor_start = 64;
2331 serial_driver.num = NR_PORTS;
2332 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2333 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2334 serial_driver.init_termios = tty_std_termios;
2335 serial_driver.init_termios.c_cflag =
2336 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2337 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2338 serial_driver.refcount = &serial_refcount;
2339 serial_driver.table = serial_table;
2340 serial_driver.termios = serial_termios;
2341 serial_driver.termios_locked = serial_termios_locked;
2342
2343 serial_driver.open = rs_8xx_open;
2344 serial_driver.close = rs_8xx_close;
2345 serial_driver.write = rs_8xx_write;
2346 serial_driver.put_char = rs_8xx_put_char;
2347 serial_driver.write_room = rs_8xx_write_room;
2348 serial_driver.chars_in_buffer = rs_8xx_chars_in_buffer;
2349 serial_driver.flush_buffer = rs_8xx_flush_buffer;
2350 serial_driver.ioctl = rs_8xx_ioctl;
2351 serial_driver.throttle = rs_8xx_throttle;
2352 serial_driver.unthrottle = rs_8xx_unthrottle;
2353 serial_driver.send_xchar = rs_8xx_send_xchar;
2354 serial_driver.set_termios = rs_8xx_set_termios;
2355 serial_driver.stop = rs_8xx_stop;
2356 serial_driver.start = rs_8xx_start;
2357 serial_driver.hangup = rs_8xx_hangup;
2358 serial_driver.wait_until_sent = rs_8xx_wait_until_sent;
2359 serial_driver.read_proc = rs_8xx_read_proc;
2360
2361 /*
2362 * The callout device is just like normal device except for
2363 * major number and the subtype code.
2364 */
2365 callout_driver = serial_driver;
2366 callout_driver.name = "cua";
2367 callout_driver.major = TTYAUX_MAJOR;
2368 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2369 callout_driver.read_proc = 0;
2370 callout_driver.proc_entry = 0;
2371
2372 if (tty_register_driver(&serial_driver))
2373 panic("Couldn't register serial driver\n");
2374 if (tty_register_driver(&callout_driver))
2375 panic("Couldn't register callout driver\n");
2376
2377 immap = immr;
2378 cp = &immap->im_cpm;
2379 io = &immap->im_ioport;
2380
2381 /* This should have been done long ago by the early boot code,
2382 * but do it again to make sure.
2383 */
2384 *(ushort *)(&immap->im_dprambase[PROFF_SMC1_BASE]) = PROFF_SMC1;
2385 *(ushort *)(&immap->im_dprambase[PROFF_SMC2_BASE]) = PROFF_SMC2;
2386
2387 /* Geeze, here we go....Picking I/O port bits....Lots of
2388 * choices. If you don't like mine, pick your own.
2389 * Configure SMCs Tx/Rx. SMC1 is only on Port D, SMC2 is
2390 * only on Port A. You either pick 'em, or not.
2391 */
2392 io->iop_ppard |= 0x00c00000;
2393 io->iop_pdird |= 0x00400000;
2394 io->iop_pdird &= ~0x00800000;
2395 io->iop_psord &= ~0x00c00000;
2396 #if USE_SMC2
2397 io->iop_ppara |= 0x00c00000;
2398 io->iop_pdira |= 0x00400000;
2399 io->iop_pdira &= ~0x00800000;
2400 io->iop_psora &= ~0x00c00000;
2401 #endif
2402
2403 /* Configure SCC2 and SCC3. Be careful about the fine print.
2404 * Secondary options are only available when you take away
2405 * the primary option. Unless the pins are used for something
2406 * else, SCC2 and SCC3 are on Port B.
2407 * Port B, 8 - SCC3 TxD
2408 * Port B, 12 - SCC2 TxD
2409 * Port B, 14 - SCC3 RxD
2410 * Port B, 15 - SCC2 RxD
2411 */
2412 io->iop_pparb |= 0x008b0000;
2413 io->iop_pdirb |= 0x00880000;
2414 io->iop_psorb |= 0x00880000;
2415 io->iop_pdirb &= ~0x00030000;
2416 io->iop_psorb &= ~0x00030000;
2417
2418 /* Wire BRG1 to SMC1 and BRG2 to SMC2.
2419 */
2420 immap->im_cpmux.cmx_smr = 0;
2421
2422 /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and
2423 * BRG4 to SCC3.
2424 */
2425 immap->im_cpmux.cmx_scr &= ~0x00ffff00;
2426 immap->im_cpmux.cmx_scr |= 0x00121b00;
2427
2428 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2429 state->magic = SSTATE_MAGIC;
2430 state->line = i;
2431 state->type = PORT_UNKNOWN;
2432 state->custom_divisor = 0;
2433 state->close_delay = 5*HZ/10;
2434 state->closing_wait = 30*HZ;
2435 state->callout_termios = callout_driver.init_termios;
2436 state->normal_termios = serial_driver.init_termios;
2437 state->icount.cts = state->icount.dsr =
2438 state->icount.rng = state->icount.dcd = 0;
2439 state->icount.rx = state->icount.tx = 0;
2440 state->icount.frame = state->icount.parity = 0;
2441 state->icount.overrun = state->icount.brk = 0;
2442 printk(KERN_INFO "ttyS%02d at 0x%04x is a %s\n",
2443 i, state->port,
2444 (state->smc_scc_num < SCC_NUM_BASE) ? "SMC" : "SCC");
2445 #ifdef CONFIG_SERIAL_CONSOLE
2446 /* If we just printed the message on the console port, and
2447 * we are about to initialize it for general use, we have
2448 * to wait a couple of character times for the CR/NL to
2449 * make it out of the transmit buffer.
2450 */
2451 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2452 mdelay(2);
2453 #endif
2454 info = kmalloc(sizeof(ser_info_t), GFP_KERNEL);
2455 if (info) {
2456 /*memset(info, 0, sizeof(ser_info_t));*/
2457 __clear_user(info,sizeof(ser_info_t));
2458 init_waitqueue_head(&info->open_wait);
2459 init_waitqueue_head(&info->close_wait);
2460 info->magic = SERIAL_MAGIC;
2461 info->flags = state->flags;
2462 info->tqueue.routine = do_softint;
2463 info->tqueue.data = info;
2464 info->tqueue_hangup.routine = do_serial_hangup;
2465 info->tqueue_hangup.data = info;
2466 info->line = i;
2467 info->state = state;
2468 state->info = (struct async_struct *)info;
2469
2470 /* We need to allocate a transmit and receive buffer
2471 * descriptors from dual port ram, and a character
2472 * buffer area from host mem.
2473 */
2474 dp_addr = m8260_cpm_dpalloc(sizeof(cbd_t) * RX_NUM_FIFO, 8);
2475
2476 /* Allocate space for FIFOs in the host memory.
2477 */
2478 mem_addr = m8260_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE, 1);
2479
2480 /* Set the physical address of the host memory
2481 * buffers in the buffer descriptors, and the
2482 * virtual address for us to work with.
2483 */
2484 bdp = (cbd_t *)&immap->im_dprambase[dp_addr];
2485 info->rx_cur = info->rx_bd_base = (cbd_t *)bdp;
2486
2487 for (j=0; j<(RX_NUM_FIFO-1); j++) {
2488 bdp->cbd_bufaddr = __pa(mem_addr);
2489 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
2490 mem_addr += RX_BUF_SIZE;
2491 bdp++;
2492 }
2493 bdp->cbd_bufaddr = __pa(mem_addr);
2494 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2495
2496 if ((idx = state->smc_scc_num) < SCC_NUM_BASE) {
2497 sp = &immap->im_smc[idx];
2498 up = (smc_uart_t *)&immap->im_dprambase[state->port];
2499 up->smc_rbase = dp_addr;
2500 }
2501 else {
2502 scp = &immap->im_scc[idx - SCC_IDX_BASE];
2503 sup = (scc_uart_t *)&immap->im_dprambase[state->port];
2504 sup->scc_genscc.scc_rbase = dp_addr;
2505 }
2506
2507 dp_addr = m8260_cpm_dpalloc(sizeof(cbd_t) * TX_NUM_FIFO, 8);
2508
2509 /* Allocate space for FIFOs in the host memory.
2510 */
2511 mem_addr = m8260_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE, 1);
2512
2513 /* Set the physical address of the host memory
2514 * buffers in the buffer descriptors, and the
2515 * virtual address for us to work with.
2516 */
2517 bdp = (cbd_t *)&immap->im_dprambase[dp_addr];
2518 info->tx_cur = info->tx_bd_base = (cbd_t *)bdp;
2519
2520 for (j=0; j<(TX_NUM_FIFO-1); j++) {
2521 bdp->cbd_bufaddr = __pa(mem_addr);
2522 bdp->cbd_sc = BD_SC_INTRPT;
2523 mem_addr += TX_BUF_SIZE;
2524 bdp++;
2525 }
2526 bdp->cbd_bufaddr = __pa(mem_addr);
2527 bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
2528
2529 if (idx < SCC_NUM_BASE) {
2530 up->smc_tbase = dp_addr;
2531
2532 /* Set up the uart parameters in the
2533 * parameter ram.
2534 */
2535 up->smc_rfcr = CPMFCR_GBL | CPMFCR_EB;
2536 up->smc_tfcr = CPMFCR_GBL | CPMFCR_EB;
2537
2538 /* Set this to 1 for now, so we get single
2539 * character interrupts. Using idle charater
2540 * time requires some additional tuning.
2541 */
2542 up->smc_mrblr = 1;
2543 up->smc_maxidl = 0;
2544 up->smc_brkcr = 1;
2545
2546 /* Send the CPM an initialize command.
2547 */
2548 if (state->smc_scc_num == 0) {
2549 page = CPM_CR_SMC1_PAGE;
2550 sblock = CPM_CR_SMC1_SBLOCK;
2551 }
2552 else {
2553 page = CPM_CR_SMC2_PAGE;
2554 sblock = CPM_CR_SMC2_SBLOCK;
2555 }
2556
2557 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0,
2558 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2559 while (cp->cp_cpcr & CPM_CR_FLG);
2560
2561 /* Set UART mode, 8 bit, no parity, one stop.
2562 * Enable receive and transmit.
2563 */
2564 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2565
2566 /* Disable all interrupts and clear all pending
2567 * events.
2568 */
2569 sp->smc_smcm = 0;
2570 sp->smc_smce = 0xff;
2571 }
2572 else {
2573 sup->scc_genscc.scc_tbase = dp_addr;
2574
2575 /* Set up the uart parameters in the
2576 * parameter ram.
2577 */
2578 sup->scc_genscc.scc_rfcr = CPMFCR_GBL | CPMFCR_EB;
2579 sup->scc_genscc.scc_tfcr = CPMFCR_GBL | CPMFCR_EB;
2580
2581 /* Set this to 1 for now, so we get single
2582 * character interrupts. Using idle charater
2583 * time requires some additional tuning.
2584 */
2585 sup->scc_genscc.scc_mrblr = 1;
2586 sup->scc_maxidl = 0;
2587 sup->scc_brkcr = 1;
2588 sup->scc_parec = 0;
2589 sup->scc_frmec = 0;
2590 sup->scc_nosec = 0;
2591 sup->scc_brkec = 0;
2592 sup->scc_uaddr1 = 0;
2593 sup->scc_uaddr2 = 0;
2594 sup->scc_toseq = 0;
2595 sup->scc_char1 = 0x8000;
2596 sup->scc_char2 = 0x8000;
2597 sup->scc_char3 = 0x8000;
2598 sup->scc_char4 = 0x8000;
2599 sup->scc_char5 = 0x8000;
2600 sup->scc_char6 = 0x8000;
2601 sup->scc_char7 = 0x8000;
2602 sup->scc_char8 = 0x8000;
2603 sup->scc_rccm = 0xc0ff;
2604
2605 /* Send the CPM an initialize command.
2606 */
2607 if (state->smc_scc_num == 2) {
2608 page = CPM_CR_SCC2_PAGE;
2609 sblock = CPM_CR_SCC2_SBLOCK;
2610 }
2611 else {
2612 page = CPM_CR_SCC3_PAGE;
2613 sblock = CPM_CR_SCC3_SBLOCK;
2614 }
2615
2616 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0,
2617 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2618 while (cp->cp_cpcr & CPM_CR_FLG);
2619
2620 /* Set UART mode, 8 bit, no parity, one stop.
2621 * Enable receive and transmit.
2622 */
2623 scp->scc_gsmrh = 0;
2624 scp->scc_gsmrl =
2625 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2626
2627 /* Disable all interrupts and clear all pending
2628 * events.
2629 */
2630 scp->scc_sccm = 0;
2631 scp->scc_scce = 0xffff;
2632 scp->scc_dsr = 0x7e7e;
2633 scp->scc_pmsr = 0x3000;
2634 }
2635
2636 /* Install interrupt handler.
2637 */
2638 request_8xxirq(state->irq, rs_8xx_interrupt, 0, "uart", info);
2639
2640 /* Set up the baud rate generator.
2641 */
2642 m8260_cpm_setbrg(state->smc_scc_num,
2643 baud_table[baud_idx]);
2644
2645 /* If the port is the console, enable Rx and Tx.
2646 */
2647 #ifdef CONFIG_SERIAL_CONSOLE
2648 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2649 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2650 #endif
2651 }
2652 }
2653 return 0;
2654 }
2655
2656 /* This must always be called before the rs_8xx_init() function, otherwise
2657 * it blows away the port control information.
2658 */
2659 static int __init serial_console_setup(struct console *co, char *options)
2660 {
2661 struct serial_state *ser;
2662 uint mem_addr, dp_addr, bidx;
2663 volatile cbd_t *bdp;
2664 volatile cpm8260_t *cp;
2665 volatile immap_t *immap;
2666 volatile smc_t *sp;
2667 volatile smc_uart_t *up;
2668 volatile iop8260_t *io;
2669 bd_t *bd;
2670
2671 bd = (bd_t *)__res;
2672
2673 for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
2674 if (bd->bi_baudrate == baud_table[bidx])
2675 break;
2676
2677 co->cflag = CREAD|CLOCAL|bidx|CS8;
2678 baud_idx = bidx;
2679
2680 ser = rs_table + co->index;
2681
2682
2683 immap = immr;
2684 cp = &immap->im_cpm;
2685 io = &immap->im_ioport;
2686
2687 /* This should have been done long ago by the early boot code,
2688 * but do it again to make sure.
2689 */
2690 *(ushort *)(&immap->im_dprambase[PROFF_SMC1_BASE]) = PROFF_SMC1;
2691 *(ushort *)(&immap->im_dprambase[PROFF_SMC2_BASE]) = PROFF_SMC2;
2692
2693 /* Right now, assume we are using SMCs.
2694 */
2695 sp = &immap->im_smc[ser->smc_scc_num];
2696
2697 /* When we get here, the CPM has been reset, so we need
2698 * to configure the port.
2699 * We need to allocate a transmit and receive buffer descriptor
2700 * from dual port ram, and a character buffer area from host mem.
2701 */
2702 up = (smc_uart_t *)&immap->im_dprambase[ser->port];
2703
2704 /* Disable transmitter/receiver.
2705 */
2706 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
2707
2708 /* Use Port D for SMC1 instead of other functions.
2709 */
2710 io->iop_ppard |= 0x00c00000;
2711 io->iop_pdird |= 0x00400000;
2712 io->iop_pdird &= ~0x00800000;
2713 io->iop_psord &= ~0x00c00000;
2714
2715 /* Allocate space for two buffer descriptors in the DP ram.
2716 */
2717 dp_addr = m8260_cpm_dpalloc(sizeof(cbd_t) * 2, 8);
2718
2719 /* Allocate space for two 2 byte FIFOs in the host memory.
2720 */
2721 mem_addr = m8260_cpm_hostalloc(4, 1);
2722
2723 /* Set the physical address of the host memory buffers in
2724 * the buffer descriptors.
2725 */
2726 bdp = (cbd_t *)&immap->im_dprambase[dp_addr];
2727 bdp->cbd_bufaddr = __pa(mem_addr);
2728 (bdp+1)->cbd_bufaddr = __pa(mem_addr+2);
2729
2730 /* For the receive, set empty and wrap.
2731 * For transmit, set wrap.
2732 */
2733 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
2734 (bdp+1)->cbd_sc = BD_SC_WRAP;
2735
2736 /* Set up the uart parameters in the parameter ram.
2737 */
2738 up->smc_rbase = dp_addr; /* Base of receive buffer desc. */
2739 up->smc_tbase = dp_addr+sizeof(cbd_t); /* Base of xmt buffer desc. */
2740 up->smc_rfcr = CPMFCR_GBL | CPMFCR_EB;
2741 up->smc_tfcr = CPMFCR_GBL | CPMFCR_EB;
2742
2743 /* Set this to 1 for now, so we get single character interrupts.
2744 */
2745 up->smc_mrblr = 1; /* receive buffer length */
2746 up->smc_maxidl = 0; /* wait forever for next char */
2747
2748 /* Send the CPM an initialize command.
2749 */
2750 cp->cp_cpcr = mk_cr_cmd(CPM_CR_SMC1_PAGE, CPM_CR_SMC1_SBLOCK, 0,
2751 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2752 while (cp->cp_cpcr & CPM_CR_FLG);
2753
2754 /* Set UART mode, 8 bit, no parity, one stop.
2755 * Enable receive and transmit.
2756 */
2757 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2758
2759 /* Set up the baud rate generator.
2760 */
2761 m8260_cpm_setbrg(ser->smc_scc_num, bd->bi_baudrate);
2762
2763 /* And finally, enable Rx and Tx.
2764 */
2765 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2766
2767 return 0;
2768 }
2769