]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/serial/serial_arc.c
Merge git://git.denx.de/u-boot-mmc
[people/ms/u-boot.git] / drivers / serial / serial_arc.c
CommitLineData
22a240c3
AB
1/*
2 * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
9
10#include <common.h>
01496c4f 11#include <dm.h>
22a240c3
AB
12#include <serial.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16struct arc_serial_regs {
17 unsigned int id0;
18 unsigned int id1;
19 unsigned int id2;
20 unsigned int id3;
21 unsigned int data;
22 unsigned int status;
23 unsigned int baudl;
24 unsigned int baudh;
25};
26
01496c4f
AB
27
28struct arc_serial_platdata {
29 struct arc_serial_regs *reg;
30 unsigned int uartclk;
31};
32
22a240c3
AB
33/* Bit definitions of STATUS register */
34#define UART_RXEMPTY (1 << 5)
35#define UART_OVERFLOW_ERR (1 << 1)
36#define UART_TXEMPTY (1 << 7)
37
01496c4f 38static int arc_serial_setbrg(struct udevice *dev, int baudrate)
22a240c3 39{
01496c4f
AB
40 struct arc_serial_platdata *plat = dev->platdata;
41 struct arc_serial_regs *const regs = plat->reg;
42 int arc_console_baud = gd->cpu_clk / (baudrate * 4) - 1;
22a240c3 43
94b5400e 44 writeb(arc_console_baud & 0xff, &regs->baudl);
94b5400e 45 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
22a240c3 46
22a240c3
AB
47 return 0;
48}
49
01496c4f 50static int arc_serial_putc(struct udevice *dev, const char c)
22a240c3 51{
01496c4f
AB
52 struct arc_serial_platdata *plat = dev->platdata;
53 struct arc_serial_regs *const regs = plat->reg;
54
94b5400e 55 while (!(readb(&regs->status) & UART_TXEMPTY))
22a240c3
AB
56 ;
57
94b5400e 58 writeb(c, &regs->data);
01496c4f
AB
59
60 return 0;
22a240c3
AB
61}
62
01496c4f 63static int arc_serial_tstc(struct arc_serial_regs *const regs)
22a240c3 64{
94b5400e 65 return !(readb(&regs->status) & UART_RXEMPTY);
22a240c3
AB
66}
67
01496c4f
AB
68static int arc_serial_pending(struct udevice *dev, bool input)
69{
70 struct arc_serial_platdata *plat = dev->platdata;
71 struct arc_serial_regs *const regs = plat->reg;
72 uint32_t status = readb(&regs->status);
73
74 if (input)
75 return status & UART_RXEMPTY ? 0 : 1;
76 else
77 return status & UART_TXEMPTY ? 0 : 1;
78}
79
80static int arc_serial_getc(struct udevice *dev)
22a240c3 81{
01496c4f
AB
82 struct arc_serial_platdata *plat = dev->platdata;
83 struct arc_serial_regs *const regs = plat->reg;
84
85 while (!arc_serial_tstc(regs))
22a240c3
AB
86 ;
87
88 /* Check for overflow errors */
94b5400e 89 if (readb(&regs->status) & UART_OVERFLOW_ERR)
22a240c3
AB
90 return 0;
91
94b5400e 92 return readb(&regs->data) & 0xFF;
22a240c3
AB
93}
94
01496c4f 95static int arc_serial_probe(struct udevice *dev)
22a240c3 96{
01496c4f 97 return 0;
22a240c3
AB
98}
99
01496c4f
AB
100static const struct dm_serial_ops arc_serial_ops = {
101 .putc = arc_serial_putc,
102 .pending = arc_serial_pending,
103 .getc = arc_serial_getc,
104 .setbrg = arc_serial_setbrg,
105};
106
107static const struct udevice_id arc_serial_ids[] = {
108 { .compatible = "snps,arc-uart" },
109 { }
110};
111
112static int arc_serial_ofdata_to_platdata(struct udevice *dev)
22a240c3 113{
01496c4f
AB
114 struct arc_serial_platdata *plat = dev_get_platdata(dev);
115 DECLARE_GLOBAL_DATA_PTR;
116
a821c4af 117 plat->reg = (struct arc_serial_regs *)devfdt_get_addr(dev);
e160f7d4 118 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
01496c4f
AB
119 "clock-frequency", 0);
120
121 return 0;
22a240c3 122}
01496c4f
AB
123
124U_BOOT_DRIVER(serial_arc) = {
125 .name = "serial_arc",
126 .id = UCLASS_SERIAL,
127 .of_match = arc_serial_ids,
128 .ofdata_to_platdata = arc_serial_ofdata_to_platdata,
129 .probe = arc_serial_probe,
130 .ops = &arc_serial_ops,
131 .flags = DM_FLAG_PRE_RELOC,
132};