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 #ifdef AH_SUPPORT_AR5312
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 6 /* 6 GPIO pins */
32*572ff6f6SMatthew Dillon #define AR5312_GPIOD_MASK 0x0000002F /* 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
ar5312GpioCfgOutput(struct ath_hal * ah,uint32_t gpio,HAL_GPIO_MUX_TYPE type)38*572ff6f6SMatthew Dillon ar5312GpioCfgOutput(struct ath_hal *ah, uint32_t gpio, HAL_GPIO_MUX_TYPE type)
39*572ff6f6SMatthew Dillon {
40*572ff6f6SMatthew Dillon uint32_t gpioOffset = (AR5312_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+AR5312_GPIOCR,
45*572ff6f6SMatthew Dillon (OS_REG_READ(ah, gpioOffset+AR5312_GPIOCR) &~ AR_GPIOCR_CR_A(gpio))
46*572ff6f6SMatthew Dillon | AR_GPIOCR_CR_A(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
ar5312GpioCfgInput(struct ath_hal * ah,uint32_t gpio)55*572ff6f6SMatthew Dillon ar5312GpioCfgInput(struct ath_hal *ah, uint32_t gpio)
56*572ff6f6SMatthew Dillon {
57*572ff6f6SMatthew Dillon uint32_t gpioOffset = (AR5312_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+AR5312_GPIOCR,
62*572ff6f6SMatthew Dillon (OS_REG_READ(ah, gpioOffset+AR5312_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
ar5312GpioSet(struct ath_hal * ah,uint32_t gpio,uint32_t val)72*572ff6f6SMatthew Dillon ar5312GpioSet(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 = (AR5312_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+AR5312_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+AR5312_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
ar5312GpioGet(struct ath_hal * ah,uint32_t gpio)91*572ff6f6SMatthew Dillon ar5312GpioGet(struct ath_hal *ah, uint32_t gpio)
92*572ff6f6SMatthew Dillon {
93*572ff6f6SMatthew Dillon uint32_t gpioOffset = (AR5312_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+AR5312_GPIODI);
97*572ff6f6SMatthew Dillon val = ((val & AR5312_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
ar5312GpioSetIntr(struct ath_hal * ah,u_int gpio,uint32_t ilevel)108*572ff6f6SMatthew Dillon ar5312GpioSetIntr(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 = (AR5312_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+AR5312_GPIOCR);
115*572ff6f6SMatthew Dillon val &= ~(AR_GPIOCR_CR_A(gpio) |
116*572ff6f6SMatthew Dillon AR_GPIOCR_INT_MASK | AR_GPIOCR_INT_ENA | AR_GPIOCR_INT_SEL);
117*572ff6f6SMatthew Dillon val |= AR_GPIOCR_CR_N(gpio) | AR_GPIOCR_INT(gpio) | AR_GPIOCR_INT_ENA;
118*572ff6f6SMatthew Dillon if (ilevel)
119*572ff6f6SMatthew Dillon val |= AR_GPIOCR_INT_SELH; /* interrupt on pin high */
120*572ff6f6SMatthew Dillon else
121*572ff6f6SMatthew Dillon val |= AR_GPIOCR_INT_SELL; /* interrupt on pin low */
122*572ff6f6SMatthew Dillon
123*572ff6f6SMatthew Dillon /* Don't need to change anything for low level interrupt. */
124*572ff6f6SMatthew Dillon OS_REG_WRITE(ah, gpioOffset+AR5312_GPIOCR, val);
125*572ff6f6SMatthew Dillon
126*572ff6f6SMatthew Dillon /* Change the interrupt mask. */
127*572ff6f6SMatthew Dillon (void) ar5212SetInterrupts(ah, AH5212(ah)->ah_maskReg | HAL_INT_GPIO);
128*572ff6f6SMatthew Dillon }
129*572ff6f6SMatthew Dillon
130*572ff6f6SMatthew Dillon
131*572ff6f6SMatthew Dillon #endif /* AH_SUPPORT_AR5312 */
132