Merge branch 'x86-platform-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / staging / ath6kl / miscdrv / common_drv.c
1 //------------------------------------------------------------------------------
2 // <copyright file="common_drv.c" company="Atheros">
3 //    Copyright (c) 2004-2010 Atheros Corporation.  All rights reserved.
4 // 
5 //
6 // Permission to use, copy, modify, and/or distribute this software for any
7 // purpose with or without fee is hereby granted, provided that the above
8 // copyright notice and this permission notice appear in all copies.
9 //
10 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 //
18 //
19 //------------------------------------------------------------------------------
20 //==============================================================================
21 // Author(s): ="Atheros"
22 //==============================================================================
23
24 #include "a_config.h"
25 #include "athdefs.h"
26 #include "a_types.h"
27
28 #include "AR6002/hw2.0/hw/mbox_host_reg.h"
29 #include "AR6002/hw2.0/hw/apb_map.h"
30 #include "AR6002/hw2.0/hw/si_reg.h"
31 #include "AR6002/hw2.0/hw/gpio_reg.h"
32 #include "AR6002/hw2.0/hw/rtc_reg.h"
33 #include "AR6002/hw2.0/hw/vmc_reg.h"
34 #include "AR6002/hw2.0/hw/mbox_reg.h"
35
36 #include "a_osapi.h"
37 #include "targaddrs.h"
38 #include "hif.h"
39 #include "htc_api.h"
40 #include "wmi.h"
41 #include "bmi.h"
42 #include "bmi_msg.h"
43 #include "common_drv.h"
44 #define ATH_MODULE_NAME misc
45 #include "a_debug.h"
46 #include "ar6000_diag.h"
47
48 static ATH_DEBUG_MODULE_DBG_INFO *g_pModuleInfoHead = NULL;
49 static A_MUTEX_T                 g_ModuleListLock;
50 static A_BOOL                    g_ModuleDebugInit = FALSE;
51
52 #ifdef ATH_DEBUG_MODULE
53
54 ATH_DEBUG_INSTANTIATE_MODULE_VAR(misc,
55                                  "misc",
56                                  "Common and misc APIs",
57                                  ATH_DEBUG_MASK_DEFAULTS,
58                                  0,
59                                  NULL);
60
61 #endif
62
63 #define HOST_INTEREST_ITEM_ADDRESS(target, item) \
64         ((((target) == TARGET_TYPE_AR6002) ? AR6002_HOST_INTEREST_ITEM_ADDRESS(item) : \
65          (((target) == TARGET_TYPE_AR6003) ? AR6003_HOST_INTEREST_ITEM_ADDRESS(item) : 0)))
66
67
68 #define AR6001_LOCAL_COUNT_ADDRESS 0x0c014080
69 #define AR6002_LOCAL_COUNT_ADDRESS 0x00018080
70 #define AR6003_LOCAL_COUNT_ADDRESS 0x00018080
71 #define CPU_DBG_SEL_ADDRESS                      0x00000483
72 #define CPU_DBG_ADDRESS                          0x00000484
73
74 static A_UINT8 custDataAR6002[AR6002_CUST_DATA_SIZE];
75 static A_UINT8 custDataAR6003[AR6003_CUST_DATA_SIZE];
76
77 /* Compile the 4BYTE version of the window register setup routine,
78  * This mitigates host interconnect issues with non-4byte aligned bus requests, some
79  * interconnects use bus adapters that impose strict limitations.
80  * Since diag window access is not intended for performance critical operations, the 4byte mode should
81  * be satisfactory even though it generates 4X the bus activity. */
82
83 #ifdef USE_4BYTE_REGISTER_ACCESS
84
85     /* set the window address register (using 4-byte register access ). */
86 A_STATUS ar6000_SetAddressWindowRegister(HIF_DEVICE *hifDevice, A_UINT32 RegisterAddr, A_UINT32 Address)
87 {
88     A_STATUS status;
89     A_UINT8 addrValue[4];
90     A_INT32 i;
91
92         /* write bytes 1,2,3 of the register to set the upper address bytes, the LSB is written
93          * last to initiate the access cycle */
94
95     for (i = 1; i <= 3; i++) {
96             /* fill the buffer with the address byte value we want to hit 4 times*/
97         addrValue[0] = ((A_UINT8 *)&Address)[i];
98         addrValue[1] = addrValue[0];
99         addrValue[2] = addrValue[0];
100         addrValue[3] = addrValue[0];
101
102             /* hit each byte of the register address with a 4-byte write operation to the same address,
103              * this is a harmless operation */
104         status = HIFReadWrite(hifDevice,
105                               RegisterAddr+i,
106                               addrValue,
107                               4,
108                               HIF_WR_SYNC_BYTE_FIX,
109                               NULL);
110         if (status != A_OK) {
111             break;
112         }
113     }
114
115     if (status != A_OK) {
116         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot write initial bytes of 0x%x to window reg: 0x%X \n",
117             Address, RegisterAddr));
118         return status;
119     }
120
121         /* write the address register again, this time write the whole 4-byte value.
122          * The effect here is that the LSB write causes the cycle to start, the extra
123          * 3 byte write to bytes 1,2,3 has no effect since we are writing the same values again */
124     status = HIFReadWrite(hifDevice,
125                           RegisterAddr,
126                           (A_UCHAR *)(&Address),
127                           4,
128                           HIF_WR_SYNC_BYTE_INC,
129                           NULL);
130
131     if (status != A_OK) {
132         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot write 0x%x to window reg: 0x%X \n",
133             Address, RegisterAddr));
134         return status;
135     }
136
137     return A_OK;
138
139
140
141 }
142
143
144 #else
145
146     /* set the window address register */
147 A_STATUS ar6000_SetAddressWindowRegister(HIF_DEVICE *hifDevice, A_UINT32 RegisterAddr, A_UINT32 Address)
148 {
149     A_STATUS status;
150
151         /* write bytes 1,2,3 of the register to set the upper address bytes, the LSB is written
152          * last to initiate the access cycle */
153     status = HIFReadWrite(hifDevice,
154                           RegisterAddr+1,  /* write upper 3 bytes */
155                           ((A_UCHAR *)(&Address))+1,
156                           sizeof(A_UINT32)-1,
157                           HIF_WR_SYNC_BYTE_INC,
158                           NULL);
159
160     if (status != A_OK) {
161         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot write initial bytes of 0x%x to window reg: 0x%X \n",
162              RegisterAddr, Address));
163         return status;
164     }
165
166         /* write the LSB of the register, this initiates the operation */
167     status = HIFReadWrite(hifDevice,
168                           RegisterAddr,
169                           (A_UCHAR *)(&Address),
170                           sizeof(A_UINT8),
171                           HIF_WR_SYNC_BYTE_INC,
172                           NULL);
173
174     if (status != A_OK) {
175         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot write 0x%x to window reg: 0x%X \n",
176             RegisterAddr, Address));
177         return status;
178     }
179
180     return A_OK;
181 }
182
183 #endif
184
185 /*
186  * Read from the AR6000 through its diagnostic window.
187  * No cooperation from the Target is required for this.
188  */
189 A_STATUS
190 ar6000_ReadRegDiag(HIF_DEVICE *hifDevice, A_UINT32 *address, A_UINT32 *data)
191 {
192     A_STATUS status;
193
194         /* set window register to start read cycle */
195     status = ar6000_SetAddressWindowRegister(hifDevice,
196                                              WINDOW_READ_ADDR_ADDRESS,
197                                              *address);
198
199     if (status != A_OK) {
200         return status;
201     }
202
203         /* read the data */
204     status = HIFReadWrite(hifDevice,
205                           WINDOW_DATA_ADDRESS,
206                           (A_UCHAR *)data,
207                           sizeof(A_UINT32),
208                           HIF_RD_SYNC_BYTE_INC,
209                           NULL);
210     if (status != A_OK) {
211         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot read from WINDOW_DATA_ADDRESS\n"));
212         return status;
213     }
214
215     return status;
216 }
217
218
219 /*
220  * Write to the AR6000 through its diagnostic window.
221  * No cooperation from the Target is required for this.
222  */
223 A_STATUS
224 ar6000_WriteRegDiag(HIF_DEVICE *hifDevice, A_UINT32 *address, A_UINT32 *data)
225 {
226     A_STATUS status;
227
228         /* set write data */
229     status = HIFReadWrite(hifDevice,
230                           WINDOW_DATA_ADDRESS,
231                           (A_UCHAR *)data,
232                           sizeof(A_UINT32),
233                           HIF_WR_SYNC_BYTE_INC,
234                           NULL);
235     if (status != A_OK) {
236         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot write 0x%x to WINDOW_DATA_ADDRESS\n", *data));
237         return status;
238     }
239
240         /* set window register, which starts the write cycle */
241     return ar6000_SetAddressWindowRegister(hifDevice,
242                                            WINDOW_WRITE_ADDR_ADDRESS,
243                                            *address);
244     }
245
246 A_STATUS
247 ar6000_ReadDataDiag(HIF_DEVICE *hifDevice, A_UINT32 address,
248                     A_UCHAR *data, A_UINT32 length)
249 {
250     A_UINT32 count;
251     A_STATUS status = A_OK;
252
253     for (count = 0; count < length; count += 4, address += 4) {
254         if ((status = ar6000_ReadRegDiag(hifDevice, &address,
255                                          (A_UINT32 *)&data[count])) != A_OK)
256         {
257             break;
258         }
259     }
260
261     return status;
262 }
263
264 A_STATUS
265 ar6000_WriteDataDiag(HIF_DEVICE *hifDevice, A_UINT32 address,
266                     A_UCHAR *data, A_UINT32 length)
267 {
268     A_UINT32 count;
269     A_STATUS status = A_OK;
270
271     for (count = 0; count < length; count += 4, address += 4) {
272         if ((status = ar6000_WriteRegDiag(hifDevice, &address,
273                                          (A_UINT32 *)&data[count])) != A_OK)
274         {
275             break;
276         }
277     }
278
279     return status;
280 }
281
282 A_STATUS
283 ar6k_ReadTargetRegister(HIF_DEVICE *hifDevice, int regsel, A_UINT32 *regval)
284 {
285     A_STATUS status;
286     A_UCHAR vals[4];
287     A_UCHAR register_selection[4];
288
289     register_selection[0] = register_selection[1] = register_selection[2] = register_selection[3] = (regsel & 0xff);
290     status = HIFReadWrite(hifDevice,
291                           CPU_DBG_SEL_ADDRESS,
292                           register_selection,
293                           4,
294                           HIF_WR_SYNC_BYTE_FIX,
295                           NULL);
296
297     if (status != A_OK) {
298         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot write CPU_DBG_SEL (%d)\n", regsel));
299         return status;
300     }
301
302     status = HIFReadWrite(hifDevice,
303                           CPU_DBG_ADDRESS,
304                           (A_UCHAR *)vals,
305                           sizeof(vals),
306                           HIF_RD_SYNC_BYTE_INC,
307                           NULL);
308     if (status != A_OK) {
309         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot read from CPU_DBG_ADDRESS\n"));
310         return status;
311     }
312
313     *regval = vals[0]<<0 | vals[1]<<8 | vals[2]<<16 | vals[3]<<24;
314
315     return status;
316 }
317
318 void
319 ar6k_FetchTargetRegs(HIF_DEVICE *hifDevice, A_UINT32 *targregs)
320 {
321     int i;
322     A_UINT32 val;
323
324     for (i=0; i<AR6003_FETCH_TARG_REGS_COUNT; i++) {
325         val=0xffffffff;
326         (void)ar6k_ReadTargetRegister(hifDevice, i, &val);
327         targregs[i] = val;
328     }
329 }
330
331 #if 0
332 static A_STATUS
333 _do_write_diag(HIF_DEVICE *hifDevice, A_UINT32 addr, A_UINT32 value)
334 {
335     A_STATUS status;
336
337     status = ar6000_WriteRegDiag(hifDevice, &addr, &value);
338     if (status != A_OK)
339     {
340         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Cannot force Target to execute ROM!\n"));
341     }
342
343     return status;
344 }
345 #endif
346
347
348 /*
349  * Delay up to wait_msecs millisecs to allow Target to enter BMI phase,
350  * which is a good sign that it's alive and well.  This is used after
351  * explicitly forcing the Target to reset.
352  *
353  * The wait_msecs time should be sufficiently long to cover any reasonable
354  * boot-time delay.  For instance, AR6001 firmware allow one second for a
355  * low frequency crystal to settle before it calibrates the refclk frequency.
356  *
357  * TBD: Might want to add special handling for AR6K_OPTION_BMI_DISABLE.
358  */
359 #if 0
360 static A_STATUS
361 _delay_until_target_alive(HIF_DEVICE *hifDevice, A_INT32 wait_msecs, A_UINT32 TargetType)
362 {
363     A_INT32 actual_wait;
364     A_INT32 i;
365     A_UINT32 address;
366
367     actual_wait = 0;
368
369     /* Hardcode the address of LOCAL_COUNT_ADDRESS based on the target type */
370     if (TargetType == TARGET_TYPE_AR6002) {
371        address = AR6002_LOCAL_COUNT_ADDRESS;
372     } else if (TargetType == TARGET_TYPE_AR6003) {
373        address = AR6003_LOCAL_COUNT_ADDRESS;
374     } else {
375        A_ASSERT(0);
376     }
377     address += 0x10;
378     for (i=0; actual_wait < wait_msecs; i++) {
379         A_UINT32 data;
380
381         A_MDELAY(100);
382         actual_wait += 100;
383
384         data = 0;
385         if (ar6000_ReadRegDiag(hifDevice, &address, &data) != A_OK) {
386             return A_ERROR;
387         }
388
389         if (data != 0) {
390             /* No need to wait longer -- we have a BMI credit */
391             return A_OK;
392         }
393     }
394     return A_ERROR; /* timed out */
395 }
396 #endif
397
398 #define AR6001_RESET_CONTROL_ADDRESS 0x0C000000
399 #define AR6002_RESET_CONTROL_ADDRESS 0x00004000
400 #define AR6003_RESET_CONTROL_ADDRESS 0x00004000
401 /* reset device */
402 A_STATUS ar6000_reset_device(HIF_DEVICE *hifDevice, A_UINT32 TargetType, A_BOOL waitForCompletion, A_BOOL coldReset)
403 {
404     A_STATUS status = A_OK;
405     A_UINT32 address;
406     A_UINT32 data;
407
408     do {
409 // Workaround BEGIN
410         // address = RESET_CONTROL_ADDRESS;
411         
412         if (coldReset) {
413             data = RESET_CONTROL_COLD_RST_MASK;
414         }
415         else {
416             data = RESET_CONTROL_MBOX_RST_MASK;
417         }
418
419           /* Hardcode the address of RESET_CONTROL_ADDRESS based on the target type */
420         if (TargetType == TARGET_TYPE_AR6002) {
421             address = AR6002_RESET_CONTROL_ADDRESS;
422         } else if (TargetType == TARGET_TYPE_AR6003) {
423             address = AR6003_RESET_CONTROL_ADDRESS;
424         } else {
425             A_ASSERT(0);
426         }
427
428
429         status = ar6000_WriteRegDiag(hifDevice, &address, &data);
430
431         if (A_FAILED(status)) {
432             break;
433         }
434
435         if (!waitForCompletion) {
436             break;
437         }
438
439 #if 0
440         /* Up to 2 second delay to allow things to settle down */
441         (void)_delay_until_target_alive(hifDevice, 2000, TargetType);
442
443         /*
444          * Read back the RESET CAUSE register to ensure that the cold reset
445          * went through.
446          */
447
448         // address = RESET_CAUSE_ADDRESS;
449         /* Hardcode the address of RESET_CAUSE_ADDRESS based on the target type */
450         if (TargetType == TARGET_TYPE_AR6002) {
451             address = 0x000040C0;
452         } else if (TargetType == TARGET_TYPE_AR6003) {
453             address = 0x000040C0;
454         } else {
455             A_ASSERT(0);
456         }
457
458         data = 0;
459         status = ar6000_ReadRegDiag(hifDevice, &address, &data);
460
461         if (A_FAILED(status)) {
462             break;
463         }
464
465         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Reset Cause readback: 0x%X \n",data));
466         data &= RESET_CAUSE_LAST_MASK;
467         if (data != 2) {
468             AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Unable to cold reset the target \n"));
469         }
470 #endif
471 // Workaroud END
472
473     } while (FALSE);
474
475     if (A_FAILED(status)) {
476         AR_DEBUG_PRINTF(ATH_LOG_ERR, ("Failed to reset target \n"));
477     }
478
479     return A_OK;
480 }
481
482 /* This should be called in BMI phase after firmware is downloaded */
483 void
484 ar6000_copy_cust_data_from_target(HIF_DEVICE *hifDevice, A_UINT32 TargetType)
485 {
486     A_UINT32 eepHeaderAddr;
487     A_UINT8 AR6003CustDataShadow[AR6003_CUST_DATA_SIZE+4];
488     A_INT32 i;
489
490     if (BMIReadMemory(hifDevice,
491             HOST_INTEREST_ITEM_ADDRESS(TargetType, hi_board_data),
492             (A_UCHAR *)&eepHeaderAddr,
493             4)!= A_OK)
494     {
495         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("BMIReadMemory for reading board data address failed \n"));
496         return;
497     }
498
499     if (TargetType == TARGET_TYPE_AR6003) {
500         eepHeaderAddr += 36;  /* AR6003 customer data section offset is 37 */
501
502         for (i=0; i<AR6003_CUST_DATA_SIZE+4; i+=4){
503             if (BMIReadSOCRegister(hifDevice, eepHeaderAddr, (A_UINT32 *)&AR6003CustDataShadow[i])!= A_OK) {
504                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("BMIReadSOCRegister () failed \n"));
505                 return ;
506             }  
507             eepHeaderAddr +=4;
508         }
509
510         memcpy(custDataAR6003, AR6003CustDataShadow+1, AR6003_CUST_DATA_SIZE);
511     }
512
513     if (TargetType == TARGET_TYPE_AR6002) {
514         eepHeaderAddr += 64;  /* AR6002 customer data sectioin offset is 64 */
515
516         for (i=0; i<AR6002_CUST_DATA_SIZE; i+=4){
517             if (BMIReadSOCRegister(hifDevice, eepHeaderAddr, (A_UINT32 *)&custDataAR6002[i])!= A_OK) {
518                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("BMIReadSOCRegister () failed \n"));
519                 return ;
520             }  
521             eepHeaderAddr +=4;
522         }
523     }
524
525     return;
526 }
527
528 /* This is the function to call when need to use the cust data */
529 A_UINT8 *
530 ar6000_get_cust_data_buffer(A_UINT32 TargetType)
531 {
532     if (TargetType == TARGET_TYPE_AR6003)
533         return custDataAR6003;
534
535     if (TargetType == TARGET_TYPE_AR6002)
536         return custDataAR6002;
537
538     return NULL;
539 }
540
541 #define REG_DUMP_COUNT_AR6001   38  /* WORDs, derived from AR600x_regdump.h */
542 #define REG_DUMP_COUNT_AR6002   60
543 #define REG_DUMP_COUNT_AR6003   60
544 #define REGISTER_DUMP_LEN_MAX   60
545 #if REG_DUMP_COUNT_AR6001 > REGISTER_DUMP_LEN_MAX
546 #error "REG_DUMP_COUNT_AR6001 too large"
547 #endif
548 #if REG_DUMP_COUNT_AR6002 > REGISTER_DUMP_LEN_MAX
549 #error "REG_DUMP_COUNT_AR6002 too large"
550 #endif
551 #if REG_DUMP_COUNT_AR6003 > REGISTER_DUMP_LEN_MAX
552 #error "REG_DUMP_COUNT_AR6003 too large"
553 #endif
554
555
556 void ar6000_dump_target_assert_info(HIF_DEVICE *hifDevice, A_UINT32 TargetType)
557 {
558     A_UINT32 address;
559     A_UINT32 regDumpArea = 0;
560     A_STATUS status;
561     A_UINT32 regDumpValues[REGISTER_DUMP_LEN_MAX];
562     A_UINT32 regDumpCount = 0;
563     A_UINT32 i;
564
565     do {
566
567             /* the reg dump pointer is copied to the host interest area */
568         address = HOST_INTEREST_ITEM_ADDRESS(TargetType, hi_failure_state);
569         address = TARG_VTOP(TargetType, address);
570
571         if (TargetType == TARGET_TYPE_AR6002) {
572             regDumpCount = REG_DUMP_COUNT_AR6002;
573         } else  if (TargetType == TARGET_TYPE_AR6003) {
574             regDumpCount = REG_DUMP_COUNT_AR6003;
575         } else {
576             A_ASSERT(0);
577         }
578
579             /* read RAM location through diagnostic window */
580         status = ar6000_ReadRegDiag(hifDevice, &address, &regDumpArea);
581
582         if (A_FAILED(status)) {
583             AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR6K: Failed to get ptr to register dump area \n"));
584             break;
585         }
586
587         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR6K: Location of register dump data: 0x%X \n",regDumpArea));
588
589         if (regDumpArea == 0) {
590                 /* no reg dump */
591             break;
592         }
593
594         regDumpArea = TARG_VTOP(TargetType, regDumpArea);
595
596             /* fetch register dump data */
597         status = ar6000_ReadDataDiag(hifDevice,
598                                      regDumpArea,
599                                      (A_UCHAR *)&regDumpValues[0],
600                                      regDumpCount * (sizeof(A_UINT32)));
601
602         if (A_FAILED(status)) {
603             AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR6K: Failed to get register dump \n"));
604             break;
605         }
606         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR6K: Register Dump: \n"));
607
608         for (i = 0; i < regDumpCount; i++) {
609             //ATHR_DISPLAY_MSG (_T(" %d :  0x%8.8X \n"), i, regDumpValues[i]);
610             AR_DEBUG_PRINTF(ATH_DEBUG_ERR,(" %d :  0x%8.8X \n",i, regDumpValues[i]));
611
612 #ifdef UNDER_CE
613         /*
614          * For Every logPrintf() Open the File so that in case of Crashes
615          * We will have until the Last Message Flushed on to the File
616          * So use logPrintf Sparingly..!!
617          */
618         tgtassertPrintf (ATH_DEBUG_TRC," %d:  0x%8.8X \n",i, regDumpValues[i]);
619 #endif
620         }
621
622     } while (FALSE);
623
624 }
625
626 /* set HTC/Mbox operational parameters, this can only be called when the target is in the
627  * BMI phase */
628 A_STATUS ar6000_set_htc_params(HIF_DEVICE *hifDevice,
629                                A_UINT32    TargetType,
630                                A_UINT32    MboxIsrYieldValue,
631                                A_UINT8     HtcControlBuffers)
632 {
633     A_STATUS status;
634     A_UINT32 blocksizes[HTC_MAILBOX_NUM_MAX];
635
636     do {
637             /* get the block sizes */
638         status = HIFConfigureDevice(hifDevice, HIF_DEVICE_GET_MBOX_BLOCK_SIZE,
639                                     blocksizes, sizeof(blocksizes));
640
641         if (A_FAILED(status)) {
642             AR_DEBUG_PRINTF(ATH_LOG_ERR,("Failed to get block size info from HIF layer...\n"));
643             break;
644         }
645             /* note: we actually get the block size for mailbox 1, for SDIO the block
646              * size on mailbox 0 is artificially set to 1 */
647             /* must be a power of 2 */
648         A_ASSERT((blocksizes[1] & (blocksizes[1] - 1)) == 0);
649
650         if (HtcControlBuffers != 0) {
651                 /* set override for number of control buffers to use */
652             blocksizes[1] |=  ((A_UINT32)HtcControlBuffers) << 16;
653         }
654
655             /* set the host interest area for the block size */
656         status = BMIWriteMemory(hifDevice,
657                                 HOST_INTEREST_ITEM_ADDRESS(TargetType, hi_mbox_io_block_sz),
658                                 (A_UCHAR *)&blocksizes[1],
659                                 4);
660
661         if (A_FAILED(status)) {
662             AR_DEBUG_PRINTF(ATH_LOG_ERR,("BMIWriteMemory for IO block size failed \n"));
663             break;
664         }
665
666         AR_DEBUG_PRINTF(ATH_LOG_INF,("Block Size Set: %d (target address:0x%X)\n",
667                 blocksizes[1], HOST_INTEREST_ITEM_ADDRESS(TargetType, hi_mbox_io_block_sz)));
668
669         if (MboxIsrYieldValue != 0) {
670                 /* set the host interest area for the mbox ISR yield limit */
671             status = BMIWriteMemory(hifDevice,
672                                     HOST_INTEREST_ITEM_ADDRESS(TargetType, hi_mbox_isr_yield_limit),
673                                     (A_UCHAR *)&MboxIsrYieldValue,
674                                     4);
675
676             if (A_FAILED(status)) {
677                 AR_DEBUG_PRINTF(ATH_LOG_ERR,("BMIWriteMemory for yield limit failed \n"));
678                 break;
679             }
680         }
681
682     } while (FALSE);
683
684     return status;
685 }
686
687
688 static A_STATUS prepare_ar6002(HIF_DEVICE *hifDevice, A_UINT32 TargetVersion)
689 {
690     A_STATUS status = A_OK;
691
692     /* placeholder */
693
694     return status;
695 }
696
697 static A_STATUS prepare_ar6003(HIF_DEVICE *hifDevice, A_UINT32 TargetVersion)
698 {
699     A_STATUS status = A_OK;
700
701     /* placeholder */
702
703     return status;
704 }
705
706 /* this function assumes the caller has already initialized the BMI APIs */
707 A_STATUS ar6000_prepare_target(HIF_DEVICE *hifDevice,
708                                A_UINT32    TargetType,
709                                A_UINT32    TargetVersion)
710 {
711     if (TargetType == TARGET_TYPE_AR6002) {
712             /* do any preparations for AR6002 devices */
713         return prepare_ar6002(hifDevice,TargetVersion);
714     } else if (TargetType == TARGET_TYPE_AR6003) {
715         return prepare_ar6003(hifDevice,TargetVersion);
716     }
717
718     return A_OK;
719 }
720
721 #if defined(CONFIG_AR6002_REV1_FORCE_HOST)
722 /*
723  * Call this function just before the call to BMIInit
724  * in order to force* AR6002 rev 1.x firmware to detect a Host.
725  * THIS IS FOR USE ONLY WITH AR6002 REV 1.x.
726  * TBDXXX: Remove this function when REV 1.x is desupported.
727  */
728 A_STATUS
729 ar6002_REV1_reset_force_host (HIF_DEVICE *hifDevice)
730 {
731     A_INT32 i;
732     struct forceROM_s {
733         A_UINT32 addr;
734         A_UINT32 data;
735     };
736     struct forceROM_s *ForceROM;
737     A_INT32 szForceROM;
738     A_STATUS status = A_OK;
739     A_UINT32 address;
740     A_UINT32 data;
741
742     /* Force AR6002 REV1.x to recognize Host presence.
743      *
744      * Note: Use RAM at 0x52df80..0x52dfa0 with ROM Remap entry 0
745      * so that this workaround functions with AR6002.war1.sh.  We
746      * could fold that entire workaround into this one, but it's not
747      * worth the effort at this point.  This workaround cannot be
748      * merged into the other workaround because this must be done
749      * before BMI.
750      */
751
752     static struct forceROM_s ForceROM_NEW[] = {
753         {0x52df80, 0x20f31c07},
754         {0x52df84, 0x92374420},
755         {0x52df88, 0x1d120c03},
756         {0x52df8c, 0xff8216f0},
757         {0x52df90, 0xf01d120c},
758         {0x52df94, 0x81004136},
759         {0x52df98, 0xbc9100bd},
760         {0x52df9c, 0x00bba100},
761
762         {0x00008000|MC_TCAM_TARGET_ADDRESS, 0x0012dfe0}, /* Use remap entry 0 */
763         {0x00008000|MC_TCAM_COMPARE_ADDRESS, 0x000e2380},
764         {0x00008000|MC_TCAM_MASK_ADDRESS, 0x00000000},
765         {0x00008000|MC_TCAM_VALID_ADDRESS, 0x00000001},
766
767         {0x00018000|(LOCAL_COUNT_ADDRESS+0x10), 0}, /* clear BMI credit counter */
768
769         {0x00004000|AR6002_RESET_CONTROL_ADDRESS, RESET_CONTROL_WARM_RST_MASK},
770     };
771
772     address = 0x004ed4b0; /* REV1 target software ID is stored here */
773     status = ar6000_ReadRegDiag(hifDevice, &address, &data);
774     if (A_FAILED(status) || (data != AR6002_VERSION_REV1)) {
775         return A_ERROR; /* Not AR6002 REV1 */
776     }
777
778     ForceROM = ForceROM_NEW;
779     szForceROM = sizeof(ForceROM_NEW)/sizeof(*ForceROM);
780
781     ATH_DEBUG_PRINTF (DBG_MISC_DRV, ATH_DEBUG_TRC, ("Force Target to recognize Host....\n"));
782     for (i = 0; i < szForceROM; i++)
783     {
784         if (ar6000_WriteRegDiag(hifDevice,
785                                 &ForceROM[i].addr,
786                                 &ForceROM[i].data) != A_OK)
787         {
788             ATH_DEBUG_PRINTF (DBG_MISC_DRV, ATH_DEBUG_TRC, ("Cannot force Target to recognize Host!\n"));
789             return A_ERROR;
790         }
791     }
792
793     A_MDELAY(1000);
794
795     return A_OK;
796 }
797
798 #endif /* CONFIG_AR6002_REV1_FORCE_HOST */
799
800 void DebugDumpBytes(A_UCHAR *buffer, A_UINT16 length, char *pDescription)
801 {
802     A_CHAR stream[60];
803     A_CHAR byteOffsetStr[10];
804     A_UINT32 i;
805     A_UINT16 offset, count, byteOffset;
806
807     A_PRINTF("<---------Dumping %d Bytes : %s ------>\n", length, pDescription);
808
809     count = 0;
810     offset = 0;
811     byteOffset = 0;
812     for(i = 0; i < length; i++) {
813         A_SPRINTF(stream + offset, "%2.2X ", buffer[i]);
814         count ++;
815         offset += 3;
816
817         if(count == 16) {
818             count = 0;
819             offset = 0;
820             A_SPRINTF(byteOffsetStr,"%4.4X",byteOffset);
821             A_PRINTF("[%s]: %s\n", byteOffsetStr, stream);
822             A_MEMZERO(stream, 60);
823             byteOffset += 16;
824         }
825     }
826
827     if(offset != 0) {
828         A_SPRINTF(byteOffsetStr,"%4.4X",byteOffset);
829         A_PRINTF("[%s]: %s\n", byteOffsetStr, stream);
830     }
831
832     A_PRINTF("<------------------------------------------------->\n");
833 }
834
835 void a_dump_module_debug_info(ATH_DEBUG_MODULE_DBG_INFO *pInfo)
836 {
837     int                         i;
838     ATH_DEBUG_MASK_DESCRIPTION *pDesc;
839
840     if (pInfo == NULL) {
841         return;
842     }
843
844     pDesc = pInfo->pMaskDescriptions;
845
846     A_PRINTF("========================================================\n\n");
847     A_PRINTF("Module Debug Info => Name   : %s    \n", pInfo->ModuleName);
848     A_PRINTF("                  => Descr. : %s \n", pInfo->ModuleDescription);
849     A_PRINTF("\n  Current mask    => 0x%8.8X \n", pInfo->CurrentMask);
850     A_PRINTF("\n  Avail. Debug Masks :\n\n");
851
852     for (i = 0; i < pInfo->MaxDescriptions; i++,pDesc++) {
853         A_PRINTF("                  => 0x%8.8X -- %s \n", pDesc->Mask, pDesc->Description);
854     }
855
856     if (0 == i) {
857         A_PRINTF("                  => * none defined * \n");
858     }
859
860     A_PRINTF("\n  Standard Debug Masks :\n\n");
861         /* print standard masks */
862     A_PRINTF("                  => 0x%8.8X -- Errors \n", ATH_DEBUG_ERR);
863     A_PRINTF("                  => 0x%8.8X -- Warnings \n", ATH_DEBUG_WARN);
864     A_PRINTF("                  => 0x%8.8X -- Informational \n", ATH_DEBUG_INFO);
865     A_PRINTF("                  => 0x%8.8X -- Tracing \n", ATH_DEBUG_TRC);
866     A_PRINTF("\n========================================================\n");
867
868 }
869
870
871 static ATH_DEBUG_MODULE_DBG_INFO *FindModule(A_CHAR *module_name)
872 {
873     ATH_DEBUG_MODULE_DBG_INFO *pInfo = g_pModuleInfoHead;
874
875     if (!g_ModuleDebugInit) {
876         return NULL;
877     }
878
879     while (pInfo != NULL) {
880             /* TODO: need to use something other than strlen */
881         if (A_MEMCMP(pInfo->ModuleName,module_name,strlen(module_name)) == 0) {
882             break;
883         }
884         pInfo = pInfo->pNext;
885     }
886
887     return pInfo;
888 }
889
890
891 void a_register_module_debug_info(ATH_DEBUG_MODULE_DBG_INFO *pInfo)
892 {
893     if (!g_ModuleDebugInit) {
894         return;
895     }
896
897     A_MUTEX_LOCK(&g_ModuleListLock);
898
899     if (!(pInfo->Flags & ATH_DEBUG_INFO_FLAGS_REGISTERED)) {
900         if (g_pModuleInfoHead == NULL) {
901             g_pModuleInfoHead = pInfo;
902         } else {
903            pInfo->pNext = g_pModuleInfoHead;
904            g_pModuleInfoHead = pInfo;
905         }
906         pInfo->Flags |= ATH_DEBUG_INFO_FLAGS_REGISTERED;
907     }
908
909     A_MUTEX_UNLOCK(&g_ModuleListLock);
910 }
911
912 void a_dump_module_debug_info_by_name(A_CHAR *module_name)
913 {
914     ATH_DEBUG_MODULE_DBG_INFO *pInfo = g_pModuleInfoHead;
915
916     if (!g_ModuleDebugInit) {
917         return;
918     }
919
920     if (A_MEMCMP(module_name,"all",3) == 0) {
921             /* dump all */
922         while (pInfo != NULL) {
923             a_dump_module_debug_info(pInfo);
924             pInfo = pInfo->pNext;
925         }
926         return;
927     }
928
929     pInfo = FindModule(module_name);
930
931     if (pInfo != NULL) {
932          a_dump_module_debug_info(pInfo);
933     }
934
935 }
936
937 A_STATUS a_get_module_mask(A_CHAR *module_name, A_UINT32 *pMask)
938 {
939     ATH_DEBUG_MODULE_DBG_INFO *pInfo = FindModule(module_name);
940
941     if (NULL == pInfo) {
942         return A_ERROR;
943     }
944
945     *pMask = pInfo->CurrentMask;
946     return A_OK;
947 }
948
949 A_STATUS a_set_module_mask(A_CHAR *module_name, A_UINT32 Mask)
950 {
951     ATH_DEBUG_MODULE_DBG_INFO *pInfo = FindModule(module_name);
952
953     if (NULL == pInfo) {
954         return A_ERROR;
955     }
956
957     pInfo->CurrentMask = Mask;
958     A_PRINTF("Module %s,  new mask: 0x%8.8X \n",module_name,pInfo->CurrentMask);
959     return A_OK;
960 }
961
962
963 void a_module_debug_support_init(void)
964 {
965     if (g_ModuleDebugInit) {
966         return;
967     }
968     A_MUTEX_INIT(&g_ModuleListLock);
969     g_pModuleInfoHead = NULL;
970     g_ModuleDebugInit = TRUE;
971     A_REGISTER_MODULE_DEBUG_INFO(misc);
972 }
973
974 void a_module_debug_support_cleanup(void)
975 {
976     ATH_DEBUG_MODULE_DBG_INFO *pInfo = g_pModuleInfoHead;
977     ATH_DEBUG_MODULE_DBG_INFO *pCur;
978
979     if (!g_ModuleDebugInit) {
980         return;
981     }
982
983     g_ModuleDebugInit = FALSE;
984
985     A_MUTEX_LOCK(&g_ModuleListLock);
986
987     while (pInfo != NULL) {
988         pCur = pInfo;
989         pInfo = pInfo->pNext;
990         pCur->pNext = NULL;
991             /* clear registered flag */
992         pCur->Flags &= ~ATH_DEBUG_INFO_FLAGS_REGISTERED;
993     }
994
995     A_MUTEX_UNLOCK(&g_ModuleListLock);
996
997     A_MUTEX_DELETE(&g_ModuleListLock);
998     g_pModuleInfoHead = NULL;
999 }
1000
1001     /* can only be called during bmi init stage */
1002 A_STATUS ar6000_set_hci_bridge_flags(HIF_DEVICE *hifDevice,
1003                                      A_UINT32    TargetType,
1004                                      A_UINT32    Flags)
1005 {
1006     A_STATUS status = A_OK;
1007
1008     do {
1009
1010         if (TargetType != TARGET_TYPE_AR6003) {
1011             AR_DEBUG_PRINTF(ATH_DEBUG_WARN, ("Target Type:%d, does not support HCI bridging! \n",
1012                 TargetType));
1013             break;
1014         }
1015
1016             /* set hci bridge flags */
1017         status = BMIWriteMemory(hifDevice,
1018                                 HOST_INTEREST_ITEM_ADDRESS(TargetType, hi_hci_bridge_flags),
1019                                 (A_UCHAR *)&Flags,
1020                                 4);
1021
1022
1023     } while (FALSE);
1024
1025     return status;
1026 }
1027