1/*
2 * Copyright (C) 2000,2001,2002,2003,2004 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18
19/*
20 * Driver support for the on-chip sb1250 dual-channel serial port,
21 * running in asynchronous mode.  Also, support for doing a serial console
22 * on one of those ports
23 */
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/serial.h>
27#include <linux/interrupt.h>
28#include <linux/module.h>
29#include <linux/console.h>
30#include <linux/kdev_t.h>
31#include <linux/major.h>
32#include <linux/termios.h>
33#include <linux/spinlock.h>
34#include <linux/irq.h>
35#include <linux/errno.h>
36#include <linux/tty.h>
37#include <linux/sched.h>
38#include <linux/tty_flip.h>
39#include <linux/timer.h>
40#include <linux/init.h>
41#include <linux/mm.h>
42#include <asm/delay.h>
43#include <asm/io.h>
44#include <asm/uaccess.h>
45#include <asm/sibyte/swarm.h>
46#include <asm/sibyte/sb1250.h>
47#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
48#include <asm/sibyte/bcm1480_regs.h>
49#include <asm/sibyte/bcm1480_int.h>
50#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
51#include <asm/sibyte/sb1250_regs.h>
52#include <asm/sibyte/sb1250_int.h>
53#else
54#error invalid SiByte UART configuation
55#endif
56#include <asm/sibyte/sb1250_uart.h>
57#include <asm/war.h>
58
59#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
60#define UNIT_CHANREG(n,reg)	A_BCM1480_DUART_CHANREG((n),(reg))
61#define UNIT_IMRREG(n)		A_BCM1480_DUART_IMRREG(n)
62#define UNIT_INT(n)		(K_BCM1480_INT_UART_0 + (n))
63#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
64#define UNIT_CHANREG(n,reg)	A_DUART_CHANREG((n),(reg))
65#define UNIT_IMRREG(n)		A_DUART_IMRREG(n)
66#define UNIT_INT(n)		(K_INT_UART_0 + (n))
67#else
68#error invalid SiByte UART configuation
69#endif
70
71/* Toggle spewing of debugging output */
72#undef DEBUG
73
74#define DEFAULT_CFLAGS          (CS8 | B115200)
75
76#define TX_INTEN          1
77#define DUART_INITIALIZED 2
78
79#define DUART_MAX_LINE 4
80char sb1250_duart_present[DUART_MAX_LINE];
81EXPORT_SYMBOL(sb1250_duart_present);
82
83#if SIBYTE_1956_WAR
84# define SB1_SER1956_WAR {							\
85	u32 ignore;										\
86	ignore = csr_in32(uart_states[line].mode_1);	\
87	ignore = csr_in32(uart_states[line].mode_2);	\
88	}
89#else
90# define SB1_SER1956_WAR
91#endif
92
93/*
94 * Still not sure what the termios structures set up here are for,
95 *  but we have to supply pointers to them to register the tty driver
96 */
97static struct tty_driver *sb1250_duart_driver; //, sb1250_duart_callout_driver;
98
99/*
100 * This lock protects both the open flags for all the uart states as
101 * well as the reference count for the module
102 */
103static DEFINE_SPINLOCK(open_lock);
104
105typedef struct {
106	unsigned char		outp_buf[SERIAL_XMIT_SIZE];
107	unsigned int		outp_head;
108	unsigned int		outp_tail;
109	unsigned int		outp_count;
110	spinlock_t		outp_lock;
111	unsigned int		open;
112	unsigned int		line;
113	unsigned int		last_cflags;
114	unsigned long		flags;
115	struct tty_struct	*tty;
116
117	/* CSR addresses */
118	unsigned int		*status;
119	unsigned int		*imr;
120	unsigned int		*tx_hold;
121	unsigned int		*rx_hold;
122	unsigned int		*mode_1;
123	unsigned int		*mode_2;
124	unsigned int		*clk_sel;
125	unsigned int		*cmd;
126} uart_state_t;
127
128static uart_state_t uart_states[DUART_MAX_LINE];
129
130/*
131 * Inline functions local to this module
132 */
133
134static inline u32 READ_SERCSR(volatile u32 *addr, int line)
135{
136	u32 val = csr_in32(addr);
137	SB1_SER1956_WAR;
138	return val;
139}
140
141static inline void WRITE_SERCSR(u32 val, volatile u32 *addr, int line)
142{
143	csr_out32(val, addr);
144	SB1_SER1956_WAR;
145}
146
147static void init_duart_port(uart_state_t *port, int line)
148{
149	if (!(port->flags & DUART_INITIALIZED)) {
150		port->line = line;
151		port->status = IOADDR(UNIT_CHANREG(line, R_DUART_STATUS));
152		port->imr = IOADDR(UNIT_IMRREG(line));
153		port->tx_hold = IOADDR(UNIT_CHANREG(line, R_DUART_TX_HOLD));
154		port->rx_hold = IOADDR(UNIT_CHANREG(line, R_DUART_RX_HOLD));
155		port->mode_1 = IOADDR(UNIT_CHANREG(line, R_DUART_MODE_REG_1));
156		port->mode_2 = IOADDR(UNIT_CHANREG(line, R_DUART_MODE_REG_2));
157		port->clk_sel = IOADDR(UNIT_CHANREG(line, R_DUART_CLK_SEL));
158		port->cmd = IOADDR(UNIT_CHANREG(line, R_DUART_CMD));
159		port->last_cflags = DEFAULT_CFLAGS;
160		spin_lock_init(&port->outp_lock);
161		port->flags |= DUART_INITIALIZED;
162	}
163}
164
165/*
166 * Mask out the passed interrupt lines at the duart level.  This should be
167 * called while holding the associated outp_lock.
168 */
169static inline void duart_mask_ints(unsigned int line, unsigned int mask)
170{
171	uart_state_t *port = uart_states + line;
172	u64 tmp = READ_SERCSR(port->imr, line);
173	WRITE_SERCSR(tmp & ~mask, port->imr, line);
174}
175
176
177/* Unmask the passed interrupt lines at the duart level */
178static inline void duart_unmask_ints(unsigned int line, unsigned int mask)
179{
180	uart_state_t *port = uart_states + line;
181	u64 tmp = READ_SERCSR(port->imr, line);
182	WRITE_SERCSR(tmp | mask, port->imr, line);
183}
184
185static inline void transmit_char_pio(uart_state_t *us)
186{
187	struct tty_struct *tty = us->tty;
188	int blocked = 0;
189
190	if (spin_trylock(&us->outp_lock)) {
191		for (;;) {
192			if (!(READ_SERCSR(us->status, us->line) & M_DUART_TX_RDY))
193				break;
194			if (us->outp_count <= 0 || tty->stopped || tty->hw_stopped) {
195				break;
196			} else {
197				WRITE_SERCSR(us->outp_buf[us->outp_head],
198					     us->tx_hold, us->line);
199				us->outp_head = (us->outp_head + 1) & (SERIAL_XMIT_SIZE-1);
200				if (--us->outp_count <= 0)
201					break;
202			}
203			udelay(10);
204		}
205		spin_unlock(&us->outp_lock);
206	} else {
207		blocked = 1;
208	}
209
210	if (!us->outp_count || tty->stopped ||
211	    tty->hw_stopped || blocked) {
212		us->flags &= ~TX_INTEN;
213		duart_mask_ints(us->line, M_DUART_IMR_TX);
214	}
215
216	if (us->open &&
217	    (us->outp_count < (SERIAL_XMIT_SIZE/2))) {
218		/*
219		 * We told the discipline at one point that we had no
220		 * space, so it went to sleep.  Wake it up when we hit
221		 * half empty
222		 */
223		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
224		    tty->ldisc.write_wakeup)
225			tty->ldisc.write_wakeup(tty);
226		wake_up_interruptible(&tty->write_wait);
227	}
228}
229
230/*
231 * Generic interrupt handler for both channels.  dev_id is a pointer
232 * to the proper uart_states structure, so from that we can derive
233 * which port interrupted
234 */
235
236static irqreturn_t duart_int(int irq, void *dev_id)
237{
238	uart_state_t *us = (uart_state_t *)dev_id;
239	struct tty_struct *tty = us->tty;
240	unsigned int status = READ_SERCSR(us->status, us->line);
241
242	pr_debug("DUART INT\n");
243
244	if (status & M_DUART_RX_RDY) {
245		int counter = 2048;
246		unsigned int ch;
247
248		if (status & M_DUART_OVRUN_ERR)
249			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
250		if (status & M_DUART_PARITY_ERR) {
251			printk("Parity error!\n");
252		} else if (status & M_DUART_FRM_ERR) {
253			printk("Frame error!\n");
254		}
255
256		while (counter > 0) {
257			if (!(READ_SERCSR(us->status, us->line) & M_DUART_RX_RDY))
258				break;
259			ch = READ_SERCSR(us->rx_hold, us->line);
260			tty_insert_flip_char(tty, ch, 0);
261			udelay(1);
262			counter--;
263		}
264		tty_flip_buffer_push(tty);
265	}
266
267	if (status & M_DUART_TX_RDY) {
268		transmit_char_pio(us);
269	}
270
271	return IRQ_HANDLED;
272}
273
274/*
275 *  Actual driver functions
276 */
277
278/* Return the number of characters we can accomodate in a write at this instant */
279static int duart_write_room(struct tty_struct *tty)
280{
281	uart_state_t *us = (uart_state_t *) tty->driver_data;
282	int retval;
283
284	retval = SERIAL_XMIT_SIZE - us->outp_count;
285
286	pr_debug("duart_write_room called, returning %i\n", retval);
287
288	return retval;
289}
290
291/* memcpy the data from src to destination, but take extra care if the
292   data is coming from user space */
293static inline int copy_buf(char *dest, const char *src, int size, int from_user)
294{
295	if (from_user) {
296		(void) copy_from_user(dest, src, size);
297	} else {
298		memcpy(dest, src, size);
299	}
300	return size;
301}
302
303/*
304 * Buffer up to count characters from buf to be written.  If we don't have
305 * other characters buffered, enable the tx interrupt to start sending
306 */
307static int duart_write(struct tty_struct *tty, const unsigned char *buf,
308		       int count)
309{
310	uart_state_t *us;
311	int c, t, total = 0;
312	unsigned long flags;
313
314	if (!tty) return 0;
315
316	us = tty->driver_data;
317	if (!us) return 0;
318
319	pr_debug("duart_write called for %i chars by %i (%s)\n", count,
320			current->pid, current->comm);
321
322	spin_lock_irqsave(&us->outp_lock, flags);
323
324	for (;;) {
325		c = count;
326
327		t = SERIAL_XMIT_SIZE - us->outp_tail;
328		if (t < c) c = t;
329
330		t = SERIAL_XMIT_SIZE - 1 - us->outp_count;
331		if (t < c) c = t;
332
333		if (c <= 0) break;
334
335		memcpy(us->outp_buf + us->outp_tail, buf, c);
336
337		us->outp_count += c;
338		us->outp_tail = (us->outp_tail + c) & (SERIAL_XMIT_SIZE - 1);
339		buf += c;
340		count -= c;
341		total += c;
342	}
343
344	spin_unlock_irqrestore(&us->outp_lock, flags);
345
346	if (us->outp_count && !tty->stopped &&
347	    !tty->hw_stopped && !(us->flags & TX_INTEN)) {
348		us->flags |= TX_INTEN;
349		duart_unmask_ints(us->line, M_DUART_IMR_TX);
350	}
351
352	return total;
353}
354
355
356/* Buffer one character to be written.  If there's not room for it, just drop
357   it on the floor.  This is used for echo, among other things */
358static void duart_put_char(struct tty_struct *tty, u_char ch)
359{
360	uart_state_t *us = (uart_state_t *) tty->driver_data;
361	unsigned long flags;
362
363	pr_debug("duart_put_char called.  Char is %x (%c)\n", (int)ch, ch);
364
365	spin_lock_irqsave(&us->outp_lock, flags);
366
367	if (us->outp_count == SERIAL_XMIT_SIZE) {
368		spin_unlock_irqrestore(&us->outp_lock, flags);
369		return;
370	}
371
372	us->outp_buf[us->outp_tail] = ch;
373	us->outp_tail = (us->outp_tail + 1) &(SERIAL_XMIT_SIZE-1);
374	us->outp_count++;
375
376	spin_unlock_irqrestore(&us->outp_lock, flags);
377}
378
379static void duart_flush_chars(struct tty_struct * tty)
380{
381	uart_state_t *port;
382
383	if (!tty)
384		return;
385
386	port = tty->driver_data;
387
388	if (!port)
389		return;
390
391	if (port->outp_count <= 0 || tty->stopped || tty->hw_stopped) {
392		return;
393	}
394
395	port->flags |= TX_INTEN;
396	duart_unmask_ints(port->line, M_DUART_IMR_TX);
397}
398
399/* Return the number of characters in the output buffer that have yet to be
400   written */
401static int duart_chars_in_buffer(struct tty_struct *tty)
402{
403	uart_state_t *us = (uart_state_t *) tty->driver_data;
404	int retval;
405
406	retval = us->outp_count;
407
408	pr_debug("duart_chars_in_buffer returning %i\n", retval);
409
410	return retval;
411}
412
413/* Kill everything we haven't yet shoved into the FIFO.  Turn off the
414   transmit interrupt since we've nothing more to transmit */
415static void duart_flush_buffer(struct tty_struct *tty)
416{
417	uart_state_t *us = (uart_state_t *) tty->driver_data;
418	unsigned long flags;
419
420	pr_debug("duart_flush_buffer called\n");
421	spin_lock_irqsave(&us->outp_lock, flags);
422	us->outp_head = us->outp_tail = us->outp_count = 0;
423	spin_unlock_irqrestore(&us->outp_lock, flags);
424
425	wake_up_interruptible(&us->tty->write_wait);
426	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
427	    tty->ldisc.write_wakeup)
428		tty->ldisc.write_wakeup(tty);
429}
430
431
432/* See sb1250 user manual for details on these registers */
433static inline void duart_set_cflag(unsigned int line, unsigned int cflag)
434{
435	unsigned int mode_reg1 = 0, mode_reg2 = 0;
436	unsigned int clk_divisor;
437	uart_state_t *port = uart_states + line;
438
439	switch (cflag & CSIZE) {
440	case CS7:
441		mode_reg1 |= V_DUART_BITS_PER_CHAR_7;
442
443	default:
444		/* We don't handle CS5 or CS6...is there a way we're supposed to
445		 * flag this?  right now we just force them to CS8 */
446		mode_reg1 |= 0x0;
447		break;
448	}
449	if (cflag & CSTOPB) {
450	        mode_reg2 |= M_DUART_STOP_BIT_LEN_2;
451	}
452	if (!(cflag & PARENB)) {
453	        mode_reg1 |= V_DUART_PARITY_MODE_NONE;
454	}
455	if (cflag & PARODD) {
456		mode_reg1 |= M_DUART_PARITY_TYPE_ODD;
457	}
458
459	/* Formula for this is (5000000/baud)-1, but we saturate
460	   at 12 bits, which means we can't actually do anything less
461	   that 1200 baud */
462	switch (cflag & CBAUD) {
463	case B200:
464	case B300:
465	case B1200:	clk_divisor = 4095;		break;
466	case B1800:	clk_divisor = 2776;		break;
467	case B2400:	clk_divisor = 2082;		break;
468	case B4800:	clk_divisor = 1040;		break;
469	case B9600:	clk_divisor = 519;		break;
470	case B19200:	clk_divisor = 259;		break;
471	case B38400:	clk_divisor = 129;		break;
472	default:
473	case B57600:	clk_divisor = 85;		break;
474	case B115200:	clk_divisor = 42;		break;
475	}
476	WRITE_SERCSR(mode_reg1, port->mode_1, port->line);
477	WRITE_SERCSR(mode_reg2, port->mode_2, port->line);
478	WRITE_SERCSR(clk_divisor, port->clk_sel, port->line);
479	port->last_cflags = cflag;
480}
481
482
483/* Handle notification of a termios change.  */
484static void duart_set_termios(struct tty_struct *tty, struct termios *old)
485{
486	uart_state_t *us = (uart_state_t *) tty->driver_data;
487
488	pr_debug("duart_set_termios called by %i (%s)\n", current->pid,
489		current->comm);
490	if (old && tty->termios->c_cflag == old->c_cflag)
491		return;
492	duart_set_cflag(us->line, tty->termios->c_cflag);
493}
494
495static int get_serial_info(uart_state_t *us, struct serial_struct * retinfo)
496{
497	struct serial_struct tmp;
498
499	memset(&tmp, 0, sizeof(tmp));
500
501	tmp.type = PORT_SB1250;
502	tmp.line = us->line;
503	tmp.port = UNIT_CHANREG(tmp.line,0);
504	tmp.irq = UNIT_INT(tmp.line);
505	tmp.xmit_fifo_size = 16; /* fixed by hw */
506	tmp.baud_base = 5000000;
507	tmp.io_type = SERIAL_IO_MEM;
508
509	if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
510		return -EFAULT;
511
512	return 0;
513}
514
515static int duart_ioctl(struct tty_struct *tty, struct file * file,
516		       unsigned int cmd, unsigned long arg)
517{
518	uart_state_t *us = (uart_state_t *) tty->driver_data;
519
520/*	if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
521	return -ENODEV;*/
522	switch (cmd) {
523	case TIOCMGET:
524		printk("Ignoring TIOCMGET\n");
525		break;
526	case TIOCMBIS:
527		printk("Ignoring TIOCMBIS\n");
528		break;
529	case TIOCMBIC:
530		printk("Ignoring TIOCMBIC\n");
531		break;
532	case TIOCMSET:
533		printk("Ignoring TIOCMSET\n");
534		break;
535	case TIOCGSERIAL:
536		return get_serial_info(us,(struct serial_struct *) arg);
537	case TIOCSSERIAL:
538		printk("Ignoring TIOCSSERIAL\n");
539		break;
540	case TIOCSERCONFIG:
541		printk("Ignoring TIOCSERCONFIG\n");
542		break;
543	case TIOCSERGETLSR: /* Get line status register */
544		printk("Ignoring TIOCSERGETLSR\n");
545		break;
546	case TIOCSERGSTRUCT:
547		printk("Ignoring TIOCSERGSTRUCT\n");
548		break;
549	case TIOCMIWAIT:
550		printk("Ignoring TIOCMIWAIT\n");
551		break;
552	case TIOCGICOUNT:
553		printk("Ignoring TIOCGICOUNT\n");
554		break;
555	case TIOCSERGWILD:
556		printk("Ignoring TIOCSERGWILD\n");
557		break;
558	case TIOCSERSWILD:
559		printk("Ignoring TIOCSERSWILD\n");
560		break;
561	default:
562		break;
563	}
564//	printk("Ignoring IOCTL %x from pid %i (%s)\n", cmd, current->pid, current->comm);
565	return -ENOIOCTLCMD;
566}
567
568/* XXXKW locking? */
569static void duart_start(struct tty_struct *tty)
570{
571	uart_state_t *us = (uart_state_t *) tty->driver_data;
572
573	pr_debug("duart_start called\n");
574
575	if (us->outp_count && !(us->flags & TX_INTEN)) {
576		us->flags |= TX_INTEN;
577		duart_unmask_ints(us->line, M_DUART_IMR_TX);
578	}
579}
580
581/* XXXKW locking? */
582static void duart_stop(struct tty_struct *tty)
583{
584	uart_state_t *us = (uart_state_t *) tty->driver_data;
585
586	pr_debug("duart_stop called\n");
587
588	if (us->outp_count && (us->flags & TX_INTEN)) {
589		us->flags &= ~TX_INTEN;
590		duart_mask_ints(us->line, M_DUART_IMR_TX);
591	}
592}
593
594/* Not sure on the semantics of this; are we supposed to wait until the stuff
595 * already in the hardware FIFO drains, or are we supposed to wait until
596 * we've drained the output buffer, too?  I'm assuming the former, 'cause thats
597 * what the other drivers seem to assume
598 */
599
600static void duart_wait_until_sent(struct tty_struct *tty, int timeout)
601{
602	uart_state_t *us = (uart_state_t *) tty->driver_data;
603	unsigned long orig_jiffies;
604
605	orig_jiffies = jiffies;
606	pr_debug("duart_wait_until_sent(%d)+\n", timeout);
607	while (!(READ_SERCSR(us->status, us->line) & M_DUART_TX_EMT)) {
608		set_current_state(TASK_INTERRUPTIBLE);
609		schedule_timeout(1);
610		if (signal_pending(current))
611			break;
612		if (timeout && time_after(jiffies, orig_jiffies + timeout))
613			break;
614	}
615	pr_debug("duart_wait_until_sent()-\n");
616}
617
618/*
619 * duart_hangup() --- called by tty_hangup() when a hangup is signaled.
620 */
621static void duart_hangup(struct tty_struct *tty)
622{
623	uart_state_t *us = (uart_state_t *) tty->driver_data;
624
625	duart_flush_buffer(tty);
626	us->open = 0;
627	us->tty = 0;
628}
629
630/*
631 * Open a tty line.  Note that this can be called multiple times, so ->open can
632 * be >1.  Only set up the tty struct if this is a "new" open, e.g. ->open was
633 * zero
634 */
635static int duart_open(struct tty_struct *tty, struct file *filp)
636{
637	uart_state_t *us;
638	unsigned int line = tty->index;
639	unsigned long flags;
640
641	if ((line >= tty->driver->num) || !sb1250_duart_present[line])
642		return -ENODEV;
643
644	pr_debug("duart_open called by %i (%s), tty is %p, rw is %p, ww is %p\n",
645	       current->pid, current->comm, tty, (void *)&tty->read_wait,
646	       (void *)&tty->write_wait);
647
648	us = uart_states + line;
649	tty->driver_data = us;
650
651	spin_lock_irqsave(&open_lock, flags);
652	if (!us->open) {
653		us->tty = tty;
654		us->tty->termios->c_cflag = us->last_cflags;
655	}
656	us->open++;
657	us->flags &= ~TX_INTEN;
658	duart_unmask_ints(line, M_DUART_IMR_RX);
659	spin_unlock_irqrestore(&open_lock, flags);
660
661	return 0;
662}
663
664
665/*
666 * Close a reference count out.  If reference count hits zero, null the
667 * tty, kill the interrupts.  The tty_io driver is responsible for making
668 * sure we've cleared out our internal buffers before calling close()
669 */
670static void duart_close(struct tty_struct *tty, struct file *filp)
671{
672	uart_state_t *us = (uart_state_t *) tty->driver_data;
673	unsigned long flags;
674
675	pr_debug("duart_close called by %i (%s)\n", current->pid, current->comm);
676
677	if (!us || !us->open)
678		return;
679
680	spin_lock_irqsave(&open_lock, flags);
681	if (tty_hung_up_p(filp)) {
682		spin_unlock_irqrestore(&open_lock, flags);
683		return;
684	}
685
686	if (--us->open < 0) {
687		us->open = 0;
688		printk(KERN_ERR "duart: bad open count: %d\n", us->open);
689	}
690	if (us->open) {
691		spin_unlock_irqrestore(&open_lock, flags);
692		return;
693	}
694
695	spin_unlock_irqrestore(&open_lock, flags);
696
697	tty->closing = 1;
698
699	/* Stop accepting input */
700	duart_mask_ints(us->line, M_DUART_IMR_RX);
701	/* Wait for FIFO to drain */
702	while (!(READ_SERCSR(us->status, us->line) & M_DUART_TX_EMT))
703		;
704
705	if (tty->driver->flush_buffer)
706		tty->driver->flush_buffer(tty);
707	if (tty->ldisc.flush_buffer)
708		tty->ldisc.flush_buffer(tty);
709	tty->closing = 0;
710}
711
712
713static struct tty_operations duart_ops = {
714        .open   = duart_open,
715        .close = duart_close,
716        .write = duart_write,
717        .put_char = duart_put_char,
718        .flush_chars = duart_flush_chars,
719        .write_room = duart_write_room,
720        .chars_in_buffer = duart_chars_in_buffer,
721        .flush_buffer = duart_flush_buffer,
722        .ioctl = duart_ioctl,
723//        .throttle = duart_throttle,
724//        .unthrottle = duart_unthrottle,
725        .set_termios = duart_set_termios,
726        .stop = duart_stop,
727        .start = duart_start,
728        .hangup = duart_hangup,
729	.wait_until_sent = duart_wait_until_sent,
730};
731
732/* Initialize the sb1250_duart_present array based on SOC type.  */
733static void __init sb1250_duart_init_present_lines(void)
734{
735	int i, max_lines;
736
737	/* Set the number of available units based on the SOC type.  */
738	switch (soc_type) {
739	case K_SYS_SOC_TYPE_BCM1x55:
740	case K_SYS_SOC_TYPE_BCM1x80:
741		max_lines = 4;
742		break;
743	default:
744		/* Assume at least two serial ports at the normal address.  */
745		max_lines = 2;
746		break;
747	}
748	if (max_lines > DUART_MAX_LINE)
749		max_lines = DUART_MAX_LINE;
750
751	for (i = 0; i < max_lines; i++)
752		sb1250_duart_present[i] = 1;
753}
754
755/* Set up the driver and register it, register the UART interrupts.  This
756   is called from tty_init, or as a part of the module init */
757static int __init sb1250_duart_init(void)
758{
759	int i;
760
761	sb1250_duart_init_present_lines();
762
763	sb1250_duart_driver = alloc_tty_driver(DUART_MAX_LINE);
764	if (!sb1250_duart_driver)
765		return -ENOMEM;
766
767	sb1250_duart_driver->owner = THIS_MODULE;
768	sb1250_duart_driver->name = "duart";
769	sb1250_duart_driver->major = TTY_MAJOR;
770	sb1250_duart_driver->minor_start = SB1250_DUART_MINOR_BASE;
771	sb1250_duart_driver->type            = TTY_DRIVER_TYPE_SERIAL;
772	sb1250_duart_driver->subtype         = SERIAL_TYPE_NORMAL;
773	sb1250_duart_driver->init_termios    = tty_std_termios;
774	sb1250_duart_driver->flags           = TTY_DRIVER_REAL_RAW;
775	tty_set_operations(sb1250_duart_driver, &duart_ops);
776
777	for (i = 0; i < DUART_MAX_LINE; i++) {
778		uart_state_t *port = uart_states + i;
779
780		if (!sb1250_duart_present[i])
781			continue;
782
783		init_duart_port(port, i);
784		duart_mask_ints(i, M_DUART_IMR_ALL);
785		if (request_irq(UNIT_INT(i), duart_int, 0, "uart", port)) {
786			panic("Couldn't get uart0 interrupt line");
787		}
788		/*
789		 * this generic write to a register does not implement the 1956
790		 * WAR and sometimes output gets corrupted afterwards,
791		 * especially if the port was in use as a console.
792		 */
793		__raw_writel(M_DUART_RX_EN|M_DUART_TX_EN, port->cmd);
794
795		/*
796		 * we should really check to see if it's registered as a console
797		 * before trashing those settings
798		 */
799		duart_set_cflag(i, port->last_cflags);
800	}
801
802	/* Interrupts are now active, our ISR can be called. */
803
804	if (tty_register_driver(sb1250_duart_driver)) {
805		printk(KERN_ERR "Couldn't register sb1250 duart serial driver\n");
806		put_tty_driver(sb1250_duart_driver);
807		return 1;
808	}
809	return 0;
810}
811
812/* Unload the driver.  Unregister stuff, get ready to go away */
813static void __exit sb1250_duart_fini(void)
814{
815	unsigned long flags;
816	int i;
817
818	local_irq_save(flags);
819	tty_unregister_driver(sb1250_duart_driver);
820	put_tty_driver(sb1250_duart_driver);
821
822	for (i = 0; i < DUART_MAX_LINE; i++) {
823		if (!sb1250_duart_present[i])
824			continue;
825		free_irq(UNIT_INT(i), &uart_states[i]);
826		disable_irq(UNIT_INT(i));
827	}
828	local_irq_restore(flags);
829}
830
831module_init(sb1250_duart_init);
832module_exit(sb1250_duart_fini);
833MODULE_DESCRIPTION("SB1250 Duart serial driver");
834MODULE_AUTHOR("Broadcom Corp.");
835
836#ifdef CONFIG_SIBYTE_SB1250_DUART_CONSOLE
837
838/*
839 * Serial console stuff.  Very basic, polling driver for doing serial
840 * console output.  The console_sem is held by the caller, so we
841 * shouldn't be interrupted for more console activity.
842 * XXXKW What about getting interrupted by uart driver activity?
843 */
844
845void serial_outc(unsigned char c, int line)
846{
847	uart_state_t *port = uart_states + line;
848	while (!(READ_SERCSR(port->status, line) & M_DUART_TX_RDY)) ;
849	WRITE_SERCSR(c, port->tx_hold, line);
850	while (!(READ_SERCSR(port->status, port->line) & M_DUART_TX_EMT)) ;
851}
852
853static void ser_console_write(struct console *cons, const char *s,
854	unsigned int count)
855{
856	int line = cons->index;
857	uart_state_t *port = uart_states + line;
858	u32 imr;
859
860	imr = READ_SERCSR(port->imr, line);
861	WRITE_SERCSR(0, port->imr, line);
862	while (count--) {
863		if (*s == '\n')
864			serial_outc('\r', line);
865		serial_outc(*s++, line);
866	}
867	WRITE_SERCSR(imr, port->imr, line);
868}
869
870static struct tty_driver *ser_console_device(struct console *c, int *index)
871{
872	*index = c->index;
873	return sb1250_duart_driver;
874}
875
876static int ser_console_setup(struct console *cons, char *str)
877{
878	int i;
879
880	sb1250_duart_init_present_lines();
881
882	for (i = 0; i < DUART_MAX_LINE; i++) {
883		uart_state_t *port = uart_states + i;
884		u32 cflags = DEFAULT_CFLAGS;
885
886		if (!sb1250_duart_present[i])
887			continue;
888
889		init_duart_port(port, i);
890		if (str) {
891			int speed;
892			char par = 'n';
893			int cbits = 8;
894
895			cflags = 0;
896
897			/*
898			 * format is in Documentation/serial_console.txt
899			 */
900			sscanf(str, "%d%c%d", &speed, &par, &cbits);
901
902			switch (speed) {
903			case 200:
904			case 300:
905			case 1200:
906				cflags |= B1200;
907				break;
908			case 1800:
909				cflags |= B1800;
910				break;
911			case 2400:
912				cflags |= B2400;
913				break;
914			case 4800:
915				cflags |= B4800;
916				break;
917			default:
918			case 9600:
919				cflags |= B9600;
920				break;
921			case 19200:
922				cflags |= B19200;
923				break;
924			case 38400:
925				cflags |= B38400;
926				break;
927			case 57600:
928				cflags |= B57600;
929				break;
930			case 115200:
931				cflags |= B115200;
932				break;
933			}
934			switch (par) {
935			case 'o':
936				cflags |= PARODD;
937			case 'e':
938				cflags |= PARENB;
939			}
940			switch (cbits) {
941			default:	// we only do 7 or 8
942			case 8:
943				cflags |= CS8;
944				break;
945			case 7:
946				cflags |= CS7;
947				break;
948			}
949		}
950		duart_set_cflag(i, cflags);
951		WRITE_SERCSR(M_DUART_RX_EN | M_DUART_TX_EN, port->cmd, i);
952	}
953
954	return 0;
955}
956
957static struct console sb1250_ser_cons = {
958	.name		= "duart",
959	.write		= ser_console_write,
960	.device		= ser_console_device,
961	.setup		= ser_console_setup,
962	.flags		= CON_PRINTBUFFER,
963	.index		= -1,
964};
965
966static int __init sb1250_serial_console_init(void)
967{
968	//add_preferred_console("duart", 0, "57600n8");
969	register_console(&sb1250_ser_cons);
970	return 0;
971}
972
973console_initcall(sb1250_serial_console_init);
974
975#endif /* CONFIG_SIBYTE_SB1250_DUART_CONSOLE */
976