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";
45 * We only need 2 bits of data, so instead of creating a whole structure for
46 * this, use bits of the private_data pointer of the uart port structure.
48 #define tx_enabled_bit 0
49 #define rx_enabled_bit 1
51 static bool is_enabled(struct uart_port
*port
, int bit
)
53 unsigned long *private_data
= (unsigned long *)&port
->private_data
;
55 if (test_bit(bit
, private_data
))
60 static void enable(struct uart_port
*port
, int bit
)
62 unsigned long *private_data
= (unsigned long *)&port
->private_data
;
64 set_bit(bit
, private_data
);
67 static void disable(struct uart_port
*port
, int bit
)
69 unsigned long *private_data
= (unsigned long *)&port
->private_data
;
71 clear_bit(bit
, private_data
);
74 #define is_tx_enabled(port) is_enabled(port, tx_enabled_bit)
75 #define tx_enable(port) enable(port, tx_enabled_bit)
76 #define tx_disable(port) disable(port, tx_enabled_bit)
78 #define is_rx_enabled(port) is_enabled(port, rx_enabled_bit)
79 #define rx_enable(port) enable(port, rx_enabled_bit)
80 #define rx_disable(port) disable(port, rx_enabled_bit)
83 * The documented expression for selecting the divisor is:
84 * BAUD_BASE / baud - 1
85 * However, typically BAUD_BASE is not divisible by baud, so
86 * we want to select the divisor that gives us the minimum
87 * error. Therefore, we want:
88 * int(BAUD_BASE / baud - 0.5) ->
89 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
90 * int((BAUD_BASE - (baud >> 1)) / baud)
93 static void serial21285_stop_tx(struct uart_port
*port
)
95 if (is_tx_enabled(port
)) {
96 disable_irq_nosync(IRQ_CONTX
);
101 static void serial21285_start_tx(struct uart_port
*port
)
103 if (!is_tx_enabled(port
)) {
104 enable_irq(IRQ_CONTX
);
109 static void serial21285_stop_rx(struct uart_port
*port
)
111 if (is_rx_enabled(port
)) {
112 disable_irq_nosync(IRQ_CONRX
);
117 static irqreturn_t
serial21285_rx_chars(int irq
, void *dev_id
)
119 struct uart_port
*port
= dev_id
;
120 unsigned int status
, rxs
, max_count
= 256;
123 status
= *CSR_UARTFLG
;
124 while (!(status
& 0x10) && max_count
--) {
129 rxs
= *CSR_RXSTAT
| RXSTAT_DUMMY_READ
;
130 if (unlikely(rxs
& RXSTAT_ANYERR
)) {
131 if (rxs
& RXSTAT_PARITY
)
132 port
->icount
.parity
++;
133 else if (rxs
& RXSTAT_FRAME
)
134 port
->icount
.frame
++;
135 if (rxs
& RXSTAT_OVERRUN
)
136 port
->icount
.overrun
++;
138 rxs
&= port
->read_status_mask
;
140 if (rxs
& RXSTAT_PARITY
)
142 else if (rxs
& RXSTAT_FRAME
)
146 uart_insert_char(port
, rxs
, RXSTAT_OVERRUN
, ch
, flag
);
148 status
= *CSR_UARTFLG
;
150 tty_flip_buffer_push(&port
->state
->port
);
155 static irqreturn_t
serial21285_tx_chars(int irq
, void *dev_id
)
157 struct uart_port
*port
= dev_id
;
160 uart_port_tx_limited(port
, ch
, 256,
161 !(*CSR_UARTFLG
& 0x20),
168 static unsigned int serial21285_tx_empty(struct uart_port
*port
)
170 return (*CSR_UARTFLG
& 8) ? 0 : TIOCSER_TEMT
;
173 /* no modem control lines */
174 static unsigned int serial21285_get_mctrl(struct uart_port
*port
)
176 return TIOCM_CAR
| TIOCM_DSR
| TIOCM_CTS
;
179 static void serial21285_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
183 static void serial21285_break_ctl(struct uart_port
*port
, int break_state
)
188 uart_port_lock_irqsave(port
, &flags
);
189 h_lcr
= *CSR_H_UBRLCR
;
191 h_lcr
|= H_UBRLCR_BREAK
;
193 h_lcr
&= ~H_UBRLCR_BREAK
;
194 *CSR_H_UBRLCR
= h_lcr
;
195 uart_port_unlock_irqrestore(port
, flags
);
198 static int serial21285_startup(struct uart_port
*port
)
205 ret
= request_irq(IRQ_CONRX
, serial21285_rx_chars
, 0,
206 serial21285_name
, port
);
208 ret
= request_irq(IRQ_CONTX
, serial21285_tx_chars
, 0,
209 serial21285_name
, port
);
211 free_irq(IRQ_CONRX
, port
);
217 static void serial21285_shutdown(struct uart_port
*port
)
219 free_irq(IRQ_CONTX
, port
);
220 free_irq(IRQ_CONRX
, port
);
224 serial21285_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
225 const struct ktermios
*old
)
228 unsigned int baud
, quot
, h_lcr
, b
;
231 * We don't support modem control lines.
233 termios
->c_cflag
&= ~(HUPCL
| CRTSCTS
| CMSPAR
);
234 termios
->c_cflag
|= CLOCAL
;
237 * We don't support BREAK character recognition.
239 termios
->c_iflag
&= ~(IGNBRK
| BRKINT
);
242 * Ask the core to calculate the divisor for us.
244 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
245 quot
= uart_get_divisor(port
, baud
);
246 b
= port
->uartclk
/ (16 * quot
);
247 tty_termios_encode_baud_rate(termios
, b
, b
);
249 switch (termios
->c_cflag
& CSIZE
) {
264 if (termios
->c_cflag
& CSTOPB
)
265 h_lcr
|= H_UBRLCR_STOPB
;
266 if (termios
->c_cflag
& PARENB
) {
267 h_lcr
|= H_UBRLCR_PARENB
;
268 if (!(termios
->c_cflag
& PARODD
))
269 h_lcr
|= H_UBRLCR_PAREVN
;
273 h_lcr
|= H_UBRLCR_FIFO
;
275 uart_port_lock_irqsave(port
, &flags
);
278 * Update the per-port timeout.
280 uart_update_timeout(port
, termios
->c_cflag
, baud
);
283 * Which character status flags are we interested in?
285 port
->read_status_mask
= RXSTAT_OVERRUN
;
286 if (termios
->c_iflag
& INPCK
)
287 port
->read_status_mask
|= RXSTAT_FRAME
| RXSTAT_PARITY
;
290 * Which character status flags should we ignore?
292 port
->ignore_status_mask
= 0;
293 if (termios
->c_iflag
& IGNPAR
)
294 port
->ignore_status_mask
|= RXSTAT_FRAME
| RXSTAT_PARITY
;
295 if (termios
->c_iflag
& IGNBRK
&& termios
->c_iflag
& IGNPAR
)
296 port
->ignore_status_mask
|= RXSTAT_OVERRUN
;
299 * Ignore all characters if CREAD is not set.
301 if ((termios
->c_cflag
& CREAD
) == 0)
302 port
->ignore_status_mask
|= RXSTAT_DUMMY_READ
;
307 *CSR_L_UBRLCR
= quot
& 0xff;
308 *CSR_M_UBRLCR
= (quot
>> 8) & 0x0f;
309 *CSR_H_UBRLCR
= h_lcr
;
312 uart_port_unlock_irqrestore(port
, flags
);
315 static const char *serial21285_type(struct uart_port
*port
)
317 return port
->type
== PORT_21285
? "DC21285" : NULL
;
320 static void serial21285_release_port(struct uart_port
*port
)
322 release_mem_region(port
->mapbase
, 32);
325 static int serial21285_request_port(struct uart_port
*port
)
327 return request_mem_region(port
->mapbase
, 32, serial21285_name
)
328 != NULL
? 0 : -EBUSY
;
331 static void serial21285_config_port(struct uart_port
*port
, int flags
)
333 if (flags
& UART_CONFIG_TYPE
&& serial21285_request_port(port
) == 0)
334 port
->type
= PORT_21285
;
338 * verify the new serial_struct (for TIOCSSERIAL).
340 static int serial21285_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
343 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_21285
)
347 if (ser
->baud_base
!= port
->uartclk
/ 16)
352 static const struct uart_ops serial21285_ops
= {
353 .tx_empty
= serial21285_tx_empty
,
354 .get_mctrl
= serial21285_get_mctrl
,
355 .set_mctrl
= serial21285_set_mctrl
,
356 .stop_tx
= serial21285_stop_tx
,
357 .start_tx
= serial21285_start_tx
,
358 .stop_rx
= serial21285_stop_rx
,
359 .break_ctl
= serial21285_break_ctl
,
360 .startup
= serial21285_startup
,
361 .shutdown
= serial21285_shutdown
,
362 .set_termios
= serial21285_set_termios
,
363 .type
= serial21285_type
,
364 .release_port
= serial21285_release_port
,
365 .request_port
= serial21285_request_port
,
366 .config_port
= serial21285_config_port
,
367 .verify_port
= serial21285_verify_port
,
370 static struct uart_port serial21285_port
= {
371 .mapbase
= 0x42000160,
375 .ops
= &serial21285_ops
,
376 .flags
= UPF_BOOT_AUTOCONF
,
379 static void serial21285_setup_ports(void)
381 serial21285_port
.uartclk
= mem_fclk_21285
/ 4;
384 #ifdef CONFIG_SERIAL_21285_CONSOLE
385 static void serial21285_console_putchar(struct uart_port
*port
, unsigned char ch
)
387 while (*CSR_UARTFLG
& 0x20)
393 serial21285_console_write(struct console
*co
, const char *s
,
396 uart_console_write(&serial21285_port
, s
, count
, serial21285_console_putchar
);
400 serial21285_get_options(struct uart_port
*port
, int *baud
,
401 int *parity
, int *bits
)
403 if (*CSR_UARTCON
== 1) {
407 switch (tmp
& 0x60) {
423 if (tmp
& H_UBRLCR_PARENB
) {
425 if (tmp
& H_UBRLCR_PAREVN
)
429 tmp
= *CSR_L_UBRLCR
| (*CSR_M_UBRLCR
<< 8);
431 *baud
= port
->uartclk
/ (16 * (tmp
+ 1));
435 static int __init
serial21285_console_setup(struct console
*co
, char *options
)
437 struct uart_port
*port
= &serial21285_port
;
444 * Check whether an invalid uart number has been specified, and
445 * if so, search for the first available port that does have
449 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
451 serial21285_get_options(port
, &baud
, &parity
, &bits
);
453 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
456 static struct uart_driver serial21285_reg
;
458 static struct console serial21285_console
=
460 .name
= SERIAL_21285_NAME
,
461 .write
= serial21285_console_write
,
462 .device
= uart_console_device
,
463 .setup
= serial21285_console_setup
,
464 .flags
= CON_PRINTBUFFER
,
466 .data
= &serial21285_reg
,
469 static int __init
rs285_console_init(void)
471 serial21285_setup_ports();
472 register_console(&serial21285_console
);
475 console_initcall(rs285_console_init
);
477 #define SERIAL_21285_CONSOLE &serial21285_console
479 #define SERIAL_21285_CONSOLE NULL
482 static struct uart_driver serial21285_reg
= {
483 .owner
= THIS_MODULE
,
484 .driver_name
= "ttyFB",
486 .major
= SERIAL_21285_MAJOR
,
487 .minor
= SERIAL_21285_MINOR
,
489 .cons
= SERIAL_21285_CONSOLE
,
492 static int __init
serial21285_init(void)
496 printk(KERN_INFO
"Serial: 21285 driver\n");
498 serial21285_setup_ports();
500 ret
= uart_register_driver(&serial21285_reg
);
502 uart_add_one_port(&serial21285_reg
, &serial21285_port
);
507 static void __exit
serial21285_exit(void)
509 uart_remove_one_port(&serial21285_reg
, &serial21285_port
);
510 uart_unregister_driver(&serial21285_reg
);
513 module_init(serial21285_init
);
514 module_exit(serial21285_exit
);
516 MODULE_LICENSE("GPL");
517 MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver");
518 MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR
, SERIAL_21285_MINOR
);