blob: bca6b6548b9aa9efbbcd448bdb75c344e5dee03b [file] [log] [blame]
Daniel Hellstromb552dbe2008-03-26 22:51:29 +01001/* GRLIB APBUART Serial controller driver
2 *
3 * (C) Copyright 2007
4 * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Daniel Hellstromb552dbe2008-03-26 22:51:29 +01007 */
8
9#include <common.h>
10#include <asm/processor.h>
11#include <asm/leon.h>
12#include <ambapp.h>
Marek Vasut2c91a962012-09-13 12:25:48 +020013#include <serial.h>
14#include <linux/compiler.h>
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010015
16DECLARE_GLOBAL_DATA_PTR;
17
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010018ambapp_dev_apbuart *leon3_apbuart = NULL;
19
Marek Vasut2c91a962012-09-13 12:25:48 +020020static int leon3_serial_init(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010021{
22 ambapp_apbdev apbdev;
23 unsigned int tmp;
24
25 /* find UART */
26 if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_APBUART, &apbdev) == 1) {
27
28 leon3_apbuart = (ambapp_dev_apbuart *) apbdev.address;
29
30 /* found apbuart, let's init...
31 *
32 * Set scaler / baud rate
33 *
34 * Receiver & transmitter enable
35 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036 leon3_apbuart->scaler = CONFIG_SYS_GRLIB_APBUART_SCALER;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010037
38 /* Let bit 11 be unchanged (debug bit for GRMON) */
39 tmp = READ_WORD(leon3_apbuart->ctrl);
40
41 leon3_apbuart->ctrl = ((tmp & LEON_REG_UART_CTRL_DBG) |
42 LEON_REG_UART_CTRL_RE |
43 LEON_REG_UART_CTRL_TE);
44
45 return 0;
46 }
47 return -1; /* didn't find hardware */
48}
49
Marek Vasut2c91a962012-09-13 12:25:48 +020050static void leon3_serial_putc_raw(const char c)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010051{
52 if (!leon3_apbuart)
53 return;
54
55 /* Wait for last character to go. */
56 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_THE)) ;
57
58 /* Send data */
59 leon3_apbuart->data = c;
60
61#ifdef LEON_DEBUG
62 /* Wait for data to be sent */
63 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_TSE)) ;
64#endif
65}
66
Marek Vasut2c91a962012-09-13 12:25:48 +020067static void leon3_serial_putc(const char c)
68{
69 if (c == '\n')
70 leon3_serial_putc_raw('\r');
71
72 leon3_serial_putc_raw(c);
73}
74
Marek Vasut2c91a962012-09-13 12:25:48 +020075static int leon3_serial_getc(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010076{
77 if (!leon3_apbuart)
78 return 0;
79
80 /* Wait for a character to arrive. */
81 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_DR)) ;
82
83 /* read data */
84 return READ_WORD(leon3_apbuart->data);
85}
86
Marek Vasut2c91a962012-09-13 12:25:48 +020087static int leon3_serial_tstc(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010088{
89 if (leon3_apbuart)
90 return (READ_WORD(leon3_apbuart->status) &
91 LEON_REG_UART_STATUS_DR);
92 return 0;
93}
94
95/* set baud rate for uart */
Marek Vasut2c91a962012-09-13 12:25:48 +020096static void leon3_serial_setbrg(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010097{
98 /* update baud rate settings, read it from gd->baudrate */
99 unsigned int scaler;
100 if (leon3_apbuart && (gd->baudrate > 0)) {
101 scaler =
102 (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
103 5) / 10;
104 leon3_apbuart->scaler = scaler;
105 }
106 return;
107}
Marek Vasut2c91a962012-09-13 12:25:48 +0200108
Marek Vasut2c91a962012-09-13 12:25:48 +0200109static struct serial_device leon3_serial_drv = {
110 .name = "leon3_serial",
111 .start = leon3_serial_init,
112 .stop = NULL,
113 .setbrg = leon3_serial_setbrg,
114 .putc = leon3_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000115 .puts = default_serial_puts,
Marek Vasut2c91a962012-09-13 12:25:48 +0200116 .getc = leon3_serial_getc,
117 .tstc = leon3_serial_tstc,
118};
119
120void leon3_serial_initialize(void)
121{
122 serial_register(&leon3_serial_drv);
123}
124
125__weak struct serial_device *default_serial_console(void)
126{
127 return &leon3_serial_drv;
128}