summaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/drivers/serial/ubi32_serdes.c
blob: 79037da49416a4500798315307101432b5709b19 (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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/*
 * drivers/serial/ubi32_serdes.c
 *   Ubicom32 On-Chip Serial Driver
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include <linux/platform_device.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial_core.h>

#include <asm/ip5000.h>
#include <asm/ubicom32suart.h>


#define SERIAL_UBICOM_PIN_RXD	(1 << 0)
#define SERIAL_UBICOM_PIN_TXD	(1 << 6)
#define SERIAL_UBICOM_CTL0	0x8b300000
#define SERIAL_UBICOM_CTL1	0x00000009

#define SERIAL_UBICOM_DATA_BIT	8	/* Fixed parameter - do not change */
#define SERIAL_UBICOM_PAR_BIT	0	/* Fixed parameter - do not change */
#define SERIAL_UBICOM_STOP_BIT	1	/* Fixed parameter - do not change */

/* UART name and device definitions */
#define UBI32_SERDES_NAME	"ttyUS" // XXX
#define UBI32_SERDES_MAJOR	206 // XXX
#define UBI32_SERDES_MINOR	64 // XXX

#define PORT_UBI32_SERDES	1234
#define NR_PORTS 1

struct uart_port ubi32_serdes_ports[NR_PORTS];

struct ubi32_serdes_resource {
	void *uart_base_addr;
	int uart_irq;
	int uart_clock;
} ubi32_serdes_resource[NR_PORTS] = {
	/*
	 * Get params from kernel command line (required for early printk)
	 * or from platform resources.
	 */
	{0, 0, 0}
};

/*
 * Can get overridden by 'serdes=' kernel command line.
 */
static int ubi32_serdes_default_baud_rate = 115200;


#define IO_PORT(port) ((struct ubicom32_io_port *)port->membase)
#define IO_PORT_INT_STATUS(port) (IO_PORT(port)->int_status)
#define IO_PORT_INT_MASK(port) (IO_PORT(port)->int_mask)
#define IO_PORT_INT_CLR(port) (IO_PORT(port)->int_clr)


/*
 * ubi32_serdes_get_char()
 */
static u8_t ubi32_serdes_get_char(struct ubicom32_io_port *io_port)
{
	/*
	 * Read from hardware (forced 32-bit atomic read).
	 */
	u32_t data = 0;

	if ( io_port ) {
		io_port->int_clr = IO_PORTX_INT_SERDES_RXBF;
		asm volatile (
			"move.4		%0, %1		\n\t"
			: "=r" (data)
			: "m" (*(u32_t *)&(io_port->rx_fifo))
			);
	}

	return (u8_t)(data & 0x000000ff);
}

/*
 * ubi32_serdes_put_char()
 */
static void ubi32_serdes_put_char(struct ubicom32_io_port *io_port, u8_t c)
{
	u32_t data = 0x0000fe00 | (c << 1);

	if ( io_port ) {
		/*
		 * Fixed data format:
		 * [LSB]1 start bit - 8 data bits - no parity - 1 stop bit[MSB]
		 */
		io_port->int_clr = IO_PORTX_INT_SERDES_TXBE;
		io_port->ctl2 = data;
		io_port->int_set = IO_PORTX_INT_SERDES_TXBUF_VALID;
	}
}

static void ubi32_serdes_hw_init(struct uart_port *port, int baud)
{
	struct ubicom32_io_port *io_port = IO_PORT(port);

	if ( io_port ) {
		/*
		 * Put port functions 1-4 into reset state.
		 * Function 0 (GPIO) does not need or have a reset bit.
		 *
		 * Select SERDES function for restart below.
		 */
		io_port->function =
			IO_FUNC_FUNCTION_RESET(1) | IO_FUNC_FUNCTION_RESET(2) |
			IO_FUNC_FUNCTION_RESET(3) | IO_FUNC_FUNCTION_RESET(4) |
			IO_PORTX_FUNC_SERDES;

		/*
		 * Configure SERDES baudrate
		 */
		if ( baud == 0 ) {
			baud = ubi32_serdes_default_baud_rate;
		}

		io_port->ctl0 =
			SERIAL_UBICOM_CTL0 |
			((port->uartclk / (16 * baud)) - 1);

		io_port->ctl1 =
			SERIAL_UBICOM_CTL1;

		/*
		 * don't interrupt until startup and start_tx
		 */
		io_port->int_mask = 0;

		/*
		 * Set TXD pin output, RXD input and prevent GPIO
		 * override on the TXD & RXD pins
		 */
		io_port->gpio_ctl &= ~SERIAL_UBICOM_PIN_RXD;
		io_port->gpio_ctl |= SERIAL_UBICOM_PIN_TXD;
		io_port->gpio_mask &= ~(SERIAL_UBICOM_PIN_RXD | SERIAL_UBICOM_PIN_TXD);

		/*
		 * Restart (un-reset) the port's SERDES function.
		 */
		io_port->function &= ~(IO_FUNC_FUNCTION_RESET(IO_PORTX_FUNC_SERDES));
	}
}

#define ULITE_STATUS_RXVALID IO_PORTX_INT_SERDES_RXBF
#define ULITE_STATUS_OVERRUN 0
#define ULITE_STATUS_FRAME 0
#define ULITE_STATUS_PARITY 0
#define ULITE_STATUS_TXEMPTY IO_PORTX_INT_SERDES_TXBE
#define ULITE_STATUS_TXFULL 0

static int ubi32_serdes_receive(struct uart_port *port, int stat)
{
	struct tty_struct *tty = port->info->port.tty;
	unsigned char ch = 0;
	char flag = TTY_NORMAL;

	if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
		     | ULITE_STATUS_FRAME)) == 0)
		return 0;

	/* stats */
	if (stat & ULITE_STATUS_RXVALID) {
		port->icount.rx++;
		ch = ubi32_serdes_get_char((struct ubicom32_io_port *)port->membase);

		if (stat & ULITE_STATUS_PARITY)
			port->icount.parity++;
	}

	if (stat & ULITE_STATUS_OVERRUN)
		port->icount.overrun++;

	if (stat & ULITE_STATUS_FRAME)
		port->icount.frame++;


	/* drop byte with parity error if IGNPAR specificed */
	if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
		stat &= ~ULITE_STATUS_RXVALID;

	stat &= port->read_status_mask;

	if (stat & ULITE_STATUS_PARITY)
		flag = TTY_PARITY;

	stat &= ~port->ignore_status_mask;

	if (stat & ULITE_STATUS_RXVALID)
		tty_insert_flip_char(tty, ch, flag);

	if (stat & ULITE_STATUS_FRAME)
		tty_insert_flip_char(tty, 0, TTY_FRAME);

	if (stat & ULITE_STATUS_OVERRUN)
		tty_insert_flip_char(tty, 0, TTY_OVERRUN);

	return 1;
}

