TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 1 | /* |
TsiChungLiew | 36d5b1a | 2007-07-05 23:17:36 -0500 | [diff] [blame] | 2 | * (C) Copyright 2004-2007 Freescale Semiconductor, Inc. |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 3 | * TsiChung Liew, Tsi-Chung.Liew@freescale.com. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Minimal serial functions needed to use one of the uart ports |
| 10 | * as serial console interface. |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Alison Wang | 234151a | 2012-10-18 16:54:38 +0000 | [diff] [blame] | 14 | #include <serial.h> |
| 15 | #include <linux/compiler.h> |
Wolfgang Denk | c30709a | 2007-08-19 10:27:34 +0200 | [diff] [blame] | 16 | |
TsiChungLiew | 36d5b1a | 2007-07-05 23:17:36 -0500 | [diff] [blame] | 17 | #include <asm/immap.h> |
| 18 | #include <asm/uart.h> |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
TsiChung Liew | b0c37e5 | 2010-03-09 19:24:43 -0600 | [diff] [blame] | 22 | extern void uart_port_conf(int port); |
TsiChungLiew | 74634c8 | 2007-08-05 03:55:21 -0500 | [diff] [blame] | 23 | |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 24 | static int mcf_serial_init(void) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 25 | { |
| 26 | volatile uart_t *uart; |
| 27 | u32 counter; |
| 28 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 29 | uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 30 | |
TsiChung Liew | b0c37e5 | 2010-03-09 19:24:43 -0600 | [diff] [blame] | 31 | uart_port_conf(CONFIG_SYS_UART_PORT); |
TsiChungLiew | 74634c8 | 2007-08-05 03:55:21 -0500 | [diff] [blame] | 32 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 33 | /* write to SICR: SIM2 = uart mode,dcd does not affect rx */ |
| 34 | uart->ucr = UART_UCR_RESET_RX; |
| 35 | uart->ucr = UART_UCR_RESET_TX; |
| 36 | uart->ucr = UART_UCR_RESET_ERROR; |
| 37 | uart->ucr = UART_UCR_RESET_MR; |
| 38 | __asm__("nop"); |
| 39 | |
| 40 | uart->uimr = 0; |
| 41 | |
| 42 | /* write to CSR: RX/TX baud rate from timers */ |
| 43 | uart->ucsr = (UART_UCSR_RCS_SYS_CLK | UART_UCSR_TCS_SYS_CLK); |
| 44 | |
| 45 | uart->umr = (UART_UMR_BC_8 | UART_UMR_PM_NONE); |
| 46 | uart->umr = UART_UMR_SB_STOP_BITS_1; |
| 47 | |
| 48 | /* Setting up BaudRate */ |
TsiChung Liew | d6a4da0 | 2008-05-29 12:21:54 -0500 | [diff] [blame] | 49 | counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2)); |
| 50 | counter = counter / gd->baudrate; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 51 | |
| 52 | /* write to CTUR: divide counter upper byte */ |
| 53 | uart->ubg1 = (u8) ((counter & 0xff00) >> 8); |
| 54 | /* write to CTLR: divide counter lower byte */ |
| 55 | uart->ubg2 = (u8) (counter & 0x00ff); |
| 56 | |
| 57 | uart->ucr = (UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED); |
| 58 | |
| 59 | return (0); |
| 60 | } |
| 61 | |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 62 | static void mcf_serial_putc(const char c) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 63 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 64 | volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 65 | |
| 66 | if (c == '\n') |
| 67 | serial_putc('\r'); |
| 68 | |
| 69 | /* Wait for last character to go. */ |
| 70 | while (!(uart->usr & UART_USR_TXRDY)) ; |
| 71 | |
| 72 | uart->utb = c; |
| 73 | } |
| 74 | |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 75 | static int mcf_serial_getc(void) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 76 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 77 | volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 78 | |
| 79 | /* Wait for a character to arrive. */ |
| 80 | while (!(uart->usr & UART_USR_RXRDY)) ; |
| 81 | return uart->urb; |
| 82 | } |
| 83 | |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 84 | static int mcf_serial_tstc(void) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 85 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 87 | |
| 88 | return (uart->usr & UART_USR_RXRDY); |
| 89 | } |
| 90 | |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 91 | static void mcf_serial_setbrg(void) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 92 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 93 | volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 94 | u32 counter; |
| 95 | |
Richard Retanubun | 209e53d | 2009-01-23 11:44:30 -0500 | [diff] [blame] | 96 | /* Setting up BaudRate */ |
| 97 | counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2)); |
| 98 | counter = counter / gd->baudrate; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 99 | |
| 100 | /* write to CTUR: divide counter upper byte */ |
| 101 | uart->ubg1 = ((counter & 0xff00) >> 8); |
| 102 | /* write to CTLR: divide counter lower byte */ |
| 103 | uart->ubg2 = (counter & 0x00ff); |
| 104 | |
| 105 | uart->ucr = UART_UCR_RESET_RX; |
| 106 | uart->ucr = UART_UCR_RESET_TX; |
| 107 | |
| 108 | uart->ucr = UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED; |
| 109 | } |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 110 | |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 111 | static struct serial_device mcf_serial_drv = { |
| 112 | .name = "mcf_serial", |
| 113 | .start = mcf_serial_init, |
| 114 | .stop = NULL, |
| 115 | .setbrg = mcf_serial_setbrg, |
| 116 | .putc = mcf_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 117 | .puts = default_serial_puts, |
Marek Vasut | 96e8ff6 | 2012-09-13 16:51:38 +0200 | [diff] [blame] | 118 | .getc = mcf_serial_getc, |
| 119 | .tstc = mcf_serial_tstc, |
| 120 | }; |
| 121 | |
| 122 | void mcf_serial_initialize(void) |
| 123 | { |
| 124 | serial_register(&mcf_serial_drv); |
| 125 | } |
| 126 | |
| 127 | __weak struct serial_device *default_serial_console(void) |
| 128 | { |
| 129 | return &mcf_serial_drv; |
| 130 | } |