Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / magpie_1_1 / sboot / cmnos / misc / src / cmnos_misc.c
1
2 #include "sys_cfg.h"
3
4 #if SYSTEM_MODULE_MISC
5
6 #include "athos_api.h"
7 #include "regdump.h"
8
9 #if SYSTEM_MODULE_USB
10 extern uint16_t UsbDeviceDescriptor[];
11 #endif
12
13 /* This number gets bumped on each official build.  */
14 // uint32_t cmnos_target_software_id = AR6K_SW_VERSION;
15
16 /*!
17  *      system reset
18  */
19 LOCAL void
20 cmnos_system_reset(void)
21 {
22         /* TBD: to be finished */
23         /*!
24          * sytem reset backdoor
25          */
26          HAL_WORD_REG_WRITE(MAGPIE_REG_RST_RESET_ADDR, (0x1<<24));
27 }
28
29 #if 0
30 /*!
31  *      wdt reset
32  */
33 #LOCAL void cmnos_wdt_reset(void)
34 {
35         HAL_WORD_REG_WRITE(MAGPIE_REG_RST_RESET_ADDR, (0x1<<24));
36 }
37 #endif
38
39 /*!
40  * mac reset
41  */
42 LOCAL void
43 cmnos_mac_reset(void)
44 {
45         /* TBD: to be finished */
46         /*!
47          * mac reset backdoor
48          */
49 }
50
51 volatile int assloop = 1;
52 int assprint = 1;
53
54 //A_COMPILE_TIME_ASSERT(verify_RD_SIZE, (RD_SIZE == sizeof(CPU_exception_frame_t)))
55
56 LOCAL void
57 cmnos_misaligned_load_handler(struct register_dump_s *dump)
58 {
59         /* TBD: to be finished */
60     if (A_IML_IS_ASSERT(dump->badvaddr)) {
61         /*
62          * Probably an Intentional Misaligned Load, used to
63          * signal an assertion failure
64          */
65         dump->assline = A_IML_ASSLINE(dump->badvaddr);
66         A_ASSFAIL(dump);
67     } else {
68         /* A genuine misaligned load */
69         A_PRINTF("Misaligned load: pc=0x%x badvaddr=0x%x dump area=0x%x\n",
70                 dump->pc, dump->badvaddr, dump);
71         dump->assline = 0;
72         A_ASSFAIL(dump); /* Not really an assertion failure, but we'll treat it similarly. */
73     }
74
75     // trigger wdt, in case hang
76     HAL_WORD_REG_WRITE(MAGPIE_REG_RST_WDT_TIMER_CTRL_ADDR, 0x03);
77     HAL_WORD_REG_WRITE(MAGPIE_REG_RST_WDT_TIMER_ADDR, 0x10);
78
79     while(1);
80 }
81
82 struct register_dump_s *current_dump = NULL;
83
84 /*!
85  * A convenient place to set a breakpoint.
86  * Whenever an A_ASSERT triggers, it comes here.
87  */
88 LOCAL void
89 cmnos_assfail(struct register_dump_s *dump)
90 {
91     if (current_dump == NULL ) {
92         A_UINT32 target_id;
93
94         current_dump = dump;
95         //A_TARGET_ID_GET(&target_id);
96         dump->target_id = target_id;
97
98         if (assprint) {
99             unsigned int i;
100
101             A_PRINTF("assertion failed? pc=0x%x, line=%d, dump area=0x%x\n",
102                     dump->pc, dump->assline, dump);
103 //            INF_DBG2_LOG(INF_ASSERTION_FAILED, dump->pc,
104 //                         A_IML_ASSLINE(dump->badvaddr));
105 //            INF_DBG1_LOG(INF_ASSERTION_FAILED, (A_UINT32)dump);
106
107             A_PRINTF("Target ID: 0x%x (%d)\n", target_id, target_id);
108 //            INF_DBG1_LOG(INF_TARGET_ID, target_id);
109
110             A_PRINTF("Debug Info:");
111             for (i=0; i<(sizeof(struct register_dump_s)/sizeof(A_UINT32)); i++) {
112                 if ((i%4) == 0) {
113                     A_PRINTF("\n");
114                 }
115                 A_PRINTF("0x%08x ", ((A_UINT32 *)dump)[i]);
116 //                INF_DBG1_LOG(INF_ASSERTION_FAILED, ((A_UINT32 *)dump)[i]);
117             }
118             A_PRINTF("\n");
119         }
120     } else {
121         /*
122          * We must have assfail'ed again while processing the first assfail.
123          * Don't try to print anything -- keep it very simple.
124          */
125
126     }
127 }
128
129 /*!
130  * failure state report
131  */
132 LOCAL void
133 cmnos_report_failure_to_host(struct register_dump_s *dump, int len)
134 {
135         /* TBD: to be removed! */
136 }
137
138 /*!
139  * get target id
140  */
141 LOCAL int
142 cmnos_target_id_get(void)
143 {
144         /* TBD: to be removed! */
145 }
146
147 /*!
148  * get keyboard hit with delay
149  */
150 LOCAL uint8_t
151 cmnos_get_kbhit(uint8_t delay)
152 {
153     uint32_t last_ccount;
154     uint8_t kbhit;
155
156     last_ccount = xthal_get_ccount();
157     while (1)
158     {
159         if( A_GETC(&kbhit) != 0 )
160             break;
161
162         if((xthal_get_ccount() - last_ccount)>=delay*1000*ONE_MSEC)
163         {
164             break;
165         }
166     }
167
168     return kbhit;
169 }
170
171 /*!
172  * host alive & return the hostif type
173  */
174 LOCAL A_HOSTIF
175 cmnos_is_host_present(void)
176 {
177     /*!
178       *  TODO: check the hostif and return the type of host interface
179       */
180         A_HOSTIF mHif = HIF_USB;
181 #if defined(PROJECT_K2)
182     A_PRINTF("5. usb only!!\n");
183     return mHif;
184 #elif defined(PROJECT_MAGPIE)
185     uint32_t mData;
186
187     mData = MAGPIE_REG_RST_BOOTSTRAP;
188
189         //@RYAN@TODO - this one is somehow not working on L5, need to turn on!!!
190 #if 1
191     /* 4:3  of BOOTSTRAP could distinguish the host interfce
192      *
193      *   2'b11 -> gmac
194      *   2'b10 -> pci
195      *   2'b01 -> pcie
196      *   2'b00 -> usb
197      *
198      */
199     if( mData & BIT3 )
200     {
201         if ( mData & BIT2 )
202             mHif = HIF_GMAC;
203         else
204             mHif = HIF_PCI;
205     }    
206     else
207     {
208         if ( mData & BIT2 )
209             mHif = HIF_PCIE;
210         else
211             mHif = HIF_USB;
212     }
213 #endif
214     //A_PRINTF("5. hif (0x%08x) is read!!\n", mData);
215     return mHif;
216 #endif
217 }
218
219 /*!
220  * get ROM code version
221  */
222 LOCAL uint16_t
223 cmnos_rom_version_get(void)
224 {
225 #if SYSTEM_MODULE_USB
226     /* USB Device Descriptor : byte 12, 13 Device BCD -> Device release number in binary-coded decimal. */
227     return UsbDeviceDescriptor[6];
228 #else
229     return 0;
230 #endif
231 }
232
233 void
234 cmnos_misc_module_install(struct misc_api *tbl)
235 {
236     tbl->_system_reset           = cmnos_system_reset;
237     tbl->_mac_reset              = cmnos_mac_reset;
238     tbl->_assfail                = cmnos_assfail;
239     tbl->_misaligned_load_handler= cmnos_misaligned_load_handler;
240     tbl->_report_failure_to_host = cmnos_report_failure_to_host;
241     //tbl->_target_id_get        = cmnos_target_id_get;
242     tbl->_is_host_present        = cmnos_is_host_present;
243     tbl->_kbhit                  = cmnos_get_kbhit;
244     tbl->_rom_version_get        = cmnos_rom_version_get;
245 }
246
247 #endif
248