/*
 * interrupts are disabled on entry
 */
static void ubi32_serdes_stop_tx(struct uart_port *port)
{
	IO_PORT_INT_MASK(port) = IO_PORT_INT_MASK(port) & ~IO_PORTX_INT_SERDES_TXBE;
}

static int ubi32_serdes_transmit(struct uart_port *port, int stat)
{
	struct circ_buf *xmit  = &port->info->xmit;

	if (!(stat & IO_PORTX_INT_SERDES_TXBE))
		return 0;

	if (port->x_char) {
		ubi32_serdes_put_char(IO_PORT(port), port->x_char);
		port->x_char = 0;
		port->icount.tx++;
		return 1;
	}

	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
		ubi32_serdes_stop_tx(port);
		return 0;
	}

	ubi32_serdes_put_char(IO_PORT(port), xmit->buf[xmit->tail]);
	xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
	port->icount.tx++;

	/* wake up */
	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(port);

	if (uart_circ_empty(xmit))
		ubi32_serdes_stop_tx(port);

	return 1;
}

/*
 * port is locked and interrupts are disabled
 */
static void ubi32_serdes_start_tx(struct uart_port *port)
{
	IO_PORT_INT_MASK(port) = IO_PORT_INT_MASK(port) | IO_PORTX_INT_SERDES_TXBE;
	ubi32_serdes_transmit(port, IO_PORT_INT_STATUS(port));
}

