]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/serial/opencores_yanu.c
Merge branch 'master' of git://git.denx.de/u-boot-spi
[karo-tx-uboot.git] / drivers / serial / opencores_yanu.c
1 /*
2  * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <watchdog.h>
9 #include <asm/io.h>
10 #include <nios2-yanu.h>
11 #include <serial.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 /*-----------------------------------------------------------------*/
16 /* YANU Imagos serial port */
17 /*-----------------------------------------------------------------*/
18
19 static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
20
21 static void oc_serial_setbrg(void)
22 {
23         int n, k;
24         const unsigned max_uns = 0xFFFFFFFF;
25         unsigned best_n, best_m, baud;
26         unsigned baudrate;
27
28 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
29         /* Everything's already setup for fixed-baud PTF assignment */
30         baudrate = CONFIG_BAUDRATE;
31 #else
32         baudrate = gd->baudrate;
33 #endif
34         /* 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)) >=
38                     baudrate) {
39                         best_n = n;
40                         break;
41                 }
42         }
43         for (k = 0;; k++) {
44                 if (baudrate <= (max_uns >> (15+n-k)))
45                         break;
46         }
47         best_m =
48             (baudrate * (1 << (15 + n - k))) /
49             ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
50
51         baud = best_m + best_n * YANU_BAUDE;
52         writel(baud, &uart->baud);
53
54         return;
55 }
56
57 static int oc_serial_init(void)
58 {
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
70         writel(action, &uart->action);
71
72         /*
73          * control register cleanup
74          * no interrupts enabled
75          * one stop bit
76          * hardware flow control disabled
77          * 8 bits
78          */
79         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
86         writel(control, &uart->control);
87
88         /* to set baud rate */
89         serial_setbrg();
90
91         return (0);
92 }
93
94
95 /*-----------------------------------------------------------------------
96  * YANU CONSOLE
97  *---------------------------------------------------------------------*/
98 static void oc_serial_putc(char c)
99 {
100         int tx_chars;
101         unsigned status;
102
103         if (c == '\n')
104                 serial_putc ('\r');
105
106         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
115         writel((unsigned char)c, &uart->data);
116 }
117
118 static int oc_serial_tstc(void)
119 {
120         unsigned status ;
121
122         status = readl(&uart->status);
123         return (((status >> YANU_RFIFO_CHARS_POS) &
124                  ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
125 }
126
127 static int oc_serial_getc(void)
128 {
129         while (serial_tstc() == 0)
130                 WATCHDOG_RESET ();
131
132         /* first we pull the char */
133         writel(YANU_ACTION_RFIFO_PULL, &uart->action);
134
135         return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
136 }
137
138 static 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,
144         .puts   = default_serial_puts,
145         .getc   = oc_serial_getc,
146         .tstc   = oc_serial_tstc,
147 };
148
149 void 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 }