From patchwork Fri Sep 18 03:24:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Chou X-Patchwork-Id: 519094 X-Patchwork-Delegate: thomas@wytron.com.tw Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 98F371401DA for ; Fri, 18 Sep 2015 13:34:02 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0D86F4B88C; Fri, 18 Sep 2015 05:34:00 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7muD0CPNoWli; Fri, 18 Sep 2015 05:33:59 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 866694B885; Fri, 18 Sep 2015 05:33:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id DB0214B885 for ; Fri, 18 Sep 2015 05:33:54 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id oDCkO9qw5MvT for ; Fri, 18 Sep 2015 05:33:54 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from www.wytron.com.tw (220-134-43-68.HINET-IP.hinet.net [220.134.43.68]) by theia.denx.de (Postfix) with ESMTP id 0BAFF4B884 for ; Fri, 18 Sep 2015 05:33:49 +0200 (CEST) Received: from localhost.localdomain (unknown [192.168.1.250]) by www.wytron.com.tw (Postfix) with ESMTP id 2AAB1D00300; Fri, 18 Sep 2015 11:24:23 +0800 (CST) From: Thomas Chou To: u-boot@lists.denx.de Date: Fri, 18 Sep 2015 11:24:18 +0800 Message-Id: <1442546658-26012-1-git-send-email-thomas@wytron.com.tw> X-Mailer: git-send-email 2.1.4 Cc: Marek Vasut , clsee@altera.com, lftan@altera.com Subject: [U-Boot] [PATCH] nios2: convert altera_uart to driver model X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Convert altera_uart to driver model. Signed-off-by: Thomas Chou Reviewed-by: Simon Glass --- arch/nios2/dts/3c120_devboard.dts | 1 + drivers/serial/Kconfig | 6 ++ drivers/serial/altera_uart.c | 153 ++++++++++++++++++-------------------- include/configs/nios2-generic.h | 9 +-- 4 files changed, 79 insertions(+), 90 deletions(-) diff --git a/arch/nios2/dts/3c120_devboard.dts b/arch/nios2/dts/3c120_devboard.dts index 7f76328..2159cfc 100644 --- a/arch/nios2/dts/3c120_devboard.dts +++ b/arch/nios2/dts/3c120_devboard.dts @@ -130,6 +130,7 @@ interrupts = <10>; current-speed = <115200>; clock-frequency = <62500000>; + u-boot,dm-pre-reloc; }; }; diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig index 5a8cb3a..34195e1 100644 --- a/drivers/serial/Kconfig +++ b/drivers/serial/Kconfig @@ -122,6 +122,12 @@ config ALTERA_JTAG_UART_BYPASS Bypass console output and keep going even if there is no JTAG terminal connection with the host. +config ALTERA_UART + bool "Altera UART support" + depends on NIOS2 && DM_SERIAL + help + Select this to enable an UART for Altera devices. + config ROCKCHIP_SERIAL bool "Rockchip on-chip UART support" depends on ARCH_UNIPHIER && DM_SERIAL diff --git a/drivers/serial/altera_uart.c b/drivers/serial/altera_uart.c index d6b1484..8393d39 100644 --- a/drivers/serial/altera_uart.c +++ b/drivers/serial/altera_uart.c @@ -5,50 +5,31 @@ * SPDX-License-Identifier: GPL-2.0+ */ - #include -#include +#include +#include #include #include #include -typedef volatile struct { +struct altera_uart_regs { unsigned rxdata; /* Rx data reg */ unsigned txdata; /* Tx data reg */ unsigned status; /* Status reg */ unsigned control; /* Control reg */ unsigned divisor; /* Baud rate divisor reg */ unsigned endofpacket; /* End-of-packet reg */ -} nios_uart_t; +}; + +struct altera_uart_platdata { + struct altera_uart_regs *reg; + unsigned int uartclk; +}; /* status register */ -#define NIOS_UART_PE (1 << 0) /* parity error */ -#define NIOS_UART_FE (1 << 1) /* frame error */ -#define NIOS_UART_BRK (1 << 2) /* break detect */ -#define NIOS_UART_ROE (1 << 3) /* rx overrun */ -#define NIOS_UART_TOE (1 << 4) /* tx overrun */ #define NIOS_UART_TMT (1 << 5) /* tx empty */ #define NIOS_UART_TRDY (1 << 6) /* tx ready */ #define NIOS_UART_RRDY (1 << 7) /* rx ready */ -#define NIOS_UART_E (1 << 8) /* exception */ -#define NIOS_UART_DCTS (1 << 10) /* cts change */ -#define NIOS_UART_CTS (1 << 11) /* cts */ -#define NIOS_UART_EOP (1 << 12) /* eop detected */ - -/* control register */ -#define NIOS_UART_IPE (1 << 0) /* parity error int ena*/ -#define NIOS_UART_IFE (1 << 1) /* frame error int ena */ -#define NIOS_UART_IBRK (1 << 2) /* break detect int ena */ -#define NIOS_UART_IROE (1 << 3) /* rx overrun int ena */ -#define NIOS_UART_ITOE (1 << 4) /* tx overrun int ena */ -#define NIOS_UART_ITMT (1 << 5) /* tx empty int ena */ -#define NIOS_UART_ITRDY (1 << 6) /* tx ready int ena */ -#define NIOS_UART_IRRDY (1 << 7) /* rx ready int ena */ -#define NIOS_UART_IE (1 << 8) /* exception int ena */ -#define NIOS_UART_TBRK (1 << 9) /* transmit break */ -#define NIOS_UART_IDCTS (1 << 10) /* cts change int ena */ -#define NIOS_UART_RTS (1 << 11) /* rts */ -#define NIOS_UART_IEOP (1 << 12) /* eop detected int ena */ DECLARE_GLOBAL_DATA_PTR; @@ -56,82 +37,90 @@ DECLARE_GLOBAL_DATA_PTR; * UART the serial port *-----------------------------------------------------------------*/ -static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE; +static int altera_uart_setbrg(struct udevice *dev, int baudrate) +{ + struct altera_uart_platdata *plat = dev->platdata; + struct altera_uart_regs *const regs = plat->reg; + unsigned div; -#if defined(CONFIG_SYS_NIOS_FIXEDBAUD) + div = (plat->uartclk / baudrate) - 1; + writel(div, ®s->divisor); -/* - * Everything's already setup for fixed-baud PTF - * assignment - */ -static void altera_serial_setbrg(void) -{ + return 0; } -static int altera_serial_init(void) +static int altera_uart_putc(struct udevice *dev, const char c) { - return 0; -} + struct altera_uart_platdata *plat = dev->platdata; + struct altera_uart_regs *const regs = plat->reg; -#else + if (!(readl(®s->status) & NIOS_UART_TRDY)) + return -EAGAIN; -static void altera_serial_setbrg(void) -{ - unsigned div; + writel((unsigned char)c, ®s->txdata); - div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1; - writel (div, &uart->divisor); + return 0; } -static int altera_serial_init(void) +static int altera_uart_pending(struct udevice *dev, bool input) { - serial_setbrg(); - return 0; + struct altera_uart_platdata *plat = dev->platdata; + struct altera_uart_regs *const regs = plat->reg; + unsigned st = readl(®s->status); + + if (input) + return (st & NIOS_UART_RRDY) ? 1 : 0; + else + return (st & NIOS_UART_TMT) ? 0 : 1; } -#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ - -/*----------------------------------------------------------------------- - * UART CONSOLE - *---------------------------------------------------------------------*/ -static void altera_serial_putc(char c) +static int altera_uart_getc(struct udevice *dev) { - if (c == '\n') - serial_putc ('\r'); - while ((readl (&uart->status) & NIOS_UART_TRDY) == 0) - WATCHDOG_RESET (); - writel ((unsigned char)c, &uart->txdata); + struct altera_uart_platdata *plat = dev->platdata; + struct altera_uart_regs *const regs = plat->reg; + + if (readl(®s->status) & NIOS_UART_RRDY) + return (readl(®s->rxdata) & 0xff); + else + return -EAGAIN; } -static int altera_serial_tstc(void) +static int altera_uart_probe(struct udevice *dev) { - return (readl (&uart->status) & NIOS_UART_RRDY); + return 0; } -static int altera_serial_getc(void) +static int altera_uart_ofdata_to_platdata(struct udevice *dev) { - while (serial_tstc () == 0) - WATCHDOG_RESET (); - return (readl (&uart->rxdata) & 0x00ff ); + struct altera_uart_platdata *plat = dev_get_platdata(dev); + + plat->reg = ioremap(dev_get_addr(dev), + sizeof(struct altera_uart_regs)); + plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset, + "clock-frequency", 0); + + return 0; } -static struct serial_device altera_serial_drv = { - .name = "altera_serial", - .start = altera_serial_init, - .stop = NULL, - .setbrg = altera_serial_setbrg, - .putc = altera_serial_putc, - .puts = default_serial_puts, - .getc = altera_serial_getc, - .tstc = altera_serial_tstc, +static const struct dm_serial_ops altera_uart_ops = { + .putc = altera_uart_putc, + .pending = altera_uart_pending, + .getc = altera_uart_getc, + .setbrg = altera_uart_setbrg, }; -void altera_serial_initialize(void) -{ - serial_register(&altera_serial_drv); -} +static const struct udevice_id altera_uart_ids[] = { + { .compatible = "altr,uart-1.0", }, + { } +}; -__weak struct serial_device *default_serial_console(void) -{ - return &altera_serial_drv; -} +U_BOOT_DRIVER(altera_uart) = { + .name = "altera_uart", + .id = UCLASS_SERIAL, + .of_match = altera_uart_ids, + .ofdata_to_platdata = altera_uart_ofdata_to_platdata, + .platdata_auto_alloc_size = sizeof(struct altera_uart_platdata), + .probe = altera_uart_probe, + .ops = &altera_uart_ops, + .flags = DM_FLAG_PRE_RELOC, +}; diff --git a/include/configs/nios2-generic.h b/include/configs/nios2-generic.h index bd6d45c..dcb90de 100644 --- a/include/configs/nios2-generic.h +++ b/include/configs/nios2-generic.h @@ -23,14 +23,7 @@ /* * SERIAL */ -#if defined(CONFIG_ALTERA_JTAG_UART) -#else -# define CONFIG_SYS_NIOS_CONSOLE CONFIG_SYS_UART_BASE -#endif - -#define CONFIG_SYS_NIOS_FIXEDBAUD -#define CONFIG_BAUDRATE CONFIG_SYS_UART_BAUD -#define CONFIG_SYS_BAUDRATE_TABLE {CONFIG_BAUDRATE} +#define CONFIG_BAUDRATE 115200 #define CONFIG_SYS_CONSOLE_INFO_QUIET /* Suppress console info */ /*