wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * DAVE Srl |
| 4 | * http://www.dave-tech.it |
| 5 | * http://www.wawnet.biz |
| 6 | * mailto:info@wawnet.biz |
| 7 | * |
| 8 | * (C) Copyright 2002-2004 |
| 9 | * Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
| 10 | * |
| 11 | * (C) Copyright 2002 |
| 12 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 13 | * Marius Groeger <mgroeger@sysgo.de> |
| 14 | * |
| 15 | * (C) Copyright 2002 |
| 16 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 17 | * Alex Zuepke <azu@sysgo.de> |
| 18 | * |
| 19 | * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) |
| 20 | * |
| 21 | * This program is free software; you can redistribute it and/or modify |
| 22 | * it under the terms of the GNU General Public License as published by |
| 23 | * the Free Software Foundation; either version 2 of the License, or |
| 24 | * (at your option) any later version. |
| 25 | * |
| 26 | * This program is distributed in the hope that it will be useful, |
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | * GNU General Public License for more details. |
| 30 | * |
| 31 | * You should have received a copy of the GNU General Public License |
| 32 | * along with this program; if not, write to the Free Software |
| 33 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | #include <common.h> |
| 38 | #include <asm/hardware.h> |
| 39 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 40 | DECLARE_GLOBAL_DATA_PTR; |
| 41 | |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 42 | /* flush serial input queue. returns 0 on success or negative error |
| 43 | * number otherwise |
| 44 | */ |
| 45 | static int serial_flush_input(void) |
| 46 | { |
| 47 | volatile u32 tmp; |
| 48 | |
| 49 | /* keep on reading as long as the receiver is not empty */ |
| 50 | while(UTRSTAT0&0x01) { |
| 51 | tmp = REGB(URXH0); |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /* flush output queue. returns 0 on success or negative error number |
| 59 | * otherwise |
| 60 | */ |
| 61 | static int serial_flush_output(void) |
| 62 | { |
| 63 | /* wait until the transmitter is no longer busy */ |
| 64 | while(!(UTRSTAT0 & 0x02)) { |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 71 | static void s3c44b0_serial_setbrg(void) |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 72 | { |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 73 | u32 divisor = 0; |
| 74 | |
| 75 | /* get correct divisor */ |
| 76 | switch(gd->baudrate) { |
| 77 | |
| 78 | case 1200: |
| 79 | #if CONFIG_S3C44B0_CLOCK_SPEED==66 |
| 80 | divisor = 3124; |
| 81 | #elif CONFIG_S3C44B0_CLOCK_SPEED==75 |
| 82 | divisor = 3905; |
| 83 | #else |
| 84 | # error CONFIG_S3C44B0_CLOCK_SPEED undefined |
| 85 | #endif |
| 86 | break; |
| 87 | |
| 88 | case 9600: |
| 89 | #if CONFIG_S3C44B0_CLOCK_SPEED==66 |
| 90 | divisor = 390; |
| 91 | #elif CONFIG_S3C44B0_CLOCK_SPEED==75 |
| 92 | divisor = 487; |
| 93 | #else |
| 94 | # error CONFIG_S3C44B0_CLOCK_SPEED undefined |
| 95 | #endif |
| 96 | break; |
| 97 | |
| 98 | case 19200: |
| 99 | #if CONFIG_S3C44B0_CLOCK_SPEED==66 |
| 100 | divisor = 194; |
| 101 | #elif CONFIG_S3C44B0_CLOCK_SPEED==75 |
| 102 | divisor = 243; |
| 103 | #else |
| 104 | # error CONFIG_S3C44B0_CLOCK_SPEED undefined |
| 105 | #endif |
| 106 | break; |
| 107 | |
| 108 | case 38400: |
| 109 | #if CONFIG_S3C44B0_CLOCK_SPEED==66 |
| 110 | divisor = 97; |
| 111 | #elif CONFIG_S3C44B0_CLOCK_SPEED==75 |
| 112 | divisor = 121; |
| 113 | #else |
| 114 | # error CONFIG_S3C44B0_CLOCK_SPEED undefined |
wdenk | ce4832c | 2004-10-17 21:12:06 +0000 | [diff] [blame] | 115 | #endif /* break; */ |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 116 | |
| 117 | case 57600: |
| 118 | #if CONFIG_S3C44B0_CLOCK_SPEED==66 |
| 119 | divisor = 64; |
| 120 | #elif CONFIG_S3C44B0_CLOCK_SPEED==75 |
| 121 | divisor = 80; |
| 122 | #else |
| 123 | # error CONFIG_S3C44B0_CLOCK_SPEED undefined |
wdenk | ce4832c | 2004-10-17 21:12:06 +0000 | [diff] [blame] | 124 | #endif /* break; */ |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 125 | |
| 126 | case 115200: |
| 127 | #if CONFIG_S3C44B0_CLOCK_SPEED==66 |
| 128 | divisor = 32; |
| 129 | #elif CONFIG_S3C44B0_CLOCK_SPEED==75 |
| 130 | divisor = 40; |
| 131 | #else |
| 132 | # error CONFIG_S3C44B0_CLOCK_SPEED undefined |
wdenk | ce4832c | 2004-10-17 21:12:06 +0000 | [diff] [blame] | 133 | #endif /* break; */ |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | serial_flush_output(); |
| 137 | serial_flush_input(); |
| 138 | UFCON0 = 0x0; |
| 139 | ULCON0 = 0x03; |
| 140 | UCON0 = 0x05; |
| 141 | UBRDIV0 = divisor; |
| 142 | |
| 143 | UFCON1 = 0x0; |
| 144 | ULCON1 = 0x03; |
| 145 | UCON1 = 0x05; |
| 146 | UBRDIV1 = divisor; |
| 147 | |
| 148 | for(divisor=0; divisor<100; divisor++) { |
| 149 | /* NOP */ |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /* |
| 155 | * Initialise the serial port with the given baudrate. The settings |
| 156 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 157 | * |
| 158 | */ |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 159 | static int s3c44b0_serial_init(void) |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 160 | { |
| 161 | serial_setbrg (); |
| 162 | |
| 163 | return (0); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * Output a single byte to the serial port. |
| 169 | */ |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 170 | static void s3c44b0_serial_putc(const char c) |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 171 | { |
| 172 | /* wait for room in the transmit FIFO */ |
| 173 | while(!(UTRSTAT0 & 0x02)); |
| 174 | |
| 175 | UTXH0 = (unsigned char)c; |
| 176 | |
| 177 | /* |
| 178 | to be polite with serial console add a line feed |
| 179 | to the carriage return character |
| 180 | */ |
| 181 | if (c=='\n') |
| 182 | serial_putc('\r'); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 187 | * otherwise. When the function is succesfull, the character read is |
| 188 | * written into its argument c. |
| 189 | */ |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 190 | static int s3c44b0_serial_tstc(void) |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 191 | { |
| 192 | return (UTRSTAT0 & 0x01); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 197 | * otherwise. When the function is succesfull, the character read is |
| 198 | * written into its argument c. |
| 199 | */ |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 200 | static int s3c44b0_serial_getc(void) |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 201 | { |
| 202 | int rv; |
| 203 | |
| 204 | for(;;) { |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 205 | rv = s3c44b0_serial_tstc(); |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 206 | |
| 207 | if(rv > 0) |
| 208 | return URXH0; |
| 209 | } |
| 210 | } |
| 211 | |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 212 | static void s3c44b0_serial_puts(const char *s) |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 213 | { |
| 214 | while (*s) { |
| 215 | serial_putc (*s++); |
| 216 | } |
| 217 | } |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 218 | |
Marek Vasut | 9776a44 | 2012-09-14 22:39:19 +0200 | [diff] [blame] | 219 | static struct serial_device s3c44b0_serial_drv = { |
| 220 | .name = "s3c44b0_serial", |
| 221 | .start = s3c44b0_serial_init, |
| 222 | .stop = NULL, |
| 223 | .setbrg = s3c44b0_serial_setbrg, |
| 224 | .putc = s3c44b0_serial_putc, |
| 225 | .puts = s3c44b0_serial_puts, |
| 226 | .getc = s3c44b0_serial_getc, |
| 227 | .tstc = s3c44b0_serial_tstc, |
| 228 | }; |
| 229 | |
| 230 | void s3c44b0_serial_initialize(void) |
| 231 | { |
| 232 | serial_register(&s3c44b0_serial_drv); |
| 233 | } |
| 234 | |
| 235 | __weak struct serial_device *default_serial_console(void) |
| 236 | { |
| 237 | return &s3c44b0_serial_drv; |
| 238 | } |