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 39*abaef69fSMarek 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 77*abaef69fSMarek 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 90*abaef69fSMarek Vasut static void mcf_serial_puts(const char *s) 912bd806feSTsiChungLiew { 922bd806feSTsiChungLiew while (*s) { 932bd806feSTsiChungLiew serial_putc(*s++); 942bd806feSTsiChungLiew } 952bd806feSTsiChungLiew } 962bd806feSTsiChungLiew 97*abaef69fSMarek Vasut static int mcf_serial_getc(void) 982bd806feSTsiChungLiew { 996d0f6bcfSJean-Christophe PLAGNIOL-VILLARD volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 1002bd806feSTsiChungLiew 1012bd806feSTsiChungLiew /* Wait for a character to arrive. */ 1022bd806feSTsiChungLiew while (!(uart->usr & UART_USR_RXRDY)) ; 1032bd806feSTsiChungLiew return uart->urb; 1042bd806feSTsiChungLiew } 1052bd806feSTsiChungLiew 106*abaef69fSMarek Vasut static int mcf_serial_tstc(void) 1072bd806feSTsiChungLiew { 1086d0f6bcfSJean-Christophe PLAGNIOL-VILLARD volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 1092bd806feSTsiChungLiew 1102bd806feSTsiChungLiew return (uart->usr & UART_USR_RXRDY); 1112bd806feSTsiChungLiew } 1122bd806feSTsiChungLiew 113*abaef69fSMarek Vasut static void mcf_serial_setbrg(void) 1142bd806feSTsiChungLiew { 1156d0f6bcfSJean-Christophe PLAGNIOL-VILLARD volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE); 1162bd806feSTsiChungLiew u32 counter; 1172bd806feSTsiChungLiew 11892d3e6e0SRichard Retanubun /* Setting up BaudRate */ 11992d3e6e0SRichard Retanubun counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2)); 12092d3e6e0SRichard Retanubun counter = counter / gd->baudrate; 1212bd806feSTsiChungLiew 1222bd806feSTsiChungLiew /* write to CTUR: divide counter upper byte */ 1232bd806feSTsiChungLiew uart->ubg1 = ((counter & 0xff00) >> 8); 1242bd806feSTsiChungLiew /* write to CTLR: divide counter lower byte */ 1252bd806feSTsiChungLiew uart->ubg2 = (counter & 0x00ff); 1262bd806feSTsiChungLiew 1272bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_RX; 1282bd806feSTsiChungLiew uart->ucr = UART_UCR_RESET_TX; 1292bd806feSTsiChungLiew 1302bd806feSTsiChungLiew uart->ucr = UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED; 1312bd806feSTsiChungLiew } 132*abaef69fSMarek Vasut 133*abaef69fSMarek Vasut #ifdef CONFIG_SERIAL_MULTI 134*abaef69fSMarek Vasut static struct serial_device mcf_serial_drv = { 135*abaef69fSMarek Vasut .name = "mcf_serial", 136*abaef69fSMarek Vasut .start = mcf_serial_init, 137*abaef69fSMarek Vasut .stop = NULL, 138*abaef69fSMarek Vasut .setbrg = mcf_serial_setbrg, 139*abaef69fSMarek Vasut .putc = mcf_serial_putc, 140*abaef69fSMarek Vasut .puts = mcf_serial_puts, 141*abaef69fSMarek Vasut .getc = mcf_serial_getc, 142*abaef69fSMarek Vasut .tstc = mcf_serial_tstc, 143*abaef69fSMarek Vasut }; 144*abaef69fSMarek Vasut 145*abaef69fSMarek Vasut void mcf_serial_initialize(void) 146*abaef69fSMarek Vasut { 147*abaef69fSMarek Vasut serial_register(&mcf_serial_drv); 148*abaef69fSMarek Vasut } 149*abaef69fSMarek Vasut 150*abaef69fSMarek Vasut __weak struct serial_device *default_serial_console(void) 151*abaef69fSMarek Vasut { 152*abaef69fSMarek Vasut return &mcf_serial_drv; 153*abaef69fSMarek Vasut } 154*abaef69fSMarek Vasut #else 155*abaef69fSMarek Vasut int serial_init(void) 156*abaef69fSMarek Vasut { 157*abaef69fSMarek Vasut return mcf_serial_init(); 158*abaef69fSMarek Vasut } 159*abaef69fSMarek Vasut 160*abaef69fSMarek Vasut void serial_setbrg(void) 161*abaef69fSMarek Vasut { 162*abaef69fSMarek Vasut mcf_serial_setbrg(); 163*abaef69fSMarek Vasut } 164*abaef69fSMarek Vasut 165*abaef69fSMarek Vasut void serial_putc(const char c) 166*abaef69fSMarek Vasut { 167*abaef69fSMarek Vasut mcf_serial_putc(c); 168*abaef69fSMarek Vasut } 169*abaef69fSMarek Vasut 170*abaef69fSMarek Vasut void serial_puts(const char *s) 171*abaef69fSMarek Vasut { 172*abaef69fSMarek Vasut mcf_serial_puts(s); 173*abaef69fSMarek Vasut } 174*abaef69fSMarek Vasut 175*abaef69fSMarek Vasut int serial_getc(void) 176*abaef69fSMarek Vasut { 177*abaef69fSMarek Vasut return mcf_serial_getc(); 178*abaef69fSMarek Vasut } 179*abaef69fSMarek Vasut 180*abaef69fSMarek Vasut int serial_tstc(void) 181*abaef69fSMarek Vasut { 182*abaef69fSMarek Vasut return mcf_serial_tstc(); 183*abaef69fSMarek Vasut } 184*abaef69fSMarek Vasut #endif 185