summaryrefslogtreecommitdiffstats
path: root/target/linux/adm5120/files/drivers/serial/adm5120_uart.c
blob: 83c5f720127ef50275a452a0995b91f2537381ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 *	Serial driver for ADM5120 SoC
 *
 *	Derived from drivers/serial/uart00.c
 *	Copyright 2001 Altera Corporation
 *
 *	Some pieces are derived from the ADMtek 2.4 serial driver.
 *	Copyright (C) ADMtek Incorporated, 2003
 *		daniell@admtek.com.tw
 *	Which again was derived from drivers/char/serial.c
 *	Copyright (C) Linus Torvalds et al.
 *
 *	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2005
 */

#include <linux/autoconf.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/console.h>

#include <asm/mach-adm5120/adm5120_defs.h>
#include <asm/mach-adm5120/adm5120_irq.h>

#define ADM5120_UART_REG(base, reg) \
	(*(volatile u32 *)KSEG1ADDR((base)+(reg)))

#define ADM5120_UARTCLK_FREQ            62500000
#define ADM5120_UART_BAUDDIV(rate)	((unsigned long)(ADM5120_UARTCLK_FREQ/(16*(rate)) - 1))

#define ADM5120_UART_BAUD115200		ADM5120_UART_BAUDDIV(115200)

#define ADM5120_UART_DATA		0x00
#define ADM5120_UART_RS			0x04
#define ADM5120_UART_LCR_H		0x08
#define ADM5120_UART_LCR_M		0x0c
#define ADM5120_UART_LCR_L		0x10
#define ADM5120_UART_CR			0x14
#define ADM5120_UART_FR			0x18
#define ADM5120_UART_IR			0x1c

#define ADM5120_UART_FE			0x01
#define ADM5120_UART_PE			0x02
#define ADM5120_UART_BE			0x04
#define ADM5120_UART_OE			0x08
#define ADM5120_UART_ERR		0x0f
#define ADM5120_UART_FIFO_EN		0x10
#define ADM5120_UART_EN			0x01
#define ADM5120_UART_TIE		0x20
#define ADM5120_UART_RIE		0x50
#define ADM5120_UART_IE			0x78
#define ADM5120_UART_CTS		0x01
#define ADM5120_UART_DSR		0x02
#define ADM5120_UART_DCD		0x04
#define ADM5120_UART_TXFF		0x20
#define ADM5120_UART_TXFE		0x80
#define ADM5120_UART_RXFE		0x10
#define ADM5120_UART_BRK		0x01
#define ADM5120_UART_PEN		0x02
#define ADM5120_UART_EPS		0x04
#define ADM5120_UART_STP2		0x08
#define ADM5120_UART_W5			0x00
#define ADM5120_UART_W6			0x20
#define ADM5120_UART_W7			0x40
#define ADM5120_UART_W8			0x60
#define ADM5120_UART_MIS		0x01
#define ADM5120_UART_RIS		0x02
#define ADM5120_UART_TIS		0x04
#define ADM5120_UART_RTIS		0x08

static void adm5120ser_stop_tx(struct uart_port *port)
{
	ADM5120_UART_REG(port->iobase, ADM5120_UART_CR) &= ~ADM5120_UART_TIE;
}

