uboot/cpu/nios2/serial.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
   3 * Scott McNutt <smcnutt@psyent.com>
   4 *
   5 * YANU Support:
   6 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
   7 *
   8 * See file CREDITS for list of people who contributed to this
   9 * project.
  10 *
  11 * This program is free software; you can redistribute it and/or
  12 * modify it under the terms of the GNU General Public License as
  13 * published by the Free Software Foundation; either version 2 of
  14 * the License, or (at your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24 * MA 02111-1307 USA
  25 */
  26
  27
  28#include <common.h>
  29#include <watchdog.h>
  30#include <asm/io.h>
  31#include <nios2-io.h>
  32#include <nios2-yanu.h>
  33
  34DECLARE_GLOBAL_DATA_PTR;
  35
  36/*------------------------------------------------------------------
  37 * JTAG acts as the serial port
  38 *-----------------------------------------------------------------*/
  39#if defined(CONFIG_CONSOLE_JTAG)
  40
  41static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE;
  42
  43void serial_setbrg( void ){ return; }
  44int serial_init( void ) { return(0);}
  45
  46void serial_putc (char c)
  47{
  48        unsigned val;
  49
  50        while (NIOS_JTAG_WSPACE ( readl (&jtag->control)) == 0)
  51                WATCHDOG_RESET ();
  52        writel (&jtag->data, (unsigned char)c);
  53}
  54
  55void serial_puts (const char *s)
  56{
  57        while (*s != 0)
  58                serial_putc (*s++);
  59}
  60
  61int serial_tstc (void)
  62{
  63        return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
  64}
  65
  66int serial_getc (void)
  67{
  68        int c;
  69        unsigned val;
  70
  71        while (1) {
  72                WATCHDOG_RESET ();
  73                val = readl (&jtag->data);
  74                if (val & NIOS_JTAG_RVALID)
  75                        break;
  76        }
  77        c = val & 0x0ff;
  78        return (c);
  79}
  80
  81#elif defined(CONFIG_CONSOLE_YANU)
  82/*-----------------------------------------------------------------*/
  83/* YANU Imagos serial port */
  84/*-----------------------------------------------------------------*/
  85
  86static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
  87
  88#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
  89
  90/* Everything's already setup for fixed-baud PTF assignment*/
  91
  92void serial_setbrg (void)
  93{
  94        int n, k;
  95        const unsigned max_uns = 0xFFFFFFFF;
  96        unsigned best_n, best_m, baud;
  97
  98        /* compute best N and M couple */
  99        best_n = YANU_MAX_PRESCALER_N;
 100        for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
 101                if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
 102                    (unsigned)CONFIG_BAUDRATE) {
 103                        best_n = n;
 104                        break;
 105                }
 106        }
 107        for (k = 0;; k++) {
 108                if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
 109                        break;
 110        }
 111        best_m =
 112            ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
 113            ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
 114
 115        baud = best_m + best_n * YANU_BAUDE;
 116        writel(&uart->baud, baud);
 117
 118        return;
 119}
 120
 121#else
 122
 123void serial_setbrg (void)
 124{       
 125        int n, k;
 126        const unsigned max_uns = 0xFFFFFFFF;
 127        unsigned best_n, best_m, baud;
 128
 129        /* compute best N and M couple */
 130        best_n = YANU_MAX_PRESCALER_N;
 131        for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
 132                if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
 133                    gd->baudrate) {
 134                        best_n = n;
 135                        break;
 136                }
 137        }
 138        for (k = 0;; k++) {
 139                if (gd->baudrate <= (max_uns >> (15+n-k)))
 140                        break;
 141        }
 142        best_m =
 143            (gd->baudrate * (1 << (15 + n - k))) /
 144            ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
 145
 146        baud = best_m + best_n * YANU_BAUDE;
 147        writel(&uart->baud, baud);
 148
 149        return;
 150}
 151
 152
 153#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
 154
 155int serial_init (void)
 156{
 157        unsigned action,control;
 158
 159        /* status register cleanup */
 160        action =  YANU_ACTION_RRRDY     |
 161                YANU_ACTION_RTRDY       |
 162                YANU_ACTION_ROE         |
 163                YANU_ACTION_RBRK        |
 164                YANU_ACTION_RFE         |
 165                YANU_ACTION_RPE         |
 166            YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
 167
 168        writel(&uart->action, action);
 169        
 170        /*  control register cleanup */
 171        /* no interrupts enabled */
 172        /* one stop bit */
 173        /* hardware flow control disabled */
 174        /* 8 bits */
 175        control = (0x7 << YANU_CONTROL_BITS_POS);
 176        /* enven parity just to be clean */
 177        control |= YANU_CONTROL_PAREVEN;
 178        /* we set threshold for fifo */
 179        control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
 180        control |= YANU_CONTROL_TXTHR *  YANU_TXFIFO_THR;
 181
 182        writel(&uart->control, control);
 183
 184        /* to set baud rate */
 185        serial_setbrg();
 186
 187        return (0);
 188}
 189
 190
 191/*-----------------------------------------------------------------------
 192 * YANU CONSOLE
 193 *---------------------------------------------------------------------*/
 194void serial_putc (char c)
 195{
 196        int tx_chars;
 197        unsigned status;
 198
 199        if (c == '\n')
 200                serial_putc ('\r');
 201        
 202        while (1) {
 203                status = readl(&uart->status);
 204                tx_chars = (status>>YANU_TFIFO_CHARS_POS)
 205                        & ((1<<YANU_TFIFO_CHARS_N)-1);
 206                if (tx_chars < YANU_TXFIFO_SIZE-1)
 207                        break;
 208                WATCHDOG_RESET ();
 209        }
 210
 211        writel(&uart->data, (unsigned char)c);
 212}
 213
 214void serial_puts (const char *s)
 215{
 216        while (*s != 0) {
 217                serial_putc (*s++);
 218        }
 219}
 220
 221
 222int serial_tstc(void)
 223{
 224        unsigned status ;
 225
 226        status = readl(&uart->status);
 227        return (((status >> YANU_RFIFO_CHARS_POS) &
 228                 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
 229}       
 230
 231int serial_getc (void)
 232{
 233        while (serial_tstc() == 0)
 234                WATCHDOG_RESET ();
 235        
 236        /* first we pull the char */
 237        writel(&uart->action, YANU_ACTION_RFIFO_PULL);
 238
 239        return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
 240}
 241
 242#else /*CONFIG_CONSOLE_YANU*/
 243
 244/*------------------------------------------------------------------
 245 * UART the serial port
 246 *-----------------------------------------------------------------*/
 247
 248static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
 249
 250#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
 251
 252/* Everything's already setup for fixed-baud PTF
 253 * assignment
 254 */
 255void serial_setbrg (void){ return; }
 256int serial_init (void) { return (0);}
 257
 258#else
 259
 260void serial_setbrg (void)
 261{
 262        unsigned div;
 263
 264        div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
 265        writel (&uart->divisor,div);
 266        return;
 267}
 268
 269int serial_init (void)
 270{
 271        serial_setbrg ();
 272        return (0);
 273}
 274
 275#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
 276
 277
 278/*-----------------------------------------------------------------------
 279 * UART CONSOLE
 280 *---------------------------------------------------------------------*/
 281void serial_putc (char c)
 282{
 283        if (c == '\n')
 284                serial_putc ('\r');
 285        while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
 286                WATCHDOG_RESET ();
 287        writel (&uart->txdata,(unsigned char)c);
 288}
 289
 290void serial_puts (const char *s)
 291{
 292        while (*s != 0) {
 293                serial_putc (*s++);
 294        }
 295}
 296
 297int serial_tstc (void)
 298{
 299        return (readl (&uart->status) & NIOS_UART_RRDY);
 300}
 301
 302int serial_getc (void)
 303{
 304        while (serial_tstc () == 0)
 305                WATCHDOG_RESET ();
 306        return (readl (&uart->rxdata) & 0x00ff );
 307}
 308
 309#endif /* CONFIG_JTAG_CONSOLE */
 310