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 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 10 | #include <dm.h> |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 11 | #include <serial.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 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 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 27 | struct arc_serial_plat { |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 28 | struct arc_serial_regs *reg; |
| 29 | unsigned int uartclk; |
| 30 | }; |
| 31 | |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 32 | /* Bit definitions of STATUS register */ |
| 33 | #define UART_RXEMPTY (1 << 5) |
| 34 | #define UART_OVERFLOW_ERR (1 << 1) |
| 35 | #define UART_TXEMPTY (1 << 7) |
| 36 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 37 | static int arc_serial_setbrg(struct udevice *dev, int baudrate) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 38 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 39 | struct arc_serial_plat *plat = dev_get_plat(dev); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 40 | struct arc_serial_regs *const regs = plat->reg; |
| 41 | int arc_console_baud = gd->cpu_clk / (baudrate * 4) - 1; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 42 | |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 43 | writeb(arc_console_baud & 0xff, ®s->baudl); |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 44 | writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh); |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 45 | |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 49 | static int arc_serial_putc(struct udevice *dev, const char c) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 50 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 51 | struct arc_serial_plat *plat = dev_get_plat(dev); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 52 | struct arc_serial_regs *const regs = plat->reg; |
| 53 | |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 54 | if (!(readb(®s->status) & UART_TXEMPTY)) |
| 55 | return -EAGAIN; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 56 | |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 57 | writeb(c, ®s->data); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 58 | |
| 59 | return 0; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 60 | } |
| 61 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 62 | static int arc_serial_tstc(struct arc_serial_regs *const regs) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 63 | { |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 64 | return !(readb(®s->status) & UART_RXEMPTY); |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 65 | } |
| 66 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 67 | static int arc_serial_pending(struct udevice *dev, bool input) |
| 68 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 69 | struct arc_serial_plat *plat = dev_get_plat(dev); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 70 | struct arc_serial_regs *const regs = plat->reg; |
| 71 | uint32_t status = readb(®s->status); |
| 72 | |
| 73 | if (input) |
| 74 | return status & UART_RXEMPTY ? 0 : 1; |
| 75 | else |
| 76 | return status & UART_TXEMPTY ? 0 : 1; |
| 77 | } |
| 78 | |
| 79 | static int arc_serial_getc(struct udevice *dev) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 80 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 81 | struct arc_serial_plat *plat = dev_get_plat(dev); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 82 | struct arc_serial_regs *const regs = plat->reg; |
| 83 | |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 84 | if (!arc_serial_tstc(regs)) |
| 85 | return -EAGAIN; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 86 | |
| 87 | /* Check for overflow errors */ |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 88 | if (readb(®s->status) & UART_OVERFLOW_ERR) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 89 | return 0; |
| 90 | |
Alexey Brodkin | 326d837 | 2014-02-08 10:10:02 +0400 | [diff] [blame] | 91 | return readb(®s->data) & 0xFF; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 92 | } |
| 93 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 94 | static int arc_serial_probe(struct udevice *dev) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 95 | { |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 96 | return 0; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 97 | } |
| 98 | |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 99 | static const struct dm_serial_ops arc_serial_ops = { |
| 100 | .putc = arc_serial_putc, |
| 101 | .pending = arc_serial_pending, |
| 102 | .getc = arc_serial_getc, |
| 103 | .setbrg = arc_serial_setbrg, |
| 104 | }; |
| 105 | |
| 106 | static const struct udevice_id arc_serial_ids[] = { |
| 107 | { .compatible = "snps,arc-uart" }, |
| 108 | { } |
| 109 | }; |
| 110 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 111 | static int arc_serial_of_to_plat(struct udevice *dev) |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 112 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 113 | struct arc_serial_plat *plat = dev_get_plat(dev); |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 114 | DECLARE_GLOBAL_DATA_PTR; |
| 115 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 116 | plat->reg = dev_read_addr_ptr(dev); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 117 | plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 118 | "clock-frequency", 0); |
| 119 | |
| 120 | return 0; |
Alexey Brodkin | c51eb11 | 2013-12-13 10:35:11 +0400 | [diff] [blame] | 121 | } |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 122 | |
| 123 | U_BOOT_DRIVER(serial_arc) = { |
| 124 | .name = "serial_arc", |
| 125 | .id = UCLASS_SERIAL, |
| 126 | .of_match = arc_serial_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 127 | .of_to_plat = arc_serial_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 128 | .plat_auto = sizeof(struct arc_serial_plat), |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 129 | .probe = arc_serial_probe, |
| 130 | .ops = &arc_serial_ops, |
Alexey Brodkin | e458cc1 | 2015-03-17 14:55:14 +0300 | [diff] [blame] | 131 | }; |
Alexey Brodkin | eede167 | 2018-05-21 16:42:07 +0300 | [diff] [blame] | 132 | |
| 133 | #ifdef CONFIG_DEBUG_ARC_SERIAL |
| 134 | #include <debug_uart.h> |
| 135 | |
| 136 | static inline void _debug_uart_init(void) |
| 137 | { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 138 | struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE); |
Alexey Brodkin | eede167 | 2018-05-21 16:42:07 +0300 | [diff] [blame] | 139 | int arc_console_baud = CONFIG_DEBUG_UART_CLOCK / (CONFIG_BAUDRATE * 4) - 1; |
| 140 | |
| 141 | writeb(arc_console_baud & 0xff, ®s->baudl); |
| 142 | writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh); |
| 143 | } |
| 144 | |
| 145 | static inline void _debug_uart_putc(int c) |
| 146 | { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 147 | struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE); |
Alexey Brodkin | eede167 | 2018-05-21 16:42:07 +0300 | [diff] [blame] | 148 | |
| 149 | while (!(readb(®s->status) & UART_TXEMPTY)) |
| 150 | ; |
| 151 | |
| 152 | writeb(c, ®s->data); |
| 153 | } |
| 154 | |
| 155 | DEBUG_UART_FUNCS |
| 156 | |
| 157 | #endif |