Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004, Psyent Corporation <www.psyent.com> |
| 3 | * Scott McNutt <smcnutt@psyent.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <watchdog.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <nios2-io.h> |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 13 | #include <linux/compiler.h> |
| 14 | #include <serial.h> |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | /*------------------------------------------------------------------ |
| 19 | * UART the serial port |
| 20 | *-----------------------------------------------------------------*/ |
| 21 | |
| 22 | static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE; |
| 23 | |
| 24 | #if defined(CONFIG_SYS_NIOS_FIXEDBAUD) |
| 25 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 26 | /* |
| 27 | * Everything's already setup for fixed-baud PTF |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 28 | * assignment |
| 29 | */ |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 30 | static void altera_serial_setbrg(void) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | static int altera_serial_init(void) |
| 35 | { |
| 36 | return 0; |
| 37 | } |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 38 | |
| 39 | #else |
| 40 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 41 | static void altera_serial_setbrg(void) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 42 | { |
| 43 | unsigned div; |
| 44 | |
| 45 | div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1; |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 46 | writel (div, &uart->divisor); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 49 | static int altera_serial_init(void) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 50 | { |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 51 | serial_setbrg(); |
| 52 | return 0; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ |
| 56 | |
| 57 | /*----------------------------------------------------------------------- |
| 58 | * UART CONSOLE |
| 59 | *---------------------------------------------------------------------*/ |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 60 | static void altera_serial_putc(char c) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 61 | { |
| 62 | if (c == '\n') |
| 63 | serial_putc ('\r'); |
| 64 | while ((readl (&uart->status) & NIOS_UART_TRDY) == 0) |
| 65 | WATCHDOG_RESET (); |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 66 | writel ((unsigned char)c, &uart->txdata); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 69 | static int altera_serial_tstc(void) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 70 | { |
| 71 | return (readl (&uart->status) & NIOS_UART_RRDY); |
| 72 | } |
| 73 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 74 | static int altera_serial_getc(void) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 75 | { |
| 76 | while (serial_tstc () == 0) |
| 77 | WATCHDOG_RESET (); |
| 78 | return (readl (&uart->rxdata) & 0x00ff ); |
| 79 | } |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 80 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 81 | static struct serial_device altera_serial_drv = { |
| 82 | .name = "altera_serial", |
| 83 | .start = altera_serial_init, |
| 84 | .stop = NULL, |
| 85 | .setbrg = altera_serial_setbrg, |
| 86 | .putc = altera_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 87 | .puts = default_serial_puts, |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 88 | .getc = altera_serial_getc, |
| 89 | .tstc = altera_serial_tstc, |
| 90 | }; |
| 91 | |
| 92 | void altera_serial_initialize(void) |
| 93 | { |
| 94 | serial_register(&altera_serial_drv); |
| 95 | } |
| 96 | |
| 97 | __weak struct serial_device *default_serial_console(void) |
| 98 | { |
| 99 | return &altera_serial_drv; |
| 100 | } |