Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 1 | /* GRLIB APBUART Serial controller driver |
| 2 | * |
| 3 | * (C) Copyright 2007 |
| 4 | * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com. |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <asm/processor.h> |
| 28 | #include <asm/leon.h> |
| 29 | #include <ambapp.h> |
| 30 | |
| 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
| 33 | /* Force cache miss each time a serial controller reg is read */ |
| 34 | #define CACHE_BYPASS 1 |
| 35 | |
| 36 | #ifdef CACHE_BYPASS |
| 37 | #define READ_BYTE(var) SPARC_NOCACHE_READ_BYTE((unsigned int)&(var)) |
| 38 | #define READ_HWORD(var) SPARC_NOCACHE_READ_HWORD((unsigned int)&(var)) |
| 39 | #define READ_WORD(var) SPARC_NOCACHE_READ((unsigned int)&(var)) |
| 40 | #define READ_DWORD(var) SPARC_NOCACHE_READ_DWORD((unsigned int)&(var)) |
| 41 | #endif |
| 42 | |
| 43 | ambapp_dev_apbuart *leon3_apbuart = NULL; |
| 44 | |
| 45 | int serial_init(void) |
| 46 | { |
| 47 | ambapp_apbdev apbdev; |
| 48 | unsigned int tmp; |
| 49 | |
| 50 | /* find UART */ |
| 51 | if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_APBUART, &apbdev) == 1) { |
| 52 | |
| 53 | leon3_apbuart = (ambapp_dev_apbuart *) apbdev.address; |
| 54 | |
| 55 | /* found apbuart, let's init... |
| 56 | * |
| 57 | * Set scaler / baud rate |
| 58 | * |
| 59 | * Receiver & transmitter enable |
| 60 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 61 | leon3_apbuart->scaler = CONFIG_SYS_GRLIB_APBUART_SCALER; |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 62 | |
| 63 | /* Let bit 11 be unchanged (debug bit for GRMON) */ |
| 64 | tmp = READ_WORD(leon3_apbuart->ctrl); |
| 65 | |
| 66 | leon3_apbuart->ctrl = ((tmp & LEON_REG_UART_CTRL_DBG) | |
| 67 | LEON_REG_UART_CTRL_RE | |
| 68 | LEON_REG_UART_CTRL_TE); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | return -1; /* didn't find hardware */ |
| 73 | } |
| 74 | |
| 75 | void serial_putc(const char c) |
| 76 | { |
| 77 | if (c == '\n') |
| 78 | serial_putc_raw('\r'); |
| 79 | |
| 80 | serial_putc_raw(c); |
| 81 | } |
| 82 | |
| 83 | void serial_putc_raw(const char c) |
| 84 | { |
| 85 | if (!leon3_apbuart) |
| 86 | return; |
| 87 | |
| 88 | /* Wait for last character to go. */ |
| 89 | while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_THE)) ; |
| 90 | |
| 91 | /* Send data */ |
| 92 | leon3_apbuart->data = c; |
| 93 | |
| 94 | #ifdef LEON_DEBUG |
| 95 | /* Wait for data to be sent */ |
| 96 | while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_TSE)) ; |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | void serial_puts(const char *s) |
| 101 | { |
| 102 | while (*s) { |
| 103 | serial_putc(*s++); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | int serial_getc(void) |
| 108 | { |
| 109 | if (!leon3_apbuart) |
| 110 | return 0; |
| 111 | |
| 112 | /* Wait for a character to arrive. */ |
| 113 | while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_DR)) ; |
| 114 | |
| 115 | /* read data */ |
| 116 | return READ_WORD(leon3_apbuart->data); |
| 117 | } |
| 118 | |
| 119 | int serial_tstc(void) |
| 120 | { |
| 121 | if (leon3_apbuart) |
| 122 | return (READ_WORD(leon3_apbuart->status) & |
| 123 | LEON_REG_UART_STATUS_DR); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* set baud rate for uart */ |
| 128 | void serial_setbrg(void) |
| 129 | { |
| 130 | /* update baud rate settings, read it from gd->baudrate */ |
| 131 | unsigned int scaler; |
| 132 | if (leon3_apbuart && (gd->baudrate > 0)) { |
| 133 | scaler = |
| 134 | (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) - |
| 135 | 5) / 10; |
| 136 | leon3_apbuart->scaler = scaler; |
| 137 | } |
| 138 | return; |
| 139 | } |