Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | fa1f73f | 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 | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 5 | * |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
Masahiro Yamada | 663a23f | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 9 | #include <linux/io.h> |
Masahiro Yamada | 9caae5e | 2014-10-30 12:11:14 +0900 | [diff] [blame] | 10 | #include <linux/serial_reg.h> |
Masahiro Yamada | 6ee016e | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 11 | #include <linux/sizes.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 13 | #include <dm/device.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 14 | #include <serial.h> |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 15 | #include <fdtdec.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 16 | |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 17 | /* |
| 18 | * Note: Register map is slightly different from that of 16550. |
| 19 | */ |
| 20 | struct uniphier_serial { |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 21 | u32 rx; /* In: Receive buffer */ |
| 22 | #define tx rx /* Out: Transmit buffer */ |
| 23 | u32 ier; /* Interrupt Enable Register */ |
| 24 | u32 iir; /* In: Interrupt ID Register */ |
| 25 | u32 char_fcr; /* Charactor / FIFO Control Register */ |
| 26 | u32 lcr_mcr; /* Line/Modem Control Register */ |
| 27 | #define LCR_SHIFT 8 |
| 28 | #define LCR_MASK (0xff << (LCR_SHIFT)) |
| 29 | u32 lsr; /* In: Line Status Register */ |
| 30 | u32 msr; /* In: Modem Status Register */ |
| 31 | u32 __rsv0; |
| 32 | u32 __rsv1; |
| 33 | u32 dlr; /* Divisor Latch Register */ |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 34 | }; |
| 35 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 36 | struct uniphier_serial_private_data { |
| 37 | struct uniphier_serial __iomem *membase; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 38 | unsigned int uartclk; |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define uniphier_serial_port(dev) \ |
| 42 | ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 43 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 44 | static int uniphier_serial_setbrg(struct udevice *dev, int baudrate) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 45 | { |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 46 | struct uniphier_serial_private_data *priv = dev_get_priv(dev); |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 47 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 48 | const unsigned int mode_x_div = 16; |
| 49 | unsigned int divisor; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 50 | |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 51 | divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 52 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 53 | writel(divisor, &port->dlr); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 54 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 55 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 56 | } |
| 57 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 58 | static int uniphier_serial_getc(struct udevice *dev) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 59 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 60 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 61 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 62 | if (!(readl(&port->lsr) & UART_LSR_DR)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 63 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 64 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 65 | return readl(&port->rx); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 68 | static int uniphier_serial_putc(struct udevice *dev, const char c) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 69 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 70 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 71 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 72 | if (!(readl(&port->lsr) & UART_LSR_THRE)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 73 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 74 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 75 | writel(c, &port->tx); |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 76 | |
| 77 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 78 | } |
| 79 | |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 80 | static int uniphier_serial_pending(struct udevice *dev, bool input) |
| 81 | { |
| 82 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
| 83 | |
| 84 | if (input) |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 85 | return readl(&port->lsr) & UART_LSR_DR; |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 86 | else |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 87 | return !(readl(&port->lsr) & UART_LSR_THRE); |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 90 | static int uniphier_serial_probe(struct udevice *dev) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 91 | { |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 92 | DECLARE_GLOBAL_DATA_PTR; |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 93 | struct uniphier_serial_private_data *priv = dev_get_priv(dev); |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 94 | struct uniphier_serial __iomem *port; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 95 | fdt_addr_t base; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 96 | u32 tmp; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 97 | |
Masahiro Yamada | 6ee016e | 2016-03-24 22:32:38 +0900 | [diff] [blame] | 98 | base = dev_get_addr(dev); |
| 99 | if (base == FDT_ADDR_T_NONE) |
| 100 | return -EINVAL; |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 101 | |
Masahiro Yamada | fa1f73f | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 102 | port = devm_ioremap(dev, base, SZ_64); |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 103 | if (!port) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 104 | return -ENOMEM; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 105 | |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 106 | priv->membase = port; |
| 107 | |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 108 | priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 109 | "clock-frequency", 0); |
| 110 | |
Masahiro Yamada | fef7fcd | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 111 | tmp = readl(&port->lcr_mcr); |
| 112 | tmp &= ~LCR_MASK; |
| 113 | tmp |= UART_LCR_WLEN8 << LCR_SHIFT; |
| 114 | writel(tmp, &port->lcr_mcr); |
| 115 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 116 | return 0; |
| 117 | } |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 118 | |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 119 | static const struct udevice_id uniphier_uart_of_match[] = { |
Masahiro Yamada | d5f83a4 | 2015-03-11 15:54:46 +0900 | [diff] [blame] | 120 | { .compatible = "socionext,uniphier-uart" }, |
| 121 | { /* sentinel */ } |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 122 | }; |
| 123 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 124 | static const struct dm_serial_ops uniphier_serial_ops = { |
| 125 | .setbrg = uniphier_serial_setbrg, |
| 126 | .getc = uniphier_serial_getc, |
| 127 | .putc = uniphier_serial_putc, |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 128 | .pending = uniphier_serial_pending, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | U_BOOT_DRIVER(uniphier_serial) = { |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 132 | .name = "uniphier-uart", |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 133 | .id = UCLASS_SERIAL, |
Masahiro Yamada | 25318dc | 2015-08-28 20:13:19 +0900 | [diff] [blame] | 134 | .of_match = uniphier_uart_of_match, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 135 | .probe = uniphier_serial_probe, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 136 | .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data), |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 137 | .ops = &uniphier_serial_ops, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 138 | }; |