public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 3/4] s5pc1xx: support serial driver
@ 2009-09-04  8:22 Minkyu Kang
  2009-09-04 21:19 ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 1 reply; 2+ messages in thread
From: Minkyu Kang @ 2009-09-04  8:22 UTC (permalink / raw)
  To: u-boot

This patch includes the serial driver for s5pc1xx

Signed-off-by: Minkyu Kang <mk7.kang@samsung.com>
---
 drivers/serial/Makefile         |    1 +
 drivers/serial/serial_s5pc1xx.c |  250 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 251 insertions(+), 0 deletions(-)
 create mode 100644 drivers/serial/serial_s5pc1xx.c

diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
index 64882a2..3c77a7c 100644
--- a/drivers/serial/Makefile
+++ b/drivers/serial/Makefile
@@ -33,6 +33,7 @@ COBJS-$(CONFIG_NS9750_UART) += ns9750_serial.o
 COBJS-$(CONFIG_SYS_NS16550) += ns16550.o
 COBJS-$(CONFIG_DRIVER_S3C4510_UART) += s3c4510b_uart.o
 COBJS-$(CONFIG_S3C64XX) += s3c64xx.o
+COBJS-$(CONFIG_S5PC1XX) += serial_s5pc1xx.o
 COBJS-$(CONFIG_SYS_NS16550_SERIAL) += serial.o
 COBJS-$(CONFIG_CLPS7111_SERIAL) += serial_clps7111.o
 COBJS-$(CONFIG_IMX_SERIAL) += serial_imx.o
diff --git a/drivers/serial/serial_s5pc1xx.c b/drivers/serial/serial_s5pc1xx.c
new file mode 100644
index 0000000..4fd275e
--- /dev/null
+++ b/drivers/serial/serial_s5pc1xx.c
@@ -0,0 +1,250 @@
+/*
+ * (C) Copyright 2009 SAMSUNG Electronics
+ * Minkyu Kang <mk7.kang@samsung.com>
+ * Heungjun Kim <riverful.kim@samsung.com>
+ *
+ * based on drivers/serial/s3c64xx.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <common.h>
+#include <asm/io.h>
+#include <asm/arch/uart.h>
+#include <asm/arch/clk.h>
+
+#ifdef CONFIG_SERIAL0
+#define UART_NR	S5PC1XX_UART0
+#elif defined(CONFIG_SERIAL1)
+#define UART_NR	S5PC1XX_UART1
+#elif defined(CONFIG_SERIAL2)
+#define UART_NR	S5PC1XX_UART2
+#elif defined(CONFIG_SERIAL3)
+#define UART_NR	S5PC1XX_UART3
+#else
+#error "Bad: you didn't configure serial ..."
+#endif
+
+#define barrier() asm volatile("" : : : "memory")
+
+static inline s5pc1xx_uart_t *s5pc1xx_get_base_uart(enum s5pc1xx_uarts_nr nr)
+{
+	if (cpu_is_s5pc100())
+		return (s5pc1xx_uart_t *)(S5PC100_PA_UART + (nr * 0x400));
+	else
+		return (s5pc1xx_uart_t *)(S5PC110_PA_UART + (nr * 0x400));
+}
+
+/*
+ * The coefficient, used to calculate the baudrate on S5PC1XX UARTs is
+ * calculated as
+ * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
+ * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
+ * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
+ */
+static const int udivslot[] = {
+	0,
+	0x0080,
+	0x0808,
+	0x0888,
+	0x2222,
+	0x4924,
+	0x4a52,
+	0x54aa,
+	0x5555,
+	0xd555,
+	0xd5d5,
+	0xddd5,
+	0xdddd,
+	0xdfdd,
+	0xdfdf,
+	0xffdf,
+};
+
+void serial_setbrg(void)
+{
+	DECLARE_GLOBAL_DATA_PTR;
+	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(UART_NR);
+	u32 pclk = get_pclk();
+	u32 baudrate = gd->baudrate;
+	int i;
+
+	i = (pclk / baudrate) % 16;
+
+	uart->UBRDIV = pclk / baudrate / 16 - 1;
+	uart->UDIVSLOT = udivslot[i];
+}
+
+/*
+ * Initialise the serial port with the given baudrate. The settings
+ * are always 8 data bits, no parity, 1 stop bit, no start bits.
+ */
+int serial_init(void)
+{
+	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(UART_NR);
+
+	/* reset and enable FIFOs, set triggers to the maximum */
+	uart->UFCON = 0;
+	uart->UMCON = 0;
+	/* 8N1 */
+	uart->ULCON = 0x3;
+	/* No interrupts, no DMA, pure polling */
+	uart->UCON = 0x245;
+
+	serial_setbrg();
+
+	return 0;
+}
+
+/*
+ * Read a single byte from the serial port. Returns 1 on success, 0
+ * otherwise. When the function is succesfull, the character read is
+ * written into its argument c.
+ */
+int serial_getc(void)
+{
+	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(UART_NR);
+
+	/* wait for character to arrive */
+	while (!(uart->UTRSTAT & 0x1))
+		;
+
+	return uart->URXH & 0xff;
+}
+
+#ifdef CONFIG_MODEM_SUPPORT
+static int be_quiet;
+void disable_putc(void)
+{
+	be_quiet = 1;
+}
+
+void enable_putc(void)
+{
+	be_quiet = 0;
+}
+#endif
+
+
+/*
+ * Output a single byte to the serial port.
+ */
+void serial_putc(const char c)
+{
+	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(UART_NR);
+
+#ifdef CONFIG_MODEM_SUPPORT
+	if (be_quiet)
+		return;
+#endif
+
+	/* wait for room in the tx FIFO */
+	while (!(uart->UTRSTAT & 0x2))
+		;
+
+	uart->UTXH = c;
+
+	/* If \n, also do \r */
+	if (c == '\n')
+		serial_putc('\r');
+}
+
+/*
+ * Test whether a character is in the RX buffer
+ */
+int serial_tstc(void)
+{
+	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(UART_NR);
+
+	return uart->UTRSTAT & 0x1;
+}
+
+void serial_puts(const char *s)
+{
+	while (*s)
+		serial_putc(*s++);
+}
+
+int restartpowersequence = 0;
+int atmsecs;
+
+void uart_serial_setbrg(unsigned int baudrate, int port)
+{
+	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(port);
+	u32 pclk = get_pclk();
+	int i;
+
+	i = (pclk / baudrate) % 16;
+
+	uart->UBRDIV = pclk / baudrate / 16 - 1;
+	uart->UDIVSLOT = udivslot[i];
+}
+
+int uart_serial_pollc(int retry, int port)
+{
+	int i;
+	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(port);
+
+	for (i = 0; i < retry; i++) {
+		if (uart->UTRSTAT & 0x1)
+			return uart->URXH & 0xff;
+		udelay(1000); /* 1ms */
+	}
+
+	return -1;
+}
+
+#ifdef CONFIG_HWFLOW
+static int hwflow;             /* turned off by default */
+int hwflow_onoff(int on)
+{
+	switch (on) {
+	case 1:
+		hwflow = 1;     /* turn on */
+		break;
+	case -1:
+		hwflow = 0;     /* turn off */
+		break;
+	}
+	return hwflow;
+}
+#endif
+
+void uart_serial_putc(const char c, int port)
+{
+       s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(port);
+
+#ifdef CONFIG_MODEM_SUPPORT
+	if (be_quiet)
+		return;
+#endif
+
+	/* wait for room in the tx FIFO */
+	while (!(uart->UTRSTAT & 0x2))
+		;
+
+	uart->UTXH = c;
+
+	/* If \n, also do \r */
+	if (c == '\n')
+		serial_putc('\r');
+}
+
+void uart_serial_puts(const char *s, int port)
+{
+	while (*s)
+		uart_serial_putc(*s++, port);
+}
-- 
1.5.4.3

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [U-Boot] [PATCH 3/4] s5pc1xx: support serial driver
  2009-09-04  8:22 [U-Boot] [PATCH 3/4] s5pc1xx: support serial driver Minkyu Kang
