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> |
Angelo Dureghello | 461ea07 | 2019-03-13 21:46:49 +0100 | [diff] [blame] | 8 | * |
| 9 | * Modified to add DM and fdt support, removed non DM code |
| 10 | * (C) Copyright 2018 Angelo Dureghello <angelo@sysam.it> |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Minimal serial functions needed to use one of the uart ports |
| 15 | * as serial console interface. |
| 16 | */ |
| 17 | |
| 18 | #include <common.h> |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 19 | #include <dm.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 20 | #include <asm/global_data.h> |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 21 | #include <dm/platform_data/serial_coldfire.h> |
Alison Wang | 39c7a26 | 2012-10-18 16:54:38 +0000 | [diff] [blame] | 22 | #include <serial.h> |
| 23 | #include <linux/compiler.h> |
TsiChungLiew | 2bd806f | 2007-07-05 23:17:36 -0500 | [diff] [blame] | 24 | #include <asm/immap.h> |
| 25 | #include <asm/uart.h> |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
TsiChung Liew | fa9da59 | 2010-03-09 19:24:43 -0600 | [diff] [blame] | 29 | extern void uart_port_conf(int port); |
TsiChungLiew | 8d1d66a | 2007-08-05 03:55:21 -0500 | [diff] [blame] | 30 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 31 | 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] | 32 | { |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 33 | u32 counter; |
| 34 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 35 | uart_port_conf(port_idx); |
TsiChungLiew | 8d1d66a | 2007-08-05 03:55:21 -0500 | [diff] [blame] | 36 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 37 | /* write to SICR: SIM2 = uart mode,dcd does not affect rx */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 38 | writeb(UART_UCR_RESET_RX, &uart->ucr); |
| 39 | writeb(UART_UCR_RESET_TX, &uart->ucr); |
| 40 | writeb(UART_UCR_RESET_ERROR, &uart->ucr); |
| 41 | writeb(UART_UCR_RESET_MR, &uart->ucr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 42 | __asm__("nop"); |
| 43 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 44 | writeb(0, &uart->uimr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 45 | |
| 46 | /* write to CSR: RX/TX baud rate from timers */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 47 | 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] | 48 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 49 | writeb(UART_UMR_BC_8 | UART_UMR_PM_NONE, &uart->umr); |
| 50 | writeb(UART_UMR_SB_STOP_BITS_1, &uart->umr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 51 | |
| 52 | /* Setting up BaudRate */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 53 | counter = (u32) ((gd->bus_clk / 32) + (baudrate / 2)); |
| 54 | counter = counter / baudrate; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 55 | |
| 56 | /* write to CTUR: divide counter upper byte */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 57 | writeb((u8)((counter & 0xff00) >> 8), &uart->ubg1); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 58 | /* write to CTLR: divide counter lower byte */ |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 59 | writeb((u8)(counter & 0x00ff), &uart->ubg2); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 60 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 61 | writeb(UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED, &uart->ucr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 62 | |
| 63 | return (0); |
| 64 | } |
| 65 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 66 | static void mcf_serial_setbrg_common(uart_t *uart, int baudrate) |
| 67 | { |
| 68 | u32 counter; |
| 69 | |
| 70 | /* Setting up BaudRate */ |
| 71 | counter = (u32) ((gd->bus_clk / 32) + (baudrate / 2)); |
| 72 | counter = counter / baudrate; |
| 73 | |
| 74 | /* write to CTUR: divide counter upper byte */ |
| 75 | writeb(((counter & 0xff00) >> 8), &uart->ubg1); |
| 76 | /* write to CTLR: divide counter lower byte */ |
| 77 | writeb((counter & 0x00ff), &uart->ubg2); |
| 78 | |
| 79 | writeb(UART_UCR_RESET_RX, &uart->ucr); |
| 80 | writeb(UART_UCR_RESET_TX, &uart->ucr); |
| 81 | |
| 82 | writeb(UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED, &uart->ucr); |
| 83 | } |
| 84 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 85 | static int coldfire_serial_probe(struct udevice *dev) |
| 86 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 87 | struct coldfire_serial_plat *plat = dev_get_plat(dev); |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 88 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 89 | plat->port = dev_seq(dev); |
Angelo Durgehello | ce5e3ea | 2020-02-29 01:01:32 +0100 | [diff] [blame] | 90 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 91 | return mcf_serial_init_common((uart_t *)plat->base, |
| 92 | plat->port, plat->baudrate); |
| 93 | } |
| 94 | |
| 95 | static int coldfire_serial_putc(struct udevice *dev, const char ch) |
| 96 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 97 | struct coldfire_serial_plat *plat = dev_get_plat(dev); |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 98 | uart_t *uart = (uart_t *)plat->base; |
| 99 | |
| 100 | /* Wait for last character to go. */ |
| 101 | if (!(readb(&uart->usr) & UART_USR_TXRDY)) |
| 102 | return -EAGAIN; |
| 103 | |
| 104 | writeb(ch, &uart->utb); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int coldfire_serial_getc(struct udevice *dev) |
| 110 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 111 | struct coldfire_serial_plat *plat = dev_get_plat(dev); |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 112 | uart_t *uart = (uart_t *)(plat->base); |
| 113 | |
| 114 | /* Wait for a character to arrive. */ |
| 115 | if (!(readb(&uart->usr) & UART_USR_RXRDY)) |
| 116 | return -EAGAIN; |
| 117 | |
| 118 | return readb(&uart->urb); |
| 119 | } |
| 120 | |
| 121 | int coldfire_serial_setbrg(struct udevice *dev, int baudrate) |
| 122 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 123 | struct coldfire_serial_plat *plat = dev_get_plat(dev); |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 124 | uart_t *uart = (uart_t *)(plat->base); |
| 125 | |
| 126 | mcf_serial_setbrg_common(uart, baudrate); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int coldfire_serial_pending(struct udevice *dev, bool input) |
| 132 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 133 | struct coldfire_serial_plat *plat = dev_get_plat(dev); |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 134 | uart_t *uart = (uart_t *)(plat->base); |
| 135 | |
| 136 | if (input) |
| 137 | return readb(&uart->usr) & UART_USR_RXRDY ? 1 : 0; |
| 138 | else |
| 139 | return readb(&uart->usr) & UART_USR_TXRDY ? 0 : 1; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 144 | static int coldfire_of_to_plat(struct udevice *dev) |
Angelo Dureghello | 461ea07 | 2019-03-13 21:46:49 +0100 | [diff] [blame] | 145 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 146 | struct coldfire_serial_plat *plat = dev_get_plat(dev); |
Angelo Dureghello | 461ea07 | 2019-03-13 21:46:49 +0100 | [diff] [blame] | 147 | fdt_addr_t addr_base; |
| 148 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 149 | addr_base = dev_read_addr(dev); |
Angelo Dureghello | 461ea07 | 2019-03-13 21:46:49 +0100 | [diff] [blame] | 150 | if (addr_base == FDT_ADDR_T_NONE) |
| 151 | return -ENODEV; |
| 152 | |
| 153 | plat->base = (uint32_t)addr_base; |
Angelo Dureghello | 461ea07 | 2019-03-13 21:46:49 +0100 | [diff] [blame] | 154 | plat->baudrate = gd->baudrate; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 159 | static const struct dm_serial_ops coldfire_serial_ops = { |
| 160 | .putc = coldfire_serial_putc, |
| 161 | .pending = coldfire_serial_pending, |
| 162 | .getc = coldfire_serial_getc, |
| 163 | .setbrg = coldfire_serial_setbrg, |
| 164 | }; |
| 165 | |
Angelo Dureghello | 461ea07 | 2019-03-13 21:46:49 +0100 | [diff] [blame] | 166 | static const struct udevice_id coldfire_serial_ids[] = { |
| 167 | { .compatible = "fsl,mcf-uart" }, |
| 168 | { } |
| 169 | }; |
| 170 | |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 171 | U_BOOT_DRIVER(serial_coldfire) = { |
| 172 | .name = "serial_coldfire", |
| 173 | .id = UCLASS_SERIAL, |
Angelo Dureghello | 461ea07 | 2019-03-13 21:46:49 +0100 | [diff] [blame] | 174 | .of_match = coldfire_serial_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 175 | .of_to_plat = coldfire_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 176 | .plat_auto = sizeof(struct coldfire_serial_plat), |
angelo@sysam.it | e27802a | 2016-04-27 21:51:13 +0200 | [diff] [blame] | 177 | .probe = coldfire_serial_probe, |
| 178 | .ops = &coldfire_serial_ops, |
| 179 | .flags = DM_FLAG_PRE_RELOC, |
| 180 | }; |