Skip to contentSkip to conversation

PATCH - Added support to YANU Nios2 uart

U-Boot Renato Andreola 2 participants 2 messages Aug 24, 2009

lore
Share & tools
lore
#0 lore raw

From eae7788c435cfab344c9741c9501475e9ab2797a Mon Sep 17 00:00:00 2001
From: Renato Andreola <[email protected]>
Date: Mon, 24 Aug 2009 11:10:44 +0200
Subject: [PATCH] Yanu Support for U-Boot

with this patch YANU uart (from opencores) support will be added to u-boot. Remember to modify properly your board files for defining physical address etc etc

---
  cpu/nios2/serial.c   |  170 +++++++++++++++++++++++++++++++++++++++++++++++++-
  include/nios2-yanu.h |  122 ++++++++++++++++++++++++++++++++++++
  2 files changed, 291 insertions(+), 1 deletions(-)

create mode 100644 include/nios2-yanu.h

cpu/nios2/serial.c +169 -1
index 8bbb803..84be160 100644
@@ -26,6 +26,7 @@
  #include <watchdog.h>
  #include <asm/io.h>
  #include <nios2-io.h>
+#include <nios2-yanu.h>
 
  DECLARE_GLOBAL_DATA_PTR;
 
@@ -74,10 +75,177 @@ int serial_getc (void)
  	return (c);
  }
 
+#elif defined(CONFIG_CONSOLE_YANU)
+/*-----------------------------------------------------------------*/
+/* YANU Imagos serial port */
+/*-----------------------------------------------------------------*/
+
+static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
+
+#if defined(CONFIG_NIOS_FIXEDBAUD)
+
+/* Everything's already setup for fixed-baud PTF assignment*/
+
+void serial_setbrg (void)
+{
+	int n, k;
+	const unsigned max_uns = 0xFFFFFFFF;
+	unsigned best_n, best_m, baud;
+
+	/* compute best N and M couple */
+	best_n = YANU_MAX_PRESCALER_N;
+	for (n = YANU_MAX_PRESCALER_N; n >= 0; n--)
+	{
+		if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n+4)) >= (unsigned)CONFIG_BAUDRATE)
+		{
+			best_n = n;
+			break;
+		}
+	}
+	for (k=0; ; k++)
+	{
+		if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
+			break;
+	}
+	best_m = ((unsigned)CONFIG_BAUDRATE * (1 << (15+n-k))) / ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
+
+	baud = best_m + best_n * YANU_BAUDE;
+	writel(&uart->baud, baud);
+
+	return;
+}
+
+#else
+
+void serial_setbrg (void)
+{
+	DECLARE_GLOBAL_DATA_PTR;
+	int n, k;
+	const unsigned max_uns = 0xFFFFFFFF;
+	unsigned best_n, best_m, baud;
+
+	/* compute best N and M couple */
+	best_n = YANU_MAX_PRESCALER_N;
+	for (n = YANU_MAX_PRESCALER_N; n >= 0; n--)
+	{
+		if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n+4)) >= gd->baudrate)
+		{
+			best_n = n;
+			break;
+		}
+	}
+	for (k=0; ; k++)
+	{
+		if (gd->baudrate <= (max_uns >> (15+n-k)))
+			break;
+	}
+	best_m = (gd->baudrate * (1 << (15+n-k))) / ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
+
+	baud = best_m + best_n * YANU_BAUDE;
+	writel(&uart->baud, baud);
+
+	return;
+}
+
+
+#endif /* CFG_NIOS_FIXEDBAUD */
+
+int serial_init (void)
+{
+	unsigned action,control;
+
+	/* status register cleanup */
+	action =  YANU_ACTION_RRRDY     |
+		YANU_ACTION_RTRDY       |
+		YANU_ACTION_ROE         |
+		YANU_ACTION_RBRK        |
+		YANU_ACTION_RFE         |
+		YANU_ACTION_RPE         |
+		YANU_ACTION_RFE         |
+		YANU_ACTION_RFIFO_CLEAR |
+		YANU_ACTION_TFIFO_CLEAR;
+
+	writel(&uart->action, action);
+	
+	/*  control register cleanup */
+	/* no interrupts enabled */
+	/* one stop bit */
+	/* hardware flow control disabled */
+	/* 8 bits */
+	control = (0x7 << YANU_CONTROL_BITS_POS);
+	/* enven parity just to be clean */
+	control |= YANU_CONTROL_PAREVEN;
+	/* we set threshold for fifo */
+	control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
+	control |= YANU_CONTROL_TXTHR *  YANU_TXFIFO_THR;
+
+	writel(&uart->control, control);
+
+	/* to set baud rate */
+	serial_setbrg();
+
+	return (0);
+}
+
+
+/*-----------------------------------------------------------------------
+ * YANU CONSOLE
+ *---------------------------------------------------------------------*/
+void serial_putc (char c)
+{
+	int tx_chars;
+	unsigned status;
+
+	if (c == '\n')
+		serial_putc ('\r');
+	
+	while (1)
+	{
+		status = readl(&uart->status);
+		tx_chars = (status>>YANU_TFIFO_CHARS_POS)
+			& ((1<<YANU_TFIFO_CHARS_N)-1);
+		if (tx_chars < YANU_TXFIFO_SIZE-1)
+			break;
+		WATCHDOG_RESET ();
+	}
+
+	writel(&uart->data, (unsigned char)c);
+}
+
+void serial_puts (const char *s)
+{
+	while (*s != 0)
+	{
+		serial_putc (*s++);
+	}
+}
+
+
+int serial_tstc(void)
+{
+	unsigned status ;
+
+	status = readl(&uart->status);
+	return(((status>>YANU_RFIFO_CHARS_POS) & ((1<<YANU_RFIFO_CHARS_N)-1)) > 0);
+}	
+
+
+int serial_getc (void)
+{
+	while (serial_tstc() == 0)
+		WATCHDOG_RESET ();
+	
+	/* first we pull the char */
+	writel(&uart->action, YANU_ACTION_RFIFO_PULL);
+
+	return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
+}
+
+#else /*CONFIG_CONSOLE_YANU*/
+
  /*------------------------------------------------------------------
   * UART the serial port
   *-----------------------------------------------------------------*/
