cvmx-uart.h revision 215990
1218885Sdim/***********************license start*************** 2218885Sdim * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights 3218885Sdim * reserved. 4218885Sdim * 5218885Sdim * 6218885Sdim * Redistribution and use in source and binary forms, with or without 7218885Sdim * modification, are permitted provided that the following conditions are 8218885Sdim * met: 9218885Sdim * 10234353Sdim * * Redistributions of source code must retain the above copyright 11218885Sdim * notice, this list of conditions and the following disclaimer. 12243830Sdim * 13243830Sdim * * Redistributions in binary form must reproduce the above 14218885Sdim * copyright notice, this list of conditions and the following 15218885Sdim * disclaimer in the documentation and/or other materials provided 16218885Sdim * with the distribution. 17218885Sdim 18234353Sdim * * Neither the name of Cavium Networks nor the names of 19218885Sdim * its contributors may be used to endorse or promote products 20239462Sdim * derived from this software without specific prior written 21239462Sdim * permission. 22234353Sdim 23239462Sdim * This Software, including technical data, may be subject to U.S. export control 24239462Sdim * laws, including the U.S. Export Administration Act and its associated 25218885Sdim * regulations, and may be subject to export or import regulations in other 26218885Sdim * countries. 27288943Sdim 28276479Sdim * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29276479Sdim * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR 30234353Sdim * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31288943Sdim * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32288943Sdim * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33288943Sdim * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34288943Sdim * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35288943Sdim * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36288943Sdim * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37288943Sdim * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38288943Sdim ***********************license end**************************************/ 39288943Sdim 40288943Sdim 41288943Sdim 42288943Sdim 43288943Sdim 44288943Sdim 45288943Sdim 46288943Sdim/** 47288943Sdim * @file 48288943Sdim * 49288943Sdim * interface to the serial port UART hardware 50288943Sdim * 51288943Sdim * <hr>$Revision: 52004 $<hr> 52 * 53 */ 54 55#ifndef __CVMX_UART_H__ 56#define __CVMX_UART_H__ 57 58#ifdef __cplusplus 59extern "C" { 60#endif 61 62#define CVMX_UART_NUM_PORTS 2 63#define CVMX_UART_TX_FIFO_SIZE 64 64#define CVMX_UART_RX_FIFO_SIZE 64 65 66/* CSR typedefs have been moved to cvmx-uart-defs.h */ 67 68typedef void (*cvmx_uart_intr_handler_t)(int, uint64_t[], void *); 69 70extern void cvmx_uart_enable_intr(int, cvmx_uart_intr_handler_t); 71extern int cvmx_uart_setup2(int, int, int); 72extern int cvmx_uart_setup(int); 73 74/* Defined in libc. */ 75unsigned __octeon_uart_trylock (void); 76void __octeon_uart_unlock (void); 77 78/** 79 * Get a single byte from serial port. 80 * 81 * @param uart_index Uart to read from (0 or 1) 82 * @return The byte read 83 */ 84static inline uint8_t cvmx_uart_read_byte(int uart_index) 85{ 86 cvmx_uart_lsr_t lsrval; 87 88 /* Spin until data is available */ 89 do 90 { 91 lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(uart_index)); 92 } while (!lsrval.s.dr); 93 94 /* Read and return the data */ 95 return cvmx_read_csr(CVMX_MIO_UARTX_RBR(uart_index)); 96} 97 98/** 99 * Get a single byte from serial port with a timeout. 100 * 101 * @param uart_index Uart to read from (0 or 1) 102 * @param timedout Record if a timeout has happened 103 * @param timeout the timeout count 104 * @return The byte read 105 */ 106static inline uint8_t cvmx_uart_read_byte_with_timeout(int uart_index, int *timedout, volatile unsigned timeout) 107{ 108 cvmx_uart_lsr_t lsrval; 109 110 /* Spin until data is available */ 111 *timedout = 0; 112 do 113 { 114 if(timeout == 0) 115 { 116 *timedout = 1; 117 return -1; 118 } 119 lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(uart_index)); 120 timeout --; 121 } while (!lsrval.s.dr); 122 123 /* Read and return the data */ 124 return cvmx_read_csr(CVMX_MIO_UARTX_RBR(uart_index)); 125} 126 127 128/** 129 * Put a single byte to uart port. 130 * 131 * @param uart_index Uart to write to (0 or 1) 132 * @param ch Byte to write 133 */ 134static inline void cvmx_uart_write_byte(int uart_index, uint8_t ch) 135{ 136 cvmx_uart_lsr_t lsrval; 137 138 /* Spin until there is room */ 139 do 140 { 141 lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(uart_index)); 142 } 143 while (lsrval.s.thre == 0); 144 145 /* Write the byte */ 146 cvmx_write_csr(CVMX_MIO_UARTX_THR(uart_index), ch); 147} 148 149/** 150 * Write a string to the uart 151 * 152 * @param uart_index Uart to use (0 or 1) 153 * @param str String to write 154 */ 155static inline void cvmx_uart_write_string(int uart_index, const char *str) 156{ 157 /* Just loop writing one byte at a time */ 158 while (*str) 159 { 160 cvmx_uart_write_byte(uart_index, *str); 161 str++; 162 } 163} 164 165#ifdef __cplusplus 166} 167#endif 168 169#endif /* __CVM_UART_H__ */ 170