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>
43 #include <sys/mutex.h>
45 #include <machine/stdarg.h>
47 #include <net/ethernet.h> /* XXX for ether_sprintf */
49 #include <dev/ath/ath_hal/ah.h>
50 #include <dev/ath/ath_hal/ah_debug.h>
53 * WiSoC boards overload the bus tag with information about the
54 * board layout. We must extract the bus space tag from that
55 * indirect structure. For everyone else the tag is passed in
57 * XXX cache indirect ref privately
59 #ifdef AH_SUPPORT_AR5312
61 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
63 #define BUSTAG(ah) ((ah)->ah_st)
67 * This lock is used to seralise register access for chips which have
68 * problems w/ SMP CPUs issuing concurrent PCI transactions.
70 * XXX This is a global lock for now; it should be pushed to
71 * a per-device lock in some platform-independent fashion.
73 struct mtx ah_regser_mtx;
74 MTX_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex",
77 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
79 extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
81 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
82 extern void *ath_hal_malloc(size_t);
83 extern void ath_hal_free(void *);
85 extern void ath_hal_assert_failed(const char* filename,
86 int lineno, const char* msg);
89 extern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
92 /* NB: put this here instead of the driver to avoid circular references */
93 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
94 static SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0,
95 "Atheros HAL parameters");
98 int ath_hal_debug = 0;
99 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RWTUN, &ath_hal_debug,
100 0, "Atheros HAL debugging printfs");
101 #endif /* AH_DEBUG */
103 static MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
106 ath_hal_malloc(size_t size)
108 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
112 ath_hal_free(void* p)
118 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
124 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
128 ath_hal_vprintf(ah, fmt, ap);
133 ath_hal_ether_sprintf(const u_int8_t *mac)
135 return ether_sprintf(mac);
141 * XXX This is highly relevant only for the AR5416 and later
142 * PCI/PCIe NICs. It'll need adjustment for other hardware
146 ath_hal_reg_whilst_asleep(struct ath_hal *ah, uint32_t reg)
149 if (reg >= 0x4000 && reg < 0x5000)
151 if (reg >= 0x6000 && reg < 0x7000)
153 if (reg >= 0x7000 && reg < 0x8000)
159 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
161 if ((mask == HAL_DEBUG_UNMASKABLE) ||
162 (ah != NULL && ah->ah_config.ah_debug & mask) ||
163 (ath_hal_debug & mask)) {
166 ath_hal_vprintf(ah, fmt, ap);
170 #undef HAL_DEBUG_UNMASKABLE
171 #endif /* AH_DEBUG */
175 * ALQ register tracing support.
177 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
178 * writes to the file /tmp/ath_hal.log. The file format is a simple
179 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
180 * and then decode the file with the arcode program (that is part of the
181 * HAL). If you start+stop tracing the data will be appended to an
184 * NB: doesn't handle multiple devices properly; only one DEVICE record
185 * is emitted and the different devices are not identified.
188 #include <sys/pcpu.h>
189 #include <dev/ath/ath_hal/ah_decode.h>
191 static struct alq *ath_hal_alq;
192 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
193 static u_int ath_hal_alq_lost; /* count of lost records */
194 static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
196 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
197 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
199 static u_int ath_hal_alq_qsize = 64*1024;
202 ath_hal_setlogging(int enable)
207 error = alq_open(&ath_hal_alq, ath_hal_logfile,
208 curthread->td_ucred, ALQ_DEFAULT_CMODE,
209 sizeof (struct athregrec), ath_hal_alq_qsize);
210 ath_hal_alq_lost = 0;
211 ath_hal_alq_emitdev = 1;
212 printf("ath_hal: logging to %s enabled\n",
216 alq_close(ath_hal_alq);
218 printf("ath_hal: logging disabled\n");
225 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
229 enable = (ath_hal_alq != NULL);
230 error = sysctl_handle_int(oidp, &enable, 0, req);
231 if (error || !req->newptr)
234 return (ath_hal_setlogging(enable));
236 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
237 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
238 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
239 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
240 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
241 &ath_hal_alq_lost, 0, "Register operations not logged");
244 ath_hal_alq_get(struct ath_hal *ah)
248 if (ath_hal_alq_emitdev) {
249 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
251 struct athregrec *r =
252 (struct athregrec *) ale->ae_data;
255 r->val = ah->ah_devid;
256 alq_post(ath_hal_alq, ale);
257 ath_hal_alq_emitdev = 0;
261 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
268 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
270 bus_space_tag_t tag = BUSTAG(ah);
271 bus_space_handle_t h = ah->ah_sh;
273 /* Debug - complain if we haven't fully waken things up */
274 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
275 ah->ah_powerMode != HAL_PM_AWAKE) {
276 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
277 __func__, reg, val, ah->ah_powerMode);
281 struct ale *ale = ath_hal_alq_get(ah);
283 struct athregrec *r = (struct athregrec *) ale->ae_data;
284 r->threadid = curthread->td_tid;
288 alq_post(ath_hal_alq, ale);
291 if (ah->ah_config.ah_serialise_reg_war)
292 mtx_lock_spin(&ah_regser_mtx);
293 bus_space_write_4(tag, h, reg, val);
294 if (ah->ah_config.ah_serialise_reg_war)
295 mtx_unlock_spin(&ah_regser_mtx);
299 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
301 bus_space_tag_t tag = BUSTAG(ah);
302 bus_space_handle_t h = ah->ah_sh;
305 /* Debug - complain if we haven't fully waken things up */
306 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
307 ah->ah_powerMode != HAL_PM_AWAKE) {
308 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
309 __func__, reg, ah->ah_powerMode);
312 if (ah->ah_config.ah_serialise_reg_war)
313 mtx_lock_spin(&ah_regser_mtx);
314 val = bus_space_read_4(tag, h, reg);
315 if (ah->ah_config.ah_serialise_reg_war)
316 mtx_unlock_spin(&ah_regser_mtx);
318 struct ale *ale = ath_hal_alq_get(ah);
320 struct athregrec *r = (struct athregrec *) ale->ae_data;
321 r->threadid = curthread->td_tid;
325 alq_post(ath_hal_alq, ale);
332 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
335 struct ale *ale = ath_hal_alq_get(ah);
337 struct athregrec *r = (struct athregrec *) ale->ae_data;
338 r->threadid = curthread->td_tid;
342 alq_post(ath_hal_alq, ale);
346 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
348 * Memory-mapped device register read/write. These are here
349 * as routines when debugging support is enabled and/or when
350 * explicitly configured to use function calls. The latter is
351 * for architectures that might need to do something before
352 * referencing memory (e.g. remap an i/o window).
354 * NB: see the comments in ah_osdep.h about byte-swapping register
355 * reads and writes to understand what's going on below.
359 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
361 bus_space_tag_t tag = BUSTAG(ah);
362 bus_space_handle_t h = ah->ah_sh;
364 /* Debug - complain if we haven't fully waken things up */
365 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
366 ah->ah_powerMode != HAL_PM_AWAKE) {
367 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
368 __func__, reg, val, ah->ah_powerMode);
371 if (ah->ah_config.ah_serialise_reg_war)
372 mtx_lock_spin(&ah_regser_mtx);
373 bus_space_write_4(tag, h, reg, val);
374 if (ah->ah_config.ah_serialise_reg_war)
375 mtx_unlock_spin(&ah_regser_mtx);
379 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
381 bus_space_tag_t tag = BUSTAG(ah);
382 bus_space_handle_t h = ah->ah_sh;
385 /* Debug - complain if we haven't fully waken things up */
386 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
387 ah->ah_powerMode != HAL_PM_AWAKE) {
388 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
389 __func__, reg, ah->ah_powerMode);
392 if (ah->ah_config.ah_serialise_reg_war)
393 mtx_lock_spin(&ah_regser_mtx);
394 val = bus_space_read_4(tag, h, reg);
395 if (ah->ah_config.ah_serialise_reg_war)
396 mtx_unlock_spin(&ah_regser_mtx);
399 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
403 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
405 printf("Atheros HAL assertion failure: %s: line %u: %s\n",
406 filename, lineno, msg);
407 panic("ath_hal_assert");
409 #endif /* AH_ASSERT */