-#else
 
  static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
 
include/nios2-yanu.h new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
1.5.5
#1 lore raw

Dear Renato Andreola,

Quoted 19 lines · In message <[email protected]> you wrote:

From eae7788c435cfab344c9741c9501475e9ab2797a Mon Sep 17 00:00:00 2001
From: Renato Andreola <[email protected]>
Date: Mon, 24 Aug 2009 11:10:44 +0200
Subject: [PATCH] Yanu Support for U-Boot

with this patch YANU uart (from opencores) support will be added to u-boot. Remember to modify properly your board files for defining physical address etc etc

---
  cpu/nios2/serial.c   |  170 +++++++++++++++++++++++++++++++++++++++++++++++++-
  include/nios2-yanu.h |  122 ++++++++++++++++++++++++++++++++++++
  2 files changed, 291 insertions(+), 1 deletions(-)

create mode 100644 include/nios2-yanu.h

cpu/nios2/serial.c +0 -0
index 8bbb803..84be160 100644

cpu/nios2/serial.c line 29

Quoted 4 lines
cpu/nios2/serial.c +1 -0
@@ -26,6 +26,7 @@
  #include <watchdog.h>
  #include <asm/io.h>
  #include <nios2-io.h>
+#include <nios2-yanu.h>

This patch is white-space corrupted. Please fix your mailer settings.

Quoted 18 lines
cpu/nios2/serial.c +12 -0
  DECLARE_GLOBAL_DATA_PTR;
 
@@ -74,10 +75,177 @@ int serial_getc (void)
  	return (c);
  }
 
