xref: /dflybsd-src/sys/dev/netif/ath/ath_hal/ar5212/ar5212_gpio.c (revision 572ff6f6e8b95055988f178b6ba12ce77bb5b3c2)
1*572ff6f6SMatthew Dillon /*
2*572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3*572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4*572ff6f6SMatthew Dillon  *
5*572ff6f6SMatthew Dillon  * Permission to use, copy, modify, and/or distribute this software for any
6*572ff6f6SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
7*572ff6f6SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
8*572ff6f6SMatthew Dillon  *
9*572ff6f6SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*572ff6f6SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*572ff6f6SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*572ff6f6SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*572ff6f6SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*572ff6f6SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*572ff6f6SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*572ff6f6SMatthew Dillon  *
17*572ff6f6SMatthew Dillon  * $FreeBSD$
18*572ff6f6SMatthew Dillon  */
19*572ff6f6SMatthew Dillon #include "opt_ah.h"
20*572ff6f6SMatthew Dillon 
21*572ff6f6SMatthew Dillon #include "ah.h"
22*572ff6f6SMatthew Dillon #include "ah_internal.h"
23*572ff6f6SMatthew Dillon #include "ah_devid.h"
24*572ff6f6SMatthew Dillon #ifdef AH_DEBUG
25*572ff6f6SMatthew Dillon #include "ah_desc.h"			/* NB: for HAL_PHYERR* */
26*572ff6f6SMatthew Dillon #endif
27*572ff6f6SMatthew Dillon 
28*572ff6f6SMatthew Dillon #include "ar5212/ar5212.h"
29*572ff6f6SMatthew Dillon #include "ar5212/ar5212reg.h"
30*572ff6f6SMatthew Dillon #include "ar5212/ar5212phy.h"
31*572ff6f6SMatthew Dillon 
32*572ff6f6SMatthew Dillon #define	AR_NUM_GPIO	6		/* 6 GPIO pins */
33*572ff6f6SMatthew Dillon #define	AR_GPIOD_MASK	0x0000002F	/* GPIO data reg r/w mask */
34*572ff6f6SMatthew Dillon 
35*572ff6f6SMatthew Dillon /*
36*572ff6f6SMatthew Dillon  * Configure GPIO Output lines
37*572ff6f6SMatthew Dillon  */
38*572ff6f6SMatthew Dillon HAL_BOOL
ar5212GpioCfgOutput(struct ath_hal * ah,uint32_t gpio,HAL_GPIO_MUX_TYPE type)39*572ff6f6SMatthew Dillon ar5212GpioCfgOutput(struct ath_hal *ah, uint32_t gpio, HAL_GPIO_MUX_TYPE type)
40*572ff6f6SMatthew Dillon {
41*572ff6f6SMatthew Dillon 	HALASSERT(gpio < AR_NUM_GPIO);
42*572ff6f6SMatthew Dillon 
43*572ff6f6SMatthew Dillon 	/*
44*572ff6f6SMatthew Dillon 	 * NB: AR_GPIOCR_CR_A(pin) is all 1's so there's no need
45*572ff6f6SMatthew Dillon 	 *     to clear the field before or'ing in the new value.
46*572ff6f6SMatthew Dillon 	 */
47*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_GPIOCR,
48*572ff6f6SMatthew Dillon 		  OS_REG_READ(ah, AR_GPIOCR) | AR_GPIOCR_CR_A(gpio));
49*572ff6f6SMatthew Dillon 
50*572ff6f6SMatthew Dillon 	return AH_TRUE;
51*572ff6f6SMatthew Dillon }
52*572ff6f6SMatthew Dillon 
53*572ff6f6SMatthew Dillon /*
54*572ff6f6SMatthew Dillon  * Configure GPIO Input lines
55*572ff6f6SMatthew Dillon  */
56*572ff6f6SMatthew Dillon HAL_BOOL
ar5212GpioCfgInput(struct ath_hal * ah,uint32_t gpio)57*572ff6f6SMatthew Dillon ar5212GpioCfgInput(struct ath_hal *ah, uint32_t gpio)
58*572ff6f6SMatthew Dillon {
59*572ff6f6SMatthew Dillon 	HALASSERT(gpio < AR_NUM_GPIO);
60*572ff6f6SMatthew Dillon 
61*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_GPIOCR,
62*572ff6f6SMatthew Dillon 		  (OS_REG_READ(ah, AR_GPIOCR) &~ AR_GPIOCR_CR_A(gpio))
63*572ff6f6SMatthew Dillon 		| AR_GPIOCR_CR_N(gpio));
64*572ff6f6SMatthew Dillon 
65*572ff6f6SMatthew Dillon 	return AH_TRUE;
66*572ff6f6SMatthew Dillon }
67*572ff6f6SMatthew Dillon 
68*572ff6f6SMatthew Dillon /*
69*572ff6f6SMatthew Dillon  * Once configured for I/O - set output lines
70*572ff6f6SMatthew Dillon  */
71*572ff6f6SMatthew Dillon HAL_BOOL
ar5212GpioSet(struct ath_hal * ah,uint32_t gpio,uint32_t val)72*572ff6f6SMatthew Dillon ar5212GpioSet(struct ath_hal *ah, uint32_t gpio, uint32_t val)
73*572ff6f6SMatthew Dillon {
74*572ff6f6SMatthew Dillon 	uint32_t reg;
75*572ff6f6SMatthew Dillon 
76*572ff6f6SMatthew Dillon 	HALASSERT(gpio < AR_NUM_GPIO);
77*572ff6f6SMatthew Dillon 
78*572ff6f6SMatthew Dillon 	reg =  OS_REG_READ(ah, AR_GPIODO);
79*572ff6f6SMatthew Dillon 	reg &= ~(1 << gpio);
80*572ff6f6SMatthew Dillon 	reg |= (val&1) << gpio;
81*572ff6f6SMatthew Dillon 
82*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_GPIODO, reg);
83*572ff6f6SMatthew Dillon 	return AH_TRUE;
84*572ff6f6SMatthew Dillon }
85*572ff6f6SMatthew Dillon 
86*572ff6f6SMatthew Dillon /*
87*572ff6f6SMatthew Dillon  * Once configured for I/O - get input lines
88*572ff6f6SMatthew Dillon  */
89*572ff6f6SMatthew Dillon uint32_t
ar5212GpioGet(struct ath_hal * ah,uint32_t gpio)90*572ff6f6SMatthew Dillon ar5212GpioGet(struct ath_hal *ah, uint32_t gpio)
91*572ff6f6SMatthew Dillon {
92*572ff6f6SMatthew Dillon 	if (gpio < AR_NUM_GPIO) {
93*572ff6f6SMatthew Dillon 		uint32_t val = OS_REG_READ(ah, AR_GPIODI);
94*572ff6f6SMatthew Dillon 		val = ((val & AR_GPIOD_MASK) >> gpio) & 0x1;
95*572ff6f6SMatthew Dillon 		return val;
96*572ff6f6SMatthew Dillon 	} else  {
97*572ff6f6SMatthew Dillon 		return 0xffffffff;
98*572ff6f6SMatthew Dillon 	}
99*572ff6f6SMatthew Dillon }
100*572ff6f6SMatthew Dillon 
101*572ff6f6SMatthew Dillon /*
102*572ff6f6SMatthew Dillon  * Set the GPIO Interrupt
103*572ff6f6SMatthew Dillon  */
104*572ff6f6SMatthew Dillon void
ar5212GpioSetIntr(struct ath_hal * ah,u_int gpio,uint32_t ilevel)105*572ff6f6SMatthew Dillon ar5212GpioSetIntr(struct ath_hal *ah, u_int gpio, uint32_t ilevel)
106*572ff6f6SMatthew Dillon {
107*572ff6f6SMatthew Dillon 	uint32_t val;
108*572ff6f6SMatthew Dillon 
109*572ff6f6SMatthew Dillon 	/* XXX bounds check gpio */
110*572ff6f6SMatthew Dillon 	val = OS_REG_READ(ah, AR_GPIOCR);
111*572ff6f6SMatthew Dillon 	val &= ~(AR_GPIOCR_CR_A(gpio) |
112*572ff6f6SMatthew Dillon 		 AR_GPIOCR_INT_MASK | AR_GPIOCR_INT_ENA | AR_GPIOCR_INT_SEL);
113*572ff6f6SMatthew Dillon 	val |= AR_GPIOCR_CR_N(gpio) | AR_GPIOCR_INT(gpio) | AR_GPIOCR_INT_ENA;
114*572ff6f6SMatthew Dillon 	if (ilevel)
115*572ff6f6SMatthew Dillon 		val |= AR_GPIOCR_INT_SELH;	/* interrupt on pin high */
116*572ff6f6SMatthew Dillon 	else
117*572ff6f6SMatthew Dillon 		val |= AR_GPIOCR_INT_SELL;	/* interrupt on pin low */
118*572ff6f6SMatthew Dillon 
119*572ff6f6SMatthew Dillon 	/* Don't need to change anything for low level interrupt. */
120*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_GPIOCR, val);
121*572ff6f6SMatthew Dillon 
122*572ff6f6SMatthew Dillon 	/* Change the interrupt mask. */
123*572ff6f6SMatthew Dillon 	(void) ar5212SetInterrupts(ah, AH5212(ah)->ah_maskReg | HAL_INT_GPIO);
124*572ff6f6SMatthew Dillon }
125