static void adm5120ser_irq_rx(struct uart_port *port)
{
	struct tty_struct *tty = port->info->tty;
	unsigned int status, ch, rds, flg, ignored = 0;

	status = ADM5120_UART_REG(port->iobase, ADM5120_UART_FR);
	while (!(status & ADM5120_UART_RXFE)) {
		/* 
		 * We need to read rds before reading the 
		 * character from the fifo
		 */
		rds = ADM5120_UART_REG(port->iobase, ADM5120_UART_RS);
		ch = ADM5120_UART_REG(port->iobase, ADM5120_UART_DATA);
		port->icount.rx++;

		if (tty->low_latency)
			tty_flip_buffer_push(tty);

		flg = TTY_NORMAL;

		/*
		 * Note that the error handling code is
		 * out of the main execution path
		 */
		if (rds & ADM5120_UART_ERR)
			goto handle_error;
		if (uart_handle_sysrq_char(port, ch))
			goto ignore_char;

	error_return:
		tty_insert_flip_char(tty, ch, flg);

	ignore_char:
		status = ADM5120_UART_REG(port->iobase, ADM5120_UART_FR);
	}
 out:
	tty_flip_buffer_push(tty);
	return;

 handle_error:
 	ADM5120_UART_REG(port->iobase, ADM5120_UART_RS) = 0xff;
	if (rds & ADM5120_UART_BE) {
		port->icount.brk++;
		if (uart_handle_break(port))
			goto ignore_char;
	} else if (rds & ADM5120_UART_PE)
		port->icount.parity++;
	else if (rds & ADM5120_UART_FE)
		port->icount.frame++;
	if (rds & ADM5120_UART_OE)
		port->icount.overrun++;

	if (rds & port->ignore_status_mask) {
		if (++ignored > 100)
			goto out;
		goto ignore_char;
	}
	rds &= port->read_status_mask;

	if (rds & ADM5120_UART_BE)
		flg = TTY_BREAK;
	else if (rds & ADM5120_UART_PE)
		flg = TTY_PARITY;
	else if (rds & ADM5120_UART_FE)
		flg = TTY_FRAME;

	if (rds & ADM5120_UART_OE) {
		/*
		 * CHECK: does overrun affect the current character?
		 * ASSUMPTION: it does not.
		 */
		tty_insert_flip_char(tty, ch, flg);
		ch = 0;
		flg = TTY_OVERRUN;
	}
#ifdef CONFIG_MAGIC_SYSRQ
	port->sysrq = 0;
#endif
	goto error_return;
}

static void adm5120ser_irq_tx(struct uart_port *port)
{
	struct circ_buf *xmit = &port->info->xmit;
	int count;

	if (port->x_char) {
		ADM5120_UART_REG(port->iobase, ADM5120_UART_DATA) =
		    port->x_char;
		port->icount.tx++;
		port->x_char = 0;
		return;
	}
	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
		adm5120ser_stop_tx(port);
		return;
	}

	count = port->fifosize >> 1;
	do {
		ADM5120_UART_REG(port->iobase, ADM5120_UART_DATA) =
		    xmit->buf[xmit->tail];
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		port->icount.tx++;
		if (uart_circ_empty(xmit))
			break;
	} while (--count > 0);

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(port);

	if (uart_circ_empty(xmit))
		adm5120ser_stop_tx(port);
}

static void adm5120ser_irq_modem(struct uart_port *port)
{
	unsigned int status;

	status = ADM5120_UART_REG(port->iobase, ADM5120_UART_FR);

	if (status & ADM5120_UART_DCD)
		uart_handle_dcd_change(port, status & ADM5120_UART_DCD);

	if (status & ADM5120_UART_DSR)
		port->icount.dsr++;

	if (status & ADM5120_UART_CTS)
		uart_handle_cts_change(port, status & ADM5120_UART_CTS);

	wake_up_interruptible(&port->info->delta_msr_wait);
}

static irqreturn_t adm5120ser_irq(int irq, void *dev_id)
{
	struct uart_port *port = dev_id;
	unsigned long ir = ADM5120_UART_REG(port->iobase, ADM5120_UART_IR);

	if (ir & (ADM5120_UART_RIS | ADM5120_UART_RTIS))
		adm5120ser_irq_rx(port);
	if (ir & ADM5120_UART_TIS)
		adm5120ser_irq_tx(port);
	if (ir & ADM5120_UART_MIS) {
		adm5120ser_irq_modem(port);
		ADM5120_UART_REG(port->iobase, ADM5120_UART_IR) = 0xff;
	}

	return IRQ_HANDLED;
}

static unsigned int adm5120ser_tx_empty(struct uart_port *port)
{
	unsigned int fr = ADM5120_UART_REG(port->iobase, ADM5120_UART_FR);
	return (fr & ADM5120_UART_TXFE) ? TIOCSER_TEMT : 0;
}

static void adm5120ser_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
}

static unsigned int adm5120ser_get_mctrl(struct uart_port *port)
{
	unsigned int result = 0;
	unsigned int fr = ADM5120_UART_REG(port->iobase, ADM5120_UART_FR);

	if (fr & ADM5120_UART_CTS)
		result |= TIOCM_CTS;
	if (fr & ADM5120_UART_DSR)
		result |= TIOCM_DSR;
	if (fr & ADM5120_UART_DCD)
		result |= TIOCM_CAR;
	return result;
}

static void adm5120ser_start_tx(struct uart_port *port)
{
	ADM5120_UART_REG(port->iobase, ADM5120_UART_CR) |= ADM5120_UART_TIE;
}

static void adm5120ser_stop_rx(struct uart_port *port)
{
	ADM5120_UART_REG(port->iobase, ADM5120_UART_CR) &= ~ADM5120_UART_RIE;
}

