1*572ff6f6SMatthew Dillon /*
2*572ff6f6SMatthew Dillon * Copyright (c) 2002-2009 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
25*572ff6f6SMatthew Dillon #include "ar5416/ar5416.h"
26*572ff6f6SMatthew Dillon #include "ar5416/ar5416reg.h"
27*572ff6f6SMatthew Dillon #include "ar5416/ar5416phy.h"
28*572ff6f6SMatthew Dillon
29*572ff6f6SMatthew Dillon #define AR_GPIO_BIT(_gpio) (1 << _gpio)
30*572ff6f6SMatthew Dillon
31*572ff6f6SMatthew Dillon /*
32*572ff6f6SMatthew Dillon * Configure GPIO Output Mux control
33*572ff6f6SMatthew Dillon */
34*572ff6f6SMatthew Dillon static void
cfgOutputMux(struct ath_hal * ah,uint32_t gpio,uint32_t type)35*572ff6f6SMatthew Dillon cfgOutputMux(struct ath_hal *ah, uint32_t gpio, uint32_t type)
36*572ff6f6SMatthew Dillon {
37*572ff6f6SMatthew Dillon int addr;
38*572ff6f6SMatthew Dillon uint32_t gpio_shift, tmp;
39*572ff6f6SMatthew Dillon
40*572ff6f6SMatthew Dillon HALDEBUG(ah, HAL_DEBUG_GPIO, "%s: gpio=%d, type=%d\n",
41*572ff6f6SMatthew Dillon __func__, gpio, type);
42*572ff6f6SMatthew Dillon
43*572ff6f6SMatthew Dillon /* each MUX controls 6 GPIO pins */
44*572ff6f6SMatthew Dillon if (gpio > 11)
45*572ff6f6SMatthew Dillon addr = AR_GPIO_OUTPUT_MUX3;
46*572ff6f6SMatthew Dillon else if (gpio > 5)
47*572ff6f6SMatthew Dillon addr = AR_GPIO_OUTPUT_MUX2;
48*572ff6f6SMatthew Dillon else
49*572ff6f6SMatthew Dillon addr = AR_GPIO_OUTPUT_MUX1;
50*572ff6f6SMatthew Dillon
51*572ff6f6SMatthew Dillon /*
52*572ff6f6SMatthew Dillon * 5 bits per GPIO pin. Bits 0..4 for 1st pin in that mux,
53*572ff6f6SMatthew Dillon * bits 5..9 for 2nd pin, etc.
54*572ff6f6SMatthew Dillon */
55*572ff6f6SMatthew Dillon gpio_shift = (gpio % 6) * 5;
56*572ff6f6SMatthew Dillon
57*572ff6f6SMatthew Dillon /*
58*572ff6f6SMatthew Dillon * From Owl to Merlin 1.0, the value read from MUX1 bit 4 to bit
59*572ff6f6SMatthew Dillon * 9 are wrong. Here is hardware's coding:
60*572ff6f6SMatthew Dillon * PRDATA[4:0] <= gpio_output_mux[0];
61*572ff6f6SMatthew Dillon * PRDATA[9:4] <= gpio_output_mux[1];
62*572ff6f6SMatthew Dillon * <==== Bit 4 is used by both gpio_output_mux[0] [1].
63*572ff6f6SMatthew Dillon * Currently the max value for gpio_output_mux[] is 6. So bit 4
64*572ff6f6SMatthew Dillon * will never be used. So it should be fine that bit 4 won't be
65*572ff6f6SMatthew Dillon * able to recover.
66*572ff6f6SMatthew Dillon */
67*572ff6f6SMatthew Dillon if (AR_SREV_MERLIN_20_OR_LATER(ah) ||
68*572ff6f6SMatthew Dillon (addr != AR_GPIO_OUTPUT_MUX1)) {
69*572ff6f6SMatthew Dillon OS_REG_RMW(ah, addr, (type << gpio_shift),
70*572ff6f6SMatthew Dillon (0x1f << gpio_shift));
71*572ff6f6SMatthew Dillon } else {
72*572ff6f6SMatthew Dillon tmp = OS_REG_READ(ah, addr);
73*572ff6f6SMatthew Dillon tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
74*572ff6f6SMatthew Dillon tmp &= ~(0x1f << gpio_shift);
75*572ff6f6SMatthew Dillon tmp |= type << gpio_shift;
76*572ff6f6SMatthew Dillon OS_REG_WRITE(ah, addr, tmp);
77*572ff6f6SMatthew Dillon }
78*572ff6f6SMatthew Dillon }
79*572ff6f6SMatthew Dillon
80*572ff6f6SMatthew Dillon /*
81*572ff6f6SMatthew Dillon * Configure GPIO Output lines
82*572ff6f6SMatthew Dillon */
83*572ff6f6SMatthew Dillon HAL_BOOL
ar5416GpioCfgOutput(struct ath_hal * ah,uint32_t gpio,HAL_GPIO_MUX_TYPE type)84*572ff6f6SMatthew Dillon ar5416GpioCfgOutput(struct ath_hal *ah, uint32_t gpio, HAL_GPIO_MUX_TYPE type)
85*572ff6f6SMatthew Dillon {
86*572ff6f6SMatthew Dillon uint32_t gpio_shift, reg;
87*572ff6f6SMatthew Dillon
88*572ff6f6SMatthew Dillon #define N(a) (sizeof(a) / sizeof(a[0]))
89*572ff6f6SMatthew Dillon
90*572ff6f6SMatthew Dillon HALASSERT(gpio < AH_PRIVATE(ah)->ah_caps.halNumGpioPins);
91*572ff6f6SMatthew Dillon
92*572ff6f6SMatthew Dillon /*
93*572ff6f6SMatthew Dillon * This table maps the HAL GPIO pins to the actual hardware
94*572ff6f6SMatthew Dillon * values.
95*572ff6f6SMatthew Dillon */
96*572ff6f6SMatthew Dillon static const u_int32_t MuxSignalConversionTable[] = {
97*572ff6f6SMatthew Dillon AR_GPIO_OUTPUT_MUX_AS_OUTPUT,
98*572ff6f6SMatthew Dillon AR_GPIO_OUTPUT_MUX_AS_PCIE_ATTENTION_LED,
99*572ff6f6SMatthew Dillon AR_GPIO_OUTPUT_MUX_AS_PCIE_POWER_LED,
100*572ff6f6SMatthew Dillon AR_GPIO_OUTPUT_MUX_AS_MAC_NETWORK_LED,
101*572ff6f6SMatthew Dillon AR_GPIO_OUTPUT_MUX_AS_PCIE_POWER_LED,
102*572ff6f6SMatthew Dillon AR_GPIO_OUTPUT_MUX_AS_RX_CLEAR_EXTERNAL,
103*572ff6f6SMatthew Dillon AR_GPIO_OUTPUT_MUX_AS_TX_FRAME,
104*572ff6f6SMatthew Dillon };
105*572ff6f6SMatthew Dillon
106*572ff6f6SMatthew Dillon HALDEBUG(ah, HAL_DEBUG_GPIO,
107*572ff6f6SMatthew Dillon "%s: gpio=%d, type=%d\n", __func__, gpio, type);
108*572ff6f6SMatthew Dillon
109*572ff6f6SMatthew Dillon /*
110*572ff6f6SMatthew Dillon * Convert HAL signal type definitions to hardware-specific values.
111*572ff6f6SMatthew Dillon */
112*572ff6f6SMatthew Dillon if (type >= N(MuxSignalConversionTable)) {
113*572ff6f6SMatthew Dillon ath_hal_printf(ah, "%s: mux %d is invalid!\n",
114*572ff6f6SMatthew Dillon __func__,
115*572ff6f6SMatthew Dillon type);
116*572ff6f6SMatthew Dillon return AH_FALSE;
117*572ff6f6SMatthew Dillon }
118*572ff6f6SMatthew Dillon cfgOutputMux(ah, gpio, MuxSignalConversionTable[type]);
119*572ff6f6SMatthew Dillon
120*572ff6f6SMatthew Dillon /* 2 bits per output mode */
121*572ff6f6SMatthew Dillon gpio_shift = gpio << 1;
122*572ff6f6SMatthew Dillon
123*572ff6f6SMatthew Dillon /* Always drive, rather than tristate/drive low/drive high */
124*572ff6f6SMatthew Dillon reg = OS_REG_READ(ah, AR_GPIO_OE_OUT);
125*572ff6f6SMatthew Dillon reg &= ~(AR_GPIO_OE_OUT_DRV << gpio_shift);
126*572ff6f6SMatthew Dillon reg |= AR_GPIO_OE_OUT_DRV_ALL << gpio_shift;
127*572ff6f6SMatthew Dillon OS_REG_WRITE(ah, AR_GPIO_OE_OUT, reg);
128*572ff6f6SMatthew Dillon
129*572ff6f6SMatthew Dillon return AH_TRUE;
130*572ff6f6SMatthew Dillon #undef N
131*572ff6f6SMatthew Dillon }
132*572ff6f6SMatthew Dillon
133*572ff6f6SMatthew Dillon /*
134*572ff6f6SMatthew Dillon * Configure GPIO Input lines
135*572ff6f6SMatthew Dillon */
136*572ff6f6SMatthew Dillon HAL_BOOL
ar5416GpioCfgInput(struct ath_hal * ah,uint32_t gpio)137*572ff6f6SMatthew Dillon ar5416GpioCfgInput(struct ath_hal *ah, uint32_t gpio)
138*572ff6f6SMatthew Dillon {
139*572ff6f6SMatthew Dillon uint32_t gpio_shift, reg;
140*572ff6f6SMatthew Dillon
141*572ff6f6SMatthew Dillon HALASSERT(gpio < AH_PRIVATE(ah)->ah_caps.halNumGpioPins);
142*572ff6f6SMatthew Dillon
143*572ff6f6SMatthew Dillon HALDEBUG(ah, HAL_DEBUG_GPIO, "%s: gpio=%d\n", __func__, gpio);
144*572ff6f6SMatthew Dillon
145*572ff6f6SMatthew Dillon /* TODO: configure input mux for AR5416 */
146*572ff6f6SMatthew Dillon /* If configured as input, set output to tristate */
147*572ff6f6SMatthew Dillon gpio_shift = gpio << 1;
148*572ff6f6SMatthew Dillon
149*572ff6f6SMatthew Dillon reg = OS_REG_READ(ah, AR_GPIO_OE_OUT);
150*572ff6f6SMatthew Dillon reg &= ~(AR_GPIO_OE_OUT_DRV << gpio_shift);
151*572ff6f6SMatthew Dillon reg |= AR_GPIO_OE_OUT_DRV_ALL << gpio_shift;
152*572ff6f6SMatthew Dillon OS_REG_WRITE(ah, AR_GPIO_OE_OUT, reg);
153*572ff6f6SMatthew Dillon
154*572ff6f6SMatthew Dillon return AH_TRUE;
155*572ff6f6SMatthew Dillon }
156*572ff6f6SMatthew Dillon
157*572ff6f6SMatthew Dillon /*
158*572ff6f6SMatthew Dillon * Once configured for I/O - set output lines
159*572ff6f6SMatthew Dillon */
160*572ff6f6SMatthew Dillon HAL_BOOL
ar5416GpioSet(struct ath_hal * ah,uint32_t gpio,uint32_t val)161*572ff6f6SMatthew Dillon ar5416GpioSet(struct ath_hal *ah, uint32_t gpio, uint32_t val)
162*572ff6f6SMatthew Dillon {
163*572ff6f6SMatthew Dillon uint32_t reg;
164*572ff6f6SMatthew Dillon
165*572ff6f6SMatthew Dillon HALASSERT(gpio < AH_PRIVATE(ah)->ah_caps.halNumGpioPins);
166*572ff6f6SMatthew Dillon HALDEBUG(ah, HAL_DEBUG_GPIO,
167*572ff6f6SMatthew Dillon "%s: gpio=%d, val=%d\n", __func__, gpio, val);
168*572ff6f6SMatthew Dillon
169*572ff6f6SMatthew Dillon reg = OS_REG_READ(ah, AR_GPIO_IN_OUT);
170*572ff6f6SMatthew Dillon if (val & 1)
171*572ff6f6SMatthew Dillon reg |= AR_GPIO_BIT(gpio);
172*572ff6f6SMatthew Dillon else
173*572ff6f6SMatthew Dillon reg &= ~AR_GPIO_BIT(gpio);
174*572ff6f6SMatthew Dillon OS_REG_WRITE(ah, AR_GPIO_IN_OUT, reg);
175*572ff6f6SMatthew Dillon return AH_TRUE;
176*572ff6f6SMatthew Dillon }
177*572ff6f6SMatthew Dillon
178*572ff6f6SMatthew Dillon /*
179*572ff6f6SMatthew Dillon * Once configured for I/O - get input lines
180*572ff6f6SMatthew Dillon */
181*572ff6f6SMatthew Dillon uint32_t
ar5416GpioGet(struct ath_hal * ah,uint32_t gpio)182*572ff6f6SMatthew Dillon ar5416GpioGet(struct ath_hal *ah, uint32_t gpio)
183*572ff6f6SMatthew Dillon {
184*572ff6f6SMatthew Dillon uint32_t bits;
185*572ff6f6SMatthew Dillon
186*572ff6f6SMatthew Dillon if (gpio >= AH_PRIVATE(ah)->ah_caps.halNumGpioPins)
187*572ff6f6SMatthew Dillon return 0xffffffff;
188*572ff6f6SMatthew Dillon /*
189*572ff6f6SMatthew Dillon * Read output value for all gpio's, shift it,
190*572ff6f6SMatthew Dillon * and verify whether the specific bit is set.
191*572ff6f6SMatthew Dillon */
192*572ff6f6SMatthew Dillon if (AR_SREV_KIWI_10_OR_LATER(ah))
193*572ff6f6SMatthew Dillon bits = MS(OS_REG_READ(ah, AR_GPIO_IN_OUT), AR9287_GPIO_IN_VAL);
194*572ff6f6SMatthew Dillon if (AR_SREV_KITE_10_OR_LATER(ah))
195*572ff6f6SMatthew Dillon bits = MS(OS_REG_READ(ah, AR_GPIO_IN_OUT), AR9285_GPIO_IN_VAL);
196*572ff6f6SMatthew Dillon else if (AR_SREV_MERLIN_10_OR_LATER(ah))
197*572ff6f6SMatthew Dillon bits = MS(OS_REG_READ(ah, AR_GPIO_IN_OUT), AR928X_GPIO_IN_VAL);
198*572ff6f6SMatthew Dillon else
199*572ff6f6SMatthew Dillon bits = MS(OS_REG_READ(ah, AR_GPIO_IN_OUT), AR_GPIO_IN_VAL);
200*572ff6f6SMatthew Dillon return ((bits & AR_GPIO_BIT(gpio)) != 0);
201*572ff6f6SMatthew Dillon }
202*572ff6f6SMatthew Dillon
203*572ff6f6SMatthew Dillon /*
204*572ff6f6SMatthew Dillon * Set the GPIO Interrupt Sync and Async interrupts are both set/cleared.
205*572ff6f6SMatthew Dillon * Async GPIO interrupts may not be raised when the chip is put to sleep.
206*572ff6f6SMatthew Dillon */
207*572ff6f6SMatthew Dillon void
ar5416GpioSetIntr(struct ath_hal * ah,u_int gpio,uint32_t ilevel)208*572ff6f6SMatthew Dillon ar5416GpioSetIntr(struct ath_hal *ah, u_int gpio, uint32_t ilevel)
209*572ff6f6SMatthew Dillon {
210*572ff6f6SMatthew Dillon uint32_t val, mask;
211*572ff6f6SMatthew Dillon
212*572ff6f6SMatthew Dillon HALASSERT(gpio < AH_PRIVATE(ah)->ah_caps.halNumGpioPins);
213*572ff6f6SMatthew Dillon HALDEBUG(ah, HAL_DEBUG_GPIO,
214*572ff6f6SMatthew Dillon "%s: gpio=%d, ilevel=%d\n", __func__, gpio, ilevel);
215*572ff6f6SMatthew Dillon
216*572ff6f6SMatthew Dillon if (ilevel == HAL_GPIO_INTR_DISABLE) {
217*572ff6f6SMatthew Dillon val = MS(OS_REG_READ(ah, AR_INTR_ASYNC_ENABLE),
218*572ff6f6SMatthew Dillon AR_INTR_ASYNC_ENABLE_GPIO) &~ AR_GPIO_BIT(gpio);
219*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_ASYNC_ENABLE,
220*572ff6f6SMatthew Dillon AR_INTR_ASYNC_ENABLE_GPIO, val);
221*572ff6f6SMatthew Dillon
222*572ff6f6SMatthew Dillon mask = MS(OS_REG_READ(ah, AR_INTR_ASYNC_MASK),
223*572ff6f6SMatthew Dillon AR_INTR_ASYNC_MASK_GPIO) &~ AR_GPIO_BIT(gpio);
224*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_ASYNC_MASK,
225*572ff6f6SMatthew Dillon AR_INTR_ASYNC_MASK_GPIO, mask);
226*572ff6f6SMatthew Dillon
227*572ff6f6SMatthew Dillon /* Clear synchronous GPIO interrupt registers and pending interrupt flag */
228*572ff6f6SMatthew Dillon val = MS(OS_REG_READ(ah, AR_INTR_SYNC_ENABLE),
229*572ff6f6SMatthew Dillon AR_INTR_SYNC_ENABLE_GPIO) &~ AR_GPIO_BIT(gpio);
230*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_SYNC_ENABLE,
231*572ff6f6SMatthew Dillon AR_INTR_SYNC_ENABLE_GPIO, val);
232*572ff6f6SMatthew Dillon
233*572ff6f6SMatthew Dillon mask = MS(OS_REG_READ(ah, AR_INTR_SYNC_MASK),
234*572ff6f6SMatthew Dillon AR_INTR_SYNC_MASK_GPIO) &~ AR_GPIO_BIT(gpio);
235*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_SYNC_MASK,
236*572ff6f6SMatthew Dillon AR_INTR_SYNC_MASK_GPIO, mask);
237*572ff6f6SMatthew Dillon
238*572ff6f6SMatthew Dillon val = MS(OS_REG_READ(ah, AR_INTR_SYNC_CAUSE),
239*572ff6f6SMatthew Dillon AR_INTR_SYNC_ENABLE_GPIO) | AR_GPIO_BIT(gpio);
240*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_SYNC_CAUSE,
241*572ff6f6SMatthew Dillon AR_INTR_SYNC_ENABLE_GPIO, val);
242*572ff6f6SMatthew Dillon } else {
243*572ff6f6SMatthew Dillon val = MS(OS_REG_READ(ah, AR_GPIO_INTR_POL),
244*572ff6f6SMatthew Dillon AR_GPIO_INTR_POL_VAL);
245*572ff6f6SMatthew Dillon if (ilevel == HAL_GPIO_INTR_HIGH) {
246*572ff6f6SMatthew Dillon /* 0 == interrupt on pin high */
247*572ff6f6SMatthew Dillon val &= ~AR_GPIO_BIT(gpio);
248*572ff6f6SMatthew Dillon } else if (ilevel == HAL_GPIO_INTR_LOW) {
249*572ff6f6SMatthew Dillon /* 1 == interrupt on pin low */
250*572ff6f6SMatthew Dillon val |= AR_GPIO_BIT(gpio);
251*572ff6f6SMatthew Dillon }
252*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_GPIO_INTR_POL,
253*572ff6f6SMatthew Dillon AR_GPIO_INTR_POL_VAL, val);
254*572ff6f6SMatthew Dillon
255*572ff6f6SMatthew Dillon /* Change the interrupt mask. */
256*572ff6f6SMatthew Dillon val = MS(OS_REG_READ(ah, AR_INTR_ASYNC_ENABLE),
257*572ff6f6SMatthew Dillon AR_INTR_ASYNC_ENABLE_GPIO) | AR_GPIO_BIT(gpio);
258*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_ASYNC_ENABLE,
259*572ff6f6SMatthew Dillon AR_INTR_ASYNC_ENABLE_GPIO, val);
260*572ff6f6SMatthew Dillon
261*572ff6f6SMatthew Dillon mask = MS(OS_REG_READ(ah, AR_INTR_ASYNC_MASK),
262*572ff6f6SMatthew Dillon AR_INTR_ASYNC_MASK_GPIO) | AR_GPIO_BIT(gpio);
263*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_ASYNC_MASK,
264*572ff6f6SMatthew Dillon AR_INTR_ASYNC_MASK_GPIO, mask);
265*572ff6f6SMatthew Dillon
266*572ff6f6SMatthew Dillon /* Set synchronous GPIO interrupt registers as well */
267*572ff6f6SMatthew Dillon val = MS(OS_REG_READ(ah, AR_INTR_SYNC_ENABLE),
268*572ff6f6SMatthew Dillon AR_INTR_SYNC_ENABLE_GPIO) | AR_GPIO_BIT(gpio);
269*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_SYNC_ENABLE,
270*572ff6f6SMatthew Dillon AR_INTR_SYNC_ENABLE_GPIO, val);
271*572ff6f6SMatthew Dillon
272*572ff6f6SMatthew Dillon mask = MS(OS_REG_READ(ah, AR_INTR_SYNC_MASK),
273*572ff6f6SMatthew Dillon AR_INTR_SYNC_MASK_GPIO) | AR_GPIO_BIT(gpio);
274*572ff6f6SMatthew Dillon OS_REG_RMW_FIELD(ah, AR_INTR_SYNC_MASK,
275*572ff6f6SMatthew Dillon AR_INTR_SYNC_MASK_GPIO, mask);
276*572ff6f6SMatthew Dillon }
277*572ff6f6SMatthew Dillon AH5416(ah)->ah_gpioMask = mask; /* for ar5416SetInterrupts */
278*572ff6f6SMatthew Dillon }
279