blob: d6b14844d67e519a3e40064b2842cf8c3da54b4e [file] [log] [blame]
Scott McNuttc9d4f462010-03-19 19:03:28 -04001/*
2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Scott McNuttc9d4f462010-03-19 19:03:28 -04006 */
7
8
9#include <common.h>
10#include <watchdog.h>
11#include <asm/io.h>
Marek Vasutb207d642012-09-13 16:49:51 +020012#include <linux/compiler.h>
13#include <serial.h>
Scott McNuttc9d4f462010-03-19 19:03:28 -040014
Thomas Chou86450712014-08-25 16:50:14 +080015typedef volatile struct {
16 unsigned rxdata; /* Rx data reg */
17 unsigned txdata; /* Tx data reg */
18 unsigned status; /* Status reg */
19 unsigned control; /* Control reg */
20 unsigned divisor; /* Baud rate divisor reg */
21 unsigned endofpacket; /* End-of-packet reg */
22} nios_uart_t;
23
24/* status register */
25#define NIOS_UART_PE (1 << 0) /* parity error */
26#define NIOS_UART_FE (1 << 1) /* frame error */
27#define NIOS_UART_BRK (1 << 2) /* break detect */
28#define NIOS_UART_ROE (1 << 3) /* rx overrun */
29#define NIOS_UART_TOE (1 << 4) /* tx overrun */
30#define NIOS_UART_TMT (1 << 5) /* tx empty */
31#define NIOS_UART_TRDY (1 << 6) /* tx ready */
32#define NIOS_UART_RRDY (1 << 7) /* rx ready */
33#define NIOS_UART_E (1 << 8) /* exception */
34#define NIOS_UART_DCTS (1 << 10) /* cts change */
35#define NIOS_UART_CTS (1 << 11) /* cts */
36#define NIOS_UART_EOP (1 << 12) /* eop detected */
37
38/* control register */
39#define NIOS_UART_IPE (1 << 0) /* parity error int ena*/
40#define NIOS_UART_IFE (1 << 1) /* frame error int ena */
41#define NIOS_UART_IBRK (1 << 2) /* break detect int ena */
42#define NIOS_UART_IROE (1 << 3) /* rx overrun int ena */
43#define NIOS_UART_ITOE (1 << 4) /* tx overrun int ena */
44#define NIOS_UART_ITMT (1 << 5) /* tx empty int ena */
45#define NIOS_UART_ITRDY (1 << 6) /* tx ready int ena */
46#define NIOS_UART_IRRDY (1 << 7) /* rx ready int ena */
47#define NIOS_UART_IE (1 << 8) /* exception int ena */
48#define NIOS_UART_TBRK (1 << 9) /* transmit break */
49#define NIOS_UART_IDCTS (1 << 10) /* cts change int ena */
50#define NIOS_UART_RTS (1 << 11) /* rts */
51#define NIOS_UART_IEOP (1 << 12) /* eop detected int ena */
52
Scott McNuttc9d4f462010-03-19 19:03:28 -040053DECLARE_GLOBAL_DATA_PTR;
54
55/*------------------------------------------------------------------
56 * UART the serial port
57 *-----------------------------------------------------------------*/
58
59static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
60
61#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
62
Marek Vasutb207d642012-09-13 16:49:51 +020063/*
64 * Everything's already setup for fixed-baud PTF
Scott McNuttc9d4f462010-03-19 19:03:28 -040065 * assignment
66 */
Marek Vasutb207d642012-09-13 16:49:51 +020067static void altera_serial_setbrg(void)
68{
69}
70
71static int altera_serial_init(void)
72{
73 return 0;
74}
Scott McNuttc9d4f462010-03-19 19:03:28 -040075
76#else
77
Marek Vasutb207d642012-09-13 16:49:51 +020078static void altera_serial_setbrg(void)
Scott McNuttc9d4f462010-03-19 19:03:28 -040079{
80 unsigned div;
81
82 div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
Scott McNutt3ea00372010-03-21 21:24:43 -040083 writel (div, &uart->divisor);
Scott McNuttc9d4f462010-03-19 19:03:28 -040084}
85
Marek Vasutb207d642012-09-13 16:49:51 +020086static int altera_serial_init(void)
Scott McNuttc9d4f462010-03-19 19:03:28 -040087{
Marek Vasutb207d642012-09-13 16:49:51 +020088 serial_setbrg();
89 return 0;
Scott McNuttc9d4f462010-03-19 19:03:28 -040090}
91
92#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
93
94/*-----------------------------------------------------------------------
95 * UART CONSOLE
96 *---------------------------------------------------------------------*/
Marek Vasutb207d642012-09-13 16:49:51 +020097static void altera_serial_putc(char c)
Scott McNuttc9d4f462010-03-19 19:03:28 -040098{
99 if (c == '\n')
100 serial_putc ('\r');
101 while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
102 WATCHDOG_RESET ();
Scott McNutt3ea00372010-03-21 21:24:43 -0400103 writel ((unsigned char)c, &uart->txdata);
Scott McNuttc9d4f462010-03-19 19:03:28 -0400104}
105
Marek Vasutb207d642012-09-13 16:49:51 +0200106static int altera_serial_tstc(void)
Scott McNuttc9d4f462010-03-19 19:03:28 -0400107{
108 return (readl (&uart->status) & NIOS_UART_RRDY);
109}
110
Marek Vasutb207d642012-09-13 16:49:51 +0200111static int altera_serial_getc(void)
Scott McNuttc9d4f462010-03-19 19:03:28 -0400112{
113 while (serial_tstc () == 0)
114 WATCHDOG_RESET ();
115 return (readl (&uart->rxdata) & 0x00ff );
116}
Marek Vasutb207d642012-09-13 16:49:51 +0200117
Marek Vasutb207d642012-09-13 16:49:51 +0200118static struct serial_device altera_serial_drv = {
119 .name = "altera_serial",
120 .start = altera_serial_init,
121 .stop = NULL,
122 .setbrg = altera_serial_setbrg,
123 .putc = altera_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000124 .puts = default_serial_puts,
Marek Vasutb207d642012-09-13 16:49:51 +0200125 .getc = altera_serial_getc,
126 .tstc = altera_serial_tstc,
127};
128
129void altera_serial_initialize(void)
130{
131 serial_register(&altera_serial_drv);
132}
133
134__weak struct serial_device *default_serial_console(void)
135{
136 return &altera_serial_drv;
137}