Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 2 | /* |
TsiChungLiew | 2bd806f | 2007-07-05 23:17:36 -0500 | [diff] [blame] | 3 | * (C) Copyright 2004-2007 Freescale Semiconductor, Inc. |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 4 | * TsiChung Liew, Tsi-Chung.Liew@freescale.com. |
| 5 | * |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 6 | * Modified to add device model (DM) support |
| 7 | * (C) Copyright 2015 Angelo Dureghello <angelo@sysam.it> |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Minimal serial functions needed to use one of the uart ports |
| 12 | * as serial console interface. |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 16 | #include <dm.h> |
| 17 | #include <dm/platform_data/serial_coldfire.h> |
Alison Wang | 39c7a26 | 2012-10-18 16:54:38 +0000 | [diff] [blame] | 18 | #include <serial.h> |
| 19 | #include <linux/compiler.h> |
TsiChungLiew | 2bd806f | 2007-07-05 23:17:36 -0500 | [diff] [blame] | 20 | #include <asm/immap.h> |
| 21 | #include <asm/uart.h> |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
TsiChung Liew | fa9da59 | 2010-03-09 19:24:43 -0600 | [diff] [blame] | 25 | extern void uart_port_conf(int port); |
TsiChungLiew | 8d1d66a | 2007-08-05 03:55:21 -0500 | [diff] [blame] | 26 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 27 | static int mcf_serial_init_common(uart_t *uart, int port_idx, int baudrate) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 28 | { |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 29 | u32 counter; |
| 30 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 31 | uart_port_conf(port_idx); |
TsiChungLiew | 8d1d66a | 2007-08-05 03:55:21 -0500 | [diff] [blame] | 32 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 33 | /* write to SICR: SIM2 = uart mode,dcd does not affect rx */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 34 | writeb(UART_UCR_RESET_RX, &uart->ucr); |
| 35 | writeb(UART_UCR_RESET_TX, &uart->ucr); |
| 36 | writeb(UART_UCR_RESET_ERROR, &uart->ucr); |
| 37 | writeb(UART_UCR_RESET_MR, &uart->ucr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 38 | __asm__("nop"); |
| 39 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 40 | writeb(0, &uart->uimr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 41 | |
| 42 | /* write to CSR: RX/TX baud rate from timers */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 43 | writeb(UART_UCSR_RCS_SYS_CLK | UART_UCSR_TCS_SYS_CLK, &uart->ucsr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 44 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 45 | writeb(UART_UMR_BC_8 | UART_UMR_PM_NONE, &uart->umr); |
| 46 | writeb(UART_UMR_SB_STOP_BITS_1, &uart->umr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 47 | |
| 48 | /* Setting up BaudRate */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 49 | counter = (u32) ((gd->bus_clk / 32) + (baudrate / 2)); |
| 50 | counter = counter / baudrate; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 51 | |
| 52 | /* write to CTUR: divide counter upper byte */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 53 | writeb((u8)((counter & 0xff00) >> 8), &uart->ubg1); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 54 | /* write to CTLR: divide counter lower byte */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 55 | writeb((u8)(counter & 0x00ff), &uart->ubg2); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 56 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 57 | writeb(UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED, &uart->ucr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 58 | |
| 59 | return (0); |
| 60 | } |
| 61 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 62 | static void mcf_serial_setbrg_common(uart_t *uart, int baudrate) |
| 63 | { |
| 64 | u32 counter; |
| 65 | |
| 66 | /* Setting up BaudRate */ |
| 67 | counter = (u32) ((gd->bus_clk / 32) + (baudrate / 2)); |
| 68 | counter = counter / baudrate; |
| 69 | |
| 70 | /* write to CTUR: divide counter upper byte */ |
| 71 | writeb(((counter & 0xff00) >> 8), &uart->ubg1); |
| 72 | /* write to CTLR: divide counter lower byte */ |
| 73 | writeb((counter & 0x00ff), &uart->ubg2); |
| 74 | |
| 75 | writeb(UART_UCR_RESET_RX, &uart->ucr); |
| 76 | writeb(UART_UCR_RESET_TX, &uart->ucr); |
| 77 | |
| 78 | writeb(UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED, &uart->ucr); |
| 79 | } |
| 80 | |
| 81 | #ifndef CONFIG_DM_SERIAL |
| 82 | |
| 83 | static int mcf_serial_init(void) |
| 84 | { |
| 85 | uart_t *uart_base; |
| 86 | int port_idx; |
| 87 | |
| 88 | uart_base = (uart_t *)CONFIG_SYS_UART_BASE; |
| 89 | port_idx = CONFIG_SYS_UART_PORT; |
| 90 | |
| 91 | return mcf_serial_init_common(uart_base, port_idx, gd->baudrate); |
| 92 | } |
| 93 | |
Marek Vasut | abaef69 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 94 | static void mcf_serial_putc(const char c) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 95 | { |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 96 | uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 97 | |
| 98 | if (c == '\n') |
| 99 | serial_putc('\r'); |
| 100 | |
| 101 | /* Wait for last character to go. */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 102 | while (!(readb(&uart->usr) & UART_USR_TXRDY)) |
| 103 | ; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 104 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 105 | writeb(c, &uart->utb); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Marek Vasut | abaef69 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 108 | static int mcf_serial_getc(void) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 109 | { |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 110 | uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 111 | |
| 112 | /* Wait for a character to arrive. */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 113 | while (!(readb(&uart->usr) & UART_USR_RXRDY)) |
| 114 | ; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 115 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 116 | return readb(&uart->urb); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Marek Vasut | abaef69 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 119 | static void mcf_serial_setbrg(void) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 120 | { |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 121 | uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 122 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 123 | mcf_serial_setbrg_common(uart, gd->baudrate); |
| 124 | } |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 125 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 126 | static int mcf_serial_tstc(void) |
| 127 | { |
| 128 | uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 129 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 130 | return readb(&uart->usr) & UART_USR_RXRDY; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 131 | } |
Marek Vasut | abaef69 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 132 | |
Marek Vasut | abaef69 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 133 | static struct serial_device mcf_serial_drv = { |
| 134 | .name = "mcf_serial", |
| 135 | .start = mcf_serial_init, |
| 136 | .stop = NULL, |
| 137 | .setbrg = mcf_serial_setbrg, |
| 138 | .putc = mcf_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 139 | .puts = default_serial_puts, |
Marek Vasut | abaef69 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 140 | .getc = mcf_serial_getc, |
| 141 | .tstc = mcf_serial_tstc, |
| 142 | }; |
| 143 | |
| 144 | void mcf_serial_initialize(void) |
| 145 | { |
| 146 | serial_register(&mcf_serial_drv); |
| 147 | } |
| 148 | |
| 149 | __weak struct serial_device *default_serial_console(void) |
| 150 | { |
| 151 | return &mcf_serial_drv; |
| 152 | } |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 153 | |
| 154 | #endif |
| 155 | |
| 156 | #ifdef CONFIG_DM_SERIAL |
| 157 | |
| 158 | static int coldfire_serial_probe(struct udevice *dev) |
| 159 | { |
| 160 | struct coldfire_serial_platdata *plat = dev->platdata; |
| 161 | |
| 162 | return mcf_serial_init_common((uart_t *)plat->base, |
| 163 | plat->port, plat->baudrate); |
| 164 | } |
| 165 | |
| 166 | static int coldfire_serial_putc(struct udevice *dev, const char ch) |
| 167 | { |
| 168 | struct coldfire_serial_platdata *plat = dev->platdata; |
| 169 | uart_t *uart = (uart_t *)plat->base; |
| 170 | |
| 171 | /* Wait for last character to go. */ |
| 172 | if (!(readb(&uart->usr) & UART_USR_TXRDY)) |
| 173 | return -EAGAIN; |
| 174 | |
| 175 | writeb(ch, &uart->utb); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int coldfire_serial_getc(struct udevice *dev) |
| 181 | { |
| 182 | struct coldfire_serial_platdata *plat = dev->platdata; |
| 183 | uart_t *uart = (uart_t *)(plat->base); |
| 184 | |
| 185 | /* Wait for a character to arrive. */ |
| 186 | if (!(readb(&uart->usr) & UART_USR_RXRDY)) |
| 187 | return -EAGAIN; |
| 188 | |
| 189 | return readb(&uart->urb); |
| 190 | } |
| 191 | |
| 192 | int coldfire_serial_setbrg(struct udevice *dev, int baudrate) |
| 193 | { |
| 194 | struct coldfire_serial_platdata *plat = dev->platdata; |
| 195 | uart_t *uart = (uart_t *)(plat->base); |
| 196 | |
| 197 | mcf_serial_setbrg_common(uart, baudrate); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int coldfire_serial_pending(struct udevice *dev, bool input) |
| 203 | { |
| 204 | struct coldfire_serial_platdata *plat = dev->platdata; |
| 205 | uart_t *uart = (uart_t *)(plat->base); |
| 206 | |
| 207 | if (input) |
| 208 | return readb(&uart->usr) & UART_USR_RXRDY ? 1 : 0; |
| 209 | else |
| 210 | return readb(&uart->usr) & UART_USR_TXRDY ? 0 : 1; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static const struct dm_serial_ops coldfire_serial_ops = { |
| 216 | .putc = coldfire_serial_putc, |
| 217 | .pending = coldfire_serial_pending, |
| 218 | .getc = coldfire_serial_getc, |
| 219 | .setbrg = coldfire_serial_setbrg, |
| 220 | }; |
| 221 | |
| 222 | U_BOOT_DRIVER(serial_coldfire) = { |
| 223 | .name = "serial_coldfire", |
| 224 | .id = UCLASS_SERIAL, |
| 225 | .probe = coldfire_serial_probe, |
| 226 | .ops = &coldfire_serial_ops, |
| 227 | .flags = DM_FLAG_PRE_RELOC, |
| 228 | }; |
| 229 | #endif |