FreeBSD kernel ATH device code
ah_eeprom_v1.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2008 Sam Leffler, Errno Consulting
5 * Copyright (c) 2008 Atheros Communications, Inc.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * $FreeBSD$
20 */
21#include "opt_ah.h"
22
23#include "ah.h"
24#include "ah_internal.h"
25#include "ah_eeprom_v1.h"
26
27static HAL_STATUS
28v1EepromGet(struct ath_hal *ah, int param, void *val)
29{
30 HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
31 uint32_t sum;
32 uint16_t eeval;
33 uint8_t *macaddr;
34 int i;
35
36 switch (param) {
37 case AR_EEP_MACADDR: /* Get MAC Address */
38 sum = 0;
39 macaddr = val;
40 for (i = 0; i < 3; i++) {
41 if (!ath_hal_eepromRead(ah, AR_EEPROM_MAC(i), &eeval)) {
43 "%s: cannot read EEPROM location %u\n",
44 __func__, i);
45 return HAL_EEREAD;
46 }
47 sum += eeval;
48 macaddr[2*i + 0] = eeval >> 8;
49 macaddr[2*i + 1] = eeval & 0xff;
50 }
51 if (sum == 0 || sum == 0xffff*3) {
52 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
53 __func__, ath_hal_ether_sprintf(macaddr));
54 return HAL_EEBADMAC;
55 }
56 return HAL_OK;
57 case AR_EEP_REGDMN_0:
58 *(uint16_t *) val = ee->ee_regDomain[0];
59 return HAL_OK;
60 case AR_EEP_RFKILL:
61 HALASSERT(val == AH_NULL);
62 return ee->ee_rfKill ? HAL_OK : HAL_EIO;
64 HALASSERT(val == AH_NULL);
67 default:
68 HALASSERT(0);
69 return HAL_EINVAL;
70 }
71}
72
73static HAL_STATUS
74v1EepromSet(struct ath_hal *ah, int param, int v)
75{
76 return HAL_EINVAL;
77}
78
79static HAL_BOOL
80v1EepromDiag(struct ath_hal *ah, int request,
81 const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
82{
83 HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
84
85 switch (request) {
86 case HAL_DIAG_EEPROM:
87 *result = ee;
88 *resultsize = sizeof(*ee);
89 return AH_TRUE;
90 }
91 return AH_FALSE;
92}
93
94static uint16_t
95v1EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
96{
97 return AR_NO_SPUR;
98}
99
100/*
101 * Reclaim any EEPROM-related storage.
102 */
103static void
105{
106 HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
107
108 ath_hal_free(ee);
109 AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
110}
111
114{
115 HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
116 uint16_t athvals[AR_EEPROM_ATHEROS_MAX]; /* XXX off stack */
117 uint16_t protect, eeprom_version, eeval;
118 uint32_t sum;
119 int i, loc;
120
121 HALASSERT(ee == AH_NULL);
122
123 if (!ath_hal_eepromRead(ah, AR_EEPROM_MAGIC, &eeval)) {
125 "%s: cannot read EEPROM magic number\n", __func__);
126 return HAL_EEREAD;
127 }
128 if (eeval != 0x5aa5) {
130 "%s: invalid EEPROM magic number 0x%x\n", __func__, eeval);
131 return HAL_EEMAGIC;
132 }
133
134 if (!ath_hal_eepromRead(ah, AR_EEPROM_PROTECT, &protect)) {
136 "%s: cannot read EEPROM protection bits; read locked?\n",
137 __func__);
138 return HAL_EEREAD;
139 }
140 HALDEBUG(ah, HAL_DEBUG_ATTACH, "EEPROM protect 0x%x\n", protect);
141 /* XXX check proper access before continuing */
142
143 if (!ath_hal_eepromRead(ah, AR_EEPROM_VERSION, &eeprom_version)) {
145 "%s: unable to read EEPROM version\n", __func__);
146 return HAL_EEREAD;
147 }
148 if (((eeprom_version>>12) & 0xf) != 1) {
149 /*
150 * This code only groks the version 1 EEPROM layout.
151 */
153 "%s: unsupported EEPROM version 0x%x found\n",
154 __func__, eeprom_version);
155 return HAL_EEVERSION;
156 }
157
158 /*
159 * Read the Atheros EEPROM entries and calculate the checksum.
160 */
161 sum = 0;
162 for (i = 0; i < AR_EEPROM_ATHEROS_MAX; i++) {
163 if (!ath_hal_eepromRead(ah, AR_EEPROM_ATHEROS(i), &athvals[i]))
164 return HAL_EEREAD;
165 sum ^= athvals[i];
166 }
167 if (sum != 0xffff) {
168 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad EEPROM checksum 0x%x\n",
169 __func__, sum);
170 return HAL_EEBADSUM;
171 }
172
173 /*
174 * Valid checksum, fetch the regulatory domain and save values.
175 */
176 if (!ath_hal_eepromRead(ah, AR_EEPROM_REG_DOMAIN, &eeval)) {
178 "%s: cannot read regdomain from EEPROM\n", __func__);
179 return HAL_EEREAD;
180 }
181
182 ee = ath_hal_malloc(sizeof(HAL_EEPROM_v1));
183 if (ee == AH_NULL) {
184 /* XXX message */
185 return HAL_ENOMEM;
186 }
187
188 ee->ee_version = eeprom_version;
189 ee->ee_protect = protect;
190 ee->ee_antenna = athvals[2];
191 ee->ee_biasCurrents = athvals[3];
192 ee->ee_thresh62 = athvals[4] & 0xff;
193 ee->ee_xlnaOn = (athvals[4] >> 8) & 0xff;
194 ee->ee_xpaOn = athvals[5] & 0xff;
195 ee->ee_xpaOff = (athvals[5] >> 8) & 0xff;
196 ee->ee_regDomain[0] = (athvals[6] >> 8) & 0xff;
197 ee->ee_regDomain[1] = athvals[6] & 0xff;
198 ee->ee_regDomain[2] = (athvals[7] >> 8) & 0xff;
199 ee->ee_regDomain[3] = athvals[7] & 0xff;
200 ee->ee_rfKill = athvals[8] & 0x1;
201 ee->ee_devType = (athvals[8] >> 1) & 0x7;
202
203 for (i = 0, loc = AR_EEPROM_ATHEROS_TP_SETTINGS; i < AR_CHANNELS_MAX; i++, loc += AR_TP_SETTINGS_SIZE) {
204 struct tpcMap *chan = &ee->ee_tpc[i];
205
206 /* Copy pcdac and gain_f values from EEPROM */
207 chan->pcdac[0] = (athvals[loc] >> 10) & 0x3F;
208 chan->gainF[0] = (athvals[loc] >> 4) & 0x3F;
209 chan->pcdac[1] = ((athvals[loc] << 2) & 0x3C)
210 | ((athvals[loc+1] >> 14) & 0x03);
211 chan->gainF[1] = (athvals[loc+1] >> 8) & 0x3F;
212 chan->pcdac[2] = (athvals[loc+1] >> 2) & 0x3F;
213 chan->gainF[2] = ((athvals[loc+1] << 4) & 0x30)
214 | ((athvals[loc+2] >> 12) & 0x0F);
215 chan->pcdac[3] = (athvals[loc+2] >> 6) & 0x3F;
216 chan->gainF[3] = athvals[loc+2] & 0x3F;
217 chan->pcdac[4] = (athvals[loc+3] >> 10) & 0x3F;
218 chan->gainF[4] = (athvals[loc+3] >> 4) & 0x3F;
219 chan->pcdac[5] = ((athvals[loc+3] << 2) & 0x3C)
220 | ((athvals[loc+4] >> 14) & 0x03);
221 chan->gainF[5] = (athvals[loc+4] >> 8) & 0x3F;
222 chan->pcdac[6] = (athvals[loc+4] >> 2) & 0x3F;
223 chan->gainF[6] = ((athvals[loc+4] << 4) & 0x30)
224 | ((athvals[loc+5] >> 12) & 0x0F);
225 chan->pcdac[7] = (athvals[loc+5] >> 6) & 0x3F;
226 chan->gainF[7] = athvals[loc+5] & 0x3F;
227 chan->pcdac[8] = (athvals[loc+6] >> 10) & 0x3F;
228 chan->gainF[8] = (athvals[loc+6] >> 4) & 0x3F;
229 chan->pcdac[9] = ((athvals[loc+6] << 2) & 0x3C)
230 | ((athvals[loc+7] >> 14) & 0x03);
231 chan->gainF[9] = (athvals[loc+7] >> 8) & 0x3F;
232 chan->pcdac[10] = (athvals[loc+7] >> 2) & 0x3F;
233 chan->gainF[10] = ((athvals[loc+7] << 4) & 0x30)
234 | ((athvals[loc+8] >> 12) & 0x0F);
235
236 /* Copy Regulatory Domain and Rate Information from EEPROM */
237 chan->rate36 = (athvals[loc+8] >> 6) & 0x3F;
238 chan->rate48 = athvals[loc+8] & 0x3F;
239 chan->rate54 = (athvals[loc+9] >> 10) & 0x3F;
240 chan->regdmn[0] = (athvals[loc+9] >> 4) & 0x3F;
241 chan->regdmn[1] = ((athvals[loc+9] << 2) & 0x3C)
242 | ((athvals[loc+10] >> 14) & 0x03);
243 chan->regdmn[2] = (athvals[loc+10] >> 8) & 0x3F;
244 chan->regdmn[3] = (athvals[loc+10] >> 2) & 0x3F;
245 }
246
247 AH_PRIVATE(ah)->ah_eeprom = ee;
248 AH_PRIVATE(ah)->ah_eeversion = eeprom_version;
249 AH_PRIVATE(ah)->ah_eepromDetach = v1EepromDetach;
250 AH_PRIVATE(ah)->ah_eepromGet = v1EepromGet;
251 AH_PRIVATE(ah)->ah_eepromSet = v1EepromSet;
252 AH_PRIVATE(ah)->ah_getSpurChan = v1EepromGetSpurChan;
253 AH_PRIVATE(ah)->ah_eepromDiag = v1EepromDiag;
254 return HAL_OK;
255}
HAL_STATUS
Definition: ah.h:71
@ HAL_EIO
Definition: ah.h:75
@ HAL_EEVERSION
Definition: ah.h:77
@ HAL_EEREAD
Definition: ah.h:80
@ HAL_OK
Definition: ah.h:72
@ HAL_EINVAL
Definition: ah.h:84
@ HAL_EEMAGIC
Definition: ah.h:76
@ HAL_EEBADMAC
Definition: ah.h:81
@ HAL_ENOMEM
Definition: ah.h:74
@ HAL_EEBADSUM
Definition: ah.h:79
HAL_BOOL
Definition: ah.h:93
@ AH_FALSE
Definition: ah.h:94
@ AH_TRUE
Definition: ah.h:95
@ HAL_DEBUG_ANY
Definition: ah_debug.h:62
@ HAL_DEBUG_ATTACH
Definition: ah_debug.h:30
@ HAL_DIAG_EEPROM
Definition: ah_diagcodes.h:41
#define AR_NO_SPUR
Definition: ah_eeprom.h:160
@ AR_EEP_MACADDR
Definition: ah_eeprom.h:77
@ AR_EEP_REGDMN_0
Definition: ah_eeprom.h:86
@ AR_EEP_WRITEPROTECT
Definition: ah_eeprom.h:104
@ AR_EEP_RFKILL
Definition: ah_eeprom.h:69
HAL_STATUS ath_hal_v1EepromAttach(struct ath_hal *ah)
Definition: ah_eeprom_v1.c:113
static HAL_BOOL v1EepromDiag(struct ath_hal *ah, int request, const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
Definition: ah_eeprom_v1.c:80
static uint16_t v1EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
Definition: ah_eeprom_v1.c:95
static HAL_STATUS v1EepromGet(struct ath_hal *ah, int param, void *val)
Definition: ah_eeprom_v1.c:28
static void v1EepromDetach(struct ath_hal *ah)
Definition: ah_eeprom_v1.c:104
static HAL_STATUS v1EepromSet(struct ath_hal *ah, int param, int v)
Definition: ah_eeprom_v1.c:74
#define AR_TP_SETTINGS_SIZE
Definition: ah_eeprom_v1.h:56
#define AR_EEPROM_REG_DOMAIN
Definition: ah_eeprom_v1.h:48
#define AR_EEPROM_VERSION
Definition: ah_eeprom_v1.h:52
#define AR_EEPROM_ATHEROS_TP_SETTINGS
Definition: ah_eeprom_v1.h:53
#define AR_EEPROM_MAC(i)
Definition: ah_eeprom_v1.h:44
#define AR_EEPROM_ATHEROS(n)
Definition: ah_eeprom_v1.h:51
#define AR_EEPROM_PROTECT
Definition: ah_eeprom_v1.h:46
#define AR_CHANNELS_MAX
Definition: ah_eeprom_v1.h:55
#define AR_EEPROM_MAGIC
Definition: ah_eeprom_v1.h:45
#define AR_EEPROM_PROTOTECT_WP_128_191
Definition: ah_eeprom_v1.h:47
#define AR_EEPROM_ATHEROS_MAX
Definition: ah_eeprom_v1.h:50
#define AH_PRIVATE(_ah)
Definition: ah_internal.h:442
void * ath_hal_malloc(size_t)
#define AH_NULL
Definition: ah_internal.h:28
#define HALASSERT(_x)
Definition: ah_internal.h:683
#define HALDEBUG(_ah, __m,...)
Definition: ah_internal.h:658
#define ath_hal_eepromRead(_ah, _off, _data)
Definition: ah_internal.h:448
void ath_hal_free(void *p)
Definition: ah_osdep.c:116
const char * ath_hal_ether_sprintf(const u_int8_t *mac)
Definition: ah_osdep.c:137
uint16_t ee_protect
Definition: ah_eeprom_v1.h:88
struct tpcMap ee_tpc[AR_CHANNELS_MAX]
Definition: ah_eeprom_v1.h:99
uint8_t ee_devType
Definition: ah_eeprom_v1.h:96
uint8_t ee_xpaOff
Definition: ah_eeprom_v1.h:93
uint16_t ee_version
Definition: ah_eeprom_v1.h:87
uint8_t ee_xpaOn
Definition: ah_eeprom_v1.h:94
uint16_t ee_biasCurrents
Definition: ah_eeprom_v1.h:90
uint8_t ee_rfKill
Definition: ah_eeprom_v1.h:95
uint16_t ee_antenna
Definition: ah_eeprom_v1.h:89
uint8_t ee_xlnaOn
Definition: ah_eeprom_v1.h:92
uint8_t ee_thresh62
Definition: ah_eeprom_v1.h:91
uint8_t ee_regDomain[AR_REG_DOMAINS_MAX]
Definition: ah_eeprom_v1.h:97
Definition: ah.h:1219
uint8_t rate48
Definition: ah_eeprom_v1.h:78
uint8_t rate54
Definition: ah_eeprom_v1.h:79
uint8_t pcdac[AR_TP_SCALING_ENTRIES]
Definition: ah_eeprom_v1.h:75
uint8_t rate36
Definition: ah_eeprom_v1.h:77
uint8_t regdmn[AR_REG_DOMAINS_MAX]
Definition: ah_eeprom_v1.h:80
uint8_t gainF[AR_TP_SCALING_ENTRIES]
Definition: ah_eeprom_v1.h:76