+#elif defined(CONFIG_CONSOLE_YANU)
+/*-----------------------------------------------------------------*/
+/* YANU Imagos serial port */
+/*-----------------------------------------------------------------*/
+
+static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
+
+#if defined(CONFIG_NIOS_FIXEDBAUD)
+
+/* Everything's already setup for fixed-baud PTF assignment*/
+
+void serial_setbrg (void)

cpu/nios2/serial.c int serial_getc (void) · line 99

Quoted 10 lines
cpu/nios2/serial.c +10 -0
+{
+	int n, k;
+	const unsigned max_uns = 0xFFFFFFFF;
+	unsigned best_n, best_m, baud;
+
+	/* compute best N and M couple */
+	best_n = YANU_MAX_PRESCALER_N;
+	for (n = YANU_MAX_PRESCALER_N; n >= 0; n--)
+	{
+		if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n+4)) >= (unsigned)CONFIG_BAUDRATE)

Incorrect brace style.
Line too long.

cpu/nios2/serial.c int serial_getc (void) · line 109

Quoted 10 lines
cpu/nios2/serial.c +10 -0
+		{
+			best_n = n;
+			break;
+		}
+	}
+	for (k=0; ; k++)
+	{
+		if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
+			break;
+	}

Incorrect brace style.

cpu/nios2/serial.c int serial_getc (void) · line 110

Quoted 1 line
cpu/nios2/serial.c +1 -0
+	best_m = ((unsigned)CONFIG_BAUDRATE * (1 << (15+n-k))) / ((unsigned)CONFIG_SYS_CLK_FREQ >> k);

Line too long.

cpu/nios2/serial.c int serial_getc (void) · line 122

Quoted 3 lines
cpu/nios2/serial.c +3 -0
+void serial_setbrg (void)
+{
+	DECLARE_GLOBAL_DATA_PTR;

This must be done _outside_ any function body.

cpu/nios2/serial.c int serial_getc (void) · line 98

Quoted 4 lines
cpu/nios2/serial.c +4 -0
+	/* compute best N and M couple */
+	best_n = YANU_MAX_PRESCALER_N;
+	for (n = YANU_MAX_PRESCALER_N; n >= 0; n--)
+	{

Incorrect brace style.

cpu/nios2/serial.c int serial_getc (void) · line 119

Quoted 2 lines
cpu/nios2/serial.c +2 -0
+		if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n+4)) >= gd->baudrate)
+		{

Incorrect brace style.
Line too long.

cpu/nios2/serial.c int serial_getc (void) · line 106

Quoted 6 lines
cpu/nios2/serial.c +6 -0
+			best_n = n;
+			break;
+		}
+	}
+	for (k=0; ; k++)
+	{

Incorrect brace style.

cpu/nios2/serial.c int serial_getc (void) · line 142

Quoted 4 lines
cpu/nios2/serial.c +4 -0
+		if (gd->baudrate <= (max_uns >> (15+n-k)))
+			break;
+	}
+	best_m = (gd->baudrate * (1 << (15+n-k))) / ((unsigned)CONFIG_SYS_CLK_FREQ >> k);

Line too long.

etc. etc.

Quoted 4 lines
cpu/nios2/serial.c +4 -0
+typedef volatile struct yanu_uart_t
+{
+	volatile unsigned data;
+/* control register (RW) 32-bit   */

cpu/nios2/serial.c int serial_getc (void) · line 143

Quoted 10 lines
cpu/nios2/serial.c +10 -0
+	volatile unsigned control;
+/* baud/prescaler register (RW) 32-bit */
+	volatile unsigned baud;
+/* action register (W) 32-bit */
+	volatile unsigned action;
+/* status register (R) 32-bit */
+	volatile unsigned status;
+/* magic register (R) 32-bit */
+	volatile unsigned magic;
+} yanu_uart_t;

Place comments on the same line with the declaration, please.

Please clean up and resubmit.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
A star captain's most solemn oath is that he will give his life, even
his entire crew, rather than violate the Prime Directive.
	-- Kirk, "The Omega Glory", stardate unknown