/*
 * Interrupts are enabled
 */
static void ubi32_serdes_stop_rx(struct uart_port *port)
{
	/* don't forward any more data (like !CREAD) */
	port->ignore_status_mask = IO_PORTX_INT_SERDES_RXBF;
}

/*
 * Set the modem control timer to fire immediately.
 */
static void ubi32_serdes_enable_ms(struct uart_port *port)
{
	/* N/A */
}

static irqreturn_t ubi32_serdes_isr(int irq, void *dev_id)
{
	struct uart_port *port = dev_id;
	int busy;

	spin_lock(&port->lock);

	do {
		int stat = IO_PORT_INT_STATUS(port);
		busy  = ubi32_serdes_receive(port, stat);
		busy |= ubi32_serdes_transmit(port, stat);
	} while (busy);

	tty_flip_buffer_push(port->info->port.tty);

	spin_unlock(&port->lock);

	return IRQ_HANDLED;
}

/*
 * Return TIOCSER_TEMT when transmitter is not busy.
 */
static unsigned int ubi32_serdes_tx_empty(struct uart_port *port)
{
	unsigned long flags;
	unsigned int ret;

	spin_lock_irqsave(&port->lock, flags);
	ret = IO_PORT_INT_STATUS(port);
	spin_unlock_irqrestore(&port->lock, flags);

	return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
}

static unsigned int ubi32_serdes_get_mctrl(struct uart_port *port)
{
	return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
}

static void ubi32_serdes_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	/* N/A */
}

/*
 * Interrupts are always disabled.
 */
static void ubi32_serdes_break_ctl(struct uart_port *port, int break_state)
{
	/* N/A */
}

static int ubi32_serdes_startup(struct uart_port *port)
{
	if (request_irq(port->irq, ubi32_serdes_isr, IRQF_DISABLED,
	     "UBI32_SERDES", port)) {
		printk(KERN_NOTICE "Unable to attach port interrupt\n");
		return -EBUSY;
	}

	IO_PORT_INT_CLR(port) = IO_PORTX_INT_SERDES_RXBF;
	IO_PORT_INT_MASK(port) = IO_PORTX_INT_SERDES_RXBF;
	return 0;
}

static void ubi32_serdes_shutdown(struct uart_port *port)
{
	struct ubi32_serdes_port *uart = (struct ubi32_serdes_port *)port;

	IO_PORT_INT_MASK(port) = 0;
	free_irq(port->irq, uart);
}

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

	spin_lock_irqsave(&port->lock, flags);

	port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
		| ULITE_STATUS_TXFULL;

	if (termios->c_iflag & INPCK)
		port->read_status_mask |=
			ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;

	port->ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR)
		port->ignore_status_mask |= ULITE_STATUS_PARITY
			| ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;

	/* ignore all characters if CREAD is not set */
	if ((termios->c_cflag & CREAD) == 0)
		port->ignore_status_mask |=
			ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
			| ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;

	/* update timeout */
	baud = uart_get_baud_rate(port, termios, old, 0, 460800);
	uart_update_timeout(port, termios->c_cflag, baud);

	IO_PORT(port)->ctl0 = SERIAL_UBICOM_CTL0 |
			((port->uartclk / (16 * baud)) - 1);

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

static const char *ubi32_serdes_type(struct uart_port *port)
{
	return port->type == PORT_UBI32_SERDES ? "UBI32_SERDES" : NULL;
}

/*
 * Release the memory region(s) being used by 'port'.
 */
static void ubi32_serdes_release_port(struct uart_port *port)
{
}

/*
 * Request the memory region(s) being used by 'port'.
 */
static int ubi32_serdes_request_port(struct uart_port *port)
{
	return 0;
}

/*
 * Configure/autoconfigure the port.
 */
static void ubi32_serdes_config_port(struct uart_port *port, int flags)
{
	if (flags & UART_CONFIG_TYPE &&
	    ubi32_serdes_request_port(port) == 0)
		port->type = PORT_UBI32_SERDES;
}

/*
 * Verify the new serial_struct (for TIOCSSERIAL).
 * The only change we allow are to the flags and type, and
 * even then only between PORT_UBI32_SERDES and PORT_UNKNOWN
 */
