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