Merge https://gitlab.denx.de/u-boot/custodians/u-boot-riscv
[oweals/u-boot.git] / drivers / serial / serial_pxa.c
index 84bb17c11051b9d7ab33cc350d352defbc85e2ca..6f8f7e1198ae87af1381ad14d0163e3897f110c0 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  *
  *
  * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
+ * Modified to add driver model (DM) support
+ * (C) Copyright 2016 Marcel Ziswiler <marcel.ziswiler@toradex.com>
  */
 
 #include <common.h>
-#include <watchdog.h>
-#include <serial.h>
+#include <hang.h>
 #include <asm/arch/pxa-regs.h>
 #include <asm/arch/regs-uart.h>
 #include <asm/io.h>
+#include <dm.h>
+#include <dm/platform_data/serial_pxa.h>
+#include <linux/compiler.h>
+#include <serial.h>
+#include <watchdog.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
-/*
- * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can
- * easily handle enabling of clock.
- */
-#ifdef CONFIG_CPU_MONAHANS
-#define        UART_CLK_BASE   CKENA_21_BTUART
-#define        UART_CLK_REG    CKENA
-#define        BTUART_INDEX    0
-#define        FFUART_INDEX    1
-#define        STUART_INDEX    2
-#elif  CONFIG_PXA250
-#define        UART_CLK_BASE   (1 << 4)        /* HWUART */
-#define        UART_CLK_REG    CKEN
-#define        HWUART_INDEX    0
-#define        STUART_INDEX    1
-#define        FFUART_INDEX    2
-#define        BTUART_INDEX    3
-#else  /* PXA27x */
-#define        UART_CLK_BASE   CKEN5_STUART
-#define        UART_CLK_REG    CKEN
-#define        STUART_INDEX    0
-#define        FFUART_INDEX    1
-#define        BTUART_INDEX    2
-#endif
-
-/*
- * Only PXA250 has HWUART, to avoid poluting the code with more macros,
- * artificially introduce this.
- */
-#ifndef        CONFIG_PXA250
-#define        HWUART_INDEX    0xff
-#endif
-
-#ifndef CONFIG_SERIAL_MULTI
-#if defined(CONFIG_FFUART)
-#define UART_INDEX     FFUART_INDEX
-#elif defined(CONFIG_BTUART)
-#define UART_INDEX     BTUART_INDEX
-#elif defined(CONFIG_STUART)
-#define UART_INDEX     STUART_INDEX
-#elif defined(CONFIG_HWUART)
-#define UART_INDEX     HWUART_INDEX
-#else
-#error "Please select CONFIG_(FF|BT|ST|HW)UART in board config file."
-#endif
-#endif
-
-uint32_t pxa_uart_get_baud_divider(void)
+static uint32_t pxa_uart_get_baud_divider(int baudrate)
 {
-       if (gd->baudrate == 1200)
-               return 768;
-       else if (gd->baudrate == 9600)
-               return 96;
-       else if (gd->baudrate == 19200)
-               return 48;
-       else if (gd->baudrate == 38400)
-               return 24;
-       else if (gd->baudrate == 57600)
-               return 16;
-       else if (gd->baudrate == 115200)
-               return 8;
-       else    /* Unsupported baudrate */
-               return 0;
-}
-
-struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
-{
-       switch (uart_index) {
-       case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
-       case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
-       case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
-       case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
-       default:
-               return NULL;
-       }
+       return 921600 / baudrate;
 }
 
