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 | |
Simon Glass | 51a3ec3 | 2017-05-17 17:18:07 -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 | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 10 | #include <linux/bug.h> |
Masahiro Yamada | 663a23f | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 11 | #include <linux/io.h> |
Masahiro Yamada | 9caae5e | 2014-10-30 12:11:14 +0900 | [diff] [blame] | 12 | #include <linux/serial_reg.h> |
Masahiro Yamada | 6ee016e | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 13 | #include <linux/sizes.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 14 | #include <linux/errno.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 15 | #include <serial.h> |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 16 | #include <fdtdec.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 17 | |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 18 | /* |
| 19 | * Note: Register map is slightly different from that of 16550. |
| 20 | */ |
| 21 | struct uniphier_serial { |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 22 | u32 rx; /* In: Receive buffer */ |
| 23 | #define tx rx /* Out: Transmit buffer */ |
| 24 | u32 ier; /* Interrupt Enable Register */ |
| 25 | u32 iir; /* In: Interrupt ID Register */ |
| 26 | u32 char_fcr; /* Charactor / FIFO Control Register */ |
| 27 | u32 lcr_mcr; /* Line/Modem Control Register */ |
| 28 | #define LCR_SHIFT 8 |
| 29 | #define LCR_MASK (0xff << (LCR_SHIFT)) |
| 30 | u32 lsr; /* In: Line Status Register */ |
| 31 | u32 msr; /* In: Modem Status Register */ |
| 32 | u32 __rsv0; |
| 33 | u32 __rsv1; |
| 34 | u32 dlr; /* Divisor Latch Register */ |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 35 | }; |
| 36 | |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 37 | struct uniphier_serial_priv { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 38 | struct uniphier_serial __iomem *membase; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 39 | unsigned int uartclk; |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | #define uniphier_serial_port(dev) \ |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 43 | ((struct uniphier_serial_priv *)dev_get_priv(dev))->membase |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 44 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 45 | static int uniphier_serial_setbrg(struct udevice *dev, int baudrate) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 46 | { |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 47 | struct uniphier_serial_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 48 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 49 | const unsigned int mode_x_div = 16; |
| 50 | unsigned int divisor; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 51 | |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 52 | divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 53 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 54 | writel(divisor, &port->dlr); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 55 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 56 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 59 | static int uniphier_serial_getc(struct udevice *dev) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 60 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 61 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 62 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 63 | if (!(readl(&port->lsr) & UART_LSR_DR)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 64 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 65 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 66 | return readl(&port->rx); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 69 | static int uniphier_serial_putc(struct udevice *dev, const char c) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 70 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 71 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 72 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 73 | if (!(readl(&port->lsr) & UART_LSR_THRE)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 74 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 75 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 76 | writel(c, &port->tx); |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 77 | |
| 78 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 81 | static int uniphier_serial_pending(struct udevice *dev, bool input) |
| 82 | { |
| 83 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
| 84 | |
| 85 | if (input) |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 86 | return readl(&port->lsr) & UART_LSR_DR; |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 87 | else |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 88 | return !(readl(&port->lsr) & UART_LSR_THRE); |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Masahiro Yamada | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 91 | /* |
| 92 | * SPL does not have enough memory footprint for the clock driver. |
| 93 | * Hardcode clock frequency for each SoC. |
| 94 | */ |
| 95 | struct uniphier_serial_clk_data { |
| 96 | const char *compatible; |
| 97 | unsigned int clk_rate; |
| 98 | }; |
| 99 | |
| 100 | static const struct uniphier_serial_clk_data uniphier_serial_clk_data[] = { |
| 101 | { .compatible = "socionext,uniphier-ld4", .clk_rate = 36864000 }, |
| 102 | { .compatible = "socionext,uniphier-pro4", .clk_rate = 73728000 }, |
| 103 | { .compatible = "socionext,uniphier-sld8", .clk_rate = 80000000 }, |
| 104 | { .compatible = "socionext,uniphier-pro5", .clk_rate = 73728000 }, |
| 105 | { .compatible = "socionext,uniphier-pxs2", .clk_rate = 88888888 }, |
| 106 | { .compatible = "socionext,uniphier-ld6b", .clk_rate = 88888888 }, |
| 107 | { .compatible = "socionext,uniphier-ld11", .clk_rate = 58823529 }, |
| 108 | { .compatible = "socionext,uniphier-ld20", .clk_rate = 58823529 }, |
| 109 | { .compatible = "socionext,uniphier-pxs3", .clk_rate = 58823529 }, |
| 110 | { /* sentinel */ }, |
| 111 | }; |
| 112 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 113 | static int uniphier_serial_probe(struct udevice *dev) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 114 | { |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 115 | struct uniphier_serial_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 116 | struct uniphier_serial __iomem *port; |
Masahiro Yamada | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 117 | const struct uniphier_serial_clk_data *clk_data; |
| 118 | ofnode root_node; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 119 | fdt_addr_t base; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 120 | u32 tmp; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 121 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 122 | base = devfdt_get_addr(dev); |
Masahiro Yamada | 6ee016e | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 123 | if (base == FDT_ADDR_T_NONE) |
| 124 | return -EINVAL; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 125 | |
Masahiro Yamada | fa1f73f | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 126 | port = devm_ioremap(dev, base, SZ_64); |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 127 | if (!port) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 128 | return -ENOMEM; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 129 | |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 130 | priv->membase = port; |
| 131 | |
Masahiro Yamada | 66f4a9a | 2018-06-19 16:11:45 +0900 | [diff] [blame] | 132 | root_node = ofnode_path("/"); |
| 133 | clk_data = uniphier_serial_clk_data; |
| 134 | while (clk_data->compatible) { |
| 135 | if (ofnode_device_is_compatible(root_node, |
| 136 | clk_data->compatible)) |
| 137 | break; |
| 138 | clk_data++; |
| 139 | } |
| 140 | |
| 141 | if (WARN_ON(!clk_data->compatible)) |
| 142 | return -ENOTSUPP; |
| 143 | |
| 144 | priv->uartclk = clk_data->clk_rate; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 145 | |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 146 | tmp = readl(&port->lcr_mcr); |
| 147 | tmp &= ~LCR_MASK; |
| 148 | tmp |= UART_LCR_WLEN8 << LCR_SHIFT; |
| 149 | writel(tmp, &port->lcr_mcr); |
| 150 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 151 | return 0; |
| 152 | } |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 153 | |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 154 | static const struct udevice_id uniphier_uart_of_match[] = { |
Masahiro Yamada | d5f83a4 | 2015-03-11 15:54:46 +0900 | [diff] [blame] | 155 | { .compatible = "socionext,uniphier-uart" }, |
| 156 | { /* sentinel */ } |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 157 | }; |
| 158 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 159 | static const struct dm_serial_ops uniphier_serial_ops = { |
| 160 | .setbrg = uniphier_serial_setbrg, |
| 161 | .getc = uniphier_serial_getc, |
| 162 | .putc = uniphier_serial_putc, |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 163 | .pending = uniphier_serial_pending, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | U_BOOT_DRIVER(uniphier_serial) = { |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 167 | .name = "uniphier-uart", |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 168 | .id = UCLASS_SERIAL, |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 169 | .of_match = uniphier_uart_of_match, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 170 | .probe = uniphier_serial_probe, |
Masahiro Yamada | 8b5b1ee | 2018-06-19 16:11:44 +0900 | [diff] [blame] | 171 | .priv_auto_alloc_size = sizeof(struct uniphier_serial_priv), |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 172 | .ops = &uniphier_serial_ops, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 173 | }; |