wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 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 | |
| 24 | #include <config.h> |
| 25 | #include <common.h> |
| 26 | #include <asm/io.h> |
| 27 | |
| 28 | #include "fpga_serial.h" |
| 29 | #include "hardware.h" |
| 30 | #include "pcippc2.h" |
| 31 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 34 | /* 8 data, 1 stop, no parity |
| 35 | */ |
| 36 | #define LCRVAL 0x03 |
| 37 | /* RTS/DTR |
| 38 | */ |
| 39 | #define MCRVAL 0x03 |
| 40 | /* Clear & enable FIFOs |
| 41 | */ |
| 42 | #define FCRVAL 0x07 |
| 43 | |
| 44 | static void fpga_serial_wait (void); |
| 45 | static void fpga_serial_print (char c); |
| 46 | |
| 47 | void fpga_serial_init (int baudrate) |
| 48 | { |
| 49 | int clock_divisor = 115200 / baudrate; |
| 50 | |
| 51 | out8 (FPGA (INT, SERIAL_CONFIG), 0x24); |
| 52 | iobarrier_rw (); |
| 53 | |
| 54 | fpga_serial_wait (); |
| 55 | |
| 56 | out8 (UART (IER), 0); |
| 57 | out8 (UART (LCR), LCRVAL | 0x80); |
| 58 | iobarrier_rw (); |
| 59 | out8 (UART (DLL), clock_divisor & 0xff); |
| 60 | out8 (UART (DLM), clock_divisor >> 8); |
| 61 | iobarrier_rw (); |
| 62 | out8 (UART (LCR), LCRVAL); |
| 63 | iobarrier_rw (); |
| 64 | out8 (UART (MCR), MCRVAL); |
| 65 | out8 (UART (FCR), FCRVAL); |
| 66 | iobarrier_rw (); |
| 67 | } |
| 68 | |
| 69 | void fpga_serial_putc (char c) |
| 70 | { |
| 71 | if (c) { |
| 72 | fpga_serial_print (c); |
| 73 | } |
| 74 | } |
| 75 | |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 76 | int fpga_serial_getc (void) |
| 77 | { |
| 78 | while ((in8 (UART (LSR)) & 0x01) == 0); |
| 79 | |
| 80 | return in8 (UART (RBR)); |
| 81 | } |
| 82 | |
| 83 | int fpga_serial_tstc (void) |
| 84 | { |
| 85 | return (in8 (UART (LSR)) & 0x01) != 0; |
| 86 | } |
| 87 | |
| 88 | void fpga_serial_setbrg (void) |
| 89 | { |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 90 | int clock_divisor = 115200 / gd->baudrate; |
| 91 | |
| 92 | fpga_serial_wait (); |
| 93 | |
| 94 | out8 (UART (LCR), LCRVAL | 0x80); |
| 95 | iobarrier_rw (); |
| 96 | out8 (UART (DLL), clock_divisor & 0xff); |
| 97 | out8 (UART (DLM), clock_divisor >> 8); |
| 98 | iobarrier_rw (); |
| 99 | out8 (UART (LCR), LCRVAL); |
| 100 | iobarrier_rw (); |
| 101 | } |
| 102 | |
| 103 | static void fpga_serial_wait (void) |
| 104 | { |
| 105 | while ((in8 (UART (LSR)) & 0x40) == 0); |
| 106 | } |
| 107 | |
| 108 | static void fpga_serial_print (char c) |
| 109 | { |
| 110 | if (c == '\n') { |
| 111 | while ((in8 (UART (LSR)) & 0x20) == 0); |
| 112 | |
| 113 | out8 (UART (THR), '\r'); |
| 114 | iobarrier_rw (); |
| 115 | } |
| 116 | |
| 117 | while ((in8 (UART (LSR)) & 0x20) == 0); |
| 118 | |
| 119 | out8 (UART (THR), c); |
| 120 | iobarrier_rw (); |
| 121 | |
| 122 | if (c == '\n') { |
| 123 | fpga_serial_wait (); |
| 124 | } |
| 125 | } |