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