1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the serial port on the 21285 StrongArm-110 core logic chip.
5 * Based on drivers/char/serial.c
7 #include <linux/module.h>
9 #include <linux/ioport.h>
10 #include <linux/init.h>
11 #include <linux/console.h>
12 #include <linux/device.h>
13 #include <linux/tty_flip.h>
14 #include <linux/serial_core.h>
15 #include <linux/serial.h>
19 #include <asm/mach-types.h>
20 #include <asm/system_info.h>
21 #include <asm/hardware/dec21285.h>
22 #include <mach/hardware.h>
24 #define BAUD_BASE (mem_fclk_21285/64)
26 #define SERIAL_21285_NAME "ttyFB"
27 #define SERIAL_21285_MAJOR 204
28 #define SERIAL_21285_MINOR 4
30 #define RXSTAT_DUMMY_READ 0x80000000
31 #define RXSTAT_FRAME (1 << 0)
32 #define RXSTAT_PARITY (1 << 1)
33 #define RXSTAT_OVERRUN (1 << 2)
34 #define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
36 #define H_UBRLCR_BREAK (1 << 0)
37 #define H_UBRLCR_PARENB (1 << 1)
38 #define H_UBRLCR_PAREVN (1 << 2)
39 #define H_UBRLCR_STOPB (1 << 3)
40 #define H_UBRLCR_FIFO (1 << 4)
42 static const char serial21285_name
[] = "Footbridge UART";
44 #define tx_enabled(port) ((port)->unused[0])
45 #define rx_enabled(port) ((port)->unused[1])
48 * The documented expression for selecting the divisor is:
49 * BAUD_BASE / baud - 1
50 * However, typically BAUD_BASE is not divisible by baud, so
51 * we want to select the divisor that gives us the minimum
52 * error. Therefore, we want:
53 * int(BAUD_BASE / baud - 0.5) ->
54 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
55 * int((BAUD_BASE - (baud >> 1)) / baud)
58 static void serial21285_stop_tx(struct uart_port
*port
)
60 if (tx_enabled(port
)) {
61 disable_irq_nosync(IRQ_CONTX
);
66 static void serial21285_start_tx(struct uart_port
*port
)
68 if (!tx_enabled(port
)) {
69 enable_irq(IRQ_CONTX
);
74 static void serial21285_stop_rx(struct uart_port
*port
)
76 if (rx_enabled(port
)) {
77 disable_irq_nosync(IRQ_CONRX
);
82 static irqreturn_t
serial21285_rx_chars(int irq
, void *dev_id
)
84 struct uart_port
*port
= dev_id
;
85 unsigned int status
, ch
, flag
, rxs
, max_count
= 256;
87 status
= *CSR_UARTFLG
;
88 while (!(status
& 0x10) && max_count
--) {
93 rxs
= *CSR_RXSTAT
| RXSTAT_DUMMY_READ
;
94 if (unlikely(rxs
& RXSTAT_ANYERR
)) {
95 if (rxs
& RXSTAT_PARITY
)
96 port
->icount
.parity
++;
97 else if (rxs
& RXSTAT_FRAME
)
99 if (rxs
& RXSTAT_OVERRUN
)
100 port
->icount
.overrun
++;
102 rxs
&= port
->read_status_mask
;
104 if (rxs
& RXSTAT_PARITY
)
106 else if (rxs
& RXSTAT_FRAME
)
110 uart_insert_char(port
, rxs
, RXSTAT_OVERRUN
, ch
, flag
);
112 status
= *CSR_UARTFLG
;
114 tty_flip_buffer_push(&port
->state
->port
);
119 static irqreturn_t
serial21285_tx_chars(int irq
, void *dev_id
)
121 struct uart_port
*port
= dev_id
;
122 struct circ_buf
*xmit
= &port
->state
->xmit
;
126 *CSR_UARTDR
= port
->x_char
;
131 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
132 serial21285_stop_tx(port
);
137 *CSR_UARTDR
= xmit
->buf
[xmit
->tail
];
138 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
140 if (uart_circ_empty(xmit
))
142 } while (--count
> 0 && !(*CSR_UARTFLG
& 0x20));
144 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
145 uart_write_wakeup(port
);
147 if (uart_circ_empty(xmit
))
148 serial21285_stop_tx(port
);
154 static unsigned int serial21285_tx_empty(struct uart_port
*port
)
156 return (*CSR_UARTFLG
& 8) ? 0 : TIOCSER_TEMT
;
159 /* no modem control lines */
160 static unsigned int serial21285_get_mctrl(struct uart_port
*port
)
162 return TIOCM_CAR
| TIOCM_DSR
| TIOCM_CTS
;
165 static void serial21285_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
169 static void serial21285_break_ctl(struct uart_port
*port
, int break_state
)
174 spin_lock_irqsave(&port
->lock
, flags
);
175 h_lcr
= *CSR_H_UBRLCR
;
177 h_lcr
|= H_UBRLCR_BREAK
;
179 h_lcr
&= ~H_UBRLCR_BREAK
;
180 *CSR_H_UBRLCR
= h_lcr
;
181 spin_unlock_irqrestore(&port
->lock
, flags
);
184 static int serial21285_startup(struct uart_port
*port
)
188 tx_enabled(port
) = 1;
189 rx_enabled(port
) = 1;
191 ret
= request_irq(IRQ_CONRX
, serial21285_rx_chars
, 0,
192 serial21285_name
, port
);
194 ret
= request_irq(IRQ_CONTX
, serial21285_tx_chars
, 0,
195 serial21285_name
, port
);
197 free_irq(IRQ_CONRX
, port
);
203 static void serial21285_shutdown(struct uart_port
*port
)
205 free_irq(IRQ_CONTX
, port
);
206 free_irq(IRQ_CONRX
, port
);
210 serial21285_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
211 struct ktermios
*old
)
214 unsigned int baud
, quot
, h_lcr
, b
;
217 * We don't support modem control lines.
219 termios
->c_cflag
&= ~(HUPCL
| CRTSCTS
| CMSPAR
);
220 termios
->c_cflag
|= CLOCAL
;
223 * We don't support BREAK character recognition.
225 termios
->c_iflag
&= ~(IGNBRK
| BRKINT
);
228 * Ask the core to calculate the divisor for us.
230 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
231 quot
= uart_get_divisor(port
, baud
);
232 b
= port
->uartclk
/ (16 * quot
);
233 tty_termios_encode_baud_rate(termios
, b
, b
);
235 switch (termios
->c_cflag
& CSIZE
) {
250 if (termios
->c_cflag
& CSTOPB
)
251 h_lcr
|= H_UBRLCR_STOPB
;
252 if (termios
->c_cflag
& PARENB
) {
253 h_lcr
|= H_UBRLCR_PARENB
;
254 if (!(termios
->c_cflag
& PARODD
))
255 h_lcr
|= H_UBRLCR_PAREVN
;
259 h_lcr
|= H_UBRLCR_FIFO
;
261 spin_lock_irqsave(&port
->lock
, flags
);
264 * Update the per-port timeout.
266 uart_update_timeout(port
, termios
->c_cflag
, baud
);
269 * Which character status flags are we interested in?
271 port
->read_status_mask
= RXSTAT_OVERRUN
;
272 if (termios
->c_iflag
& INPCK
)
273 port
->read_status_mask
|= RXSTAT_FRAME
| RXSTAT_PARITY
;
276 * Which character status flags should we ignore?
278 port
->ignore_status_mask
= 0;
279 if (termios
->c_iflag
& IGNPAR
)
280 port
->ignore_status_mask
|= RXSTAT_FRAME
| RXSTAT_PARITY
;
281 if (termios
->c_iflag
& IGNBRK
&& termios
->c_iflag
& IGNPAR
)
282 port
->ignore_status_mask
|= RXSTAT_OVERRUN
;
285 * Ignore all characters if CREAD is not set.
287 if ((termios
->c_cflag
& CREAD
) == 0)
288 port
->ignore_status_mask
|= RXSTAT_DUMMY_READ
;
293 *CSR_L_UBRLCR
= quot
& 0xff;
294 *CSR_M_UBRLCR
= (quot
>> 8) & 0x0f;
295 *CSR_H_UBRLCR
= h_lcr
;
298 spin_unlock_irqrestore(&port
->lock
, flags
);
301 static const char *serial21285_type(struct uart_port
*port
)
303 return port
->type
== PORT_21285
? "DC21285" : NULL
;
306 static void serial21285_release_port(struct uart_port
*port
)
308 release_mem_region(port
->mapbase
, 32);
311 static int serial21285_request_port(struct uart_port
*port
)
313 return request_mem_region(port
->mapbase
, 32, serial21285_name
)
314 != NULL
? 0 : -EBUSY
;
317 static void serial21285_config_port(struct uart_port
*port
, int flags
)
319 if (flags
& UART_CONFIG_TYPE
&& serial21285_request_port(port
) == 0)
320 port
->type
= PORT_21285
;
324 * verify the new serial_struct (for TIOCSSERIAL).
326 static int serial21285_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
329 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_21285
)
333 if (ser
->baud_base
!= port
->uartclk
/ 16)
338 static const struct uart_ops serial21285_ops
= {
339 .tx_empty
= serial21285_tx_empty
,
340 .get_mctrl
= serial21285_get_mctrl
,
341 .set_mctrl
= serial21285_set_mctrl
,
342 .stop_tx
= serial21285_stop_tx
,
343 .start_tx
= serial21285_start_tx
,
344 .stop_rx
= serial21285_stop_rx
,
345 .break_ctl
= serial21285_break_ctl
,
346 .startup
= serial21285_startup
,
347 .shutdown
= serial21285_shutdown
,
348 .set_termios
= serial21285_set_termios
,
349 .type
= serial21285_type
,
350 .release_port
= serial21285_release_port
,
351 .request_port
= serial21285_request_port
,
352 .config_port
= serial21285_config_port
,
353 .verify_port
= serial21285_verify_port
,
356 static struct uart_port serial21285_port
= {
357 .mapbase
= 0x42000160,
361 .ops
= &serial21285_ops
,
362 .flags
= UPF_BOOT_AUTOCONF
,
365 static void serial21285_setup_ports(void)
367 serial21285_port
.uartclk
= mem_fclk_21285
/ 4;
370 #ifdef CONFIG_SERIAL_21285_CONSOLE
371 static void serial21285_console_putchar(struct uart_port
*port
, int ch
)
373 while (*CSR_UARTFLG
& 0x20)
379 serial21285_console_write(struct console
*co
, const char *s
,
382 uart_console_write(&serial21285_port
, s
, count
, serial21285_console_putchar
);
386 serial21285_get_options(struct uart_port
*port
, int *baud
,
387 int *parity
, int *bits
)
389 if (*CSR_UARTCON
== 1) {
393 switch (tmp
& 0x60) {
409 if (tmp
& H_UBRLCR_PARENB
) {
411 if (tmp
& H_UBRLCR_PAREVN
)
415 tmp
= *CSR_L_UBRLCR
| (*CSR_M_UBRLCR
<< 8);
417 *baud
= port
->uartclk
/ (16 * (tmp
+ 1));
421 static int __init
serial21285_console_setup(struct console
*co
, char *options
)
423 struct uart_port
*port
= &serial21285_port
;
429 if (machine_is_personal_server())
433 * Check whether an invalid uart number has been specified, and
434 * if so, search for the first available port that does have
438 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
440 serial21285_get_options(port
, &baud
, &parity
, &bits
);
442 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
445 static struct uart_driver serial21285_reg
;
447 static struct console serial21285_console
=
449 .name
= SERIAL_21285_NAME
,
450 .write
= serial21285_console_write
,
451 .device
= uart_console_device
,
452 .setup
= serial21285_console_setup
,
453 .flags
= CON_PRINTBUFFER
,
455 .data
= &serial21285_reg
,
458 static int __init
rs285_console_init(void)
460 serial21285_setup_ports();
461 register_console(&serial21285_console
);
464 console_initcall(rs285_console_init
);
466 #define SERIAL_21285_CONSOLE &serial21285_console
468 #define SERIAL_21285_CONSOLE NULL
471 static struct uart_driver serial21285_reg
= {
472 .owner
= THIS_MODULE
,
473 .driver_name
= "ttyFB",
475 .major
= SERIAL_21285_MAJOR
,
476 .minor
= SERIAL_21285_MINOR
,
478 .cons
= SERIAL_21285_CONSOLE
,
481 static int __init
serial21285_init(void)
485 printk(KERN_INFO
"Serial: 21285 driver\n");
487 serial21285_setup_ports();
489 ret
= uart_register_driver(&serial21285_reg
);
491 uart_add_one_port(&serial21285_reg
, &serial21285_port
);
496 static void __exit
serial21285_exit(void)
498 uart_remove_one_port(&serial21285_reg
, &serial21285_port
);
499 uart_unregister_driver(&serial21285_reg
);
502 module_init(serial21285_init
);
503 module_exit(serial21285_exit
);
505 MODULE_LICENSE("GPL");
506 MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver");
507 MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR
, SERIAL_21285_MINOR
);