1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17
18 *   * Neither the name of Cavium Inc. nor the names of
19 *     its contributors may be used to endorse or promote products
20 *     derived from this software without specific prior written
21 *     permission.
22
23 * This Software, including technical data, may be subject to U.S. export  control
24 * laws, including the U.S. Export Administration Act and its  associated
25 * regulations, and may be subject to export or import  regulations in other
26 * countries.
27
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40
41
42
43
44
45
46/**
47 * @file
48 *
49 * interface to the serial port UART hardware
50 *
51 * <hr>$Revision: 70030 $<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