blob: 97e16db32468842c6778a57ad8046f5e6f8b7c2f [file] [log] [blame]
Carlo Caioneff7693d2014-08-17 12:49:49 +02001/*
2 * Based on meson_uart.c, by AMLOGIC, INC.
3 *
4 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/clk.h>
18#include <linux/console.h>
19#include <linux/delay.h>
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/of.h>
25#include <linux/platform_device.h>
26#include <linux/serial.h>
27#include <linux/serial_core.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30
31/* Register offsets */
32#define AML_UART_WFIFO 0x00
33#define AML_UART_RFIFO 0x04
34#define AML_UART_CONTROL 0x08
35#define AML_UART_STATUS 0x0c
36#define AML_UART_MISC 0x10
37#define AML_UART_REG5 0x14
38
39/* AML_UART_CONTROL bits */
40#define AML_UART_TX_EN BIT(12)
41#define AML_UART_RX_EN BIT(13)
42#define AML_UART_TX_RST BIT(22)
43#define AML_UART_RX_RST BIT(23)
44#define AML_UART_CLR_ERR BIT(24)
45#define AML_UART_RX_INT_EN BIT(27)
46#define AML_UART_TX_INT_EN BIT(28)
47#define AML_UART_DATA_LEN_MASK (0x03 << 20)
48#define AML_UART_DATA_LEN_8BIT (0x00 << 20)
49#define AML_UART_DATA_LEN_7BIT (0x01 << 20)
50#define AML_UART_DATA_LEN_6BIT (0x02 << 20)
51#define AML_UART_DATA_LEN_5BIT (0x03 << 20)
52
53/* AML_UART_STATUS bits */
54#define AML_UART_PARITY_ERR BIT(16)
55#define AML_UART_FRAME_ERR BIT(17)
56#define AML_UART_TX_FIFO_WERR BIT(18)
57#define AML_UART_RX_EMPTY BIT(20)
58#define AML_UART_TX_FULL BIT(21)
59#define AML_UART_TX_EMPTY BIT(22)
Ben Dooks88679732015-11-18 14:41:13 +000060#define AML_UART_XMIT_BUSY BIT(25)
Carlo Caioneff7693d2014-08-17 12:49:49 +020061#define AML_UART_ERR (AML_UART_PARITY_ERR | \
62 AML_UART_FRAME_ERR | \
63 AML_UART_TX_FIFO_WERR)
64
65/* AML_UART_CONTROL bits */
66#define AML_UART_TWO_WIRE_EN BIT(15)
67#define AML_UART_PARITY_TYPE BIT(18)
68#define AML_UART_PARITY_EN BIT(19)
69#define AML_UART_CLEAR_ERR BIT(24)
70#define AML_UART_STOP_BIN_LEN_MASK (0x03 << 16)
71#define AML_UART_STOP_BIN_1SB (0x00 << 16)
72#define AML_UART_STOP_BIN_2SB (0x01 << 16)
73
74/* AML_UART_MISC bits */
75#define AML_UART_XMIT_IRQ(c) (((c) & 0xff) << 8)
76#define AML_UART_RECV_IRQ(c) ((c) & 0xff)
77
78/* AML_UART_REG5 bits */
79#define AML_UART_BAUD_MASK 0x7fffff
80#define AML_UART_BAUD_USE BIT(23)
Andreas Färber146f3802016-02-08 13:49:42 +010081#define AML_UART_BAUD_XTAL BIT(24)
Carlo Caioneff7693d2014-08-17 12:49:49 +020082
83#define AML_UART_PORT_NUM 6
84#define AML_UART_DEV_NAME "ttyAML"
85
86
87static struct uart_driver meson_uart_driver;
88
89static struct uart_port *meson_ports[AML_UART_PORT_NUM];
90
91static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
92{
93}
94
95static unsigned int meson_uart_get_mctrl(struct uart_port *port)
96{
97 return TIOCM_CTS;
98}
99
100static unsigned int meson_uart_tx_empty(struct uart_port *port)
101{
102 u32 val;
103
104 val = readl(port->membase + AML_UART_STATUS);
Ben Dooks88679732015-11-18 14:41:13 +0000105 val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
106 return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200107}
108
109static void meson_uart_stop_tx(struct uart_port *port)
110{
111 u32 val;
112
113 val = readl(port->membase + AML_UART_CONTROL);
Ben Dooks855ddca2015-11-18 14:41:15 +0000114 val &= ~AML_UART_TX_INT_EN;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200115 writel(val, port->membase + AML_UART_CONTROL);
116}
117
118static void meson_uart_stop_rx(struct uart_port *port)
119{
120 u32 val;
121
122 val = readl(port->membase + AML_UART_CONTROL);
123 val &= ~AML_UART_RX_EN;
124 writel(val, port->membase + AML_UART_CONTROL);
125}
126
Heiner Kallweitba50f1d2017-04-19 22:17:44 +0200127static void meson_uart_enable_tx_engine(struct uart_port *port)
128{
129 u32 val;
130
131 val = readl(port->membase + AML_UART_CONTROL);
132 val |= AML_UART_TX_EN;
133 writel(val, port->membase + AML_UART_CONTROL);
134}
135
Carlo Caioneff7693d2014-08-17 12:49:49 +0200136static void meson_uart_shutdown(struct uart_port *port)
137{
138 unsigned long flags;
139 u32 val;
140
141 free_irq(port->irq, port);
142
143 spin_lock_irqsave(&port->lock, flags);
144
145 val = readl(port->membase + AML_UART_CONTROL);
Ben Dooks855ddca2015-11-18 14:41:15 +0000146 val &= ~AML_UART_RX_EN;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200147 val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
148 writel(val, port->membase + AML_UART_CONTROL);
149
150 spin_unlock_irqrestore(&port->lock, flags);
151}
152
153static void meson_uart_start_tx(struct uart_port *port)
154{
155 struct circ_buf *xmit = &port->state->xmit;
156 unsigned int ch;
Ben Dooksf1dd05c2015-11-18 14:41:18 +0000157 u32 val;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200158
159 if (uart_tx_stopped(port)) {
160 meson_uart_stop_tx(port);
161 return;
162 }
163
164 while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
165 if (port->x_char) {
166 writel(port->x_char, port->membase + AML_UART_WFIFO);
167 port->icount.tx++;
168 port->x_char = 0;
169 continue;
170 }
171
172 if (uart_circ_empty(xmit))
173 break;
174
175 ch = xmit->buf[xmit->tail];
176 writel(ch, port->membase + AML_UART_WFIFO);
177 xmit->tail = (xmit->tail+1) & (SERIAL_XMIT_SIZE - 1);
178 port->icount.tx++;
179 }
180
Ben Dooksf1dd05c2015-11-18 14:41:18 +0000181 if (!uart_circ_empty(xmit)) {
182 val = readl(port->membase + AML_UART_CONTROL);
183 val |= AML_UART_TX_INT_EN;
184 writel(val, port->membase + AML_UART_CONTROL);
185 }
186
Carlo Caioneff7693d2014-08-17 12:49:49 +0200187 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
188 uart_write_wakeup(port);
189}
190
191static void meson_receive_chars(struct uart_port *port)
192{
193 struct tty_port *tport = &port->state->port;
194 char flag;
195 u32 status, ch, mode;
196
197 do {
198 flag = TTY_NORMAL;
199 port->icount.rx++;
200 status = readl(port->membase + AML_UART_STATUS);
201
202 if (status & AML_UART_ERR) {
203 if (status & AML_UART_TX_FIFO_WERR)
204 port->icount.overrun++;
205 else if (status & AML_UART_FRAME_ERR)
206 port->icount.frame++;
207 else if (status & AML_UART_PARITY_ERR)
208 port->icount.frame++;
209
210 mode = readl(port->membase + AML_UART_CONTROL);
211 mode |= AML_UART_CLEAR_ERR;
212 writel(mode, port->membase + AML_UART_CONTROL);
213
214 /* It doesn't clear to 0 automatically */
215 mode &= ~AML_UART_CLEAR_ERR;
216 writel(mode, port->membase + AML_UART_CONTROL);
217
218 status &= port->read_status_mask;
219 if (status & AML_UART_FRAME_ERR)
220 flag = TTY_FRAME;
221 else if (status & AML_UART_PARITY_ERR)
222 flag = TTY_PARITY;
223 }
224
225 ch = readl(port->membase + AML_UART_RFIFO);
226 ch &= 0xff;
227
228 if ((status & port->ignore_status_mask) == 0)
229 tty_insert_flip_char(tport, ch, flag);
230
231 if (status & AML_UART_TX_FIFO_WERR)
232 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
233
234 } while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
235
236 spin_unlock(&port->lock);
237 tty_flip_buffer_push(tport);
238 spin_lock(&port->lock);
239}
240
241static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
242{
243 struct uart_port *port = (struct uart_port *)dev_id;
244
245 spin_lock(&port->lock);
246
247 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
248 meson_receive_chars(port);
249
Ben Dooks39469652015-11-18 14:41:19 +0000250 if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
251 if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
252 meson_uart_start_tx(port);
253 }
Carlo Caioneff7693d2014-08-17 12:49:49 +0200254
255 spin_unlock(&port->lock);
256
257 return IRQ_HANDLED;
258}
259
260static const char *meson_uart_type(struct uart_port *port)
261{
262 return (port->type == PORT_MESON) ? "meson_uart" : NULL;
263}
264
Ben Dooks00661dd2015-11-18 14:41:12 +0000265static void meson_uart_reset(struct uart_port *port)
Carlo Caioneff7693d2014-08-17 12:49:49 +0200266{
267 u32 val;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200268
269 val = readl(port->membase + AML_UART_CONTROL);
270 val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
271 writel(val, port->membase + AML_UART_CONTROL);
272
273 val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
274 writel(val, port->membase + AML_UART_CONTROL);
Ben Dooks00661dd2015-11-18 14:41:12 +0000275}
276
277static int meson_uart_startup(struct uart_port *port)
278{
279 u32 val;
280 int ret = 0;
281
282 val = readl(port->membase + AML_UART_CONTROL);
283 val |= AML_UART_CLR_ERR;
284 writel(val, port->membase + AML_UART_CONTROL);
285 val &= ~AML_UART_CLR_ERR;
286 writel(val, port->membase + AML_UART_CONTROL);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200287
288 val |= (AML_UART_RX_EN | AML_UART_TX_EN);
289 writel(val, port->membase + AML_UART_CONTROL);
290
291 val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
292 writel(val, port->membase + AML_UART_CONTROL);
293
294 val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
295 writel(val, port->membase + AML_UART_MISC);
296
297 ret = request_irq(port->irq, meson_uart_interrupt, 0,
298 meson_uart_type(port), port);
299
300 return ret;
301}
302
303static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
304{
305 u32 val;
306
Ben Dooksf1f5c142015-11-18 14:41:16 +0000307 while (!meson_uart_tx_empty(port))
Carlo Caioneff7693d2014-08-17 12:49:49 +0200308 cpu_relax();
309
310 val = readl(port->membase + AML_UART_REG5);
311 val &= ~AML_UART_BAUD_MASK;
Andreas Färber146f3802016-02-08 13:49:42 +0100312 if (port->uartclk == 24000000) {
313 val = ((port->uartclk / 3) / baud) - 1;
314 val |= AML_UART_BAUD_XTAL;
315 } else {
316 val = ((port->uartclk * 10 / (baud * 4) + 5) / 10) - 1;
317 }
Carlo Caioneff7693d2014-08-17 12:49:49 +0200318 val |= AML_UART_BAUD_USE;
319 writel(val, port->membase + AML_UART_REG5);
320}
321
322static void meson_uart_set_termios(struct uart_port *port,
323 struct ktermios *termios,
324 struct ktermios *old)
325{
326 unsigned int cflags, iflags, baud;
327 unsigned long flags;
328 u32 val;
329
330 spin_lock_irqsave(&port->lock, flags);
331
332 cflags = termios->c_cflag;
333 iflags = termios->c_iflag;
334
335 val = readl(port->membase + AML_UART_CONTROL);
336
337 val &= ~AML_UART_DATA_LEN_MASK;
338 switch (cflags & CSIZE) {
339 case CS8:
340 val |= AML_UART_DATA_LEN_8BIT;
341 break;
342 case CS7:
343 val |= AML_UART_DATA_LEN_7BIT;
344 break;
345 case CS6:
346 val |= AML_UART_DATA_LEN_6BIT;
347 break;
348 case CS5:
349 val |= AML_UART_DATA_LEN_5BIT;
350 break;
351 }
352
353 if (cflags & PARENB)
354 val |= AML_UART_PARITY_EN;
355 else
356 val &= ~AML_UART_PARITY_EN;
357
358 if (cflags & PARODD)
359 val |= AML_UART_PARITY_TYPE;
360 else
361 val &= ~AML_UART_PARITY_TYPE;
362
363 val &= ~AML_UART_STOP_BIN_LEN_MASK;
364 if (cflags & CSTOPB)
365 val |= AML_UART_STOP_BIN_2SB;
366 else
Heiner Kallweit88f37d72017-04-19 22:17:24 +0200367 val |= AML_UART_STOP_BIN_1SB;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200368
369 if (cflags & CRTSCTS)
370 val &= ~AML_UART_TWO_WIRE_EN;
371 else
372 val |= AML_UART_TWO_WIRE_EN;
373
374 writel(val, port->membase + AML_UART_CONTROL);
375
Martin Blumenstingl8c9faa52017-01-15 23:32:52 +0100376 baud = uart_get_baud_rate(port, termios, old, 9600, 4000000);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200377 meson_uart_change_speed(port, baud);
378
379 port->read_status_mask = AML_UART_TX_FIFO_WERR;
380 if (iflags & INPCK)
381 port->read_status_mask |= AML_UART_PARITY_ERR |
382 AML_UART_FRAME_ERR;
383
384 port->ignore_status_mask = 0;
385 if (iflags & IGNPAR)
386 port->ignore_status_mask |= AML_UART_PARITY_ERR |
387 AML_UART_FRAME_ERR;
388
389 uart_update_timeout(port, termios->c_cflag, baud);
390 spin_unlock_irqrestore(&port->lock, flags);
391}
392
393static int meson_uart_verify_port(struct uart_port *port,
394 struct serial_struct *ser)
395{
396 int ret = 0;
397
398 if (port->type != PORT_MESON)
399 ret = -EINVAL;
400 if (port->irq != ser->irq)
401 ret = -EINVAL;
402 if (ser->baud_base < 9600)
403 ret = -EINVAL;
404 return ret;
405}
406
Ben Dooks1bc1f172015-11-18 14:41:11 +0000407static int meson_uart_res_size(struct uart_port *port)
408{
409 struct platform_device *pdev = to_platform_device(port->dev);
410 struct resource *res;
411
412 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
413 if (!res) {
414 dev_err(port->dev, "cannot obtain I/O memory region");
415 return -ENODEV;
416 }
417
418 return resource_size(res);
419}
420
Carlo Caioneff7693d2014-08-17 12:49:49 +0200421static void meson_uart_release_port(struct uart_port *port)
422{
Ben Dooks1bc1f172015-11-18 14:41:11 +0000423 int size = meson_uart_res_size(port);
424
Carlo Caioneff7693d2014-08-17 12:49:49 +0200425 if (port->flags & UPF_IOREMAP) {
Ben Dooks1bc1f172015-11-18 14:41:11 +0000426 devm_release_mem_region(port->dev, port->mapbase, size);
Firo Yangc547630f2015-04-26 18:46:06 +0800427 devm_iounmap(port->dev, port->membase);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200428 port->membase = NULL;
429 }
430}
431
432static int meson_uart_request_port(struct uart_port *port)
433{
Ben Dooks1bc1f172015-11-18 14:41:11 +0000434 int size = meson_uart_res_size(port);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200435
Ben Dooks1bc1f172015-11-18 14:41:11 +0000436 if (size < 0)
437 return size;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200438
439 if (!devm_request_mem_region(port->dev, port->mapbase, size,
440 dev_name(port->dev))) {
441 dev_err(port->dev, "Memory region busy\n");
442 return -EBUSY;
443 }
444
445 if (port->flags & UPF_IOREMAP) {
446 port->membase = devm_ioremap_nocache(port->dev,
447 port->mapbase,
448 size);
449 if (port->membase == NULL)
450 return -ENOMEM;
451 }
452
453 return 0;
454}
455
456static void meson_uart_config_port(struct uart_port *port, int flags)
457{
458 if (flags & UART_CONFIG_TYPE) {
459 port->type = PORT_MESON;
460 meson_uart_request_port(port);
461 }
462}
463
464static struct uart_ops meson_uart_ops = {
465 .set_mctrl = meson_uart_set_mctrl,
466 .get_mctrl = meson_uart_get_mctrl,
467 .tx_empty = meson_uart_tx_empty,
468 .start_tx = meson_uart_start_tx,
469 .stop_tx = meson_uart_stop_tx,
470 .stop_rx = meson_uart_stop_rx,
471 .startup = meson_uart_startup,
472 .shutdown = meson_uart_shutdown,
473 .set_termios = meson_uart_set_termios,
474 .type = meson_uart_type,
475 .config_port = meson_uart_config_port,
476 .request_port = meson_uart_request_port,
477 .release_port = meson_uart_release_port,
478 .verify_port = meson_uart_verify_port,
479};
480
481#ifdef CONFIG_SERIAL_MESON_CONSOLE
482
483static void meson_console_putchar(struct uart_port *port, int ch)
484{
485 if (!port->membase)
486 return;
487
488 while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
489 cpu_relax();
490 writel(ch, port->membase + AML_UART_WFIFO);
491}
492
Andreas Färber736d5532016-03-06 12:21:24 +0100493static void meson_serial_port_write(struct uart_port *port, const char *s,
494 u_int count)
Carlo Caioneff7693d2014-08-17 12:49:49 +0200495{
Carlo Caioneff7693d2014-08-17 12:49:49 +0200496 unsigned long flags;
497 int locked;
Ben Dooks2561f062015-11-18 14:41:17 +0000498 u32 val, tmp;
Carlo Caioneff7693d2014-08-17 12:49:49 +0200499
Carlo Caioneff7693d2014-08-17 12:49:49 +0200500 local_irq_save(flags);
501 if (port->sysrq) {
502 locked = 0;
503 } else if (oops_in_progress) {
504 locked = spin_trylock(&port->lock);
505 } else {
506 spin_lock(&port->lock);
507 locked = 1;
508 }
509
Ben Dooks41788f02015-11-18 14:41:14 +0000510 val = readl(port->membase + AML_UART_CONTROL);
Ben Dooks2561f062015-11-18 14:41:17 +0000511 tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
512 writel(tmp, port->membase + AML_UART_CONTROL);
Ben Dooks41788f02015-11-18 14:41:14 +0000513
Carlo Caioneff7693d2014-08-17 12:49:49 +0200514 uart_console_write(port, s, count, meson_console_putchar);
Ben Dooks2561f062015-11-18 14:41:17 +0000515 writel(val, port->membase + AML_UART_CONTROL);
Carlo Caioneff7693d2014-08-17 12:49:49 +0200516
517 if (locked)
518 spin_unlock(&port->lock);
519 local_irq_restore(flags);
520}
521
Andreas Färber736d5532016-03-06 12:21:24 +0100522static void meson_serial_console_write(struct console *co, const char *s,
523 u_int count)
524{
525 struct uart_port *port;
526
527 port = meson_ports[co->index];
528 if (!port)
529 return;
530
531 meson_serial_port_write(port, s, count);
532}
533
Carlo Caioneff7693d2014-08-17 12:49:49 +0200534static int meson_serial_console_setup(struct console *co, char *options)
535{
536 struct uart_port *port;
537 int baud = 115200;
538 int bits = 8;
539 int parity = 'n';
540 int flow = 'n';
541
542 if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
543 return -EINVAL;
544
545 port = meson_ports[co->index];
546 if (!port || !port->membase)
547 return -ENODEV;
548
Heiner Kallweitba50f1d2017-04-19 22:17:44 +0200549 meson_uart_enable_tx_engine(port);
550
Carlo Caioneff7693d2014-08-17 12:49:49 +0200551 if (options)
552 uart_parse_options(options, &baud, &parity, &bits, &flow);
553
554 return uart_set_options(port, co, baud, parity, bits, flow);
555}
556
557static struct console meson_serial_console = {
558 .name = AML_UART_DEV_NAME,
559 .write = meson_serial_console_write,
560 .device = uart_console_device,
561 .setup = meson_serial_console_setup,
562 .flags = CON_PRINTBUFFER,
563 .index = -1,
564 .data = &meson_uart_driver,
565};
566
567static int __init meson_serial_console_init(void)
568{
569 register_console(&meson_serial_console);
570 return 0;
571}
572console_initcall(meson_serial_console_init);
573
Andreas Färber736d5532016-03-06 12:21:24 +0100574static void meson_serial_early_console_write(struct console *co,
575 const char *s,
576 u_int count)
577{
578 struct earlycon_device *dev = co->data;
579
580 meson_serial_port_write(&dev->port, s, count);
581}
582
583static int __init
584meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
585{
586 if (!device->port.membase)
587 return -ENODEV;
588
Heiner Kallweitba50f1d2017-04-19 22:17:44 +0200589 meson_uart_enable_tx_engine(&device->port);
Andreas Färber736d5532016-03-06 12:21:24 +0100590 device->con->write = meson_serial_early_console_write;
591 return 0;
592}
593OF_EARLYCON_DECLARE(meson, "amlogic,meson-uart",
594 meson_serial_early_console_setup);
595
Carlo Caioneff7693d2014-08-17 12:49:49 +0200596#define MESON_SERIAL_CONSOLE (&meson_serial_console)
597#else
598#define MESON_SERIAL_CONSOLE NULL
599#endif
600
601static struct uart_driver meson_uart_driver = {
602 .owner = THIS_MODULE,
603 .driver_name = "meson_uart",
604 .dev_name = AML_UART_DEV_NAME,
605 .nr = AML_UART_PORT_NUM,
606 .cons = MESON_SERIAL_CONSOLE,
607};
608
609static int meson_uart_probe(struct platform_device *pdev)
610{
611 struct resource *res_mem, *res_irq;
612 struct uart_port *port;
613 struct clk *clk;
614 int ret = 0;
615
616 if (pdev->dev.of_node)
617 pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
618
619 if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
620 return -EINVAL;
621
622 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
623 if (!res_mem)
624 return -ENODEV;
625
626 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
627 if (!res_irq)
628 return -ENODEV;
629
630 if (meson_ports[pdev->id]) {
631 dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
632 return -EBUSY;
633 }
634
635 port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
636 if (!port)
637 return -ENOMEM;
638
639 clk = clk_get(&pdev->dev, NULL);
640 if (IS_ERR(clk))
641 return PTR_ERR(clk);
642
643 port->uartclk = clk_get_rate(clk);
644 port->iotype = UPIO_MEM;
645 port->mapbase = res_mem->start;
646 port->irq = res_irq->start;
647 port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP | UPF_LOW_LATENCY;
648 port->dev = &pdev->dev;
649 port->line = pdev->id;
650 port->type = PORT_MESON;
651 port->x_char = 0;
652 port->ops = &meson_uart_ops;
653 port->fifosize = 64;
654
655 meson_ports[pdev->id] = port;
656 platform_set_drvdata(pdev, port);
657
Ben Dooks00661dd2015-11-18 14:41:12 +0000658 /* reset port before registering (and possibly registering console) */
659 if (meson_uart_request_port(port) >= 0) {
660 meson_uart_reset(port);
661 meson_uart_release_port(port);
662 }
663
Carlo Caioneff7693d2014-08-17 12:49:49 +0200664 ret = uart_add_one_port(&meson_uart_driver, port);
665 if (ret)
666 meson_ports[pdev->id] = NULL;
667
668 return ret;
669}
670
671static int meson_uart_remove(struct platform_device *pdev)
672{
673 struct uart_port *port;
674
675 port = platform_get_drvdata(pdev);
676 uart_remove_one_port(&meson_uart_driver, port);
677 meson_ports[pdev->id] = NULL;
678
679 return 0;
680}
681
682
683static const struct of_device_id meson_uart_dt_match[] = {
684 { .compatible = "amlogic,meson-uart" },
685 { /* sentinel */ },
686};
687MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
688
689static struct platform_driver meson_uart_platform_driver = {
690 .probe = meson_uart_probe,
691 .remove = meson_uart_remove,
692 .driver = {
Carlo Caioneff7693d2014-08-17 12:49:49 +0200693 .name = "meson_uart",
694 .of_match_table = meson_uart_dt_match,
695 },
696};
697
698static int __init meson_uart_init(void)
699{
700 int ret;
701
702 ret = uart_register_driver(&meson_uart_driver);
703 if (ret)
704 return ret;
705
706 ret = platform_driver_register(&meson_uart_platform_driver);
707 if (ret)
708 uart_unregister_driver(&meson_uart_driver);
709
710 return ret;
711}
712
713static void __exit meson_uart_exit(void)
714{
715 platform_driver_unregister(&meson_uart_platform_driver);
716 uart_unregister_driver(&meson_uart_driver);
717}
718
719module_init(meson_uart_init);
720module_exit(meson_uart_exit);
721
722MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
723MODULE_DESCRIPTION("Amlogic Meson serial port driver");
724MODULE_LICENSE("GPL v2");