@ 2009-09-04 21:19 ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 0 replies; 2+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2009-09-04 21:19 UTC (permalink / raw)
  To: u-boot

> +
> +#ifdef CONFIG_SERIAL0
> +#define UART_NR	S5PC1XX_UART0
> +#elif defined(CONFIG_SERIAL1)
> +#define UART_NR	S5PC1XX_UART1
> +#elif defined(CONFIG_SERIAL2)
> +#define UART_NR	S5PC1XX_UART2
> +#elif defined(CONFIG_SERIAL3)
> +#define UART_NR	S5PC1XX_UART3
> +#else
> +#error "Bad: you didn't configure serial ..."
> +#endif
why not use serial multi API?
> +
> +#define barrier() asm volatile("" : : : "memory")
this is really generic maybe put it somewhere else
> +
> +static inline s5pc1xx_uart_t *s5pc1xx_get_base_uart(enum s5pc1xx_uarts_nr nr)
> +{
> +	if (cpu_is_s5pc100())
> +		return (s5pc1xx_uart_t *)(S5PC100_PA_UART + (nr * 0x400));
> +	else
> +		return (s5pc1xx_uart_t *)(S5PC110_PA_UART + (nr * 0x400));
> +}
> +
> +/*
> + * The coefficient, used to calculate the baudrate on S5PC1XX UARTs is
> + * calculated as
> + * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
> + * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
> + * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
> + */
> +static const int udivslot[] = {
> +	0,
> +	0x0080,
> +	0x0808,
> +	0x0888,
> +	0x2222,
> +	0x4924,
> +	0x4a52,
> +	0x54aa,
> +	0x5555,
> +	0xd555,
> +	0xd5d5,
> +	0xddd5,
> +	0xdddd,
> +	0xdfdd,
> +	0xdfdf,
> +	0xffdf,
> +};
> +
> +void serial_setbrg(void)
> +{
> +	DECLARE_GLOBAL_DATA_PTR;
> +	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(UART_NR);
> +	u32 pclk = get_pclk();
please use this API
get_pclk_rate() as at91 sh4 and other

> +	u32 baudrate = gd->baudrate;
> +	int i;
> +
> +	i = (pclk / baudrate) % 16;
> +
> +	uart->UBRDIV = pclk / baudrate / 16 - 1;
please use accesor writex/readx
> +
> +int restartpowersequence = 0;
> +int atmsecs;
do you use it somewhere else?
I guess no so please make them static
> +
> +void uart_serial_setbrg(unsigned int baudrate, int port)
> +{
> +	s5pc1xx_uart_t *const uart = s5pc1xx_get_base_uart(port);
> +	u32 pclk = get_pclk();
> +	int i;
> +
> +	i = (pclk / baudrate) % 16;
> +
> +	uart->UBRDIV = pclk / baudrate / 16 - 1;
> +	uart->UDIVSLOT = udivslot[i];
> +}
please do not duplidate code
> +

Best Regards,
J.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-09-04 21:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-04  8:22 [U-Boot] [PATCH 3/4] s5pc1xx: support serial driver Minkyu Kang
2009-09-04 21:19 ` Jean-Christophe PLAGNIOL-VILLARD

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox