2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
42 #include <machine/stdarg.h>
44 #include <net/ethernet.h> /* XXX for ether_sprintf */
46 #include <dev/ath/ath_hal/ah.h>
49 * WiSoC boards overload the bus tag with information about the
50 * board layout. We must extract the bus space tag from that
51 * indirect structure. For everyone else the tag is passed in
53 * XXX cache indirect ref privately
55 #ifdef AH_SUPPORT_AR5312
57 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
59 #define BUSTAG(ah) ((ah)->ah_st)
62 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
64 extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
66 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
67 extern void *ath_hal_malloc(size_t);
68 extern void ath_hal_free(void *);
70 extern void ath_hal_assert_failed(const char* filename,
71 int lineno, const char* msg);
74 extern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
77 /* NB: put this here instead of the driver to avoid circular references */
78 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
79 SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters");
82 int ath_hal_debug = 0;
83 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug,
84 0, "Atheros HAL debugging printfs");
85 TUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug);
88 MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
91 ath_hal_malloc(size_t size)
93 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
103 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
109 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
113 ath_hal_vprintf(ah, fmt, ap);
118 ath_hal_ether_sprintf(const u_int8_t *mac)
120 return ether_sprintf(mac);
125 /* This must match the definition in ath_hal/ah_debug.h */
126 #define HAL_DEBUG_UNMASKABLE 0xf0000000
128 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
130 if ((mask == HAL_DEBUG_UNMASKABLE) ||
131 (ah->ah_config.ah_debug & mask) ||
132 (ath_hal_debug & mask)) {
135 ath_hal_vprintf(ah, fmt, ap);
139 #undef HAL_DEBUG_UNMASKABLE
140 #endif /* AH_DEBUG */
144 * ALQ register tracing support.
146 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
147 * writes to the file /tmp/ath_hal.log. The file format is a simple
148 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
149 * and then decode the file with the arcode program (that is part of the
150 * HAL). If you start+stop tracing the data will be appended to an
153 * NB: doesn't handle multiple devices properly; only one DEVICE record
154 * is emitted and the different devices are not identified.
157 #include <sys/pcpu.h>
158 #include <dev/ath/ath_hal/ah_decode.h>
160 static struct alq *ath_hal_alq;
161 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
162 static u_int ath_hal_alq_lost; /* count of lost records */
163 static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
165 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
166 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
168 static u_int ath_hal_alq_qsize = 64*1024;
171 ath_hal_setlogging(int enable)
176 error = alq_open(&ath_hal_alq, ath_hal_logfile,
177 curthread->td_ucred, ALQ_DEFAULT_CMODE,
178 sizeof (struct athregrec), ath_hal_alq_qsize);
179 ath_hal_alq_lost = 0;
180 ath_hal_alq_emitdev = 1;
181 printf("ath_hal: logging to %s enabled\n",
185 alq_close(ath_hal_alq);
187 printf("ath_hal: logging disabled\n");
194 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
198 enable = (ath_hal_alq != NULL);
199 error = sysctl_handle_int(oidp, &enable, 0, req);
200 if (error || !req->newptr)
203 return (ath_hal_setlogging(enable));
205 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
206 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
207 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
208 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
209 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
210 &ath_hal_alq_lost, 0, "Register operations not logged");
213 ath_hal_alq_get(struct ath_hal *ah)
217 if (ath_hal_alq_emitdev) {
218 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
220 struct athregrec *r =
221 (struct athregrec *) ale->ae_data;
224 r->val = ah->ah_devid;
225 alq_post(ath_hal_alq, ale);
226 ath_hal_alq_emitdev = 0;
230 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
237 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
239 bus_space_tag_t tag = BUSTAG(ah);
240 bus_space_handle_t h = ah->ah_sh;
243 struct ale *ale = ath_hal_alq_get(ah);
245 struct athregrec *r = (struct athregrec *) ale->ae_data;
249 alq_post(ath_hal_alq, ale);
252 #if _BYTE_ORDER == _BIG_ENDIAN
253 if (OS_REG_UNSWAPPED(reg))
254 bus_space_write_4(tag, h, reg, val);
257 bus_space_write_stream_4(tag, h, reg, val);
261 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
263 bus_space_tag_t tag = BUSTAG(ah);
264 bus_space_handle_t h = ah->ah_sh;
267 #if _BYTE_ORDER == _BIG_ENDIAN
268 if (OS_REG_UNSWAPPED(reg))
269 val = bus_space_read_4(tag, h, reg);
272 val = bus_space_read_stream_4(tag, h, reg);
274 struct ale *ale = ath_hal_alq_get(ah);
276 struct athregrec *r = (struct athregrec *) ale->ae_data;
280 alq_post(ath_hal_alq, ale);
287 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
290 struct ale *ale = ath_hal_alq_get(ah);
292 struct athregrec *r = (struct athregrec *) ale->ae_data;
296 alq_post(ath_hal_alq, ale);
300 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
302 * Memory-mapped device register read/write. These are here
303 * as routines when debugging support is enabled and/or when
304 * explicitly configured to use function calls. The latter is
305 * for architectures that might need to do something before
306 * referencing memory (e.g. remap an i/o window).
308 * NB: see the comments in ah_osdep.h about byte-swapping register
309 * reads and writes to understand what's going on below.
313 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
315 bus_space_tag_t tag = BUSTAG(ah);
316 bus_space_handle_t h = ah->ah_sh;
318 #if _BYTE_ORDER == _BIG_ENDIAN
319 if (OS_REG_UNSWAPPED(reg))
320 bus_space_write_4(tag, h, reg, val);
323 bus_space_write_stream_4(tag, h, reg, val);
327 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
329 bus_space_tag_t tag = BUSTAG(ah);
330 bus_space_handle_t h = ah->ah_sh;
333 #if _BYTE_ORDER == _BIG_ENDIAN
334 if (OS_REG_UNSWAPPED(reg))
335 val = bus_space_read_4(tag, h, reg);
338 val = bus_space_read_stream_4(tag, h, reg);
341 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
345 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
347 printf("Atheros HAL assertion failure: %s: line %u: %s\n",
348 filename, lineno, msg);
349 panic("ath_hal_assert");
351 #endif /* AH_ASSERT */