blob: 61b607f2488e36761069e0e58a9bd2cf74661100 [file] [log] [blame]
Tobias Klauser6b7d8f82010-05-05 10:35:23 +02001/*
2 * altera_uart.c -- Altera UART driver
3 *
4 * Based on mcf.c -- Freescale ColdFire UART driver
5 *
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
7 * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
8 * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Anton Vorontsov2f8b9c12010-10-01 17:21:33 +040018#include <linux/timer.h>
Tobias Klauser6b7d8f82010-05-05 10:35:23 +020019#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/console.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/serial.h>
25#include <linux/serial_core.h>
26#include <linux/platform_device.h>
Tobias Klauser7c9325d2011-02-18 11:35:32 +010027#include <linux/of.h>
Tobias Klauser6b7d8f82010-05-05 10:35:23 +020028#include <linux/io.h>
29#include <linux/altera_uart.h>
30
31#define DRV_NAME "altera_uart"
Anton Vorontsov99793c62010-10-01 18:23:24 +040032#define SERIAL_ALTERA_MAJOR 204
33#define SERIAL_ALTERA_MINOR 213
Tobias Klauser6b7d8f82010-05-05 10:35:23 +020034
35/*
36 * Altera UART register definitions according to the Nios UART datasheet:
37 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
38 */
39
40#define ALTERA_UART_SIZE 32
41
42#define ALTERA_UART_RXDATA_REG 0
43#define ALTERA_UART_TXDATA_REG 4
44#define ALTERA_UART_STATUS_REG 8
45#define ALTERA_UART_CONTROL_REG 12
46#define ALTERA_UART_DIVISOR_REG 16
47#define ALTERA_UART_EOP_REG 20
48
49#define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
50#define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
51#define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
52#define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
53#define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
54#define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
55#define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
56#define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
57#define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
58#define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
59#define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
60#define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
61
62 /* Enable interrupt on... */
63#define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
64#define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
65#define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
66#define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
67#define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
68#define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
69#define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
70#define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
71#define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
72
73#define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
74#define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
75#define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
76#define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
77
78/*
79 * Local per-uart structure.
80 */
81struct altera_uart {
82 struct uart_port port;
Anton Vorontsov2f8b9c12010-10-01 17:21:33 +040083 struct timer_list tmr;
Tobias Klauser6b7d8f82010-05-05 10:35:23 +020084 unsigned int sigs; /* Local copy of line sigs */
85 unsigned short imr; /* Local IMR mirror */
86};
87
Anton Vorontsov0d426ed2010-10-01 17:21:54 +040088static u32 altera_uart_readl(struct uart_port *port, int reg)
89{
Tobias Klauser2780ad42011-02-09 10:57:04 +010090 return readl(port->membase + (reg << port->regshift));
Anton Vorontsov0d426ed2010-10-01 17:21:54 +040091}
92
93static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
94{
Tobias Klauser2780ad42011-02-09 10:57:04 +010095 writel(dat, port->membase + (reg << port->regshift));
Anton Vorontsov0d426ed2010-10-01 17:21:54 +040096}
97
Tobias Klauser6b7d8f82010-05-05 10:35:23 +020098static unsigned int altera_uart_tx_empty(struct uart_port *port)
99{
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400100 return (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200101 ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
102}
103
104static unsigned int altera_uart_get_mctrl(struct uart_port *port)
105{
106 struct altera_uart *pp = container_of(port, struct altera_uart, port);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200107 unsigned int sigs;
108
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400109 sigs = (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200110 ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
111 sigs |= (pp->sigs & TIOCM_RTS);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200112
113 return sigs;
114}
115
116static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
117{
118 struct altera_uart *pp = container_of(port, struct altera_uart, port);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200119
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200120 pp->sigs = sigs;
121 if (sigs & TIOCM_RTS)
122 pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
123 else
124 pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400125 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200126}
127
128static void altera_uart_start_tx(struct uart_port *port)
129{
130 struct altera_uart *pp = container_of(port, struct altera_uart, port);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200131
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200132 pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400133 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200134}
135
136static void altera_uart_stop_tx(struct uart_port *port)
137{
138 struct altera_uart *pp = container_of(port, struct altera_uart, port);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200139
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200140 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400141 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200142}
143
144static void altera_uart_stop_rx(struct uart_port *port)
145{
146 struct altera_uart *pp = container_of(port, struct altera_uart, port);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200147
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200148 pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400149 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200150}
151
152static void altera_uart_break_ctl(struct uart_port *port, int break_state)
153{
154 struct altera_uart *pp = container_of(port, struct altera_uart, port);
155 unsigned long flags;
156
157 spin_lock_irqsave(&port->lock, flags);
158 if (break_state == -1)
159 pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
160 else
161 pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400162 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200163 spin_unlock_irqrestore(&port->lock, flags);
164}
165
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200166static void altera_uart_set_termios(struct uart_port *port,
167 struct ktermios *termios,
168 struct ktermios *old)
169{
170 unsigned long flags;
171 unsigned int baud, baudclk;
172
173 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
174 baudclk = port->uartclk / baud;
175
176 if (old)
177 tty_termios_copy_hw(termios, old);
178 tty_termios_encode_baud_rate(termios, baud, baud);
179
180 spin_lock_irqsave(&port->lock, flags);
Anton Vorontsov2f8b9c12010-10-01 17:21:33 +0400181 uart_update_timeout(port, termios->c_cflag, baud);
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400182 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200183 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400184
185 /*
186 * FIXME: port->read_status_mask and port->ignore_status_mask
187 * need to be initialized based on termios settings for
188 * INPCK, IGNBRK, IGNPAR, PARMRK, BRKINT
189 */
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200190}
191
192static void altera_uart_rx_chars(struct altera_uart *pp)
193{
194 struct uart_port *port = &pp->port;
195 unsigned char ch, flag;
196 unsigned short status;
197
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400198 while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200199 ALTERA_UART_STATUS_RRDY_MSK) {
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400200 ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200201 flag = TTY_NORMAL;
202 port->icount.rx++;
203
204 if (status & ALTERA_UART_STATUS_E_MSK) {
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400205 altera_uart_writel(port, status,
206 ALTERA_UART_STATUS_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200207
208 if (status & ALTERA_UART_STATUS_BRK_MSK) {
209 port->icount.brk++;
210 if (uart_handle_break(port))
211 continue;
212 } else if (status & ALTERA_UART_STATUS_PE_MSK) {
213 port->icount.parity++;
214 } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
215 port->icount.overrun++;
216 } else if (status & ALTERA_UART_STATUS_FE_MSK) {
217 port->icount.frame++;
218 }
219
220 status &= port->read_status_mask;
221
222 if (status & ALTERA_UART_STATUS_BRK_MSK)
223 flag = TTY_BREAK;
224 else if (status & ALTERA_UART_STATUS_PE_MSK)
225 flag = TTY_PARITY;
226 else if (status & ALTERA_UART_STATUS_FE_MSK)
227 flag = TTY_FRAME;
228 }
229
230 if (uart_handle_sysrq_char(port, ch))
231 continue;
232 uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
233 flag);
234 }
235
Viresh Kumardd085ed2013-08-19 20:14:06 +0530236 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100237 tty_flip_buffer_push(&port->state->port);
Viresh Kumardd085ed2013-08-19 20:14:06 +0530238 spin_lock(&port->lock);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200239}
240
241static void altera_uart_tx_chars(struct altera_uart *pp)
242{
243 struct uart_port *port = &pp->port;
244 struct circ_buf *xmit = &port->state->xmit;
245
246 if (port->x_char) {
247 /* Send special char - probably flow control */
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400248 altera_uart_writel(port, port->x_char, ALTERA_UART_TXDATA_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200249 port->x_char = 0;
250 port->icount.tx++;
251 return;
252 }
253
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400254 while (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200255 ALTERA_UART_STATUS_TRDY_MSK) {
256 if (xmit->head == xmit->tail)
257 break;
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400258 altera_uart_writel(port, xmit->buf[xmit->tail],
259 ALTERA_UART_TXDATA_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200260 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
261 port->icount.tx++;
262 }
263
264 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
265 uart_write_wakeup(port);
266
267 if (xmit->head == xmit->tail) {
268 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400269 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200270 }
271}
272
273static irqreturn_t altera_uart_interrupt(int irq, void *data)
274{
275 struct uart_port *port = data;
276 struct altera_uart *pp = container_of(port, struct altera_uart, port);
277 unsigned int isr;
278
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400279 isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
Tobias Klauserd8d721f2010-05-25 16:59:55 +0200280
281 spin_lock(&port->lock);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200282 if (isr & ALTERA_UART_STATUS_RRDY_MSK)
283 altera_uart_rx_chars(pp);
284 if (isr & ALTERA_UART_STATUS_TRDY_MSK)
285 altera_uart_tx_chars(pp);
Tobias Klauserd8d721f2010-05-25 16:59:55 +0200286 spin_unlock(&port->lock);
287
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200288 return IRQ_RETVAL(isr);
289}
290
Anton Vorontsov2f8b9c12010-10-01 17:21:33 +0400291static void altera_uart_timer(unsigned long data)
292{
293 struct uart_port *port = (void *)data;
294 struct altera_uart *pp = container_of(port, struct altera_uart, port);
295
296 altera_uart_interrupt(0, port);
297 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
298}
299
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200300static void altera_uart_config_port(struct uart_port *port, int flags)
301{
302 port->type = PORT_ALTERA_UART;
303
304 /* Clear mask, so no surprise interrupts. */
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400305 altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200306 /* Clear status register */
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400307 altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200308}
309
310static int altera_uart_startup(struct uart_port *port)
311{
312 struct altera_uart *pp = container_of(port, struct altera_uart, port);
313 unsigned long flags;
314 int ret;
315
Anton Vorontsov2f8b9c12010-10-01 17:21:33 +0400316 if (!port->irq) {
317 setup_timer(&pp->tmr, altera_uart_timer, (unsigned long)port);
318 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
319 return 0;
320 }
321
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800322 ret = request_irq(port->irq, altera_uart_interrupt, 0,
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200323 DRV_NAME, port);
324 if (ret) {
325 pr_err(DRV_NAME ": unable to attach Altera UART %d "
326 "interrupt vector=%d\n", port->line, port->irq);
327 return ret;
328 }
329
330 spin_lock_irqsave(&port->lock, flags);
331
332 /* Enable RX interrupts now */
333 pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
334 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
335
336 spin_unlock_irqrestore(&port->lock, flags);
337
338 return 0;
339}
340
341static void altera_uart_shutdown(struct uart_port *port)
342{
343 struct altera_uart *pp = container_of(port, struct altera_uart, port);
344 unsigned long flags;
345
346 spin_lock_irqsave(&port->lock, flags);
347
348 /* Disable all interrupts now */
349 pp->imr = 0;
350 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
351
352 spin_unlock_irqrestore(&port->lock, flags);
353
Anton Vorontsov2f8b9c12010-10-01 17:21:33 +0400354 if (port->irq)
355 free_irq(port->irq, port);
356 else
357 del_timer_sync(&pp->tmr);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200358}
359
360static const char *altera_uart_type(struct uart_port *port)
361{
362 return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
363}
364
365static int altera_uart_request_port(struct uart_port *port)
366{
367 /* UARTs always present */
368 return 0;
369}
370
371static void altera_uart_release_port(struct uart_port *port)
372{
373 /* Nothing to release... */
374}
375
376static int altera_uart_verify_port(struct uart_port *port,
377 struct serial_struct *ser)
378{
379 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
380 return -EINVAL;
381 return 0;
382}
383
Tobias Klauser418a9362012-02-08 14:36:09 +0100384#ifdef CONFIG_CONSOLE_POLL
385static int altera_uart_poll_get_char(struct uart_port *port)
386{
387 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
388 ALTERA_UART_STATUS_RRDY_MSK))
389 cpu_relax();
390
391 return altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
392}
393
394static void altera_uart_poll_put_char(struct uart_port *port, unsigned char c)
395{
396 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
397 ALTERA_UART_STATUS_TRDY_MSK))
398 cpu_relax();
399
400 altera_uart_writel(port, c, ALTERA_UART_TXDATA_REG);
401}
402#endif
403
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200404/*
405 * Define the basic serial functions we support.
406 */
407static struct uart_ops altera_uart_ops = {
408 .tx_empty = altera_uart_tx_empty,
409 .get_mctrl = altera_uart_get_mctrl,
410 .set_mctrl = altera_uart_set_mctrl,
411 .start_tx = altera_uart_start_tx,
412 .stop_tx = altera_uart_stop_tx,
413 .stop_rx = altera_uart_stop_rx,
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200414 .break_ctl = altera_uart_break_ctl,
415 .startup = altera_uart_startup,
416 .shutdown = altera_uart_shutdown,
417 .set_termios = altera_uart_set_termios,
418 .type = altera_uart_type,
419 .request_port = altera_uart_request_port,
420 .release_port = altera_uart_release_port,
421 .config_port = altera_uart_config_port,
422 .verify_port = altera_uart_verify_port,
Tobias Klauser418a9362012-02-08 14:36:09 +0100423#ifdef CONFIG_CONSOLE_POLL
424 .poll_get_char = altera_uart_poll_get_char,
425 .poll_put_char = altera_uart_poll_put_char,
426#endif
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200427};
428
429static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
430
431#if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
432
Daniel Thompson2970b7f2014-07-11 14:00:11 +0100433static void altera_uart_console_putc(struct uart_port *port, int c)
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200434{
Anton Vorontsov0d426ed2010-10-01 17:21:54 +0400435 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
Andrew Mortone8dd4752010-07-20 15:26:10 -0700436 ALTERA_UART_STATUS_TRDY_MSK))
Tobias Klauserfadf34f2010-05-25 17:00:08 +0200437 cpu_relax();
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200438
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200439 writel(c, port->membase + ALTERA_UART_TXDATA_REG);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200440}
441
442static void altera_uart_console_write(struct console *co, const char *s,
443 unsigned int count)
444{
Tobias Klauserfadf34f2010-05-25 17:00:08 +0200445 struct uart_port *port = &(altera_uart_ports + co->index)->port;
446
Daniel Thompson2970b7f2014-07-11 14:00:11 +0100447 uart_console_write(port, s, count, altera_uart_console_putc);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200448}
449
450static int __init altera_uart_console_setup(struct console *co, char *options)
451{
452 struct uart_port *port;
453 int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
454 int bits = 8;
455 int parity = 'n';
456 int flow = 'n';
457
458 if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
459 return -EINVAL;
460 port = &altera_uart_ports[co->index].port;
Anton Vorontsov70eebd02010-10-01 17:23:13 +0400461 if (!port->membase)
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200462 return -ENODEV;
463
464 if (options)
465 uart_parse_options(options, &baud, &parity, &bits, &flow);
466
467 return uart_set_options(port, co, baud, parity, bits, flow);
468}
469
470static struct uart_driver altera_uart_driver;
471
472static struct console altera_uart_console = {
Anton Vorontsov99793c62010-10-01 18:23:24 +0400473 .name = "ttyAL",
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200474 .write = altera_uart_console_write,
475 .device = uart_console_device,
476 .setup = altera_uart_console_setup,
477 .flags = CON_PRINTBUFFER,
478 .index = -1,
479 .data = &altera_uart_driver,
480};
481
482static int __init altera_uart_console_init(void)
483{
484 register_console(&altera_uart_console);
485 return 0;
486}
487
488console_initcall(altera_uart_console_init);
489
490#define ALTERA_UART_CONSOLE (&altera_uart_console)
491
492#else
493
494#define ALTERA_UART_CONSOLE NULL
495
Valentin Rothberg0656b1a2015-04-11 12:21:30 +0200496#endif /* CONFIG_SERIAL_ALTERA_UART_CONSOLE */
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200497
498/*
499 * Define the altera_uart UART driver structure.
500 */
501static struct uart_driver altera_uart_driver = {
502 .owner = THIS_MODULE,
503 .driver_name = DRV_NAME,
Anton Vorontsov99793c62010-10-01 18:23:24 +0400504 .dev_name = "ttyAL",
505 .major = SERIAL_ALTERA_MAJOR,
506 .minor = SERIAL_ALTERA_MINOR,
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200507 .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
508 .cons = ALTERA_UART_CONSOLE,
509};
510
Bill Pemberton9671f092012-11-19 13:21:50 -0500511static int altera_uart_probe(struct platform_device *pdev)
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200512{
Jingoo Han574de552013-07-30 17:06:57 +0900513 struct altera_uart_platform_uart *platp = dev_get_platdata(&pdev->dev);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200514 struct uart_port *port;
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400515 struct resource *res_mem;
516 struct resource *res_irq;
517 int i = pdev->id;
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100518 int ret;
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200519
Tobias Klausera664ec92011-03-28 13:57:11 +0200520 /* if id is -1 scan for a free id and use that one */
521 if (i == -1) {
522 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS; i++)
523 if (altera_uart_ports[i].port.mapbase == 0)
524 break;
525 }
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200526
Tobias Klausera664ec92011-03-28 13:57:11 +0200527 if (i < 0 || i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400528 return -EINVAL;
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200529
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400530 port = &altera_uart_ports[i].port;
531
532 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
533 if (res_mem)
534 port->mapbase = res_mem->start;
Yuriy Kozlovacede702012-03-29 09:55:27 +0200535 else if (platp)
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400536 port->mapbase = platp->mapbase;
537 else
538 return -EINVAL;
539
540 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
541 if (res_irq)
542 port->irq = res_irq->start;
Yuriy Kozlovacede702012-03-29 09:55:27 +0200543 else if (platp)
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400544 port->irq = platp->irq;
545
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100546 /* Check platform data first so we can override device node data */
547 if (platp)
548 port->uartclk = platp->uartclk;
549 else {
Tobias Klauserd144aff2015-09-09 20:34:12 +0200550 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
551 &port->uartclk);
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100552 if (ret)
553 return ret;
554 }
555
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400556 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
557 if (!port->membase)
558 return -ENOMEM;
559
Tobias Klauser2780ad42011-02-09 10:57:04 +0100560 if (platp)
561 port->regshift = platp->bus_shift;
562 else
563 port->regshift = 0;
564
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400565 port->line = i;
566 port->type = PORT_ALTERA_UART;
567 port->iotype = SERIAL_IO_MEM;
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400568 port->ops = &altera_uart_ops;
Anton Vorontsov288e9fe2010-10-01 17:22:37 +0400569 port->flags = UPF_BOOT_AUTOCONF;
Ley Foon Tanb820cd72014-12-16 15:28:34 +0800570 port->dev = &pdev->dev;
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400571
Tobias Klauser1a16afa2012-08-20 15:56:34 +0200572 platform_set_drvdata(pdev, port);
Tobias Klausera664ec92011-03-28 13:57:11 +0200573
Anton Vorontsov6b5756f2010-10-01 17:21:42 +0400574 uart_add_one_port(&altera_uart_driver, port);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200575
576 return 0;
577}
578
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500579static int altera_uart_remove(struct platform_device *pdev)
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200580{
Tobias Klauser1a16afa2012-08-20 15:56:34 +0200581 struct uart_port *port = platform_get_drvdata(pdev);
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200582
Tobias Klausera664ec92011-03-28 13:57:11 +0200583 if (port) {
584 uart_remove_one_port(&altera_uart_driver, port);
Tobias Klausera664ec92011-03-28 13:57:11 +0200585 port->mapbase = 0;
586 }
Tobias Klausere96fabd2011-02-09 10:56:52 +0100587
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200588 return 0;
589}
590
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100591#ifdef CONFIG_OF
Tobias Klauser4d199a52014-08-05 09:14:35 +0200592static const struct of_device_id altera_uart_match[] = {
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100593 { .compatible = "ALTR,uart-1.0", },
Dinh Nguyen13960b42013-08-14 15:25:19 -0500594 { .compatible = "altr,uart-1.0", },
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100595 {},
596};
597MODULE_DEVICE_TABLE(of, altera_uart_match);
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100598#endif /* CONFIG_OF */
599
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200600static struct platform_driver altera_uart_platform_driver = {
601 .probe = altera_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500602 .remove = altera_uart_remove,
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200603 .driver = {
Tobias Klauser7c9325d2011-02-18 11:35:32 +0100604 .name = DRV_NAME,
Ben Dooks85888062011-08-03 10:11:43 +0100605 .of_match_table = of_match_ptr(altera_uart_match),
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200606 },
607};
608
609static int __init altera_uart_init(void)
610{
611 int rc;
612
613 rc = uart_register_driver(&altera_uart_driver);
614 if (rc)
615 return rc;
616 rc = platform_driver_register(&altera_uart_platform_driver);
Tobias Klauser61bc6552013-02-05 18:07:42 +0100617 if (rc)
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200618 uart_unregister_driver(&altera_uart_driver);
Tobias Klauser61bc6552013-02-05 18:07:42 +0100619 return rc;
Tobias Klauser6b7d8f82010-05-05 10:35:23 +0200620}
621
622static void __exit altera_uart_exit(void)
623{
624 platform_driver_unregister(&altera_uart_platform_driver);
625 uart_unregister_driver(&altera_uart_driver);
626}
627
628module_init(altera_uart_init);
629module_exit(altera_uart_exit);
630
631MODULE_DESCRIPTION("Altera UART driver");
632MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
633MODULE_LICENSE("GPL");
634MODULE_ALIAS("platform:" DRV_NAME);
Anton Vorontsov99793c62010-10-01 18:23:24 +0400635MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_ALTERA_MAJOR);