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 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
Andreas Engel | 48d0192 | 2008-09-08 14:30:53 +0200 | [diff] [blame] | 28 | /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */ |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 29 | |
| 30 | #include <common.h> |
Stuart Wood | 8b616ed | 2008-06-02 16:42:19 -0400 | [diff] [blame] | 31 | #include <watchdog.h> |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 32 | #include <asm/io.h> |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 33 | #include "serial_pl01x.h" |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 34 | |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 35 | /* |
| 36 | * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1 |
| 37 | * Integrator CP has two UARTs, use the first one, at 38400-8-N-1 |
| 38 | * Versatile PB has four UARTs. |
| 39 | */ |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 40 | #define CONSOLE_PORT CONFIG_CONS_INDEX |
wdenk | 6705d81 | 2004-08-02 23:22:59 +0000 | [diff] [blame] | 41 | static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS; |
| 42 | #define NUM_PORTS (sizeof(port)/sizeof(port[0])) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 43 | |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 44 | static void pl01x_putc (int portnum, char c); |
| 45 | static int pl01x_getc (int portnum); |
| 46 | static int pl01x_tstc (int portnum); |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 47 | unsigned int baudrate = CONFIG_BAUDRATE; |
| 48 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 49 | |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 50 | static struct pl01x_regs *pl01x_get_regs(int portnum) |
| 51 | { |
| 52 | return (struct pl01x_regs *) port[portnum]; |
| 53 | } |
| 54 | |
Andreas Engel | 48d0192 | 2008-09-08 14:30:53 +0200 | [diff] [blame] | 55 | #ifdef CONFIG_PL010_SERIAL |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 56 | |
| 57 | int serial_init (void) |
| 58 | { |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 59 | struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 60 | unsigned int divisor; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 61 | |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 62 | /* First, disable everything */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 63 | writel(0, ®s->pl010_cr); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 64 | |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 65 | /* Set baud rate */ |
| 66 | switch (baudrate) { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 67 | case 9600: |
| 68 | divisor = UART_PL010_BAUD_9600; |
| 69 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 70 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 71 | case 19200: |
| 72 | divisor = UART_PL010_BAUD_9600; |
| 73 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 74 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 75 | case 38400: |
| 76 | divisor = UART_PL010_BAUD_38400; |
| 77 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 78 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 79 | case 57600: |
| 80 | divisor = UART_PL010_BAUD_57600; |
| 81 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 82 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 83 | case 115200: |
| 84 | divisor = UART_PL010_BAUD_115200; |
| 85 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 86 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 87 | default: |
| 88 | divisor = UART_PL010_BAUD_38400; |
| 89 | } |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 90 | |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 91 | writel((divisor & 0xf00) >> 8, ®s->pl010_lcrm); |
| 92 | writel(divisor & 0xff, ®s->pl010_lcrl); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 93 | |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 94 | /* 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] | 95 | writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN, ®s->pl010_lcrh); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 96 | |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 97 | /* Finally, enable the UART */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 98 | writel(UART_PL010_CR_UARTEN, ®s->pl010_cr); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 99 | |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 100 | return 0; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Andreas Engel | 48d0192 | 2008-09-08 14:30:53 +0200 | [diff] [blame] | 103 | #endif /* CONFIG_PL010_SERIAL */ |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 104 | |
Andreas Engel | 48d0192 | 2008-09-08 14:30:53 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_PL011_SERIAL |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 106 | |
| 107 | int serial_init (void) |
| 108 | { |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 109 | struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT); |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 110 | unsigned int temp; |
| 111 | unsigned int divider; |
| 112 | unsigned int remainder; |
| 113 | unsigned int fraction; |
John Rigby | 910f1ae | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 114 | unsigned int lcr; |
| 115 | |
| 116 | #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT |
| 117 | /* Empty RX fifo if necessary */ |
| 118 | if (readl(®s->pl011_cr) & UART_PL011_CR_UARTEN) { |
| 119 | while (!(readl(®s->fr) & UART_PL01x_FR_RXFE)) |
| 120 | readl(®s->dr); |
| 121 | } |
| 122 | #endif |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 123 | |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 124 | /* First, disable everything */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 125 | writel(0, ®s->pl011_cr); |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 126 | |
| 127 | /* |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 128 | * Set baud rate |
| 129 | * |
| 130 | * IBRD = UART_CLK / (16 * BAUD_RATE) |
| 131 | * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE)) |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 132 | */ |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 133 | temp = 16 * baudrate; |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 134 | divider = CONFIG_PL011_CLOCK / temp; |
| 135 | remainder = CONFIG_PL011_CLOCK % temp; |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 136 | temp = (8 * remainder) / baudrate; |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 137 | fraction = (temp >> 1) + (temp & 1); |
| 138 | |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 139 | writel(divider, ®s->pl011_ibrd); |
| 140 | writel(fraction, ®s->pl011_fbrd); |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 141 | |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 142 | /* 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] | 143 | lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN; |
| 144 | writel(lcr, ®s->pl011_lcrh); |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 145 | |
John Rigby | 910f1ae | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 146 | #ifdef CONFIG_PL011_SERIAL_RLCR |
| 147 | { |
| 148 | int i; |
| 149 | |
| 150 | /* |
| 151 | * Program receive line control register after waiting |
| 152 | * 10 bus cycles. Delay be writing to readonly register |
| 153 | * 10 times |
| 154 | */ |
| 155 | for (i = 0; i < 10; i++) |
| 156 | writel(lcr, ®s->fr); |
| 157 | |
| 158 | writel(lcr, ®s->pl011_rlcr); |
Mathieu J. Poirier | 84dee30 | 2012-08-03 11:05:12 +0000 | [diff] [blame^] | 159 | /* lcrh needs to be set again for change to be effective */ |
| 160 | writel(lcr, ®s->pl011_lcrh); |
John Rigby | 910f1ae | 2011-04-19 10:42:39 +0000 | [diff] [blame] | 161 | } |
| 162 | #endif |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 163 | /* Finally, enable the UART */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 164 | writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE, |
| 165 | ®s->pl011_cr); |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Andreas Engel | 48d0192 | 2008-09-08 14:30:53 +0200 | [diff] [blame] | 170 | #endif /* CONFIG_PL011_SERIAL */ |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 171 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 172 | void serial_putc (const char c) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 173 | { |
| 174 | if (c == '\n') |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 175 | pl01x_putc (CONSOLE_PORT, '\r'); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 176 | |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 177 | pl01x_putc (CONSOLE_PORT, c); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 178 | } |
| 179 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 180 | void serial_puts (const char *s) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 181 | { |
| 182 | while (*s) { |
| 183 | serial_putc (*s++); |
| 184 | } |
| 185 | } |
| 186 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 187 | int serial_getc (void) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 188 | { |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 189 | return pl01x_getc (CONSOLE_PORT); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 190 | } |
| 191 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 192 | int serial_tstc (void) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 193 | { |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 194 | return pl01x_tstc (CONSOLE_PORT); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 195 | } |
| 196 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 197 | void serial_setbrg (void) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 198 | { |
Linus Walleij | 96baa4c | 2011-10-02 11:52:52 +0000 | [diff] [blame] | 199 | struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT); |
| 200 | |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 201 | baudrate = gd->baudrate; |
Linus Walleij | 96baa4c | 2011-10-02 11:52:52 +0000 | [diff] [blame] | 202 | /* |
| 203 | * Flush FIFO and wait for non-busy before changing baudrate to avoid |
| 204 | * crap in console |
| 205 | */ |
| 206 | while (!(readl(®s->fr) & UART_PL01x_FR_TXFE)) |
| 207 | WATCHDOG_RESET(); |
| 208 | while (readl(®s->fr) & UART_PL01x_FR_BUSY) |
| 209 | WATCHDOG_RESET(); |
Matt Waddel | 249d521 | 2010-10-07 15:48:46 -0600 | [diff] [blame] | 210 | serial_init(); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 213 | static void pl01x_putc (int portnum, char c) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 214 | { |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 215 | struct pl01x_regs *regs = pl01x_get_regs(portnum); |
| 216 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 217 | /* Wait until there is space in the FIFO */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 218 | while (readl(®s->fr) & UART_PL01x_FR_TXFF) |
Stuart Wood | 8b616ed | 2008-06-02 16:42:19 -0400 | [diff] [blame] | 219 | WATCHDOG_RESET(); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 220 | |
| 221 | /* Send the character */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 222 | writel(c, ®s->dr); |
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_getc (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); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 228 | unsigned int data; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 229 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 230 | /* Wait until there is data in the FIFO */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 231 | while (readl(®s->fr) & UART_PL01x_FR_RXFE) |
Stuart Wood | 8b616ed | 2008-06-02 16:42:19 -0400 | [diff] [blame] | 232 | WATCHDOG_RESET(); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 233 | |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 234 | data = readl(®s->dr); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 235 | |
| 236 | /* Check for an error flag */ |
| 237 | if (data & 0xFFFFFF00) { |
| 238 | /* Clear the error */ |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 239 | writel(0xFFFFFFFF, ®s->ecr); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | return (int) data; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Andreas Engel | 20c9226 | 2008-09-08 10:17:31 +0200 | [diff] [blame] | 246 | static int pl01x_tstc (int portnum) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 247 | { |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 248 | struct pl01x_regs *regs = pl01x_get_regs(portnum); |
| 249 | |
Stuart Wood | 8b616ed | 2008-06-02 16:42:19 -0400 | [diff] [blame] | 250 | WATCHDOG_RESET(); |
Rabin Vincent | 72d5e44 | 2010-05-05 09:23:07 +0530 | [diff] [blame] | 251 | return !(readl(®s->fr) & UART_PL01x_FR_RXFE); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 252 | } |