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 438cb9b237SMichal 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 9225239e12SMichal Simek static int uartlite_serial_init(const int port) 9325239e12SMichal Simek { 9425239e12SMichal Simek if (userial_ports[port]) 9525239e12SMichal Simek return 0; 9625239e12SMichal Simek return -1; 9725239e12SMichal Simek } 9825239e12SMichal Simek 9949a23e4aSMichal Simek #if !defined(CONFIG_SERIAL_MULTI) 1001378df79SJean-Christophe PLAGNIOL-VILLARD int serial_init(void) 1011378df79SJean-Christophe PLAGNIOL-VILLARD { 10225239e12SMichal Simek return uartlite_serial_init(0); 1031378df79SJean-Christophe PLAGNIOL-VILLARD } 1041378df79SJean-Christophe PLAGNIOL-VILLARD 1051378df79SJean-Christophe PLAGNIOL-VILLARD void serial_setbrg(void) 1061378df79SJean-Christophe PLAGNIOL-VILLARD { 1071378df79SJean-Christophe PLAGNIOL-VILLARD /* FIXME: what's this for? */ 1081378df79SJean-Christophe PLAGNIOL-VILLARD } 1091378df79SJean-Christophe PLAGNIOL-VILLARD 1101378df79SJean-Christophe PLAGNIOL-VILLARD void serial_putc(const char c) 1111378df79SJean-Christophe PLAGNIOL-VILLARD { 11249a23e4aSMichal Simek uartlite_serial_putc(c, 0); 1131378df79SJean-Christophe PLAGNIOL-VILLARD } 1141378df79SJean-Christophe PLAGNIOL-VILLARD 1151378df79SJean-Christophe PLAGNIOL-VILLARD void serial_puts(const char *s) 1161378df79SJean-Christophe PLAGNIOL-VILLARD { 11749a23e4aSMichal Simek uartlite_serial_puts(s, 0); 1181378df79SJean-Christophe PLAGNIOL-VILLARD } 1191378df79SJean-Christophe PLAGNIOL-VILLARD 1201378df79SJean-Christophe PLAGNIOL-VILLARD int serial_getc(void) 1211378df79SJean-Christophe PLAGNIOL-VILLARD { 12249a23e4aSMichal Simek return uartlite_serial_getc(0); 1231378df79SJean-Christophe PLAGNIOL-VILLARD } 1241378df79SJean-Christophe PLAGNIOL-VILLARD 1251378df79SJean-Christophe PLAGNIOL-VILLARD int serial_tstc(void) 1261378df79SJean-Christophe PLAGNIOL-VILLARD { 12749a23e4aSMichal Simek return uartlite_serial_tstc(0); 1281378df79SJean-Christophe PLAGNIOL-VILLARD } 12949a23e4aSMichal Simek #endif 13049a23e4aSMichal Simek 13149a23e4aSMichal Simek #if defined(CONFIG_SERIAL_MULTI) 13249a23e4aSMichal Simek /* Multi serial device functions */ 13349a23e4aSMichal Simek #define DECLARE_ESERIAL_FUNCTIONS(port) \ 13449a23e4aSMichal Simek int userial##port##_init(void) \ 13525239e12SMichal Simek { return uartlite_serial_init(port); } \ 13649a23e4aSMichal Simek void userial##port##_setbrg(void) {} \ 13749a23e4aSMichal Simek int userial##port##_getc(void) \ 13849a23e4aSMichal Simek { return uartlite_serial_getc(port); } \ 13949a23e4aSMichal Simek int userial##port##_tstc(void) \ 14049a23e4aSMichal Simek { return uartlite_serial_tstc(port); } \ 14149a23e4aSMichal Simek void userial##port##_putc(const char c) \ 14249a23e4aSMichal Simek { uartlite_serial_putc(c, port); } \ 14349a23e4aSMichal Simek void userial##port##_puts(const char *s) \ 14449a23e4aSMichal Simek { uartlite_serial_puts(s, port); } 14549a23e4aSMichal Simek 14649a23e4aSMichal Simek /* Serial device descriptor */ 14790bad891SMarek Vasut #define INIT_ESERIAL_STRUCTURE(port, __name) { \ 14890bad891SMarek Vasut .name = __name, \ 14990bad891SMarek Vasut .start = userial##port##_init, \ 15090bad891SMarek Vasut .stop = NULL, \ 15190bad891SMarek Vasut .setbrg = userial##port##_setbrg, \ 15290bad891SMarek Vasut .getc = userial##port##_getc, \ 15390bad891SMarek Vasut .tstc = userial##port##_tstc, \ 15490bad891SMarek Vasut .putc = userial##port##_putc, \ 15590bad891SMarek Vasut .puts = userial##port##_puts, \ 15690bad891SMarek Vasut } 15749a23e4aSMichal Simek 15849a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(0); 15949a23e4aSMichal Simek struct serial_device uartlite_serial0_device = 16049a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(0, "ttyUL0"); 16149a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(1); 16249a23e4aSMichal Simek struct serial_device uartlite_serial1_device = 16349a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(1, "ttyUL1"); 16449a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(2); 16549a23e4aSMichal Simek struct serial_device uartlite_serial2_device = 16649a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(2, "ttyUL2"); 16749a23e4aSMichal Simek DECLARE_ESERIAL_FUNCTIONS(3); 16849a23e4aSMichal Simek struct serial_device uartlite_serial3_device = 16949a23e4aSMichal Simek INIT_ESERIAL_STRUCTURE(3, "ttyUL3"); 17049a23e4aSMichal Simek 17149a23e4aSMichal Simek __weak struct serial_device *default_serial_console(void) 17249a23e4aSMichal Simek { 17325239e12SMichal Simek if (userial_ports[0]) 17449a23e4aSMichal Simek return &uartlite_serial0_device; 17525239e12SMichal Simek if (userial_ports[1]) 17649a23e4aSMichal Simek return &uartlite_serial1_device; 17725239e12SMichal Simek if (userial_ports[2]) 17849a23e4aSMichal Simek return &uartlite_serial2_device; 17925239e12SMichal Simek if (userial_ports[3]) 18049a23e4aSMichal Simek return &uartlite_serial3_device; 18125239e12SMichal Simek 18225239e12SMichal Simek return NULL; 18349a23e4aSMichal Simek } 184*87d69229SMarek Vasut 185*87d69229SMarek Vasut void uartlite_serial_initialize(void) 186*87d69229SMarek Vasut { 187*87d69229SMarek Vasut #ifdef XILINX_UARTLITE_BASEADDR 188*87d69229SMarek Vasut serial_register(&uartlite_serial0_device); 189*87d69229SMarek Vasut #endif /* XILINX_UARTLITE_BASEADDR */ 190*87d69229SMarek Vasut #ifdef XILINX_UARTLITE_BASEADDR1 191*87d69229SMarek Vasut serial_register(&uartlite_serial1_device); 192*87d69229SMarek Vasut #endif /* XILINX_UARTLITE_BASEADDR1 */ 193*87d69229SMarek Vasut #ifdef XILINX_UARTLITE_BASEADDR2 194*87d69229SMarek Vasut serial_register(&uartlite_serial2_device); 195*87d69229SMarek Vasut #endif /* XILINX_UARTLITE_BASEADDR2 */ 196*87d69229SMarek Vasut #ifdef XILINX_UARTLITE_BASEADDR3 197*87d69229SMarek Vasut serial_register(&uartlite_serial3_device); 198*87d69229SMarek Vasut #endif /* XILINX_UARTLITE_BASEADDR3 */ 199*87d69229SMarek Vasut } 20049a23e4aSMichal Simek #endif /* CONFIG_SERIAL_MULTI */ 201