2 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <nios2-yanu.h>
28 DECLARE_GLOBAL_DATA_PTR
;
30 /*-----------------------------------------------------------------*/
31 /* YANU Imagos serial port */
32 /*-----------------------------------------------------------------*/
34 static yanu_uart_t
*uart
= (yanu_uart_t
*)CONFIG_SYS_NIOS_CONSOLE
;
36 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
38 /* Everything's already setup for fixed-baud PTF assignment*/
40 static void oc_serial_setbrg(void)
43 const unsigned max_uns
= 0xFFFFFFFF;
44 unsigned best_n
, best_m
, baud
;
46 /* compute best N and M couple */
47 best_n
= YANU_MAX_PRESCALER_N
;
48 for (n
= YANU_MAX_PRESCALER_N
; n
>= 0; n
--) {
49 if ((unsigned)CONFIG_SYS_CLK_FREQ
/ (1 << (n
+ 4)) >=
50 (unsigned)CONFIG_BAUDRATE
) {
56 if ((unsigned)CONFIG_BAUDRATE
<= (max_uns
>> (15+n
-k
)))
60 ((unsigned)CONFIG_BAUDRATE
* (1 << (15 + n
- k
))) /
61 ((unsigned)CONFIG_SYS_CLK_FREQ
>> k
);
63 baud
= best_m
+ best_n
* YANU_BAUDE
;
64 writel(baud
, &uart
->baud
);
71 static void oc_serial_setbrg(void)
74 const unsigned max_uns
= 0xFFFFFFFF;
75 unsigned best_n
, best_m
, baud
;
77 /* compute best N and M couple */
78 best_n
= YANU_MAX_PRESCALER_N
;
79 for (n
= YANU_MAX_PRESCALER_N
; n
>= 0; n
--) {
80 if ((unsigned)CONFIG_SYS_CLK_FREQ
/ (1 << (n
+ 4)) >=
87 if (gd
->baudrate
<= (max_uns
>> (15+n
-k
)))
91 (gd
->baudrate
* (1 << (15 + n
- k
))) /
92 ((unsigned)CONFIG_SYS_CLK_FREQ
>> k
);
94 baud
= best_m
+ best_n
* YANU_BAUDE
;
95 writel(baud
, &uart
->baud
);
101 #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
103 static int oc_serial_init(void)
105 unsigned action
,control
;
107 /* status register cleanup */
108 action
= YANU_ACTION_RRRDY
|
114 YANU_ACTION_RFE
| YANU_ACTION_RFIFO_CLEAR
| YANU_ACTION_TFIFO_CLEAR
;
116 writel(action
, &uart
->action
);
119 * control register cleanup
120 * no interrupts enabled
122 * hardware flow control disabled
125 control
= (0x7 << YANU_CONTROL_BITS_POS
);
126 /* enven parity just to be clean */
127 control
|= YANU_CONTROL_PAREVEN
;
128 /* we set threshold for fifo */
129 control
|= YANU_CONTROL_RDYDLY
* YANU_RXFIFO_DLY
;
130 control
|= YANU_CONTROL_TXTHR
* YANU_TXFIFO_THR
;
132 writel(control
, &uart
->control
);
134 /* to set baud rate */
141 /*-----------------------------------------------------------------------
143 *---------------------------------------------------------------------*/
144 static void oc_serial_putc(char c
)
153 status
= readl(&uart
->status
);
154 tx_chars
= (status
>>YANU_TFIFO_CHARS_POS
)
155 & ((1<<YANU_TFIFO_CHARS_N
)-1);
156 if (tx_chars
< YANU_TXFIFO_SIZE
-1)
161 writel((unsigned char)c
, &uart
->data
);
164 static int oc_serial_tstc(void)
168 status
= readl(&uart
->status
);
169 return (((status
>> YANU_RFIFO_CHARS_POS
) &
170 ((1 << YANU_RFIFO_CHARS_N
) - 1)) > 0);
173 statoc
int oc_serial_getc(void)
175 while (serial_tstc() == 0)
178 /* first we pull the char */
179 writel(YANU_ACTION_RFIFO_PULL
, &uart
->action
);
181 return(readl(&uart
->data
) & YANU_DATA_CHAR_MASK
);
184 static struct serial_device oc_serial_drv
= {
186 .start
= oc_serial_init
,
188 .setbrg
= oc_serial_setbrg
,
189 .putc
= oc_serial_putc
,
190 .puts
= default_serial_puts
,
191 .getc
= oc_serial_getc
,
192 .tstc
= oc_serial_tstc
,
195 void oc_serial_initialize(void)
197 serial_register(&oc_serial_drv
);
200 __weak
struct serial_device
*default_serial_console(void)
202 return &oc_serial_drv
;