wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 - 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | * |
| 23 | * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 19-Oct-00, with |
Stefan Roese | 88fbf93 | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 24 | * changes based on the file arch/powerpc/mbxboot/m8260_tty.c from the |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 25 | * Linux/PPC sources (m8260_tty.c had no copyright info in it). |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 26 | * |
| 27 | * Martin Krause, 8 Jun 2006 |
| 28 | * Added CONFIG_SERIAL_MULTI support |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Minimal serial functions needed to use one of the PSC ports |
| 33 | * as serial console interface. |
| 34 | */ |
| 35 | |
| 36 | #include <common.h> |
| 37 | #include <mpc5xxx.h> |
| 38 | |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 39 | #if defined (CONFIG_SERIAL_MULTI) |
| 40 | #include <serial.h> |
| 41 | #endif |
| 42 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 43 | DECLARE_GLOBAL_DATA_PTR; |
| 44 | |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 45 | #if defined(CONFIG_PSC_CONSOLE) |
| 46 | |
| 47 | #if CONFIG_PSC_CONSOLE == 1 |
| 48 | #define PSC_BASE MPC5XXX_PSC1 |
| 49 | #elif CONFIG_PSC_CONSOLE == 2 |
| 50 | #define PSC_BASE MPC5XXX_PSC2 |
| 51 | #elif CONFIG_PSC_CONSOLE == 3 |
| 52 | #define PSC_BASE MPC5XXX_PSC3 |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 53 | #elif CONFIG_PSC_CONSOLE == 4 |
| 54 | #define PSC_BASE MPC5XXX_PSC4 |
| 55 | #elif CONFIG_PSC_CONSOLE == 5 |
| 56 | #define PSC_BASE MPC5XXX_PSC5 |
| 57 | #elif CONFIG_PSC_CONSOLE == 6 |
| 58 | #define PSC_BASE MPC5XXX_PSC6 |
| 59 | #else |
| 60 | #error CONFIG_PSC_CONSOLE must be in 1 ... 6 |
| 61 | #endif |
| 62 | |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 63 | #if defined(CONFIG_SERIAL_MULTI) && !defined(CONFIG_PSC_CONSOLE2) |
| 64 | #error you must define CONFIG_PSC_CONSOLE2 if CONFIG_SERIAL_MULTI is set |
| 65 | #endif |
| 66 | |
| 67 | #if defined(CONFIG_SERIAL_MULTI) |
| 68 | #if CONFIG_PSC_CONSOLE2 == 1 |
| 69 | #define PSC_BASE2 MPC5XXX_PSC1 |
| 70 | #elif CONFIG_PSC_CONSOLE2 == 2 |
| 71 | #define PSC_BASE2 MPC5XXX_PSC2 |
| 72 | #elif CONFIG_PSC_CONSOLE2 == 3 |
| 73 | #define PSC_BASE2 MPC5XXX_PSC3 |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 74 | #elif CONFIG_PSC_CONSOLE2 == 4 |
| 75 | #define PSC_BASE2 MPC5XXX_PSC4 |
| 76 | #elif CONFIG_PSC_CONSOLE2 == 5 |
| 77 | #define PSC_BASE2 MPC5XXX_PSC5 |
| 78 | #elif CONFIG_PSC_CONSOLE2 == 6 |
| 79 | #define PSC_BASE2 MPC5XXX_PSC6 |
| 80 | #else |
| 81 | #error CONFIG_PSC_CONSOLE2 must be in 1 ... 6 |
| 82 | #endif |
| 83 | #endif /* CONFIG_SERIAL_MULTI */ |
| 84 | |
| 85 | #if defined(CONFIG_SERIAL_MULTI) |
| 86 | int serial_init_dev (unsigned long dev_base) |
| 87 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 88 | int serial_init (void) |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 89 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 90 | { |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 91 | #if defined(CONFIG_SERIAL_MULTI) |
| 92 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 93 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 94 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 95 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 96 | unsigned long baseclk; |
| 97 | int div; |
| 98 | |
| 99 | /* reset PSC */ |
| 100 | psc->command = PSC_SEL_MODE_REG_1; |
| 101 | |
| 102 | /* select clock sources */ |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 103 | psc->psc_clock_select = 0; |
wdenk | ca26216 | 2004-02-09 20:51:26 +0000 | [diff] [blame] | 104 | baseclk = (gd->ipb_clk + 16) / 32; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 105 | |
| 106 | /* switch to UART mode */ |
| 107 | psc->sicr = 0; |
| 108 | |
| 109 | /* configure parity, bit length and so on */ |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 110 | psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 111 | psc->mode = PSC_MODE_ONE_STOP; |
| 112 | |
| 113 | /* set up UART divisor */ |
wdenk | ca26216 | 2004-02-09 20:51:26 +0000 | [diff] [blame] | 114 | div = (baseclk + (gd->baudrate/2)) / gd->baudrate; |
| 115 | psc->ctur = (div >> 8) & 0xff; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 116 | psc->ctlr = div & 0xff; |
| 117 | |
| 118 | /* disable all interrupts */ |
| 119 | psc->psc_imr = 0; |
| 120 | |
| 121 | /* reset and enable Rx/Tx */ |
| 122 | psc->command = PSC_RST_RX; |
| 123 | psc->command = PSC_RST_TX; |
| 124 | psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE; |
| 125 | |
| 126 | return (0); |
| 127 | } |
| 128 | |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 129 | #if defined(CONFIG_SERIAL_MULTI) |
| 130 | void serial_putc_dev (unsigned long dev_base, const char c) |
| 131 | #else |
| 132 | void serial_putc(const char c) |
| 133 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 134 | { |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 135 | #if defined(CONFIG_SERIAL_MULTI) |
| 136 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 137 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 138 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 139 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 140 | |
| 141 | if (c == '\n') |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 142 | #if defined(CONFIG_SERIAL_MULTI) |
| 143 | serial_putc_dev (dev_base, '\r'); |
| 144 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 145 | serial_putc('\r'); |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 146 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 147 | |
| 148 | /* Wait for last character to go. */ |
| 149 | while (!(psc->psc_status & PSC_SR_TXEMP)) |
| 150 | ; |
| 151 | |
| 152 | psc->psc_buffer_8 = c; |
| 153 | } |
| 154 | |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 155 | #if defined(CONFIG_SERIAL_MULTI) |
Wolfgang Denk | d4321d3 | 2006-08-30 23:09:00 +0200 | [diff] [blame] | 156 | void serial_putc_raw_dev(unsigned long dev_base, const char c) |
| 157 | #else |
| 158 | void serial_putc_raw(const char c) |
| 159 | #endif |
| 160 | { |
| 161 | #if defined(CONFIG_SERIAL_MULTI) |
| 162 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 163 | #else |
| 164 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
| 165 | #endif |
| 166 | /* Wait for last character to go. */ |
| 167 | while (!(psc->psc_status & PSC_SR_TXEMP)) |
| 168 | ; |
| 169 | |
| 170 | psc->psc_buffer_8 = c; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | #if defined(CONFIG_SERIAL_MULTI) |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 175 | void serial_puts_dev (unsigned long dev_base, const char *s) |
| 176 | #else |
| 177 | void serial_puts (const char *s) |
| 178 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 179 | { |
| 180 | while (*s) { |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 181 | #if defined(CONFIG_SERIAL_MULTI) |
| 182 | serial_putc_dev (dev_base, *s++); |
| 183 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 184 | serial_putc (*s++); |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 185 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 189 | #if defined(CONFIG_SERIAL_MULTI) |
| 190 | int serial_getc_dev (unsigned long dev_base) |
| 191 | #else |
| 192 | int serial_getc(void) |
| 193 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 194 | { |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 195 | #if defined(CONFIG_SERIAL_MULTI) |
| 196 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 197 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 198 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 199 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 200 | |
| 201 | /* Wait for a character to arrive. */ |
| 202 | while (!(psc->psc_status & PSC_SR_RXRDY)) |
| 203 | ; |
| 204 | |
| 205 | return psc->psc_buffer_8; |
| 206 | } |
| 207 | |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 208 | #if defined(CONFIG_SERIAL_MULTI) |
| 209 | int serial_tstc_dev (unsigned long dev_base) |
| 210 | #else |
| 211 | int serial_tstc(void) |
| 212 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 213 | { |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 214 | #if defined(CONFIG_SERIAL_MULTI) |
| 215 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 216 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 217 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 218 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 219 | |
| 220 | return (psc->psc_status & PSC_SR_RXRDY); |
| 221 | } |
| 222 | |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 223 | #if defined(CONFIG_SERIAL_MULTI) |
| 224 | void serial_setbrg_dev (unsigned long dev_base) |
| 225 | #else |
| 226 | void serial_setbrg(void) |
| 227 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 228 | { |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 229 | #if defined(CONFIG_SERIAL_MULTI) |
| 230 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 231 | #else |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 232 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 233 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 234 | unsigned long baseclk, div; |
| 235 | |
wdenk | a2b932d | 2005-06-27 13:30:03 +0000 | [diff] [blame] | 236 | baseclk = (gd->ipb_clk + 16) / 32; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 237 | |
| 238 | /* set up UART divisor */ |
wdenk | a2b932d | 2005-06-27 13:30:03 +0000 | [diff] [blame] | 239 | div = (baseclk + (gd->baudrate/2)) / gd->baudrate; |
Wolfgang Denk | 42a1419 | 2005-09-25 01:09:58 +0200 | [diff] [blame] | 240 | psc->ctur = (div >> 8) & 0xFF; |
| 241 | psc->ctlr = div & 0xff; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 242 | } |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 243 | |
| 244 | #if defined(CONFIG_SERIAL_MULTI) |
Wolfgang Denk | d4321d3 | 2006-08-30 23:09:00 +0200 | [diff] [blame] | 245 | void serial_setrts_dev (unsigned long dev_base, int s) |
| 246 | #else |
| 247 | void serial_setrts(int s) |
| 248 | #endif |
| 249 | { |
| 250 | #if defined(CONFIG_SERIAL_MULTI) |
| 251 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 252 | #else |
| 253 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
| 254 | #endif |
| 255 | |
| 256 | if (s) { |
| 257 | /* Assert RTS (become LOW) */ |
| 258 | psc->op1 = 0x1; |
| 259 | } |
| 260 | else { |
| 261 | /* Negate RTS (become HIGH) */ |
| 262 | psc->op0 = 0x1; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #if defined(CONFIG_SERIAL_MULTI) |
| 267 | int serial_getcts_dev (unsigned long dev_base) |
| 268 | #else |
| 269 | int serial_getcts(void) |
| 270 | #endif |
| 271 | { |
| 272 | #if defined(CONFIG_SERIAL_MULTI) |
| 273 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base; |
| 274 | #else |
| 275 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
| 276 | #endif |
| 277 | |
| 278 | return (psc->ip & 0x1) ? 0 : 1; |
| 279 | } |
| 280 | |
| 281 | #if defined(CONFIG_SERIAL_MULTI) |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 282 | int serial0_init(void) |
| 283 | { |
| 284 | return (serial_init_dev(PSC_BASE)); |
| 285 | } |
| 286 | |
| 287 | int serial1_init(void) |
| 288 | { |
| 289 | return (serial_init_dev(PSC_BASE2)); |
| 290 | } |
| 291 | void serial0_setbrg (void) |
| 292 | { |
| 293 | serial_setbrg_dev(PSC_BASE); |
| 294 | } |
| 295 | void serial1_setbrg (void) |
| 296 | { |
| 297 | serial_setbrg_dev(PSC_BASE2); |
| 298 | } |
| 299 | |
| 300 | void serial0_putc(const char c) |
| 301 | { |
| 302 | serial_putc_dev(PSC_BASE,c); |
| 303 | } |
| 304 | |
| 305 | void serial1_putc(const char c) |
| 306 | { |
| 307 | serial_putc_dev(PSC_BASE2, c); |
| 308 | } |
| 309 | void serial0_puts(const char *s) |
| 310 | { |
| 311 | serial_puts_dev(PSC_BASE, s); |
| 312 | } |
| 313 | |
| 314 | void serial1_puts(const char *s) |
| 315 | { |
| 316 | serial_puts_dev(PSC_BASE2, s); |
| 317 | } |
| 318 | |
| 319 | int serial0_getc(void) |
| 320 | { |
| 321 | return(serial_getc_dev(PSC_BASE)); |
| 322 | } |
| 323 | |
| 324 | int serial1_getc(void) |
| 325 | { |
| 326 | return(serial_getc_dev(PSC_BASE2)); |
| 327 | } |
| 328 | int serial0_tstc(void) |
| 329 | { |
| 330 | return (serial_tstc_dev(PSC_BASE)); |
| 331 | } |
| 332 | |
| 333 | int serial1_tstc(void) |
| 334 | { |
| 335 | return (serial_tstc_dev(PSC_BASE2)); |
| 336 | } |
| 337 | |
| 338 | struct serial_device serial0_device = |
| 339 | { |
| 340 | "serial0", |
| 341 | "UART0", |
| 342 | serial0_init, |
Anatolij Gustschin | 99d25f9 | 2010-04-24 19:27:04 +0200 | [diff] [blame^] | 343 | NULL, |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 344 | serial0_setbrg, |
| 345 | serial0_getc, |
| 346 | serial0_tstc, |
| 347 | serial0_putc, |
| 348 | serial0_puts, |
| 349 | }; |
| 350 | |
| 351 | struct serial_device serial1_device = |
| 352 | { |
| 353 | "serial1", |
| 354 | "UART1", |
| 355 | serial1_init, |
Anatolij Gustschin | 99d25f9 | 2010-04-24 19:27:04 +0200 | [diff] [blame^] | 356 | NULL, |
Wolfgang Denk | 16c3a45 | 2006-06-16 17:04:45 +0200 | [diff] [blame] | 357 | serial1_setbrg, |
| 358 | serial1_getc, |
| 359 | serial1_tstc, |
| 360 | serial1_putc, |
| 361 | serial1_puts, |
| 362 | }; |
| 363 | #endif /* CONFIG_SERIAL_MULTI */ |
| 364 | |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 365 | #endif /* CONFIG_PSC_CONSOLE */ |