Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 2 | * Copyright (C) 2012-2015 Panasonic Corporation |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 3 | * Author: Masahiro Yamada <yamada.m@jp.panasonic.com> |
| 4 | * |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
Masahiro Yamada | 9caae5e | 2014-10-30 12:11:14 +0900 | [diff] [blame] | 8 | #include <linux/serial_reg.h> |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <asm/errno.h> |
| 11 | #include <dm/device.h> |
| 12 | #include <dm/platform_data/serial-uniphier.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 13 | #include <serial.h> |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 14 | #include <fdtdec.h> |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 15 | |
Masahiro Yamada | b289073 | 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 | e7c3a89 | 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 | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 33 | }; |
| 34 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 35 | struct uniphier_serial_private_data { |
| 36 | struct uniphier_serial __iomem *membase; |
| 37 | }; |
| 38 | |
| 39 | #define uniphier_serial_port(dev) \ |
| 40 | ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 41 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 42 | static int uniphier_serial_setbrg(struct udevice *dev, int baudrate) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 43 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 44 | struct uniphier_serial_platform_data *plat = dev_get_platdata(dev); |
| 45 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 46 | const unsigned int mode_x_div = 16; |
| 47 | unsigned int divisor; |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 48 | u32 tmp; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 49 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 50 | tmp = readl(&port->lcr_mcr); |
| 51 | tmp &= ~LCR_MASK; |
| 52 | tmp |= UART_LCR_WLEN8 << LCR_SHIFT; |
| 53 | writel(tmp, &port->lcr_mcr); |
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 | divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 56 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 57 | writel(divisor, &port->dlr); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 58 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 59 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 62 | static int uniphier_serial_getc(struct udevice *dev) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 63 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 64 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
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 | if (!(readl(&port->lsr) & UART_LSR_DR)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 67 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 68 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 69 | return readl(&port->rx); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 72 | static int uniphier_serial_putc(struct udevice *dev, const char c) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 73 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 74 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
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 | if (!(readl(&port->lsr) & UART_LSR_THRE)) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 77 | return -EAGAIN; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 78 | |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 79 | writel(c, &port->tx); |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 80 | |
| 81 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 82 | } |
| 83 | |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 84 | static int uniphier_serial_pending(struct udevice *dev, bool input) |
| 85 | { |
| 86 | struct uniphier_serial __iomem *port = uniphier_serial_port(dev); |
| 87 | |
| 88 | if (input) |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 89 | return readl(&port->lsr) & UART_LSR_DR; |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 90 | else |
Masahiro Yamada | e7c3a89 | 2015-02-27 02:26:46 +0900 | [diff] [blame^] | 91 | return !(readl(&port->lsr) & UART_LSR_THRE); |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 92 | } |
| 93 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 94 | static int uniphier_serial_probe(struct udevice *dev) |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 95 | { |
| 96 | struct uniphier_serial_private_data *priv = dev_get_priv(dev); |
| 97 | struct uniphier_serial_platform_data *plat = dev_get_platdata(dev); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 98 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 99 | priv->membase = map_sysmem(plat->base, sizeof(struct uniphier_serial)); |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 100 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 101 | if (!priv->membase) |
| 102 | return -ENOMEM; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 103 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 104 | return 0; |
| 105 | } |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 106 | |
Masahiro Yamada | 0bf0c6d | 2014-10-24 17:00:11 +0900 | [diff] [blame] | 107 | static int uniphier_serial_remove(struct udevice *dev) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 108 | { |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 109 | unmap_sysmem(uniphier_serial_port(dev)); |
| 110 | |
| 111 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 114 | #ifdef CONFIG_OF_CONTROL |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 115 | static const struct udevice_id uniphier_uart_of_match[] = { |
| 116 | { .compatible = "panasonic,uniphier-uart" }, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 117 | {}, |
| 118 | }; |
| 119 | |
| 120 | static int uniphier_serial_ofdata_to_platdata(struct udevice *dev) |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 121 | { |
Masahiro Yamada | f8cd9b0 | 2014-11-26 18:34:00 +0900 | [diff] [blame] | 122 | struct uniphier_serial_platform_data *plat = dev_get_platdata(dev); |
| 123 | DECLARE_GLOBAL_DATA_PTR; |
| 124 | |
| 125 | plat->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); |
| 126 | plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 127 | "clock-frequency", 0); |
| 128 | |
| 129 | return 0; |
Masahiro Yamada | b289073 | 2014-10-03 19:21:05 +0900 | [diff] [blame] | 130 | } |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 131 | #endif |
| 132 | |
| 133 | static const struct dm_serial_ops uniphier_serial_ops = { |
| 134 | .setbrg = uniphier_serial_setbrg, |
| 135 | .getc = uniphier_serial_getc, |
| 136 | .putc = uniphier_serial_putc, |
Masahiro Yamada | d11fbd5 | 2014-10-24 17:00:10 +0900 | [diff] [blame] | 137 | .pending = uniphier_serial_pending, |
Masahiro Yamada | 50c1254 | 2014-10-23 22:26:10 +0900 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | U_BOOT_DRIVER(uniphier_serial) = { |
| 141 | .name = DRIVER_NAME, |
| 142 | .id = UCLASS_SERIAL, |
| 143 | .of_match = of_match_ptr(uniphier_uart_of_match), |
| 144 | .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata), |
| 145 | .probe = uniphier_serial_probe, |
| 146 | .remove = uniphier_serial_remove, |
| 147 | .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data), |
| 148 | .platdata_auto_alloc_size = |
| 149 | sizeof(struct uniphier_serial_platform_data), |
| 150 | .ops = &uniphier_serial_ops, |
| 151 | .flags = DM_FLAG_PRE_RELOC, |
| 152 | }; |