static int
ubi32_serdes_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	return 0;
}

static struct uart_ops ubi32_serdes_pops = {
	.tx_empty	= ubi32_serdes_tx_empty,
	.set_mctrl	= ubi32_serdes_set_mctrl,
	.get_mctrl	= ubi32_serdes_get_mctrl,
	.stop_tx	= ubi32_serdes_stop_tx,
	.start_tx	= ubi32_serdes_start_tx,
	.stop_rx	= ubi32_serdes_stop_rx,
	.enable_ms	= ubi32_serdes_enable_ms,
	.break_ctl	= ubi32_serdes_break_ctl,
	.startup	= ubi32_serdes_startup,
	.shutdown	= ubi32_serdes_shutdown,
	.set_termios	= ubi32_serdes_set_termios,
	.type		= ubi32_serdes_type,
	.release_port	= ubi32_serdes_release_port,
	.request_port	= ubi32_serdes_request_port,
	.config_port	= ubi32_serdes_config_port,
	.verify_port	= ubi32_serdes_verify_port,
};

static void __init ubi32_serdes_init_ports(void)
{
	int i;

	for (i = 0; i < NR_PORTS; i++) {
		ubi32_serdes_ports[i].uartclk   = ubi32_serdes_resource[i].uart_clock;
		ubi32_serdes_ports[i].ops       = &ubi32_serdes_pops;
		ubi32_serdes_ports[i].line      = i;
		ubi32_serdes_ports[i].iotype    = UPIO_MEM;
		ubi32_serdes_ports[i].membase   =
			(void __iomem *)ubi32_serdes_resource[i].uart_base_addr;
		ubi32_serdes_ports[i].mapbase   =
			(resource_size_t)ubi32_serdes_resource[i].uart_base_addr;
		ubi32_serdes_ports[i].irq       =
			ubi32_serdes_resource[i].uart_irq;
		ubi32_serdes_ports[i].flags     = UPF_BOOT_AUTOCONF;

		ubi32_serdes_hw_init(&ubi32_serdes_ports[i], 0);
	}

}

#ifdef CONFIG_SERIAL_UBI32_SERDES_CONSOLE
/*
 * If the port was already initialised (eg, by a boot loader),
 * try to determine the current setup.
 */
static void __init
ubi32_serdes_console_get_options(struct uart_port *port, int *baud)
{
	u32 round_to = 1200;
	u32 real_baud;

	/*
	 * We might get called before platform init and with no
	 * kernel command line options, so port might be NULL.
	 */
	*baud = ubi32_serdes_default_baud_rate;;
	if ( IO_PORT(port) == 0 )
		return;

	real_baud = port->uartclk
		/ (16 * ((IO_PORT(port)->ctl0 & ~SERIAL_UBICOM_CTL0) + 1));

	*baud = ((real_baud + round_to - 1) / round_to) * round_to;

	pr_debug("%s:baud = %d, real_baud = %d\n", __FUNCTION__, *baud, real_baud);
}
#endif

#if defined(CONFIG_SERIAL_UBI32_SERDES_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
static struct uart_driver ubi32_serdes_reg;

static int __init
ubi32_serdes_console_setup(struct console *co, char *options)
{
	struct uart_port *port;
#ifdef CONFIG_SERIAL_UBI32_SERDES_CONSOLE
	int baud = ubi32_serdes_default_baud_rate;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';
#endif

	/*
	 * Check whether an invalid uart number has been specified, and
	 * if so, search for the first available port that does have
	 * console support.
	 */
	if (co->index == -1 || co->index >= NR_PORTS)
		co->index = 0;
	port = &ubi32_serdes_ports[co->index];

#ifdef CONFIG_SERIAL_UBI32_SERDES_CONSOLE
	if (options) {
		uart_parse_options(options, &baud, &parity, &bits, &flow);
		ubi32_serdes_hw_init(port, baud);
	}
	else
		ubi32_serdes_console_get_options(port, &baud);

	return uart_set_options(port, co, baud, parity, bits, flow);
#else
	return 0;
#endif
}
#endif /* defined (CONFIG_SERIAL_UBI32_SERDES_CONSOLE) ||
				 defined (CONFIG_EARLY_PRINTK) */

