1/*
2 * Copyright (c) 2013 Qualcomm Atheros, Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16diff --git a/hal/ar9300/ar9300_eeprom.c b/hal/ar9300/ar9300_eeprom.c
17index 2fe5506..628026f 100644
18--- a/hal/ar9300/ar9300_eeprom.c
19+++ b/hal/ar9300/ar9300_eeprom.c
20@@ -342,6 +342,8 @@ ar9300_otp_read(struct ath_hal *ah, u_int off, u_int32_t *data, HAL_BOOL is_wifi
21     int status = 0;
22     u_int32_t addr;
23 
24+    //ath_hal_printf(ah, "%s: reading offset 0x%x\n", __func__, off);
25+
26     addr = (AR_SREV_WASP(ah) || AR_SREV_SCORPION(ah))?
27         OTP_MEM_START_ADDRESS_WASP : OTP_MEM_START_ADDRESS;
28 	if (!is_wifi) {
29@@ -372,6 +374,7 @@ ar9300_otp_read(struct ath_hal *ah, u_int off, u_int32_t *data, HAL_BOOL is_wifi
30         addr = BTOTP_STATUS1_EFUSE_READ_DATA;
31     }
32     *data = OS_REG_READ(ah, addr);
33+    //ath_hal_printf(ah, "%s:  data=0x%x\n", __func__, *data);
34     return AH_TRUE;
35 }
36 
37@@ -603,6 +606,8 @@ ar9300_eeprom_attach(struct ath_hal *ah)
38     systime_t current_system_time = OS_GET_TIMESTAMP();
39 #endif    
40 #endif
41+
42+    ath_hal_printf(ah, "%s: starting\n", __func__);
43     ahp->try_dram = 1;
44     ahp->try_eeprom = 1;
45     ahp->try_otp = 1;
46@@ -679,10 +684,14 @@ ar9300_eeprom_attach(struct ath_hal *ah)
47 #endif
48 #endif
49     
50+    ath_hal_printf(ah, "%s: calling ar9300_fill_eeprom\n", __func__);
51     if (!ar9300_fill_eeprom(ah)) {
52         return HAL_EIO;
53     }
54 
55+    ath_hal_printf(ah, "%s: calibration data type = %d\n", __func__,
56+      AH9300(ah)->calibration_data_source);
57+
58     return HAL_OK;
59     /* return ar9300_check_eeprom(ah); */
60 #endif
61@@ -704,6 +713,7 @@ ar9300_eeprom_attach(struct ath_hal *ah)
62     ahp->ah_eeprom.mac_addr[4] = 0xD0;
63     ahp->ah_eeprom.mac_addr[5] = 0x00;
64 #endif
65+    ath_hal_printf(ah, "%s: %s:%d\n", __func__, __FILE__, __LINE__);
66     return HAL_OK;
67 #else
68 #if ATH_DRIVER_SIM
69@@ -764,13 +774,17 @@ ar9300_eeprom_attach(struct ath_hal *ah)
70         return HAL_OK;
71     }
72 #endif
73+    ath_hal_printf(ah, "%s: %s:%d\n", __func__, __FILE__, __LINE__);
74     if (AR_SREV_HORNET(ah) || AR_SREV_WASP(ah) || AR_SREV_SCORPION(ah)) {
75         ahp->try_eeprom = 0;
76     }
77 
78+    ath_hal_printf(ah, "%s: %s:%d\n", __func__, __FILE__, __LINE__);
79     if (!ar9300_eeprom_restore(ah)) {
80+    	ath_hal_printf(ah, "%s: %s:%d\n", __func__, __FILE__, __LINE__);
81         return HAL_EIO;
82     }
83+    ath_hal_printf(ah, "%s: %s:%d\n", __func__, __FILE__, __LINE__);
84     return HAL_OK;
85 #endif
86 #endif
87@@ -3743,6 +3757,12 @@ ar9300_calibration_data_read_otp(struct ath_hal *ah, long address,
88     unsigned long byte_addr;
89     u_int32_t svalue;
90 
91+    ath_hal_printf(ah, "%s: called: address=%d, many=%d, is_wifi=%d\n",
92+        __func__,
93+	(int) address,
94+	many,
95+	is_wifi);
96+
97     if (((address) < 0) || ((address + many) > 0x400)) {
98         return AH_FALSE;
99     }
100@@ -3802,6 +3822,8 @@ ar9300_calibration_data_read_array(struct ath_hal *ah, int address,
101 {
102     int it;
103 
104+    ath_hal_printf(ah, "%s: address=%d, many=%d\n", __func__, address, many);
105+
106     for (it = 0; it < many; it++) {
107         (void)ar9300_calibration_data_read(ah, address - it, buffer + it, 1);
108     }
109@@ -4001,13 +4023,24 @@ ar9300_eeprom_restore_internal_address(struct ath_hal *ah,
110     int restored;
111     u_int16_t checksum, mchecksum;
112 
113+    ath_hal_printf(ah, "%s: called, cptr=0x%x, mdata_size=%d, blank=%d\n",
114+      __func__, cptr, mdata_size, blank);
115+
116     restored = 0;
117     for (it = 0; it < MSTATE; it++) {            
118         (void) ar9300_calibration_data_read_array(
119             ah, cptr, word, compression_header_length);
120+        ath_hal_printf(ah, "%s:  word = 0x%x, 0x%x, 0x%x, 0x%x\n",
121+          __func__,
122+          word[0],
123+          word[1],
124+          word[2],
125+          word[3]);
126         if (word[0] == blank && word[1] == blank && word[2] == blank && word[3] == blank)
127         {
128-            break;
129+            ath_hal_printf(ah, "%s: word=blank, skipping\n", __func__);
130+            cptr -= compression_header_length;
131+            continue;
132         }
133         ar9300_compression_header_unpack(
134             word, &code, &reference, &length, &major, &minor);
135@@ -4143,19 +4176,18 @@ static int
136 ar9300_eeprom_restore_from_dram(struct ath_hal *ah, ar9300_eeprom_t *mptr,
137     int mdata_size)
138 {
139-#if 0
140     struct ath_hal_9300 *ahp = AH9300(ah);
141     char *cal_ptr;
142-#endif
143 
144+#if 0
145     /* cal data in flash / DRAM isn't currently supported */
146     ath_hal_printf(ah, "%s: Cal data from DRAM isn't supported\n",
147       __func__);
148 
149     return -1;
150+#endif
151 
152 #ifndef WIN32
153-#if 0
154     HALASSERT(mdata_size > 0);
155 
156     /* if cal_in_flash is true, the address sent by LMAC to HAL
157@@ -4164,14 +4196,17 @@ ar9300_eeprom_restore_from_dram(struct ath_hal *ah, ar9300_eeprom_t *mptr,
158     if(ar9300_eep_data_in_flash(ah))
159         return -1;
160 
161+#if 0
162     /* check if LMAC sent DRAM address is valid */
163     if (!(uintptr_t)(AH_PRIVATE(ah)->ah_st)) {
164         return -1;
165     }
166+#endif
167 
168     /* When calibration data is from host, Host will copy the 
169        compressed data to the predefined DRAM location saved at ah->ah_st */
170     ath_hal_printf(ah, "Restoring Cal data from DRAM\n");
171+#if 0
172 #ifdef __NetBSD__
173     ahp->ah_cal_mem = OS_REMAP(ah, (uintptr_t)(AH_PRIVATE(ah)->ah_st), 
174 							HOST_CALDATA_SIZE);
175@@ -4184,6 +4219,20 @@ ar9300_eeprom_restore_from_dram(struct ath_hal *ah, ar9300_eeprom_t *mptr,
176        HALDEBUG(ah, HAL_DEBUG_EEPROM,"%s: can't remap dram region\n", __func__);
177        return -1;
178     }
179+#endif
180+
181+    /*
182+     * The atheros HAL passes in this buffer anyway, and we copy things
183+     * into it.  However, the FreeBSD driver doesn't supply this
184+     * particular memory.  So, let's just assume attach/detach will
185+     * provide us with a 16 kilobyte buffer for now, and hope the whole
186+     * OTP path works.
187+     */
188+    if (! ahp->ah_cal_mem) {
189+        ath_hal_printf(ah, "%s: FreeBSD: ah_cal_mem isn't set\n", __func__);
190+        return -1;
191+    }
192+
193     cal_ptr = &((char *)(ahp->ah_cal_mem))[AR9300_FLASH_CAL_START_OFFSET];
194     OS_MEMCPY(mptr, cal_ptr, mdata_size);
195 
196@@ -4201,7 +4250,6 @@ ar9300_eeprom_restore_from_dram(struct ath_hal *ah, ar9300_eeprom_t *mptr,
197     }
198 
199     return mdata_size;
200-#endif
201 #else
202     return -1;
203 #endif
204@@ -4290,6 +4338,11 @@ ar9300_eeprom_restore_from_flash(struct ath_hal *ah, ar9300_eeprom_t *mptr,
205     }
206 
207 #endif
208+    ath_hal_printf(ah, "%s: eeprom version=%d, template version=%d\n",
209+        __func__,
210+        (int) mptr->eeprom_version,
211+        (int) mptr->template_version);
212+
213     if (mptr->eeprom_version   == 0xff ||
214         mptr->template_version == 0xff ||
215         mptr->eeprom_version   == 0    ||
216@@ -4321,10 +4374,11 @@ ar9300_eeprom_restore_internal(struct ath_hal *ah, ar9300_eeprom_t *mptr,
217 
218     nptr = -1;    
219 
220-    if ((AH9300(ah)->calibration_data_try == calibration_data_none ||
221+    if (0 && (AH9300(ah)->calibration_data_try == calibration_data_none ||
222          AH9300(ah)->calibration_data_try == calibration_data_dram) &&
223          AH9300(ah)->try_dram && nptr < 0)
224     {   
225+        ath_hal_printf(ah, "%s: trying dram\n", __func__);
226         AH9300(ah)->calibration_data_source = calibration_data_dram;
227         AH9300(ah)->calibration_data_source_address = 0;
228         nptr = ar9300_eeprom_restore_from_dram(ah, mptr, mdata_size);
229@@ -4334,7 +4388,7 @@ ar9300_eeprom_restore_internal(struct ath_hal *ah, ar9300_eeprom_t *mptr,
230         }
231     }
232     
233-    if ((AH9300(ah)->calibration_data_try == calibration_data_none ||
234+    if (0 && (AH9300(ah)->calibration_data_try == calibration_data_none ||
235          AH9300(ah)->calibration_data_try == calibration_data_eeprom) &&
236         AH9300(ah)->try_eeprom && nptr < 0)
237     {
238@@ -4343,6 +4397,7 @@ ar9300_eeprom_restore_internal(struct ath_hal *ah, ar9300_eeprom_t *mptr,
239          * base_address=0x3ff where we used to write the data
240          */
241         AH9300(ah)->calibration_data_source = calibration_data_eeprom;
242+        ath_hal_printf(ah, "%s: trying eeprom\n", __func__);
243         if (AH9300(ah)->calibration_data_try_address != 0) {
244             AH9300(ah)->calibration_data_source_address =
245                 AH9300(ah)->calibration_data_try_address;
246@@ -4374,7 +4429,7 @@ ar9300_eeprom_restore_internal(struct ath_hal *ah, ar9300_eeprom_t *mptr,
247      * ##### should be an ifdef test for any AP usage,
248      * either in driver or in nart
249      */
250-    if ((AH9300(ah)->calibration_data_try == calibration_data_none ||
251+    if (0 && (AH9300(ah)->calibration_data_try == calibration_data_none ||
252          AH9300(ah)->calibration_data_try == calibration_data_flash) &&
253         AH9300(ah)->try_flash && nptr < 0)
254     {
255@@ -4388,10 +4443,17 @@ ar9300_eeprom_restore_internal(struct ath_hal *ah, ar9300_eeprom_t *mptr,
256         }
257     }
258 
259+    /* XXX FreeBSD? */
260+    AH9300(ah)->calibration_data_try = calibration_data_otp;
261+    AH9300(ah)->calibration_data_try_address = 0x2ff;
262+
263     if ((AH9300(ah)->calibration_data_try == calibration_data_none ||
264          AH9300(ah)->calibration_data_try == calibration_data_otp) &&
265         AH9300(ah)->try_otp && nptr < 0)
266     {
267+        ath_hal_printf(ah, "%s: trying OTP, try_address=0x%x\n",
268+            __func__,
269+            AH9300(ah)->calibration_data_try_address);
270         AH9300(ah)->calibration_data_source = calibration_data_otp;
271         if (AH9300(ah)->calibration_data_try_address != 0) {
272             AH9300(ah)->calibration_data_source_address =
273@@ -4526,6 +4588,8 @@ ar9300_eeprom_restore(struct ath_hal *ah)
274     int mdata_size;
275     HAL_BOOL status = AH_FALSE;
276 
277+    ath_hal_printf(ah, "%s: called\n", __func__);
278+
279     mptr = &ahp->ah_eeprom;
280     mdata_size = ar9300_eeprom_struct_size();
281 
282