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 | * |
| 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 | |
| 28 | /* Simple U-Boot driver for the PrimeCell PL011 UARTs on the IntegratorCP */ |
| 29 | /* Should be fairly simple to make it work with the PL010 as well */ |
| 30 | |
| 31 | #include <common.h> |
| 32 | |
| 33 | #ifdef CFG_PL010_SERIAL |
| 34 | |
| 35 | #include "serial_pl011.h" |
| 36 | |
| 37 | #define IO_WRITE(addr, val) (*(volatile unsigned int *)(addr) = (val)) |
| 38 | #define IO_READ(addr) (*(volatile unsigned int *)(addr)) |
| 39 | |
| 40 | /* Integrator AP has two UARTs, we use the first one, at 38400-8-N-1 */ |
| 41 | #define NUM_PORTS 2 |
| 42 | #define CONSOLE_PORT CONFIG_CONS_INDEX |
| 43 | #define baudRate CONFIG_BAUDRATE |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 44 | static volatile unsigned char *const port[NUM_PORTS] = { |
| 45 | (void *) (CFG_SERIAL0), |
| 46 | (void *) (CFG_SERIAL1) |
| 47 | }; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 48 | |
| 49 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 50 | static void pl010_putc (int portnum, char c); |
| 51 | static int pl010_getc (int portnum); |
| 52 | static int pl010_tstc (int portnum); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | int serial_init (void) |
| 56 | { |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 57 | unsigned int divisor; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 58 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 59 | /* |
| 60 | ** First, disable everything. |
| 61 | */ |
| 62 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_CR, 0x0); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 63 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 64 | /* |
| 65 | ** Set baud rate |
| 66 | ** |
| 67 | */ |
| 68 | switch (baudRate) { |
| 69 | case 9600: |
| 70 | divisor = UART_PL010_BAUD_9600; |
| 71 | break; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 72 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 73 | case 19200: |
| 74 | divisor = UART_PL010_BAUD_9600; |
| 75 | break; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 76 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 77 | case 38400: |
| 78 | divisor = UART_PL010_BAUD_38400; |
| 79 | break; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 80 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 81 | case 57600: |
| 82 | divisor = UART_PL010_BAUD_57600; |
| 83 | break; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 84 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 85 | case 115200: |
| 86 | divisor = UART_PL010_BAUD_115200; |
| 87 | break; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 88 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 89 | default: |
| 90 | divisor = UART_PL010_BAUD_38400; |
| 91 | } |
| 92 | |
| 93 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_LCRM, |
| 94 | ((divisor & 0xf00) >> 8)); |
| 95 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_LCRL, (divisor & 0xff)); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 96 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 97 | /* |
| 98 | ** Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled. |
| 99 | */ |
| 100 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_LCRH, |
| 101 | (UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN)); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 102 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 103 | /* |
| 104 | ** Finally, enable the UART |
| 105 | */ |
| 106 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_CR, (UART_PL010_CR_UARTEN)); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 107 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 108 | return (0); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 109 | } |
| 110 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 111 | void serial_putc (const char c) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 112 | { |
| 113 | if (c == '\n') |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 114 | pl010_putc (CONSOLE_PORT, '\r'); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 115 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 116 | pl010_putc (CONSOLE_PORT, c); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 117 | } |
| 118 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 119 | void serial_puts (const char *s) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 120 | { |
| 121 | while (*s) { |
| 122 | serial_putc (*s++); |
| 123 | } |
| 124 | } |
| 125 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 126 | int serial_getc (void) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 127 | { |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 128 | return pl010_getc (CONSOLE_PORT); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 129 | } |
| 130 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 131 | int serial_tstc (void) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 132 | { |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 133 | return pl010_tstc (CONSOLE_PORT); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 134 | } |
| 135 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 136 | void serial_setbrg (void) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 137 | { |
| 138 | } |
| 139 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 140 | static void pl010_putc (int portnum, char c) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 141 | { |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 142 | /* Wait until there is space in the FIFO */ |
| 143 | while (IO_READ (port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_TXFF); |
| 144 | |
| 145 | /* Send the character */ |
| 146 | IO_WRITE (port[portnum] + UART_PL01x_DR, c); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 149 | static int pl010_getc (int portnum) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 150 | { |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 151 | unsigned int data; |
| 152 | |
| 153 | /* Wait until there is data in the FIFO */ |
| 154 | while (IO_READ (port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 155 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 156 | data = IO_READ (port[portnum] + UART_PL01x_DR); |
| 157 | |
| 158 | /* Check for an error flag */ |
| 159 | if (data & 0xFFFFFF00) { |
| 160 | /* Clear the error */ |
| 161 | IO_WRITE (port[portnum] + UART_PL01x_ECR, 0xFFFFFFFF); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | return (int) data; |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 166 | } |
| 167 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 168 | static int pl010_tstc (int portnum) |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 169 | { |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 170 | return !(IO_READ (port[portnum] + UART_PL01x_FR) & |
| 171 | UART_PL01x_FR_RXFE); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | #endif |