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 | |
| 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 */ |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 41 | #define CONSOLE_PORT CONFIG_CONS_INDEX |
| 42 | #define baudRate CONFIG_BAUDRATE |
wdenk | 6705d81 | 2004-08-02 23:22:59 +0000 | [diff] [blame] | 43 | static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS; |
| 44 | #define NUM_PORTS (sizeof(port)/sizeof(port[0])) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 45 | |
| 46 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 47 | static void pl010_putc (int portnum, char c); |
| 48 | static int pl010_getc (int portnum); |
| 49 | static int pl010_tstc (int portnum); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | int serial_init (void) |
| 53 | { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 54 | unsigned int divisor; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 55 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 56 | /* |
| 57 | ** First, disable everything. |
| 58 | */ |
| 59 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_CR, 0x0); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 60 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 61 | /* |
| 62 | ** Set baud rate |
| 63 | ** |
| 64 | */ |
| 65 | switch (baudRate) { |
| 66 | case 9600: |
| 67 | divisor = UART_PL010_BAUD_9600; |
| 68 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 69 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 70 | case 19200: |
| 71 | divisor = UART_PL010_BAUD_9600; |
| 72 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 73 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 74 | case 38400: |
| 75 | divisor = UART_PL010_BAUD_38400; |
| 76 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 77 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 78 | case 57600: |
| 79 | divisor = UART_PL010_BAUD_57600; |
| 80 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 81 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 82 | case 115200: |
| 83 | divisor = UART_PL010_BAUD_115200; |
| 84 | break; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 85 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 86 | default: |
| 87 | divisor = UART_PL010_BAUD_38400; |
| 88 | } |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 89 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 90 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_LCRM, |
| 91 | ((divisor & 0xf00) >> 8)); |
| 92 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_LCRL, (divisor & 0xff)); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 93 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 94 | /* |
| 95 | ** Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled. |
| 96 | */ |
| 97 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_LCRH, |
| 98 | (UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN)); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 99 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 100 | /* |
| 101 | ** Finally, enable the UART |
| 102 | */ |
| 103 | IO_WRITE (port[CONSOLE_PORT] + UART_PL010_CR, (UART_PL010_CR_UARTEN)); |
| 104 | |
| 105 | return (0); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 108 | void serial_putc (const char c) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 109 | { |
| 110 | if (c == '\n') |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 111 | pl010_putc (CONSOLE_PORT, '\r'); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 112 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 113 | pl010_putc (CONSOLE_PORT, c); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 116 | void serial_puts (const char *s) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 117 | { |
| 118 | while (*s) { |
| 119 | serial_putc (*s++); |
| 120 | } |
| 121 | } |
| 122 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 123 | int serial_getc (void) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 124 | { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 125 | return pl010_getc (CONSOLE_PORT); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 126 | } |
| 127 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 128 | int serial_tstc (void) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 129 | { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 130 | return pl010_tstc (CONSOLE_PORT); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 131 | } |
| 132 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 133 | void serial_setbrg (void) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 134 | { |
| 135 | } |
| 136 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 137 | static void pl010_putc (int portnum, char c) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 138 | { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 139 | /* Wait until there is space in the FIFO */ |
| 140 | while (IO_READ (port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_TXFF); |
| 141 | |
| 142 | /* Send the character */ |
| 143 | IO_WRITE (port[portnum] + UART_PL01x_DR, c); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 146 | static int pl010_getc (int portnum) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 147 | { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 148 | unsigned int data; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 149 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 150 | /* Wait until there is data in the FIFO */ |
| 151 | while (IO_READ (port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE); |
| 152 | |
| 153 | data = IO_READ (port[portnum] + UART_PL01x_DR); |
| 154 | |
| 155 | /* Check for an error flag */ |
| 156 | if (data & 0xFFFFFF00) { |
| 157 | /* Clear the error */ |
| 158 | IO_WRITE (port[portnum] + UART_PL01x_ECR, 0xFFFFFFFF); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | return (int) data; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 163 | } |
| 164 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 165 | static int pl010_tstc (int portnum) |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 166 | { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 167 | return !(IO_READ (port[portnum] + UART_PL01x_FR) & |
| 168 | UART_PL01x_FR_RXFE); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #endif |