blob: 7cf392829ff13db86f250089db5287c0cc66a734 [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.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/module.h>
29#include <linux/ioport.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/sysrq.h>
33#include <linux/spinlock.h>
34#include <linux/device.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial.h>
Russell King99730222009-03-25 10:21:35 +000039#include <linux/io.h>
Alexander Shiyanc08f0152012-10-14 11:05:26 +040040#include <linux/clk.h>
Alexander Shiyan95113722012-10-14 11:05:23 +040041#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Russell Kinga09e64f2008-08-05 16:14:15 +010043#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Alexander Shiyan95113722012-10-14 11:05:23 +040046#define UART_CLPS711X_NAME "uart-clps711x"
Alexander Shiyan117d5d42012-10-14 11:05:24 +040047#define UART_CLPS711X_NR 2
48#define UART_CLPS711X_MAJOR 204
49#define UART_CLPS711X_MINOR 40
Alexander Shiyan95113722012-10-14 11:05:23 +040050
Alexander Shiyan117d5d42012-10-14 11:05:24 +040051#define UBRLCR(port) ((port)->line ? UBRLCR2 : UBRLCR1)
52#define UARTDR(port) ((port)->line ? UARTDR2 : UARTDR1)
53#define SYSFLG(port) ((port)->line ? SYSFLG2 : SYSFLG1)
54#define SYSCON(port) ((port)->line ? SYSCON2 : SYSCON1)
55#define TX_IRQ(port) ((port)->line ? IRQ_UTXINT2 : IRQ_UTXINT1)
56#define RX_IRQ(port) ((port)->line ? IRQ_URXINT2 : IRQ_URXINT1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Alexander Shiyan117d5d42012-10-14 11:05:24 +040058struct clps711x_port {
59 struct uart_driver uart;
Alexander Shiyanc08f0152012-10-14 11:05:26 +040060 struct clk *uart_clk;
Alexander Shiyan117d5d42012-10-14 11:05:24 +040061 struct uart_port port[UART_CLPS711X_NR];
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040062 int tx_enabled[UART_CLPS711X_NR];
Alexander Shiyan117d5d42012-10-14 11:05:24 +040063#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
64 struct console console;
65#endif
66};
67
Russell Kingb129a8c2005-08-31 10:12:14 +010068static void clps711xuart_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040070 struct clps711x_port *s = dev_get_drvdata(port->dev);
71
72 if (s->tx_enabled[port->line]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 disable_irq(TX_IRQ(port));
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040074 s->tx_enabled[port->line] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76}
77
Russell Kingb129a8c2005-08-31 10:12:14 +010078static void clps711xuart_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040080 struct clps711x_port *s = dev_get_drvdata(port->dev);
81
82 if (!s->tx_enabled[port->line]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 enable_irq(TX_IRQ(port));
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040084 s->tx_enabled[port->line] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86}
87
88static void clps711xuart_stop_rx(struct uart_port *port)
89{
90 disable_irq(RX_IRQ(port));
91}
92
93static void clps711xuart_enable_ms(struct uart_port *port)
94{
95}
96
Alexander Shiyan135cc792012-10-14 11:05:31 +040097static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct uart_port *port = dev_id;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400100 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Russell Kingf9937242005-09-24 10:12:47 +0100101 unsigned int status, ch, flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Alexander Shiyanf27de952012-10-14 11:05:30 +0400103 if (!tty)
104 return IRQ_HANDLED;
105
106 for (;;) {
107 status = clps_readl(SYSFLG(port));
108 if (status & SYSFLG_URXFE)
109 break;
110
111 ch = clps_readw(UARTDR(port));
112 status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR);
113 ch &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 port->icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 flg = TTY_NORMAL;
117
Alexander Shiyanf27de952012-10-14 11:05:30 +0400118 if (unlikely(status)) {
119 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100120 port->icount.parity++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400121 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100122 port->icount.frame++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400123 else if (status & UARTDR_OVERR)
Russell King2a9604b2005-04-26 15:32:00 +0100124 port->icount.overrun++;
125
Alexander Shiyanf27de952012-10-14 11:05:30 +0400126 status &= port->read_status_mask;
Russell King2a9604b2005-04-26 15:32:00 +0100127
Alexander Shiyanf27de952012-10-14 11:05:30 +0400128 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100129 flg = TTY_PARITY;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400130 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100131 flg = TTY_FRAME;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400132 else if (status & UARTDR_OVERR)
133 flg = TTY_OVERRUN;
Russell King2a9604b2005-04-26 15:32:00 +0100134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
David Howells7d12e782006-10-05 14:55:46 +0100136 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf27de952012-10-14 11:05:30 +0400137 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Alexander Shiyanf27de952012-10-14 11:05:30 +0400139 if (status & port->ignore_status_mask)
140 continue;
Russell King2a9604b2005-04-26 15:32:00 +0100141
Alexander Shiyanf27de952012-10-14 11:05:30 +0400142 uart_insert_char(port, status, UARTDR_OVERR, ch, flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Alexander Shiyanf27de952012-10-14 11:05:30 +0400144
Russell King2a9604b2005-04-26 15:32:00 +0100145 tty_flip_buffer_push(tty);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400146
147 tty_kref_put(tty);
148
Russell King2a9604b2005-04-26 15:32:00 +0100149 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Alexander Shiyan135cc792012-10-14 11:05:31 +0400152static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct uart_port *port = dev_id;
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400155 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700156 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (port->x_char) {
159 clps_writel(port->x_char, UARTDR(port));
160 port->icount.tx++;
161 port->x_char = 0;
162 return IRQ_HANDLED;
163 }
Alexander Shiyan7a6fbc92012-03-27 12:22:49 +0400164
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400165 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
166 disable_irq_nosync(TX_IRQ(port));
167 s->tx_enabled[port->line] = 0;
168 return IRQ_HANDLED;
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Alexander Shiyancf03a882012-10-14 11:05:27 +0400171 while (!uart_circ_empty(xmit)) {
172 clps_writew(xmit->buf[xmit->tail], UARTDR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
174 port->icount.tx++;
Alexander Shiyancf03a882012-10-14 11:05:27 +0400175 if (clps_readl(SYSFLG(port) & SYSFLG_UTXFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
Alexander Shiyancf03a882012-10-14 11:05:27 +0400177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
180 uart_write_wakeup(port);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return IRQ_HANDLED;
183}
184
185static unsigned int clps711xuart_tx_empty(struct uart_port *port)
186{
187 unsigned int status = clps_readl(SYSFLG(port));
188 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
189}
190
191static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
192{
Alexander Shiyan1593daf2012-10-14 11:05:28 +0400193 unsigned int status, result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Alexander Shiyan1593daf2012-10-14 11:05:28 +0400195 if (port->line == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 status = clps_readl(SYSFLG1);
197 if (status & SYSFLG1_DCD)
198 result |= TIOCM_CAR;
199 if (status & SYSFLG1_DSR)
200 result |= TIOCM_DSR;
201 if (status & SYSFLG1_CTS)
202 result |= TIOCM_CTS;
Alexander Shiyan1593daf2012-10-14 11:05:28 +0400203 } else
204 result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 return result;
207}
208
209static void
210clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
211{
212}
213
214static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
215{
216 unsigned long flags;
217 unsigned int ubrlcr;
218
219 spin_lock_irqsave(&port->lock, flags);
Alexander Shiyanec335522012-10-14 11:05:29 +0400220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ubrlcr = clps_readl(UBRLCR(port));
Alexander Shiyanec335522012-10-14 11:05:29 +0400222 if (break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 ubrlcr |= UBRLCR_BREAK;
224 else
225 ubrlcr &= ~UBRLCR_BREAK;
226 clps_writel(ubrlcr, UBRLCR(port));
Alexander Shiyanec335522012-10-14 11:05:29 +0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_unlock_irqrestore(&port->lock, flags);
229}
230
231static int clps711xuart_startup(struct uart_port *port)
232{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400233 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned int syscon;
Alexander Shiyan135cc792012-10-14 11:05:31 +0400235 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400237 s->tx_enabled[port->line] = 1;
Alexander Shiyan135cc792012-10-14 11:05:31 +0400238 /* Allocate the IRQs */
239 ret = devm_request_irq(port->dev, TX_IRQ(port), uart_clps711x_int_tx,
240 0, UART_CLPS711X_NAME " TX", port);
241 if (ret)
242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Alexander Shiyan135cc792012-10-14 11:05:31 +0400244 ret = devm_request_irq(port->dev, RX_IRQ(port), uart_clps711x_int_rx,
245 0, UART_CLPS711X_NAME " RX", port);
246 if (ret) {
247 devm_free_irq(port->dev, TX_IRQ(port), port);
248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
251 /*
252 * enable the port
253 */
254 syscon = clps_readl(SYSCON(port));
255 syscon |= SYSCON_UARTEN;
256 clps_writel(syscon, SYSCON(port));
257
258 return 0;
259}
260
261static void clps711xuart_shutdown(struct uart_port *port)
262{
263 unsigned int ubrlcr, syscon;
264
Alexander Shiyan135cc792012-10-14 11:05:31 +0400265 /* Free the interrupts */
266 devm_free_irq(port->dev, TX_IRQ(port), port);
267 devm_free_irq(port->dev, RX_IRQ(port), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
270 * disable the port
271 */
272 syscon = clps_readl(SYSCON(port));
273 syscon &= ~SYSCON_UARTEN;
274 clps_writel(syscon, SYSCON(port));
275
276 /*
277 * disable break condition and fifos
278 */
279 ubrlcr = clps_readl(UBRLCR(port));
280 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
281 clps_writel(ubrlcr, UBRLCR(port));
282}
283
284static void
Alan Cox606d0992006-12-08 02:38:45 -0800285clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios,
286 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 unsigned int ubrlcr, baud, quot;
289 unsigned long flags;
290
291 /*
292 * We don't implement CREAD.
293 */
294 termios->c_cflag |= CREAD;
295
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400296 /* Ask the core to calculate the divisor for us */
297 baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
298 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 quot = uart_get_divisor(port, baud);
300
301 switch (termios->c_cflag & CSIZE) {
302 case CS5:
303 ubrlcr = UBRLCR_WRDLEN5;
304 break;
305 case CS6:
306 ubrlcr = UBRLCR_WRDLEN6;
307 break;
308 case CS7:
309 ubrlcr = UBRLCR_WRDLEN7;
310 break;
311 default: // CS8
312 ubrlcr = UBRLCR_WRDLEN8;
313 break;
314 }
315 if (termios->c_cflag & CSTOPB)
316 ubrlcr |= UBRLCR_XSTOP;
317 if (termios->c_cflag & PARENB) {
318 ubrlcr |= UBRLCR_PRTEN;
319 if (!(termios->c_cflag & PARODD))
320 ubrlcr |= UBRLCR_EVENPRT;
321 }
Alexander Shiyancf03a882012-10-14 11:05:27 +0400322
323 /* Enable FIFO */
324 ubrlcr |= UBRLCR_FIFOEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 spin_lock_irqsave(&port->lock, flags);
327
328 /*
329 * Update the per-port timeout.
330 */
331 uart_update_timeout(port, termios->c_cflag, baud);
332
333 port->read_status_mask = UARTDR_OVERR;
334 if (termios->c_iflag & INPCK)
335 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
336
337 /*
338 * Characters to ignore
339 */
340 port->ignore_status_mask = 0;
341 if (termios->c_iflag & IGNPAR)
342 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
343 if (termios->c_iflag & IGNBRK) {
344 /*
345 * If we're ignoring parity and break indicators,
346 * ignore overruns to (for real raw support).
347 */
348 if (termios->c_iflag & IGNPAR)
349 port->ignore_status_mask |= UARTDR_OVERR;
350 }
351
352 quot -= 1;
353
354 clps_writel(ubrlcr | quot, UBRLCR(port));
355
356 spin_unlock_irqrestore(&port->lock, flags);
357}
358
359static const char *clps711xuart_type(struct uart_port *port)
360{
361 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
362}
363
364/*
365 * Configure/autoconfigure the port.
366 */
367static void clps711xuart_config_port(struct uart_port *port, int flags)
368{
369 if (flags & UART_CONFIG_TYPE)
370 port->type = PORT_CLPS711X;
371}
372
373static void clps711xuart_release_port(struct uart_port *port)
374{
375}
376
377static int clps711xuart_request_port(struct uart_port *port)
378{
379 return 0;
380}
381
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400382static struct uart_ops uart_clps711x_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 .tx_empty = clps711xuart_tx_empty,
384 .set_mctrl = clps711xuart_set_mctrl_null,
385 .get_mctrl = clps711xuart_get_mctrl,
386 .stop_tx = clps711xuart_stop_tx,
387 .start_tx = clps711xuart_start_tx,
388 .stop_rx = clps711xuart_stop_rx,
389 .enable_ms = clps711xuart_enable_ms,
390 .break_ctl = clps711xuart_break_ctl,
391 .startup = clps711xuart_startup,
392 .shutdown = clps711xuart_shutdown,
393 .set_termios = clps711xuart_set_termios,
394 .type = clps711xuart_type,
395 .config_port = clps711xuart_config_port,
396 .release_port = clps711xuart_release_port,
397 .request_port = clps711xuart_request_port,
398};
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400401static void uart_clps711x_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +0000402{
403 while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
404 barrier();
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400405
406 clps_writew(ch, UARTDR(port));
Russell Kingd3587882006-03-20 20:00:09 +0000407}
408
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400409static void uart_clps711x_console_write(struct console *co, const char *c,
410 unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400412 struct clps711x_port *s = (struct clps711x_port *)co->data;
413 struct uart_port *port = &s->port[co->index];
414 u32 syscon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400416 /* Ensure that the port is enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 syscon = clps_readl(SYSCON(port));
418 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
419
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400420 uart_console_write(port, c, n, uart_clps711x_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400422 /* Wait for transmitter to become empty */
423 while (clps_readl(SYSFLG(port)) & SYSFLG_UBUSY)
424 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400426 /* Restore the uart state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 clps_writel(syscon, SYSCON(port));
428}
429
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400430static void uart_clps711x_console_get_options(struct uart_port *port,
431 int *baud, int *parity,
432 int *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
435 unsigned int ubrlcr, quot;
436
437 ubrlcr = clps_readl(UBRLCR(port));
438
439 *parity = 'n';
440 if (ubrlcr & UBRLCR_PRTEN) {
441 if (ubrlcr & UBRLCR_EVENPRT)
442 *parity = 'e';
443 else
444 *parity = 'o';
445 }
446
447 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
448 *bits = 7;
449 else
450 *bits = 8;
451
452 quot = ubrlcr & UBRLCR_BAUD_MASK;
453 *baud = port->uartclk / (16 * (quot + 1));
454 }
455}
456
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400457static int uart_clps711x_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400459 int baud = 38400, bits = 8, parity = 'n', flow = 'n';
460 struct clps711x_port *s = (struct clps711x_port *)co->data;
461 struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (options)
464 uart_parse_options(options, &baud, &parity, &bits, &flow);
465 else
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400466 uart_clps711x_console_get_options(port, &baud, &parity, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 return uart_set_options(port, co, baud, parity, bits, flow);
469}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#endif
471
Alexander Shiyan95113722012-10-14 11:05:23 +0400472static int __devinit uart_clps711x_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400474 struct clps711x_port *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int ret, i;
476
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400477 s = devm_kzalloc(&pdev->dev, sizeof(struct clps711x_port), GFP_KERNEL);
478 if (!s) {
479 dev_err(&pdev->dev, "Error allocating port structure\n");
480 return -ENOMEM;
481 }
482 platform_set_drvdata(pdev, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400484 s->uart_clk = devm_clk_get(&pdev->dev, "uart");
485 if (IS_ERR(s->uart_clk)) {
486 dev_err(&pdev->dev, "Can't get UART clocks\n");
487 ret = PTR_ERR(s->uart_clk);
488 goto err_out;
489 }
490
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400491 s->uart.owner = THIS_MODULE;
492 s->uart.dev_name = "ttyCL";
493 s->uart.major = UART_CLPS711X_MAJOR;
494 s->uart.minor = UART_CLPS711X_MINOR;
495 s->uart.nr = UART_CLPS711X_NR;
496#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
497 s->uart.cons = &s->console;
498 s->uart.cons->device = uart_console_device;
499 s->uart.cons->write = uart_clps711x_console_write;
500 s->uart.cons->setup = uart_clps711x_console_setup;
501 s->uart.cons->flags = CON_PRINTBUFFER;
502 s->uart.cons->index = -1;
503 s->uart.cons->data = s;
504 strcpy(s->uart.cons->name, "ttyCL");
505#endif
506 ret = uart_register_driver(&s->uart);
507 if (ret) {
508 dev_err(&pdev->dev, "Registering UART driver failed\n");
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400509 devm_clk_put(&pdev->dev, s->uart_clk);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400510 goto err_out;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400513 for (i = 0; i < UART_CLPS711X_NR; i++) {
514 s->port[i].line = i;
515 s->port[i].dev = &pdev->dev;
516 s->port[i].irq = TX_IRQ(&s->port[i]);
517 s->port[i].iobase = SYSCON(&s->port[i]);
518 s->port[i].type = PORT_CLPS711X;
519 s->port[i].fifosize = 16;
520 s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400521 s->port[i].uartclk = clk_get_rate(s->uart_clk);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400522 s->port[i].ops = &uart_clps711x_ops;
523 WARN_ON(uart_add_one_port(&s->uart, &s->port[i]));
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 return 0;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400527
528err_out:
529 platform_set_drvdata(pdev, NULL);
530
531 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Alexander Shiyan95113722012-10-14 11:05:23 +0400534static int __devexit uart_clps711x_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400536 struct clps711x_port *s = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int i;
538
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400539 for (i = 0; i < UART_CLPS711X_NR; i++)
540 uart_remove_one_port(&s->uart, &s->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400542 devm_clk_put(&pdev->dev, s->uart_clk);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400543 uart_unregister_driver(&s->uart);
544 platform_set_drvdata(pdev, NULL);
Alexander Shiyan95113722012-10-14 11:05:23 +0400545
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Alexander Shiyan95113722012-10-14 11:05:23 +0400549static struct platform_driver clps711x_uart_driver = {
550 .driver = {
551 .name = UART_CLPS711X_NAME,
552 .owner = THIS_MODULE,
553 },
554 .probe = uart_clps711x_probe,
555 .remove = __devexit_p(uart_clps711x_remove),
556};
557module_platform_driver(clps711x_uart_driver);
558
559static struct platform_device clps711x_uart_device = {
560 .name = UART_CLPS711X_NAME,
561};
562
563static int __init uart_clps711x_init(void)
564{
565 return platform_device_register(&clps711x_uart_device);
566}
567module_init(uart_clps711x_init);
568
569static void __exit uart_clps711x_exit(void)
570{
571 platform_device_unregister(&clps711x_uart_device);
572}
573module_exit(uart_clps711x_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575MODULE_AUTHOR("Deep Blue Solutions Ltd");
Alexander Shiyan95113722012-10-14 11:05:23 +0400576MODULE_DESCRIPTION("CLPS711X serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577MODULE_LICENSE("GPL");