Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | fa1f73f | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 3 | * Copyright (C) 2012-2015 Panasonic Corporation |
| 4 | * Copyright (C) 2015-2016 Socionext Inc. |
| 5 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 10 | #include <linux/bitfield.h> |
| 11 | #include <linux/bitops.h> |
Masahiro Yamada | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 12 | #include <linux/bug.h> |
Masahiro Yamada | 663a23f | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 13 | #include <linux/io.h> |
Masahiro Yamada | 9caae5e | 2014-10-30 12:11:14 +0900 | [diff] [blame] | 14 | #include <linux/serial_reg.h> |
Masahiro Yamada | 6ee016e | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 15 | #include <linux/sizes.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 16 | #include <linux/errno.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 17 | #include <serial.h> |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 18 | #include <fdtdec.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 19 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 20 | #define UNIPHIER_UART_REGSHIFT 2 |
| 21 | |
| 22 | #define UNIPHIER_UART_RX (0 << (UNIPHIER_UART_REGSHIFT)) |
| 23 | #define UNIPHIER_UART_TX UNIPHIER_UART_RX |
| 24 | /* bit[15:8] = CHAR, bit[7:0] = FCR */ |
| 25 | #define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT)) |
Masahiro Yamada | c346d42 | 2020-07-10 01:12:08 +0900 | [diff] [blame] | 26 | #define UNIPHIER_UART_FCR_MASK GENMASK(7, 0) |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 27 | /* bit[15:8] = LCR, bit[7:0] = MCR */ |
| 28 | #define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT)) |
| 29 | #define UNIPHIER_UART_LCR_MASK GENMASK(15, 8) |
| 30 | #define UNIPHIER_UART_LSR (5 << (UNIPHIER_UART_REGSHIFT)) |
| 31 | /* Divisor Latch Register */ |
| 32 | #define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT)) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 33 | |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 34 | struct uniphier_serial_priv { |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 35 | void __iomem *membase; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 36 | unsigned int uartclk; |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 37 | }; |
| 38 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 39 | static int uniphier_serial_setbrg(struct udevice *dev, int baudrate) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 40 | { |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 41 | struct uniphier_serial_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 42 | static const unsigned int mode_x_div = 16; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 43 | unsigned int divisor; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 44 | |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 45 | divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 46 | |
Masahiro Yamada | b8daeda | 2020-07-10 01:12:07 +0900 | [diff] [blame] | 47 | /* flush the trasmitter before changing hw setting */ |
| 48 | while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT)) |
| 49 | ; |
| 50 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 51 | writel(divisor, priv->membase + UNIPHIER_UART_DLR); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 52 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 53 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 56 | static int uniphier_serial_getc(struct udevice *dev) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 57 | { |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 58 | struct uniphier_serial_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 59 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 60 | if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 61 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 62 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 63 | return readl(priv->membase + UNIPHIER_UART_RX); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 64 | } |
| 65 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 66 | static int uniphier_serial_putc(struct udevice *dev, const char c) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 67 | { |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 68 | struct uniphier_serial_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 69 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 70 | if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 71 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 72 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 73 | writel(c, priv->membase + UNIPHIER_UART_TX); |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 74 | |
| 75 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 76 | } |
| 77 | |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 78 | static int uniphier_serial_pending(struct udevice *dev, bool input) |
| 79 | { |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 80 | struct uniphier_serial_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 81 | |
| 82 | if (input) |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 83 | return readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR; |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 84 | else |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 85 | return !(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE); |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Masahiro Yamada | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 88 | /* |
| 89 | * SPL does not have enough memory footprint for the clock driver. |
| 90 | * Hardcode clock frequency for each SoC. |
| 91 | */ |
| 92 | struct uniphier_serial_clk_data { |
| 93 | const char *compatible; |
| 94 | unsigned int clk_rate; |
| 95 | }; |
| 96 | |
| 97 | static const struct uniphier_serial_clk_data uniphier_serial_clk_data[] = { |
| 98 | { .compatible = "socionext,uniphier-ld4", .clk_rate = 36864000 }, |
| 99 | { .compatible = "socionext,uniphier-pro4", .clk_rate = 73728000 }, |
| 100 | { .compatible = "socionext,uniphier-sld8", .clk_rate = 80000000 }, |
| 101 | { .compatible = "socionext,uniphier-pro5", .clk_rate = 73728000 }, |
| 102 | { .compatible = "socionext,uniphier-pxs2", .clk_rate = 88888888 }, |
| 103 | { .compatible = "socionext,uniphier-ld6b", .clk_rate = 88888888 }, |
| 104 | { .compatible = "socionext,uniphier-ld11", .clk_rate = 58823529 }, |
| 105 | { .compatible = "socionext,uniphier-ld20", .clk_rate = 58823529 }, |
| 106 | { .compatible = "socionext,uniphier-pxs3", .clk_rate = 58823529 }, |
| 107 | { /* sentinel */ }, |
| 108 | }; |
| 109 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 110 | static int uniphier_serial_probe(struct udevice *dev) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 111 | { |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 112 | struct uniphier_serial_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 113 | const struct uniphier_serial_clk_data *clk_data; |
| 114 | ofnode root_node; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 115 | fdt_addr_t base; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 116 | u32 tmp; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 117 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 118 | base = dev_read_addr(dev); |
Masahiro Yamada | 6ee016e | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 119 | if (base == FDT_ADDR_T_NONE) |
| 120 | return -EINVAL; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 121 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 122 | priv->membase = devm_ioremap(dev, base, SZ_64); |
| 123 | if (!priv->membase) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 124 | return -ENOMEM; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 125 | |
Masahiro Yamada | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 126 | root_node = ofnode_path("/"); |
| 127 | clk_data = uniphier_serial_clk_data; |
| 128 | while (clk_data->compatible) { |
| 129 | if (ofnode_device_is_compatible(root_node, |
| 130 | clk_data->compatible)) |
| 131 | break; |
| 132 | clk_data++; |
| 133 | } |
| 134 | |
| 135 | if (WARN_ON(!clk_data->compatible)) |
| 136 | return -ENOTSUPP; |
| 137 | |
| 138 | priv->uartclk = clk_data->clk_rate; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 139 | |
Masahiro Yamada | c3d2559 | 2020-07-30 18:28:07 +0900 | [diff] [blame] | 140 | /* flush the trasmitter before changing hw setting */ |
Masahiro Yamada | b8daeda | 2020-07-10 01:12:07 +0900 | [diff] [blame] | 141 | while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT)) |
| 142 | ; |
| 143 | |
Masahiro Yamada | c346d42 | 2020-07-10 01:12:08 +0900 | [diff] [blame] | 144 | /* enable FIFO */ |
| 145 | tmp = readl(priv->membase + UNIPHIER_UART_CHAR_FCR); |
| 146 | tmp &= ~UNIPHIER_UART_FCR_MASK; |
| 147 | tmp |= FIELD_PREP(UNIPHIER_UART_FCR_MASK, UART_FCR_ENABLE_FIFO); |
| 148 | writel(tmp, priv->membase + UNIPHIER_UART_CHAR_FCR); |
| 149 | |
Masahiro Yamada | f2d670a | 2020-07-10 01:12:06 +0900 | [diff] [blame] | 150 | tmp = readl(priv->membase + UNIPHIER_UART_LCR_MCR); |
| 151 | tmp &= ~UNIPHIER_UART_LCR_MASK; |
| 152 | tmp |= FIELD_PREP(UNIPHIER_UART_LCR_MASK, UART_LCR_WLEN8); |
| 153 | writel(tmp, priv->membase + UNIPHIER_UART_LCR_MCR); |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 154 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 155 | return 0; |
| 156 | } |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 157 | |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 158 | static const struct udevice_id uniphier_uart_of_match[] = { |
Masahiro Yamada | d5f83a4 | 2015-03-11 15:54:46 +0900 | [diff] [blame] | 159 | { .compatible = "socionext,uniphier-uart" }, |
| 160 | { /* sentinel */ } |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 161 | }; |
| 162 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 163 | static const struct dm_serial_ops uniphier_serial_ops = { |
| 164 | .setbrg = uniphier_serial_setbrg, |
| 165 | .getc = uniphier_serial_getc, |
| 166 | .putc = uniphier_serial_putc, |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 167 | .pending = uniphier_serial_pending, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | U_BOOT_DRIVER(uniphier_serial) = { |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 171 | .name = "uniphier-uart", |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 172 | .id = UCLASS_SERIAL, |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 173 | .of_match = uniphier_uart_of_match, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 174 | .probe = uniphier_serial_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 175 | .priv_auto = sizeof(struct uniphier_serial_priv), |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 176 | .ops = &uniphier_serial_ops, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 177 | }; |