Alexey Brodkin | 22a240c | 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 | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 11 | #include <dm.h> |
Alexey Brodkin | 22a240c | 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 | 01496c4 | 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 | 22a240c | 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 | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 38 | static int arc_serial_setbrg(struct udevice *dev, int baudrate) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 39 | { |
Alexey Brodkin | 01496c4 | 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 | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 43 | |
Alexey Brodkin | 94b5400 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 44 | writeb(arc_console_baud & 0xff, ®s->baudl); |
Alexey Brodkin | 94b5400 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 45 | writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh); |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 46 | |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 50 | static int arc_serial_putc(struct udevice *dev, const char c) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 51 | { |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 52 | struct arc_serial_platdata *plat = dev->platdata; |
| 53 | struct arc_serial_regs *const regs = plat->reg; |
| 54 | |
Alexey Brodkin | 94b5400 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 55 | while (!(readb(®s->status) & UART_TXEMPTY)) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 56 | ; |
| 57 | |
Alexey Brodkin | 94b5400 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 58 | writeb(c, ®s->data); |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 59 | |
| 60 | return 0; |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 61 | } |
| 62 | |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 63 | static int arc_serial_tstc(struct arc_serial_regs *const regs) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 64 | { |
Alexey Brodkin | 94b5400 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 65 | return !(readb(®s->status) & UART_RXEMPTY); |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 66 | } |
| 67 | |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 68 | static int arc_serial_pending(struct udevice *dev, bool input) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 69 | { |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 70 | struct arc_serial_platdata *plat = dev->platdata; |
| 71 | struct arc_serial_regs *const regs = plat->reg; |
| 72 | uint32_t status = readb(®s->status); |
| 73 | |
| 74 | if (input) |
| 75 | return status & UART_RXEMPTY ? 0 : 1; |
| 76 | else |
| 77 | return status & UART_TXEMPTY ? 0 : 1; |
| 78 | } |
| 79 | |
| 80 | static int arc_serial_getc(struct udevice *dev) |
| 81 | { |
| 82 | struct arc_serial_platdata *plat = dev->platdata; |
| 83 | struct arc_serial_regs *const regs = plat->reg; |
| 84 | |
| 85 | while (!arc_serial_tstc(regs)) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 86 | ; |
| 87 | |
| 88 | /* Check for overflow errors */ |
Alexey Brodkin | 94b5400 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 89 | if (readb(®s->status) & UART_OVERFLOW_ERR) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 90 | return 0; |
| 91 | |
Alexey Brodkin | 94b5400 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 92 | return readb(®s->data) & 0xFF; |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 93 | } |
| 94 | |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 95 | static int arc_serial_probe(struct udevice *dev) |
| 96 | { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static 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, |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 105 | }; |
| 106 | |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 107 | static const struct udevice_id arc_serial_ids[] = { |
| 108 | { .compatible = "snps,arc-uart" }, |
| 109 | { } |
| 110 | }; |
| 111 | |
| 112 | static int arc_serial_ofdata_to_platdata(struct udevice *dev) |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 113 | { |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 114 | struct arc_serial_platdata *plat = dev_get_platdata(dev); |
| 115 | DECLARE_GLOBAL_DATA_PTR; |
| 116 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 117 | plat->reg = (struct arc_serial_regs *)devfdt_get_addr(dev); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 118 | plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 119 | "clock-frequency", 0); |
| 120 | |
| 121 | return 0; |
Alexey Brodkin | 22a240c | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 122 | } |
| 123 | |
Alexey Brodkin | 01496c4 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 124 | U_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 | }; |
Alexey Brodkin | 5470501 | 2018-05-21 16:42:07 +0300 | [diff] [blame] | 133 | |
| 134 | #ifdef CONFIG_DEBUG_ARC_SERIAL |
| 135 | #include <debug_uart.h> |
| 136 | |
| 137 | static inline void _debug_uart_init(void) |
| 138 | { |
| 139 | struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE; |
| 140 | int arc_console_baud = CONFIG_DEBUG_UART_CLOCK / (CONFIG_BAUDRATE * 4) - 1; |
| 141 | |
| 142 | writeb(arc_console_baud & 0xff, ®s->baudl); |
| 143 | writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh); |
| 144 | } |
| 145 | |
| 146 | static inline void _debug_uart_putc(int c) |
| 147 | { |
| 148 | struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE; |
| 149 | |
| 150 | while (!(readb(®s->status) & UART_TXEMPTY)) |
| 151 | ; |
| 152 | |
| 153 | writeb(c, ®s->data); |
| 154 | } |
| 155 | |
| 156 | DEBUG_UART_FUNCS |
| 157 | |
| 158 | #endif |