ah_osdep.c revision 223459
1162413Ssam/*- 2178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 3162413Ssam * All rights reserved. 4162413Ssam * 5162413Ssam * Redistribution and use in source and binary forms, with or without 6162413Ssam * modification, are permitted provided that the following conditions 7162413Ssam * are met: 8162413Ssam * 1. Redistributions of source code must retain the above copyright 9162413Ssam * notice, this list of conditions and the following disclaimer, 10162413Ssam * without modification. 11162413Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12162413Ssam * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13162413Ssam * redistribution must be conditioned upon including a substantially 14162413Ssam * similar Disclaimer requirement for further binary redistribution. 15162413Ssam * 16162413Ssam * NO WARRANTY 17162413Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18162413Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19162413Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20162413Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21162413Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22162413Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23162413Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24162413Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25162413Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26162413Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27162413Ssam * THE POSSIBILITY OF SUCH DAMAGES. 28162413Ssam * 29162413Ssam * $FreeBSD: head/sys/dev/ath/ah_osdep.c 223459 2011-06-23 02:38:36Z adrian $ 30162413Ssam */ 31162413Ssam#include "opt_ah.h" 32162413Ssam 33162413Ssam#include <sys/param.h> 34162413Ssam#include <sys/systm.h> 35162413Ssam#include <sys/kernel.h> 36162413Ssam#include <sys/module.h> 37162413Ssam#include <sys/sysctl.h> 38162413Ssam#include <sys/bus.h> 39162413Ssam#include <sys/malloc.h> 40162413Ssam#include <sys/proc.h> 41162413Ssam 42162413Ssam#include <machine/stdarg.h> 43162413Ssam 44162413Ssam#include <net/ethernet.h> /* XXX for ether_sprintf */ 45162413Ssam 46185522Ssam#include <dev/ath/ath_hal/ah.h> 47162413Ssam 48162413Ssam/* 49162413Ssam * WiSoC boards overload the bus tag with information about the 50162413Ssam * board layout. We must extract the bus space tag from that 51162413Ssam * indirect structure. For everyone else the tag is passed in 52162413Ssam * directly. 53162413Ssam * XXX cache indirect ref privately 54162413Ssam */ 55162413Ssam#ifdef AH_SUPPORT_AR5312 56162413Ssam#define BUSTAG(ah) \ 57162413Ssam ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag) 58162413Ssam#else 59185522Ssam#define BUSTAG(ah) ((ah)->ah_st) 60162413Ssam#endif 61162413Ssam 62162413Ssamextern void ath_hal_printf(struct ath_hal *, const char*, ...) 63162413Ssam __printflike(2,3); 64162413Ssamextern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list) 65162413Ssam __printflike(2, 0); 66162413Ssamextern const char* ath_hal_ether_sprintf(const u_int8_t *mac); 67162413Ssamextern void *ath_hal_malloc(size_t); 68162413Ssamextern void ath_hal_free(void *); 69162413Ssam#ifdef AH_ASSERT 70162413Ssamextern void ath_hal_assert_failed(const char* filename, 71162413Ssam int lineno, const char* msg); 72162413Ssam#endif 73162413Ssam#ifdef AH_DEBUG 74219315Sadrianextern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...); 75162413Ssam#endif /* AH_DEBUG */ 76162413Ssam 77162413Ssam/* NB: put this here instead of the driver to avoid circular references */ 78162413SsamSYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters"); 79162413Ssam 80162413SsamMALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data"); 81162413Ssam 82162413Ssamvoid* 83162413Ssamath_hal_malloc(size_t size) 84162413Ssam{ 85162413Ssam return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); 86162413Ssam} 87162413Ssam 88162413Ssamvoid 89162413Ssamath_hal_free(void* p) 90162413Ssam{ 91196935Ssam free(p, M_ATH_HAL); 92162413Ssam} 93162413Ssam 94162413Ssamvoid 95162413Ssamath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap) 96162413Ssam{ 97162413Ssam vprintf(fmt, ap); 98162413Ssam} 99162413Ssam 100162413Ssamvoid 101162413Ssamath_hal_printf(struct ath_hal *ah, const char* fmt, ...) 102162413Ssam{ 103162413Ssam va_list ap; 104162413Ssam va_start(ap, fmt); 105162413Ssam ath_hal_vprintf(ah, fmt, ap); 106162413Ssam va_end(ap); 107162413Ssam} 108162413Ssam 109162413Ssamconst char* 110162413Ssamath_hal_ether_sprintf(const u_int8_t *mac) 111162413Ssam{ 112162413Ssam return ether_sprintf(mac); 113162413Ssam} 114162413Ssam 115162413Ssam#ifdef AH_DEBUG 116222031Sadrian 117222031Sadrian/* This must match the definition in ath_hal/ah_debug.h */ 118222031Sadrian#define HAL_DEBUG_UNMASKABLE 0xf0000000 119162413Ssamvoid 120219315SadrianDO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...) 121184369Ssam{ 122223459Sadrian if ((mask == HAL_DEBUG_UNMASKABLE) || (ah->ah_config.ah_debug & mask)) { 123184369Ssam __va_list ap; 124184369Ssam va_start(ap, fmt); 125184369Ssam ath_hal_vprintf(ah, fmt, ap); 126184369Ssam va_end(ap); 127184369Ssam } 128184369Ssam} 129222031Sadrian#undef HAL_DEBUG_UNMASKABLE 130162413Ssam#endif /* AH_DEBUG */ 131162413Ssam 132162413Ssam#ifdef AH_DEBUG_ALQ 133162413Ssam/* 134162413Ssam * ALQ register tracing support. 135162413Ssam * 136162413Ssam * Setting hw.ath.hal.alq=1 enables tracing of all register reads and 137162413Ssam * writes to the file /tmp/ath_hal.log. The file format is a simple 138162413Ssam * fixed-size array of records. When done logging set hw.ath.hal.alq=0 139162413Ssam * and then decode the file with the arcode program (that is part of the 140162413Ssam * HAL). If you start+stop tracing the data will be appended to an 141162413Ssam * existing file. 142162413Ssam * 143162413Ssam * NB: doesn't handle multiple devices properly; only one DEVICE record 144162413Ssam * is emitted and the different devices are not identified. 145162413Ssam */ 146162413Ssam#include <sys/alq.h> 147162413Ssam#include <sys/pcpu.h> 148185522Ssam#include <dev/ath/ath_hal/ah_decode.h> 149162413Ssam 150223459SadrianSYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters"); 151223459Sadrian 152162413Ssamstatic struct alq *ath_hal_alq; 153162413Ssamstatic int ath_hal_alq_emitdev; /* need to emit DEVICE record */ 154162413Ssamstatic u_int ath_hal_alq_lost; /* count of lost records */ 155220367Sadrianstatic char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log"; 156220367Sadrian 157220367SadrianSYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW, 158220367Sadrian &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile"); 159220367Sadrian 160162413Ssamstatic u_int ath_hal_alq_qsize = 64*1024; 161162413Ssam 162162413Ssamstatic int 163162413Ssamath_hal_setlogging(int enable) 164162413Ssam{ 165162413Ssam int error; 166162413Ssam 167162413Ssam if (enable) { 168168589Srwatson error = alq_open(&ath_hal_alq, ath_hal_logfile, 169168589Srwatson curthread->td_ucred, ALQ_DEFAULT_CMODE, 170168589Srwatson sizeof (struct athregrec), ath_hal_alq_qsize); 171168589Srwatson ath_hal_alq_lost = 0; 172168589Srwatson ath_hal_alq_emitdev = 1; 173168589Srwatson printf("ath_hal: logging to %s enabled\n", 174168589Srwatson ath_hal_logfile); 175162413Ssam } else { 176162413Ssam if (ath_hal_alq) 177162413Ssam alq_close(ath_hal_alq); 178162413Ssam ath_hal_alq = NULL; 179162413Ssam printf("ath_hal: logging disabled\n"); 180162413Ssam error = 0; 181162413Ssam } 182162413Ssam return (error); 183162413Ssam} 184162413Ssam 185162413Ssamstatic int 186162413Ssamsysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS) 187162413Ssam{ 188162413Ssam int error, enable; 189162413Ssam 190162413Ssam enable = (ath_hal_alq != NULL); 191162413Ssam error = sysctl_handle_int(oidp, &enable, 0, req); 192162413Ssam if (error || !req->newptr) 193162413Ssam return (error); 194162413Ssam else 195162413Ssam return (ath_hal_setlogging(enable)); 196162413Ssam} 197162413SsamSYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW, 198162413Ssam 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging"); 199162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW, 200162413Ssam &ath_hal_alq_qsize, 0, "In-memory log size (#records)"); 201162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW, 202162413Ssam &ath_hal_alq_lost, 0, "Register operations not logged"); 203162413Ssam 204162413Ssamstatic struct ale * 205162413Ssamath_hal_alq_get(struct ath_hal *ah) 206162413Ssam{ 207162413Ssam struct ale *ale; 208162413Ssam 209162413Ssam if (ath_hal_alq_emitdev) { 210162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 211162413Ssam if (ale) { 212162413Ssam struct athregrec *r = 213162413Ssam (struct athregrec *) ale->ae_data; 214162413Ssam r->op = OP_DEVICE; 215162413Ssam r->reg = 0; 216162413Ssam r->val = ah->ah_devid; 217162413Ssam alq_post(ath_hal_alq, ale); 218162413Ssam ath_hal_alq_emitdev = 0; 219162413Ssam } else 220162413Ssam ath_hal_alq_lost++; 221162413Ssam } 222162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 223162413Ssam if (!ale) 224162413Ssam ath_hal_alq_lost++; 225162413Ssam return ale; 226162413Ssam} 227162413Ssam 228162413Ssamvoid 229162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 230162413Ssam{ 231162413Ssam bus_space_tag_t tag = BUSTAG(ah); 232185522Ssam bus_space_handle_t h = ah->ah_sh; 233162413Ssam 234162413Ssam if (ath_hal_alq) { 235162413Ssam struct ale *ale = ath_hal_alq_get(ah); 236162413Ssam if (ale) { 237162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 238162413Ssam r->op = OP_WRITE; 239162413Ssam r->reg = reg; 240162413Ssam r->val = val; 241162413Ssam alq_post(ath_hal_alq, ale); 242162413Ssam } 243162413Ssam } 244162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 245195418Ssam if (OS_REG_UNSWAPPED(reg)) 246162413Ssam bus_space_write_4(tag, h, reg, val); 247162413Ssam else 248162413Ssam#endif 249162413Ssam bus_space_write_stream_4(tag, h, reg, val); 250162413Ssam} 251162413Ssam 252162413Ssamu_int32_t 253162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 254162413Ssam{ 255162413Ssam bus_space_tag_t tag = BUSTAG(ah); 256185522Ssam bus_space_handle_t h = ah->ah_sh; 257162413Ssam u_int32_t val; 258162413Ssam 259162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 260195418Ssam if (OS_REG_UNSWAPPED(reg)) 261162413Ssam val = bus_space_read_4(tag, h, reg); 262162413Ssam else 263162413Ssam#endif 264162413Ssam val = bus_space_read_stream_4(tag, h, reg); 265162413Ssam if (ath_hal_alq) { 266162413Ssam struct ale *ale = ath_hal_alq_get(ah); 267162413Ssam if (ale) { 268162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 269162413Ssam r->op = OP_READ; 270162413Ssam r->reg = reg; 271162413Ssam r->val = val; 272162413Ssam alq_post(ath_hal_alq, ale); 273162413Ssam } 274162413Ssam } 275162413Ssam return val; 276162413Ssam} 277162413Ssam 278162413Ssamvoid 279162413SsamOS_MARK(struct ath_hal *ah, u_int id, u_int32_t v) 280162413Ssam{ 281162413Ssam if (ath_hal_alq) { 282162413Ssam struct ale *ale = ath_hal_alq_get(ah); 283162413Ssam if (ale) { 284162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 285162413Ssam r->op = OP_MARK; 286162413Ssam r->reg = id; 287162413Ssam r->val = v; 288162413Ssam alq_post(ath_hal_alq, ale); 289162413Ssam } 290162413Ssam } 291162413Ssam} 292162413Ssam#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC) 293162413Ssam/* 294162413Ssam * Memory-mapped device register read/write. These are here 295162413Ssam * as routines when debugging support is enabled and/or when 296162413Ssam * explicitly configured to use function calls. The latter is 297162413Ssam * for architectures that might need to do something before 298162413Ssam * referencing memory (e.g. remap an i/o window). 299162413Ssam * 300162413Ssam * NB: see the comments in ah_osdep.h about byte-swapping register 301162413Ssam * reads and writes to understand what's going on below. 302162413Ssam */ 303162413Ssam 304162413Ssamvoid 305162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 306162413Ssam{ 307162413Ssam bus_space_tag_t tag = BUSTAG(ah); 308185522Ssam bus_space_handle_t h = ah->ah_sh; 309162413Ssam 310162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 311195418Ssam if (OS_REG_UNSWAPPED(reg)) 312162413Ssam bus_space_write_4(tag, h, reg, val); 313162413Ssam else 314162413Ssam#endif 315162413Ssam bus_space_write_stream_4(tag, h, reg, val); 316162413Ssam} 317162413Ssam 318162413Ssamu_int32_t 319162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 320162413Ssam{ 321162413Ssam bus_space_tag_t tag = BUSTAG(ah); 322185522Ssam bus_space_handle_t h = ah->ah_sh; 323162413Ssam u_int32_t val; 324162413Ssam 325162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 326195418Ssam if (OS_REG_UNSWAPPED(reg)) 327162413Ssam val = bus_space_read_4(tag, h, reg); 328162413Ssam else 329162413Ssam#endif 330162413Ssam val = bus_space_read_stream_4(tag, h, reg); 331162413Ssam return val; 332162413Ssam} 333162413Ssam#endif /* AH_DEBUG || AH_REGOPS_FUNC */ 334162413Ssam 335162413Ssam#ifdef AH_ASSERT 336162413Ssamvoid 337162413Ssamath_hal_assert_failed(const char* filename, int lineno, const char *msg) 338162413Ssam{ 339162413Ssam printf("Atheros HAL assertion failure: %s: line %u: %s\n", 340162413Ssam filename, lineno, msg); 341162413Ssam panic("ath_hal_assert"); 342162413Ssam} 343162413Ssam#endif /* AH_ASSERT */ 344