1c9d4f46bSScott McNutt /* 2c9d4f46bSScott McNutt * (C) Copyright 2004, Psyent Corporation <www.psyent.com> 3c9d4f46bSScott McNutt * Scott McNutt <smcnutt@psyent.com> 4c9d4f46bSScott McNutt * 5c9d4f46bSScott McNutt * See file CREDITS for list of people who contributed to this 6c9d4f46bSScott McNutt * project. 7c9d4f46bSScott McNutt * 8c9d4f46bSScott McNutt * This program is free software; you can redistribute it and/or 9c9d4f46bSScott McNutt * modify it under the terms of the GNU General Public License as 10c9d4f46bSScott McNutt * published by the Free Software Foundation; either version 2 of 11c9d4f46bSScott McNutt * the License, or (at your option) any later version. 12c9d4f46bSScott McNutt * 13c9d4f46bSScott McNutt * This program is distributed in the hope that it will be useful, 14c9d4f46bSScott McNutt * but WITHOUT ANY WARRANTY; without even the implied warranty of 15c9d4f46bSScott McNutt * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16c9d4f46bSScott McNutt * GNU General Public License for more details. 17c9d4f46bSScott McNutt * 18c9d4f46bSScott McNutt * You should have received a copy of the GNU General Public License 19c9d4f46bSScott McNutt * along with this program; if not, write to the Free Software 20c9d4f46bSScott McNutt * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21c9d4f46bSScott McNutt * MA 02111-1307 USA 22c9d4f46bSScott McNutt */ 23c9d4f46bSScott McNutt 24c9d4f46bSScott McNutt 25c9d4f46bSScott McNutt #include <common.h> 26c9d4f46bSScott McNutt #include <watchdog.h> 27c9d4f46bSScott McNutt #include <asm/io.h> 28c9d4f46bSScott McNutt #include <nios2-io.h> 29*b207d645SMarek Vasut #include <linux/compiler.h> 30*b207d645SMarek Vasut #include <serial.h> 31c9d4f46bSScott McNutt 32c9d4f46bSScott McNutt DECLARE_GLOBAL_DATA_PTR; 33c9d4f46bSScott McNutt 34c9d4f46bSScott McNutt /*------------------------------------------------------------------ 35c9d4f46bSScott McNutt * UART the serial port 36c9d4f46bSScott McNutt *-----------------------------------------------------------------*/ 37c9d4f46bSScott McNutt 38c9d4f46bSScott McNutt static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE; 39c9d4f46bSScott McNutt 40c9d4f46bSScott McNutt #if defined(CONFIG_SYS_NIOS_FIXEDBAUD) 41c9d4f46bSScott McNutt 42*b207d645SMarek Vasut /* 43*b207d645SMarek Vasut * Everything's already setup for fixed-baud PTF 44c9d4f46bSScott McNutt * assignment 45c9d4f46bSScott McNutt */ 46*b207d645SMarek Vasut static void altera_serial_setbrg(void) 47*b207d645SMarek Vasut { 48*b207d645SMarek Vasut } 49*b207d645SMarek Vasut 50*b207d645SMarek Vasut static int altera_serial_init(void) 51*b207d645SMarek Vasut { 52*b207d645SMarek Vasut return 0; 53*b207d645SMarek Vasut } 54c9d4f46bSScott McNutt 55c9d4f46bSScott McNutt #else 56c9d4f46bSScott McNutt 57*b207d645SMarek Vasut static void altera_serial_setbrg(void) 58c9d4f46bSScott McNutt { 59c9d4f46bSScott McNutt unsigned div; 60c9d4f46bSScott McNutt 61c9d4f46bSScott McNutt div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1; 623ea0037fSScott McNutt writel (div, &uart->divisor); 63c9d4f46bSScott McNutt } 64c9d4f46bSScott McNutt 65*b207d645SMarek Vasut static int altera_serial_init(void) 66c9d4f46bSScott McNutt { 67c9d4f46bSScott McNutt serial_setbrg(); 68*b207d645SMarek Vasut return 0; 69c9d4f46bSScott McNutt } 70c9d4f46bSScott McNutt 71c9d4f46bSScott McNutt #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ 72c9d4f46bSScott McNutt 73c9d4f46bSScott McNutt /*----------------------------------------------------------------------- 74c9d4f46bSScott McNutt * UART CONSOLE 75c9d4f46bSScott McNutt *---------------------------------------------------------------------*/ 76*b207d645SMarek Vasut static void altera_serial_putc(char c) 77c9d4f46bSScott McNutt { 78c9d4f46bSScott McNutt if (c == '\n') 79c9d4f46bSScott McNutt serial_putc ('\r'); 80c9d4f46bSScott McNutt while ((readl (&uart->status) & NIOS_UART_TRDY) == 0) 81c9d4f46bSScott McNutt WATCHDOG_RESET (); 823ea0037fSScott McNutt writel ((unsigned char)c, &uart->txdata); 83c9d4f46bSScott McNutt } 84c9d4f46bSScott McNutt 85*b207d645SMarek Vasut static void altera_serial_puts(const char *s) 86c9d4f46bSScott McNutt { 87c9d4f46bSScott McNutt while (*s != 0) { 88c9d4f46bSScott McNutt serial_putc (*s++); 89c9d4f46bSScott McNutt } 90c9d4f46bSScott McNutt } 91c9d4f46bSScott McNutt 92*b207d645SMarek Vasut static int altera_serial_tstc(void) 93c9d4f46bSScott McNutt { 94c9d4f46bSScott McNutt return (readl (&uart->status) & NIOS_UART_RRDY); 95c9d4f46bSScott McNutt } 96c9d4f46bSScott McNutt 97*b207d645SMarek Vasut static int altera_serial_getc(void) 98c9d4f46bSScott McNutt { 99c9d4f46bSScott McNutt while (serial_tstc () == 0) 100c9d4f46bSScott McNutt WATCHDOG_RESET (); 101c9d4f46bSScott McNutt return (readl (&uart->rxdata) & 0x00ff ); 102c9d4f46bSScott McNutt } 103*b207d645SMarek Vasut 104*b207d645SMarek Vasut #ifdef CONFIG_SERIAL_MULTI 105*b207d645SMarek Vasut static struct serial_device altera_serial_drv = { 106*b207d645SMarek Vasut .name = "altera_serial", 107*b207d645SMarek Vasut .start = altera_serial_init, 108*b207d645SMarek Vasut .stop = NULL, 109*b207d645SMarek Vasut .setbrg = altera_serial_setbrg, 110*b207d645SMarek Vasut .putc = altera_serial_putc, 111*b207d645SMarek Vasut .puts = altera_serial_puts, 112*b207d645SMarek Vasut .getc = altera_serial_getc, 113*b207d645SMarek Vasut .tstc = altera_serial_tstc, 114*b207d645SMarek Vasut }; 115*b207d645SMarek Vasut 116*b207d645SMarek Vasut void altera_serial_initialize(void) 117*b207d645SMarek Vasut { 118*b207d645SMarek Vasut serial_register(&altera_serial_drv); 119*b207d645SMarek Vasut } 120*b207d645SMarek Vasut 121*b207d645SMarek Vasut __weak struct serial_device *default_serial_console(void) 122*b207d645SMarek Vasut { 123*b207d645SMarek Vasut return &altera_serial_drv; 124*b207d645SMarek Vasut } 125*b207d645SMarek Vasut #else 126*b207d645SMarek Vasut int serial_init(void) 127*b207d645SMarek Vasut { 128*b207d645SMarek Vasut return altera_serial_init(); 129*b207d645SMarek Vasut } 130*b207d645SMarek Vasut 131*b207d645SMarek Vasut void serial_setbrg(void) 132*b207d645SMarek Vasut { 133*b207d645SMarek Vasut altera_serial_setbrg(); 134*b207d645SMarek Vasut } 135*b207d645SMarek Vasut 136*b207d645SMarek Vasut void serial_putc(const char c) 137*b207d645SMarek Vasut { 138*b207d645SMarek Vasut altera_serial_putc(c); 139*b207d645SMarek Vasut } 140*b207d645SMarek Vasut 141*b207d645SMarek Vasut void serial_puts(const char *s) 142*b207d645SMarek Vasut { 143*b207d645SMarek Vasut altera_serial_puts(s); 144*b207d645SMarek Vasut } 145*b207d645SMarek Vasut 146*b207d645SMarek Vasut int serial_getc(void) 147*b207d645SMarek Vasut { 148*b207d645SMarek Vasut return altera_serial_getc(); 149*b207d645SMarek Vasut } 150*b207d645SMarek Vasut 151*b207d645SMarek Vasut int serial_tstc(void) 152*b207d645SMarek Vasut { 153*b207d645SMarek Vasut return altera_serial_tstc(); 154*b207d645SMarek Vasut } 155*b207d645SMarek Vasut #endif 156