static void adm5120ser_enable_ms(struct uart_port *port)
{
}

static void adm5120ser_break_ctl(struct uart_port *port, int break_state)
{
	unsigned long flags;
	unsigned long lcrh;

	spin_lock_irqsave(&port->lock, flags);
	lcrh = ADM5120_UART_REG(port->iobase, ADM5120_UART_LCR_H);
	if (break_state == -1)
		lcrh |= ADM5120_UART_BRK;
	else
		lcrh &= ~ADM5120_UART_BRK;
	ADM5120_UART_REG(port->iobase, ADM5120_UART_LCR_H) = lcrh;
	spin_unlock_irqrestore(&port->lock, flags);
}

static int adm5120ser_startup(struct uart_port *port)
{
	int ret;

	ret = request_irq(port->irq, adm5120ser_irq, 0, "ADM5120 UART", port);
	if (ret) {
		printk(KERN_ERR "Couldn't get irq %d\n", port->irq);
		return ret;
	}
	ADM5120_UART_REG(port->iobase, ADM5120_UART_LCR_H) |=
	    ADM5120_UART_FIFO_EN;
	ADM5120_UART_REG(port->iobase, ADM5120_UART_CR) |=
	    ADM5120_UART_EN | ADM5120_UART_IE;
	return 0;
}

static void adm5120ser_shutdown(struct uart_port *port)
{
	ADM5120_UART_REG(port->iobase, ADM5120_UART_CR) &= ~ADM5120_UART_IE;
	free_irq(port->irq, port);
}

static void adm5120ser_set_termios(struct uart_port *port,
    struct ktermios *termios, struct ktermios *old)
{
	unsigned int baud, quot, lcrh;
	unsigned long flags;

	termios->c_cflag |= CREAD;

	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
	quot = uart_get_divisor(port, baud);

	lcrh = ADM5120_UART_FIFO_EN;
	switch (termios->c_cflag & CSIZE) {
		case CS5:
			lcrh |= ADM5120_UART_W5;
			break;
		case CS6:
			lcrh |= ADM5120_UART_W6;
			break;
		case CS7:
			lcrh |= ADM5120_UART_W7;
			break;
		default:
			lcrh |= ADM5120_UART_W8;
			break;
	}
	if (termios->c_cflag & CSTOPB)
		lcrh |= ADM5120_UART_STP2;
	if (termios->c_cflag & PARENB) {
		lcrh |= ADM5120_UART_PEN;
		if (!(termios->c_cflag & PARODD))
			lcrh |= ADM5120_UART_EPS;
	}

	spin_lock_irqsave(port->lock, flags);

	ADM5120_UART_REG(port->iobase, ADM5120_UART_LCR_H) = lcrh;

	/*
	 * Update the per-port timeout.
	 */
	uart_update_timeout(port, termios->c_cflag, baud);

	port->read_status_mask = ADM5120_UART_OE;
	if (termios->c_iflag & INPCK)
		port->read_status_mask |= ADM5120_UART_FE | ADM5120_UART_PE;
	if (termios->c_iflag & (BRKINT | PARMRK))
		port->read_status_mask |= ADM5120_UART_BE;

	/*
	 * Characters to ignore
	 */
	port->ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR)
		port->ignore_status_mask |= ADM5120_UART_FE | ADM5120_UART_PE;
	if (termios->c_iflag & IGNBRK) {
		port->ignore_status_mask |= ADM5120_UART_BE;
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns to (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
			port->ignore_status_mask |= ADM5120_UART_OE;
	}

	quot = ADM5120_UART_BAUD115200;
	ADM5120_UART_REG(port->iobase, ADM5120_UART_LCR_L) = quot & 0xff;
	ADM5120_UART_REG(port->iobase, ADM5120_UART_LCR_M) = quot >> 8;

	spin_unlock_irqrestore(&port->lock, flags);
}

static const char *adm5120ser_type(struct uart_port *port)
{
	return port->type == PORT_ADM5120 ? "ADM5120" : NULL;
}

static void adm5120ser_config_port(struct uart_port *port, int flags)
{
	if (flags & UART_CONFIG_TYPE)
		port->type = PORT_ADM5120;
}

static void adm5120ser_release_port(struct uart_port *port)
{
	release_mem_region(port->iobase, ADM5120_UART_SIZE);
}

static int adm5120ser_request_port(struct uart_port *port)
{
	return request_mem_region(port->iobase, ADM5120_UART_SIZE,
	    "adm5120-uart") != NULL ? 0 : -EBUSY; 
}

