wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Marius Groeger <mgroeger@sysgo.de> |
| 8 | * |
| 9 | * (C) Copyright 2002 |
| 10 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 11 | * Alex Zuepke <azu@sysgo.de> |
| 12 | * |
| 13 | * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <common.h> |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 32 | #include <watchdog.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 33 | #include <asm/arch/pxa-regs.h> |
| 34 | |
| 35 | void serial_setbrg (void) |
| 36 | { |
| 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
| 39 | unsigned int quot = 0; |
| 40 | |
| 41 | if (gd->baudrate == 1200) |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 42 | quot = 768; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 43 | else if (gd->baudrate == 9600) |
| 44 | quot = 96; |
| 45 | else if (gd->baudrate == 19200) |
| 46 | quot = 48; |
| 47 | else if (gd->baudrate == 38400) |
| 48 | quot = 24; |
| 49 | else if (gd->baudrate == 57600) |
| 50 | quot = 16; |
| 51 | else if (gd->baudrate == 115200) |
| 52 | quot = 8; |
| 53 | else |
| 54 | hang (); |
| 55 | |
| 56 | #ifdef CONFIG_FFUART |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 57 | CKEN |= CKEN6_FFUART; |
| 58 | |
| 59 | FFIER = 0; /* Disable for now */ |
| 60 | FFFCR = 0; /* No fifos enabled */ |
| 61 | |
| 62 | /* set baud rate */ |
| 63 | FFLCR = LCR_WLS0 | LCR_WLS1 | LCR_DLAB; |
| 64 | FFDLL = quot & 0xff; |
| 65 | FFDLH = quot >> 8; |
| 66 | FFLCR = LCR_WLS0 | LCR_WLS1; |
| 67 | |
| 68 | FFIER = IER_UUE; /* Enable FFUART */ |
| 69 | |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 70 | #elif defined(CONFIG_BTUART) |
| 71 | CKEN |= CKEN7_BTUART; |
| 72 | |
| 73 | BTIER = 0; |
| 74 | BTFCR = 0; |
| 75 | |
| 76 | /* set baud rate */ |
| 77 | BTLCR = LCR_DLAB; |
| 78 | BTDLL = quot & 0xff; |
| 79 | BTDLH = quot >> 8; |
| 80 | BTLCR = LCR_WLS0 | LCR_WLS1; |
| 81 | |
| 82 | BTIER = IER_UUE; /* Enable BFUART */ |
| 83 | |
| 84 | #elif defined(CONFIG_STUART) |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 85 | CKEN |= CKEN5_STUART; |
| 86 | |
| 87 | STIER = 0; |
| 88 | STFCR = 0; |
| 89 | |
| 90 | /* set baud rate */ |
| 91 | STLCR = LCR_DLAB; |
| 92 | STDLL = quot & 0xff; |
| 93 | STDLH = quot >> 8; |
| 94 | STLCR = LCR_WLS0 | LCR_WLS1; |
| 95 | |
| 96 | STIER = IER_UUE; /* Enable STUART */ |
| 97 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | #else |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 99 | #error "Bad: you didn't configure serial ..." |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | #endif |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /* |
| 105 | * Initialise the serial port with the given baudrate. The settings |
| 106 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 107 | * |
| 108 | */ |
| 109 | int serial_init (void) |
| 110 | { |
| 111 | serial_setbrg (); |
| 112 | |
| 113 | return (0); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * Output a single byte to the serial port. |
| 119 | */ |
| 120 | void serial_putc (const char c) |
| 121 | { |
| 122 | #ifdef CONFIG_FFUART |
| 123 | /* wait for room in the tx FIFO on FFUART */ |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 124 | while ((FFLSR & LSR_TEMT) == 0) |
| 125 | WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | FFTHR = c; |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 127 | #elif defined(CONFIG_BTUART) |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 128 | while ((BTLSR & LSR_TEMT ) == 0 ) |
| 129 | WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */ |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 130 | BTTHR = c; |
| 131 | #elif defined(CONFIG_STUART) |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 132 | while ((STLSR & LSR_TEMT ) == 0 ) |
| 133 | WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */ |
| 134 | STTHR = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
| 137 | /* If \n, also do \r */ |
| 138 | if (c == '\n') |
| 139 | serial_putc ('\r'); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 144 | * otherwise. When the function is succesfull, the character read is |
| 145 | * written into its argument c. |
| 146 | */ |
| 147 | int serial_tstc (void) |
| 148 | { |
| 149 | #ifdef CONFIG_FFUART |
| 150 | return FFLSR & LSR_DR; |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 151 | #elif defined(CONFIG_BTUART) |
| 152 | return BTLSR & LSR_DR; |
| 153 | #elif defined(CONFIG_STUART) |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 154 | return STLSR & LSR_DR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | #endif |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 160 | * otherwise. When the function is succesfull, the character read is |
| 161 | * written into its argument c. |
| 162 | */ |
| 163 | int serial_getc (void) |
| 164 | { |
| 165 | #ifdef CONFIG_FFUART |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 166 | while (!(FFLSR & LSR_DR)) |
| 167 | WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 168 | return (char) FFRBR & 0xff; |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 169 | #elif defined(CONFIG_BTUART) |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 170 | while (!(BTLSR & LSR_DR)) |
| 171 | WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */ |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 172 | return (char) BTRBR & 0xff; |
| 173 | #elif defined(CONFIG_STUART) |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame^] | 174 | while (!(STLSR & LSR_DR)) |
| 175 | WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */ |
| 176 | return (char) STRBR & 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 177 | #endif |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | serial_puts (const char *s) |
| 182 | { |
| 183 | while (*s) { |
| 184 | serial_putc (*s++); |
| 185 | } |
| 186 | } |