From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v4 4/8] mips: ath79: add serial driver for ar933x SOC
Date: Sat, 26 Dec 2015 14:20:28 +0100 [thread overview]
Message-ID: <567E941C.9090300@gmail.com> (raw)
In-Reply-To: <BLU436-SMTP105F72B3D2C0F70924E92CEFFF80@phx.gbl>
Am 25.12.2015 um 19:56 schrieb Wills Wang:
> Signed-off-by: Wills Wang <wills.wang@live.com>
> ---
>
> Changes in v4: None
> Changes in v3: None
> Changes in v2: None
>
> drivers/serial/Makefile | 1 +
> drivers/serial/serial_ar933x.c | 225 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 226 insertions(+)
> create mode 100644 drivers/serial/serial_ar933x.c
>
> diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
> index dd87147..9a7ad89 100644
> --- a/drivers/serial/Makefile
> +++ b/drivers/serial/Makefile
> @@ -17,6 +17,7 @@ endif
>
> obj-$(CONFIG_ALTERA_UART) += altera_uart.o
> obj-$(CONFIG_ALTERA_JTAG_UART) += altera_jtag_uart.o
> +obj-$(CONFIG_AR933X_SERIAL) += serial_ar933x.o
> obj-$(CONFIG_ARM_DCC) += arm_dcc.o
> obj-$(CONFIG_ATMEL_USART) += atmel_usart.o
> obj-$(CONFIG_EFI_APP) += serial_efi.o
> diff --git a/drivers/serial/serial_ar933x.c b/drivers/serial/serial_ar933x.c
> new file mode 100644
> index 0000000..efca93c
> --- /dev/null
> +++ b/drivers/serial/serial_ar933x.c
> @@ -0,0 +1,225 @@
> +/*
> + * (C) Copyright 2015
> + * Wills Wang, <wills.wang@live.com>
> + *
> + * SPDX-License-Identifier: GPL-2.0+
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <errno.h>
> +#include <serial.h>
> +#include <asm/io.h>
> +#include <asm/addrspace.h>
> +#include <asm/div64.h>
use the existing and generic implementation in include/div64.h
> +#include <asm/types.h>
> +#include <asm/arch/ar71xx_regs.h>
#include <mach/ar71xx_regs.h>
I wonder how you can stil compile your code
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +#define AR933X_UART_DATA_REG 0x00
> +#define AR933X_UART_CS_REG 0x04
> +#define AR933X_UART_CLK_REG 0x08
> +
> +#define AR933X_UART_DATA_TX_RX_MASK 0xff
> +#define AR933X_UART_DATA_RX_CSR BIT(8)
> +#define AR933X_UART_DATA_TX_CSR BIT(9)
> +#define AR933X_UART_CS_IF_MODE_S 2
> +#define AR933X_UART_CS_IF_MODE_M 0x3
> +#define AR933X_UART_CS_IF_MODE_DTE 1
> +#define AR933X_UART_CS_IF_MODE_DCE 2
> +#define AR933X_UART_CS_TX_RDY_ORIDE BIT(7)
> +#define AR933X_UART_CS_RX_RDY_ORIDE BIT(8)
> +#define AR933X_UART_CLK_STEP_M 0xffff
> +#define AR933X_UART_CLK_SCALE_M 0xfff
> +#define AR933X_UART_CLK_SCALE_S 16
> +#define AR933X_UART_CLK_STEP_S 0
> +
> +struct ar933x_serial_platdata {
> + void __iomem *regs;
> +};
if you always support device-tree, you do not need platform data
> +
> +struct ar933x_serial_priv {
> + void __iomem *regs;
> +};
> +
> +static inline u32 ar933x_serial_read(struct udevice *dev, u32 offset)
> +{
> + struct ar933x_serial_priv *priv = dev_get_priv(dev);
> + return readl(priv->regs + offset);
> +}
> +
> +static inline void ar933x_serial_write(struct udevice *dev,
> + u32 val, u32 offset)
> +{
> + struct ar933x_serial_priv *priv = dev_get_priv(dev);
> + writel(val, priv->regs + offset);
> +}
> +
> +/*
> + * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
> + */
> +static u32 ar933x_serial_get_baud(u32 clk, u32 scale, u32 step)
> +{
> + u64 t;
> + u32 div;
> +
> + div = (2 << 16) * (scale + 1);
> + t = clk;
> + t *= step;
> + t += (div / 2);
> + do_div(t, div);
> +
> + return t;
> +}
> +
> +static void ar933x_serial_get_scale_step(u32 clk, u32 baud,
> + u32 *scale, u32 *step)
> +{
> + u32 tscale, baudrate;
> + long min_diff;
> +
> + *scale = 0;
> + *step = 0;
> +
> + min_diff = baud;
> + for (tscale = 0; tscale < AR933X_UART_CLK_SCALE_M; tscale++) {
> + u64 tstep;
> + int diff;
> +
> + tstep = baud * (tscale + 1);
> + tstep *= (2 << 16);
> + do_div(tstep, clk);
> +
> + if (tstep > AR933X_UART_CLK_STEP_M)
> + break;
> +
> + baudrate = ar933x_serial_get_baud(clk, tscale, tstep);
> + diff = abs(baudrate - baud);
> + if (diff < min_diff) {
> + min_diff = diff;
> + *scale = tscale;
> + *step = tstep;
> + }
> + }
> +}
> +
> +static int ar933x_serial_setbrg(struct udevice *dev, int baudrate)
> +{
> + u32 val, scale, step;
> +
> + val = get_serial_clock();
> + ar933x_serial_get_scale_step(val, baudrate, &scale, &step);
> +
> + val = (scale & AR933X_UART_CLK_SCALE_M)
> + << AR933X_UART_CLK_SCALE_S;
> + val |= (step & AR933X_UART_CLK_STEP_M)
> + << AR933X_UART_CLK_STEP_S;
> + ar933x_serial_write(dev, val, AR933X_UART_CLK_REG);
> +
> + return 0;
> +}
> +
> +static int ar933x_serial_putc(struct udevice *dev, const char c)
> +{
> + u32 data;
> +
> + if (c == '\n')
> + ar933x_serial_putc(dev, '\r');
remove this, the serial core driver takes care of it
> +
> + do {
> + data = ar933x_serial_read(dev, AR933X_UART_DATA_REG);
> + } while (!(data & AR933X_UART_DATA_TX_CSR));
remove this, the serial core driver takes care of it via your pending
callback (ar933x_serial_pending)
> +
> + data = (u32)c | AR933X_UART_DATA_TX_CSR;
> + ar933x_serial_write(dev, data, AR933X_UART_DATA_REG);
> +
> + return 0;
> +}
> +
> +static int ar933x_serial_getc(struct udevice *dev)
> +{
> + u32 data;
> +
> + do {
> + data = ar933x_serial_read(dev, AR933X_UART_DATA_REG);
> + } while (!(data & AR933X_UART_DATA_RX_CSR));
dito
> +
> + data = ar933x_serial_read(dev, AR933X_UART_DATA_REG);
> + ar933x_serial_write(dev, AR933X_UART_DATA_RX_CSR,
> + AR933X_UART_DATA_REG);
> + return data & AR933X_UART_DATA_TX_RX_MASK;
> +}
> +
> +static int ar933x_serial_pending(struct udevice *dev, bool input)
> +{
> + u32 data;
> +
> + data = ar933x_serial_read(dev, AR933X_UART_DATA_REG);
> + if (input)
> + return (data & AR933X_UART_DATA_RX_CSR) ? 1 : 0;
> + else
> + return (data & AR933X_UART_DATA_TX_CSR) ? 0 : 1;
> +}
> +
> +static int ar933x_serial_probe(struct udevice *dev)
> +{
> + struct ar933x_serial_priv *priv = dev_get_priv(dev);
> + struct ar933x_serial_platdata *plat = dev_get_platdata(dev);
> + u32 val;
> +
> + priv->regs = plat->regs;
> +
> + /*
> + * UART controller configuration:
> + * - no DMA
> + * - no interrupt
> + * - DCE mode
> + * - no flow control
> + * - set RX ready oride
> + * - set TX ready oride
> + */
> + val = (AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S) |
> + AR933X_UART_CS_TX_RDY_ORIDE | AR933X_UART_CS_RX_RDY_ORIDE;
> + ar933x_serial_write(dev, val, AR933X_UART_CS_REG);
> + return 0;
> +}
> +
> +static int ar933x_serial_ofdata_to_platdata(struct udevice *dev)
> +{
> + struct ar933x_serial_platdata *plat = dev_get_platdata(dev);
> + fdt_addr_t addr;
> +
> + addr = dev_get_addr(dev);
> + if (addr == FDT_ADDR_T_NONE)
> + return -EINVAL;
> +
> + plat->regs = map_physmem(addr,
> + AR933X_UART_SIZE,
> + MAP_NOCACHE);
move this code to function ar933x_serial_probe and drop this function
> + return 0;
> +}
> +
> +static const struct dm_serial_ops ar933x_serial_ops = {
> + .putc = ar933x_serial_putc,
> + .pending = ar933x_serial_pending,
> + .getc = ar933x_serial_getc,
> + .setbrg = ar933x_serial_setbrg,
> +};
> +
> +static const struct udevice_id ar933x_serial_ids[] = {
> + { .compatible = "ath79,ar933x-uart" },
> + { }
> +};
> +
> +U_BOOT_DRIVER(serial_ar933x) = {
> + .name = "serial_ar933x",
> + .id = UCLASS_SERIAL,
> + .of_match = ar933x_serial_ids,
> + .ofdata_to_platdata = ar933x_serial_ofdata_to_platdata,
> + .platdata_auto_alloc_size = sizeof(struct ar933x_serial_platdata),
drop the two lines, you do not need to allocate platdata
> + .priv_auto_alloc_size = sizeof(struct ar933x_serial_priv),
> + .probe = ar933x_serial_probe,
> + .ops = &ar933x_serial_ops,
> + .flags = DM_FLAG_PRE_RELOC,
> +};
>
--
- Daniel
next prev parent reply other threads:[~2015-12-26 13:20 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1451069788-6786-1-git-send-email-wills.wang@live.com>
2015-12-25 18:56 ` [U-Boot] [PATCH v4 1/8] include: Add support for "do_div" macro Wills Wang
2015-12-26 7:24 ` Marek Vasut
2015-12-26 15:48 ` Wills Wang
2015-12-26 13:09 ` Daniel Schwierzeck
2015-12-26 16:54 ` Wills Wang
2015-12-25 18:56 ` [U-Boot] [PATCH v4 2/8] mips: implement to access the KSEG0/1 memory range in map_physmem Wills Wang
2015-12-26 7:25 ` Marek Vasut
2015-12-25 18:56 ` [U-Boot] [PATCH v4 3/8] mips: add base support for atheros ath79 based SOCs Wills Wang
2015-12-26 7:28 ` Marek Vasut
2015-12-26 16:17 ` Wills Wang
2015-12-26 17:01 ` Daniel Schwierzeck
2015-12-26 17:06 ` Marek Vasut
2015-12-27 9:37 ` Wills Wang
2015-12-25 18:56 ` [U-Boot] [PATCH v4 4/8] mips: ath79: add serial driver for ar933x SOC Wills Wang
2015-12-26 13:20 ` Daniel Schwierzeck [this message]
2015-12-26 16:54 ` Wills Wang
2015-12-26 17:19 ` Daniel Schwierzeck
2015-12-27 6:28 ` Wills Wang
2015-12-27 8:21 ` Thomas Chou
2015-12-27 13:07 ` Thomas Chou
2015-12-27 8:31 ` Thomas Chou
2015-12-25 18:56 ` [U-Boot] [PATCH v4 5/8] mips: ath79: add spi driver Wills Wang
2015-12-26 13:23 ` Daniel Schwierzeck
2015-12-26 16:56 ` Wills Wang
2015-12-25 18:56 ` [U-Boot] [PATCH v4 6/8] mips: ath79: add AP121 reference board Wills Wang
2015-12-26 13:52 ` Daniel Schwierzeck
2015-12-26 16:59 ` Wills Wang
2015-12-26 17:07 ` Daniel Schwierzeck
2015-12-27 6:36 ` Wills Wang
2015-12-27 6:41 ` Marek Vasut
2015-12-27 7:03 ` Wills Wang
2015-12-27 7:10 ` Marek Vasut
2015-12-25 18:56 ` [U-Boot] [PATCH v4 7/8] mips: support optimize tuning for same common processor cores Wills Wang
2015-12-26 7:30 ` Marek Vasut
2015-12-26 18:58 ` Daniel Schwierzeck
2015-12-27 3:03 ` Wills Wang
2015-12-25 18:56 ` [U-Boot] [PATCH v4 8/8] mips: move optimize tuning option from deprecated config.mk to Kconfig Wills Wang
2015-12-26 7:30 ` Marek Vasut
2015-12-26 17:33 ` Wills Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=567E941C.9090300@gmail.com \
--to=daniel.schwierzeck@gmail.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.