blob: 4ff9fe27a8bca7bf70f52c574e8711ff2fe1373a [file] [log] [blame]
Scott McNuttc9d4f462010-03-19 19:03:28 -04001/*
2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Scott McNuttc9d4f462010-03-19 19:03:28 -04006 */
7
Scott McNuttc9d4f462010-03-19 19:03:28 -04008#include <common.h>
Thomas Chouda2f8382015-10-21 21:26:54 +08009#include <dm.h>
10#include <errno.h>
Scott McNuttc9d4f462010-03-19 19:03:28 -040011#include <asm/io.h>
Marek Vasutb207d642012-09-13 16:49:51 +020012#include <linux/compiler.h>
13#include <serial.h>
Scott McNuttc9d4f462010-03-19 19:03:28 -040014
Thomas Chouda2f8382015-10-21 21:26:54 +080015struct altera_uart_regs {
16 u32 rxdata; /* Rx data reg */
17 u32 txdata; /* Tx data reg */
18 u32 status; /* Status reg */
19 u32 control; /* Control reg */
20 u32 divisor; /* Baud rate divisor reg */
21 u32 endofpacket; /* End-of-packet reg */
22};
23
24struct altera_uart_platdata {
25 struct altera_uart_regs *regs;
26 unsigned int uartclk;
27};
Thomas Chou86450712014-08-25 16:50:14 +080028
29/* status register */
Thomas Chouda2f8382015-10-21 21:26:54 +080030#define ALTERA_UART_TMT (1 << 5) /* tx empty */
31#define ALTERA_UART_TRDY (1 << 6) /* tx ready */
32#define ALTERA_UART_RRDY (1 << 7) /* rx ready */
Thomas Chou86450712014-08-25 16:50:14 +080033
Scott McNuttc9d4f462010-03-19 19:03:28 -040034DECLARE_GLOBAL_DATA_PTR;
35
Thomas Chouda2f8382015-10-21 21:26:54 +080036static int altera_uart_setbrg(struct udevice *dev, int baudrate)
Marek Vasutb207d642012-09-13 16:49:51 +020037{
Thomas Chouda2f8382015-10-21 21:26:54 +080038 struct altera_uart_platdata *plat = dev->platdata;
39 struct altera_uart_regs *const regs = plat->regs;
40 u32 div;
41
42 div = (plat->uartclk / baudrate) - 1;
43 writel(div, &regs->divisor);
44
45 return 0;
Marek Vasutb207d642012-09-13 16:49:51 +020046}
47
Thomas Chouda2f8382015-10-21 21:26:54 +080048static int altera_uart_putc(struct udevice *dev, const char ch)
49{
50 struct altera_uart_platdata *plat = dev->platdata;
51 struct altera_uart_regs *const regs = plat->regs;
52
53 if (!(readl(&regs->status) & ALTERA_UART_TRDY))
54 return -EAGAIN;
55
56 writel(ch, &regs->txdata);
57
58 return 0;
59}
60
61static int altera_uart_pending(struct udevice *dev, bool input)
62{
63 struct altera_uart_platdata *plat = dev->platdata;
64 struct altera_uart_regs *const regs = plat->regs;
65 u32 st = readl(&regs->status);
66
67 if (input)
68 return st & ALTERA_UART_RRDY ? 1 : 0;
69 else
70 return !(st & ALTERA_UART_TMT);
71}
72
73static int altera_uart_getc(struct udevice *dev)
74{
75 struct altera_uart_platdata *plat = dev->platdata;
76 struct altera_uart_regs *const regs = plat->regs;
77
78 if (!(readl(&regs->status) & ALTERA_UART_RRDY))
79 return -EAGAIN;
80
81 return readl(&regs->rxdata) & 0xff;
82}
83
84static int altera_uart_probe(struct udevice *dev)
Marek Vasutb207d642012-09-13 16:49:51 +020085{
86 return 0;
87}
Scott McNuttc9d4f462010-03-19 19:03:28 -040088
Thomas Chouda2f8382015-10-21 21:26:54 +080089static int altera_uart_ofdata_to_platdata(struct udevice *dev)
Scott McNuttc9d4f462010-03-19 19:03:28 -040090{
Thomas Chouda2f8382015-10-21 21:26:54 +080091 struct altera_uart_platdata *plat = dev_get_platdata(dev);
Scott McNuttc9d4f462010-03-19 19:03:28 -040092
Thomas Chouda2f8382015-10-21 21:26:54 +080093 plat->regs = ioremap(dev_get_addr(dev),
94 sizeof(struct altera_uart_regs));
95 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
96 "clock-frequency", 0);
Scott McNuttc9d4f462010-03-19 19:03:28 -040097
Marek Vasutb207d642012-09-13 16:49:51 +020098 return 0;
Scott McNuttc9d4f462010-03-19 19:03:28 -040099}
100
Thomas Chouda2f8382015-10-21 21:26:54 +0800101static const struct dm_serial_ops altera_uart_ops = {
102 .putc = altera_uart_putc,
103 .pending = altera_uart_pending,
104 .getc = altera_uart_getc,
105 .setbrg = altera_uart_setbrg,
Marek Vasutb207d642012-09-13 16:49:51 +0200106};
107
Thomas Chouda2f8382015-10-21 21:26:54 +0800108static const struct udevice_id altera_uart_ids[] = {
109 { .compatible = "altr,uart-1.0", },
110 { }
111};
112
113U_BOOT_DRIVER(altera_uart) = {
114 .name = "altera_uart",
115 .id = UCLASS_SERIAL,
116 .of_match = altera_uart_ids,
117 .ofdata_to_platdata = altera_uart_ofdata_to_platdata,
118 .platdata_auto_alloc_size = sizeof(struct altera_uart_platdata),
119 .probe = altera_uart_probe,
120 .ops = &altera_uart_ops,
121 .flags = DM_FLAG_PRE_RELOC,
122};
123
124#ifdef CONFIG_DEBUG_UART_ALTERA_UART
125
126#include <debug_uart.h>
127
128void debug_uart_init(void)
Marek Vasutb207d642012-09-13 16:49:51 +0200129{
Thomas Chouda2f8382015-10-21 21:26:54 +0800130 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
131 u32 div;
132
133 div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1;
134 writel(div, &regs->divisor);
Marek Vasutb207d642012-09-13 16:49:51 +0200135}
136
Thomas Chouda2f8382015-10-21 21:26:54 +0800137static inline void _debug_uart_putc(int ch)
Marek Vasutb207d642012-09-13 16:49:51 +0200138{
Thomas Chouda2f8382015-10-21 21:26:54 +0800139 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
140
141 while (1) {
142 u32 st = readl(&regs->status);
143
144 if (st & ALTERA_UART_TRDY)
145 break;
146 }
147
148 writel(ch, &regs->txdata);
Marek Vasutb207d642012-09-13 16:49:51 +0200149}
Thomas Chouda2f8382015-10-21 21:26:54 +0800150
151DEBUG_UART_FUNCS
152
153#endif