cvmx-uart.h revision 215990
1210284Sjmallett/***********************license start*************** 2215990Sjmallett * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights 3215990Sjmallett * reserved. 4210284Sjmallett * 5210284Sjmallett * 6215990Sjmallett * Redistribution and use in source and binary forms, with or without 7215990Sjmallett * modification, are permitted provided that the following conditions are 8215990Sjmallett * met: 9210284Sjmallett * 10215990Sjmallett * * Redistributions of source code must retain the above copyright 11215990Sjmallett * notice, this list of conditions and the following disclaimer. 12210284Sjmallett * 13215990Sjmallett * * Redistributions in binary form must reproduce the above 14215990Sjmallett * copyright notice, this list of conditions and the following 15215990Sjmallett * disclaimer in the documentation and/or other materials provided 16215990Sjmallett * with the distribution. 17215990Sjmallett 18215990Sjmallett * * Neither the name of Cavium Networks nor the names of 19215990Sjmallett * its contributors may be used to endorse or promote products 20215990Sjmallett * derived from this software without specific prior written 21215990Sjmallett * permission. 22215990Sjmallett 23215990Sjmallett * This Software, including technical data, may be subject to U.S. export control 24215990Sjmallett * laws, including the U.S. Export Administration Act and its associated 25215990Sjmallett * regulations, and may be subject to export or import regulations in other 26215990Sjmallett * countries. 27215990Sjmallett 28215990Sjmallett * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29215990Sjmallett * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR 30215990Sjmallett * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31215990Sjmallett * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32215990Sjmallett * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33215990Sjmallett * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34215990Sjmallett * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35215990Sjmallett * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36215990Sjmallett * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37215990Sjmallett * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38210284Sjmallett ***********************license end**************************************/ 39210284Sjmallett 40210284Sjmallett 41210284Sjmallett 42210284Sjmallett 43210284Sjmallett 44210284Sjmallett 45215990Sjmallett 46210284Sjmallett/** 47210284Sjmallett * @file 48210284Sjmallett * 49210284Sjmallett * interface to the serial port UART hardware 50210284Sjmallett * 51215990Sjmallett * <hr>$Revision: 52004 $<hr> 52210284Sjmallett * 53210284Sjmallett */ 54210284Sjmallett 55210284Sjmallett#ifndef __CVMX_UART_H__ 56210284Sjmallett#define __CVMX_UART_H__ 57210284Sjmallett 58210284Sjmallett#ifdef __cplusplus 59210284Sjmallettextern "C" { 60210284Sjmallett#endif 61210284Sjmallett 62210284Sjmallett#define CVMX_UART_NUM_PORTS 2 63210284Sjmallett#define CVMX_UART_TX_FIFO_SIZE 64 64210284Sjmallett#define CVMX_UART_RX_FIFO_SIZE 64 65210284Sjmallett 66215990Sjmallett/* CSR typedefs have been moved to cvmx-uart-defs.h */ 67210284Sjmallett 68215990Sjmalletttypedef void (*cvmx_uart_intr_handler_t)(int, uint64_t[], void *); 69215990Sjmallett 70215990Sjmallettextern void cvmx_uart_enable_intr(int, cvmx_uart_intr_handler_t); 71215990Sjmallettextern int cvmx_uart_setup2(int, int, int); 72215990Sjmallettextern int cvmx_uart_setup(int); 73215990Sjmallett 74210284Sjmallett/* Defined in libc. */ 75210284Sjmallettunsigned __octeon_uart_trylock (void); 76210284Sjmallettvoid __octeon_uart_unlock (void); 77210284Sjmallett 78215990Sjmallett/** 79215990Sjmallett * Get a single byte from serial port. 80215990Sjmallett * 81215990Sjmallett * @param uart_index Uart to read from (0 or 1) 82215990Sjmallett * @return The byte read 83215990Sjmallett */ 84215990Sjmallettstatic inline uint8_t cvmx_uart_read_byte(int uart_index) 85215990Sjmallett{ 86215990Sjmallett cvmx_uart_lsr_t lsrval; 87215990Sjmallett 88215990Sjmallett /* Spin until data is available */ 89215990Sjmallett do 90215990Sjmallett { 91215990Sjmallett lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(uart_index)); 92215990Sjmallett } while (!lsrval.s.dr); 93215990Sjmallett 94215990Sjmallett /* Read and return the data */ 95215990Sjmallett return cvmx_read_csr(CVMX_MIO_UARTX_RBR(uart_index)); 96215990Sjmallett} 97215990Sjmallett 98215990Sjmallett/** 99215990Sjmallett * Get a single byte from serial port with a timeout. 100215990Sjmallett * 101215990Sjmallett * @param uart_index Uart to read from (0 or 1) 102215990Sjmallett * @param timedout Record if a timeout has happened 103215990Sjmallett * @param timeout the timeout count 104215990Sjmallett * @return The byte read 105215990Sjmallett */ 106215990Sjmallettstatic inline uint8_t cvmx_uart_read_byte_with_timeout(int uart_index, int *timedout, volatile unsigned timeout) 107215990Sjmallett{ 108215990Sjmallett cvmx_uart_lsr_t lsrval; 109215990Sjmallett 110215990Sjmallett /* Spin until data is available */ 111215990Sjmallett *timedout = 0; 112215990Sjmallett do 113215990Sjmallett { 114215990Sjmallett if(timeout == 0) 115215990Sjmallett { 116215990Sjmallett *timedout = 1; 117215990Sjmallett return -1; 118215990Sjmallett } 119215990Sjmallett lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(uart_index)); 120215990Sjmallett timeout --; 121215990Sjmallett } while (!lsrval.s.dr); 122215990Sjmallett 123215990Sjmallett /* Read and return the data */ 124215990Sjmallett return cvmx_read_csr(CVMX_MIO_UARTX_RBR(uart_index)); 125215990Sjmallett} 126215990Sjmallett 127215990Sjmallett 128215990Sjmallett/** 129215990Sjmallett * Put a single byte to uart port. 130215990Sjmallett * 131215990Sjmallett * @param uart_index Uart to write to (0 or 1) 132215990Sjmallett * @param ch Byte to write 133215990Sjmallett */ 134215990Sjmallettstatic inline void cvmx_uart_write_byte(int uart_index, uint8_t ch) 135215990Sjmallett{ 136215990Sjmallett cvmx_uart_lsr_t lsrval; 137215990Sjmallett 138215990Sjmallett /* Spin until there is room */ 139215990Sjmallett do 140215990Sjmallett { 141215990Sjmallett lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(uart_index)); 142215990Sjmallett } 143215990Sjmallett while (lsrval.s.thre == 0); 144215990Sjmallett 145215990Sjmallett /* Write the byte */ 146215990Sjmallett cvmx_write_csr(CVMX_MIO_UARTX_THR(uart_index), ch); 147215990Sjmallett} 148215990Sjmallett 149215990Sjmallett/** 150215990Sjmallett * Write a string to the uart 151215990Sjmallett * 152215990Sjmallett * @param uart_index Uart to use (0 or 1) 153215990Sjmallett * @param str String to write 154215990Sjmallett */ 155215990Sjmallettstatic inline void cvmx_uart_write_string(int uart_index, const char *str) 156215990Sjmallett{ 157215990Sjmallett /* Just loop writing one byte at a time */ 158215990Sjmallett while (*str) 159215990Sjmallett { 160215990Sjmallett cvmx_uart_write_byte(uart_index, *str); 161215990Sjmallett str++; 162215990Sjmallett } 163215990Sjmallett} 164215990Sjmallett 165210284Sjmallett#ifdef __cplusplus 166210284Sjmallett} 167210284Sjmallett#endif 168210284Sjmallett 169210284Sjmallett#endif /* __CVM_UART_H__ */ 170