#ifdef CONFIG_SERIAL_UBI32_SERDES_CONSOLE
static void
ubi32_serdes_console_putchar(struct uart_port *port, int ch)
{
	if ( IO_PORT(port) ) {
		while (!(IO_PORT_INT_STATUS(port) & IO_PORTX_INT_SERDES_TXBE))
			barrier();
		ubi32_serdes_put_char(IO_PORT(port), ch);
	}
}

/*
 * Interrupts are disabled on entering
 */
static void
ubi32_serdes_console_write(struct console *co, const char *s, unsigned int count)
{
	struct uart_port *port = &ubi32_serdes_ports[co->index];
	unsigned long flags = 0;

	spin_lock_irqsave(&port->lock, flags);
	uart_console_write(port, s, count, ubi32_serdes_console_putchar);
	spin_unlock_irqrestore(&port->lock, flags);

}

static struct console ubi32_serdes_console = {
	.name		= UBI32_SERDES_NAME,
	.write		= ubi32_serdes_console_write,
	.device		= uart_console_device,
	.setup		= ubi32_serdes_console_setup,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
	.data		= &ubi32_serdes_reg,
};

static int __init ubi32_serdes_console_init(void)
{
	ubi32_serdes_init_ports();
	register_console(&ubi32_serdes_console);
	return 0;
}
console_initcall(ubi32_serdes_console_init);

#define UBI32_SERDES_CONSOLE	&ubi32_serdes_console
#else
#define UBI32_SERDES_CONSOLE	NULL
#endif /* CONFIG_SERIAL_UBI32_SERDES_CONSOLE */


#ifdef CONFIG_EARLY_PRINTK
static __init void ubi32_serdes_early_putc(struct uart_port *port, int ch)
{
	unsigned timeout = 0xffff;

	while ((!(IO_PORT_INT_STATUS(port) & IO_PORTX_INT_SERDES_TXBE)) && --timeout)
		cpu_relax();
	ubi32_serdes_put_char(IO_PORT(port), ch);
}

static __init void ubi32_serdes_early_write(struct console *con, const char *s,
					unsigned int n)
{
	struct uart_port *port = &ubi32_serdes_ports[con->index];
	unsigned int i;

	for (i = 0; i < n; i++, s++) {
		if (*s == '\n')
			ubi32_serdes_early_putc(port, '\r');
		ubi32_serdes_early_putc(port, *s);
	}
}

static struct __init console ubi32_serdes_early_console = {
	.name = "early_US",
	.write = ubi32_serdes_early_write,
	.device = uart_console_device,
	.flags = CON_PRINTBUFFER,
	.setup = ubi32_serdes_console_setup,
	.index = -1,
	.data  = &ubi32_serdes_reg,
};

/*
 * XXX Unused in our driver. Need to find out what the termios initialization is good/needed for.
 */
struct console __init *ubi32_serdes_early_init(unsigned int port_index,
						unsigned int cflag)
{
	struct uart_port *uart;
	struct ktermios t;

	if (port_index == -1 || port_index >= NR_PORTS)
		port_index = 0;
	ubi32_serdes_init_ports();
	ubi32_serdes_early_console.index = port_index;
	uart = &ubi32_serdes_ports[port_index];
	t.c_cflag = cflag;
	t.c_iflag = 0;
	t.c_oflag = 0;
	t.c_lflag = ICANON;
	t.c_line = port_index;
	ubi32_serdes_set_termios(uart, &t, &t);
	return &ubi32_serdes_early_console;
}

#endif /* CONFIG_SERIAL_UBI32_SERDES_CONSOLE */

static struct uart_driver ubi32_serdes_reg = {
	.owner			= THIS_MODULE,
	.driver_name		= "ubi32_serdes",
	.dev_name		= UBI32_SERDES_NAME,
	.major			= UBI32_SERDES_MAJOR,
	.minor			= UBI32_SERDES_MINOR,
	.nr			= NR_PORTS,
	.cons			= UBI32_SERDES_CONSOLE,
};

