blob: 98dcea73b298595571b3bc05eaa9261974dd2a17 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/serial_txx9.c
3 *
4 * Derived from many drivers using generic_serial interface,
5 * especially serial_tx3912.c by Steven J. Hill and r39xx_serial.c
6 * (was in Linux/VR tree) by Jim Pick.
7 *
8 * Copyright (C) 1999 Harald Koerfgen
9 * Copyright (C) 2000 Jim Pick <jim@jimpick.com>
10 * Copyright (C) 2001 Steven J. Hill (sjhill@realitydiluted.com)
11 * Copyright (C) 2000-2002 Toshiba Corporation
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * Serial driver for TX3927/TX4927/TX4925/TX4938 internal SIO controller
18 *
19 * Revision History:
20 * 0.30 Initial revision. (Renamed from serial_txx927.c)
21 * 0.31 Use save_flags instead of local_irq_save.
22 * 0.32 Support SCLK.
23 * 0.33 Switch TXX9_TTY_NAME by CONFIG_SERIAL_TXX9_STDSERIAL.
24 * Support TIOCSERGETLSR.
25 * 0.34 Support slow baudrate.
26 * 0.40 Merge codes from mainstream kernel (2.4.22).
27 * 0.41 Fix console checking in rs_shutdown_port().
28 * Disable flow-control in serial_console_write().
29 * 0.42 Fix minor compiler warning.
30 * 1.00 Kernel 2.6. Converted to new serial core (based on 8250.c).
31 * 1.01 Set fifosize to make tx_empry called properly.
32 * Use standard uart_get_divisor.
33 * 1.02 Cleanup. (import 8250.c changes)
Ralf Baechlef5ee56c2005-09-09 13:01:32 -070034 * 1.03 Fix low-latency mode. (import 8250.c changes)
35 * 1.04 Remove usage of deprecated functions, cleanup.
Atsushi Nemoto83485f82006-03-22 00:07:45 -080036 * 1.05 More strict check in verify_port. Cleanup.
37 * 1.06 Do not insert a char caused previous overrun.
38 * Fix some spin_locks.
39 * Do not call uart_add_one_port for absent ports.
Atsushi Nemoto138c5d22007-02-10 01:45:05 -080040 * 1.07 Use CONFIG_SERIAL_TXX9_NR_UARTS. Cleanup.
Atsushi Nemoto09707692007-02-22 02:17:28 +090041 * 1.08 Use platform_device.
42 * Fix and cleanup suspend/resume/initialization codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#if defined(CONFIG_SERIAL_TXX9_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
46#define SUPPORT_SYSRQ
47#endif
48
49#include <linux/module.h>
50#include <linux/ioport.h>
51#include <linux/init.h>
52#include <linux/console.h>
53#include <linux/sysrq.h>
54#include <linux/delay.h>
Atsushi Nemoto09707692007-02-22 02:17:28 +090055#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/pci.h>
57#include <linux/tty.h>
58#include <linux/tty_flip.h>
59#include <linux/serial_core.h>
60#include <linux/serial.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000061#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Atsushi Nemoto07bafde2007-05-08 00:30:26 -070065static char *serial_version = "1.09";
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static char *serial_name = "TX39/49 Serial driver";
67
68#define PASS_LIMIT 256
69
70#if !defined(CONFIG_SERIAL_TXX9_STDSERIAL)
71/* "ttyS" is used for standard serial driver */
72#define TXX9_TTY_NAME "ttyTX"
Atsushi Nemoto07bafde2007-05-08 00:30:26 -070073#define TXX9_TTY_MINOR_START 196
74#define TXX9_TTY_MAJOR 204
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#else
76/* acts like standard serial driver */
77#define TXX9_TTY_NAME "ttyS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define TXX9_TTY_MINOR_START 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define TXX9_TTY_MAJOR TTY_MAJOR
Atsushi Nemoto07bafde2007-05-08 00:30:26 -070080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* flag aliases */
83#define UPF_TXX9_HAVE_CTS_LINE UPF_BUGGY_UART
84#define UPF_TXX9_USE_SCLK UPF_MAGIC_MULTIPLIER
85
86#ifdef CONFIG_PCI
87/* support for Toshiba TC86C001 SIO */
88#define ENABLE_SERIAL_TXX9_PCI
89#endif
90
91/*
92 * Number of serial ports
93 */
Atsushi Nemoto138c5d22007-02-10 01:45:05 -080094#define UART_NR CONFIG_SERIAL_TXX9_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Atsushi Nemoto83485f82006-03-22 00:07:45 -080096#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct uart_txx9_port {
99 struct uart_port port;
Atsushi Nemoto09707692007-02-22 02:17:28 +0900100 /* No additional info for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103#define TXX9_REGION_SIZE 0x24
104
105/* TXX9 Serial Registers */
106#define TXX9_SILCR 0x00
107#define TXX9_SIDICR 0x04
108#define TXX9_SIDISR 0x08
109#define TXX9_SICISR 0x0c
110#define TXX9_SIFCR 0x10
111#define TXX9_SIFLCR 0x14
112#define TXX9_SIBGR 0x18
113#define TXX9_SITFIFO 0x1c
114#define TXX9_SIRFIFO 0x20
115
116/* SILCR : Line Control */
117#define TXX9_SILCR_SCS_MASK 0x00000060
118#define TXX9_SILCR_SCS_IMCLK 0x00000000
119#define TXX9_SILCR_SCS_IMCLK_BG 0x00000020
120#define TXX9_SILCR_SCS_SCLK 0x00000040
121#define TXX9_SILCR_SCS_SCLK_BG 0x00000060
122#define TXX9_SILCR_UEPS 0x00000010
123#define TXX9_SILCR_UPEN 0x00000008
124#define TXX9_SILCR_USBL_MASK 0x00000004
125#define TXX9_SILCR_USBL_1BIT 0x00000000
126#define TXX9_SILCR_USBL_2BIT 0x00000004
127#define TXX9_SILCR_UMODE_MASK 0x00000003
128#define TXX9_SILCR_UMODE_8BIT 0x00000000
129#define TXX9_SILCR_UMODE_7BIT 0x00000001
130
131/* SIDICR : DMA/Int. Control */
132#define TXX9_SIDICR_TDE 0x00008000
133#define TXX9_SIDICR_RDE 0x00004000
134#define TXX9_SIDICR_TIE 0x00002000
135#define TXX9_SIDICR_RIE 0x00001000
136#define TXX9_SIDICR_SPIE 0x00000800
137#define TXX9_SIDICR_CTSAC 0x00000600
138#define TXX9_SIDICR_STIE_MASK 0x0000003f
139#define TXX9_SIDICR_STIE_OERS 0x00000020
140#define TXX9_SIDICR_STIE_CTSS 0x00000010
141#define TXX9_SIDICR_STIE_RBRKD 0x00000008
142#define TXX9_SIDICR_STIE_TRDY 0x00000004
143#define TXX9_SIDICR_STIE_TXALS 0x00000002
144#define TXX9_SIDICR_STIE_UBRKD 0x00000001
145
146/* SIDISR : DMA/Int. Status */
147#define TXX9_SIDISR_UBRK 0x00008000
148#define TXX9_SIDISR_UVALID 0x00004000
149#define TXX9_SIDISR_UFER 0x00002000
150#define TXX9_SIDISR_UPER 0x00001000
151#define TXX9_SIDISR_UOER 0x00000800
152#define TXX9_SIDISR_ERI 0x00000400
153#define TXX9_SIDISR_TOUT 0x00000200
154#define TXX9_SIDISR_TDIS 0x00000100
155#define TXX9_SIDISR_RDIS 0x00000080
156#define TXX9_SIDISR_STIS 0x00000040
157#define TXX9_SIDISR_RFDN_MASK 0x0000001f
158
159/* SICISR : Change Int. Status */
160#define TXX9_SICISR_OERS 0x00000020
161#define TXX9_SICISR_CTSS 0x00000010
162#define TXX9_SICISR_RBRKD 0x00000008
163#define TXX9_SICISR_TRDY 0x00000004
164#define TXX9_SICISR_TXALS 0x00000002
165#define TXX9_SICISR_UBRKD 0x00000001
166
167/* SIFCR : FIFO Control */
168#define TXX9_SIFCR_SWRST 0x00008000
169#define TXX9_SIFCR_RDIL_MASK 0x00000180
170#define TXX9_SIFCR_RDIL_1 0x00000000
171#define TXX9_SIFCR_RDIL_4 0x00000080
172#define TXX9_SIFCR_RDIL_8 0x00000100
173#define TXX9_SIFCR_RDIL_12 0x00000180
174#define TXX9_SIFCR_RDIL_MAX 0x00000180
175#define TXX9_SIFCR_TDIL_MASK 0x00000018
176#define TXX9_SIFCR_TDIL_MASK 0x00000018
177#define TXX9_SIFCR_TDIL_1 0x00000000
178#define TXX9_SIFCR_TDIL_4 0x00000001
179#define TXX9_SIFCR_TDIL_8 0x00000010
180#define TXX9_SIFCR_TDIL_MAX 0x00000010
181#define TXX9_SIFCR_TFRST 0x00000004
182#define TXX9_SIFCR_RFRST 0x00000002
183#define TXX9_SIFCR_FRSTE 0x00000001
184#define TXX9_SIO_TX_FIFO 8
185#define TXX9_SIO_RX_FIFO 16
186
187/* SIFLCR : Flow Control */
188#define TXX9_SIFLCR_RCS 0x00001000
189#define TXX9_SIFLCR_TES 0x00000800
190#define TXX9_SIFLCR_RTSSC 0x00000200
191#define TXX9_SIFLCR_RSDE 0x00000100
192#define TXX9_SIFLCR_TSDE 0x00000080
193#define TXX9_SIFLCR_RTSTL_MASK 0x0000001e
194#define TXX9_SIFLCR_RTSTL_MAX 0x0000001e
195#define TXX9_SIFLCR_TBRK 0x00000001
196
197/* SIBGR : Baudrate Control */
198#define TXX9_SIBGR_BCLK_MASK 0x00000300
199#define TXX9_SIBGR_BCLK_T0 0x00000000
200#define TXX9_SIBGR_BCLK_T2 0x00000100
201#define TXX9_SIBGR_BCLK_T4 0x00000200
202#define TXX9_SIBGR_BCLK_T6 0x00000300
203#define TXX9_SIBGR_BRD_MASK 0x000000ff
204
205static inline unsigned int sio_in(struct uart_txx9_port *up, int offset)
206{
207 switch (up->port.iotype) {
208 default:
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800209 return __raw_readl(up->port.membase + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 case UPIO_PORT:
211 return inl(up->port.iobase + offset);
212 }
213}
214
215static inline void
216sio_out(struct uart_txx9_port *up, int offset, int value)
217{
218 switch (up->port.iotype) {
219 default:
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800220 __raw_writel(value, up->port.membase + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
222 case UPIO_PORT:
223 outl(value, up->port.iobase + offset);
224 break;
225 }
226}
227
228static inline void
229sio_mask(struct uart_txx9_port *up, int offset, unsigned int value)
230{
231 sio_out(up, offset, sio_in(up, offset) & ~value);
232}
233static inline void
234sio_set(struct uart_txx9_port *up, int offset, unsigned int value)
235{
236 sio_out(up, offset, sio_in(up, offset) | value);
237}
238
239static inline void
240sio_quot_set(struct uart_txx9_port *up, int quot)
241{
242 quot >>= 1;
243 if (quot < 256)
244 sio_out(up, TXX9_SIBGR, quot | TXX9_SIBGR_BCLK_T0);
245 else if (quot < (256 << 2))
246 sio_out(up, TXX9_SIBGR, (quot >> 2) | TXX9_SIBGR_BCLK_T2);
247 else if (quot < (256 << 4))
248 sio_out(up, TXX9_SIBGR, (quot >> 4) | TXX9_SIBGR_BCLK_T4);
249 else if (quot < (256 << 6))
250 sio_out(up, TXX9_SIBGR, (quot >> 6) | TXX9_SIBGR_BCLK_T6);
251 else
252 sio_out(up, TXX9_SIBGR, 0xff | TXX9_SIBGR_BCLK_T6);
253}
254
Russell Kingb129a8c2005-08-31 10:12:14 +0100255static void serial_txx9_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sio_mask(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Russell Kingb129a8c2005-08-31 10:12:14 +0100261static void serial_txx9_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static void serial_txx9_stop_rx(struct uart_port *port)
268{
269 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 up->port.read_status_mask &= ~TXX9_SIDISR_RDIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273static void serial_txx9_enable_ms(struct uart_port *port)
274{
275 /* TXX9-SIO can not control DTR... */
276}
277
Atsushi Nemoto09707692007-02-22 02:17:28 +0900278static void serial_txx9_initialize(struct uart_port *port)
279{
280 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
281 unsigned int tmout = 10000;
282
283 sio_out(up, TXX9_SIFCR, TXX9_SIFCR_SWRST);
284 /* TX4925 BUG WORKAROUND. Accessing SIOC register
285 * immediately after soft reset causes bus error. */
286 mmiowb();
287 udelay(1);
288 while ((sio_in(up, TXX9_SIFCR) & TXX9_SIFCR_SWRST) && --tmout)
289 udelay(1);
290 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
291 sio_set(up, TXX9_SIFCR,
292 TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1);
293 /* initial settings */
294 sio_out(up, TXX9_SILCR,
295 TXX9_SILCR_UMODE_8BIT | TXX9_SILCR_USBL_1BIT |
296 ((up->port.flags & UPF_TXX9_USE_SCLK) ?
297 TXX9_SILCR_SCS_SCLK_BG : TXX9_SILCR_SCS_IMCLK_BG));
298 sio_quot_set(up, uart_get_divisor(port, 9600));
299 sio_out(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSTL_MAX /* 15 */);
300 sio_out(up, TXX9_SIDICR, 0);
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static inline void
David Howells7d12e782006-10-05 14:55:46 +0100304receive_chars(struct uart_txx9_port *up, unsigned int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 struct tty_struct *tty = up->port.info->tty;
307 unsigned char ch;
308 unsigned int disr = *status;
309 int max_count = 256;
310 char flag;
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800311 unsigned int next_ignore_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ch = sio_in(up, TXX9_SIRFIFO);
315 flag = TTY_NORMAL;
316 up->port.icount.rx++;
317
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800318 /* mask out RFDN_MASK bit added by previous overrun */
319 next_ignore_status_mask =
320 up->port.ignore_status_mask & ~TXX9_SIDISR_RFDN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (unlikely(disr & (TXX9_SIDISR_UBRK | TXX9_SIDISR_UPER |
322 TXX9_SIDISR_UFER | TXX9_SIDISR_UOER))) {
323 /*
324 * For statistics only
325 */
326 if (disr & TXX9_SIDISR_UBRK) {
327 disr &= ~(TXX9_SIDISR_UFER | TXX9_SIDISR_UPER);
328 up->port.icount.brk++;
329 /*
330 * We do the SysRQ and SAK checking
331 * here because otherwise the break
332 * may get masked by ignore_status_mask
333 * or read_status_mask.
334 */
335 if (uart_handle_break(&up->port))
336 goto ignore_char;
337 } else if (disr & TXX9_SIDISR_UPER)
338 up->port.icount.parity++;
339 else if (disr & TXX9_SIDISR_UFER)
340 up->port.icount.frame++;
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800341 if (disr & TXX9_SIDISR_UOER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 up->port.icount.overrun++;
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800343 /*
344 * The receiver read buffer still hold
345 * a char which caused overrun.
346 * Ignore next char by adding RFDN_MASK
347 * to ignore_status_mask temporarily.
348 */
349 next_ignore_status_mask |=
350 TXX9_SIDISR_RFDN_MASK;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /*
354 * Mask off conditions which should be ingored.
355 */
356 disr &= up->port.read_status_mask;
357
358 if (disr & TXX9_SIDISR_UBRK) {
359 flag = TTY_BREAK;
360 } else if (disr & TXX9_SIDISR_UPER)
361 flag = TTY_PARITY;
362 else if (disr & TXX9_SIDISR_UFER)
363 flag = TTY_FRAME;
364 }
David Howells7d12e782006-10-05 14:55:46 +0100365 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +0100367
368 uart_insert_char(&up->port, disr, TXX9_SIDISR_UOER, ch, flag);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ignore_char:
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800371 up->port.ignore_status_mask = next_ignore_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 disr = sio_in(up, TXX9_SIDISR);
373 } while (!(disr & TXX9_SIDISR_UVALID) && (max_count-- > 0));
Ralf Baechlef5ee56c2005-09-09 13:01:32 -0700374 spin_unlock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 tty_flip_buffer_push(tty);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -0700376 spin_lock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *status = disr;
378}
379
380static inline void transmit_chars(struct uart_txx9_port *up)
381{
382 struct circ_buf *xmit = &up->port.info->xmit;
383 int count;
384
385 if (up->port.x_char) {
386 sio_out(up, TXX9_SITFIFO, up->port.x_char);
387 up->port.icount.tx++;
388 up->port.x_char = 0;
389 return;
390 }
391 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100392 serial_txx9_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return;
394 }
395
396 count = TXX9_SIO_TX_FIFO;
397 do {
398 sio_out(up, TXX9_SITFIFO, xmit->buf[xmit->tail]);
399 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
400 up->port.icount.tx++;
401 if (uart_circ_empty(xmit))
402 break;
403 } while (--count > 0);
404
405 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
406 uart_write_wakeup(&up->port);
407
408 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100409 serial_txx9_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
David Howells7d12e782006-10-05 14:55:46 +0100412static irqreturn_t serial_txx9_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int pass_counter = 0;
415 struct uart_txx9_port *up = dev_id;
416 unsigned int status;
417
418 while (1) {
419 spin_lock(&up->port.lock);
420 status = sio_in(up, TXX9_SIDISR);
421 if (!(sio_in(up, TXX9_SIDICR) & TXX9_SIDICR_TIE))
422 status &= ~TXX9_SIDISR_TDIS;
423 if (!(status & (TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
424 TXX9_SIDISR_TOUT))) {
425 spin_unlock(&up->port.lock);
426 break;
427 }
428
429 if (status & TXX9_SIDISR_RDIS)
David Howells7d12e782006-10-05 14:55:46 +0100430 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (status & TXX9_SIDISR_TDIS)
432 transmit_chars(up);
433 /* Clear TX/RX Int. Status */
434 sio_mask(up, TXX9_SIDISR,
435 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
436 TXX9_SIDISR_TOUT);
437 spin_unlock(&up->port.lock);
438
439 if (pass_counter++ > PASS_LIMIT)
440 break;
441 }
442
443 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
444}
445
446static unsigned int serial_txx9_tx_empty(struct uart_port *port)
447{
448 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
449 unsigned long flags;
450 unsigned int ret;
451
452 spin_lock_irqsave(&up->port.lock, flags);
453 ret = (sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS) ? TIOCSER_TEMT : 0;
454 spin_unlock_irqrestore(&up->port.lock, flags);
455
456 return ret;
457}
458
459static unsigned int serial_txx9_get_mctrl(struct uart_port *port)
460{
461 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 unsigned int ret;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ret = ((sio_in(up, TXX9_SIFLCR) & TXX9_SIFLCR_RTSSC) ? 0 : TIOCM_RTS)
465 | ((sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS) ? 0 : TIOCM_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return ret;
468}
469
470static void serial_txx9_set_mctrl(struct uart_port *port, unsigned int mctrl)
471{
472 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (mctrl & TIOCM_RTS)
475 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
476 else
477 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480static void serial_txx9_break_ctl(struct uart_port *port, int break_state)
481{
482 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
483 unsigned long flags;
484
485 spin_lock_irqsave(&up->port.lock, flags);
486 if (break_state == -1)
487 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
488 else
489 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
490 spin_unlock_irqrestore(&up->port.lock, flags);
491}
492
493static int serial_txx9_startup(struct uart_port *port)
494{
495 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
496 unsigned long flags;
497 int retval;
498
499 /*
500 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800501 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
503 sio_set(up, TXX9_SIFCR,
504 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
505 /* clear reset */
506 sio_mask(up, TXX9_SIFCR,
507 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
508 sio_out(up, TXX9_SIDICR, 0);
509
510 /*
511 * Clear the interrupt registers.
512 */
513 sio_out(up, TXX9_SIDISR, 0);
514
515 retval = request_irq(up->port.irq, serial_txx9_interrupt,
Thomas Gleixner40663cc2006-07-01 19:29:43 -0700516 IRQF_SHARED, "serial_txx9", up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (retval)
518 return retval;
519
520 /*
521 * Now, initialize the UART
522 */
523 spin_lock_irqsave(&up->port.lock, flags);
524 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
525 spin_unlock_irqrestore(&up->port.lock, flags);
526
527 /* Enable RX/TX */
528 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
529
530 /*
531 * Finally, enable interrupts.
532 */
533 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_RIE);
534
535 return 0;
536}
537
538static void serial_txx9_shutdown(struct uart_port *port)
539{
540 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
541 unsigned long flags;
542
543 /*
544 * Disable interrupts from this port
545 */
546 sio_out(up, TXX9_SIDICR, 0); /* disable all intrs */
547
548 spin_lock_irqsave(&up->port.lock, flags);
549 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
550 spin_unlock_irqrestore(&up->port.lock, flags);
551
552 /*
553 * Disable break condition
554 */
555 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
556
557#ifdef CONFIG_SERIAL_TXX9_CONSOLE
558 if (up->port.cons && up->port.line == up->port.cons->index) {
559 free_irq(up->port.irq, up);
560 return;
561 }
562#endif
563 /* reset FIFOs */
564 sio_set(up, TXX9_SIFCR,
565 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
566 /* clear reset */
567 sio_mask(up, TXX9_SIFCR,
568 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
569
570 /* Disable RX/TX */
571 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
572
573 free_irq(up->port.irq, up);
574}
575
576static void
Alan Cox606d0992006-12-08 02:38:45 -0800577serial_txx9_set_termios(struct uart_port *port, struct ktermios *termios,
578 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
581 unsigned int cval, fcr = 0;
582 unsigned long flags;
583 unsigned int baud, quot;
584
585 cval = sio_in(up, TXX9_SILCR);
586 /* byte size and parity */
587 cval &= ~TXX9_SILCR_UMODE_MASK;
588 switch (termios->c_cflag & CSIZE) {
589 case CS7:
590 cval |= TXX9_SILCR_UMODE_7BIT;
591 break;
592 default:
593 case CS5: /* not supported */
594 case CS6: /* not supported */
595 case CS8:
596 cval |= TXX9_SILCR_UMODE_8BIT;
597 break;
598 }
599
600 cval &= ~TXX9_SILCR_USBL_MASK;
601 if (termios->c_cflag & CSTOPB)
602 cval |= TXX9_SILCR_USBL_2BIT;
603 else
604 cval |= TXX9_SILCR_USBL_1BIT;
605 cval &= ~(TXX9_SILCR_UPEN | TXX9_SILCR_UEPS);
606 if (termios->c_cflag & PARENB)
607 cval |= TXX9_SILCR_UPEN;
608 if (!(termios->c_cflag & PARODD))
609 cval |= TXX9_SILCR_UEPS;
610
611 /*
612 * Ask the core to calculate the divisor for us.
613 */
614 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16/2);
615 quot = uart_get_divisor(port, baud);
616
617 /* Set up FIFOs */
618 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
619 fcr = TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1;
620
621 /*
622 * Ok, we're now changing the port state. Do it with
623 * interrupts disabled.
624 */
625 spin_lock_irqsave(&up->port.lock, flags);
626
627 /*
628 * Update the per-port timeout.
629 */
630 uart_update_timeout(port, termios->c_cflag, baud);
631
632 up->port.read_status_mask = TXX9_SIDISR_UOER |
633 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS;
634 if (termios->c_iflag & INPCK)
635 up->port.read_status_mask |= TXX9_SIDISR_UFER | TXX9_SIDISR_UPER;
636 if (termios->c_iflag & (BRKINT | PARMRK))
637 up->port.read_status_mask |= TXX9_SIDISR_UBRK;
638
639 /*
640 * Characteres to ignore
641 */
642 up->port.ignore_status_mask = 0;
643 if (termios->c_iflag & IGNPAR)
644 up->port.ignore_status_mask |= TXX9_SIDISR_UPER | TXX9_SIDISR_UFER;
645 if (termios->c_iflag & IGNBRK) {
646 up->port.ignore_status_mask |= TXX9_SIDISR_UBRK;
647 /*
648 * If we're ignoring parity and break indicators,
649 * ignore overruns too (for real raw support).
650 */
651 if (termios->c_iflag & IGNPAR)
652 up->port.ignore_status_mask |= TXX9_SIDISR_UOER;
653 }
654
655 /*
656 * ignore all characters if CREAD is not set
657 */
658 if ((termios->c_cflag & CREAD) == 0)
659 up->port.ignore_status_mask |= TXX9_SIDISR_RDIS;
660
661 /* CTS flow control flag */
662 if ((termios->c_cflag & CRTSCTS) &&
663 (up->port.flags & UPF_TXX9_HAVE_CTS_LINE)) {
664 sio_set(up, TXX9_SIFLCR,
665 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
666 } else {
667 sio_mask(up, TXX9_SIFLCR,
668 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
669 }
670
671 sio_out(up, TXX9_SILCR, cval);
672 sio_quot_set(up, quot);
673 sio_out(up, TXX9_SIFCR, fcr);
674
675 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
676 spin_unlock_irqrestore(&up->port.lock, flags);
677}
678
679static void
680serial_txx9_pm(struct uart_port *port, unsigned int state,
681 unsigned int oldstate)
682{
Atsushi Nemoto09707692007-02-22 02:17:28 +0900683 if (state == 0)
684 serial_txx9_initialize(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static int serial_txx9_request_resource(struct uart_txx9_port *up)
688{
689 unsigned int size = TXX9_REGION_SIZE;
690 int ret = 0;
691
692 switch (up->port.iotype) {
693 default:
694 if (!up->port.mapbase)
695 break;
696
697 if (!request_mem_region(up->port.mapbase, size, "serial_txx9")) {
698 ret = -EBUSY;
699 break;
700 }
701
702 if (up->port.flags & UPF_IOREMAP) {
703 up->port.membase = ioremap(up->port.mapbase, size);
704 if (!up->port.membase) {
705 release_mem_region(up->port.mapbase, size);
706 ret = -ENOMEM;
707 }
708 }
709 break;
710
711 case UPIO_PORT:
712 if (!request_region(up->port.iobase, size, "serial_txx9"))
713 ret = -EBUSY;
714 break;
715 }
716 return ret;
717}
718
719static void serial_txx9_release_resource(struct uart_txx9_port *up)
720{
721 unsigned int size = TXX9_REGION_SIZE;
722
723 switch (up->port.iotype) {
724 default:
725 if (!up->port.mapbase)
726 break;
727
728 if (up->port.flags & UPF_IOREMAP) {
729 iounmap(up->port.membase);
730 up->port.membase = NULL;
731 }
732
733 release_mem_region(up->port.mapbase, size);
734 break;
735
736 case UPIO_PORT:
737 release_region(up->port.iobase, size);
738 break;
739 }
740}
741
742static void serial_txx9_release_port(struct uart_port *port)
743{
744 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
745 serial_txx9_release_resource(up);
746}
747
748static int serial_txx9_request_port(struct uart_port *port)
749{
750 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
751 return serial_txx9_request_resource(up);
752}
753
754static void serial_txx9_config_port(struct uart_port *port, int uflags)
755{
756 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int ret;
758
759 /*
760 * Find the region that we can probe for. This in turn
761 * tells us whether we can probe for the type of port.
762 */
763 ret = serial_txx9_request_resource(up);
764 if (ret < 0)
765 return;
766 port->type = PORT_TXX9;
767 up->port.fifosize = TXX9_SIO_TX_FIFO;
768
769#ifdef CONFIG_SERIAL_TXX9_CONSOLE
770 if (up->port.line == up->port.cons->index)
771 return;
772#endif
Atsushi Nemoto09707692007-02-22 02:17:28 +0900773 serial_txx9_initialize(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776static int
777serial_txx9_verify_port(struct uart_port *port, struct serial_struct *ser)
778{
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800779 unsigned long new_port = ser->port;
780 if (HIGH_BITS_OFFSET)
781 new_port += (unsigned long)ser->port_high << HIGH_BITS_OFFSET;
782 if (ser->type != port->type ||
783 ser->irq != port->irq ||
784 ser->io_type != port->iotype ||
785 new_port != port->iobase ||
786 (unsigned long)ser->iomem_base != port->mapbase)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EINVAL;
788 return 0;
789}
790
791static const char *
792serial_txx9_type(struct uart_port *port)
793{
794 return "txx9";
795}
796
797static struct uart_ops serial_txx9_pops = {
798 .tx_empty = serial_txx9_tx_empty,
799 .set_mctrl = serial_txx9_set_mctrl,
800 .get_mctrl = serial_txx9_get_mctrl,
801 .stop_tx = serial_txx9_stop_tx,
802 .start_tx = serial_txx9_start_tx,
803 .stop_rx = serial_txx9_stop_rx,
804 .enable_ms = serial_txx9_enable_ms,
805 .break_ctl = serial_txx9_break_ctl,
806 .startup = serial_txx9_startup,
807 .shutdown = serial_txx9_shutdown,
808 .set_termios = serial_txx9_set_termios,
809 .pm = serial_txx9_pm,
810 .type = serial_txx9_type,
811 .release_port = serial_txx9_release_port,
812 .request_port = serial_txx9_request_port,
813 .config_port = serial_txx9_config_port,
814 .verify_port = serial_txx9_verify_port,
815};
816
817static struct uart_txx9_port serial_txx9_ports[UART_NR];
818
Atsushi Nemoto09707692007-02-22 02:17:28 +0900819static void __init serial_txx9_register_ports(struct uart_driver *drv,
820 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 int i;
823
824 for (i = 0; i < UART_NR; i++) {
825 struct uart_txx9_port *up = &serial_txx9_ports[i];
826
827 up->port.line = i;
828 up->port.ops = &serial_txx9_pops;
Atsushi Nemoto09707692007-02-22 02:17:28 +0900829 up->port.dev = dev;
Atsushi Nemoto83485f82006-03-22 00:07:45 -0800830 if (up->port.iobase || up->port.mapbase)
831 uart_add_one_port(drv, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833}
834
835#ifdef CONFIG_SERIAL_TXX9_CONSOLE
836
837/*
838 * Wait for transmitter & holding register to empty
839 */
840static inline void wait_for_xmitr(struct uart_txx9_port *up)
841{
842 unsigned int tmout = 10000;
843
844 /* Wait up to 10ms for the character(s) to be sent. */
845 while (--tmout &&
846 !(sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS))
847 udelay(1);
848
849 /* Wait up to 1s for flow control if necessary */
850 if (up->port.flags & UPF_CONS_FLOW) {
851 tmout = 1000000;
852 while (--tmout &&
853 (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS))
854 udelay(1);
855 }
856}
857
Russell Kingd3587882006-03-20 20:00:09 +0000858static void serial_txx9_console_putchar(struct uart_port *port, int ch)
859{
860 struct uart_txx9_port *up = (struct uart_txx9_port *)port;
861
862 wait_for_xmitr(up);
863 sio_out(up, TXX9_SITFIFO, ch);
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * Print a string to the serial port trying not to disturb
868 * any possible real use of the port...
869 *
870 * The console_lock must be held when we get here.
871 */
872static void
873serial_txx9_console_write(struct console *co, const char *s, unsigned int count)
874{
875 struct uart_txx9_port *up = &serial_txx9_ports[co->index];
876 unsigned int ier, flcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /*
879 * First save the UER then disable the interrupts
880 */
881 ier = sio_in(up, TXX9_SIDICR);
882 sio_out(up, TXX9_SIDICR, 0);
883 /*
884 * Disable flow-control if enabled (and unnecessary)
885 */
886 flcr = sio_in(up, TXX9_SIFLCR);
887 if (!(up->port.flags & UPF_CONS_FLOW) && (flcr & TXX9_SIFLCR_TES))
888 sio_out(up, TXX9_SIFLCR, flcr & ~TXX9_SIFLCR_TES);
889
Russell Kingd3587882006-03-20 20:00:09 +0000890 uart_console_write(&up->port, s, count, serial_txx9_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /*
893 * Finally, wait for transmitter to become empty
894 * and restore the IER
895 */
896 wait_for_xmitr(up);
897 sio_out(up, TXX9_SIFLCR, flcr);
898 sio_out(up, TXX9_SIDICR, ier);
899}
900
Atsushi Nemoto09707692007-02-22 02:17:28 +0900901static int __init serial_txx9_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct uart_port *port;
904 struct uart_txx9_port *up;
905 int baud = 9600;
906 int bits = 8;
907 int parity = 'n';
908 int flow = 'n';
909
910 /*
911 * Check whether an invalid uart number has been specified, and
912 * if so, search for the first available port that does have
913 * console support.
914 */
915 if (co->index >= UART_NR)
916 co->index = 0;
917 up = &serial_txx9_ports[co->index];
918 port = &up->port;
919 if (!port->ops)
920 return -ENODEV;
921
Atsushi Nemoto09707692007-02-22 02:17:28 +0900922 serial_txx9_initialize(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (options)
925 uart_parse_options(options, &baud, &parity, &bits, &flow);
926
927 return uart_set_options(port, co, baud, parity, bits, flow);
928}
929
930static struct uart_driver serial_txx9_reg;
931static struct console serial_txx9_console = {
932 .name = TXX9_TTY_NAME,
933 .write = serial_txx9_console_write,
934 .device = uart_console_device,
935 .setup = serial_txx9_console_setup,
936 .flags = CON_PRINTBUFFER,
937 .index = -1,
938 .data = &serial_txx9_reg,
939};
940
941static int __init serial_txx9_console_init(void)
942{
943 register_console(&serial_txx9_console);
944 return 0;
945}
946console_initcall(serial_txx9_console_init);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948#define SERIAL_TXX9_CONSOLE &serial_txx9_console
949#else
950#define SERIAL_TXX9_CONSOLE NULL
951#endif
952
953static struct uart_driver serial_txx9_reg = {
954 .owner = THIS_MODULE,
955 .driver_name = "serial_txx9",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 .dev_name = TXX9_TTY_NAME,
957 .major = TXX9_TTY_MAJOR,
958 .minor = TXX9_TTY_MINOR_START,
959 .nr = UART_NR,
960 .cons = SERIAL_TXX9_CONSOLE,
961};
962
963int __init early_serial_txx9_setup(struct uart_port *port)
964{
965 if (port->line >= ARRAY_SIZE(serial_txx9_ports))
966 return -ENODEV;
967
968 serial_txx9_ports[port->line].port = *port;
969 serial_txx9_ports[port->line].port.ops = &serial_txx9_pops;
970 serial_txx9_ports[port->line].port.flags |= UPF_BOOT_AUTOCONF;
971 return 0;
972}
973
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000974static DEFINE_MUTEX(serial_txx9_mutex);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -0700975
976/**
977 * serial_txx9_register_port - register a serial port
978 * @port: serial port template
979 *
980 * Configure the serial port specified by the request.
981 *
982 * The port is then probed and if necessary the IRQ is autodetected
983 * If this fails an error is returned.
984 *
985 * On success the port is ready to use and the line number is returned.
986 */
987static int __devinit serial_txx9_register_port(struct uart_port *port)
988{
989 int i;
990 struct uart_txx9_port *uart;
991 int ret = -ENOSPC;
992
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000993 mutex_lock(&serial_txx9_mutex);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -0700994 for (i = 0; i < UART_NR; i++) {
995 uart = &serial_txx9_ports[i];
Atsushi Nemoto09707692007-02-22 02:17:28 +0900996 if (uart_match_port(&uart->port, port)) {
997 uart_remove_one_port(&serial_txx9_reg, &uart->port);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -0700998 break;
Atsushi Nemoto09707692007-02-22 02:17:28 +0900999 }
1000 }
1001 if (i == UART_NR) {
1002 /* Find unused port */
1003 for (i = 0; i < UART_NR; i++) {
1004 uart = &serial_txx9_ports[i];
1005 if (!(uart->port.iobase || uart->port.mapbase))
1006 break;
1007 }
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001008 }
1009 if (i < UART_NR) {
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001010 uart->port.iobase = port->iobase;
1011 uart->port.membase = port->membase;
1012 uart->port.irq = port->irq;
1013 uart->port.uartclk = port->uartclk;
1014 uart->port.iotype = port->iotype;
1015 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
1016 uart->port.mapbase = port->mapbase;
1017 if (port->dev)
1018 uart->port.dev = port->dev;
1019 ret = uart_add_one_port(&serial_txx9_reg, &uart->port);
1020 if (ret == 0)
1021 ret = uart->port.line;
1022 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001023 mutex_unlock(&serial_txx9_mutex);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001024 return ret;
1025}
1026
1027/**
1028 * serial_txx9_unregister_port - remove a txx9 serial port at runtime
1029 * @line: serial line number
1030 *
1031 * Remove one serial port. This may not be called from interrupt
1032 * context. We hand the port back to the our control.
1033 */
1034static void __devexit serial_txx9_unregister_port(int line)
1035{
1036 struct uart_txx9_port *uart = &serial_txx9_ports[line];
1037
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001038 mutex_lock(&serial_txx9_mutex);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001039 uart_remove_one_port(&serial_txx9_reg, &uart->port);
1040 uart->port.flags = 0;
1041 uart->port.type = PORT_UNKNOWN;
1042 uart->port.iobase = 0;
1043 uart->port.mapbase = 0;
Atsushi Nemoto83485f82006-03-22 00:07:45 -08001044 uart->port.membase = NULL;
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001045 uart->port.dev = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001046 mutex_unlock(&serial_txx9_mutex);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*
Atsushi Nemoto09707692007-02-22 02:17:28 +09001050 * Register a set of serial devices attached to a platform device.
1051 */
1052static int __devinit serial_txx9_probe(struct platform_device *dev)
1053{
1054 struct uart_port *p = dev->dev.platform_data;
1055 struct uart_port port;
1056 int ret, i;
1057
1058 memset(&port, 0, sizeof(struct uart_port));
1059 for (i = 0; p && p->uartclk != 0; p++, i++) {
1060 port.iobase = p->iobase;
1061 port.membase = p->membase;
1062 port.irq = p->irq;
1063 port.uartclk = p->uartclk;
1064 port.iotype = p->iotype;
1065 port.flags = p->flags;
1066 port.mapbase = p->mapbase;
1067 port.dev = &dev->dev;
1068 ret = serial_txx9_register_port(&port);
1069 if (ret < 0) {
1070 dev_err(&dev->dev, "unable to register port at index %d "
1071 "(IO%x MEM%lx IRQ%d): %d\n", i,
1072 p->iobase, p->mapbase, p->irq, ret);
1073 }
1074 }
1075 return 0;
1076}
1077
1078/*
1079 * Remove serial ports registered against a platform device.
1080 */
1081static int __devexit serial_txx9_remove(struct platform_device *dev)
1082{
1083 int i;
1084
1085 for (i = 0; i < UART_NR; i++) {
1086 struct uart_txx9_port *up = &serial_txx9_ports[i];
1087
1088 if (up->port.dev == &dev->dev)
1089 serial_txx9_unregister_port(i);
1090 }
1091 return 0;
1092}
1093
1094#ifdef CONFIG_PM
1095static int serial_txx9_suspend(struct platform_device *dev, pm_message_t state)
1096{
1097 int i;
1098
1099 for (i = 0; i < UART_NR; i++) {
1100 struct uart_txx9_port *up = &serial_txx9_ports[i];
1101
1102 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1103 uart_suspend_port(&serial_txx9_reg, &up->port);
1104 }
1105
1106 return 0;
1107}
1108
1109static int serial_txx9_resume(struct platform_device *dev)
1110{
1111 int i;
1112
1113 for (i = 0; i < UART_NR; i++) {
1114 struct uart_txx9_port *up = &serial_txx9_ports[i];
1115
1116 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1117 uart_resume_port(&serial_txx9_reg, &up->port);
1118 }
1119
1120 return 0;
1121}
1122#endif
1123
1124static struct platform_driver serial_txx9_plat_driver = {
1125 .probe = serial_txx9_probe,
1126 .remove = __devexit_p(serial_txx9_remove),
1127#ifdef CONFIG_PM
1128 .suspend = serial_txx9_suspend,
1129 .resume = serial_txx9_resume,
1130#endif
1131 .driver = {
1132 .name = "serial_txx9",
1133 .owner = THIS_MODULE,
1134 },
1135};
1136
1137#ifdef ENABLE_SERIAL_TXX9_PCI
1138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * Probe one serial board. Unfortunately, there is no rhyme nor reason
1140 * to the arrangement of serial ports on a PCI card.
1141 */
1142static int __devinit
1143pciserial_txx9_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
1144{
1145 struct uart_port port;
1146 int line;
1147 int rc;
1148
1149 rc = pci_enable_device(dev);
1150 if (rc)
1151 return rc;
1152
1153 memset(&port, 0, sizeof(port));
1154 port.ops = &serial_txx9_pops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 port.flags |= UPF_TXX9_HAVE_CTS_LINE;
1156 port.uartclk = 66670000;
1157 port.irq = dev->irq;
1158 port.iotype = UPIO_PORT;
1159 port.iobase = pci_resource_start(dev, 1);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001160 port.dev = &dev->dev;
1161 line = serial_txx9_register_port(&port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (line < 0) {
1163 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), line);
Atsushi Nemoto09707692007-02-22 02:17:28 +09001164 pci_disable_device(dev);
1165 return line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Atsushi Nemoto09707692007-02-22 02:17:28 +09001167 pci_set_drvdata(dev, &serial_txx9_ports[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 return 0;
1170}
1171
1172static void __devexit pciserial_txx9_remove_one(struct pci_dev *dev)
1173{
Atsushi Nemoto09707692007-02-22 02:17:28 +09001174 struct uart_txx9_port *up = pci_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 pci_set_drvdata(dev, NULL);
1177
Atsushi Nemoto09707692007-02-22 02:17:28 +09001178 if (up) {
1179 serial_txx9_unregister_port(up->port.line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 pci_disable_device(dev);
1181 }
1182}
1183
Atsushi Nemoto138c5d22007-02-10 01:45:05 -08001184#ifdef CONFIG_PM
Pavel Machek0370aff2005-04-16 15:25:35 -07001185static int pciserial_txx9_suspend_one(struct pci_dev *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Atsushi Nemoto09707692007-02-22 02:17:28 +09001187 struct uart_txx9_port *up = pci_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Atsushi Nemoto09707692007-02-22 02:17:28 +09001189 if (up)
1190 uart_suspend_port(&serial_txx9_reg, &up->port);
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001191 pci_save_state(dev);
1192 pci_set_power_state(dev, pci_choose_state(dev, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return 0;
1194}
1195
1196static int pciserial_txx9_resume_one(struct pci_dev *dev)
1197{
Atsushi Nemoto09707692007-02-22 02:17:28 +09001198 struct uart_txx9_port *up = pci_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Ralf Baechlef5ee56c2005-09-09 13:01:32 -07001200 pci_set_power_state(dev, PCI_D0);
1201 pci_restore_state(dev);
Atsushi Nemoto09707692007-02-22 02:17:28 +09001202 if (up)
1203 uart_resume_port(&serial_txx9_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return 0;
1205}
Atsushi Nemoto138c5d22007-02-10 01:45:05 -08001206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Atsushi Nemoto138c5d22007-02-10 01:45:05 -08001208static const struct pci_device_id serial_txx9_pci_tbl[] = {
1209 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC86C001_MISC) },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 { 0, }
1211};
1212
1213static struct pci_driver serial_txx9_pci_driver = {
1214 .name = "serial_txx9",
1215 .probe = pciserial_txx9_init_one,
1216 .remove = __devexit_p(pciserial_txx9_remove_one),
Atsushi Nemoto138c5d22007-02-10 01:45:05 -08001217#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 .suspend = pciserial_txx9_suspend_one,
1219 .resume = pciserial_txx9_resume_one,
Atsushi Nemoto138c5d22007-02-10 01:45:05 -08001220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 .id_table = serial_txx9_pci_tbl,
1222};
1223
1224MODULE_DEVICE_TABLE(pci, serial_txx9_pci_tbl);
1225#endif /* ENABLE_SERIAL_TXX9_PCI */
1226
Atsushi Nemoto09707692007-02-22 02:17:28 +09001227static struct platform_device *serial_txx9_plat_devs;
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static int __init serial_txx9_init(void)
1230{
1231 int ret;
1232
1233 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1234
1235 ret = uart_register_driver(&serial_txx9_reg);
Atsushi Nemoto09707692007-02-22 02:17:28 +09001236 if (ret)
1237 goto out;
1238
1239 serial_txx9_plat_devs = platform_device_alloc("serial_txx9", -1);
1240 if (!serial_txx9_plat_devs) {
1241 ret = -ENOMEM;
1242 goto unreg_uart_drv;
1243 }
1244
1245 ret = platform_device_add(serial_txx9_plat_devs);
1246 if (ret)
1247 goto put_dev;
1248
1249 serial_txx9_register_ports(&serial_txx9_reg,
1250 &serial_txx9_plat_devs->dev);
1251
1252 ret = platform_driver_register(&serial_txx9_plat_driver);
1253 if (ret)
1254 goto del_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256#ifdef ENABLE_SERIAL_TXX9_PCI
Atsushi Nemoto09707692007-02-22 02:17:28 +09001257 ret = pci_register_driver(&serial_txx9_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#endif
Atsushi Nemoto09707692007-02-22 02:17:28 +09001259 if (ret == 0)
1260 goto out;
1261
1262 del_dev:
1263 platform_device_del(serial_txx9_plat_devs);
1264 put_dev:
1265 platform_device_put(serial_txx9_plat_devs);
1266 unreg_uart_drv:
1267 uart_unregister_driver(&serial_txx9_reg);
1268 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return ret;
1270}
1271
1272static void __exit serial_txx9_exit(void)
1273{
1274 int i;
1275
1276#ifdef ENABLE_SERIAL_TXX9_PCI
1277 pci_unregister_driver(&serial_txx9_pci_driver);
1278#endif
Atsushi Nemoto09707692007-02-22 02:17:28 +09001279 platform_driver_unregister(&serial_txx9_plat_driver);
1280 platform_device_unregister(serial_txx9_plat_devs);
Atsushi Nemoto83485f82006-03-22 00:07:45 -08001281 for (i = 0; i < UART_NR; i++) {
1282 struct uart_txx9_port *up = &serial_txx9_ports[i];
1283 if (up->port.iobase || up->port.mapbase)
1284 uart_remove_one_port(&serial_txx9_reg, &up->port);
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 uart_unregister_driver(&serial_txx9_reg);
1288}
1289
1290module_init(serial_txx9_init);
1291module_exit(serial_txx9_exit);
1292
1293MODULE_LICENSE("GPL");
1294MODULE_DESCRIPTION("TX39/49 serial driver");
1295
1296MODULE_ALIAS_CHARDEV_MAJOR(TXX9_TTY_MAJOR);