11378df79SJean-Christophe PLAGNIOL-VILLARD /* 249a23e4aSMichal Simek * (C) Copyright 2008-2011 Michal Simek <monstr@monstr.eu> 353ea981cSMichal Simek * Clean driver and add xilinx constant from header file 41378df79SJean-Christophe PLAGNIOL-VILLARD * 553ea981cSMichal Simek * (C) Copyright 2004 Atmark Techno, Inc. 61378df79SJean-Christophe PLAGNIOL-VILLARD * Yasushi SHOJI <yashi@atmark-techno.com> 71378df79SJean-Christophe PLAGNIOL-VILLARD * 81378df79SJean-Christophe PLAGNIOL-VILLARD * See file CREDITS for list of people who contributed to this 91378df79SJean-Christophe PLAGNIOL-VILLARD * project. 101378df79SJean-Christophe PLAGNIOL-VILLARD * 111378df79SJean-Christophe PLAGNIOL-VILLARD * This program is free software; you can redistribute it and/or 121378df79SJean-Christophe PLAGNIOL-VILLARD * modify it under the terms of the GNU General Public License as 131378df79SJean-Christophe PLAGNIOL-VILLARD * published by the Free Software Foundation; either version 2 of 141378df79SJean-Christophe PLAGNIOL-VILLARD * the License, or (at your option) any later version. 151378df79SJean-Christophe PLAGNIOL-VILLARD * 161378df79SJean-Christophe PLAGNIOL-VILLARD * This program is distributed in the hope that it will be useful, 171378df79SJean-Christophe PLAGNIOL-VILLARD * but WITHOUT ANY WARRANTY; without even the implied warranty of 181378df79SJean-Christophe PLAGNIOL-VILLARD * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 191378df79SJean-Christophe PLAGNIOL-VILLARD * GNU General Public License for more details. 201378df79SJean-Christophe PLAGNIOL-VILLARD * 211378df79SJean-Christophe PLAGNIOL-VILLARD * You should have received a copy of the GNU General Public License 221378df79SJean-Christophe PLAGNIOL-VILLARD * along with this program; if not, write to the Free Software 231378df79SJean-Christophe PLAGNIOL-VILLARD * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 241378df79SJean-Christophe PLAGNIOL-VILLARD * MA 02111-1307 USA 251378df79SJean-Christophe PLAGNIOL-VILLARD */ 261378df79SJean-Christophe PLAGNIOL-VILLARD 271378df79SJean-Christophe PLAGNIOL-VILLARD #include <config.h> 2849a23e4aSMichal Simek #include <common.h> 2953ea981cSMichal Simek #include <asm/io.h> 3049a23e4aSMichal Simek #include <linux/compiler.h> 3149a23e4aSMichal Simek #include <serial.h> 321378df79SJean-Christophe PLAGNIOL-VILLARD 3353ea981cSMichal Simek #define SR_TX_FIFO_FULL 0x08 /* transmit FIFO full */ 3453ea981cSMichal Simek #define SR_RX_FIFO_VALID_DATA 0x01 /* data in receive FIFO */ 3553ea981cSMichal Simek #define SR_RX_FIFO_FULL 0x02 /* receive FIFO full */ 361378df79SJean-Christophe PLAGNIOL-VILLARD 3749a23e4aSMichal Simek struct uartlite { 3849a23e4aSMichal Simek unsigned int rx_fifo; 3949a23e4aSMichal Simek unsigned int tx_fifo; 4049a23e4aSMichal Simek unsigned int status; 4149a23e4aSMichal Simek }; 421378df79SJean-Christophe PLAGNIOL-VILLARD 43*8cb9b237SMichal Simek static struct uartlite *userial_ports[4] = { 4449a23e4aSMichal Simek #ifdef XILINX_UARTLITE_BASEADDR 4549a23e4aSMichal Simek [0] = (struct uartlite *)XILINX_UARTLITE_BASEADDR, 4649a23e4aSMichal Simek #endif 4749a23e4aSMichal Simek #ifdef XILINX_UARTLITE_BASEADDR1 4849a23e4aSMichal Simek [1] = (struct uartlite *)XILINX_UARTLITE_BASEADDR1, 4949a23e4aSMichal Simek #endif 5049a23e4aSMichal Simek #ifdef XILINX_UARTLITE_BASEADDR2 5149a23e4aSMichal Simek [2] = (struct uartlite *)XILINX_UARTLITE_BASEADDR2, 5249a23e4aSMichal Simek #endif 5349a23e4aSMichal Simek #ifdef XILINX_UARTLITE_BASEADDR3 5449a23e4aSMichal Simek [3] = (struct uartlite *)XILINX_UARTLITE_BASEADDR3 5549a23e4aSMichal Simek #endif 5649a23e4aSMichal Simek }; 5749a23e4aSMichal Simek 5849a23e4aSMichal Simek void uartlite_serial_putc(const char c, const int port) 5949a23e4aSMichal Simek { 6049a23e4aSMichal Simek struct uartlite *regs = userial_ports[port]; 6149a23e4aSMichal Simek 6249a23e4aSMichal Simek if (c == '\n') 6349a23e4aSMichal Simek uartlite_serial_putc('\r', port); 6449a23e4aSMichal Simek 6549a23e4aSMichal Simek while (in_be32(®s->status) & SR_TX_FIFO_FULL) 6649a23e4aSMichal Simek ; 6749a23e4aSMichal Simek out_be32(®s->tx_fifo, c & 0xff); 6849a23e4aSMichal Simek } 6949a23e4aSMichal Simek 7049a23e4aSMichal Simek void uartlite_serial_puts(const char *s, const int port) 7149a23e4aSMichal Simek { 7249a23e4aSMichal Simek while (*s) 7349a23e4aSMichal Simek uartlite_serial_putc(*s++, port); 7449a23e4aSMichal Simek } 7549a23e4aSMichal Simek 7649a23e4aSMichal Simek int uartlite_serial_getc(const int port) 7749a23e4aSMichal Simek { 7849a23e4aSMichal Simek struct uartlite *regs = userial_ports[port]; 7949a23e4aSMichal Simek 8049a23e4aSMichal Simek while (!(in_be32(®s->status) & SR_RX_FIFO_VALID_DATA)) 8149a23e4aSMichal Simek ; 8249a23e4aSMichal Simek return in_be32(®s->rx_fifo) & 0xff; 8349a23e4aSMichal Simek } 8449a23e4aSMichal Simek 8549a23e4aSMichal Simek int uartlite_serial_tstc(const int port) 8649a23e4aSMichal Simek { 8749a23e4aSMichal Simek struct uartlite *regs = userial_ports[port]; 8849a23e4aSMichal Simek 8949a23e4aSMichal Simek return in_be32(®s->status) & SR_RX_FIFO_VALID_DATA; 9049a23e4aSMichal Simek } 9149a23e4aSMichal Simek 9249a23e4aSMichal Simek #if !defined(CONFIG_SERIAL_MULTI) 931378df79SJean-Christophe PLAGNIOL-VILLARD int serial_init(void) 941378df79SJean-Christophe PLAGNIOL-VILLARD { 951378df79SJean-Christophe PLAGNIOL-VILLARD /* FIXME: Nothing for now. We should initialize fifo, etc */ 961378df79SJean-Christophe PLAGNIOL-VILLARD return 0; 971378df79SJean-Christophe PLAGNIOL-VILLARD } 981378df79SJean-Christophe PLAGNIOL-VILLARD 991378df79SJean-Christophe PLAGNIOL-VILLARD void serial_setbrg(void) 1001378df79SJean-Christophe PLAGNIOL-VILLARD { 1011378df79SJean-Christophe PLAGNIOL-VILLARD /* FIXME: what's this for? */ 1021378df79SJean-Christophe PLAGNIOL-VILLARD } 1031378df79SJean-Christophe PLAGNIOL-VILLARD 1041378df79SJean-Christophe PLAGNIOL-VILLARD void serial_putc(const char c) 1051378df79SJean-Christophe PLAGNIOL-VILLARD { 10649a23e4aSMichal Simek uartlite_serial_putc(c, 0); 1071378df79SJean-Christophe PLAGNIOL-VILLARD } 1081378df79SJean-Christophe PLAGNIOL-VILLARD 1091378df79SJean-Christophe PLAGNIOL-VILLARD void serial_puts(const char *s) 1101378df79SJean-Christophe PLAGNIOL-VILLARD { 11149a23e4aSMichal Simek uartlite_serial_puts(s, 0); 1121378df79SJean-Christophe PLAGNIOL-VILLARD } 1131378df79SJean-Christophe PLAGNIOL-VILLARD 1141378df79SJean-Christophe PLAGNIOL-VILLARD int serial_getc(void) 1151378df79SJean-Christophe PLAGNIOL-VILLARD { 11649a23e4aSMichal Simek return uartlite_serial_getc(0); 1171378df79SJean-Christophe PLAGNIOL-VILLARD } 1181378df79SJean-Christophe PLAGNIOL-VILLARD 1191378df79SJean-Christophe PLAGNIOL-VILLARD int serial_tstc(void) 1201378df79SJean-Christophe PLAGNIOL-VILLARD { 12149a23e4aSMichal Simek return uartlite_serial_tstc(0); 1221378df79SJean-Christophe PLAGNIOL-VILLARD } 12349a23e4aSMichal Simek #endif 12449a23e4aSMichal Simek 12549a23e4aSMichal Simek #if defined(CONFIG_SERIAL_MULTI) 12649a23e4aSMichal Simek /* Multi serial device functions */ 12749a23e4aSMichal Simek #define DECLARE_ESERIAL_FUNCTIONS(port) \ 12849a23e4aSMichal Simek int userial##port##_init(void) \ 12949a23e4aSMichal Simek { return(0); } \ 13049a23e4aSMichal Simek void userial##port##_setbrg(void) {} \ 13149a23e4aSMichal Simek int userial##port##_getc(void) \ 13249a23e4aSMichal Simek { return uartlite_serial_getc(port); } \ 13349a23e4aSMichal Simek int userial##port##_tstc(void) \ 13449a23e4aSMichal Simek { return uartlite_serial_tstc(port); } \ 13549a23e4aSMichal Simek void userial##port##_putc(const char c) \ 13649a23e4aSMichal Simek { uartlite_serial_putc(c, port); } \ 13749a23e4aSMichal Simek void userial##port##_puts(const char *s) \ 13849a23e4aSMichal Simek { uartlite_serial_puts(s, port); } 13949a23e4aSMichal Simek 14049a23e4aSMichal Simek /* Serial device descriptor */ 14149a23e4aSMichal Simek #define INIT_ESERIAL_STRUCTURE(port, name) {\ 14249a23e4aSMichal Simek name,\ 14349a23e4aSMichal Simek userial##port##_init,\ 14449a23e4aSMichal Simek NULL,\ 14549a23e4aSMichal Simek userial##port##_setbrg,\ 14649a23e4aSMichal Simek userial##port##_getc,\ 14749a23e4aSMichal Simek userial##port##_tstc,\ 14849a23e4aSMichal Simek userial##port##_putc,\ 14949a23e4aSMichal Simek userial##port##_puts, } 15049a23e4aSMichal Simek 15149a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(0); 15249a23e4aSMichal Simek struct serial_device uartlite_serial0_device = 15349a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(0, "ttyUL0"); 15449a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(1); 15549a23e4aSMichal Simek struct serial_device uartlite_serial1_device = 15649a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(1, "ttyUL1"); 15749a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(2); 15849a23e4aSMichal Simek struct serial_device uartlite_serial2_device = 15949a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(2, "ttyUL2"); 16049a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(3); 16149a23e4aSMichal Simek struct serial_device uartlite_serial3_device = 16249a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(3, "ttyUL3"); 16349a23e4aSMichal Simek 16449a23e4aSMichal Simek __weak struct serial_device *default_serial_console(void) 16549a23e4aSMichal Simek { 16649a23e4aSMichal Simek # ifdef XILINX_UARTLITE_BASEADDR 16749a23e4aSMichal Simek return &uartlite_serial0_device; 16849a23e4aSMichal Simek # endif /* XILINX_UARTLITE_BASEADDR */ 16949a23e4aSMichal Simek # ifdef XILINX_UARTLITE_BASEADDR1 17049a23e4aSMichal Simek return &uartlite_serial1_device; 17149a23e4aSMichal Simek # endif /* XILINX_UARTLITE_BASEADDR1 */ 17249a23e4aSMichal Simek # ifdef XILINX_UARTLITE_BASEADDR2 17349a23e4aSMichal Simek return &uartlite_serial2_device; 17449a23e4aSMichal Simek # endif /* XILINX_UARTLITE_BASEADDR2 */ 17549a23e4aSMichal Simek # ifdef XILINX_UARTLITE_BASEADDR3 17649a23e4aSMichal Simek return &uartlite_serial3_device; 17749a23e4aSMichal Simek # endif /* XILINX_UARTLITE_BASEADDR3 */ 17849a23e4aSMichal Simek } 17949a23e4aSMichal Simek #endif /* CONFIG_SERIAL_MULTI */ 180