static int ubi32_serdes_suspend(struct platform_device *dev, pm_message_t state)
{
	struct uart_port *port = platform_get_drvdata(dev);

	if (port)
		uart_suspend_port(&ubi32_serdes_reg, port);

	return 0;
}

static int ubi32_serdes_resume(struct platform_device *dev)
{
	struct uart_port *port = platform_get_drvdata(dev);

	if (port)
		uart_resume_port(&ubi32_serdes_reg, port);

	return 0;
}

static int ubi32_serdes_probe(struct platform_device *dev)
{
	struct resource *res = dev->resource;
	int i;

	for (i = 0; i < dev->num_resources; i++, res++) {
		if (res->flags & IORESOURCE_MEM) {
			ubi32_serdes_resource[0].uart_base_addr = (void *) res->start;
		}
		else if (res->flags & IORESOURCE_IRQ) {
			ubi32_serdes_resource[0].uart_irq = res->start;
		}
		else if (res->flags & UBICOM32_SUART_IORESOURCE_CLOCK) {
			ubi32_serdes_resource[0].uart_clock = res->start;
		}
	}

	ubi32_serdes_init_ports();

	return 0;
}

static int ubi32_serdes_remove(struct platform_device *pdev)
{
	struct uart_port *port = platform_get_drvdata(pdev);

	platform_set_drvdata(pdev, NULL);

	if (port)
		uart_remove_one_port(&ubi32_serdes_reg, port);

	return 0;
}

static struct platform_driver ubi32_serdes_driver = {
	.remove		= ubi32_serdes_remove,
	.suspend	= ubi32_serdes_suspend,
	.resume		= ubi32_serdes_resume,
	.driver		= {
		.name	= "ubicom32suart",
		.owner	= THIS_MODULE,
	},
};


#ifndef MODULE
/*
 * Called at boot time.
 *
 * You can specify IO base, IRQ, and clock for the serdes serial port
 * using kernel command line "serdes=0xiobase,irq,clock".  Values
 * specified will be overwritten by platform device data, if present.
 */
static int __init ubi32_serdes_setup(char *str)
{
#define N_PARMS   (4+1)
	int ints[N_PARMS];
	int i;

	str = get_options(str, ARRAY_SIZE(ints), ints);

	for (i = 0; i < N_PARMS; i++) {
		if (i < ints[0]) {
			if (i == 0) {
				ubi32_serdes_resource[0].uart_base_addr = (void *) ints[i+1];
			}
			else if (i == 1) {
				ubi32_serdes_resource[0].uart_irq = ints[i+1];
			}
			else if (i == 2) {
				ubi32_serdes_resource[0].uart_clock = ints[i+1];
			}
			else if (i == 3) {
				ubi32_serdes_default_baud_rate = ints[i+1];
			}
		}
	}
	return 1;
}

__setup("serdes=", ubi32_serdes_setup);
#endif

static int __init ubi32_serdes_init(void)
{
	int ret;

	pr_info("Serial: Ubicom32 serdes uart serial driver\n");

	ret = platform_driver_probe(&ubi32_serdes_driver, ubi32_serdes_probe);
	if (ret != 0) {
		printk(KERN_INFO "serdes platform_driver_probe() failed: %d\n", ret);
		return ret;
	}

	ubi32_serdes_init_ports();

	ret = uart_register_driver(&ubi32_serdes_reg);
	if ( ret == 0 ) {
		ret = uart_add_one_port(&ubi32_serdes_reg, &ubi32_serdes_ports[0]);
		if ( ret != 0 ) {
			uart_unregister_driver(&ubi32_serdes_reg);
		}
	}

	return ret;
}

static void __exit ubi32_serdes_exit(void)
{
	platform_driver_unregister(&ubi32_serdes_driver);
	uart_unregister_driver(&ubi32_serdes_reg);
}

module_init(ubi32_serdes_init);
module_exit(ubi32_serdes_exit);

MODULE_AUTHOR("Rainer Keller <rkeller@ubicom.com>");
MODULE_DESCRIPTION("Ubicom generic serial port driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_CHARDEV_MAJOR(UBI32_SERDES_MAJOR);
MODULE_ALIAS("platform:ubi32_serdes");