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