1 /*
2  *  drivers/char/serial_tx3912.c
3  *
4  *  Copyright (C) 2001 Steven J. Hill (sjhill@realitydiluted.com)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Serial driver for TMPR3912/05 and PR31700 processors
11  */
12 #include <linux/init.h>
13 #include <linux/config.h>
14 #include <linux/tty.h>
15 #include <linux/major.h>
16 #include <linux/console.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/serial.h>
20 #include <asm/io.h>
21 #include <asm/uaccess.h>
22 #include <asm/delay.h>
23 #include <asm/tx3912.h>
24 #include "serial_tx3912.h"
25 
26 /*
27  * Forward declarations for serial routines
28  */
29 static void rs_disable_tx_interrupts (void * ptr);
30 static void rs_enable_tx_interrupts (void * ptr);
31 static void rs_disable_rx_interrupts (void * ptr);
32 static void rs_enable_rx_interrupts (void * ptr);
33 static int rs_get_CD (void * ptr);
34 static void rs_shutdown_port (void * ptr);
35 static int rs_set_real_termios (void *ptr);
36 static int rs_chars_in_buffer (void * ptr);
37 static void rs_hungup (void *ptr);
38 static void rs_close (void *ptr);
39 
40 /*
41  * Used by generic serial driver to access hardware
42  */
43 static struct real_driver rs_real_driver = {
44 	disable_tx_interrupts: rs_disable_tx_interrupts,
45 	enable_tx_interrupts:  rs_enable_tx_interrupts,
46 	disable_rx_interrupts: rs_disable_rx_interrupts,
47 	enable_rx_interrupts:  rs_enable_rx_interrupts,
48 	get_CD:                rs_get_CD,
49 	shutdown_port:         rs_shutdown_port,
50 	set_real_termios:      rs_set_real_termios,
51 	chars_in_buffer:       rs_chars_in_buffer,
52 	close:                 rs_close,
53 	hungup:                rs_hungup,
54 };
55 
56 /*
57  * Structures and usage counts
58  */
59 static struct tty_driver rs_driver, rs_callout_driver;
60 static struct tty_struct **rs_tty;
61 static struct termios **rs_termios;
62 static struct termios **rs_termios_locked;
63 static struct rs_port *rs_port;
64 static int rs_refcount;
65 static int rs_initialized;
66 
67 
68 /*
69  * Receive a character
70  */
receive_char_pio(struct rs_port * port)71 static inline void receive_char_pio(struct rs_port *port)
72 {
73 	struct tty_struct *tty = port->gs.tty;
74 	unsigned char ch;
75 	int counter = 2048;
76 
77 	/* While there are characters */
78 	while (counter > 0) {
79 		if (!(inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_RXHOLDFULL))
80 			break;
81 		ch = inb(TX3912_UARTA_DATA);
82 		if (tty->flip.count < TTY_FLIPBUF_SIZE) {
83 			*tty->flip.char_buf_ptr++ = ch;
84 			*tty->flip.flag_buf_ptr++ = 0;
85 			tty->flip.count++;
86 		}
87 		udelay(1);
88 		counter--;
89 	}
90 
91 	tty_flip_buffer_push(tty);
92 }
93 
94 /*
95  * Transmit a character
96  */
transmit_char_pio(struct rs_port * port)97 static inline void transmit_char_pio(struct rs_port *port)
98 {
99 	/* TX while bytes available */
100 	for (;;) {
101 		if (!(inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_EMPTY))
102 			break;
103 		else if (port->x_char) {
104 			outb(port->x_char, TX3912_UARTA_DATA);
105 			port->icount.tx++;
106 			port->x_char = 0;
107 		}
108 		else if (port->gs.xmit_cnt <= 0 || port->gs.tty->stopped ||
109 		    port->gs.tty->hw_stopped) {
110 			break;
111 		}
112 		else {
113 			outb(port->gs.xmit_buf[port->gs.xmit_tail++],
114 				TX3912_UARTA_DATA);
115 			port->icount.tx++;
116 			port->gs.xmit_tail &= SERIAL_XMIT_SIZE-1;
117 			if (--port->gs.xmit_cnt <= 0) {
118 				break;
119 			}
120 		}
121 		udelay(10);
122 	}
123 
124 	if (port->gs.xmit_cnt <= 0 || port->gs.tty->stopped ||
125 	     port->gs.tty->hw_stopped) {
126 		rs_disable_tx_interrupts(port);
127 	}
128 
129         if (port->gs.xmit_cnt <= port->gs.wakeup_chars) {
130 		tty_wakeup(port->gs.tty);
131                 rs_dprintk(TX3912_UART_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
132                             port->gs.wakeup_chars);
133        	}
134 }
135 
136 /*
137  * We don't have MSR
138  */
check_modem_status(struct rs_port * port)139 static inline void check_modem_status(struct rs_port *port)
140 {
141 	wake_up_interruptible(&port->gs.open_wait);
142 }
143 
144 /*
145  * RX interrupt handler
146  */
rs_rx_interrupt(int irq,void * dev_id,struct pt_regs * regs)147 static inline void rs_rx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
148 {
149 	unsigned long flags, status;
150 
151 	save_and_cli(flags);
152 
153 	rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "rs_rx_interrupt...");
154 
155 	/* Get the interrupts */
156 	status = inl(TX3912_INT2_STATUS);
157 
158 	/* Clear any interrupts we might be about to handle */
159 	outl(TX3912_INT2_UARTA_RX_BITS, TX3912_INT2_CLEAR);
160 
161 	if(!rs_port || !rs_port->gs.tty) {
162 		restore_flags(flags);
163 		return;
164 	}
165 
166 	/* RX Receiver Holding Register Overrun */
167 	if(status & TX3912_INT2_UARTATXOVERRUNINT) {
168 		rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "overrun");
169 		rs_port->icount.overrun++;
170 	}
171 
172 	/* RX Frame Error */
173 	if(status & TX3912_INT2_UARTAFRAMEERRINT) {
174 		rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "frame error");
175 		rs_port->icount.frame++;
176 	}
177 
178 	/* Break signal received */
179 	if(status & TX3912_INT2_UARTABREAKINT) {
180 		rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "break");
181 		rs_port->icount.brk++;
182       	}
183 
184 	/* RX Parity Error */
185 	if(status & TX3912_INT2_UARTAPARITYINT) {
186 		rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "parity error");
187 		rs_port->icount.parity++;
188 	}
189 
190 	/* Byte received */
191 	if(status & TX3912_INT2_UARTARXINT) {
192 		receive_char_pio(rs_port);
193 	}
194 
195 	restore_flags(flags);
196 
197 	rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "end.\n");
198 }
199 
200 /*
201  * TX interrupt handler
202  */
rs_tx_interrupt(int irq,void * dev_id,struct pt_regs * regs)203 static inline void rs_tx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
204 {
205 	unsigned long flags, status;
206 
207 	save_and_cli(flags);
208 
209 	rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "rs_tx_interrupt...");
210 
211 	/* Get the interrupts */
212 	status = inl(TX3912_INT2_STATUS);
213 
214 	if(!rs_port || !rs_port->gs.tty) {
215 		restore_flags(flags);
216 		return;
217 	}
218 
219 	/* Clear interrupts */
220 	outl(TX3912_INT2_UARTA_TX_BITS, TX3912_INT2_CLEAR);
221 
222 	/* TX holding register empty - transmit a byte */
223 	if(status & TX3912_INT2_UARTAEMPTYINT) {
224 		transmit_char_pio(rs_port);
225 	}
226 
227 	/* TX Transmit Holding Register Overrun (shouldn't happen) */
228 	if(status & TX3912_INT2_UARTATXOVERRUNINT) {
229 		printk( "rs_tx_interrupt: TX overrun\n");
230 	}
231 
232 	restore_flags(flags);
233 
234 	rs_dprintk(TX3912_UART_DEBUG_INTERRUPTS, "end.\n");
235 }
236 
237 /*
238  * Here are the routines that actually interface with the generic driver
239  */
rs_disable_tx_interrupts(void * ptr)240 static void rs_disable_tx_interrupts (void * ptr)
241 {
242 	unsigned long flags;
243 
244 	save_and_cli(flags);
245 
246 	outl(inl(TX3912_INT2_ENABLE) & ~TX3912_INT2_UARTA_TX_BITS,
247 		TX3912_INT2_ENABLE);
248 	outl(TX3912_INT2_UARTA_TX_BITS, TX3912_INT2_CLEAR);
249 
250 	restore_flags(flags);
251 }
252 
rs_enable_tx_interrupts(void * ptr)253 static void rs_enable_tx_interrupts (void * ptr)
254 {
255 	unsigned long flags;
256 
257 	save_and_cli(flags);
258 
259 	outl(TX3912_INT2_UARTA_TX_BITS, TX3912_INT2_CLEAR);
260 	outl(inl(TX3912_INT2_ENABLE) | TX3912_INT2_UARTA_TX_BITS,
261 		TX3912_INT2_ENABLE);
262 	transmit_char_pio(rs_port);
263 
264 	restore_flags(flags);
265 }
266 
rs_disable_rx_interrupts(void * ptr)267 static void rs_disable_rx_interrupts (void * ptr)
268 {
269 	unsigned long flags;
270 
271 	save_and_cli(flags);
272 
273 	outl(inl(TX3912_INT2_ENABLE) & ~TX3912_INT2_UARTA_RX_BITS,
274 		TX3912_INT2_ENABLE);
275 	outl(TX3912_INT2_UARTA_RX_BITS, TX3912_INT2_CLEAR);
276 
277 	restore_flags(flags);
278 }
279 
rs_enable_rx_interrupts(void * ptr)280 static void rs_enable_rx_interrupts (void * ptr)
281 {
282 	unsigned long flags;
283 
284 	save_and_cli(flags);
285 
286 	outl(inl(TX3912_INT2_ENABLE) | TX3912_INT2_UARTA_RX_BITS,
287 		TX3912_INT2_ENABLE);
288 	while (inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_RXHOLDFULL)
289 		inb(TX3912_UARTA_DATA);
290 	outl(TX3912_INT2_UARTA_RX_BITS, TX3912_INT2_CLEAR);
291 
292 	restore_flags(flags);
293 }
294 
295 /*
296  * We have no CD
297  */
rs_get_CD(void * ptr)298 static int rs_get_CD (void * ptr)
299 {
300 	return 1;
301 }
302 
303 /*
304  * Shut down the port
305  */
rs_shutdown_port(void * ptr)306 static void rs_shutdown_port (void * ptr)
307 {
308 	func_enter();
309 	rs_port->gs.flags &= ~GS_ACTIVE;
310 	func_exit();
311 }
312 
rs_set_real_termios(void * ptr)313 static int rs_set_real_termios (void *ptr)
314 {
315 	unsigned int ctrl1 = 0;
316 	unsigned int ctrl2 = 0;
317 
318 	/* Set baud rate */
319 	switch (rs_port->gs.baud) {
320 		case 0:
321 			goto done;
322 		case 1200:
323 			ctrl2 = TX3912_UART_CTRL2_B1200;
324 			break;
325 		case 2400:
326 			ctrl2 = TX3912_UART_CTRL2_B2400;
327 			break;
328 		case 4800:
329 			ctrl2 = TX3912_UART_CTRL2_B4800;
330 			break;
331 		case 9600:
332 			ctrl2 = TX3912_UART_CTRL2_B9600;
333 			break;
334 		case 19200:
335 			ctrl2 = TX3912_UART_CTRL2_B19200;
336 			break;
337 		case 38400:
338 			ctrl2 = TX3912_UART_CTRL2_B38400;
339 			break;
340 		case 57600:
341 			ctrl2 = TX3912_UART_CTRL2_B57600;
342 			break;
343 		case 115200:
344 		default:
345 			ctrl2 = TX3912_UART_CTRL2_B115200;
346 			break;
347 	}
348 
349   	/* Clear current UARTA settings */
350 	ctrl1 = inl(TX3912_UARTA_CTRL1) & 0xf000000f;
351 
352 	/* Set parity */
353 	if(C_PARENB(rs_port->gs.tty)) {
354 		if (!C_PARODD(rs_port->gs.tty))
355 			ctrl1 |= (TX3912_UART_CTRL1_ENPARITY |
356 				 TX3912_UART_CTRL1_EVENPARITY);
357 		else
358 			ctrl1 |= TX3912_UART_CTRL1_ENPARITY;
359 	}
360 
361 	/* Set data size */
362 	switch(rs_port->gs.tty->termios->c_cflag & CSIZE) {
363 		case CS7:
364 			ctrl1 |= TX3912_UART_CTRL1_BIT_7;
365 			break;
366 		case CS5:
367 		case CS6:
368 			printk(KERN_ERR "Data byte size unsupported. Defaulting to CS8\n");
369 		case CS8:
370 		default:
371 			ctrl1 &= ~TX3912_UART_CTRL1_BIT_7;
372 	}
373 
374 	/* Set stop bits */
375 	if(C_CSTOPB(rs_port->gs.tty))
376 		ctrl1 |= TX3912_UART_CTRL1_TWOSTOP;
377 
378 	/* Write the control registers */
379 	outl(ctrl2, TX3912_UARTA_CTRL2);
380 	outl(0, TX3912_UARTA_DMA_CTRL1);
381 	outl(0, TX3912_UARTA_DMA_CTRL2);
382 	outl(ctrl1, TX3912_UARTA_CTRL1);
383 
384 	/* Loop until the UART is on */
385 	while(~inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_UARTON);
386 
387 done:
388 	func_exit();
389 	return 0;
390 }
391 
392 /*
393  * Anyone in the buffer?
394  */
rs_chars_in_buffer(void * ptr)395 static int rs_chars_in_buffer (void * ptr)
396 {
397 	return ((inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_EMPTY) ? 0 : 1);
398 }
399 
400 /*
401  * Open the serial port
402  */
rs_open(struct tty_struct * tty,struct file * filp)403 static int rs_open(struct tty_struct * tty, struct file * filp)
404 {
405 	int retval;
406 
407 	func_enter();
408 
409 	if(!rs_initialized) {
410 		return -EIO;
411 	}
412 
413 	if(MINOR(tty->device) - tty->driver.minor_start) {
414 		return -ENODEV;
415 	}
416 
417 	rs_dprintk(TX3912_UART_DEBUG_OPEN, "Serial opening...\n");
418 
419 	tty->driver_data = rs_port;
420 	rs_port->gs.tty = tty;
421 	rs_port->gs.count++;
422 
423 	/*
424 	 * Start up serial port
425 	 */
426 	retval = gs_init_port(&rs_port->gs);
427 	rs_dprintk(TX3912_UART_DEBUG_OPEN, "Finished gs_init...\n");
428 	if(retval) {
429 		rs_port->gs.count--;
430 		return retval;
431 	}
432 
433 	rs_port->gs.flags |= GS_ACTIVE;
434 	if(rs_port->gs.count == 1) {
435 		MOD_INC_USE_COUNT;
436 	}
437 
438 	rs_enable_rx_interrupts(rs_port);
439 	rs_enable_tx_interrupts(rs_port);
440 
441 	retval = gs_block_til_ready(&rs_port->gs, filp);
442 	if(retval) {
443 		MOD_DEC_USE_COUNT;
444 		rs_port->gs.count--;
445 		return retval;
446 	}
447 
448 	if((rs_port->gs.count == 1) &&
449 		(rs_port->gs.flags & ASYNC_SPLIT_TERMIOS)) {
450 		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
451 			*tty->termios = rs_port->gs.normal_termios;
452 		else
453 			*tty->termios = rs_port->gs.callout_termios;
454 		rs_set_real_termios(rs_port);
455 	}
456 
457 	rs_port->gs.session = current->session;
458 	rs_port->gs.pgrp = current->pgrp;
459 	func_exit();
460 
461 	return 0;
462 }
463 
464 /*
465  * Close the serial port
466  */
rs_close(void * ptr)467 static void rs_close(void *ptr)
468 {
469 	func_enter();
470 	MOD_DEC_USE_COUNT;
471 	func_exit();
472 }
473 
474 /*
475  * Hang up the serial port
476  */
rs_hungup(void * ptr)477 static void rs_hungup(void *ptr)
478 {
479 	func_enter();
480 	MOD_DEC_USE_COUNT;
481 	func_exit();
482 }
483 
484 /*
485  * Serial ioctl call
486  */
rs_ioctl(struct tty_struct * tty,struct file * filp,unsigned int cmd,unsigned long arg)487 static int rs_ioctl(struct tty_struct * tty, struct file * filp,
488                      unsigned int cmd, unsigned long arg)
489 {
490 	int ival, rc;
491 
492 	rc = 0;
493 	switch (cmd) {
494 		case TIOCGSOFTCAR:
495 			rc = put_user((tty->termios->c_cflag & CLOCAL) ? 1 : 0,
496 		              (unsigned int *) arg);
497 			break;
498 		case TIOCSSOFTCAR:
499 			if ((rc = verify_area(VERIFY_READ, (void *) arg,
500 				sizeof(int))) == 0) {
501 				get_user(ival, (unsigned int *) arg);
502 				tty->termios->c_cflag =
503 					(tty->termios->c_cflag & ~CLOCAL) |
504 					(ival ? CLOCAL : 0);
505 			}
506 			break;
507 		case TIOCGSERIAL:
508 			if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
509 				sizeof(struct serial_struct))) == 0)
510 				rc = gs_getserial(&rs_port->gs, (struct serial_struct *) arg);
511 			break;
512 		case TIOCSSERIAL:
513 			if ((rc = verify_area(VERIFY_READ, (void *) arg,
514 				sizeof(struct serial_struct))) == 0)
515 				rc = gs_setserial(&rs_port->gs, (struct serial_struct *) arg);
516 			break;
517 		default:
518 			rc = -ENOIOCTLCMD;
519 			break;
520 	}
521 
522 	return rc;
523 }
524 
525 /*
526  * Send xchar
527  */
rs_send_xchar(struct tty_struct * tty,char ch)528 static void rs_send_xchar(struct tty_struct * tty, char ch)
529 {
530 	func_enter();
531 
532 	rs_port->x_char = ch;
533 	if (ch) {
534 		rs_enable_tx_interrupts(tty);
535 	}
536 
537 	func_exit();
538 }
539 
540 /*
541  * Throttle characters as directed by upper tty layer
542  */
rs_throttle(struct tty_struct * tty)543 static void rs_throttle(struct tty_struct * tty)
544 {
545 #ifdef TX3912_UART_DEBUG_THROTTLE
546 	char	buf[64];
547 
548 	printk("throttle %s: %d....\n", tty_name(tty, buf),
549 	       tty->ldisc.chars_in_buffer(tty));
550 #endif
551 
552 	func_enter();
553 
554 	if (I_IXOFF(tty))
555 		rs_send_xchar(tty, STOP_CHAR(tty));
556 
557 	func_exit();
558 }
559 
560 /*
561  * Un-throttle characters as directed by upper tty layer
562  */
rs_unthrottle(struct tty_struct * tty)563 static void rs_unthrottle(struct tty_struct * tty)
564 {
565 #ifdef TX3912_UART_DEBUG_THROTTLE
566 	char	buf[64];
567 
568 	printk("unthrottle %s: %d....\n", tty_name(tty, buf),
569 	       tty->ldisc.chars_in_buffer(tty));
570 #endif
571 
572 	func_enter();
573 
574 	if (I_IXOFF(tty)) {
575 		if (rs_port->x_char)
576 			rs_port->x_char = 0;
577 		else
578 			rs_send_xchar(tty, START_CHAR(tty));
579 	}
580 
581 	func_exit();
582 }
583 
584 /*
585  * Initialize the serial port
586  */
tx3912_rs_init(void)587 void __init tx3912_rs_init(void)
588 {
589 	func_enter();
590 	rs_dprintk(TX3912_UART_DEBUG_INIT, "Initializing serial...\n");
591 
592 	/* Allocate critical structures */
593 	if(!(rs_tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL))) {
594 		return;
595 	}
596 	if(!(rs_port = kmalloc(sizeof(struct rs_port), GFP_KERNEL))) {
597 		kfree(rs_tty);
598 		return;
599 	}
600 	if(!(rs_termios = kmalloc(sizeof(struct termios), GFP_KERNEL))) {
601 		kfree(rs_port);
602 		kfree(rs_tty);
603 		return;
604 	}
605 	if(!(rs_termios_locked = kmalloc(sizeof(struct termios), GFP_KERNEL))) {
606 		kfree(rs_termios);
607 		kfree(rs_port);
608 		kfree(rs_tty);
609 		return;
610 	}
611 
612 	/* Zero out the structures */
613 	memset(rs_tty, 0, sizeof(struct tty_struct));
614 	memset(rs_port, 0, sizeof(struct rs_port));
615 	memset(rs_termios, 0, sizeof(struct termios));
616 	memset(rs_termios_locked, 0, sizeof(struct termios));
617 	memset(&rs_driver, 0, sizeof(rs_driver));
618 	memset(&rs_callout_driver, 0, sizeof(rs_callout_driver));
619 
620 	/* Fill in hardware specific port structure */
621 	rs_port->gs.callout_termios = tty_std_termios;
622 	rs_port->gs.normal_termios	= tty_std_termios;
623 	rs_port->gs.magic = SERIAL_MAGIC;
624 	rs_port->gs.close_delay = HZ/2;
625 	rs_port->gs.closing_wait = 30 * HZ;
626 	rs_port->gs.rd = &rs_real_driver;
627 #ifdef NEW_WRITE_LOCKING
628 	rs_port->gs.port_write_sem = MUTEX;
629 #endif
630 #ifdef DECLARE_WAITQUEUE
631 	init_waitqueue_head(&rs_port->gs.open_wait);
632 	init_waitqueue_head(&rs_port->gs.close_wait);
633 #endif
634 
635 	/* Fill in generic serial driver structures */
636 	rs_driver.magic = TTY_DRIVER_MAGIC;
637 	rs_driver.driver_name = "serial";
638 	rs_driver.name = "ttyS";
639 	rs_driver.major = TTY_MAJOR;
640 	rs_driver.minor_start = 64;
641 	rs_driver.num = 1;
642 	rs_driver.type = TTY_DRIVER_TYPE_SERIAL;
643 	rs_driver.subtype = SERIAL_TYPE_NORMAL;
644 	rs_driver.init_termios = tty_std_termios;
645 	rs_driver.init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
646 	rs_driver.refcount = &rs_refcount;
647 	rs_driver.table = rs_tty;
648 	rs_driver.termios = rs_termios;
649 	rs_driver.termios_locked = rs_termios_locked;
650 	rs_driver.open	= rs_open;
651 	rs_driver.close = gs_close;
652 	rs_driver.write = gs_write;
653 	rs_driver.put_char = gs_put_char;
654 	rs_driver.flush_chars = gs_flush_chars;
655 	rs_driver.write_room = gs_write_room;
656 	rs_driver.chars_in_buffer = gs_chars_in_buffer;
657 	rs_driver.flush_buffer = gs_flush_buffer;
658 	rs_driver.ioctl = rs_ioctl;
659 	rs_driver.throttle = rs_throttle;
660 	rs_driver.unthrottle = rs_unthrottle;
661 	rs_driver.set_termios = gs_set_termios;
662 	rs_driver.stop = gs_stop;
663 	rs_driver.start = gs_start;
664 	rs_driver.hangup = gs_hangup;
665 	rs_callout_driver = rs_driver;
666 	rs_callout_driver.name = "cua";
667 	rs_callout_driver.major = TTYAUX_MAJOR;
668 	rs_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
669 
670 	/* Register serial and callout drivers */
671 	if(tty_register_driver(&rs_driver)) {
672 		printk(KERN_ERR "Unable to register serial driver\n");
673 		goto error;
674 	}
675 	if(tty_register_driver(&rs_callout_driver)) {
676 		tty_unregister_driver(&rs_driver);
677 		printk(KERN_ERR "Unable to register callout driver\n");
678 		goto error;
679 	}
680 
681 	/* Assign IRQs */
682 	if(request_irq(2, rs_tx_interrupt, SA_SHIRQ,
683 		"uarta_tx", rs_port)) {
684 		printk(KERN_ERR "Cannot allocate IRQ for UARTA_TX.\n");
685 		goto error;
686 	}
687 
688 	if(request_irq(3, rs_rx_interrupt, SA_SHIRQ,
689 		"uarta_rx", rs_port)) {
690 		printk(KERN_ERR "Cannot allocate IRQ for UARTA_RX.\n");
691 		goto error;
692 	}
693 
694 	/* Enable the serial receive interrupt */
695 	rs_enable_rx_interrupts(rs_port);
696 
697 #ifndef CONFIG_SERIAL_TX3912_CONSOLE
698 	/* Write the control registers */
699 	outl(TX3912_UART_CTRL2_B115200, TX3912_UARTA_CTRL2);
700 	outl(0x00000000, TX3912_UARTA_DMA_CTRL1);
701 	outl(0x00000000, TX3912_UARTA_DMA_CTRL2);
702 	outl(inl(TX3912_UARTA_CTRL1) | TX3912_UART_CTRL1_ENUART,
703 		TX3912_UARTA_CTRL1);
704 
705 	/* Loop until the UART is on */
706 	while(~inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_UARTON);
707 #endif
708 
709 	rs_initialized = 1;
710 	func_exit();
711 	return;
712 
713 error:
714 	kfree(rs_termios_locked);
715 	kfree(rs_termios);
716 	kfree(rs_port);
717 	kfree(rs_tty);
718 	func_exit();
719 }
720 
721 /*
722  * Begin serial console routines
723  */
724 #ifdef CONFIG_SERIAL_TX3912_CONSOLE
serial_outc(unsigned char c)725 void serial_outc(unsigned char c)
726 {
727 	int i;
728 	unsigned long int2;
729 
730 	/* Disable UARTA_TX interrupts */
731 	int2 = inl(TX3912_INT2_ENABLE);
732 	outl(inl(TX3912_INT2_ENABLE) & ~TX3912_INT2_UARTA_TX_BITS,
733 		 TX3912_INT2_ENABLE);
734 
735 	/* Wait for UARTA_TX register to empty */
736 	i = 10000;
737 	while(!(inl(TX3912_INT2_STATUS) & TX3912_INT2_UARTATXINT) && i--);
738 	outl(TX3912_INT2_UARTA_TX_BITS, TX3912_INT2_CLEAR);
739 
740 	/* Send the character */
741 	outl(c, TX3912_UARTA_DATA);
742 
743 	/* Wait for UARTA_TX register to empty */
744 	i = 10000;
745 	while(!(inl(TX3912_INT2_STATUS) & TX3912_INT2_UARTATXINT) && i--);
746 	outl(TX3912_INT2_UARTA_TX_BITS, TX3912_INT2_CLEAR);
747 
748 	/* Enable UARTA_TX interrupts */
749 	outl(int2, TX3912_INT2_ENABLE);
750 }
751 
serial_console_wait_key(struct console * co)752 static int serial_console_wait_key(struct console *co)
753 {
754 	unsigned int int2, res;
755 
756 	int2 = inl(TX3912_INT2_ENABLE);
757 	outl(0, TX3912_INT2_ENABLE);
758 
759 	while (!(inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_RXHOLDFULL));
760 	res = inl(TX3912_UARTA_DATA);
761 	udelay(10);
762 
763 	outl(int2, TX3912_INT2_ENABLE);
764 	return res;
765 }
766 
serial_console_write(struct console * co,const char * s,unsigned count)767 static void serial_console_write(struct console *co, const char *s,
768 	unsigned count)
769 {
770 	unsigned int i;
771 
772 	for (i = 0; i < count; i++) {
773 		if (*s == '\n')
774 			serial_outc('\r');
775 		serial_outc(*s++);
776     	}
777 }
778 
serial_console_device(struct console * c)779 static kdev_t serial_console_device(struct console *c)
780 {
781 	return MKDEV(TTY_MAJOR, 64 + c->index);
782 }
783 
serial_console_setup(struct console * co,char * options)784 static __init int serial_console_setup(struct console *co, char *options)
785 {
786 	int baud = 115200;
787 	int bits = 8;
788 	int parity = 'n';
789 	char *s;
790 	unsigned int ctrl1 = 0;
791 	unsigned int ctrl2 = 0;
792 
793 	if (options) {
794 		baud = simple_strtoul(options, NULL, 10);
795 		s = options;
796 		while(*s >= '0' && *s <= '9')
797 			s++;
798 		if (*s) parity = *s++;
799 		if (*s) bits   = *s++ - '0';
800 	}
801 
802 	switch(baud) {
803 		case 1200:
804 			ctrl2 = TX3912_UART_CTRL2_B1200;
805 			break;
806 		case 2400:
807 			ctrl2 = TX3912_UART_CTRL2_B2400;
808 			break;
809 		case 4800:
810 			ctrl2 = TX3912_UART_CTRL2_B4800;
811 			break;
812 		case 9600:
813 			ctrl2 = TX3912_UART_CTRL2_B9600;
814 			break;
815 		case 19200:
816 			ctrl2 = TX3912_UART_CTRL2_B19200;
817 			break;
818 		case 38400:
819 			ctrl2 = TX3912_UART_CTRL2_B38400;
820 			break;
821 		case 57600:
822 			ctrl2 = TX3912_UART_CTRL2_B57600;
823 			break;
824 		case 115200:
825 		default:
826 			ctrl2 = TX3912_UART_CTRL2_B115200;
827 			break;
828 	}
829 
830 	switch(bits) {
831 		case 7:
832 			ctrl1 = TX3912_UART_CTRL1_BIT_7;
833 			break;
834 		default:
835 			break;
836 	}
837 
838 	switch(parity) {
839 		case 'o': case 'O':
840 			ctrl1 |= TX3912_UART_CTRL1_ENPARITY;
841 			break;
842 		case 'e': case 'E':
843 			ctrl1 |= (TX3912_UART_CTRL1_ENPARITY |
844 				 TX3912_UART_CTRL1_EVENPARITY);
845 			break;
846 		default:
847 			break;
848 	}
849 
850 	/* Write the control registers */
851 	outl(ctrl2, TX3912_UARTA_CTRL2);
852 	outl(0x00000000, TX3912_UARTA_DMA_CTRL1);
853 	outl(0x00000000, TX3912_UARTA_DMA_CTRL2);
854 	outl((ctrl1 | TX3912_UART_CTRL1_ENUART), TX3912_UARTA_CTRL1);
855 
856 	/* Loop until the UART is on */
857 	while(~inl(TX3912_UARTA_CTRL1) & TX3912_UART_CTRL1_UARTON);
858 
859 	return 0;
860 }
861 
862 static struct console sercons = {
863 	name:     "ttyS",
864 	write:    serial_console_write,
865 	device:   serial_console_device,
866 	setup:    serial_console_setup,
867 	flags:    CON_PRINTBUFFER,
868 	index:    -1
869 };
870 
tx3912_console_init(void)871 void __init tx3912_console_init(void)
872 {
873 	register_console(&sercons);
874 }
875 #endif
876