2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
45 #include <sys/mutex.h>
48 #include <machine/stdarg.h>
50 #include <net/ethernet.h> /* XXX for ether_sprintf */
52 #include <dev/ath/ath_hal/ah.h>
53 #include <dev/ath/ath_hal/ah_debug.h>
56 * WiSoC boards overload the bus tag with information about the
57 * board layout. We must extract the bus space tag from that
58 * indirect structure. For everyone else the tag is passed in
60 * XXX cache indirect ref privately
62 #ifdef AH_SUPPORT_AR5312
64 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
66 #define BUSTAG(ah) ((ah)->ah_st)
70 * This lock is used to seralise register access for chips which have
71 * problems w/ SMP CPUs issuing concurrent PCI transactions.
73 * XXX This is a global lock for now; it should be pushed to
74 * a per-device lock in some platform-independent fashion.
76 struct mtx ah_regser_mtx;
77 MTX_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex",
80 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
82 extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
84 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
85 extern void *ath_hal_malloc(size_t);
86 extern void ath_hal_free(void *);
88 extern void ath_hal_assert_failed(const char* filename,
89 int lineno, const char* msg);
92 extern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
95 /* NB: put this here instead of the driver to avoid circular references */
96 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
97 static SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0,
98 "Atheros HAL parameters");
101 int ath_hal_debug = 0;
102 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RWTUN, &ath_hal_debug,
103 0, "Atheros HAL debugging printfs");
104 #endif /* AH_DEBUG */
106 static MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
109 ath_hal_malloc(size_t size)
111 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
115 ath_hal_free(void* p)
121 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
127 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
131 ath_hal_vprintf(ah, fmt, ap);
136 ath_hal_ether_sprintf(const u_int8_t *mac)
138 return ether_sprintf(mac);
144 * XXX This is highly relevant only for the AR5416 and later
145 * PCI/PCIe NICs. It'll need adjustment for other hardware
149 ath_hal_reg_whilst_asleep(struct ath_hal *ah, uint32_t reg)
152 if (reg >= 0x4000 && reg < 0x5000)
154 if (reg >= 0x6000 && reg < 0x7000)
156 if (reg >= 0x7000 && reg < 0x8000)
162 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
164 if ((mask == HAL_DEBUG_UNMASKABLE) ||
165 (ah != NULL && ah->ah_config.ah_debug & mask) ||
166 (ath_hal_debug & mask)) {
169 ath_hal_vprintf(ah, fmt, ap);
173 #undef HAL_DEBUG_UNMASKABLE
174 #endif /* AH_DEBUG */
178 * ALQ register tracing support.
180 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
181 * writes to the file /tmp/ath_hal.log. The file format is a simple
182 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
183 * and then decode the file with the arcode program (that is part of the
184 * HAL). If you start+stop tracing the data will be appended to an
187 * NB: doesn't handle multiple devices properly; only one DEVICE record
188 * is emitted and the different devices are not identified.
191 #include <sys/pcpu.h>
192 #include <dev/ath/ath_hal/ah_decode.h>
194 static struct alq *ath_hal_alq;
195 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
196 static u_int ath_hal_alq_lost; /* count of lost records */
197 static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
199 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
200 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
202 static u_int ath_hal_alq_qsize = 64*1024;
205 ath_hal_setlogging(int enable)
210 error = alq_open(&ath_hal_alq, ath_hal_logfile,
211 curthread->td_ucred, ALQ_DEFAULT_CMODE,
212 sizeof (struct athregrec), ath_hal_alq_qsize);
213 ath_hal_alq_lost = 0;
214 ath_hal_alq_emitdev = 1;
215 printf("ath_hal: logging to %s enabled\n",
219 alq_close(ath_hal_alq);
221 printf("ath_hal: logging disabled\n");
228 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
232 enable = (ath_hal_alq != NULL);
233 error = sysctl_handle_int(oidp, &enable, 0, req);
234 if (error || !req->newptr)
237 return (ath_hal_setlogging(enable));
239 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
240 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
241 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
242 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
243 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
244 &ath_hal_alq_lost, 0, "Register operations not logged");
247 ath_hal_alq_get(struct ath_hal *ah)
251 if (ath_hal_alq_emitdev) {
252 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
254 struct athregrec *r =
255 (struct athregrec *) ale->ae_data;
258 r->val = ah->ah_devid;
259 alq_post(ath_hal_alq, ale);
260 ath_hal_alq_emitdev = 0;
264 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
271 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
273 bus_space_tag_t tag = BUSTAG(ah);
274 bus_space_handle_t h = ah->ah_sh;
277 /* Debug - complain if we haven't fully waken things up */
278 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
279 ah->ah_powerMode != HAL_PM_AWAKE) {
280 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
281 __func__, reg, val, ah->ah_powerMode);
286 struct ale *ale = ath_hal_alq_get(ah);
288 struct athregrec *r = (struct athregrec *) ale->ae_data;
289 r->threadid = curthread->td_tid;
293 alq_post(ath_hal_alq, ale);
296 if (ah->ah_config.ah_serialise_reg_war)
297 mtx_lock_spin(&ah_regser_mtx);
298 bus_space_write_4(tag, h, reg, val);
299 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_WRITE);
300 if (ah->ah_config.ah_serialise_reg_war)
301 mtx_unlock_spin(&ah_regser_mtx);
305 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
307 bus_space_tag_t tag = BUSTAG(ah);
308 bus_space_handle_t h = ah->ah_sh;
312 /* Debug - complain if we haven't fully waken things up */
313 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
314 ah->ah_powerMode != HAL_PM_AWAKE) {
315 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
316 __func__, reg, ah->ah_powerMode);
320 if (ah->ah_config.ah_serialise_reg_war)
321 mtx_lock_spin(&ah_regser_mtx);
322 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_READ);
323 val = bus_space_read_4(tag, h, reg);
324 if (ah->ah_config.ah_serialise_reg_war)
325 mtx_unlock_spin(&ah_regser_mtx);
327 struct ale *ale = ath_hal_alq_get(ah);
329 struct athregrec *r = (struct athregrec *) ale->ae_data;
330 r->threadid = curthread->td_tid;
334 alq_post(ath_hal_alq, ale);
341 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
344 struct ale *ale = ath_hal_alq_get(ah);
346 struct athregrec *r = (struct athregrec *) ale->ae_data;
347 r->threadid = curthread->td_tid;
351 alq_post(ath_hal_alq, ale);
355 #else /* AH_DEBUG_ALQ */
358 * Memory-mapped device register read/write. These are here
359 * as routines when debugging support is enabled and/or when
360 * explicitly configured to use function calls. The latter is
361 * for architectures that might need to do something before
362 * referencing memory (e.g. remap an i/o window).
364 * NB: see the comments in ah_osdep.h about byte-swapping register
365 * reads and writes to understand what's going on below.
369 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
371 bus_space_tag_t tag = BUSTAG(ah);
372 bus_space_handle_t h = ah->ah_sh;
375 /* Debug - complain if we haven't fully waken things up */
376 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
377 ah->ah_powerMode != HAL_PM_AWAKE) {
378 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
379 __func__, reg, val, ah->ah_powerMode);
383 if (ah->ah_config.ah_serialise_reg_war)
384 mtx_lock_spin(&ah_regser_mtx);
385 bus_space_write_4(tag, h, reg, val);
386 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_WRITE);
387 if (ah->ah_config.ah_serialise_reg_war)
388 mtx_unlock_spin(&ah_regser_mtx);
392 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
394 bus_space_tag_t tag = BUSTAG(ah);
395 bus_space_handle_t h = ah->ah_sh;
399 /* Debug - complain if we haven't fully waken things up */
400 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
401 ah->ah_powerMode != HAL_PM_AWAKE) {
402 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
403 __func__, reg, ah->ah_powerMode);
407 if (ah->ah_config.ah_serialise_reg_war)
408 mtx_lock_spin(&ah_regser_mtx);
409 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_READ);
410 val = bus_space_read_4(tag, h, reg);
411 if (ah->ah_config.ah_serialise_reg_war)
412 mtx_unlock_spin(&ah_regser_mtx);
415 #endif /* AH_DEBUG_ALQ */
419 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
421 printf("Atheros HAL assertion failure: %s: line %u: %s\n",
422 filename, lineno, msg);
423 panic("ath_hal_assert");
425 #endif /* AH_ASSERT */
428 ath_hal_modevent(module_t mod __unused, int type, void *data __unused)
434 printf("[ath_hal] loaded\n");
438 printf("[ath_hal] unloaded\n");
452 DEV_MODULE(ath_hal, ath_hal_modevent, NULL);
453 MODULE_VERSION(ath_hal, 1);
454 #if defined(AH_DEBUG_ALQ)
455 MODULE_DEPEND(ath_hal, alq, 1, 1, 1);