12bd806feSTsiChungLiew /* 22bd806feSTsiChungLiew * (C) Copyright 2004-2007 Freescale Semiconductor, Inc. 32bd806feSTsiChungLiew * TsiChung Liew, Tsi-Chung.Liew@freescale.com. 42bd806feSTsiChungLiew * 52bd806feSTsiChungLiew * See file CREDITS for list of people who contributed to this 62bd806feSTsiChungLiew * project. 72bd806feSTsiChungLiew * 82bd806feSTsiChungLiew * This program is free software; you can redistribute it and/or 92bd806feSTsiChungLiew * modify it under the terms of the GNU General Public License as 102bd806feSTsiChungLiew * published by the Free Software Foundation; either version 2 of 112bd806feSTsiChungLiew * the License, or (at your option) any later version. 122bd806feSTsiChungLiew * 132bd806feSTsiChungLiew * This program is distributed in the hope that it will be useful, 142bd806feSTsiChungLiew * but WITHOUT ANY WARRANTY; without even the implied warranty of 152bd806feSTsiChungLiew * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 162bd806feSTsiChungLiew * GNU General Public License for more details. 172bd806feSTsiChungLiew * 182bd806feSTsiChungLiew * You should have received a copy of the GNU General Public License 192bd806feSTsiChungLiew * along with this program; if not, write to the Free Software 202bd806feSTsiChungLiew * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 212bd806feSTsiChungLiew * MA 02111-1307 USA 222bd806feSTsiChungLiew * 232bd806feSTsiChungLiew */ 242bd806feSTsiChungLiew 252bd806feSTsiChungLiew /* 262bd806feSTsiChungLiew * Minimal serial functions needed to use one of the uart ports 272bd806feSTsiChungLiew * as serial console interface. 282bd806feSTsiChungLiew */ 292bd806feSTsiChungLiew 302bd806feSTsiChungLiew #include <common.h> 313e66c078SWolfgang Denk 322bd806feSTsiChungLiew #include <asm/immap.h> 332bd806feSTsiChungLiew #include <asm/uart.h> 342bd806feSTsiChungLiew 352bd806feSTsiChungLiew DECLARE_GLOBAL_DATA_PTR; 362bd806feSTsiChungLiew 37fa9da596STsiChung Liew extern void uart_port_conf(int port); 388d1d66afSTsiChungLiew 39abaef69fSMarek Vasut static int mcf_serial_init(void) 402bd806feSTsiChungLiew { 412bd806feSTsiChungLiew volatile uart_t *uart; 422bd806feSTsiChungLiew u32 counter; 432bd806feSTsiChungLiew 446d0f6bcfSJean-Christophe PLAGNIOL-VILLARD uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 452bd806feSTsiChungLiew 46fa9da596STsiChung Liew uart_port_conf(CONFIG_SYS_UART_PORT); 478d1d66afSTsiChungLiew 482bd806feSTsiChungLiew /* write to SICR: SIM2 = uart mode,dcd does not affect rx */ 492bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_RX; 502bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_TX; 512bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_ERROR; 522bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_MR; 532bd806feSTsiChungLiew __asm__("nop"); 542bd806feSTsiChungLiew 552bd806feSTsiChungLiew uart->uimr = 0; 562bd806feSTsiChungLiew 572bd806feSTsiChungLiew /* write to CSR: RX/TX baud rate from timers */ 582bd806feSTsiChungLiew uart->ucsr = (UART_UCSR_RCS_SYS_CLK | UART_UCSR_TCS_SYS_CLK); 592bd806feSTsiChungLiew 602bd806feSTsiChungLiew uart->umr = (UART_UMR_BC_8 | UART_UMR_PM_NONE); 612bd806feSTsiChungLiew uart->umr = UART_UMR_SB_STOP_BITS_1; 622bd806feSTsiChungLiew 632bd806feSTsiChungLiew /* Setting up BaudRate */ 6481cc3232STsiChung Liew counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2)); 6581cc3232STsiChung Liew counter = counter / gd->baudrate; 662bd806feSTsiChungLiew 672bd806feSTsiChungLiew /* write to CTUR: divide counter upper byte */ 682bd806feSTsiChungLiew uart->ubg1 = (u8) ((counter & 0xff00) >> 8); 692bd806feSTsiChungLiew /* write to CTLR: divide counter lower byte */ 702bd806feSTsiChungLiew uart->ubg2 = (u8) (counter & 0x00ff); 712bd806feSTsiChungLiew 722bd806feSTsiChungLiew uart->ucr = (UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED); 732bd806feSTsiChungLiew 742bd806feSTsiChungLiew return (0); 752bd806feSTsiChungLiew } 762bd806feSTsiChungLiew 77abaef69fSMarek Vasut static void mcf_serial_putc(const char c) 782bd806feSTsiChungLiew { 796d0f6bcfSJean-Christophe PLAGNIOL-VILLARD volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 802bd806feSTsiChungLiew 812bd806feSTsiChungLiew if (c == '\n') 822bd806feSTsiChungLiew serial_putc('\r'); 832bd806feSTsiChungLiew 842bd806feSTsiChungLiew /* Wait for last character to go. */ 852bd806feSTsiChungLiew while (!(uart->usr & UART_USR_TXRDY)) ; 862bd806feSTsiChungLiew 872bd806feSTsiChungLiew uart->utb = c; 882bd806feSTsiChungLiew } 892bd806feSTsiChungLiew 90abaef69fSMarek Vasut static int mcf_serial_getc(void) 912bd806feSTsiChungLiew { 926d0f6bcfSJean-Christophe PLAGNIOL-VILLARD volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 932bd806feSTsiChungLiew 942bd806feSTsiChungLiew /* Wait for a character to arrive. */ 952bd806feSTsiChungLiew while (!(uart->usr & UART_USR_RXRDY)) ; 962bd806feSTsiChungLiew return uart->urb; 972bd806feSTsiChungLiew } 982bd806feSTsiChungLiew 99abaef69fSMarek Vasut static int mcf_serial_tstc(void) 1002bd806feSTsiChungLiew { 1016d0f6bcfSJean-Christophe PLAGNIOL-VILLARD volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 1022bd806feSTsiChungLiew 1032bd806feSTsiChungLiew return (uart->usr & UART_USR_RXRDY); 1042bd806feSTsiChungLiew } 1052bd806feSTsiChungLiew 106abaef69fSMarek Vasut static void mcf_serial_setbrg(void) 1072bd806feSTsiChungLiew { 1086d0f6bcfSJean-Christophe PLAGNIOL-VILLARD volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 1092bd806feSTsiChungLiew u32 counter; 1102bd806feSTsiChungLiew 11192d3e6e0SRichard Retanubun /* Setting up BaudRate */ 11292d3e6e0SRichard Retanubun counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2)); 11392d3e6e0SRichard Retanubun counter = counter / gd->baudrate; 1142bd806feSTsiChungLiew 1152bd806feSTsiChungLiew /* write to CTUR: divide counter upper byte */ 1162bd806feSTsiChungLiew uart->ubg1 = ((counter & 0xff00) >> 8); 1172bd806feSTsiChungLiew /* write to CTLR: divide counter lower byte */ 1182bd806feSTsiChungLiew uart->ubg2 = (counter & 0x00ff); 1192bd806feSTsiChungLiew 1202bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_RX; 1212bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_TX; 1222bd806feSTsiChungLiew 1232bd806feSTsiChungLiew uart->ucr = UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED; 1242bd806feSTsiChungLiew } 125abaef69fSMarek Vasut 126abaef69fSMarek Vasut static struct serial_device mcf_serial_drv = { 127abaef69fSMarek Vasut .name = "mcf_serial", 128abaef69fSMarek Vasut .start = mcf_serial_init, 129abaef69fSMarek Vasut .stop = NULL, 130abaef69fSMarek Vasut .setbrg = mcf_serial_setbrg, 131abaef69fSMarek Vasut .putc = mcf_serial_putc, 132*ec3fd689SMarek Vasut .puts = default_serial_puts, 133abaef69fSMarek Vasut .getc = mcf_serial_getc, 134abaef69fSMarek Vasut .tstc = mcf_serial_tstc, 135abaef69fSMarek Vasut }; 136abaef69fSMarek Vasut 137abaef69fSMarek Vasut void mcf_serial_initialize(void) 138abaef69fSMarek Vasut { 139abaef69fSMarek Vasut serial_register(&mcf_serial_drv); 140abaef69fSMarek Vasut } 141abaef69fSMarek Vasut 142abaef69fSMarek Vasut __weak struct serial_device *default_serial_console(void) 143abaef69fSMarek Vasut { 144abaef69fSMarek Vasut return &mcf_serial_drv; 145abaef69fSMarek Vasut } 146