GNU Linux-libre 4.14.294-gnu1
[releases.git] / drivers / tty / serial / meson_uart.c
1 /*
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)
60 #define AML_UART_XMIT_BUSY              BIT(25)
61 #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)
81 #define AML_UART_BAUD_XTAL              BIT(24)
82
83 #define AML_UART_PORT_NUM               6
84 #define AML_UART_DEV_NAME               "ttyAML"
85
86
87 static struct uart_driver meson_uart_driver;
88
89 static struct uart_port *meson_ports[AML_UART_PORT_NUM];
90
91 static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
92 {
93 }
94
95 static unsigned int meson_uart_get_mctrl(struct uart_port *port)
96 {
97         return TIOCM_CTS;
98 }
99
100 static unsigned int meson_uart_tx_empty(struct uart_port *port)
101 {
102         u32 val;
103
104         val = readl(port->membase + AML_UART_STATUS);
105         val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
106         return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
107 }
108
109 static void meson_uart_stop_tx(struct uart_port *port)
110 {
111         u32 val;
112
113         val = readl(port->membase + AML_UART_CONTROL);
114         val &= ~AML_UART_TX_INT_EN;
115         writel(val, port->membase + AML_UART_CONTROL);
116 }
117
118 static 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
127 static void meson_uart_shutdown(struct uart_port *port)
128 {
129         unsigned long flags;
130         u32 val;
131
132         free_irq(port->irq, port);
133
134         spin_lock_irqsave(&port->lock, flags);
135
136         val = readl(port->membase + AML_UART_CONTROL);
137         val &= ~AML_UART_RX_EN;
138         val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
139         writel(val, port->membase + AML_UART_CONTROL);
140
141         spin_unlock_irqrestore(&port->lock, flags);
142 }
143
144 static void meson_uart_start_tx(struct uart_port *port)
145 {
146         struct circ_buf *xmit = &port->state->xmit;
147         unsigned int ch;
148         u32 val;
149
150         if (uart_tx_stopped(port)) {
151                 meson_uart_stop_tx(port);
152                 return;
153         }
154
155         while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
156                 if (port->x_char) {
157                         writel(port->x_char, port->membase + AML_UART_WFIFO);
158                         port->icount.tx++;
159                         port->x_char = 0;
160                         continue;
161                 }
162
163                 if (uart_circ_empty(xmit))
164                         break;
165
166                 ch = xmit->buf[xmit->tail];
167                 writel(ch, port->membase + AML_UART_WFIFO);
168                 xmit->tail = (xmit->tail+1) & (SERIAL_XMIT_SIZE - 1);
169                 port->icount.tx++;
170         }
171
172         if (!uart_circ_empty(xmit)) {
173                 val = readl(port->membase + AML_UART_CONTROL);
174                 val |= AML_UART_TX_INT_EN;
175                 writel(val, port->membase + AML_UART_CONTROL);
176         }
177
178         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
179                 uart_write_wakeup(port);
180 }
181
182 static void meson_receive_chars(struct uart_port *port)
183 {
184         struct tty_port *tport = &port->state->port;
185         char flag;
186         u32 status, ch, mode;
187
188         do {
189                 flag = TTY_NORMAL;
190                 port->icount.rx++;
191                 status = readl(port->membase + AML_UART_STATUS);
192
193                 if (status & AML_UART_ERR) {
194                         if (status & AML_UART_TX_FIFO_WERR)
195                                 port->icount.overrun++;
196                         else if (status & AML_UART_FRAME_ERR)
197                                 port->icount.frame++;
198                         else if (status & AML_UART_PARITY_ERR)
199                                 port->icount.frame++;
200
201                         mode = readl(port->membase + AML_UART_CONTROL);
202                         mode |= AML_UART_CLEAR_ERR;
203                         writel(mode, port->membase + AML_UART_CONTROL);
204
205                         /* It doesn't clear to 0 automatically */
206                         mode &= ~AML_UART_CLEAR_ERR;
207                         writel(mode, port->membase + AML_UART_CONTROL);
208
209                         status &= port->read_status_mask;
210                         if (status & AML_UART_FRAME_ERR)
211                                 flag = TTY_FRAME;
212                         else if (status & AML_UART_PARITY_ERR)
213                                 flag = TTY_PARITY;
214                 }
215
216                 ch = readl(port->membase + AML_UART_RFIFO);
217                 ch &= 0xff;
218
219                 if ((status & port->ignore_status_mask) == 0)
220                         tty_insert_flip_char(tport, ch, flag);
221
222                 if (status & AML_UART_TX_FIFO_WERR)
223                         tty_insert_flip_char(tport, 0, TTY_OVERRUN);
224
225         } while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
226
227         spin_unlock(&port->lock);
228         tty_flip_buffer_push(tport);
229         spin_lock(&port->lock);
230 }
231
232 static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
233 {
234         struct uart_port *port = (struct uart_port *)dev_id;
235
236         spin_lock(&port->lock);
237
238         if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
239                 meson_receive_chars(port);
240
241         if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
242                 if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
243                         meson_uart_start_tx(port);
244         }
245
246         spin_unlock(&port->lock);
247
248         return IRQ_HANDLED;
249 }
250
251 static const char *meson_uart_type(struct uart_port *port)
252 {
253         return (port->type == PORT_MESON) ? "meson_uart" : NULL;
254 }
255
256 /*
257  * This function is called only from probe() using a temporary io mapping
258  * in order to perform a reset before setting up the device. Since the
259  * temporarily mapped region was successfully requested, there can be no
260  * console on this port at this time. Hence it is not necessary for this
261  * function to acquire the port->lock. (Since there is no console on this
262  * port at this time, the port->lock is not initialized yet.)
263  */
264 static void meson_uart_reset(struct uart_port *port)
265 {
266         u32 val;
267
268         val = readl(port->membase + AML_UART_CONTROL);
269         val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
270         writel(val, port->membase + AML_UART_CONTROL);
271
272         val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
273         writel(val, port->membase + AML_UART_CONTROL);
274 }
275
276 static int meson_uart_startup(struct uart_port *port)
277 {
278         unsigned long flags;
279         u32 val;
280         int ret = 0;
281
282         spin_lock_irqsave(&port->lock, flags);
283
284         val = readl(port->membase + AML_UART_CONTROL);
285         val |= AML_UART_CLR_ERR;
286         writel(val, port->membase + AML_UART_CONTROL);
287         val &= ~AML_UART_CLR_ERR;
288         writel(val, port->membase + AML_UART_CONTROL);
289
290         val |= (AML_UART_RX_EN | AML_UART_TX_EN);
291         writel(val, port->membase + AML_UART_CONTROL);
292
293         val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
294         writel(val, port->membase + AML_UART_CONTROL);
295
296         val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
297         writel(val, port->membase + AML_UART_MISC);
298
299         spin_unlock_irqrestore(&port->lock, flags);
300
301         ret = request_irq(port->irq, meson_uart_interrupt, 0,
302                           port->name, port);
303
304         return ret;
305 }
306
307 static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
308 {
309         u32 val;
310
311         while (!meson_uart_tx_empty(port))
312                 cpu_relax();
313
314         if (port->uartclk == 24000000) {
315                 val = ((port->uartclk / 3) / baud) - 1;
316                 val |= AML_UART_BAUD_XTAL;
317         } else {
318                 val = ((port->uartclk * 10 / (baud * 4) + 5) / 10) - 1;
319         }
320         val |= AML_UART_BAUD_USE;
321         writel(val, port->membase + AML_UART_REG5);
322 }
323
324 static void meson_uart_set_termios(struct uart_port *port,
325                                    struct ktermios *termios,
326                                    struct ktermios *old)
327 {
328         unsigned int cflags, iflags, baud;
329         unsigned long flags;
330         u32 val;
331
332         spin_lock_irqsave(&port->lock, flags);
333
334         cflags = termios->c_cflag;
335         iflags = termios->c_iflag;
336
337         val = readl(port->membase + AML_UART_CONTROL);
338
339         val &= ~AML_UART_DATA_LEN_MASK;
340         switch (cflags & CSIZE) {
341         case CS8:
342                 val |= AML_UART_DATA_LEN_8BIT;
343                 break;
344         case CS7:
345                 val |= AML_UART_DATA_LEN_7BIT;
346                 break;
347         case CS6:
348                 val |= AML_UART_DATA_LEN_6BIT;
349                 break;
350         case CS5:
351                 val |= AML_UART_DATA_LEN_5BIT;
352                 break;
353         }
354
355         if (cflags & PARENB)
356                 val |= AML_UART_PARITY_EN;
357         else
358                 val &= ~AML_UART_PARITY_EN;
359
360         if (cflags & PARODD)
361                 val |= AML_UART_PARITY_TYPE;
362         else
363                 val &= ~AML_UART_PARITY_TYPE;
364
365         val &= ~AML_UART_STOP_BIN_LEN_MASK;
366         if (cflags & CSTOPB)
367                 val |= AML_UART_STOP_BIN_2SB;
368         else
369                 val |= AML_UART_STOP_BIN_1SB;
370
371         if (cflags & CRTSCTS)
372                 val &= ~AML_UART_TWO_WIRE_EN;
373         else
374                 val |= AML_UART_TWO_WIRE_EN;
375
376         writel(val, port->membase + AML_UART_CONTROL);
377
378         baud = uart_get_baud_rate(port, termios, old, 9600, 4000000);
379         meson_uart_change_speed(port, baud);
380
381         port->read_status_mask = AML_UART_TX_FIFO_WERR;
382         if (iflags & INPCK)
383                 port->read_status_mask |= AML_UART_PARITY_ERR |
384                                           AML_UART_FRAME_ERR;
385
386         port->ignore_status_mask = 0;
387         if (iflags & IGNPAR)
388                 port->ignore_status_mask |= AML_UART_PARITY_ERR |
389                                             AML_UART_FRAME_ERR;
390
391         uart_update_timeout(port, termios->c_cflag, baud);
392         spin_unlock_irqrestore(&port->lock, flags);
393 }
394
395 static int meson_uart_verify_port(struct uart_port *port,
396                                   struct serial_struct *ser)
397 {
398         int ret = 0;
399
400         if (port->type != PORT_MESON)
401                 ret = -EINVAL;
402         if (port->irq != ser->irq)
403                 ret = -EINVAL;
404         if (ser->baud_base < 9600)
405                 ret = -EINVAL;
406         return ret;
407 }
408
409 static void meson_uart_release_port(struct uart_port *port)
410 {
411         devm_iounmap(port->dev, port->membase);
412         port->membase = NULL;
413         devm_release_mem_region(port->dev, port->mapbase, port->mapsize);
414 }
415
416 static int meson_uart_request_port(struct uart_port *port)
417 {
418         if (!devm_request_mem_region(port->dev, port->mapbase, port->mapsize,
419                                      dev_name(port->dev))) {
420                 dev_err(port->dev, "Memory region busy\n");
421                 return -EBUSY;
422         }
423
424         port->membase = devm_ioremap_nocache(port->dev, port->mapbase,
425                                              port->mapsize);
426         if (!port->membase)
427                 return -ENOMEM;
428
429         return 0;
430 }
431
432 static void meson_uart_config_port(struct uart_port *port, int flags)
433 {
434         if (flags & UART_CONFIG_TYPE) {
435                 port->type = PORT_MESON;
436                 meson_uart_request_port(port);
437         }
438 }
439
440 static const struct uart_ops meson_uart_ops = {
441         .set_mctrl      = meson_uart_set_mctrl,
442         .get_mctrl      = meson_uart_get_mctrl,
443         .tx_empty       = meson_uart_tx_empty,
444         .start_tx       = meson_uart_start_tx,
445         .stop_tx        = meson_uart_stop_tx,
446         .stop_rx        = meson_uart_stop_rx,
447         .startup        = meson_uart_startup,
448         .shutdown       = meson_uart_shutdown,
449         .set_termios    = meson_uart_set_termios,
450         .type           = meson_uart_type,
451         .config_port    = meson_uart_config_port,
452         .request_port   = meson_uart_request_port,
453         .release_port   = meson_uart_release_port,
454         .verify_port    = meson_uart_verify_port,
455 };
456
457 #ifdef CONFIG_SERIAL_MESON_CONSOLE
458 static void meson_uart_enable_tx_engine(struct uart_port *port)
459 {
460         u32 val;
461
462         val = readl(port->membase + AML_UART_CONTROL);
463         val |= AML_UART_TX_EN;
464         writel(val, port->membase + AML_UART_CONTROL);
465 }
466
467 static void meson_console_putchar(struct uart_port *port, int ch)
468 {
469         if (!port->membase)
470                 return;
471
472         while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
473                 cpu_relax();
474         writel(ch, port->membase + AML_UART_WFIFO);
475 }
476
477 static void meson_serial_port_write(struct uart_port *port, const char *s,
478                                     u_int count)
479 {
480         unsigned long flags;
481         int locked;
482         u32 val, tmp;
483
484         local_irq_save(flags);
485         if (port->sysrq) {
486                 locked = 0;
487         } else if (oops_in_progress) {
488                 locked = spin_trylock(&port->lock);
489         } else {
490                 spin_lock(&port->lock);
491                 locked = 1;
492         }
493
494         val = readl(port->membase + AML_UART_CONTROL);
495         tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
496         writel(tmp, port->membase + AML_UART_CONTROL);
497
498         uart_console_write(port, s, count, meson_console_putchar);
499         writel(val, port->membase + AML_UART_CONTROL);
500
501         if (locked)
502                 spin_unlock(&port->lock);
503         local_irq_restore(flags);
504 }
505
506 static void meson_serial_console_write(struct console *co, const char *s,
507                                        u_int count)
508 {
509         struct uart_port *port;
510
511         port = meson_ports[co->index];
512         if (!port)
513                 return;
514
515         meson_serial_port_write(port, s, count);
516 }
517
518 static int meson_serial_console_setup(struct console *co, char *options)
519 {
520         struct uart_port *port;
521         int baud = 115200;
522         int bits = 8;
523         int parity = 'n';
524         int flow = 'n';
525
526         if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
527                 return -EINVAL;
528
529         port = meson_ports[co->index];
530         if (!port || !port->membase)
531                 return -ENODEV;
532
533         meson_uart_enable_tx_engine(port);
534
535         if (options)
536                 uart_parse_options(options, &baud, &parity, &bits, &flow);
537
538         return uart_set_options(port, co, baud, parity, bits, flow);
539 }
540
541 static struct console meson_serial_console = {
542         .name           = AML_UART_DEV_NAME,
543         .write          = meson_serial_console_write,
544         .device         = uart_console_device,
545         .setup          = meson_serial_console_setup,
546         .flags          = CON_PRINTBUFFER,
547         .index          = -1,
548         .data           = &meson_uart_driver,
549 };
550
551 static int __init meson_serial_console_init(void)
552 {
553         register_console(&meson_serial_console);
554         return 0;
555 }
556 console_initcall(meson_serial_console_init);
557
558 static void meson_serial_early_console_write(struct console *co,
559                                              const char *s,
560                                              u_int count)
561 {
562         struct earlycon_device *dev = co->data;
563
564         meson_serial_port_write(&dev->port, s, count);
565 }
566
567 static int __init
568 meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
569 {
570         if (!device->port.membase)
571                 return -ENODEV;
572
573         meson_uart_enable_tx_engine(&device->port);
574         device->con->write = meson_serial_early_console_write;
575         return 0;
576 }
577 /* Legacy bindings, should be removed when no more used */
578 OF_EARLYCON_DECLARE(meson, "amlogic,meson-uart",
579                     meson_serial_early_console_setup);
580 /* Stable bindings */
581 OF_EARLYCON_DECLARE(meson, "amlogic,meson-ao-uart",
582                     meson_serial_early_console_setup);
583
584 #define MESON_SERIAL_CONSOLE    (&meson_serial_console)
585 #else
586 #define MESON_SERIAL_CONSOLE    NULL
587 #endif
588
589 static struct uart_driver meson_uart_driver = {
590         .owner          = THIS_MODULE,
591         .driver_name    = "meson_uart",
592         .dev_name       = AML_UART_DEV_NAME,
593         .nr             = AML_UART_PORT_NUM,
594         .cons           = MESON_SERIAL_CONSOLE,
595 };
596
597 static inline struct clk *meson_uart_probe_clock(struct device *dev,
598                                                  const char *id)
599 {
600         struct clk *clk = NULL;
601         int ret;
602
603         clk = devm_clk_get(dev, id);
604         if (IS_ERR(clk))
605                 return clk;
606
607         ret = clk_prepare_enable(clk);
608         if (ret) {
609                 dev_err(dev, "couldn't enable clk\n");
610                 return ERR_PTR(ret);
611         }
612
613         devm_add_action_or_reset(dev,
614                         (void(*)(void *))clk_disable_unprepare,
615                         clk);
616
617         return clk;
618 }
619
620 /*
621  * This function gets clocks in the legacy non-stable DT bindings.
622  * This code will be remove once all the platforms switch to the
623  * new DT bindings.
624  */
625 static int meson_uart_probe_clocks_legacy(struct platform_device *pdev,
626                                           struct uart_port *port)
627 {
628         struct clk *clk = NULL;
629
630         clk = meson_uart_probe_clock(&pdev->dev, NULL);
631         if (IS_ERR(clk))
632                 return PTR_ERR(clk);
633
634         port->uartclk = clk_get_rate(clk);
635
636         return 0;
637 }
638
639 static int meson_uart_probe_clocks(struct platform_device *pdev,
640                                    struct uart_port *port)
641 {
642         struct clk *clk_xtal = NULL;
643         struct clk *clk_pclk = NULL;
644         struct clk *clk_baud = NULL;
645
646         clk_pclk = meson_uart_probe_clock(&pdev->dev, "pclk");
647         if (IS_ERR(clk_pclk))
648                 return PTR_ERR(clk_pclk);
649
650         clk_xtal = meson_uart_probe_clock(&pdev->dev, "xtal");
651         if (IS_ERR(clk_xtal))
652                 return PTR_ERR(clk_xtal);
653
654         clk_baud = meson_uart_probe_clock(&pdev->dev, "baud");
655         if (IS_ERR(clk_baud))
656                 return PTR_ERR(clk_baud);
657
658         port->uartclk = clk_get_rate(clk_baud);
659
660         return 0;
661 }
662
663 static int meson_uart_probe(struct platform_device *pdev)
664 {
665         struct resource *res_mem, *res_irq;
666         struct uart_port *port;
667         int ret = 0;
668
669         if (pdev->dev.of_node)
670                 pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
671
672         if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
673                 return -EINVAL;
674
675         res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
676         if (!res_mem)
677                 return -ENODEV;
678
679         res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
680         if (!res_irq)
681                 return -ENODEV;
682
683         if (meson_ports[pdev->id]) {
684                 dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
685                 return -EBUSY;
686         }
687
688         port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
689         if (!port)
690                 return -ENOMEM;
691
692         /* Use legacy way until all platforms switch to new bindings */
693         if (of_device_is_compatible(pdev->dev.of_node, "amlogic,meson-uart"))
694                 ret = meson_uart_probe_clocks_legacy(pdev, port);
695         else
696                 ret = meson_uart_probe_clocks(pdev, port);
697
698         if (ret)
699                 return ret;
700
701         port->iotype = UPIO_MEM;
702         port->mapbase = res_mem->start;
703         port->mapsize = resource_size(res_mem);
704         port->irq = res_irq->start;
705         port->flags = UPF_BOOT_AUTOCONF | UPF_LOW_LATENCY;
706         port->dev = &pdev->dev;
707         port->line = pdev->id;
708         port->type = PORT_MESON;
709         port->x_char = 0;
710         port->ops = &meson_uart_ops;
711         port->fifosize = 64;
712
713         meson_ports[pdev->id] = port;
714         platform_set_drvdata(pdev, port);
715
716         /* reset port before registering (and possibly registering console) */
717         if (meson_uart_request_port(port) >= 0) {
718                 meson_uart_reset(port);
719                 meson_uart_release_port(port);
720         }
721
722         ret = uart_add_one_port(&meson_uart_driver, port);
723         if (ret)
724                 meson_ports[pdev->id] = NULL;
725
726         return ret;
727 }
728
729 static int meson_uart_remove(struct platform_device *pdev)
730 {
731         struct uart_port *port;
732
733         port = platform_get_drvdata(pdev);
734         uart_remove_one_port(&meson_uart_driver, port);
735         meson_ports[pdev->id] = NULL;
736
737         return 0;
738 }
739
740 static const struct of_device_id meson_uart_dt_match[] = {
741         /* Legacy bindings, should be removed when no more used */
742         { .compatible = "amlogic,meson-uart" },
743         /* Stable bindings */
744         { .compatible = "amlogic,meson6-uart" },
745         { .compatible = "amlogic,meson8-uart" },
746         { .compatible = "amlogic,meson8b-uart" },
747         { .compatible = "amlogic,meson-gx-uart" },
748         { /* sentinel */ },
749 };
750 MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
751
752 static  struct platform_driver meson_uart_platform_driver = {
753         .probe          = meson_uart_probe,
754         .remove         = meson_uart_remove,
755         .driver         = {
756                 .name           = "meson_uart",
757                 .of_match_table = meson_uart_dt_match,
758         },
759 };
760
761 static int __init meson_uart_init(void)
762 {
763         int ret;
764
765         ret = uart_register_driver(&meson_uart_driver);
766         if (ret)
767                 return ret;
768
769         ret = platform_driver_register(&meson_uart_platform_driver);
770         if (ret)
771                 uart_unregister_driver(&meson_uart_driver);
772
773         return ret;
774 }
775
776 static void __exit meson_uart_exit(void)
777 {
778         platform_driver_unregister(&meson_uart_platform_driver);
779         uart_unregister_driver(&meson_uart_driver);
780 }
781
782 module_init(meson_uart_init);
783 module_exit(meson_uart_exit);
784
785 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
786 MODULE_DESCRIPTION("Amlogic Meson serial port driver");
787 MODULE_LICENSE("GPL v2");