xref: /dflybsd-src/sys/dev/netif/ath/ath_hal/ar5312/ar5315_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 #if (AH_SUPPORT_2316 || AH_SUPPORT_2317)
22*572ff6f6SMatthew Dillon 
23*572ff6f6SMatthew Dillon #include "ah.h"
24*572ff6f6SMatthew Dillon #include "ah_internal.h"
25*572ff6f6SMatthew Dillon #include "ah_devid.h"
26*572ff6f6SMatthew Dillon 
27*572ff6f6SMatthew Dillon #include "ar5312/ar5312.h"
28*572ff6f6SMatthew Dillon #include "ar5312/ar5312reg.h"
29*572ff6f6SMatthew Dillon #include "ar5312/ar5312phy.h"
30*572ff6f6SMatthew Dillon 
31*572ff6f6SMatthew Dillon #define	AR_NUM_GPIO	7		/* 6 GPIO pins */
32*572ff6f6SMatthew Dillon #define	AR5315_GPIOD_MASK	0x0000007F	/* GPIO data reg r/w mask */
33*572ff6f6SMatthew Dillon 
34*572ff6f6SMatthew Dillon /*
35*572ff6f6SMatthew Dillon  * Configure GPIO Output lines
36*572ff6f6SMatthew Dillon  */
37*572ff6f6SMatthew Dillon HAL_BOOL
ar5315GpioCfgOutput(struct ath_hal * ah,uint32_t gpio,HAL_GPIO_MUX_TYPE type)38*572ff6f6SMatthew Dillon ar5315GpioCfgOutput(struct ath_hal *ah, uint32_t gpio, HAL_GPIO_MUX_TYPE type)
39*572ff6f6SMatthew Dillon {
40*572ff6f6SMatthew Dillon 	uint32_t gpioOffset = (AR5315_GPIO_BASE - ((uint32_t) ah->ah_sh));
41*572ff6f6SMatthew Dillon 
42*572ff6f6SMatthew Dillon 	HALASSERT(gpio < AR_NUM_GPIO);
43*572ff6f6SMatthew Dillon 
44*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, gpioOffset+AR5315_GPIODIR,
45*572ff6f6SMatthew Dillon 		  (OS_REG_READ(ah, gpioOffset+AR5315_GPIODIR) &~ AR5315_GPIODIR_M(gpio))
46*572ff6f6SMatthew Dillon 		| AR5315_GPIODIR_O(gpio));
47*572ff6f6SMatthew Dillon 
48*572ff6f6SMatthew Dillon 	return AH_TRUE;
49*572ff6f6SMatthew Dillon }
50*572ff6f6SMatthew Dillon 
51*572ff6f6SMatthew Dillon /*
52*572ff6f6SMatthew Dillon  * Configure GPIO Input lines
53*572ff6f6SMatthew Dillon  */
54*572ff6f6SMatthew Dillon HAL_BOOL
ar5315GpioCfgInput(struct ath_hal * ah,uint32_t gpio)55*572ff6f6SMatthew Dillon ar5315GpioCfgInput(struct ath_hal *ah, uint32_t gpio)
56*572ff6f6SMatthew Dillon {
57*572ff6f6SMatthew Dillon 	uint32_t gpioOffset = (AR5315_GPIO_BASE - ((uint32_t) ah->ah_sh));
58*572ff6f6SMatthew Dillon 
59*572ff6f6SMatthew Dillon 	HALASSERT(gpio < AR_NUM_GPIO);
60*572ff6f6SMatthew Dillon 
61*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, gpioOffset+AR5315_GPIODIR,
62*572ff6f6SMatthew Dillon 		  (OS_REG_READ(ah, gpioOffset+AR5315_GPIODIR) &~ AR5315_GPIODIR_M(gpio))
63*572ff6f6SMatthew Dillon 		| AR5315_GPIODIR_I(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
ar5315GpioSet(struct ath_hal * ah,uint32_t gpio,uint32_t val)72*572ff6f6SMatthew Dillon ar5315GpioSet(struct ath_hal *ah, uint32_t gpio, uint32_t val)
73*572ff6f6SMatthew Dillon {
74*572ff6f6SMatthew Dillon 	uint32_t reg;
75*572ff6f6SMatthew Dillon         uint32_t gpioOffset = (AR5315_GPIO_BASE - ((uint32_t) ah->ah_sh));
76*572ff6f6SMatthew Dillon 
77*572ff6f6SMatthew Dillon 	HALASSERT(gpio < AR_NUM_GPIO);
78*572ff6f6SMatthew Dillon 
79*572ff6f6SMatthew Dillon 	reg =  OS_REG_READ(ah, gpioOffset+AR5315_GPIODO);
80*572ff6f6SMatthew Dillon 	reg &= ~(1 << gpio);
81*572ff6f6SMatthew Dillon 	reg |= (val&1) << gpio;
82*572ff6f6SMatthew Dillon 
83*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, gpioOffset+AR5315_GPIODO, reg);
84*572ff6f6SMatthew Dillon 	return AH_TRUE;
85*572ff6f6SMatthew Dillon }
86*572ff6f6SMatthew Dillon 
87*572ff6f6SMatthew Dillon /*
88*572ff6f6SMatthew Dillon  * Once configured for I/O - get input lines
89*572ff6f6SMatthew Dillon  */
90*572ff6f6SMatthew Dillon uint32_t
ar5315GpioGet(struct ath_hal * ah,uint32_t gpio)91*572ff6f6SMatthew Dillon ar5315GpioGet(struct ath_hal *ah, uint32_t gpio)
92*572ff6f6SMatthew Dillon {
93*572ff6f6SMatthew Dillon 	uint32_t gpioOffset = (AR5315_GPIO_BASE - ((uint32_t) ah->ah_sh));
94*572ff6f6SMatthew Dillon 
95*572ff6f6SMatthew Dillon 	if (gpio < AR_NUM_GPIO) {
96*572ff6f6SMatthew Dillon 		uint32_t val = OS_REG_READ(ah, gpioOffset+AR5315_GPIODI);
97*572ff6f6SMatthew Dillon 		val = ((val & AR5315_GPIOD_MASK) >> gpio) & 0x1;
98*572ff6f6SMatthew Dillon 		return val;
99*572ff6f6SMatthew Dillon 	} else  {
100*572ff6f6SMatthew Dillon 		return 0xffffffff;
101*572ff6f6SMatthew Dillon 	}
102*572ff6f6SMatthew Dillon }
103*572ff6f6SMatthew Dillon 
104*572ff6f6SMatthew Dillon /*
105*572ff6f6SMatthew Dillon  * Set the GPIO Interrupt
106*572ff6f6SMatthew Dillon  */
107*572ff6f6SMatthew Dillon void
ar5315GpioSetIntr(struct ath_hal * ah,u_int gpio,uint32_t ilevel)108*572ff6f6SMatthew Dillon ar5315GpioSetIntr(struct ath_hal *ah, u_int gpio, uint32_t ilevel)
109*572ff6f6SMatthew Dillon {
110*572ff6f6SMatthew Dillon 	uint32_t val;
111*572ff6f6SMatthew Dillon 	uint32_t gpioOffset = (AR5315_GPIO_BASE - ((uint32_t) ah->ah_sh));
112*572ff6f6SMatthew Dillon 
113*572ff6f6SMatthew Dillon 	/* XXX bounds check gpio */
114*572ff6f6SMatthew Dillon 	val = OS_REG_READ(ah, gpioOffset+AR5315_GPIOINT);
115*572ff6f6SMatthew Dillon 	val &= ~(AR5315_GPIOINT_M | AR5315_GPIOINTLVL_M);
116*572ff6f6SMatthew Dillon 	val |= gpio << AR5315_GPIOINT_S;
117*572ff6f6SMatthew Dillon 	if (ilevel)
118*572ff6f6SMatthew Dillon 		val |= 2 << AR5315_GPIOINTLVL_S;	/* interrupt on pin high */
119*572ff6f6SMatthew Dillon 	else
120*572ff6f6SMatthew Dillon 		val |= 1 << AR5315_GPIOINTLVL_S;	/* interrupt on pin low */
121*572ff6f6SMatthew Dillon 
122*572ff6f6SMatthew Dillon 	/* Don't need to change anything for low level interrupt. */
123*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, gpioOffset+AR5315_GPIOINT, val);
124*572ff6f6SMatthew Dillon 
125*572ff6f6SMatthew Dillon 	/* Change the interrupt mask. */
126*572ff6f6SMatthew Dillon 	(void) ar5212SetInterrupts(ah, AH5212(ah)->ah_maskReg | HAL_INT_GPIO);
127*572ff6f6SMatthew Dillon }
128*572ff6f6SMatthew Dillon 
129*572ff6f6SMatthew Dillon 
130*572ff6f6SMatthew Dillon #endif /* AH_SUPPORT_2316 || AH_SUPPORT_2317 */
131