blob: 8d0b994357c856319b2ffae055d1e605a7a40706 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for CLPS711x serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
16#define SUPPORT_SYSRQ
17#endif
18
19#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/device.h>
Alexander Shiyana1c25f22012-10-14 11:05:34 +040021#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/serial_core.h>
23#include <linux/serial.h>
Russell King99730222009-03-25 10:21:35 +000024#include <linux/io.h>
Alexander Shiyanc08f0152012-10-14 11:05:26 +040025#include <linux/clk.h>
Alexander Shiyana1c25f22012-10-14 11:05:34 +040026#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/ioport.h>
Alexander Shiyan95113722012-10-14 11:05:23 +040029#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Alexander Shiyan95113722012-10-14 11:05:23 +040033#define UART_CLPS711X_NAME "uart-clps711x"
Alexander Shiyan117d5d42012-10-14 11:05:24 +040034#define UART_CLPS711X_NR 2
35#define UART_CLPS711X_MAJOR 204
36#define UART_CLPS711X_MINOR 40
Alexander Shiyan95113722012-10-14 11:05:23 +040037
Alexander Shiyan117d5d42012-10-14 11:05:24 +040038#define UBRLCR(port) ((port)->line ? UBRLCR2 : UBRLCR1)
39#define UARTDR(port) ((port)->line ? UARTDR2 : UARTDR1)
40#define SYSFLG(port) ((port)->line ? SYSFLG2 : SYSFLG1)
41#define SYSCON(port) ((port)->line ? SYSCON2 : SYSCON1)
42#define TX_IRQ(port) ((port)->line ? IRQ_UTXINT2 : IRQ_UTXINT1)
43#define RX_IRQ(port) ((port)->line ? IRQ_URXINT2 : IRQ_URXINT1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Alexander Shiyan117d5d42012-10-14 11:05:24 +040045struct clps711x_port {
46 struct uart_driver uart;
Alexander Shiyanc08f0152012-10-14 11:05:26 +040047 struct clk *uart_clk;
Alexander Shiyan117d5d42012-10-14 11:05:24 +040048 struct uart_port port[UART_CLPS711X_NR];
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040049 int tx_enabled[UART_CLPS711X_NR];
Alexander Shiyan117d5d42012-10-14 11:05:24 +040050#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
51 struct console console;
52#endif
53};
54
Alexander Shiyana1c25f22012-10-14 11:05:34 +040055static void uart_clps711x_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040057 struct clps711x_port *s = dev_get_drvdata(port->dev);
58
59 if (s->tx_enabled[port->line]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 disable_irq(TX_IRQ(port));
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040061 s->tx_enabled[port->line] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63}
64
Alexander Shiyana1c25f22012-10-14 11:05:34 +040065static void uart_clps711x_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040067 struct clps711x_port *s = dev_get_drvdata(port->dev);
68
69 if (!s->tx_enabled[port->line]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 enable_irq(TX_IRQ(port));
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040071 s->tx_enabled[port->line] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73}
74
Alexander Shiyana1c25f22012-10-14 11:05:34 +040075static void uart_clps711x_stop_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 disable_irq(RX_IRQ(port));
78}
79
Alexander Shiyana1c25f22012-10-14 11:05:34 +040080static void uart_clps711x_enable_ms(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Alexander Shiyana1c25f22012-10-14 11:05:34 +040082 /* Do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Alexander Shiyan135cc792012-10-14 11:05:31 +040085static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct uart_port *port = dev_id;
Russell Kingf9937242005-09-24 10:12:47 +010088 unsigned int status, ch, flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Alexander Shiyanf27de952012-10-14 11:05:30 +040090 for (;;) {
91 status = clps_readl(SYSFLG(port));
92 if (status & SYSFLG_URXFE)
93 break;
94
95 ch = clps_readw(UARTDR(port));
96 status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR);
97 ch &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 port->icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 flg = TTY_NORMAL;
101
Alexander Shiyanf27de952012-10-14 11:05:30 +0400102 if (unlikely(status)) {
103 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100104 port->icount.parity++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400105 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100106 port->icount.frame++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400107 else if (status & UARTDR_OVERR)
Russell King2a9604b2005-04-26 15:32:00 +0100108 port->icount.overrun++;
109
Alexander Shiyanf27de952012-10-14 11:05:30 +0400110 status &= port->read_status_mask;
Russell King2a9604b2005-04-26 15:32:00 +0100111
Alexander Shiyanf27de952012-10-14 11:05:30 +0400112 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100113 flg = TTY_PARITY;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400114 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100115 flg = TTY_FRAME;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400116 else if (status & UARTDR_OVERR)
117 flg = TTY_OVERRUN;
Russell King2a9604b2005-04-26 15:32:00 +0100118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
David Howells7d12e782006-10-05 14:55:46 +0100120 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf27de952012-10-14 11:05:30 +0400121 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Alexander Shiyanf27de952012-10-14 11:05:30 +0400123 if (status & port->ignore_status_mask)
124 continue;
Russell King2a9604b2005-04-26 15:32:00 +0100125
Alexander Shiyanf27de952012-10-14 11:05:30 +0400126 uart_insert_char(port, status, UARTDR_OVERR, ch, flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
Alexander Shiyanf27de952012-10-14 11:05:30 +0400128
Jiri Slaby2e124b42013-01-03 15:53:06 +0100129 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400130
Russell King2a9604b2005-04-26 15:32:00 +0100131 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Alexander Shiyan135cc792012-10-14 11:05:31 +0400134static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct uart_port *port = dev_id;
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400137 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700138 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (port->x_char) {
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400141 clps_writew(port->x_char, UARTDR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 port->icount.tx++;
143 port->x_char = 0;
144 return IRQ_HANDLED;
145 }
Alexander Shiyan7a6fbc92012-03-27 12:22:49 +0400146
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400147 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
148 disable_irq_nosync(TX_IRQ(port));
149 s->tx_enabled[port->line] = 0;
150 return IRQ_HANDLED;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Alexander Shiyancf03a882012-10-14 11:05:27 +0400153 while (!uart_circ_empty(xmit)) {
154 clps_writew(xmit->buf[xmit->tail], UARTDR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
156 port->icount.tx++;
Alexander Shiyancf03a882012-10-14 11:05:27 +0400157 if (clps_readl(SYSFLG(port) & SYSFLG_UTXFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
Alexander Shiyancf03a882012-10-14 11:05:27 +0400159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
162 uart_write_wakeup(port);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return IRQ_HANDLED;
165}
166
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400167static unsigned int uart_clps711x_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400169 return (clps_readl(SYSFLG(port) & SYSFLG_UBUSY)) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400172static unsigned int uart_clps711x_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Alexander Shiyan1593daf2012-10-14 11:05:28 +0400174 unsigned int status, result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Alexander Shiyan1593daf2012-10-14 11:05:28 +0400176 if (port->line == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 status = clps_readl(SYSFLG1);
178 if (status & SYSFLG1_DCD)
179 result |= TIOCM_CAR;
180 if (status & SYSFLG1_DSR)
181 result |= TIOCM_DSR;
182 if (status & SYSFLG1_CTS)
183 result |= TIOCM_CTS;
Alexander Shiyan1593daf2012-10-14 11:05:28 +0400184 } else
185 result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 return result;
188}
189
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400190static void uart_clps711x_set_mctrl(struct uart_port *port, unsigned int mctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400192 /* Do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400195static void uart_clps711x_break_ctl(struct uart_port *port, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 unsigned long flags;
198 unsigned int ubrlcr;
199
200 spin_lock_irqsave(&port->lock, flags);
Alexander Shiyanec335522012-10-14 11:05:29 +0400201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 ubrlcr = clps_readl(UBRLCR(port));
Alexander Shiyanec335522012-10-14 11:05:29 +0400203 if (break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 ubrlcr |= UBRLCR_BREAK;
205 else
206 ubrlcr &= ~UBRLCR_BREAK;
207 clps_writel(ubrlcr, UBRLCR(port));
Alexander Shiyanec335522012-10-14 11:05:29 +0400208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 spin_unlock_irqrestore(&port->lock, flags);
210}
211
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400212static int uart_clps711x_startup(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400214 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan135cc792012-10-14 11:05:31 +0400215 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400217 s->tx_enabled[port->line] = 1;
Alexander Shiyan135cc792012-10-14 11:05:31 +0400218 /* Allocate the IRQs */
219 ret = devm_request_irq(port->dev, TX_IRQ(port), uart_clps711x_int_tx,
220 0, UART_CLPS711X_NAME " TX", port);
221 if (ret)
222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Alexander Shiyan135cc792012-10-14 11:05:31 +0400224 ret = devm_request_irq(port->dev, RX_IRQ(port), uart_clps711x_int_rx,
225 0, UART_CLPS711X_NAME " RX", port);
226 if (ret) {
227 devm_free_irq(port->dev, TX_IRQ(port), port);
228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400231 /* Disable break */
232 clps_writel(clps_readl(UBRLCR(port)) & ~UBRLCR_BREAK, UBRLCR(port));
233
234 /* Enable the port */
235 clps_writel(clps_readl(SYSCON(port)) | SYSCON_UARTEN, SYSCON(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 return 0;
238}
239
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400240static void uart_clps711x_shutdown(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Alexander Shiyan135cc792012-10-14 11:05:31 +0400242 /* Free the interrupts */
243 devm_free_irq(port->dev, TX_IRQ(port), port);
244 devm_free_irq(port->dev, RX_IRQ(port), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400246 /* Disable the port */
247 clps_writel(clps_readl(SYSCON(port)) & ~SYSCON_UARTEN, SYSCON(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400250static void uart_clps711x_set_termios(struct uart_port *port,
251 struct ktermios *termios,
252 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned int ubrlcr, baud, quot;
255 unsigned long flags;
256
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400257 /* Mask termios capabilities we don't support */
258 termios->c_cflag &= ~CMSPAR;
259 termios->c_iflag &= ~(BRKINT | IGNBRK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400261 /* Ask the core to calculate the divisor for us */
262 baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
263 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 quot = uart_get_divisor(port, baud);
265
266 switch (termios->c_cflag & CSIZE) {
267 case CS5:
268 ubrlcr = UBRLCR_WRDLEN5;
269 break;
270 case CS6:
271 ubrlcr = UBRLCR_WRDLEN6;
272 break;
273 case CS7:
274 ubrlcr = UBRLCR_WRDLEN7;
275 break;
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400276 case CS8:
277 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 ubrlcr = UBRLCR_WRDLEN8;
279 break;
280 }
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (termios->c_cflag & CSTOPB)
283 ubrlcr |= UBRLCR_XSTOP;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (termios->c_cflag & PARENB) {
286 ubrlcr |= UBRLCR_PRTEN;
287 if (!(termios->c_cflag & PARODD))
288 ubrlcr |= UBRLCR_EVENPRT;
289 }
Alexander Shiyancf03a882012-10-14 11:05:27 +0400290
291 /* Enable FIFO */
292 ubrlcr |= UBRLCR_FIFOEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 spin_lock_irqsave(&port->lock, flags);
295
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400296 /* Set read status mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 port->read_status_mask = UARTDR_OVERR;
298 if (termios->c_iflag & INPCK)
299 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
300
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400301 /* Set status ignore mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 port->ignore_status_mask = 0;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400303 if (!(termios->c_cflag & CREAD))
304 port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR |
305 UARTDR_FRMERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400307 uart_update_timeout(port, termios->c_cflag, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400309 clps_writel(ubrlcr | (quot - 1), UBRLCR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 spin_unlock_irqrestore(&port->lock, flags);
312}
313
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400314static const char *uart_clps711x_type(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400316 return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400319static void uart_clps711x_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 if (flags & UART_CONFIG_TYPE)
322 port->type = PORT_CLPS711X;
323}
324
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400325static void uart_clps711x_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400327 /* Do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400330static int uart_clps711x_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400332 /* Do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400336static const struct uart_ops uart_clps711x_ops = {
337 .tx_empty = uart_clps711x_tx_empty,
338 .set_mctrl = uart_clps711x_set_mctrl,
339 .get_mctrl = uart_clps711x_get_mctrl,
340 .stop_tx = uart_clps711x_stop_tx,
341 .start_tx = uart_clps711x_start_tx,
342 .stop_rx = uart_clps711x_stop_rx,
343 .enable_ms = uart_clps711x_enable_ms,
344 .break_ctl = uart_clps711x_break_ctl,
345 .startup = uart_clps711x_startup,
346 .shutdown = uart_clps711x_shutdown,
347 .set_termios = uart_clps711x_set_termios,
348 .type = uart_clps711x_type,
349 .config_port = uart_clps711x_config_port,
350 .release_port = uart_clps711x_release_port,
351 .request_port = uart_clps711x_request_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400355static void uart_clps711x_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +0000356{
357 while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
358 barrier();
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400359
360 clps_writew(ch, UARTDR(port));
Russell Kingd3587882006-03-20 20:00:09 +0000361}
362
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400363static void uart_clps711x_console_write(struct console *co, const char *c,
364 unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400366 struct clps711x_port *s = (struct clps711x_port *)co->data;
367 struct uart_port *port = &s->port[co->index];
368 u32 syscon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400370 /* Ensure that the port is enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 syscon = clps_readl(SYSCON(port));
372 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
373
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400374 uart_console_write(port, c, n, uart_clps711x_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400376 /* Wait for transmitter to become empty */
377 while (clps_readl(SYSFLG(port)) & SYSFLG_UBUSY)
378 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400380 /* Restore the uart state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 clps_writel(syscon, SYSCON(port));
382}
383
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400384static void uart_clps711x_console_get_options(struct uart_port *port,
385 int *baud, int *parity,
386 int *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
389 unsigned int ubrlcr, quot;
390
391 ubrlcr = clps_readl(UBRLCR(port));
392
393 *parity = 'n';
394 if (ubrlcr & UBRLCR_PRTEN) {
395 if (ubrlcr & UBRLCR_EVENPRT)
396 *parity = 'e';
397 else
398 *parity = 'o';
399 }
400
401 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
402 *bits = 7;
403 else
404 *bits = 8;
405
406 quot = ubrlcr & UBRLCR_BAUD_MASK;
407 *baud = port->uartclk / (16 * (quot + 1));
408 }
409}
410
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400411static int uart_clps711x_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400413 int baud = 38400, bits = 8, parity = 'n', flow = 'n';
414 struct clps711x_port *s = (struct clps711x_port *)co->data;
415 struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (options)
418 uart_parse_options(options, &baud, &parity, &bits, &flow);
419 else
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400420 uart_clps711x_console_get_options(port, &baud, &parity, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 return uart_set_options(port, co, baud, parity, bits, flow);
423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
425
Bill Pemberton9671f092012-11-19 13:21:50 -0500426static int uart_clps711x_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400428 struct clps711x_port *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int ret, i;
430
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400431 s = devm_kzalloc(&pdev->dev, sizeof(struct clps711x_port), GFP_KERNEL);
432 if (!s) {
433 dev_err(&pdev->dev, "Error allocating port structure\n");
434 return -ENOMEM;
435 }
436 platform_set_drvdata(pdev, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400438 s->uart_clk = devm_clk_get(&pdev->dev, "uart");
439 if (IS_ERR(s->uart_clk)) {
440 dev_err(&pdev->dev, "Can't get UART clocks\n");
Jingoo Han43b829b2013-06-25 10:08:49 +0900441 return PTR_ERR(s->uart_clk);
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400442 }
443
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400444 s->uart.owner = THIS_MODULE;
445 s->uart.dev_name = "ttyCL";
446 s->uart.major = UART_CLPS711X_MAJOR;
447 s->uart.minor = UART_CLPS711X_MINOR;
448 s->uart.nr = UART_CLPS711X_NR;
449#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
450 s->uart.cons = &s->console;
451 s->uart.cons->device = uart_console_device;
452 s->uart.cons->write = uart_clps711x_console_write;
453 s->uart.cons->setup = uart_clps711x_console_setup;
454 s->uart.cons->flags = CON_PRINTBUFFER;
455 s->uart.cons->index = -1;
456 s->uart.cons->data = s;
457 strcpy(s->uart.cons->name, "ttyCL");
458#endif
459 ret = uart_register_driver(&s->uart);
460 if (ret) {
461 dev_err(&pdev->dev, "Registering UART driver failed\n");
Jingoo Han43b829b2013-06-25 10:08:49 +0900462 return ret;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400465 for (i = 0; i < UART_CLPS711X_NR; i++) {
466 s->port[i].line = i;
467 s->port[i].dev = &pdev->dev;
468 s->port[i].irq = TX_IRQ(&s->port[i]);
469 s->port[i].iobase = SYSCON(&s->port[i]);
470 s->port[i].type = PORT_CLPS711X;
471 s->port[i].fifosize = 16;
472 s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400473 s->port[i].uartclk = clk_get_rate(s->uart_clk);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400474 s->port[i].ops = &uart_clps711x_ops;
475 WARN_ON(uart_add_one_port(&s->uart, &s->port[i]));
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 return 0;
479}
480
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500481static int uart_clps711x_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400483 struct clps711x_port *s = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int i;
485
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400486 for (i = 0; i < UART_CLPS711X_NR; i++)
487 uart_remove_one_port(&s->uart, &s->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400489 uart_unregister_driver(&s->uart);
Alexander Shiyan95113722012-10-14 11:05:23 +0400490
491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Alexander Shiyan95113722012-10-14 11:05:23 +0400494static struct platform_driver clps711x_uart_driver = {
495 .driver = {
496 .name = UART_CLPS711X_NAME,
497 .owner = THIS_MODULE,
498 },
499 .probe = uart_clps711x_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500500 .remove = uart_clps711x_remove,
Alexander Shiyan95113722012-10-14 11:05:23 +0400501};
502module_platform_driver(clps711x_uart_driver);
503
504static struct platform_device clps711x_uart_device = {
505 .name = UART_CLPS711X_NAME,
506};
507
508static int __init uart_clps711x_init(void)
509{
510 return platform_device_register(&clps711x_uart_device);
511}
512module_init(uart_clps711x_init);
513
514static void __exit uart_clps711x_exit(void)
515{
516 platform_device_unregister(&clps711x_uart_device);
517}
518module_exit(uart_clps711x_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520MODULE_AUTHOR("Deep Blue Solutions Ltd");
Alexander Shiyan95113722012-10-14 11:05:23 +0400521MODULE_DESCRIPTION("CLPS711X serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522MODULE_LICENSE("GPL");