Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 17 | * MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <common.h> |
| 21 | #include <asm/arch/cpu.h> |
| 22 | #include <asm/arch/clk.h> |
| 23 | #include <asm/arch/uart.h> |
| 24 | #include <asm/io.h> |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 25 | #include <serial.h> |
| 26 | #include <linux/compiler.h> |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
| 30 | static struct hsuart_regs *hsuart = (struct hsuart_regs *)HS_UART_BASE; |
| 31 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 32 | static void lpc32xx_serial_setbrg(void) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 33 | { |
| 34 | u32 div; |
| 35 | |
| 36 | /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */ |
| 37 | div = (get_serial_clock() / 14 + gd->baudrate / 2) / gd->baudrate - 1; |
| 38 | if (div > 255) |
| 39 | div = 255; |
| 40 | |
| 41 | writel(div, &hsuart->rate); |
| 42 | } |
| 43 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 44 | static int lpc32xx_serial_getc(void) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 45 | { |
| 46 | while (!(readl(&hsuart->level) & HSUART_LEVEL_RX)) |
| 47 | /* NOP */; |
| 48 | |
| 49 | return readl(&hsuart->rx) & HSUART_RX_DATA; |
| 50 | } |
| 51 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 52 | static void lpc32xx_serial_putc(const char c) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 53 | { |
| 54 | writel(c, &hsuart->tx); |
| 55 | |
| 56 | /* Wait for character to be sent */ |
| 57 | while (readl(&hsuart->level) & HSUART_LEVEL_TX) |
| 58 | /* NOP */; |
| 59 | } |
| 60 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 61 | static int lpc32xx_serial_tstc(void) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 62 | { |
| 63 | if (readl(&hsuart->level) & HSUART_LEVEL_RX) |
| 64 | return 1; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 69 | static int lpc32xx_serial_init(void) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 70 | { |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 71 | lpc32xx_serial_setbrg(); |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 72 | |
| 73 | /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */ |
| 74 | writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) | |
| 75 | HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0, |
| 76 | &hsuart->ctrl); |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 77 | return 0; |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 80 | static void lpc32xx_serial_puts(const char *s) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 81 | { |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 82 | while (*s) |
| 83 | serial_putc(*s++); |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 86 | #ifdef CONFIG_SERIAL_MULTI |
| 87 | static struct serial_device lpc32xx_serial_drv = { |
| 88 | .name = "lpc32xx_serial", |
| 89 | .start = lpc32xx_serial_init, |
| 90 | .stop = NULL, |
| 91 | .setbrg = lpc32xx_serial_setbrg, |
| 92 | .putc = lpc32xx_serial_putc, |
| 93 | .puts = lpc32xx_serial_puts, |
| 94 | .getc = lpc32xx_serial_getc, |
| 95 | .tstc = lpc32xx_serial_tstc, |
| 96 | }; |
| 97 | |
| 98 | void lpc32xx_serial_initialize(void) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 99 | { |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 100 | serial_register(&lpc32xx_serial_drv); |
| 101 | } |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 102 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 103 | __weak struct serial_device *default_serial_console(void) |
| 104 | { |
| 105 | return &lpc32xx_serial_drv; |
| 106 | } |
| 107 | #else |
| 108 | int serial_init(void) |
| 109 | { |
| 110 | return lpc32xx_serial_init(); |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 113 | void serial_setbrg(void) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 114 | { |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 115 | lpc32xx_serial_setbrg(); |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 118 | void serial_putc(const char c) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 119 | { |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 120 | lpc32xx_serial_putc(c); |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 123 | void serial_puts(const char *s) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 124 | { |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 125 | lpc32xx_serial_puts(s); |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 128 | int serial_getc(void) |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 129 | { |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 130 | return lpc32xx_serial_getc(); |
| 131 | } |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 132 | |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 133 | int serial_tstc(void) |
| 134 | { |
| 135 | return lpc32xx_serial_tstc(); |
Vladimir Zapolskiy | 8597d07 | 2012-04-19 04:33:09 +0000 | [diff] [blame] | 136 | } |
Marek Vasut | a1207aa | 2012-09-13 16:51:02 +0200 | [diff] [blame^] | 137 | #endif |