Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 1 | /* |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 2 | * Copyright (C) 2011-2015 Vladimir Zapolskiy <vz@mleia.com> |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 8 | #include <dm.h> |
Marek Vasut | e503f90 | 2012-09-13 16:51:02 +0200 | [diff] [blame] | 9 | #include <serial.h> |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 10 | #include <dm/platform_data/lpc32xx_hsuart.h> |
| 11 | |
| 12 | #include <asm/arch/uart.h> |
Marek Vasut | e503f90 | 2012-09-13 16:51:02 +0200 | [diff] [blame] | 13 | #include <linux/compiler.h> |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 17 | struct lpc32xx_hsuart_priv { |
| 18 | struct hsuart_regs *hsuart; |
| 19 | }; |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 20 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 21 | static int lpc32xx_serial_setbrg(struct udevice *dev, int baudrate) |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 22 | { |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 23 | struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev); |
| 24 | struct hsuart_regs *hsuart = priv->hsuart; |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 25 | u32 div; |
| 26 | |
| 27 | /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */ |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 28 | div = (get_serial_clock() / 14 + baudrate / 2) / baudrate - 1; |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 29 | if (div > 255) |
| 30 | div = 255; |
| 31 | |
| 32 | writel(div, &hsuart->rate); |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 33 | |
| 34 | return 0; |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 37 | static int lpc32xx_serial_getc(struct udevice *dev) |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 38 | { |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 39 | struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev); |
| 40 | struct hsuart_regs *hsuart = priv->hsuart; |
| 41 | |
| 42 | if (!(readl(&hsuart->level) & HSUART_LEVEL_RX)) |
| 43 | return -EAGAIN; |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 44 | |
| 45 | return readl(&hsuart->rx) & HSUART_RX_DATA; |
| 46 | } |
| 47 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 48 | static int lpc32xx_serial_putc(struct udevice *dev, const char c) |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 49 | { |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 50 | struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev); |
| 51 | struct hsuart_regs *hsuart = priv->hsuart; |
| 52 | |
| 53 | /* Wait for empty FIFO */ |
| 54 | if (readl(&hsuart->level) & HSUART_LEVEL_TX) |
| 55 | return -EAGAIN; |
Vladimir Zapolskiy | 5deccaf | 2013-11-30 16:47:01 +0200 | [diff] [blame] | 56 | |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 57 | writel(c, &hsuart->tx); |
| 58 | |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 62 | static int lpc32xx_serial_pending(struct udevice *dev, bool input) |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 63 | { |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 64 | struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev); |
| 65 | struct hsuart_regs *hsuart = priv->hsuart; |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 66 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 67 | if (input) { |
| 68 | if (readl(&hsuart->level) & HSUART_LEVEL_RX) |
| 69 | return 1; |
| 70 | } else { |
| 71 | if (readl(&hsuart->level) & HSUART_LEVEL_TX) |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int lpc32xx_serial_init(struct hsuart_regs *hsuart) |
| 79 | { |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 80 | /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */ |
| 81 | writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) | |
| 82 | HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0, |
| 83 | &hsuart->ctrl); |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 84 | |
Marek Vasut | e503f90 | 2012-09-13 16:51:02 +0200 | [diff] [blame] | 85 | return 0; |
Vladimir Zapolskiy | cc35fdb | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 88 | static int lpc32xx_hsuart_probe(struct udevice *dev) |
| 89 | { |
| 90 | struct lpc32xx_hsuart_platdata *platdata = dev_get_platdata(dev); |
| 91 | struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev); |
| 92 | |
| 93 | priv->hsuart = (struct hsuart_regs *)platdata->base; |
| 94 | |
| 95 | lpc32xx_serial_init(priv->hsuart); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static const struct dm_serial_ops lpc32xx_hsuart_ops = { |
Marek Vasut | e503f90 | 2012-09-13 16:51:02 +0200 | [diff] [blame] | 101 | .setbrg = lpc32xx_serial_setbrg, |
Marek Vasut | e503f90 | 2012-09-13 16:51:02 +0200 | [diff] [blame] | 102 | .getc = lpc32xx_serial_getc, |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 103 | .putc = lpc32xx_serial_putc, |
| 104 | .pending = lpc32xx_serial_pending, |
Marek Vasut | e503f90 | 2012-09-13 16:51:02 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
Vladimir Zapolskiy | f21069e | 2015-12-19 23:29:24 +0200 | [diff] [blame^] | 107 | U_BOOT_DRIVER(lpc32xx_hsuart) = { |
| 108 | .name = "lpc32xx_hsuart", |
| 109 | .id = UCLASS_SERIAL, |
| 110 | .probe = lpc32xx_hsuart_probe, |
| 111 | .ops = &lpc32xx_hsuart_ops, |
| 112 | .priv_auto_alloc_size = sizeof(struct lpc32xx_hsuart_priv), |
| 113 | .flags = DM_FLAG_PRE_RELOC, |
| 114 | }; |