blob: d620528e3e7ead1a6f67a6584e9d40329445eb34 [file] [log] [blame]
Scott McNutt4a889822010-03-19 19:03:28 -04001/*
2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Scott McNutt4a889822010-03-19 19:03:28 -04006 */
7
8
9#include <common.h>
10#include <watchdog.h>
11#include <asm/io.h>
12#include <nios2-io.h>
Marek Vasute9d0e3c2012-09-13 16:49:51 +020013#include <linux/compiler.h>
14#include <serial.h>
Scott McNutt4a889822010-03-19 19:03:28 -040015
16DECLARE_GLOBAL_DATA_PTR;
17
18/*------------------------------------------------------------------
19 * UART the serial port
20 *-----------------------------------------------------------------*/
21
22static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
23
24#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
25
Marek Vasute9d0e3c2012-09-13 16:49:51 +020026/*
27 * Everything's already setup for fixed-baud PTF
Scott McNutt4a889822010-03-19 19:03:28 -040028 * assignment
29 */
Marek Vasute9d0e3c2012-09-13 16:49:51 +020030static void altera_serial_setbrg(void)
31{
32}
33
34static int altera_serial_init(void)
35{
36 return 0;
37}
Scott McNutt4a889822010-03-19 19:03:28 -040038
39#else
40
Marek Vasute9d0e3c2012-09-13 16:49:51 +020041static void altera_serial_setbrg(void)
Scott McNutt4a889822010-03-19 19:03:28 -040042{
43 unsigned div;
44
45 div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
Scott McNutt0fd72d32010-03-21 21:24:43 -040046 writel (div, &uart->divisor);
Scott McNutt4a889822010-03-19 19:03:28 -040047}
48
Marek Vasute9d0e3c2012-09-13 16:49:51 +020049static int altera_serial_init(void)
Scott McNutt4a889822010-03-19 19:03:28 -040050{
Marek Vasute9d0e3c2012-09-13 16:49:51 +020051 serial_setbrg();
52 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040053}
54
55#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
56
57/*-----------------------------------------------------------------------
58 * UART CONSOLE
59 *---------------------------------------------------------------------*/
Marek Vasute9d0e3c2012-09-13 16:49:51 +020060static void altera_serial_putc(char c)
Scott McNutt4a889822010-03-19 19:03:28 -040061{
62 if (c == '\n')
63 serial_putc ('\r');
64 while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
65 WATCHDOG_RESET ();
Scott McNutt0fd72d32010-03-21 21:24:43 -040066 writel ((unsigned char)c, &uart->txdata);
Scott McNutt4a889822010-03-19 19:03:28 -040067}
68
Marek Vasute9d0e3c2012-09-13 16:49:51 +020069static int altera_serial_tstc(void)
Scott McNutt4a889822010-03-19 19:03:28 -040070{
71 return (readl (&uart->status) & NIOS_UART_RRDY);
72}
73
Marek Vasute9d0e3c2012-09-13 16:49:51 +020074static int altera_serial_getc(void)
Scott McNutt4a889822010-03-19 19:03:28 -040075{
76 while (serial_tstc () == 0)
77 WATCHDOG_RESET ();
78 return (readl (&uart->rxdata) & 0x00ff );
79}
Marek Vasute9d0e3c2012-09-13 16:49:51 +020080
Marek Vasute9d0e3c2012-09-13 16:49:51 +020081static 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 Vasutd9c64492012-10-06 14:07:02 +000087 .puts = default_serial_puts,
Marek Vasute9d0e3c2012-09-13 16:49:51 +020088 .getc = altera_serial_getc,
89 .tstc = altera_serial_tstc,
90};
91
92void 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}