1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (C) 2023 Nuvoton Technology Corp.
7 #include <linux/bitfield.h>
9 #include <linux/delay.h>
11 #include <linux/platform_device.h>
12 #include <linux/iopoll.h>
13 #include <linux/serial_core.h>
14 #include <linux/slab.h>
15 #include <linux/tty_flip.h>
16 #include <linux/units.h>
18 #define MA35_UART_NR 17
20 #define MA35_RBR_REG 0x00
21 #define MA35_THR_REG 0x00
22 #define MA35_IER_REG 0x04
23 #define MA35_FCR_REG 0x08
24 #define MA35_LCR_REG 0x0C
25 #define MA35_MCR_REG 0x10
26 #define MA35_MSR_REG 0x14
27 #define MA35_FSR_REG 0x18
28 #define MA35_ISR_REG 0x1C
29 #define MA35_TOR_REG 0x20
30 #define MA35_BAUD_REG 0x24
31 #define MA35_ALTCTL_REG 0x2C
32 #define MA35_FUN_SEL_REG 0x30
33 #define MA35_WKCTL_REG 0x40
34 #define MA35_WKSTS_REG 0x44
36 /* MA35_IER_REG - Interrupt Enable Register */
37 #define MA35_IER_RDA_IEN BIT(0) /* RBR Available Interrupt Enable */
38 #define MA35_IER_THRE_IEN BIT(1) /* THR Empty Interrupt Enable */
39 #define MA35_IER_RLS_IEN BIT(2) /* RX Line Status Interrupt Enable */
40 #define MA35_IER_RTO_IEN BIT(4) /* RX Time-out Interrupt Enable */
41 #define MA35_IER_BUFERR_IEN BIT(5) /* Buffer Error Interrupt Enable */
42 #define MA35_IER_TIME_OUT_EN BIT(11) /* RX Buffer Time-out Counter Enable */
43 #define MA35_IER_AUTO_RTS BIT(12) /* nRTS Auto-flow Control Enable */
44 #define MA35_IER_AUTO_CTS BIT(13) /* nCTS Auto-flow Control Enable */
46 /* MA35_FCR_REG - FIFO Control Register */
47 #define MA35_FCR_RFR BIT(1) /* RX Field Software Reset */
48 #define MA35_FCR_TFR BIT(2) /* TX Field Software Reset */
49 #define MA35_FCR_RFITL_MASK GENMASK(7, 4) /* RX FIFO Interrupt Trigger Level */
50 #define MA35_FCR_RFITL_1BYTE FIELD_PREP(MA35_FCR_RFITL_MASK, 0)
51 #define MA35_FCR_RFITL_4BYTES FIELD_PREP(MA35_FCR_RFITL_MASK, 1)
52 #define MA35_FCR_RFITL_8BYTES FIELD_PREP(MA35_FCR_RFITL_MASK, 2)
53 #define MA35_FCR_RFITL_14BYTES FIELD_PREP(MA35_FCR_RFITL_MASK, 3)
54 #define MA35_FCR_RFITL_30BYTES FIELD_PREP(MA35_FCR_RFITL_MASK, 4)
55 #define MA35_FCR_RTSTL_MASK GENMASK(19, 16) /* nRTS Trigger Level */
56 #define MA35_FCR_RTSTL_1BYTE FIELD_PREP(MA35_FCR_RTSTL_MASK, 0)
57 #define MA35_FCR_RTSTL_4BYTES FIELD_PREP(MA35_FCR_RTSTL_MASK, 1)
58 #define MA35_FCR_RTSTL_8BYTES FIELD_PREP(MA35_FCR_RTSTL_MASK, 2)
59 #define MA35_FCR_RTSTL_14BYTES FIELD_PREP(MA35_FCR_RTSTL_MASK, 3)
60 #define MA35_FCR_RTSTLL_30BYTES FIELD_PREP(MA35_FCR_RTSTL_MASK, 4)
62 /* MA35_LCR_REG - Line Control Register */
63 #define MA35_LCR_NSB BIT(2) /* Number of “STOP Bit” */
64 #define MA35_LCR_PBE BIT(3) /* Parity Bit Enable */
65 #define MA35_LCR_EPE BIT(4) /* Even Parity Enable */
66 #define MA35_LCR_SPE BIT(5) /* Stick Parity Enable */
67 #define MA35_LCR_BREAK BIT(6) /* Break Control */
68 #define MA35_LCR_WLS_MASK GENMASK(1, 0) /* Word Length Selection */
69 #define MA35_LCR_WLS_5BITS FIELD_PREP(MA35_LCR_WLS_MASK, 0)
70 #define MA35_LCR_WLS_6BITS FIELD_PREP(MA35_LCR_WLS_MASK, 1)
71 #define MA35_LCR_WLS_7BITS FIELD_PREP(MA35_LCR_WLS_MASK, 2)
72 #define MA35_LCR_WLS_8BITS FIELD_PREP(MA35_LCR_WLS_MASK, 3)
74 /* MA35_MCR_REG - Modem Control Register */
75 #define MA35_MCR_RTS_CTRL BIT(1) /* nRTS Signal Control */
76 #define MA35_MCR_RTSACTLV BIT(9) /* nRTS Pin Active Level */
77 #define MA35_MCR_RTSSTS BIT(13) /* nRTS Pin Status (Read Only) */
79 /* MA35_MSR_REG - Modem Status Register */
80 #define MA35_MSR_CTSDETF BIT(0) /* Detect nCTS State Change Flag */
81 #define MA35_MSR_CTSSTS BIT(4) /* nCTS Pin Status (Read Only) */
82 #define MA35_MSR_CTSACTLV BIT(8) /* nCTS Pin Active Level */
84 /* MA35_FSR_REG - FIFO Status Register */
85 #define MA35_FSR_RX_OVER_IF BIT(0) /* RX Overflow Error Interrupt Flag */
86 #define MA35_FSR_PEF BIT(4) /* Parity Error Flag*/
87 #define MA35_FSR_FEF BIT(5) /* Framing Error Flag */
88 #define MA35_FSR_BIF BIT(6) /* Break Interrupt Flag */
89 #define MA35_FSR_RX_EMPTY BIT(14) /* Receiver FIFO Empty (Read Only) */
90 #define MA35_FSR_RX_FULL BIT(15) /* Receiver FIFO Full (Read Only) */
91 #define MA35_FSR_TX_EMPTY BIT(22) /* Transmitter FIFO Empty (Read Only) */
92 #define MA35_FSR_TX_FULL BIT(23) /* Transmitter FIFO Full (Read Only) */
93 #define MA35_FSR_TX_OVER_IF BIT(24) /* TX Overflow Error Interrupt Flag */
94 #define MA35_FSR_TE_FLAG BIT(28) /* Transmitter Empty Flag (Read Only) */
95 #define MA35_FSR_RXPTR_MSK GENMASK(13, 8) /* TX FIFO Pointer mask */
96 #define MA35_FSR_TXPTR_MSK GENMASK(21, 16) /* RX FIFO Pointer mask */
98 /* MA35_ISR_REG - Interrupt Status Register */
99 #define MA35_ISR_RDA_IF BIT(0) /* RBR Available Interrupt Flag */
100 #define MA35_ISR_THRE_IF BIT(1) /* THR Empty Interrupt Flag */
101 #define MA35_ISR_RLSIF BIT(2) /* Receive Line Interrupt Flag */
102 #define MA35_ISR_MODEMIF BIT(3) /* MODEM Interrupt Flag */
103 #define MA35_ISR_RXTO_IF BIT(4) /* RX Time-out Interrupt Flag */
104 #define MA35_ISR_BUFEIF BIT(5) /* Buffer Error Interrupt Flag */
105 #define MA35_ISR_WK_IF BIT(6) /* UART Wake-up Interrupt Flag */
106 #define MA35_ISR_RDAINT BIT(8) /* RBR Available Interrupt Indicator */
107 #define MA35_ISR_THRE_INT BIT(9) /* THR Empty Interrupt Indicator */
108 #define MA35_ISR_ALL 0xFFFFFFFF
110 /* MA35_BAUD_REG - Baud Rate Divider Register */
111 #define MA35_BAUD_MODE_MASK GENMASK(29, 28)
112 #define MA35_BAUD_MODE0 FIELD_PREP(MA35_BAUD_MODE_MASK, 0)
113 #define MA35_BAUD_MODE1 FIELD_PREP(MA35_BAUD_MODE_MASK, 2)
114 #define MA35_BAUD_MODE2 FIELD_PREP(MA35_BAUD_MODE_MASK, 3)
115 #define MA35_BAUD_MASK GENMASK(15, 0)
117 /* MA35_ALTCTL_REG - Alternate Control/Status Register */
118 #define MA35_ALTCTL_RS485AUD BIT(10) /* RS-485 Auto Direction Function */
120 /* MA35_FUN_SEL_REG - Function Select Register */
121 #define MA35_FUN_SEL_MASK GENMASK(2, 0)
122 #define MA35_FUN_SEL_UART FIELD_PREP(MA35_FUN_SEL_MASK, 0)
123 #define MA35_FUN_SEL_RS485 FIELD_PREP(MA35_FUN_SEL_MASK, 3)
125 /* The constrain for MA35D1 UART baud rate divider */
126 #define MA35_BAUD_DIV_MAX 0xFFFF
127 #define MA35_BAUD_DIV_MIN 11
129 /* UART FIFO depth */
130 #define MA35_UART_FIFO_DEPTH 32
131 /* UART console clock */
132 #define MA35_UART_CONSOLE_CLK (24 * HZ_PER_MHZ)
133 /* UART register ioremap size */
134 #define MA35_UART_REG_SIZE 0x100
136 #define MA35_UART_RX_TOUT 0x40
138 #define MA35_IER_CONFIG (MA35_IER_RTO_IEN | MA35_IER_RDA_IEN | \
139 MA35_IER_TIME_OUT_EN | MA35_IER_BUFERR_IEN)
141 #define MA35_ISR_IF_CHECK (MA35_ISR_RDA_IF | MA35_ISR_RXTO_IF | \
142 MA35_ISR_THRE_INT | MA35_ISR_BUFEIF)
144 #define MA35_FSR_TX_BOTH_EMPTY (MA35_FSR_TE_FLAG | MA35_FSR_TX_EMPTY)
146 static struct uart_driver ma35d1serial_reg
;
148 struct uart_ma35d1_port
{
149 struct uart_port port
;
151 u16 capabilities
; /* port capabilities */
156 u32 console_baud_rate
;
161 static struct uart_ma35d1_port ma35d1serial_ports
[MA35_UART_NR
];
163 static struct uart_ma35d1_port
*to_ma35d1_uart_port(struct uart_port
*uart
)
165 return container_of(uart
, struct uart_ma35d1_port
, port
);
168 static u32
serial_in(struct uart_ma35d1_port
*p
, u32 offset
)
170 return readl_relaxed(p
->port
.membase
+ offset
);
173 static void serial_out(struct uart_ma35d1_port
*p
, u32 offset
, u32 value
)
175 writel_relaxed(value
, p
->port
.membase
+ offset
);
178 static void __stop_tx(struct uart_ma35d1_port
*p
)
182 ier
= serial_in(p
, MA35_IER_REG
);
183 if (ier
& MA35_IER_THRE_IEN
)
184 serial_out(p
, MA35_IER_REG
, ier
& ~MA35_IER_THRE_IEN
);
187 static void ma35d1serial_stop_tx(struct uart_port
*port
)
189 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
194 static void transmit_chars(struct uart_ma35d1_port
*up
)
199 if (uart_tx_stopped(&up
->port
)) {
200 ma35d1serial_stop_tx(&up
->port
);
203 count
= MA35_UART_FIFO_DEPTH
- FIELD_GET(MA35_FSR_TXPTR_MSK
,
204 serial_in(up
, MA35_FSR_REG
));
205 uart_port_tx_limited(&up
->port
, ch
, count
,
206 !(serial_in(up
, MA35_FSR_REG
) & MA35_FSR_TX_FULL
),
207 serial_out(up
, MA35_THR_REG
, ch
),
211 static void ma35d1serial_start_tx(struct uart_port
*port
)
213 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
216 ier
= serial_in(up
, MA35_IER_REG
);
217 serial_out(up
, MA35_IER_REG
, ier
& ~MA35_IER_THRE_IEN
);
219 serial_out(up
, MA35_IER_REG
, ier
| MA35_IER_THRE_IEN
);
222 static void ma35d1serial_stop_rx(struct uart_port
*port
)
224 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
227 ier
= serial_in(up
, MA35_IER_REG
);
228 ier
&= ~MA35_IER_RDA_IEN
;
229 serial_out(up
, MA35_IER_REG
, ier
);
232 static void receive_chars(struct uart_ma35d1_port
*up
)
238 fsr
= serial_in(up
, MA35_FSR_REG
);
241 up
->port
.icount
.rx
++;
243 if (unlikely(fsr
& (MA35_FSR_BIF
| MA35_FSR_FEF
|
244 MA35_FSR_PEF
| MA35_FSR_RX_OVER_IF
))) {
245 if (fsr
& MA35_FSR_BIF
) {
246 up
->port
.icount
.brk
++;
247 if (uart_handle_break(&up
->port
))
250 if (fsr
& MA35_FSR_FEF
)
251 up
->port
.icount
.frame
++;
252 if (fsr
& MA35_FSR_PEF
)
253 up
->port
.icount
.parity
++;
254 if (fsr
& MA35_FSR_RX_OVER_IF
)
255 up
->port
.icount
.overrun
++;
257 serial_out(up
, MA35_FSR_REG
,
258 fsr
& (MA35_FSR_BIF
| MA35_FSR_FEF
|
259 MA35_FSR_PEF
| MA35_FSR_RX_OVER_IF
));
260 if (fsr
& MA35_FSR_BIF
)
262 else if (fsr
& MA35_FSR_PEF
)
264 else if (fsr
& MA35_FSR_FEF
)
268 ch
= serial_in(up
, MA35_RBR_REG
);
269 if (uart_handle_sysrq_char(&up
->port
, ch
))
272 uart_port_lock(&up
->port
);
273 uart_insert_char(&up
->port
, fsr
, MA35_FSR_RX_OVER_IF
, ch
, flag
);
274 uart_port_unlock(&up
->port
);
276 fsr
= serial_in(up
, MA35_FSR_REG
);
277 } while (!(fsr
& MA35_FSR_RX_EMPTY
) && (max_count
-- > 0));
279 uart_port_lock(&up
->port
);
280 tty_flip_buffer_push(&up
->port
.state
->port
);
281 uart_port_unlock(&up
->port
);
284 static irqreturn_t
ma35d1serial_interrupt(int irq
, void *dev_id
)
286 struct uart_port
*port
= dev_id
;
287 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
290 isr
= serial_in(up
, MA35_ISR_REG
);
291 fsr
= serial_in(up
, MA35_FSR_REG
);
293 if (!(isr
& MA35_ISR_IF_CHECK
))
296 if (isr
& (MA35_ISR_RDA_IF
| MA35_ISR_RXTO_IF
))
298 if (isr
& MA35_ISR_THRE_INT
)
300 if (fsr
& MA35_FSR_TX_OVER_IF
)
301 serial_out(up
, MA35_FSR_REG
, MA35_FSR_TX_OVER_IF
);
306 static u32
ma35d1serial_tx_empty(struct uart_port
*port
)
308 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
311 fsr
= serial_in(up
, MA35_FSR_REG
);
312 if ((fsr
& MA35_FSR_TX_BOTH_EMPTY
) == MA35_FSR_TX_BOTH_EMPTY
)
318 static u32
ma35d1serial_get_mctrl(struct uart_port
*port
)
320 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
324 status
= serial_in(up
, MA35_MSR_REG
);
325 if (!(status
& MA35_MSR_CTSSTS
))
330 static void ma35d1serial_set_mctrl(struct uart_port
*port
, u32 mctrl
)
332 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
335 mcr
= serial_in(up
, MA35_MCR_REG
);
336 mcr
&= ~MA35_MCR_RTS_CTRL
;
338 if (mctrl
& TIOCM_RTS
)
339 mcr
|= MA35_MCR_RTSACTLV
;
341 mcr
&= ~MA35_MCR_RTSACTLV
;
343 if (up
->mcr
& UART_MCR_AFE
) {
344 ier
= serial_in(up
, MA35_IER_REG
);
345 ier
|= MA35_IER_AUTO_RTS
| MA35_IER_AUTO_CTS
;
346 serial_out(up
, MA35_IER_REG
, ier
);
347 up
->port
.flags
|= UPF_HARD_FLOW
;
349 ier
= serial_in(up
, MA35_IER_REG
);
350 ier
&= ~(MA35_IER_AUTO_RTS
| MA35_IER_AUTO_CTS
);
351 serial_out(up
, MA35_IER_REG
, ier
);
352 up
->port
.flags
&= ~UPF_HARD_FLOW
;
355 msr
= serial_in(up
, MA35_MSR_REG
);
356 msr
|= MA35_MSR_CTSACTLV
;
357 serial_out(up
, MA35_MSR_REG
, msr
);
358 serial_out(up
, MA35_MCR_REG
, mcr
);
361 static void ma35d1serial_break_ctl(struct uart_port
*port
, int break_state
)
363 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
367 uart_port_lock_irqsave(&up
->port
, &flags
);
368 lcr
= serial_in(up
, MA35_LCR_REG
);
369 if (break_state
!= 0)
370 lcr
|= MA35_LCR_BREAK
;
372 lcr
&= ~MA35_LCR_BREAK
;
373 serial_out(up
, MA35_LCR_REG
, lcr
);
374 uart_port_unlock_irqrestore(&up
->port
, flags
);
377 static int ma35d1serial_startup(struct uart_port
*port
)
379 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
384 serial_out(up
, MA35_FCR_REG
, MA35_FCR_TFR
| MA35_FCR_RFR
);
386 /* Clear pending interrupts */
387 serial_out(up
, MA35_ISR_REG
, MA35_ISR_ALL
);
389 retval
= request_irq(port
->irq
, ma35d1serial_interrupt
, 0,
390 dev_name(port
->dev
), port
);
392 dev_err(up
->port
.dev
, "request irq failed.\n");
396 fcr
= serial_in(up
, MA35_FCR_REG
);
397 fcr
|= MA35_FCR_RFITL_4BYTES
| MA35_FCR_RTSTL_8BYTES
;
398 serial_out(up
, MA35_FCR_REG
, fcr
);
399 serial_out(up
, MA35_LCR_REG
, MA35_LCR_WLS_8BITS
);
400 serial_out(up
, MA35_TOR_REG
, MA35_UART_RX_TOUT
);
401 serial_out(up
, MA35_IER_REG
, MA35_IER_CONFIG
);
405 static void ma35d1serial_shutdown(struct uart_port
*port
)
407 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
409 serial_out(up
, MA35_IER_REG
, 0);
410 free_irq(port
->irq
, port
);
413 static void ma35d1serial_set_termios(struct uart_port
*port
,
414 struct ktermios
*termios
,
415 const struct ktermios
*old
)
417 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
422 lcr
= UART_LCR_WLEN(tty_get_char_size(termios
->c_cflag
));
424 if (termios
->c_cflag
& CSTOPB
)
426 if (termios
->c_cflag
& PARENB
)
428 if (!(termios
->c_cflag
& PARODD
))
430 if (termios
->c_cflag
& CMSPAR
)
433 baud
= uart_get_baud_rate(port
, termios
, old
,
434 port
->uartclk
/ MA35_BAUD_DIV_MAX
,
435 port
->uartclk
/ MA35_BAUD_DIV_MIN
);
437 /* MA35D1 UART baud rate equation: baudrate = UART_CLK / (quot + 2) */
438 quot
= (port
->uartclk
/ baud
) - 2;
441 * Ok, we're now changing the port state. Do it with
442 * interrupts disabled.
444 uart_port_lock_irqsave(&up
->port
, &flags
);
446 up
->port
.read_status_mask
= MA35_FSR_RX_OVER_IF
;
447 if (termios
->c_iflag
& INPCK
)
448 up
->port
.read_status_mask
|= MA35_FSR_FEF
| MA35_FSR_PEF
;
449 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
450 up
->port
.read_status_mask
|= MA35_FSR_BIF
;
452 /* Characteres to ignore */
453 up
->port
.ignore_status_mask
= 0;
454 if (termios
->c_iflag
& IGNPAR
)
455 up
->port
.ignore_status_mask
|= MA35_FSR_FEF
| MA35_FSR_PEF
;
456 if (termios
->c_iflag
& IGNBRK
) {
457 up
->port
.ignore_status_mask
|= MA35_FSR_BIF
;
459 * If we're ignoring parity and break indicators,
460 * ignore overruns too (for real raw support).
462 if (termios
->c_iflag
& IGNPAR
)
463 up
->port
.ignore_status_mask
|= MA35_FSR_RX_OVER_IF
;
465 if (termios
->c_cflag
& CRTSCTS
)
466 up
->mcr
|= UART_MCR_AFE
;
468 up
->mcr
&= ~UART_MCR_AFE
;
470 uart_update_timeout(port
, termios
->c_cflag
, baud
);
472 ma35d1serial_set_mctrl(&up
->port
, up
->port
.mctrl
);
474 serial_out(up
, MA35_BAUD_REG
, MA35_BAUD_MODE2
| FIELD_PREP(MA35_BAUD_MASK
, quot
));
476 serial_out(up
, MA35_LCR_REG
, lcr
);
478 uart_port_unlock_irqrestore(&up
->port
, flags
);
481 static const char *ma35d1serial_type(struct uart_port
*port
)
483 return "ma35d1-uart";
486 static void ma35d1serial_config_port(struct uart_port
*port
, int flags
)
489 * Driver core for serial ports forces a non-zero value for port type.
490 * Write an arbitrary value here to accommodate the serial core driver,
491 * as ID part of UAPI is redundant.
496 static int ma35d1serial_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
498 if (port
->type
!= PORT_UNKNOWN
&& ser
->type
!= 1)
504 static const struct uart_ops ma35d1serial_ops
= {
505 .tx_empty
= ma35d1serial_tx_empty
,
506 .set_mctrl
= ma35d1serial_set_mctrl
,
507 .get_mctrl
= ma35d1serial_get_mctrl
,
508 .stop_tx
= ma35d1serial_stop_tx
,
509 .start_tx
= ma35d1serial_start_tx
,
510 .stop_rx
= ma35d1serial_stop_rx
,
511 .break_ctl
= ma35d1serial_break_ctl
,
512 .startup
= ma35d1serial_startup
,
513 .shutdown
= ma35d1serial_shutdown
,
514 .set_termios
= ma35d1serial_set_termios
,
515 .type
= ma35d1serial_type
,
516 .config_port
= ma35d1serial_config_port
,
517 .verify_port
= ma35d1serial_verify_port
,
520 static const struct of_device_id ma35d1_serial_of_match
[] = {
521 { .compatible
= "nuvoton,ma35d1-uart" },
524 MODULE_DEVICE_TABLE(of
, ma35d1_serial_of_match
);
526 #ifdef CONFIG_SERIAL_NUVOTON_MA35D1_CONSOLE
528 static struct device_node
*ma35d1serial_uart_nodes
[MA35_UART_NR
];
530 static void wait_for_xmitr(struct uart_ma35d1_port
*up
)
532 unsigned int reg
= 0;
534 read_poll_timeout_atomic(serial_in
, reg
, reg
& MA35_FSR_TX_EMPTY
,
539 static void ma35d1serial_console_putchar(struct uart_port
*port
, unsigned char ch
)
541 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
544 serial_out(up
, MA35_THR_REG
, ch
);
548 * Print a string to the serial port trying not to disturb
549 * any possible real use of the port...
551 * The console_lock must be held when we get here.
553 static void ma35d1serial_console_write(struct console
*co
, const char *s
, u32 count
)
555 struct uart_ma35d1_port
*up
;
560 if ((co
->index
< 0) || (co
->index
>= MA35_UART_NR
)) {
561 pr_warn("Failed to write on console port %x, out of range\n",
566 up
= &ma35d1serial_ports
[co
->index
];
570 else if (oops_in_progress
)
571 locked
= uart_port_trylock_irqsave(&up
->port
, &flags
);
573 uart_port_lock_irqsave(&up
->port
, &flags
);
576 * First save the IER then disable the interrupts
578 ier
= serial_in(up
, MA35_IER_REG
);
579 serial_out(up
, MA35_IER_REG
, 0);
581 uart_console_write(&up
->port
, s
, count
, ma35d1serial_console_putchar
);
584 serial_out(up
, MA35_IER_REG
, ier
);
587 uart_port_unlock_irqrestore(&up
->port
, flags
);
590 static int __init
ma35d1serial_console_setup(struct console
*co
, char *options
)
592 struct device_node
*np
;
593 struct uart_ma35d1_port
*p
;
595 struct uart_port
*port
;
601 if ((co
->index
< 0) || (co
->index
>= MA35_UART_NR
)) {
602 pr_debug("Console Port%x out of range\n", co
->index
);
606 np
= ma35d1serial_uart_nodes
[co
->index
];
607 p
= &ma35d1serial_ports
[co
->index
];
611 if (of_property_read_u32_array(np
, "reg", val32
, ARRAY_SIZE(val32
)) != 0)
614 p
->port
.iobase
= val32
[1];
615 p
->port
.membase
= ioremap(p
->port
.iobase
, MA35_UART_REG_SIZE
);
616 if (!p
->port
.membase
)
619 p
->port
.ops
= &ma35d1serial_ops
;
621 p
->port
.uartclk
= MA35_UART_CONSOLE_CLK
;
623 port
= &ma35d1serial_ports
[co
->index
].port
;
626 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
628 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
631 static struct console ma35d1serial_console
= {
633 .write
= ma35d1serial_console_write
,
634 .device
= uart_console_device
,
635 .setup
= ma35d1serial_console_setup
,
636 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
638 .data
= &ma35d1serial_reg
,
641 static void ma35d1serial_console_init_port(void)
644 struct device_node
*np
;
646 for_each_matching_node(np
, ma35d1_serial_of_match
) {
647 if (ma35d1serial_uart_nodes
[i
] == NULL
) {
649 ma35d1serial_uart_nodes
[i
] = np
;
651 if (i
== MA35_UART_NR
)
657 static int __init
ma35d1serial_console_init(void)
659 ma35d1serial_console_init_port();
660 register_console(&ma35d1serial_console
);
663 console_initcall(ma35d1serial_console_init
);
665 #define MA35D1SERIAL_CONSOLE (&ma35d1serial_console)
667 #define MA35D1SERIAL_CONSOLE NULL
670 static struct uart_driver ma35d1serial_reg
= {
671 .owner
= THIS_MODULE
,
672 .driver_name
= "serial",
673 .dev_name
= "ttyNVT",
676 .cons
= MA35D1SERIAL_CONSOLE
,
681 * Register a set of serial devices attached to a platform device.
682 * The list is terminated with a zero flags entry, which means we expect
683 * all entries to have at least UPF_BOOT_AUTOCONF set.
685 static int ma35d1serial_probe(struct platform_device
*pdev
)
687 struct resource
*res_mem
;
688 struct uart_ma35d1_port
*up
;
691 if (!pdev
->dev
.of_node
)
694 ret
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
696 dev_err(&pdev
->dev
, "failed to get alias/pdev id, errno %d\n", ret
);
699 up
= &ma35d1serial_ports
[ret
];
701 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
705 up
->port
.iobase
= res_mem
->start
;
706 up
->port
.membase
= ioremap(up
->port
.iobase
, MA35_UART_REG_SIZE
);
707 if (!up
->port
.membase
)
710 up
->port
.ops
= &ma35d1serial_ops
;
712 spin_lock_init(&up
->port
.lock
);
714 up
->clk
= of_clk_get(pdev
->dev
.of_node
, 0);
715 if (IS_ERR(up
->clk
)) {
716 ret
= PTR_ERR(up
->clk
);
717 dev_err(&pdev
->dev
, "failed to get core clk: %d\n", ret
);
721 ret
= clk_prepare_enable(up
->clk
);
725 if (up
->port
.line
!= 0)
726 up
->port
.uartclk
= clk_get_rate(up
->clk
);
728 ret
= platform_get_irq(pdev
, 0);
730 goto err_clk_disable
;
733 up
->port
.dev
= &pdev
->dev
;
734 up
->port
.flags
= UPF_BOOT_AUTOCONF
;
736 platform_set_drvdata(pdev
, up
);
738 ret
= uart_add_one_port(&ma35d1serial_reg
, &up
->port
);
745 free_irq(up
->port
.irq
, &up
->port
);
748 clk_disable_unprepare(up
->clk
);
751 iounmap(up
->port
.membase
);
756 * Remove serial ports registered against a platform device.
758 static void ma35d1serial_remove(struct platform_device
*dev
)
760 struct uart_port
*port
= platform_get_drvdata(dev
);
761 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
763 uart_remove_one_port(&ma35d1serial_reg
, port
);
764 clk_disable_unprepare(up
->clk
);
767 static int ma35d1serial_suspend(struct platform_device
*dev
, pm_message_t state
)
769 struct uart_port
*port
= platform_get_drvdata(dev
);
770 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
772 uart_suspend_port(&ma35d1serial_reg
, &up
->port
);
773 if (up
->port
.line
== 0) {
774 up
->console_baud_rate
= serial_in(up
, MA35_BAUD_REG
);
775 up
->console_line
= serial_in(up
, MA35_LCR_REG
);
776 up
->console_int
= serial_in(up
, MA35_IER_REG
);
781 static int ma35d1serial_resume(struct platform_device
*dev
)
783 struct uart_port
*port
= platform_get_drvdata(dev
);
784 struct uart_ma35d1_port
*up
= to_ma35d1_uart_port(port
);
786 if (up
->port
.line
== 0) {
787 serial_out(up
, MA35_BAUD_REG
, up
->console_baud_rate
);
788 serial_out(up
, MA35_LCR_REG
, up
->console_line
);
789 serial_out(up
, MA35_IER_REG
, up
->console_int
);
791 uart_resume_port(&ma35d1serial_reg
, &up
->port
);
795 static struct platform_driver ma35d1serial_driver
= {
796 .probe
= ma35d1serial_probe
,
797 .remove
= ma35d1serial_remove
,
798 .suspend
= ma35d1serial_suspend
,
799 .resume
= ma35d1serial_resume
,
801 .name
= "ma35d1-uart",
802 .of_match_table
= of_match_ptr(ma35d1_serial_of_match
),
806 static int __init
ma35d1serial_init(void)
810 ret
= uart_register_driver(&ma35d1serial_reg
);
814 ret
= platform_driver_register(&ma35d1serial_driver
);
816 uart_unregister_driver(&ma35d1serial_reg
);
821 static void __exit
ma35d1serial_exit(void)
823 platform_driver_unregister(&ma35d1serial_driver
);
824 uart_unregister_driver(&ma35d1serial_reg
);
827 module_init(ma35d1serial_init
);
828 module_exit(ma35d1serial_exit
);
830 MODULE_LICENSE("GPL");
831 MODULE_DESCRIPTION("MA35D1 serial driver");