Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 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> |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 11 | #include <dm.h> |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 12 | #include <serial.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
| 16 | struct 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 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 27 | |
| 28 | struct arc_serial_platdata { |
| 29 | struct arc_serial_regs *reg; |
| 30 | unsigned int uartclk; |
| 31 | }; |
| 32 | |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 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 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 38 | static int arc_serial_setbrg(struct udevice *dev, int baudrate) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 39 | { |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 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; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 43 | |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 44 | writeb(arc_console_baud & 0xff, ®s->baudl); |
Alexey Brodkin | 0c12620 | 2014-02-08 10:10:01 +0400 | [diff] [blame] | 45 | |
| 46 | #ifdef CONFIG_ARC |
| 47 | /* |
| 48 | * UART ISS(Instruction Set simulator) emulation has a subtle bug: |
| 49 | * A existing value of Baudh = 0 is used as a indication to startup |
| 50 | * it's internal state machine. |
| 51 | * Thus if baudh is set to 0, 2 times, it chokes. |
| 52 | * This happens with BAUD=115200 and the formaula above |
| 53 | * Until that is fixed, when running on ISS, we will set baudh to !0 |
| 54 | */ |
| 55 | if (gd->arch.running_on_hw) |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 56 | writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh); |
Alexey Brodkin | 0c12620 | 2014-02-08 10:10:01 +0400 | [diff] [blame] | 57 | else |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 58 | writeb(1, ®s->baudh); |
Alexey Brodkin | 0c12620 | 2014-02-08 10:10:01 +0400 | [diff] [blame] | 59 | #else |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 60 | writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh); |
Alexey Brodkin | 0c12620 | 2014-02-08 10:10:01 +0400 | [diff] [blame] | 61 | #endif |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 62 | |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 66 | static int arc_serial_putc(struct udevice *dev, const char c) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 67 | { |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 68 | struct arc_serial_platdata *plat = dev->platdata; |
| 69 | struct arc_serial_regs *const regs = plat->reg; |
| 70 | |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 71 | if (c == '\n') |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 72 | arc_serial_putc(dev, '\r'); |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 73 | |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 74 | while (!(readb(®s->status) & UART_TXEMPTY)) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 75 | ; |
| 76 | |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 77 | writeb(c, ®s->data); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 78 | |
| 79 | return 0; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 80 | } |
| 81 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 82 | static int arc_serial_tstc(struct arc_serial_regs *const regs) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 83 | { |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 84 | return !(readb(®s->status) & UART_RXEMPTY); |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 85 | } |
| 86 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 87 | static int arc_serial_pending(struct udevice *dev, bool input) |
| 88 | { |
| 89 | struct arc_serial_platdata *plat = dev->platdata; |
| 90 | struct arc_serial_regs *const regs = plat->reg; |
| 91 | uint32_t status = readb(®s->status); |
| 92 | |
| 93 | if (input) |
| 94 | return status & UART_RXEMPTY ? 0 : 1; |
| 95 | else |
| 96 | return status & UART_TXEMPTY ? 0 : 1; |
| 97 | } |
| 98 | |
| 99 | static int arc_serial_getc(struct udevice *dev) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 100 | { |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 101 | struct arc_serial_platdata *plat = dev->platdata; |
| 102 | struct arc_serial_regs *const regs = plat->reg; |
| 103 | |
| 104 | while (!arc_serial_tstc(regs)) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 105 | ; |
| 106 | |
| 107 | /* Check for overflow errors */ |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 108 | if (readb(®s->status) & UART_OVERFLOW_ERR) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 109 | return 0; |
| 110 | |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 111 | return readb(®s->data) & 0xFF; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 112 | } |
| 113 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 114 | static int arc_serial_probe(struct udevice *dev) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 115 | { |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 116 | return 0; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 117 | } |
| 118 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 119 | static const struct dm_serial_ops arc_serial_ops = { |
| 120 | .putc = arc_serial_putc, |
| 121 | .pending = arc_serial_pending, |
| 122 | .getc = arc_serial_getc, |
| 123 | .setbrg = arc_serial_setbrg, |
| 124 | }; |
| 125 | |
| 126 | static const struct udevice_id arc_serial_ids[] = { |
| 127 | { .compatible = "snps,arc-uart" }, |
| 128 | { } |
| 129 | }; |
| 130 | |
| 131 | static int arc_serial_ofdata_to_platdata(struct udevice *dev) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 132 | { |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 133 | struct arc_serial_platdata *plat = dev_get_platdata(dev); |
| 134 | DECLARE_GLOBAL_DATA_PTR; |
| 135 | |
Simon Glass | 0971778 | 2015-08-11 08:33:29 -0600 | [diff] [blame^] | 136 | plat->reg = (struct arc_serial_regs *)dev_get_addr(dev); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 137 | plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 138 | "clock-frequency", 0); |
| 139 | |
| 140 | return 0; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 141 | } |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 142 | |
| 143 | U_BOOT_DRIVER(serial_arc) = { |
| 144 | .name = "serial_arc", |
| 145 | .id = UCLASS_SERIAL, |
| 146 | .of_match = arc_serial_ids, |
| 147 | .ofdata_to_platdata = arc_serial_ofdata_to_platdata, |
| 148 | .probe = arc_serial_probe, |
| 149 | .ops = &arc_serial_ops, |
| 150 | .flags = DM_FLAG_PRE_RELOC, |
| 151 | }; |