-void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
+static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
 {
        uint32_t clk_reg, clk_offset, reg;
 
@@ -136,20 +57,14 @@ void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
 /*
  * Enable clock and set baud rate, parity etc.
  */
-void pxa_setbrg_dev(uint32_t uart_index)
+void pxa_setbrg_common(struct pxa_uart_regs *uart_regs, int port, int baudrate)
 {
-       uint32_t divider = 0;
-       struct pxa_uart_regs *uart_regs;
-
-       divider = pxa_uart_get_baud_divider();
+       uint32_t divider = pxa_uart_get_baud_divider(baudrate);
        if (!divider)
                hang();
 
-       uart_regs = pxa_uart_index_to_regs(uart_index);
-       if (!uart_regs)
-               hang();
 
-       pxa_uart_toggle_clock(uart_index, 1);
+       pxa_uart_toggle_clock(port, 1);
 
        /* Disable interrupts and FIFOs */
        writel(0, &uart_regs->ier);
@@ -165,13 +80,38 @@ void pxa_setbrg_dev(uint32_t uart_index)
        writel(IER_UUE, &uart_regs->ier);
 }
 
+#ifndef CONFIG_DM_SERIAL
+static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
+{
+       switch (uart_index) {
+       case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
+       case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
+       case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
+       case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
+       default:
+               return NULL;
+       }
+}
+
+/*
+ * Enable clock and set baud rate, parity etc.
+ */
+void pxa_setbrg_dev(uint32_t uart_index)
+{
+       struct pxa_uart_regs *uart_regs = pxa_uart_index_to_regs(uart_index);
+       if (!uart_regs)
+               panic("Failed getting UART registers\n");
+
+       pxa_setbrg_common(uart_regs, uart_index, gd->baudrate);
+}
+
 /*
  * Initialise the serial port with the given baudrate. The settings
  * are always 8 data bits, no parity, 1 stop bit, no start bits.
  */
 int pxa_init_dev(unsigned int uart_index)
 {
-       pxa_setbrg_dev (uart_index);
+       pxa_setbrg_dev(uart_index);
        return 0;
 }
 
@@ -182,6 +122,10 @@ void pxa_putc_dev(unsigned int uart_index, const char c)
 {
        struct pxa_uart_regs *uart_regs;
 
+       /* If \n, also do \r */
+       if (c == '\n')
+               pxa_putc_dev(uart_index, '\r');
+
        uart_regs = pxa_uart_index_to_regs(uart_index);
        if (!uart_regs)
                hang();
@@ -189,10 +133,6 @@ void pxa_putc_dev(unsigned int uart_index, const char c)
        while (!(readl(&uart_regs->lsr) & LSR_TEMT))
                WATCHDOG_RESET();
        writel(c, &uart_regs->thr);
-
-       /* If \n, also do \r */
-       if (c == '\n')
-               pxa_putc_dev (uart_index,'\r');
 }
 
 /*
@@ -269,14 +209,14 @@ void pxa_puts_dev(unsigned int uart_index, const char *s)
 #define        pxa_uart_desc(uart)                                             \
        struct serial_device serial_##uart##_device =                   \
        {                                                               \
-               "serial_"#uart,                                         \
-               uart##_init,                                            \
-               NULL,                                                   \
-               uart##_setbrg,                                          \
-               uart##_getc,                                            \
-               uart##_tstc,                                            \
-               uart##_putc,                                            \
-               uart##_puts,                                            \
+               .name   = "serial_"#uart,                               \
+               .start  = uart##_init,                                  \
+               .stop   = NULL,                                         \
+               .setbrg = uart##_setbrg,                                \
+               .getc   = uart##_getc,                                  \
+               .tstc   = uart##_tstc,                                  \
+               .putc   = uart##_putc,                                  \
+               .puts   = uart##_puts,                                  \
        };
 
 #define        pxa_uart_multi(uart, UART)                                      \
@@ -296,6 +236,107 @@ void pxa_puts_dev(unsigned int uart_index, const char *s)
        pxa_uart_multi(btuart, BTUART)
 #endif
 
-#ifndef        CONFIG_SERIAL_MULTI
-       pxa_uart(serial, UART)
+__weak struct serial_device *default_serial_console(void)
+{
+#if CONFIG_CONS_INDEX == 1
+       return &serial_hwuart_device;
+#elif CONFIG_CONS_INDEX == 2
+       return &serial_stuart_device;
+#elif CONFIG_CONS_INDEX == 3
+       return &serial_ffuart_device;
+#elif CONFIG_CONS_INDEX == 4
+       return &serial_btuart_device;
+#else
+#error "Bad CONFIG_CONS_INDEX."
 #endif
+}
+
+void pxa_serial_initialize(void)
+{
+#if defined(CONFIG_FFUART)
+       serial_register(&serial_ffuart_device);
+#endif
+#if defined(CONFIG_BTUART)
+       serial_register(&serial_btuart_device);
+#endif
+#if defined(CONFIG_STUART)
+       serial_register(&serial_stuart_device);
+#endif
+}
+#endif /* CONFIG_DM_SERIAL */
+
+#ifdef CONFIG_DM_SERIAL
+static int pxa_serial_probe(struct udevice *dev)
+{
+       struct pxa_serial_platdata *plat = dev->platdata;
+
+       pxa_setbrg_common((struct pxa_uart_regs *)plat->base, plat->port,
+                         plat->baudrate);
+       return 0;
+}
+
+static int pxa_serial_putc(struct udevice *dev, const char ch)
+{
+       struct pxa_serial_platdata *plat = dev->platdata;
+       struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
+
+       /* Wait for last character to go. */
+       if (!(readl(&uart_regs->lsr) & LSR_TEMT))
+               return -EAGAIN;
+
+       writel(ch, &uart_regs->thr);
+
+       return 0;
+}
+
+static int pxa_serial_getc(struct udevice *dev)
+{
+       struct pxa_serial_platdata *plat = dev->platdata;
+       struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
+
+       /* Wait for a character to arrive. */
+       if (!(readl(&uart_regs->lsr) & LSR_DR))
+               return -EAGAIN;
+
+       return readl(&uart_regs->rbr) & 0xff;
+}
+
+int pxa_serial_setbrg(struct udevice *dev, int baudrate)
+{
+       struct pxa_serial_platdata *plat = dev->platdata;
+       struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
+       int port = plat->port;
+
+       pxa_setbrg_common(uart_regs, port, baudrate);
+
+       return 0;
+}
+
+static int pxa_serial_pending(struct udevice *dev, bool input)
+{
+       struct pxa_serial_platdata *plat = dev->platdata;
+       struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
+
+       if (input)
+               return readl(&uart_regs->lsr) & LSR_DR ? 1 : 0;
+       else
+               return readl(&uart_regs->lsr) & LSR_TEMT ? 0 : 1;
+
+       return 0;
+}
+
+static const struct dm_serial_ops pxa_serial_ops = {
+       .putc           = pxa_serial_putc,
+       .pending        = pxa_serial_pending,
+       .getc           = pxa_serial_getc,
+       .setbrg         = pxa_serial_setbrg,
+};
+
+U_BOOT_DRIVER(serial_pxa) = {
+       .name   = "serial_pxa",
+       .id     = UCLASS_SERIAL,
+       .probe  = pxa_serial_probe,
+       .ops    = &pxa_serial_ops,
+       .flags  = DM_FLAG_PRE_RELOC,
+};
+#endif /* CONFIG_DM_SERIAL */