blob: d4ed60c303037b09f3f976d6f51f5130ddc5b5dc [file] [log] [blame]
wdenkef3386f2004-10-10 21:27:30 +00001/*
Renato Andreola9f64dcb2010-03-16 16:01:29 -04002 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
wdenkef3386f2004-10-10 21:27:30 +00005 */
6
wdenkef3386f2004-10-10 21:27:30 +00007#include <common.h>
8#include <watchdog.h>
Scott McNutta5721a32006-06-08 11:59:57 -04009#include <asm/io.h>
Renato Andreola9f64dcb2010-03-16 16:01:29 -040010#include <nios2-yanu.h>
Axel Lin0bdf0002014-01-16 15:01:49 +080011#include <serial.h>
wdenkef3386f2004-10-10 21:27:30 +000012
Wolfgang Denk6405a152006-03-31 18:32:53 +020013DECLARE_GLOBAL_DATA_PTR;
14
Renato Andreola9f64dcb2010-03-16 16:01:29 -040015/*-----------------------------------------------------------------*/
16/* YANU Imagos serial port */
17/*-----------------------------------------------------------------*/
18
19static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
20
Marek Vasutfcb9ef82012-09-13 16:52:38 +020021static void oc_serial_setbrg(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -040022{
23 int n, k;
24 const unsigned max_uns = 0xFFFFFFFF;
25 unsigned best_n, best_m, baud;
Axel Lin4a37b422014-01-16 15:03:52 +080026 unsigned baudrate;
Renato Andreola9f64dcb2010-03-16 16:01:29 -040027
Axel Lin4a37b422014-01-16 15:03:52 +080028#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
29 /* Everything's already setup for fixed-baud PTF assignment */
30 baudrate = CONFIG_BAUDRATE;
Renato Andreola9f64dcb2010-03-16 16:01:29 -040031#else
Axel Lin4a37b422014-01-16 15:03:52 +080032 baudrate = gd->baudrate;
33#endif
Renato Andreola9f64dcb2010-03-16 16:01:29 -040034 /* compute best N and M couple */
35 best_n = YANU_MAX_PRESCALER_N;
36 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
37 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
Axel Lin4a37b422014-01-16 15:03:52 +080038 baudrate) {
Renato Andreola9f64dcb2010-03-16 16:01:29 -040039 best_n = n;
40 break;
41 }
42 }
43 for (k = 0;; k++) {
Axel Lin4a37b422014-01-16 15:03:52 +080044 if (baudrate <= (max_uns >> (15+n-k)))
Renato Andreola9f64dcb2010-03-16 16:01:29 -040045 break;
46 }
47 best_m =
Axel Lin4a37b422014-01-16 15:03:52 +080048 (baudrate * (1 << (15 + n - k))) /
Renato Andreola9f64dcb2010-03-16 16:01:29 -040049 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
50
51 baud = best_m + best_n * YANU_BAUDE;
Scott McNutt0fd72d32010-03-21 21:24:43 -040052 writel(baud, &uart->baud);
Renato Andreola9f64dcb2010-03-16 16:01:29 -040053
54 return;
55}
56
Marek Vasutfcb9ef82012-09-13 16:52:38 +020057static int oc_serial_init(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -040058{
59 unsigned action,control;
60
61 /* status register cleanup */
62 action = YANU_ACTION_RRRDY |
63 YANU_ACTION_RTRDY |
64 YANU_ACTION_ROE |
65 YANU_ACTION_RBRK |
66 YANU_ACTION_RFE |
67 YANU_ACTION_RPE |
68 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
69
Scott McNutt0fd72d32010-03-21 21:24:43 -040070 writel(action, &uart->action);
Wolfgang Denkbe2034d2010-05-26 23:51:22 +020071
72 /*
73 * control register cleanup
74 * no interrupts enabled
75 * one stop bit
76 * hardware flow control disabled
77 * 8 bits
78 */
Renato Andreola9f64dcb2010-03-16 16:01:29 -040079 control = (0x7 << YANU_CONTROL_BITS_POS);
80 /* enven parity just to be clean */
81 control |= YANU_CONTROL_PAREVEN;
82 /* we set threshold for fifo */
83 control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
84 control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
85
Scott McNutt0fd72d32010-03-21 21:24:43 -040086 writel(control, &uart->control);
Renato Andreola9f64dcb2010-03-16 16:01:29 -040087
88 /* to set baud rate */
89 serial_setbrg();
90
91 return (0);
92}
93
94
95/*-----------------------------------------------------------------------
96 * YANU CONSOLE
97 *---------------------------------------------------------------------*/
Marek Vasutfcb9ef82012-09-13 16:52:38 +020098static void oc_serial_putc(char c)
Renato Andreola9f64dcb2010-03-16 16:01:29 -040099{
100 int tx_chars;
101 unsigned status;
102
103 if (c == '\n')
104 serial_putc ('\r');
Wolfgang Denkbe2034d2010-05-26 23:51:22 +0200105
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400106 while (1) {
107 status = readl(&uart->status);
108 tx_chars = (status>>YANU_TFIFO_CHARS_POS)
109 & ((1<<YANU_TFIFO_CHARS_N)-1);
110 if (tx_chars < YANU_TXFIFO_SIZE-1)
111 break;
112 WATCHDOG_RESET ();
113 }
114
Scott McNutt0fd72d32010-03-21 21:24:43 -0400115 writel((unsigned char)c, &uart->data);
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400116}
117
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200118static int oc_serial_tstc(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400119{
120 unsigned status ;
121
122 status = readl(&uart->status);
123 return (((status >> YANU_RFIFO_CHARS_POS) &
124 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
Wolfgang Denkbe2034d2010-05-26 23:51:22 +0200125}
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400126
Axel Lin0bdf0002014-01-16 15:01:49 +0800127static int oc_serial_getc(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400128{
129 while (serial_tstc() == 0)
130 WATCHDOG_RESET ();
Wolfgang Denkbe2034d2010-05-26 23:51:22 +0200131
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400132 /* first we pull the char */
Scott McNutt0fd72d32010-03-21 21:24:43 -0400133 writel(YANU_ACTION_RFIFO_PULL, &uart->action);
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400134
135 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
136}
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200137
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200138static struct serial_device oc_serial_drv = {
139 .name = "oc_serial",
140 .start = oc_serial_init,
141 .stop = NULL,
142 .setbrg = oc_serial_setbrg,
143 .putc = oc_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000144 .puts = default_serial_puts,
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200145 .getc = oc_serial_getc,
146 .tstc = oc_serial_tstc,
147};
148
149void oc_serial_initialize(void)
150{
151 serial_register(&oc_serial_drv);
152}
153
154__weak struct serial_device *default_serial_console(void)
155{
156 return &oc_serial_drv;
157}