Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | 4e3d840 | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Panasonic Corporation |
| 3 | * Copyright (C) 2015-2016 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 5 | * |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
Simon Glass | 4af0d7e | 2017-05-17 17:18:07 -0600 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Masahiro Yamada | f6e7f07 | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 11 | #include <linux/io.h> |
Masahiro Yamada | 325b708 | 2014-10-30 12:11:14 +0900 | [diff] [blame] | 12 | #include <linux/serial_reg.h> |
Masahiro Yamada | b37a1cc | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 13 | #include <linux/sizes.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 14 | #include <linux/errno.h> |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 15 | #include <serial.h> |
Masahiro Yamada | 625177d | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 16 | #include <fdtdec.h> |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 17 | |
Masahiro Yamada | 7f36855 | 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 | d0c47b3 | 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 | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 35 | }; |
| 36 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 37 | struct uniphier_serial_private_data { |
| 38 | struct uniphier_serial __iomem *membase; |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 39 | unsigned int uartclk; |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 40 | }; |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 41 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 42 | #define uniphier_serial_port(dev) \ |
| 43 | ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase |
| 44 | |
Masahiro Yamada | d9bc8fd | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 45 | static int uniphier_serial_setbrg(struct udevice *dev, int baudrate) |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 46 | { |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 47 | struct uniphier_serial_private_data *priv = dev_get_priv(dev); |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 48 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 49 | const unsigned int mode_x_div = 16; |
| 50 | unsigned int divisor; |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 51 | |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 52 | divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate); |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 53 | |
Masahiro Yamada | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 54 | writel(divisor, &port->dlr); |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 55 | |
| 56 | return 0; |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 59 | static int uniphier_serial_getc(struct udevice *dev) |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 60 | { |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 61 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 62 | |
Masahiro Yamada | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 63 | if (!(readl(&port->lsr) & UART_LSR_DR)) |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 64 | return -EAGAIN; |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 65 | |
Masahiro Yamada | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 66 | return readl(&port->rx); |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 69 | static int uniphier_serial_putc(struct udevice *dev, const char c) |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 70 | { |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 71 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 72 | |
Masahiro Yamada | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 73 | if (!(readl(&port->lsr) & UART_LSR_THRE)) |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 74 | return -EAGAIN; |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 75 | |
Masahiro Yamada | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 76 | writel(c, &port->tx); |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 77 | |
| 78 | return 0; |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Masahiro Yamada | bb72148 | 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 | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 86 | return readl(&port->lsr) & UART_LSR_DR; |
Masahiro Yamada | bb72148 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 87 | else |
Masahiro Yamada | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 88 | return !(readl(&port->lsr) & UART_LSR_THRE); |
Masahiro Yamada | bb72148 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Masahiro Yamada | d9bc8fd | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 91 | static int uniphier_serial_probe(struct udevice *dev) |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 92 | { |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 93 | DECLARE_GLOBAL_DATA_PTR; |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 94 | struct uniphier_serial_private_data *priv = dev_get_priv(dev); |
Masahiro Yamada | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 95 | struct uniphier_serial __iomem *port; |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 96 | fdt_addr_t base; |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 97 | u32 tmp; |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 98 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 99 | base = devfdt_get_addr(dev); |
Masahiro Yamada | b37a1cc | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 100 | if (base == FDT_ADDR_T_NONE) |
| 101 | return -EINVAL; |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 102 | |
Masahiro Yamada | 4e3d840 | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 103 | port = devm_ioremap(dev, base, SZ_64); |
Masahiro Yamada | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 104 | if (!port) |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 105 | return -ENOMEM; |
| 106 | |
Masahiro Yamada | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 107 | priv->membase = port; |
| 108 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 109 | priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 110 | "clock-frequency", 0); |
| 111 | |
Masahiro Yamada | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 112 | tmp = readl(&port->lcr_mcr); |
| 113 | tmp &= ~LCR_MASK; |
| 114 | tmp |= UART_LCR_WLEN8 << LCR_SHIFT; |
| 115 | writel(tmp, &port->lcr_mcr); |
| 116 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Masahiro Yamada | 625177d | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 120 | static const struct udevice_id uniphier_uart_of_match[] = { |
Masahiro Yamada | 6462cde | 2015-03-11 15:54:46 +0900 | [diff] [blame] | 121 | { .compatible = "socionext,uniphier-uart" }, |
| 122 | { /* sentinel */ } |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 123 | }; |
| 124 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 125 | static const struct dm_serial_ops uniphier_serial_ops = { |
| 126 | .setbrg = uniphier_serial_setbrg, |
| 127 | .getc = uniphier_serial_getc, |
| 128 | .putc = uniphier_serial_putc, |
Masahiro Yamada | bb72148 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 129 | .pending = uniphier_serial_pending, |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | U_BOOT_DRIVER(uniphier_serial) = { |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 133 | .name = "uniphier-uart", |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 134 | .id = UCLASS_SERIAL, |
Masahiro Yamada | 6d99cfa | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 135 | .of_match = uniphier_uart_of_match, |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 136 | .probe = uniphier_serial_probe, |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 137 | .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data), |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 138 | .ops = &uniphier_serial_ops, |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 139 | }; |