Path: blob/main/sys/dev/ath/ath_hal/ah_eeprom_9287.c
39537 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2008 Sam Leffler, Errno Consulting4* Copyright (c) 2010 Atheros Communications, Inc.5*6* Permission to use, copy, modify, and/or distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*/18#include "opt_ah.h"1920#include "ah.h"21#include "ah_internal.h"22#include "ah_eeprom_v14.h"23#include "ah_eeprom_9287.h"2425static HAL_STATUS26v9287EepromGet(struct ath_hal *ah, int param, void *val)27{28#define CHAN_A_IDX 029#define CHAN_B_IDX 130#define IS_VERS(op, v) ((pBase->version & AR5416_EEP_VER_MINOR_MASK) op (v))31HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;32const MODAL_EEP_9287_HEADER *pModal = &ee->ee_base.modalHeader;33const BASE_EEP_9287_HEADER *pBase = &ee->ee_base.baseEepHeader;34uint32_t sum;35uint8_t *macaddr;36int i;3738switch (param) {39case AR_EEP_NFTHRESH_2:40*(int16_t *)val = pModal->noiseFloorThreshCh[0];41return HAL_OK;42case AR_EEP_MACADDR: /* Get MAC Address */43sum = 0;44macaddr = val;45for (i = 0; i < 6; i++) {46macaddr[i] = pBase->macAddr[i];47sum += pBase->macAddr[i];48}49if (sum == 0 || sum == 0xffff*3) {50HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",51__func__, ath_hal_ether_sprintf(macaddr));52return HAL_EEBADMAC;53}54return HAL_OK;55case AR_EEP_REGDMN_0:56return pBase->regDmn[0];57case AR_EEP_REGDMN_1:58return pBase->regDmn[1];59case AR_EEP_OPCAP:60return pBase->deviceCap;61case AR_EEP_OPMODE:62return pBase->opCapFlags;63case AR_EEP_RFSILENT:64return pBase->rfSilent;65case AR_EEP_TXMASK:66return pBase->txMask;67case AR_EEP_RXMASK:68return pBase->rxMask;69case AR_EEP_OL_PWRCTRL:70HALASSERT(val == AH_NULL);71return pBase->openLoopPwrCntl ? HAL_OK : HAL_EIO;72case AR_EEP_AMODE:73return HAL_EIO; /* no 5GHz for Kiwi */74case AR_EEP_BMODE:75case AR_EEP_GMODE:76HALASSERT(val == AH_NULL);77return pBase->opCapFlags & AR5416_OPFLAGS_11G ?78HAL_OK : HAL_EIO;79case AR_EEP_32KHZCRYSTAL:80case AR_EEP_COMPRESS:81case AR_EEP_FASTFRAME: /* XXX policy decision, h/w can do it */82case AR_EEP_WRITEPROTECT: /* NB: no write protect bit */83HALASSERT(val == AH_NULL);84/* fall thru... */85case AR_EEP_MAXQCU: /* NB: not in opCapFlags */86case AR_EEP_KCENTRIES: /* NB: not in opCapFlags */87return HAL_EIO;88case AR_EEP_AES:89case AR_EEP_BURST:90case AR_EEP_RFKILL:91case AR_EEP_TURBO5DISABLE:92case AR_EEP_TURBO2DISABLE:93HALASSERT(val == AH_NULL);94return HAL_OK;95case AR_EEP_ANTGAINMAX_2:96*(int8_t *) val = ee->ee_antennaGainMax[1];97return HAL_OK;98case AR_EEP_PWR_TABLE_OFFSET:99*(int8_t *) val = pBase->pwrTableOffset;100return HAL_OK;101case AR_EEP_TEMPSENSE_SLOPE:102if (IS_VERS(>=, AR9287_EEP_MINOR_VER_2))103*(int8_t *)val = pBase->tempSensSlope;104else105*(int8_t *)val = 0;106return HAL_OK;107case AR_EEP_TEMPSENSE_SLOPE_PAL_ON:108if (IS_VERS(>=, AR9287_EEP_MINOR_VER_3))109*(int8_t *)val = pBase->tempSensSlopePalOn;110else111*(int8_t *)val = 0;112return HAL_OK;113default:114HALASSERT(0);115return HAL_EINVAL;116}117#undef IS_VERS118#undef CHAN_A_IDX119#undef CHAN_B_IDX120}121122static HAL_STATUS123v9287EepromSet(struct ath_hal *ah, int param, int v)124{125HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;126127switch (param) {128case AR_EEP_ANTGAINMAX_2:129ee->ee_antennaGainMax[1] = (int8_t) v;130return HAL_OK;131default:132return HAL_EINVAL;133}134}135136static HAL_BOOL137v9287EepromDiag(struct ath_hal *ah, int request,138const void *args, uint32_t argsize, void **result, uint32_t *resultsize)139{140HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;141142switch (request) {143case HAL_DIAG_EEPROM:144*result = ee;145*resultsize = sizeof(HAL_EEPROM_9287);146return AH_TRUE;147}148return AH_FALSE;149}150151/* Do structure specific swaps if Eeprom format is non native to host */152static void153eepromSwap(HAL_EEPROM_9287 *ee)154{155uint32_t integer, i;156uint16_t word;157MODAL_EEP_9287_HEADER *pModal;158159/* convert Base Eep header */160word = __bswap16(ee->ee_base.baseEepHeader.length);161ee->ee_base.baseEepHeader.length = word;162163word = __bswap16(ee->ee_base.baseEepHeader.checksum);164ee->ee_base.baseEepHeader.checksum = word;165166word = __bswap16(ee->ee_base.baseEepHeader.version);167ee->ee_base.baseEepHeader.version = word;168169word = __bswap16(ee->ee_base.baseEepHeader.regDmn[0]);170ee->ee_base.baseEepHeader.regDmn[0] = word;171172word = __bswap16(ee->ee_base.baseEepHeader.regDmn[1]);173ee->ee_base.baseEepHeader.regDmn[1] = word;174175word = __bswap16(ee->ee_base.baseEepHeader.rfSilent);176ee->ee_base.baseEepHeader.rfSilent = word;177178word = __bswap16(ee->ee_base.baseEepHeader.blueToothOptions);179ee->ee_base.baseEepHeader.blueToothOptions = word;180181word = __bswap16(ee->ee_base.baseEepHeader.deviceCap);182ee->ee_base.baseEepHeader.deviceCap = word;183184/* convert Modal Eep header */185186/* only 2.4ghz here; so only one modal header entry */187pModal = &ee->ee_base.modalHeader;188189/* XXX linux/ah_osdep.h only defines __bswap32 for BE */190integer = __bswap32(pModal->antCtrlCommon);191pModal->antCtrlCommon = integer;192193for (i = 0; i < AR9287_MAX_CHAINS; i++) {194integer = __bswap32(pModal->antCtrlChain[i]);195pModal->antCtrlChain[i] = integer;196}197for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {198word = __bswap16(pModal->spurChans[i].spurChan);199pModal->spurChans[i].spurChan = word;200}201}202203static uint16_t204v9287EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)205{206HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;207208HALASSERT(is2GHz == AH_TRUE);209if (is2GHz != AH_TRUE)210return 0; /* XXX ? */211212HALASSERT(0 <= ix && ix < AR5416_EEPROM_MODAL_SPURS);213return ee->ee_base.modalHeader.spurChans[ix].spurChan;214}215216/**************************************************************************217* fbin2freq218*219* Get channel value from binary representation held in eeprom220* RETURNS: the frequency in MHz221*/222static uint16_t223fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)224{225/*226* Reserved value 0xFF provides an empty definition both as227* an fbin and as a frequency - do not convert228*/229if (fbin == AR5416_BCHAN_UNUSED)230return fbin;231return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));232}233234/*235* Copy EEPROM Conformance Testing Limits contents236* into the allocated space237*/238/* USE CTLS from chain zero */239#define CTL_CHAIN 0240241static void242v9287EepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_9287 *ee)243{244RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;245int i, j;246247HALASSERT(AR9287_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);248249for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR9287_NUM_CTLS; i++) {250for (j = 0; j < NUM_EDGES; j ++) {251/* XXX Confirm this is the right thing to do when an invalid channel is stored */252if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {253rep[j].rdEdge = 0;254rep[j].twice_rdEdgePower = 0;255rep[j].flag = 0;256} else {257rep[j].rdEdge = fbin2freq(258ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,259(ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);260rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);261rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;262}263}264rep += NUM_EDGES;265}266ee->ee_numCtls = i;267HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,268"%s Numctls = %u\n",__func__,i);269}270271/*272* Reclaim any EEPROM-related storage.273*/274static void275v9287EepromDetach(struct ath_hal *ah)276{277HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;278279ath_hal_free(ee);280AH_PRIVATE(ah)->ah_eeprom = AH_NULL;281}282283#define owl_get_eep_ver(_ee) \284(((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)285#define owl_get_eep_rev(_ee) \286(((_ee)->ee_base.baseEepHeader.version) & 0xFFF)287288HAL_STATUS289ath_hal_9287EepromAttach(struct ath_hal *ah)290{291#define NW(a) (sizeof(a) / sizeof(uint16_t))292HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;293uint16_t *eep_data, magic;294HAL_BOOL need_swap;295u_int w, off, len;296uint32_t sum;297298HALASSERT(ee == AH_NULL);299300/*301* Don't check magic if we're supplied with an EEPROM block,302* typically this is from Howl but it may also be from later303* boards w/ an embedded WMAC.304*/305if (ah->ah_eepromdata == NULL) {306if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {307HALDEBUG(ah, HAL_DEBUG_ANY,308"%s Error reading Eeprom MAGIC\n", __func__);309return HAL_EEREAD;310}311HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",312__func__, magic);313if (magic != AR5416_EEPROM_MAGIC) {314HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");315return HAL_EEMAGIC;316}317}318319ee = ath_hal_malloc(sizeof(HAL_EEPROM_9287));320if (ee == AH_NULL) {321/* XXX message */322return HAL_ENOMEM;323}324325eep_data = (uint16_t *) ee;326for (w = 0; w < NW(struct ar9287_eeprom); w++) {327off = AR9287_EEP_START_LOC + w;328if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {329HALDEBUG(ah, HAL_DEBUG_ANY,330"%s eeprom read error at offset 0x%x\n",331__func__, off);332return HAL_EEREAD;333}334}335/* Convert to eeprom native eeprom endian format */336/*337* XXX this is likely incorrect but will do for now338* XXX to get embedded boards working.339*/340if (ah->ah_eepromdata == NULL && isBigEndian()) {341for (w = 0; w < NW(HAL_EEPROM_9287); w++)342eep_data[w] = __bswap16(eep_data[w]);343}344345/*346* At this point, we're in the native eeprom endian format347* Now, determine the eeprom endian by looking at byte 26??348*/349need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();350if (need_swap) {351HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,352"Byte swap EEPROM contents.\n");353len = __bswap16(ee->ee_base.baseEepHeader.length);354} else {355len = ee->ee_base.baseEepHeader.length;356}357len = AH_MIN(len, sizeof(HAL_EEPROM_9287)) / sizeof(uint16_t);358359/* Apply the checksum, done in native eeprom format */360/* XXX - Need to check to make sure checksum calculation is done361* in the correct endian format. Right now, it seems it would362* cast the raw data to host format and do the calculation, which may363* not be correct as the calculation may need to be done in the native364* eeprom format365*/366sum = 0;367for (w = 0; w < len; w++)368sum ^= eep_data[w];369/* Check CRC - Attach should fail on a bad checksum */370if (sum != 0xffff) {371HALDEBUG(ah, HAL_DEBUG_ANY,372"Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);373return HAL_EEBADSUM;374}375376if (need_swap)377eepromSwap(ee); /* byte swap multi-byte data */378379/* swap words 0+2 so version is at the front */380magic = eep_data[0];381eep_data[0] = eep_data[2];382eep_data[2] = magic;383384HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,385"%s Eeprom Version %u.%u\n", __func__,386owl_get_eep_ver(ee), owl_get_eep_rev(ee));387388/* NB: must be after all byte swapping */389if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {390HALDEBUG(ah, HAL_DEBUG_ANY,391"Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));392return HAL_EEBADSUM;393}394395v9287EepromReadCTLInfo(ah, ee); /* Get CTLs */396397AH_PRIVATE(ah)->ah_eeprom = ee;398AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;399AH_PRIVATE(ah)->ah_eepromDetach = v9287EepromDetach;400AH_PRIVATE(ah)->ah_eepromGet = v9287EepromGet;401AH_PRIVATE(ah)->ah_eepromSet = v9287EepromSet;402AH_PRIVATE(ah)->ah_getSpurChan = v9287EepromGetSpurChan;403AH_PRIVATE(ah)->ah_eepromDiag = v9287EepromDiag;404return HAL_OK;405#undef NW406}407408409