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