/* * cpu/spc300/serial.c * * Copyright (C) 2009 SPiDCOM Technologies * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */ #include #include #include #include #include #if !defined(CONFIG_USART0) #error must define CONFIG_USART0 #endif /** * Function: serial_setbrg * Parameters: void * Purpose: Set baudrate * Return Value: void */ void serial_setbrg (void) { DECLARE_GLOBAL_DATA_PTR; int baudrate; int baud_divisor; if ((baudrate = gd->baudrate) <= 0) baudrate = CONFIG_BAUDRATE; baud_divisor = (get_master_clock((gd_t*)gd) + 8*baudrate) / (16 * baudrate); serial_setdivisor ((uint32_t)baud_divisor); } /* serial_setbrg */ /** * Function: serial_setdivisor * Parameters: uint32_t divisor * Purpose: write baud divisor in UART registers * Return value: void */ void serial_setdivisor (uint32_t divisor) { writel(readl(UART_LCR_1) | 0x80, UART_LCR_1); writel(divisor & 0x00ff, UART_DLL_1); writel((divisor & 0xff00) >> 8, UART_DLH_1); writel(readl(UART_LCR_1) & ~0x80, UART_LCR_1); } /* serial_setdivisor */ /** * Function: serial_init * Parameters: void * Purpose: Initilize serial * Return Value: int */ int serial_init (void) { /* disable interrupts */ writel(0, UART_IER_1); /* set mode 8 bits, 1 stop, no parity */ writel(UART_DLS_8, UART_LCR_1); /* set FIFO RX & TX to half trigger */ writel(UART_RT_HALF | UART_TET_QUARTER | UART_XFIFOR | UART_RFIFOR | UART_FIFOE, UART_FCR_1); serial_setbrg (); return (0); } /* serial_init */ /** * Function: serial_putc * Parameters: const char c * Purpose: Send character on serial * Return Value: */ void serial_putc (const char c) { if (c == '\n') serial_putc ('\r'); while(!(readl(UART_LSR_1) & UART_TEMT)); writel(c, UART_THR_1); } /* serial_putc */ /** * Function: serial_puts * Parameters: const char *s * Purpose: Send string on serial * Return Value: void */ void serial_puts (const char *s) { while (*s) { serial_putc (*s++); } } /* serial_puts */ /** * Function: serial_getc * Parameters: void * Purpose: Receive character * Return Value: int */ int serial_getc (void) { int val; while(!(readl(UART_LSR_1) & UART_DR)); val = readl(UART_RBR_1); return val; } /* serial_getc */ /** * Function: serial_tstc * Parameters: void * Purpose: Test if a character is received * Return Value: int */ int serial_tstc (void) { if(readl(UART_LSR_1) & UART_DR) return 1; else return 0; } /* serial_tstc */