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 | |
| 27 | #include <common.h> |
| 28 | #include <watchdog.h> |
| 29 | #include <asm/blackfin.h> |
| 30 | #include <asm/mach-common/bits/uart.h> |
| 31 | |
Mike Frysinger | 7633903 | 2008-10-11 21:52:17 -0400 | [diff] [blame^] | 32 | #ifdef CONFIG_UART_CONSOLE |
| 33 | |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 34 | #if defined(UART_LSR) && (CONFIG_UART_CONSOLE != 0) |
| 35 | # error CONFIG_UART_CONSOLE must be 0 on parts with only one UART |
| 36 | #endif |
| 37 | |
| 38 | #include "serial.h" |
| 39 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 40 | #ifdef CONFIG_DEBUG_SERIAL |
| 41 | uint16_t cached_lsr[256]; |
| 42 | uint16_t cached_rbr[256]; |
| 43 | size_t cache_count; |
| 44 | |
| 45 | /* The LSR is read-to-clear on some parts, so we have to make sure status |
| 46 | * bits aren't inadvertently lost when doing various tests. |
| 47 | */ |
| 48 | static uint16_t uart_lsr_save; |
| 49 | static uint16_t uart_lsr_read(void) |
| 50 | { |
| 51 | uint16_t lsr = *pUART_LSR; |
| 52 | uart_lsr_save |= (lsr & (OE|PE|FE|BI)); |
| 53 | return lsr | uart_lsr_save; |
| 54 | } |
| 55 | /* Just do the clear for everyone since it can't hurt. */ |
| 56 | static void uart_lsr_clear(void) |
| 57 | { |
| 58 | uart_lsr_save = 0; |
| 59 | *pUART_LSR |= -1; |
| 60 | } |
| 61 | #else |
| 62 | static inline uint16_t uart_lsr_read(void) { return *pUART_LSR; } |
| 63 | static inline void uart_lsr_clear(void) { *pUART_LSR = -1; } |
| 64 | #endif |
| 65 | |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 66 | /* Symbol for our assembly to call. */ |
| 67 | void serial_set_baud(uint32_t baud) |
| 68 | { |
| 69 | serial_early_set_baud(baud); |
| 70 | } |
| 71 | |
| 72 | /* Symbol for common u-boot code to call. |
| 73 | * Setup the baudrate (brg: baudrate generator). |
| 74 | */ |
| 75 | void serial_setbrg(void) |
| 76 | { |
| 77 | DECLARE_GLOBAL_DATA_PTR; |
| 78 | serial_set_baud(gd->baudrate); |
| 79 | } |
| 80 | |
| 81 | /* Symbol for our assembly to call. */ |
| 82 | void serial_initialize(void) |
| 83 | { |
| 84 | serial_early_init(); |
| 85 | } |
| 86 | |
| 87 | /* Symbol for common u-boot code to call. */ |
| 88 | int serial_init(void) |
| 89 | { |
| 90 | serial_initialize(); |
| 91 | serial_setbrg(); |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 92 | uart_lsr_clear(); |
| 93 | #ifdef CONFIG_DEBUG_SERIAL |
| 94 | cache_count = 0; |
| 95 | memset(cached_lsr, 0x00, sizeof(cached_lsr)); |
| 96 | memset(cached_rbr, 0x00, sizeof(cached_rbr)); |
| 97 | #endif |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | void serial_putc(const char c) |
| 102 | { |
| 103 | /* send a \r for compatibility */ |
| 104 | if (c == '\n') |
| 105 | serial_putc('\r'); |
| 106 | |
| 107 | WATCHDOG_RESET(); |
| 108 | |
| 109 | /* wait for the hardware fifo to clear up */ |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 110 | while (!(uart_lsr_read() & THRE)) |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 111 | continue; |
| 112 | |
| 113 | /* queue the character for transmission */ |
| 114 | *pUART_THR = c; |
| 115 | SSYNC(); |
| 116 | |
| 117 | WATCHDOG_RESET(); |
| 118 | |
| 119 | /* wait for the byte to be shifted over the line */ |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 120 | while (!(uart_lsr_read() & TEMT)) |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | |
| 124 | int serial_tstc(void) |
| 125 | { |
| 126 | WATCHDOG_RESET(); |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 127 | return (uart_lsr_read() & DR) ? 1 : 0; |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int serial_getc(void) |
| 131 | { |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 132 | uint16_t uart_rbr_val; |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 133 | |
| 134 | /* wait for data ! */ |
| 135 | while (!serial_tstc()) |
| 136 | continue; |
| 137 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 138 | /* grab the new byte */ |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 139 | uart_rbr_val = *pUART_RBR; |
| 140 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 141 | #ifdef CONFIG_DEBUG_SERIAL |
| 142 | /* grab & clear the LSR */ |
| 143 | uint16_t uart_lsr_val = uart_lsr_read(); |
| 144 | |
| 145 | cached_lsr[cache_count] = uart_lsr_val; |
| 146 | cached_rbr[cache_count] = uart_rbr_val; |
| 147 | cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr); |
| 148 | |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 149 | if (uart_lsr_val & (OE|PE|FE|BI)) { |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 150 | uint16_t dll, dlh; |
| 151 | printf("\n[SERIAL ERROR]\n"); |
| 152 | ACCESS_LATCH(); |
| 153 | dll = *pUART_DLL; |
| 154 | dlh = *pUART_DLH; |
| 155 | ACCESS_PORT_IER(); |
| 156 | printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh); |
| 157 | do { |
| 158 | --cache_count; |
| 159 | printf("\t%3i: RBR=0x%02x LSR=0x%02x\n", cache_count, |
| 160 | cached_rbr[cache_count], cached_lsr[cache_count]); |
| 161 | } while (cache_count > 0); |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 162 | return -1; |
| 163 | } |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 164 | #endif |
| 165 | uart_lsr_clear(); |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 166 | |
Mike Frysinger | f177f42 | 2008-04-09 02:02:07 -0400 | [diff] [blame] | 167 | return uart_rbr_val; |
Mike Frysinger | 9171fc8 | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void serial_puts(const char *s) |
| 171 | { |
| 172 | while (*s) |
| 173 | serial_putc(*s++); |
| 174 | } |
Mike Frysinger | 7633903 | 2008-10-11 21:52:17 -0400 | [diff] [blame^] | 175 | |
| 176 | #endif |