120c9226cSAndreas Engel /* 220c9226cSAndreas Engel * (C) Copyright 2000 320c9226cSAndreas Engel * Rob Taylor, Flying Pig Systems. robt@flyingpig.com. 420c9226cSAndreas Engel * 520c9226cSAndreas Engel * (C) Copyright 2004 620c9226cSAndreas Engel * ARM Ltd. 720c9226cSAndreas Engel * Philippe Robin, <philippe.robin@arm.com> 820c9226cSAndreas Engel * 91a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 1020c9226cSAndreas Engel */ 1120c9226cSAndreas Engel 1248d0192fSAndreas Engel /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */ 1320c9226cSAndreas Engel 1420c9226cSAndreas Engel #include <common.h> 158a9cd5adSSimon Glass #include <dm.h> 16aed2fbefSSimon Glass #include <errno.h> 1720c9226cSAndreas Engel #include <watchdog.h> 18249d5219SMatt Waddel #include <asm/io.h> 1939f61477SMarek Vasut #include <serial.h> 2086256b79SMasahiro Yamada #include <dm/platform_data/serial_pl01x.h> 2139f61477SMarek Vasut #include <linux/compiler.h> 22aed2fbefSSimon Glass #include "serial_pl01x_internal.h" 2369751729SVikas Manocha #include <fdtdec.h> 2469751729SVikas Manocha 2569751729SVikas Manocha DECLARE_GLOBAL_DATA_PTR; 2620c9226cSAndreas Engel 278a9cd5adSSimon Glass #ifndef CONFIG_DM_SERIAL 288a9cd5adSSimon Glass 2920c9226cSAndreas Engel static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS; 30aed2fbefSSimon Glass static enum pl01x_type pl01x_type __attribute__ ((section(".data"))); 31aed2fbefSSimon Glass static struct pl01x_regs *base_regs __attribute__ ((section(".data"))); 3220c9226cSAndreas Engel #define NUM_PORTS (sizeof(port)/sizeof(port[0])) 3320c9226cSAndreas Engel 348a9cd5adSSimon Glass #endif 3520c9226cSAndreas Engel 36aed2fbefSSimon Glass static int pl01x_putc(struct pl01x_regs *regs, char c) 3772d5e44cSRabin Vincent { 3820c9226cSAndreas Engel /* Wait until there is space in the FIFO */ 39aed2fbefSSimon Glass if (readl(®s->fr) & UART_PL01x_FR_TXFF) 40aed2fbefSSimon Glass return -EAGAIN; 4120c9226cSAndreas Engel 4220c9226cSAndreas Engel /* Send the character */ 4372d5e44cSRabin Vincent writel(c, ®s->dr); 44aed2fbefSSimon Glass 45aed2fbefSSimon Glass return 0; 4620c9226cSAndreas Engel } 4720c9226cSAndreas Engel 48aed2fbefSSimon Glass static int pl01x_getc(struct pl01x_regs *regs) 4920c9226cSAndreas Engel { 5020c9226cSAndreas Engel unsigned int data; 5120c9226cSAndreas Engel 5220c9226cSAndreas Engel /* Wait until there is data in the FIFO */ 53aed2fbefSSimon Glass if (readl(®s->fr) & UART_PL01x_FR_RXFE) 54aed2fbefSSimon Glass return -EAGAIN; 5520c9226cSAndreas Engel 5672d5e44cSRabin Vincent data = readl(®s->dr); 5720c9226cSAndreas Engel 5820c9226cSAndreas Engel /* Check for an error flag */ 5920c9226cSAndreas Engel if (data & 0xFFFFFF00) { 6020c9226cSAndreas Engel /* Clear the error */ 6172d5e44cSRabin Vincent writel(0xFFFFFFFF, ®s->ecr); 6220c9226cSAndreas Engel return -1; 6320c9226cSAndreas Engel } 6420c9226cSAndreas Engel 6520c9226cSAndreas Engel return (int) data; 6620c9226cSAndreas Engel } 6720c9226cSAndreas Engel 68aed2fbefSSimon Glass static int pl01x_tstc(struct pl01x_regs *regs) 6920c9226cSAndreas Engel { 7020c9226cSAndreas Engel WATCHDOG_RESET(); 7172d5e44cSRabin Vincent return !(readl(®s->fr) & UART_PL01x_FR_RXFE); 7220c9226cSAndreas Engel } 7339f61477SMarek Vasut 74aed2fbefSSimon Glass static int pl01x_generic_serial_init(struct pl01x_regs *regs, 75aed2fbefSSimon Glass enum pl01x_type type) 76aed2fbefSSimon Glass { 77aed2fbefSSimon Glass switch (type) { 78aed2fbefSSimon Glass case TYPE_PL010: 79f7e517b4SVikas Manocha /* disable everything */ 80f7e517b4SVikas Manocha writel(0, ®s->pl010_cr); 81aed2fbefSSimon Glass break; 82d2ca9fd2SVikas Manocha case TYPE_PL011: 83eb8a4fe0SVikas Manocha #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT 84eb8a4fe0SVikas Manocha /* Empty RX fifo if necessary */ 85eb8a4fe0SVikas Manocha if (readl(®s->pl011_cr) & UART_PL011_CR_UARTEN) { 86eb8a4fe0SVikas Manocha while (!(readl(®s->fr) & UART_PL01x_FR_RXFE)) 87eb8a4fe0SVikas Manocha readl(®s->dr); 88eb8a4fe0SVikas Manocha } 89eb8a4fe0SVikas Manocha #endif 90f7e517b4SVikas Manocha /* disable everything */ 91f7e517b4SVikas Manocha writel(0, ®s->pl011_cr); 92d2ca9fd2SVikas Manocha break; 93d2ca9fd2SVikas Manocha default: 94d2ca9fd2SVikas Manocha return -EINVAL; 95d2ca9fd2SVikas Manocha } 96d2ca9fd2SVikas Manocha 97d2ca9fd2SVikas Manocha return 0; 98d2ca9fd2SVikas Manocha } 99d2ca9fd2SVikas Manocha 100d77447fdSLinus Walleij static int pl011_set_line_control(struct pl01x_regs *regs) 101d2ca9fd2SVikas Manocha { 102d2ca9fd2SVikas Manocha unsigned int lcr; 103d2ca9fd2SVikas Manocha /* 104d2ca9fd2SVikas Manocha * Internal update of baud rate register require line 105d2ca9fd2SVikas Manocha * control register write 106d2ca9fd2SVikas Manocha */ 107d2ca9fd2SVikas Manocha lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN; 108aed2fbefSSimon Glass #ifdef CONFIG_PL011_SERIAL_RLCR 109d2ca9fd2SVikas Manocha { 110aed2fbefSSimon Glass int i; 111aed2fbefSSimon Glass 112aed2fbefSSimon Glass /* 113aed2fbefSSimon Glass * Program receive line control register after waiting 114aed2fbefSSimon Glass * 10 bus cycles. Delay be writing to readonly register 115aed2fbefSSimon Glass * 10 times 116aed2fbefSSimon Glass */ 117aed2fbefSSimon Glass for (i = 0; i < 10; i++) 118aed2fbefSSimon Glass writel(lcr, ®s->fr); 119aed2fbefSSimon Glass 120aed2fbefSSimon Glass writel(lcr, ®s->pl011_rlcr); 121d2ca9fd2SVikas Manocha } 122aed2fbefSSimon Glass #endif 123d2ca9fd2SVikas Manocha writel(lcr, ®s->pl011_lcrh); 124aed2fbefSSimon Glass return 0; 125aed2fbefSSimon Glass } 126aed2fbefSSimon Glass 127aed2fbefSSimon Glass static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type, 128aed2fbefSSimon Glass int clock, int baudrate) 129aed2fbefSSimon Glass { 130aed2fbefSSimon Glass switch (type) { 131aed2fbefSSimon Glass case TYPE_PL010: { 132aed2fbefSSimon Glass unsigned int divisor; 133aed2fbefSSimon Glass 134d77447fdSLinus Walleij /* disable everything */ 135d77447fdSLinus Walleij writel(0, ®s->pl010_cr); 136d77447fdSLinus Walleij 137aed2fbefSSimon Glass switch (baudrate) { 138aed2fbefSSimon Glass case 9600: 139aed2fbefSSimon Glass divisor = UART_PL010_BAUD_9600; 140aed2fbefSSimon Glass break; 141aed2fbefSSimon Glass case 19200: 142aed2fbefSSimon Glass divisor = UART_PL010_BAUD_9600; 143aed2fbefSSimon Glass break; 144aed2fbefSSimon Glass case 38400: 145aed2fbefSSimon Glass divisor = UART_PL010_BAUD_38400; 146aed2fbefSSimon Glass break; 147aed2fbefSSimon Glass case 57600: 148aed2fbefSSimon Glass divisor = UART_PL010_BAUD_57600; 149aed2fbefSSimon Glass break; 150aed2fbefSSimon Glass case 115200: 151aed2fbefSSimon Glass divisor = UART_PL010_BAUD_115200; 152aed2fbefSSimon Glass break; 153aed2fbefSSimon Glass default: 154aed2fbefSSimon Glass divisor = UART_PL010_BAUD_38400; 155aed2fbefSSimon Glass } 156aed2fbefSSimon Glass 157aed2fbefSSimon Glass writel((divisor & 0xf00) >> 8, ®s->pl010_lcrm); 158aed2fbefSSimon Glass writel(divisor & 0xff, ®s->pl010_lcrl); 159aed2fbefSSimon Glass 160d77447fdSLinus Walleij /* 161d77447fdSLinus Walleij * Set line control for the PL010 to be 8 bits, 1 stop bit, 162d77447fdSLinus Walleij * no parity, fifo enabled 163d77447fdSLinus Walleij */ 164d77447fdSLinus Walleij writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN, 165d77447fdSLinus Walleij ®s->pl010_lcrh); 166aed2fbefSSimon Glass /* Finally, enable the UART */ 167aed2fbefSSimon Glass writel(UART_PL010_CR_UARTEN, ®s->pl010_cr); 168aed2fbefSSimon Glass break; 169aed2fbefSSimon Glass } 170aed2fbefSSimon Glass case TYPE_PL011: { 171aed2fbefSSimon Glass unsigned int temp; 172aed2fbefSSimon Glass unsigned int divider; 173aed2fbefSSimon Glass unsigned int remainder; 174aed2fbefSSimon Glass unsigned int fraction; 175aed2fbefSSimon Glass 176aed2fbefSSimon Glass /* 177aed2fbefSSimon Glass * Set baud rate 178aed2fbefSSimon Glass * 179aed2fbefSSimon Glass * IBRD = UART_CLK / (16 * BAUD_RATE) 180aed2fbefSSimon Glass * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) 181aed2fbefSSimon Glass * / (16 * BAUD_RATE)) 182aed2fbefSSimon Glass */ 183aed2fbefSSimon Glass temp = 16 * baudrate; 184aed2fbefSSimon Glass divider = clock / temp; 185aed2fbefSSimon Glass remainder = clock % temp; 186aed2fbefSSimon Glass temp = (8 * remainder) / baudrate; 187aed2fbefSSimon Glass fraction = (temp >> 1) + (temp & 1); 188aed2fbefSSimon Glass 189aed2fbefSSimon Glass writel(divider, ®s->pl011_ibrd); 190aed2fbefSSimon Glass writel(fraction, ®s->pl011_fbrd); 191aed2fbefSSimon Glass 192d77447fdSLinus Walleij pl011_set_line_control(regs); 193aed2fbefSSimon Glass /* Finally, enable the UART */ 194aed2fbefSSimon Glass writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | 195aed2fbefSSimon Glass UART_PL011_CR_RXE | UART_PL011_CR_RTS, ®s->pl011_cr); 196aed2fbefSSimon Glass break; 197aed2fbefSSimon Glass } 198aed2fbefSSimon Glass default: 199aed2fbefSSimon Glass return -EINVAL; 200aed2fbefSSimon Glass } 201aed2fbefSSimon Glass 202aed2fbefSSimon Glass return 0; 203aed2fbefSSimon Glass } 204aed2fbefSSimon Glass 205aed2fbefSSimon Glass #ifndef CONFIG_DM_SERIAL 206aed2fbefSSimon Glass static void pl01x_serial_init_baud(int baudrate) 207aed2fbefSSimon Glass { 208aed2fbefSSimon Glass int clock = 0; 209aed2fbefSSimon Glass 210aed2fbefSSimon Glass #if defined(CONFIG_PL010_SERIAL) 211aed2fbefSSimon Glass pl01x_type = TYPE_PL010; 212aed2fbefSSimon Glass #elif defined(CONFIG_PL011_SERIAL) 213aed2fbefSSimon Glass pl01x_type = TYPE_PL011; 214aed2fbefSSimon Glass clock = CONFIG_PL011_CLOCK; 215aed2fbefSSimon Glass #endif 216aed2fbefSSimon Glass base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX]; 217aed2fbefSSimon Glass 218aed2fbefSSimon Glass pl01x_generic_serial_init(base_regs, pl01x_type); 219a7deea69SVikas Manocha pl01x_generic_setbrg(base_regs, pl01x_type, clock, baudrate); 220aed2fbefSSimon Glass } 221aed2fbefSSimon Glass 222aed2fbefSSimon Glass /* 223aed2fbefSSimon Glass * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1 224aed2fbefSSimon Glass * Integrator CP has two UARTs, use the first one, at 38400-8-N-1 225aed2fbefSSimon Glass * Versatile PB has four UARTs. 226aed2fbefSSimon Glass */ 227aed2fbefSSimon Glass int pl01x_serial_init(void) 228aed2fbefSSimon Glass { 229aed2fbefSSimon Glass pl01x_serial_init_baud(CONFIG_BAUDRATE); 230aed2fbefSSimon Glass 231aed2fbefSSimon Glass return 0; 232aed2fbefSSimon Glass } 233aed2fbefSSimon Glass 234aed2fbefSSimon Glass static void pl01x_serial_putc(const char c) 235aed2fbefSSimon Glass { 236aed2fbefSSimon Glass if (c == '\n') 237aed2fbefSSimon Glass while (pl01x_putc(base_regs, '\r') == -EAGAIN); 238aed2fbefSSimon Glass 239aed2fbefSSimon Glass while (pl01x_putc(base_regs, c) == -EAGAIN); 240aed2fbefSSimon Glass } 241aed2fbefSSimon Glass 242aed2fbefSSimon Glass static int pl01x_serial_getc(void) 243aed2fbefSSimon Glass { 244aed2fbefSSimon Glass while (1) { 245aed2fbefSSimon Glass int ch = pl01x_getc(base_regs); 246aed2fbefSSimon Glass 247aed2fbefSSimon Glass if (ch == -EAGAIN) { 248aed2fbefSSimon Glass WATCHDOG_RESET(); 249aed2fbefSSimon Glass continue; 250aed2fbefSSimon Glass } 251aed2fbefSSimon Glass 252aed2fbefSSimon Glass return ch; 253aed2fbefSSimon Glass } 254aed2fbefSSimon Glass } 255aed2fbefSSimon Glass 256aed2fbefSSimon Glass static int pl01x_serial_tstc(void) 257aed2fbefSSimon Glass { 258aed2fbefSSimon Glass return pl01x_tstc(base_regs); 259aed2fbefSSimon Glass } 260aed2fbefSSimon Glass 261aed2fbefSSimon Glass static void pl01x_serial_setbrg(void) 262aed2fbefSSimon Glass { 263aed2fbefSSimon Glass /* 264aed2fbefSSimon Glass * Flush FIFO and wait for non-busy before changing baudrate to avoid 265aed2fbefSSimon Glass * crap in console 266aed2fbefSSimon Glass */ 267aed2fbefSSimon Glass while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE)) 268aed2fbefSSimon Glass WATCHDOG_RESET(); 269aed2fbefSSimon Glass while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY) 270aed2fbefSSimon Glass WATCHDOG_RESET(); 271aed2fbefSSimon Glass pl01x_serial_init_baud(gd->baudrate); 272aed2fbefSSimon Glass } 273aed2fbefSSimon Glass 27439f61477SMarek Vasut static struct serial_device pl01x_serial_drv = { 27539f61477SMarek Vasut .name = "pl01x_serial", 27639f61477SMarek Vasut .start = pl01x_serial_init, 27739f61477SMarek Vasut .stop = NULL, 27839f61477SMarek Vasut .setbrg = pl01x_serial_setbrg, 27939f61477SMarek Vasut .putc = pl01x_serial_putc, 280ec3fd689SMarek Vasut .puts = default_serial_puts, 28139f61477SMarek Vasut .getc = pl01x_serial_getc, 28239f61477SMarek Vasut .tstc = pl01x_serial_tstc, 28339f61477SMarek Vasut }; 28439f61477SMarek Vasut 28539f61477SMarek Vasut void pl01x_serial_initialize(void) 28639f61477SMarek Vasut { 28739f61477SMarek Vasut serial_register(&pl01x_serial_drv); 28839f61477SMarek Vasut } 28939f61477SMarek Vasut 29039f61477SMarek Vasut __weak struct serial_device *default_serial_console(void) 29139f61477SMarek Vasut { 29239f61477SMarek Vasut return &pl01x_serial_drv; 29339f61477SMarek Vasut } 294aed2fbefSSimon Glass 295aed2fbefSSimon Glass #endif /* nCONFIG_DM_SERIAL */ 2968a9cd5adSSimon Glass 2978a9cd5adSSimon Glass #ifdef CONFIG_DM_SERIAL 2988a9cd5adSSimon Glass 2998a9cd5adSSimon Glass struct pl01x_priv { 3008a9cd5adSSimon Glass struct pl01x_regs *regs; 3018a9cd5adSSimon Glass enum pl01x_type type; 3028a9cd5adSSimon Glass }; 3038a9cd5adSSimon Glass 3048a9cd5adSSimon Glass static int pl01x_serial_setbrg(struct udevice *dev, int baudrate) 3058a9cd5adSSimon Glass { 3068a9cd5adSSimon Glass struct pl01x_serial_platdata *plat = dev_get_platdata(dev); 3078a9cd5adSSimon Glass struct pl01x_priv *priv = dev_get_priv(dev); 3088a9cd5adSSimon Glass 3098a9cd5adSSimon Glass pl01x_generic_setbrg(priv->regs, priv->type, plat->clock, baudrate); 3108a9cd5adSSimon Glass 3118a9cd5adSSimon Glass return 0; 3128a9cd5adSSimon Glass } 3138a9cd5adSSimon Glass 3148a9cd5adSSimon Glass static int pl01x_serial_probe(struct udevice *dev) 3158a9cd5adSSimon Glass { 3168a9cd5adSSimon Glass struct pl01x_serial_platdata *plat = dev_get_platdata(dev); 3178a9cd5adSSimon Glass struct pl01x_priv *priv = dev_get_priv(dev); 3188a9cd5adSSimon Glass 3198a9cd5adSSimon Glass priv->regs = (struct pl01x_regs *)plat->base; 3208a9cd5adSSimon Glass priv->type = plat->type; 3218a9cd5adSSimon Glass return pl01x_generic_serial_init(priv->regs, priv->type); 3228a9cd5adSSimon Glass } 3238a9cd5adSSimon Glass 3248a9cd5adSSimon Glass static int pl01x_serial_getc(struct udevice *dev) 3258a9cd5adSSimon Glass { 3268a9cd5adSSimon Glass struct pl01x_priv *priv = dev_get_priv(dev); 3278a9cd5adSSimon Glass 3288a9cd5adSSimon Glass return pl01x_getc(priv->regs); 3298a9cd5adSSimon Glass } 3308a9cd5adSSimon Glass 3318a9cd5adSSimon Glass static int pl01x_serial_putc(struct udevice *dev, const char ch) 3328a9cd5adSSimon Glass { 3338a9cd5adSSimon Glass struct pl01x_priv *priv = dev_get_priv(dev); 3348a9cd5adSSimon Glass 3358a9cd5adSSimon Glass return pl01x_putc(priv->regs, ch); 3368a9cd5adSSimon Glass } 3378a9cd5adSSimon Glass 3388a9cd5adSSimon Glass static int pl01x_serial_pending(struct udevice *dev, bool input) 3398a9cd5adSSimon Glass { 3408a9cd5adSSimon Glass struct pl01x_priv *priv = dev_get_priv(dev); 3418a9cd5adSSimon Glass unsigned int fr = readl(&priv->regs->fr); 3428a9cd5adSSimon Glass 3438a9cd5adSSimon Glass if (input) 3448a9cd5adSSimon Glass return pl01x_tstc(priv->regs); 3458a9cd5adSSimon Glass else 3468a9cd5adSSimon Glass return fr & UART_PL01x_FR_TXFF ? 0 : 1; 3478a9cd5adSSimon Glass } 3488a9cd5adSSimon Glass 3498a9cd5adSSimon Glass static const struct dm_serial_ops pl01x_serial_ops = { 3508a9cd5adSSimon Glass .putc = pl01x_serial_putc, 3518a9cd5adSSimon Glass .pending = pl01x_serial_pending, 3528a9cd5adSSimon Glass .getc = pl01x_serial_getc, 3538a9cd5adSSimon Glass .setbrg = pl01x_serial_setbrg, 3548a9cd5adSSimon Glass }; 3558a9cd5adSSimon Glass 356*0f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL) 35769751729SVikas Manocha static const struct udevice_id pl01x_serial_id[] ={ 35869751729SVikas Manocha {.compatible = "arm,pl011", .data = TYPE_PL011}, 35969751729SVikas Manocha {.compatible = "arm,pl010", .data = TYPE_PL010}, 36069751729SVikas Manocha {} 36169751729SVikas Manocha }; 36269751729SVikas Manocha 36369751729SVikas Manocha static int pl01x_serial_ofdata_to_platdata(struct udevice *dev) 36469751729SVikas Manocha { 36569751729SVikas Manocha struct pl01x_serial_platdata *plat = dev_get_platdata(dev); 36669751729SVikas Manocha fdt_addr_t addr; 36769751729SVikas Manocha 36869751729SVikas Manocha addr = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); 36969751729SVikas Manocha if (addr == FDT_ADDR_T_NONE) 37069751729SVikas Manocha return -EINVAL; 37169751729SVikas Manocha 37269751729SVikas Manocha plat->base = addr; 37369751729SVikas Manocha plat->clock = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "clock", 1); 37469751729SVikas Manocha plat->type = dev_get_driver_data(dev); 37569751729SVikas Manocha return 0; 37669751729SVikas Manocha } 37769751729SVikas Manocha #endif 37869751729SVikas Manocha 3798a9cd5adSSimon Glass U_BOOT_DRIVER(serial_pl01x) = { 3808a9cd5adSSimon Glass .name = "serial_pl01x", 3818a9cd5adSSimon Glass .id = UCLASS_SERIAL, 38269751729SVikas Manocha .of_match = of_match_ptr(pl01x_serial_id), 38369751729SVikas Manocha .ofdata_to_platdata = of_match_ptr(pl01x_serial_ofdata_to_platdata), 38469751729SVikas Manocha .platdata_auto_alloc_size = sizeof(struct pl01x_serial_platdata), 3858a9cd5adSSimon Glass .probe = pl01x_serial_probe, 3868a9cd5adSSimon Glass .ops = &pl01x_serial_ops, 3878a9cd5adSSimon Glass .flags = DM_FLAG_PRE_RELOC, 38859c73d75SSimon Glass .priv_auto_alloc_size = sizeof(struct pl01x_priv), 3898a9cd5adSSimon Glass }; 3908a9cd5adSSimon Glass 3918a9cd5adSSimon Glass #endif 392