blob: b18be6e245490ab65cdc6c707a6594e13d2bc977 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott McNutt4a889822010-03-19 19:03:28 -04002/*
3 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
4 * Scott McNutt <smcnutt@psyent.com>
Scott McNutt4a889822010-03-19 19:03:28 -04005 */
6
Scott McNutt4a889822010-03-19 19:03:28 -04007#include <common.h>
Thomas Chou6917a5d2015-10-21 21:26:54 +08008#include <dm.h>
9#include <errno.h>
Marek Vasute9d0e3c2012-09-13 16:49:51 +020010#include <serial.h>
Thomas Chou65a50c92015-10-31 20:53:23 +080011#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Thomas Chou65a50c92015-10-31 20:53:23 +080013
Thomas Chou65a50c92015-10-31 20:53:23 +080014/* status register */
15#define ALTERA_UART_TMT BIT(5) /* tx empty */
16#define ALTERA_UART_TRDY BIT(6) /* tx ready */
17#define ALTERA_UART_RRDY BIT(7) /* rx ready */
Scott McNutt4a889822010-03-19 19:03:28 -040018
Thomas Chou6917a5d2015-10-21 21:26:54 +080019struct altera_uart_regs {
20 u32 rxdata; /* Rx data reg */
21 u32 txdata; /* Tx data reg */
22 u32 status; /* Status reg */
23 u32 control; /* Control reg */
24 u32 divisor; /* Baud rate divisor reg */
25 u32 endofpacket; /* End-of-packet reg */
26};
Thomas Chou769cb5c2014-08-25 16:50:14 +080027
Simon Glassb75b15b2020-12-03 16:55:23 -070028struct altera_uart_plat {
Thomas Chou6917a5d2015-10-21 21:26:54 +080029 struct altera_uart_regs *regs;
30 unsigned int uartclk;
31};
Thomas Chou769cb5c2014-08-25 16:50:14 +080032
Thomas Chou6917a5d2015-10-21 21:26:54 +080033static int altera_uart_setbrg(struct udevice *dev, int baudrate)
34{
Simon Glass95588622020-12-22 19:30:28 -070035 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080036 struct altera_uart_regs *const regs = plat->regs;
37 u32 div;
Scott McNutt4a889822010-03-19 19:03:28 -040038
Thomas Chou6917a5d2015-10-21 21:26:54 +080039 div = (plat->uartclk / baudrate) - 1;
40 writel(div, &regs->divisor);
Scott McNutt4a889822010-03-19 19:03:28 -040041
Thomas Chou6917a5d2015-10-21 21:26:54 +080042 return 0;
Marek Vasute9d0e3c2012-09-13 16:49:51 +020043}
44
Thomas Chou6917a5d2015-10-21 21:26:54 +080045static int altera_uart_putc(struct udevice *dev, const char ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +020046{
Simon Glass95588622020-12-22 19:30:28 -070047 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080048 struct altera_uart_regs *const regs = plat->regs;
49
50 if (!(readl(&regs->status) & ALTERA_UART_TRDY))
51 return -EAGAIN;
52
53 writel(ch, &regs->txdata);
54
Marek Vasute9d0e3c2012-09-13 16:49:51 +020055 return 0;
56}
Scott McNutt4a889822010-03-19 19:03:28 -040057
Thomas Chou6917a5d2015-10-21 21:26:54 +080058static int altera_uart_pending(struct udevice *dev, bool input)
Scott McNutt4a889822010-03-19 19:03:28 -040059{
Simon Glass95588622020-12-22 19:30:28 -070060 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080061 struct altera_uart_regs *const regs = plat->regs;
62 u32 st = readl(&regs->status);
Scott McNutt4a889822010-03-19 19:03:28 -040063
Thomas Chou6917a5d2015-10-21 21:26:54 +080064 if (input)
65 return st & ALTERA_UART_RRDY ? 1 : 0;
66 else
67 return !(st & ALTERA_UART_TMT);
Scott McNutt4a889822010-03-19 19:03:28 -040068}
69
Thomas Chou6917a5d2015-10-21 21:26:54 +080070static int altera_uart_getc(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040071{
Simon Glass95588622020-12-22 19:30:28 -070072 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080073 struct altera_uart_regs *const regs = plat->regs;
Scott McNutt4a889822010-03-19 19:03:28 -040074
Thomas Chou6917a5d2015-10-21 21:26:54 +080075 if (!(readl(&regs->status) & ALTERA_UART_RRDY))
76 return -EAGAIN;
Scott McNutt4a889822010-03-19 19:03:28 -040077
Thomas Chou6917a5d2015-10-21 21:26:54 +080078 return readl(&regs->rxdata) & 0xff;
Scott McNutt4a889822010-03-19 19:03:28 -040079}
80
Thomas Chou6917a5d2015-10-21 21:26:54 +080081static int altera_uart_probe(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040082{
Thomas Chou6917a5d2015-10-21 21:26:54 +080083 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040084}
85
Simon Glassaad29ae2020-12-03 16:55:21 -070086static int altera_uart_of_to_plat(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040087{
Simon Glassb75b15b2020-12-03 16:55:23 -070088 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080089
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090090 plat->regs = map_physmem(dev_read_addr(dev),
Thomas Chou15890ca2015-11-14 10:38:09 +080091 sizeof(struct altera_uart_regs),
92 MAP_NOCACHE);
Simon Goldschmidt36dc0cc2019-05-09 22:11:58 +020093 plat->uartclk = dev_read_u32_default(dev, "clock-frequency", 0);
Thomas Chou6917a5d2015-10-21 21:26:54 +080094
95 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040096}
Marek Vasute9d0e3c2012-09-13 16:49:51 +020097
Thomas Chou6917a5d2015-10-21 21:26:54 +080098static const struct dm_serial_ops altera_uart_ops = {
99 .putc = altera_uart_putc,
100 .pending = altera_uart_pending,
101 .getc = altera_uart_getc,
102 .setbrg = altera_uart_setbrg,
103};
104
105static const struct udevice_id altera_uart_ids[] = {
Thomas Chou65a50c92015-10-31 20:53:23 +0800106 { .compatible = "altr,uart-1.0" },
107 {}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800108};
109
110U_BOOT_DRIVER(altera_uart) = {
111 .name = "altera_uart",
112 .id = UCLASS_SERIAL,
113 .of_match = altera_uart_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700114 .of_to_plat = altera_uart_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700115 .plat_auto = sizeof(struct altera_uart_plat),
Thomas Chou6917a5d2015-10-21 21:26:54 +0800116 .probe = altera_uart_probe,
117 .ops = &altera_uart_ops,
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200118};
119
Thomas Chou6917a5d2015-10-21 21:26:54 +0800120#ifdef CONFIG_DEBUG_UART_ALTERA_UART
121
122#include <debug_uart.h>
123
Thomas Chou083cbdd2015-11-03 14:19:02 +0800124static inline void _debug_uart_init(void)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200125{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800126 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
127 u32 div;
128
129 div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1;
130 writel(div, &regs->divisor);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200131}
132
Thomas Chou6917a5d2015-10-21 21:26:54 +0800133static inline void _debug_uart_putc(int ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200134{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800135 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
136
137 while (1) {
138 u32 st = readl(&regs->status);
139
140 if (st & ALTERA_UART_TRDY)
141 break;
142 }
143
144 writel(ch, &regs->txdata);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200145}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800146
147DEBUG_UART_FUNCS
148
149#endif