uboot/drivers/serial/serial_linflexuart.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2013-2016 Freescale Semiconductor, Inc.
   3 *
   4 * SPDX-License-Identifier:     GPL-2.0+
   5 */
   6
   7#include <common.h>
   8#include <dm.h>
   9#include <errno.h>
  10#include <watchdog.h>
  11#include <asm/io.h>
  12#include <serial.h>
  13#include <linux/compiler.h>
  14#include <asm/arch/imx-regs.h>
  15#include <asm/arch/clock.h>
  16
  17#define US1_TDRE            (1 << 7)
  18#define US1_RDRF            (1 << 5)
  19#define UC2_TE              (1 << 3)
  20#define LINCR1_INIT         (1 << 0)
  21#define LINCR1_MME          (1 << 4)
  22#define LINCR1_BF           (1 << 7)
  23#define LINSR_LINS_INITMODE (0x00001000)
  24#define LINSR_LINS_MASK     (0x0000F000)
  25#define UARTCR_UART         (1 << 0)
  26#define UARTCR_WL0          (1 << 1)
  27#define UARTCR_PCE          (1 << 2)
  28#define UARTCR_PC0          (1 << 3)
  29#define UARTCR_TXEN         (1 << 4)
  30#define UARTCR_RXEN         (1 << 5)
  31#define UARTCR_PC1          (1 << 6)
  32#define UARTSR_DTF          (1 << 1)
  33#define UARTSR_DRF          (1 << 2)
  34#define UARTSR_RMB          (1 << 9)
  35
  36DECLARE_GLOBAL_DATA_PTR;
  37
  38#ifndef CONFIG_DM_SERIAL
  39#error "The linflex serial driver does not have non-DM support."
  40#endif
  41
  42static void _linflex_serial_setbrg(struct linflex_fsl *base, int baudrate)
  43{
  44        u32 clk = mxc_get_clock(MXC_UART_CLK);
  45        u32 ibr, fbr;
  46
  47        if (!baudrate)
  48                baudrate = CONFIG_BAUDRATE;
  49
  50        ibr = (u32) (clk / (16 * gd->baudrate));
  51        fbr = (u32) (clk % (16 * gd->baudrate)) * 16;
  52
  53        __raw_writel(ibr, &base->linibrr);
  54        __raw_writel(fbr, &base->linfbrr);
  55}
  56
  57static int _linflex_serial_getc(struct linflex_fsl *base)
  58{
  59        char c;
  60
  61        if (!(__raw_readb(&base->uartsr) & UARTSR_DRF))
  62                return -EAGAIN;
  63
  64        if (!(__raw_readl(&base->uartsr) & UARTSR_RMB))
  65                return -EAGAIN;
  66
  67        c = __raw_readl(&base->bdrm);
  68        __raw_writeb((__raw_readb(&base->uartsr) | (UARTSR_DRF | UARTSR_RMB)),
  69                     &base->uartsr);
  70        return c;
  71}
  72
  73static int _linflex_serial_putc(struct linflex_fsl *base, const char c)
  74{
  75        __raw_writeb(c, &base->bdrl);
  76
  77
  78        if (!(__raw_readb(&base->uartsr) & UARTSR_DTF))
  79                return -EAGAIN;
  80
  81        __raw_writeb((__raw_readb(&base->uartsr) | UARTSR_DTF), &base->uartsr);
  82
  83        return 0;
  84}
  85
  86/*
  87 * Initialise the serial port with the given baudrate. The settings
  88 * are always 8 data bits, no parity, 1 stop bit, no start bits.
  89 */
  90static int _linflex_serial_init(struct linflex_fsl *base)
  91{
  92        volatile u32 ctrl;
  93
  94        /* set the Linflex in master mode amd activate by-pass filter */
  95        ctrl = LINCR1_BF | LINCR1_MME;
  96        __raw_writel(ctrl, &base->lincr1);
  97
  98        /* init mode */
  99        ctrl |= LINCR1_INIT;
 100        __raw_writel(ctrl, &base->lincr1);
 101
 102        /* waiting for init mode entry - TODO: add a timeout */
 103        while ((__raw_readl(&base->linsr) & LINSR_LINS_MASK) !=
 104               LINSR_LINS_INITMODE);
 105
 106        /* set UART bit to allow writing other bits */
 107        __raw_writel(UARTCR_UART, &base->uartcr);
 108
 109        /* provide data bits, parity, stop bit, etc */
 110        serial_setbrg();
 111
 112        /* 8 bit data, no parity, Tx and Rx enabled, UART mode */
 113        __raw_writel(UARTCR_PC1 | UARTCR_RXEN | UARTCR_TXEN | UARTCR_PC0
 114                     | UARTCR_WL0 | UARTCR_UART, &base->uartcr);
 115
 116        ctrl = __raw_readl(&base->lincr1);
 117        ctrl &= ~LINCR1_INIT;
 118        __raw_writel(ctrl, &base->lincr1);      /* end init mode */
 119
 120        return 0;
 121}
 122
 123struct linflex_serial_platdata {
 124        struct linflex_fsl *base_addr;
 125        u8 port_id; /* do we need this? */
 126};
 127
 128struct linflex_serial_priv {
 129        struct linflex_fsl *lfuart;
 130};
 131
 132int linflex_serial_setbrg(struct udevice *dev, int baudrate)
 133{
 134        struct linflex_serial_priv *priv = dev_get_priv(dev);
 135
 136        _linflex_serial_setbrg(priv->lfuart, baudrate);
 137
 138        return 0;
 139}
 140
 141static int linflex_serial_getc(struct udevice *dev)
 142{
 143        struct linflex_serial_priv *priv = dev_get_priv(dev);
 144
 145        return _linflex_serial_getc(priv->lfuart);
 146}
 147
 148static int linflex_serial_putc(struct udevice *dev, const char ch)
 149{
 150
 151        struct linflex_serial_priv *priv = dev_get_priv(dev);
 152
 153        return _linflex_serial_putc(priv->lfuart, ch);
 154}
 155
 156static int linflex_serial_pending(struct udevice *dev, bool input)
 157{
 158        struct linflex_serial_priv *priv = dev_get_priv(dev);
 159        uint32_t uartsr = __raw_readl(&priv->lfuart->uartsr);
 160
 161        if (input)
 162                return ((uartsr & UARTSR_DRF) && (uartsr & UARTSR_RMB)) ? 1 : 0;
 163        else
 164                return uartsr & UARTSR_DTF ? 0 : 1;
 165}
 166
 167static void linflex_serial_init_internal(struct linflex_fsl *lfuart)
 168{
 169        _linflex_serial_init(lfuart);
 170        _linflex_serial_setbrg(lfuart, CONFIG_BAUDRATE);
 171        return;
 172}
 173
 174static int linflex_serial_probe(struct udevice *dev)
 175{
 176        struct linflex_serial_platdata *plat = dev->platdata;
 177        struct linflex_serial_priv *priv = dev_get_priv(dev);
 178
 179        priv->lfuart = (struct linflex_fsl *)plat->base_addr;
 180        linflex_serial_init_internal(priv->lfuart);
 181
 182        return 0;
 183}
 184
 185static const struct dm_serial_ops linflex_serial_ops = {
 186        .putc = linflex_serial_putc,
 187        .pending = linflex_serial_pending,
 188        .getc = linflex_serial_getc,
 189        .setbrg = linflex_serial_setbrg,
 190};
 191
 192U_BOOT_DRIVER(serial_linflex) = {
 193        .name   = "serial_linflex",
 194        .id     = UCLASS_SERIAL,
 195        .probe = linflex_serial_probe,
 196        .ops    = &linflex_serial_ops,
 197        .flags = DM_FLAG_PRE_RELOC,
 198        .priv_auto_alloc_size   = sizeof(struct linflex_serial_priv),
 199};
 200
 201#ifdef CONFIG_DEBUG_UART_LINFLEXUART
 202
 203#include <debug_uart.h>
 204
 205
 206static inline void _debug_uart_init(void)
 207{
 208        struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
 209
 210        linflex_serial_init_internal(base);
 211}
 212
 213static inline void _debug_uart_putc(int ch)
 214{
 215        struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
 216
 217        /* XXX: Is this OK? Should this use the non-DM version? */
 218        _linflex_serial_putc(base, ch);
 219}
 220
 221DEBUG_UART_FUNCS
 222
 223#endif /* CONFIG_DEBUG_UART_LINFLEXUART */
 224