Scott McNutt | c9d4f46 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Scott McNutt | c9d4f46 | 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 | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 13 | #include <linux/compiler.h> |
| 14 | #include <serial.h> |
Scott McNutt | c9d4f46 | 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 | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 26 | /* |
| 27 | * Everything's already setup for fixed-baud PTF |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 28 | * assignment |
| 29 | */ |
Marek Vasut | b207d64 | 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 | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 38 | |
| 39 | #else |
| 40 | |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 41 | static void altera_serial_setbrg(void) |
Scott McNutt | c9d4f46 | 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 | 3ea0037 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 46 | writel (div, &uart->divisor); |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 49 | static int altera_serial_init(void) |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 50 | { |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 51 | serial_setbrg(); |
| 52 | return 0; |
Scott McNutt | c9d4f46 | 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 | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 60 | static void altera_serial_putc(char c) |
Scott McNutt | c9d4f46 | 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 | 3ea0037 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 66 | writel ((unsigned char)c, &uart->txdata); |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 69 | static int altera_serial_tstc(void) |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 70 | { |
| 71 | return (readl (&uart->status) & NIOS_UART_RRDY); |
| 72 | } |
| 73 | |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 74 | static int altera_serial_getc(void) |
Scott McNutt | c9d4f46 | 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 | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 80 | |
Marek Vasut | b207d64 | 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 | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 87 | .puts = default_serial_puts, |
Marek Vasut | b207d64 | 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 | } |