static struct uart_ops adm5120ser_ops = {
	.tx_empty =	adm5120ser_tx_empty,
	.set_mctrl =	adm5120ser_set_mctrl,
	.get_mctrl =	adm5120ser_get_mctrl,
	.stop_tx =	adm5120ser_stop_tx,
	.start_tx =	adm5120ser_start_tx,
	.stop_rx =	adm5120ser_stop_rx,
	.enable_ms =	adm5120ser_enable_ms,
	.break_ctl =	adm5120ser_break_ctl,
	.startup =	adm5120ser_startup,
	.shutdown =	adm5120ser_shutdown,
	.set_termios =	adm5120ser_set_termios,
	.type =		adm5120ser_type,
	.config_port =	adm5120ser_config_port,
	.release_port =	adm5120ser_release_port,
	.request_port =	adm5120ser_request_port,
};

static void adm5120console_put(const char c)
{
	while ((ADM5120_UART_REG(ADM5120_UART0_BASE, ADM5120_UART_FR) &
	     ADM5120_UART_TXFF) != 0);
	ADM5120_UART_REG(ADM5120_UART0_BASE, ADM5120_UART_DATA) = c;
}

static void adm5120console_write(struct console *con, const char *s,
    unsigned int count)
{
	while (count--) {
		if (*s == '\n')
			adm5120console_put('\r');
		adm5120console_put(*s);
		s++;
	}
}

static int adm5120console_setup(struct console *con, char *options)
{
	/* Set to 115200 baud, 8N1 and enable FIFO */
	ADM5120_UART_REG(ADM5120_UART0_BASE, ADM5120_UART_LCR_L) =
	    ADM5120_UART_BAUD115200 & 0xff;
	ADM5120_UART_REG(ADM5120_UART0_BASE, ADM5120_UART_LCR_M) =
	    ADM5120_UART_BAUD115200 >> 8;
	ADM5120_UART_REG(ADM5120_UART0_BASE, ADM5120_UART_LCR_H) =
	    ADM5120_UART_W8 | ADM5120_UART_FIFO_EN;
	/* Enable port */
	ADM5120_UART_REG(ADM5120_UART0_BASE, ADM5120_UART_CR) =
	    ADM5120_UART_EN;

	return 0;
}

static struct uart_driver adm5120ser_reg;

static struct console adm5120_serconsole = {
	.name =		"ttyS",
	.write =	adm5120console_write,
	.device =	uart_console_device,
	.setup =	adm5120console_setup,
	.flags =	CON_PRINTBUFFER,
	.cflag =	B115200 | CS8 | CREAD,
	.index =	0,
	.data =		&adm5120ser_reg,
};

static int __init adm5120console_init(void)
{
	register_console(&adm5120_serconsole);
	return 0;
}

console_initcall(adm5120console_init);


static struct uart_port adm5120ser_ports[] = {
	{
		.iobase =	ADM5120_UART0_BASE,
		.irq =		ADM5120_IRQ_UART0,
		.uartclk =	ADM5120_UARTCLK_FREQ,
		.fifosize =	16,
		.ops =		&adm5120ser_ops,
		.line =		0,
		.flags =	ASYNC_BOOT_AUTOCONF,
	},
#if (CONFIG_ADM5120_NR_UARTS > 1)
	{
		.iobase =	ADM5120_UART1_BASE,
		.irq =		ADM5120_IRQ_UART1,
		.uartclk =	ADM5120_UARTCLK_FREQ,
		.fifosize =	16,
		.ops =		&adm5120ser_ops,
		.line =		1,
		.flags =	ASYNC_BOOT_AUTOCONF,
	},
#endif
};

static struct uart_driver adm5120ser_reg = {
	.owner	=	THIS_MODULE,
	.driver_name =	"ttyS",
	.dev_name =	"ttyS",
	.major =	TTY_MAJOR,
	.minor =	64,
	.nr =		CONFIG_ADM5120_NR_UARTS,
	.cons =		&adm5120_serconsole,
};

static int __init adm5120ser_init(void)
{
	int ret, i;

	ret = uart_register_driver(&adm5120ser_reg);
	if (!ret) {
		for (i = 0; i < CONFIG_ADM5120_NR_UARTS; i++)
			uart_add_one_port(&adm5120ser_reg, &adm5120ser_ports[i]);
	}

	return ret;
}

__initcall(adm5120ser_init);