1158961Snetchild/***********************license start***************
2158961Snetchild * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3158961Snetchild * reserved.
4158961Snetchild *
5158961Snetchild *
6158961Snetchild * Redistribution and use in source and binary forms, with or without
7158961Snetchild * modification, are permitted provided that the following conditions are
8158961Snetchild * met:
9158961Snetchild *
10158961Snetchild *   * Redistributions of source code must retain the above copyright
11160922Snetchild *     notice, this list of conditions and the following disclaimer.
12158961Snetchild *
13196511Snetchild *   * Redistributions in binary form must reproduce the above
14158961Snetchild *     copyright notice, this list of conditions and the following
15158961Snetchild *     disclaimer in the documentation and/or other materials provided
16158961Snetchild *     with the distribution.
17158961Snetchild
18158961Snetchild *   * Neither the name of Cavium Inc. nor the names of
19158961Snetchild *     its contributors may be used to endorse or promote products
20158961Snetchild *     derived from this software without specific prior written
21158961Snetchild *     permission.
22158961Snetchild
23158961Snetchild * This Software, including technical data, may be subject to U.S. export  control
24158961Snetchild * laws, including the U.S. Export Administration Act and its  associated
25158961Snetchild * regulations, and may be subject to export or import  regulations in other
26158961Snetchild * countries.
27158961Snetchild
28158961Snetchild * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29158961Snetchild * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30158961Snetchild * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31158961Snetchild * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32158961Snetchild * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33158961Snetchild * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34196511Snetchild * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35158961Snetchild * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36196511Snetchild * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
37196511Snetchild * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38196511Snetchild ***********************license end**************************************/
39158961Snetchild
40196511Snetchild
41158961Snetchild
42158961Snetchild
43196511Snetchild
44158961Snetchild
45158961Snetchild
46158961Snetchild/**
47158961Snetchild * @file
48158961Snetchild *
49158961Snetchild * General Purpose IO interface.
50158961Snetchild *
51158961Snetchild * <hr>$Revision: 70030 $<hr>
52158961Snetchild */
53158961Snetchild
54160922Snetchild#ifndef __CVMX_GPIO_H__
55158961Snetchild#define __CVMX_GPIO_H__
56158961Snetchild
57158961Snetchild#ifdef	__cplusplus
58158961Snetchildextern "C" {
59196511Snetchild#endif
60158961Snetchild
61158961Snetchild/* CSR typedefs have been moved to cvmx-gpio-defs.h */
62159010Snetchild
63160922Snetchild/**
64158961Snetchild * Clear the interrupt rising edge detector for the supplied
65196511Snetchild * pins in the mask. Chips which have more than 16 GPIO pins
66158961Snetchild * can't use them for interrupts.
67158961Snetchild e
68158961Snetchild * @param clear_mask Mask of pins to clear
69158961Snetchild */
70158961Snetchildstatic inline void cvmx_gpio_interrupt_clear(uint16_t clear_mask)
71158961Snetchild{
72158961Snetchild    if (OCTEON_IS_MODEL(OCTEON_CN61XX))
73158961Snetchild    {
74158961Snetchild        cvmx_gpio_multi_cast_t multi_cast;
75158961Snetchild        cvmx_gpio_bit_cfgx_t gpio_bit;
76158961Snetchild        int core = cvmx_get_core_num();
77158961Snetchild
78196511Snetchild        multi_cast.u64 = cvmx_read_csr(CVMX_GPIO_MULTI_CAST);
79158961Snetchild        gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(core));
80196511Snetchild
81196511Snetchild        /* If Multicast mode is enabled, and GPIO interrupt is enabled for
82196511Snetchild           edge detection, then GPIO<4..7> interrupts are per core */
83196511Snetchild        if (multi_cast.s.en && gpio_bit.s.int_en && gpio_bit.s.int_type)
84158961Snetchild        {
85158961Snetchild            /* Clear GPIO<4..7> per core */
86158961Snetchild            cvmx_ciu_intx_sum0_t ciu_sum0;
87158961Snetchild            ciu_sum0.u64 = cvmx_read_csr(CVMX_CIU_INTX_SUM0(core * 2));
88158961Snetchild            ciu_sum0.s.gpio = clear_mask & 0xf0;
89158961Snetchild            cvmx_write_csr(CVMX_CIU_INTX_SUM0(core * 2), ciu_sum0.u64);
90158961Snetchild
91158961Snetchild            /* Clear other GPIO pins for all cores. */
92158961Snetchild            cvmx_write_csr(CVMX_GPIO_INT_CLR, (clear_mask & ~0xf0));
93158961Snetchild            return;
94158961Snetchild        }
95158961Snetchild    }
96    /* Clear GPIO pins state across all cores and common interrupt states. */
97    cvmx_gpio_int_clr_t gpio_int_clr;
98    gpio_int_clr.u64 = 0;
99    gpio_int_clr.s.type = clear_mask;
100    cvmx_write_csr(CVMX_GPIO_INT_CLR, gpio_int_clr.u64);
101}
102
103/**
104 * GPIO Output Pin
105 *
106 * @param bit   The GPIO to use
107 * @param mode  Drive GPIO as output pin or not.
108 *
109 */
110static inline void cvmx_gpio_cfg(int bit, int mode)
111{
112    if (bit > 15 && bit < 20)
113    {
114        /* CN61XX/CN66XX has 20 GPIO pins and only 16 are interruptable. */
115        if (OCTEON_IS_MODEL(OCTEON_CN61XX) || OCTEON_IS_MODEL(OCTEON_CN66XX))
116        {
117            cvmx_gpio_xbit_cfgx_t gpio_xbit;
118            gpio_xbit.u64 = cvmx_read_csr(CVMX_GPIO_XBIT_CFGX(bit));
119            if (mode)
120                gpio_xbit.s.tx_oe = 1;
121            else
122                gpio_xbit.s.tx_oe = 0;
123            cvmx_write_csr(CVMX_GPIO_XBIT_CFGX(bit), gpio_xbit.u64);
124        }
125        else
126            cvmx_dprintf("cvmx_gpio_cfg: Invalid GPIO bit(%d)\n", bit);
127    }
128    else
129    {
130        cvmx_gpio_bit_cfgx_t gpio_bit;
131        gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(bit));
132        if (mode)
133            gpio_bit.s.tx_oe = 1;
134        else
135            gpio_bit.s.tx_oe = 0;
136        cvmx_write_csr(CVMX_GPIO_BIT_CFGX(bit), gpio_bit.u64);
137    }
138}
139
140/**
141 * GPIO Read Data
142 *
143 * @return Status of the GPIO pins
144 */
145static inline uint32_t cvmx_gpio_read(void)
146{
147    cvmx_gpio_rx_dat_t gpio_rx_dat;
148    gpio_rx_dat.u64 = cvmx_read_csr(CVMX_GPIO_RX_DAT);
149    return gpio_rx_dat.s.dat;
150}
151
152
153/**
154 * GPIO Clear pin
155 *
156 * @param clear_mask Bit mask to indicate which bits to drive to '0'.
157 */
158static inline void cvmx_gpio_clear(uint32_t clear_mask)
159{
160    cvmx_gpio_tx_clr_t gpio_tx_clr;
161    gpio_tx_clr.u64 = 0;
162    gpio_tx_clr.s.clr = clear_mask;
163    cvmx_write_csr(CVMX_GPIO_TX_CLR, gpio_tx_clr.u64);
164}
165
166
167/**
168 * GPIO Set pin
169 *
170 * @param set_mask Bit mask to indicate which bits to drive to '1'.
171 */
172static inline void cvmx_gpio_set(uint32_t set_mask)
173{
174    cvmx_gpio_tx_set_t gpio_tx_set;
175    gpio_tx_set.u64 = 0;
176    gpio_tx_set.s.set = set_mask;
177    cvmx_write_csr(CVMX_GPIO_TX_SET, gpio_tx_set.u64);
178}
179
180#ifdef	__cplusplus
181}
182#endif
183
184#endif
185
186