Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - serial.c Blackfin Serial Driver |
| 3 | * |
| 4 | * Copyright (c) 2005-2008 Analog Devices Inc. |
| 5 | * |
| 6 | * Copyright (c) 2003 Bas Vermeulen <bas@buyways.nl>, |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 7 | * BuyWays B.V. (www.buyways.nl) |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 8 | * |
| 9 | * Based heavily on: |
| 10 | * blkfinserial.c: Serial driver for BlackFin DSP internal USRTs. |
| 11 | * Copyright(c) 2003 Metrowerks <mwaddel@metrowerks.com> |
| 12 | * Copyright(c) 2001 Tony Z. Kou <tonyko@arcturusnetworks.com> |
| 13 | * Copyright(c) 2001-2002 Arcturus Networks Inc. <www.arcturusnetworks.com> |
| 14 | * |
| 15 | * Based on code from 68328 version serial driver imlpementation which was: |
| 16 | * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu> |
| 17 | * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com> |
| 18 | * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org> |
| 19 | * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com> |
| 20 | * |
| 21 | * (C) Copyright 2000-2004 |
| 22 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 23 | * |
| 24 | * Licensed under the GPL-2 or later. |
| 25 | */ |
| 26 | |
Mike Frysinger | aad4eca | 2009-04-04 09:10:27 -0400 | [diff] [blame] | 27 | /* Anomaly notes: |
| 28 | * 05000086 - we don't support autobaud |
| 29 | * 05000099 - we only use DR bit, so losing others is not a problem |
| 30 | * 05000100 - we don't use the UART_IIR register |
| 31 | * 05000215 - we poll the uart (no dma/interrupts) |
| 32 | * 05000225 - no workaround possible, but this shouldnt cause errors ... |
| 33 | * 05000230 - we tweak the baud rate calculation slightly |
| 34 | * 05000231 - we always use 1 stop bit |
| 35 | * 05000309 - we always enable the uart before we modify it in anyway |
| 36 | * 05000350 - we always enable the uart regardless of boot mode |
| 37 | * 05000363 - we don't support break signals, so don't generate one |
| 38 | */ |
| 39 | |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 40 | #include <common.h> |
| 41 | #include <watchdog.h> |
| 42 | #include <asm/blackfin.h> |
| 43 | #include <asm/mach-common/bits/uart.h> |
| 44 | |
Mike Frysinger | 7633903 | 2008-10-11 21:52:17 -0400 | [diff] [blame] | 45 | #ifdef CONFIG_UART_CONSOLE |
| 46 | |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 47 | #if defined(UART_LSR) && (CONFIG_UART_CONSOLE != 0) |
| 48 | # error CONFIG_UART_CONSOLE must be 0 on parts with only one UART |
| 49 | #endif |
| 50 | |
| 51 | #include "serial.h" |
| 52 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 53 | #ifdef CONFIG_DEBUG_SERIAL |
| 54 | uint16_t cached_lsr[256]; |
| 55 | uint16_t cached_rbr[256]; |
| 56 | size_t cache_count; |
| 57 | |
| 58 | /* The LSR is read-to-clear on some parts, so we have to make sure status |
Mike Frysinger | aad4eca | 2009-04-04 09:10:27 -0400 | [diff] [blame] | 59 | * bits aren't inadvertently lost when doing various tests. This also |
| 60 | * works around anomaly 05000099 at the same time by keeping a cumulative |
| 61 | * tally of all the status bits. |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 62 | */ |
| 63 | static uint16_t uart_lsr_save; |
| 64 | static uint16_t uart_lsr_read(void) |
| 65 | { |
| 66 | uint16_t lsr = *pUART_LSR; |
| 67 | uart_lsr_save |= (lsr & (OE|PE|FE|BI)); |
| 68 | return lsr | uart_lsr_save; |
| 69 | } |
| 70 | /* Just do the clear for everyone since it can't hurt. */ |
| 71 | static void uart_lsr_clear(void) |
| 72 | { |
| 73 | uart_lsr_save = 0; |
| 74 | *pUART_LSR |= -1; |
| 75 | } |
| 76 | #else |
Mike Frysinger | aad4eca | 2009-04-04 09:10:27 -0400 | [diff] [blame] | 77 | /* When debugging is disabled, we only care about the DR bit, so if other |
| 78 | * bits get set/cleared, we don't really care since we don't read them |
| 79 | * anyways (and thus anomaly 05000099 is irrelevant). |
| 80 | */ |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 81 | static inline uint16_t uart_lsr_read(void) { return *pUART_LSR; } |
| 82 | static inline void uart_lsr_clear(void) { *pUART_LSR = -1; } |
| 83 | #endif |
| 84 | |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 85 | /* Symbol for our assembly to call. */ |
| 86 | void serial_set_baud(uint32_t baud) |
| 87 | { |
| 88 | serial_early_set_baud(baud); |
| 89 | } |
| 90 | |
| 91 | /* Symbol for common u-boot code to call. |
| 92 | * Setup the baudrate (brg: baudrate generator). |
| 93 | */ |
| 94 | void serial_setbrg(void) |
| 95 | { |
| 96 | DECLARE_GLOBAL_DATA_PTR; |
| 97 | serial_set_baud(gd->baudrate); |
| 98 | } |
| 99 | |
| 100 | /* Symbol for our assembly to call. */ |
| 101 | void serial_initialize(void) |
| 102 | { |
| 103 | serial_early_init(); |
| 104 | } |
| 105 | |
| 106 | /* Symbol for common u-boot code to call. */ |
| 107 | int serial_init(void) |
| 108 | { |
| 109 | serial_initialize(); |
| 110 | serial_setbrg(); |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 111 | uart_lsr_clear(); |
| 112 | #ifdef CONFIG_DEBUG_SERIAL |
| 113 | cache_count = 0; |
| 114 | memset(cached_lsr, 0x00, sizeof(cached_lsr)); |
| 115 | memset(cached_rbr, 0x00, sizeof(cached_rbr)); |
| 116 | #endif |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | void serial_putc(const char c) |
| 121 | { |
| 122 | /* send a \r for compatibility */ |
| 123 | if (c == '\n') |
| 124 | serial_putc('\r'); |
| 125 | |
| 126 | WATCHDOG_RESET(); |
| 127 | |
| 128 | /* wait for the hardware fifo to clear up */ |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 129 | while (!(uart_lsr_read() & THRE)) |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 130 | continue; |
| 131 | |
| 132 | /* queue the character for transmission */ |
| 133 | *pUART_THR = c; |
| 134 | SSYNC(); |
| 135 | |
| 136 | WATCHDOG_RESET(); |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int serial_tstc(void) |
| 140 | { |
| 141 | WATCHDOG_RESET(); |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 142 | return (uart_lsr_read() & DR) ? 1 : 0; |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int serial_getc(void) |
| 146 | { |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 147 | uint16_t uart_rbr_val; |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 148 | |
| 149 | /* wait for data ! */ |
| 150 | while (!serial_tstc()) |
| 151 | continue; |
| 152 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 153 | /* grab the new byte */ |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 154 | uart_rbr_val = *pUART_RBR; |
| 155 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 156 | #ifdef CONFIG_DEBUG_SERIAL |
| 157 | /* grab & clear the LSR */ |
| 158 | uint16_t uart_lsr_val = uart_lsr_read(); |
| 159 | |
| 160 | cached_lsr[cache_count] = uart_lsr_val; |
| 161 | cached_rbr[cache_count] = uart_rbr_val; |
| 162 | cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr); |
| 163 | |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 164 | if (uart_lsr_val & (OE|PE|FE|BI)) { |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 165 | uint16_t dll, dlh; |
| 166 | printf("\n[SERIAL ERROR]\n"); |
| 167 | ACCESS_LATCH(); |
| 168 | dll = *pUART_DLL; |
| 169 | dlh = *pUART_DLH; |
| 170 | ACCESS_PORT_IER(); |
| 171 | printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh); |
| 172 | do { |
| 173 | --cache_count; |
| 174 | printf("\t%3i: RBR=0x%02x LSR=0x%02x\n", cache_count, |
| 175 | cached_rbr[cache_count], cached_lsr[cache_count]); |
| 176 | } while (cache_count > 0); |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 177 | return -1; |
| 178 | } |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 179 | #endif |
| 180 | uart_lsr_clear(); |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 181 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 182 | return uart_rbr_val; |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void serial_puts(const char *s) |
| 186 | { |
| 187 | while (*s) |
| 188 | serial_putc(*s++); |
| 189 | } |
Mike Frysinger | 7633903 | 2008-10-11 21:52:17 -0400 | [diff] [blame] | 190 | |
| 191 | #endif |