wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Rob Taylor, Flying Pig Systems. robt@flyingpig.com. |
| 4 | * |
| 5 | * (C) Copyright 2004 |
| 6 | * ARM Ltd. |
| 7 | * Philippe Robin, <philippe.robin@arm.com> |
| 8 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Andreas Engel | 0813b12 | 2008-09-08 14:30:53 +0200 | [diff] [blame] | 12 | /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */ |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 13 | |
| 14 | #include <common.h> |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 15 | #include <errno.h> |
Stuart Wood | 26136ef | 2008-06-02 16:42:19 -0400 | [diff] [blame] | 16 | #include <watchdog.h> |
Matt Waddel | d6ce53e | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 17 | #include <asm/io.h> |
Marek Vasut | 46e4d5f | 2012-09-14 22:38:46 +0200 | [diff] [blame] | 18 | #include <serial.h> |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 19 | #include <serial_pl01x.h> |
Marek Vasut | 46e4d5f | 2012-09-14 22:38:46 +0200 | [diff] [blame] | 20 | #include <linux/compiler.h> |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 21 | #include "serial_pl01x_internal.h" |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 22 | |
wdenk | da04a8b | 2004-08-02 23:22:59 +0000 | [diff] [blame] | 23 | static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS; |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 24 | static enum pl01x_type pl01x_type __attribute__ ((section(".data"))); |
| 25 | static struct pl01x_regs *base_regs __attribute__ ((section(".data"))); |
wdenk | da04a8b | 2004-08-02 23:22:59 +0000 | [diff] [blame] | 26 | #define NUM_PORTS (sizeof(port)/sizeof(port[0])) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 27 | |
Matt Waddel | d6ce53e | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 29 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 30 | static int pl01x_putc(struct pl01x_regs *regs, char c) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 31 | { |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 32 | /* Wait until there is space in the FIFO */ |
| 33 | if (readl(®s->fr) & UART_PL01x_FR_TXFF) |
| 34 | return -EAGAIN; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 35 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 36 | /* Send the character */ |
| 37 | writel(c, ®s->dr); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 38 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 39 | return 0; |
| 40 | } |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 41 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 42 | static int pl01x_getc(struct pl01x_regs *regs) |
| 43 | { |
| 44 | unsigned int data; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 45 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 46 | /* Wait until there is data in the FIFO */ |
| 47 | if (readl(®s->fr) & UART_PL01x_FR_RXFE) |
| 48 | return -EAGAIN; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 49 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 50 | data = readl(®s->dr); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 51 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 52 | /* Check for an error flag */ |
| 53 | if (data & 0xFFFFFF00) { |
| 54 | /* Clear the error */ |
| 55 | writel(0xFFFFFFFF, ®s->ecr); |
| 56 | return -1; |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 59 | return (int) data; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 62 | static int pl01x_tstc(struct pl01x_regs *regs) |
| 63 | { |
| 64 | WATCHDOG_RESET(); |
| 65 | return !(readl(®s->fr) & UART_PL01x_FR_RXFE); |
| 66 | } |
Andreas Engel | 8043861 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 67 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 68 | static int pl01x_generic_serial_init(struct pl01x_regs *regs, |
| 69 | enum pl01x_type type) |
Andreas Engel | 8043861 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 70 | { |
John Rigby | 34e21ee | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 71 | unsigned int lcr; |
| 72 | |
| 73 | #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 74 | if (type == TYPE_PL011) { |
| 75 | /* Empty RX fifo if necessary */ |
| 76 | if (readl(®s->pl011_cr) & UART_PL011_CR_UARTEN) { |
| 77 | while (!(readl(®s->fr) & UART_PL01x_FR_RXFE)) |
| 78 | readl(®s->dr); |
| 79 | } |
John Rigby | 34e21ee | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 80 | } |
| 81 | #endif |
Andreas Engel | 8043861 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 82 | |
Matt Waddel | d6ce53e | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 83 | /* First, disable everything */ |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 84 | writel(0, ®s->pl010_cr); |
Andreas Engel | 8043861 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 85 | |
Matt Waddel | d6ce53e | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 86 | /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */ |
John Rigby | 34e21ee | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 87 | lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN; |
| 88 | writel(lcr, ®s->pl011_lcrh); |
| 89 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 90 | switch (type) { |
| 91 | case TYPE_PL010: |
| 92 | break; |
| 93 | case TYPE_PL011: { |
John Rigby | 34e21ee | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 94 | #ifdef CONFIG_PL011_SERIAL_RLCR |
John Rigby | 34e21ee | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 95 | int i; |
| 96 | |
| 97 | /* |
| 98 | * Program receive line control register after waiting |
| 99 | * 10 bus cycles. Delay be writing to readonly register |
| 100 | * 10 times |
| 101 | */ |
| 102 | for (i = 0; i < 10; i++) |
| 103 | writel(lcr, ®s->fr); |
Andreas Engel | 8043861 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 104 | |
John Rigby | 34e21ee | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 105 | writel(lcr, ®s->pl011_rlcr); |
Mathieu J. Poirier | 4a036fe | 2012-08-03 11:05:12 +0000 | [diff] [blame] | 106 | /* lcrh needs to be set again for change to be effective */ |
| 107 | writel(lcr, ®s->pl011_lcrh); |
John Rigby | 34e21ee | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 108 | #endif |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 109 | break; |
| 110 | } |
| 111 | default: |
| 112 | return -EINVAL; |
| 113 | } |
Andreas Engel | 8043861 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 118 | static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type, |
| 119 | int clock, int baudrate) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 120 | { |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 121 | switch (type) { |
| 122 | case TYPE_PL010: { |
| 123 | unsigned int divisor; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 124 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 125 | switch (baudrate) { |
| 126 | case 9600: |
| 127 | divisor = UART_PL010_BAUD_9600; |
| 128 | break; |
| 129 | case 19200: |
| 130 | divisor = UART_PL010_BAUD_9600; |
| 131 | break; |
| 132 | case 38400: |
| 133 | divisor = UART_PL010_BAUD_38400; |
| 134 | break; |
| 135 | case 57600: |
| 136 | divisor = UART_PL010_BAUD_57600; |
| 137 | break; |
| 138 | case 115200: |
| 139 | divisor = UART_PL010_BAUD_115200; |
| 140 | break; |
| 141 | default: |
| 142 | divisor = UART_PL010_BAUD_38400; |
| 143 | } |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 144 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 145 | writel((divisor & 0xf00) >> 8, ®s->pl010_lcrm); |
| 146 | writel(divisor & 0xff, ®s->pl010_lcrl); |
| 147 | |
| 148 | /* Finally, enable the UART */ |
| 149 | writel(UART_PL010_CR_UARTEN, ®s->pl010_cr); |
| 150 | break; |
| 151 | } |
| 152 | case TYPE_PL011: { |
| 153 | unsigned int temp; |
| 154 | unsigned int divider; |
| 155 | unsigned int remainder; |
| 156 | unsigned int fraction; |
| 157 | |
| 158 | /* |
| 159 | * Set baud rate |
| 160 | * |
| 161 | * IBRD = UART_CLK / (16 * BAUD_RATE) |
| 162 | * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) |
| 163 | * / (16 * BAUD_RATE)) |
| 164 | */ |
| 165 | temp = 16 * baudrate; |
| 166 | divider = clock / temp; |
| 167 | remainder = clock % temp; |
| 168 | temp = (8 * remainder) / baudrate; |
| 169 | fraction = (temp >> 1) + (temp & 1); |
| 170 | |
| 171 | writel(divider, ®s->pl011_ibrd); |
| 172 | writel(fraction, ®s->pl011_fbrd); |
| 173 | |
| 174 | /* Finally, enable the UART */ |
| 175 | writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | |
| 176 | UART_PL011_CR_RXE | UART_PL011_CR_RTS, ®s->pl011_cr); |
| 177 | break; |
| 178 | } |
| 179 | default: |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | return 0; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 186 | #ifndef CONFIG_DM_SERIAL |
| 187 | static void pl01x_serial_init_baud(int baudrate) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 188 | { |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 189 | int clock = 0; |
| 190 | |
| 191 | #if defined(CONFIG_PL010_SERIAL) |
| 192 | pl01x_type = TYPE_PL010; |
| 193 | #elif defined(CONFIG_PL011_SERIAL) |
| 194 | pl01x_type = TYPE_PL011; |
| 195 | clock = CONFIG_PL011_CLOCK; |
| 196 | #endif |
| 197 | base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX]; |
| 198 | |
| 199 | pl01x_generic_serial_init(base_regs, pl01x_type); |
| 200 | pl01x_generic_setbrg(base_regs, TYPE_PL010, clock, baudrate); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 203 | /* |
| 204 | * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1 |
| 205 | * Integrator CP has two UARTs, use the first one, at 38400-8-N-1 |
| 206 | * Versatile PB has four UARTs. |
| 207 | */ |
| 208 | int pl01x_serial_init(void) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 209 | { |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 210 | pl01x_serial_init_baud(CONFIG_BAUDRATE); |
Linus Walleij | b8058e8 | 2011-10-02 11:52:52 +0000 | [diff] [blame] | 211 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 212 | return 0; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 215 | static void pl01x_serial_putc(const char c) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 216 | { |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 217 | if (c == '\n') |
| 218 | while (pl01x_putc(base_regs, '\r') == -EAGAIN); |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 219 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 220 | while (pl01x_putc(base_regs, c) == -EAGAIN); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 223 | static int pl01x_serial_getc(void) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 224 | { |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 225 | while (1) { |
| 226 | int ch = pl01x_getc(base_regs); |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 227 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 228 | if (ch == -EAGAIN) { |
| 229 | WATCHDOG_RESET(); |
| 230 | continue; |
| 231 | } |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 232 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 233 | return ch; |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 234 | } |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 237 | static int pl01x_serial_tstc(void) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 238 | { |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 239 | return pl01x_tstc(base_regs); |
| 240 | } |
Rabin Vincent | fb3c95f | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 241 | |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 242 | static void pl01x_serial_setbrg(void) |
| 243 | { |
| 244 | /* |
| 245 | * Flush FIFO and wait for non-busy before changing baudrate to avoid |
| 246 | * crap in console |
| 247 | */ |
| 248 | while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE)) |
| 249 | WATCHDOG_RESET(); |
| 250 | while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY) |
| 251 | WATCHDOG_RESET(); |
| 252 | pl01x_serial_init_baud(gd->baudrate); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 253 | } |
Marek Vasut | 46e4d5f | 2012-09-14 22:38:46 +0200 | [diff] [blame] | 254 | |
Marek Vasut | 46e4d5f | 2012-09-14 22:38:46 +0200 | [diff] [blame] | 255 | static struct serial_device pl01x_serial_drv = { |
| 256 | .name = "pl01x_serial", |
| 257 | .start = pl01x_serial_init, |
| 258 | .stop = NULL, |
| 259 | .setbrg = pl01x_serial_setbrg, |
| 260 | .putc = pl01x_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 261 | .puts = default_serial_puts, |
Marek Vasut | 46e4d5f | 2012-09-14 22:38:46 +0200 | [diff] [blame] | 262 | .getc = pl01x_serial_getc, |
| 263 | .tstc = pl01x_serial_tstc, |
| 264 | }; |
| 265 | |
| 266 | void pl01x_serial_initialize(void) |
| 267 | { |
| 268 | serial_register(&pl01x_serial_drv); |
| 269 | } |
| 270 | |
| 271 | __weak struct serial_device *default_serial_console(void) |
| 272 | { |
| 273 | return &pl01x_serial_drv; |
| 274 | } |
Simon Glass | f35484d | 2014-09-22 17:30:57 -0600 | [diff] [blame^] | 275 | |
| 276 | #endif /* nCONFIG_DM_SERIAL */ |