Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | d0c47b3 | 2015-02-27 02:26:46 +0900 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Panasonic Corporation |
Masahiro Yamada | 6462cde | 2015-03-11 15:54:46 +0900 | [diff] [blame] | 3 | * Copyright (C) 2015 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 | |
Masahiro Yamada | 325b708 | 2014-10-30 12:11:14 +0900 | [diff] [blame] | 9 | #include <linux/serial_reg.h> |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/errno.h> |
| 12 | #include <dm/device.h> |
| 13 | #include <dm/platform_data/serial-uniphier.h> |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 14 | #include <serial.h> |
Masahiro Yamada | 625177d | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 15 | #include <fdtdec.h> |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 16 | |
Masahiro Yamada | 7f36855 | 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 | d0c47b3 | 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 | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 34 | }; |
| 35 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 36 | struct uniphier_serial_private_data { |
| 37 | struct uniphier_serial __iomem *membase; |
| 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 | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 45 | struct uniphier_serial_platform_data *plat = dev_get_platdata(dev); |
| 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 | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 50 | divisor = DIV_ROUND_CLOSEST(plat->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 | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 91 | u32 tmp; |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 92 | struct uniphier_serial_private_data *priv = dev_get_priv(dev); |
| 93 | struct uniphier_serial_platform_data *plat = dev_get_platdata(dev); |
Masahiro Yamada | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 94 | struct uniphier_serial __iomem *port; |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 95 | |
Masahiro Yamada | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 96 | port = map_sysmem(plat->base, sizeof(struct uniphier_serial)); |
| 97 | if (!port) |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 98 | return -ENOMEM; |
| 99 | |
Masahiro Yamada | 099cf77 | 2015-02-27 02:26:47 +0900 | [diff] [blame] | 100 | priv->membase = port; |
| 101 | |
| 102 | tmp = readl(&port->lcr_mcr); |
| 103 | tmp &= ~LCR_MASK; |
| 104 | tmp |= UART_LCR_WLEN8 << LCR_SHIFT; |
| 105 | writel(tmp, &port->lcr_mcr); |
| 106 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Masahiro Yamada | d9bc8fd | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 110 | static int uniphier_serial_remove(struct udevice *dev) |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 111 | { |
| 112 | unmap_sysmem(uniphier_serial_port(dev)); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | #ifdef CONFIG_OF_CONTROL |
Masahiro Yamada | 625177d | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 118 | static const struct udevice_id uniphier_uart_of_match[] = { |
Masahiro Yamada | 6462cde | 2015-03-11 15:54:46 +0900 | [diff] [blame] | 119 | { .compatible = "socionext,uniphier-uart" }, |
| 120 | { /* sentinel */ } |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 121 | }; |
| 122 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 123 | static int uniphier_serial_ofdata_to_platdata(struct udevice *dev) |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 124 | { |
Masahiro Yamada | 625177d | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 125 | struct uniphier_serial_platform_data *plat = dev_get_platdata(dev); |
| 126 | DECLARE_GLOBAL_DATA_PTR; |
| 127 | |
| 128 | plat->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); |
| 129 | plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 130 | "clock-frequency", 0); |
| 131 | |
| 132 | return 0; |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 133 | } |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 134 | #endif |
Masahiro Yamada | 7f36855 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 135 | |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 136 | static const struct dm_serial_ops uniphier_serial_ops = { |
| 137 | .setbrg = uniphier_serial_setbrg, |
| 138 | .getc = uniphier_serial_getc, |
| 139 | .putc = uniphier_serial_putc, |
Masahiro Yamada | bb72148 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 140 | .pending = uniphier_serial_pending, |
Masahiro Yamada | d064cbf | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | U_BOOT_DRIVER(uniphier_serial) = { |
| 144 | .name = DRIVER_NAME, |
| 145 | .id = UCLASS_SERIAL, |
| 146 | .of_match = of_match_ptr(uniphier_uart_of_match), |
| 147 | .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata), |
| 148 | .probe = uniphier_serial_probe, |
| 149 | .remove = uniphier_serial_remove, |
| 150 | .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data), |
| 151 | .platdata_auto_alloc_size = |
| 152 | sizeof(struct uniphier_serial_platform_data), |
| 153 | .ops = &uniphier_serial_ops, |
| 154 | .flags = DM_FLAG_PRE_RELOC, |
| 155 | }; |