carl9170: Update to latest upstream
[linux-libre-firmware.git] / ath9k_htc / sboot / magpie_1_1 / sboot / cmnos / misc / src / cmnos_misc.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include "sys_cfg.h"
36
37 #if SYSTEM_MODULE_MISC
38
39 #include "athos_api.h"
40 #include "regdump.h"
41
42 #if SYSTEM_MODULE_USB
43 extern uint16_t UsbDeviceDescriptor[];
44 #endif
45
46 /* This number gets bumped on each official build.  */
47 // uint32_t cmnos_target_software_id = AR6K_SW_VERSION;
48
49 /*!
50  *      system reset
51  */
52 LOCAL void
53 cmnos_system_reset(void)
54 {
55         /* TBD: to be finished */
56         /*!
57          * sytem reset backdoor
58          */
59          HAL_WORD_REG_WRITE(MAGPIE_REG_RST_RESET_ADDR, (0x1<<24));
60 }
61
62 #if 0
63 /*!
64  *      wdt reset
65  */
66 #LOCAL void cmnos_wdt_reset(void)
67 {
68         HAL_WORD_REG_WRITE(MAGPIE_REG_RST_RESET_ADDR, (0x1<<24));
69 }
70 #endif
71
72 /*!
73  * mac reset
74  */
75 LOCAL void
76 cmnos_mac_reset(void)
77 {
78         /* TBD: to be finished */
79         /*!
80          * mac reset backdoor
81          */
82 }
83
84 volatile int assloop = 1;
85 int assprint = 1;
86
87 //A_COMPILE_TIME_ASSERT(verify_RD_SIZE, (RD_SIZE == sizeof(CPU_exception_frame_t)))
88
89 LOCAL void
90 cmnos_misaligned_load_handler(struct register_dump_s *dump)
91 {
92         /* TBD: to be finished */
93     if (A_IML_IS_ASSERT(dump->badvaddr)) {
94         /*
95          * Probably an Intentional Misaligned Load, used to
96          * signal an assertion failure
97          */
98         dump->assline = A_IML_ASSLINE(dump->badvaddr);
99         A_ASSFAIL(dump);
100     } else {
101         /* A genuine misaligned load */
102         A_PRINTF("Misaligned load: pc=0x%x badvaddr=0x%x dump area=0x%x\n",
103                 dump->pc, dump->badvaddr, dump);
104         dump->assline = 0;
105         A_ASSFAIL(dump); /* Not really an assertion failure, but we'll treat it similarly. */
106     }
107
108     // trigger wdt, in case hang
109     HAL_WORD_REG_WRITE(MAGPIE_REG_RST_WDT_TIMER_CTRL_ADDR, 0x03);
110     HAL_WORD_REG_WRITE(MAGPIE_REG_RST_WDT_TIMER_ADDR, 0x10);
111
112     while(1);
113 }
114
115 struct register_dump_s *current_dump = NULL;
116
117 /*!
118  * A convenient place to set a breakpoint.
119  * Whenever an A_ASSERT triggers, it comes here.
120  */
121 LOCAL void
122 cmnos_assfail(struct register_dump_s *dump)
123 {
124     if (current_dump == NULL ) {
125         A_UINT32 target_id;
126
127         current_dump = dump;
128         //A_TARGET_ID_GET(&target_id);
129         dump->target_id = target_id;
130
131         if (assprint) {
132             unsigned int i;
133
134             A_PRINTF("assertion failed? pc=0x%x, line=%d, dump area=0x%x\n",
135                     dump->pc, dump->assline, dump);
136 //            INF_DBG2_LOG(INF_ASSERTION_FAILED, dump->pc,
137 //                         A_IML_ASSLINE(dump->badvaddr));
138 //            INF_DBG1_LOG(INF_ASSERTION_FAILED, (A_UINT32)dump);
139
140             A_PRINTF("Target ID: 0x%x (%d)\n", target_id, target_id);
141 //            INF_DBG1_LOG(INF_TARGET_ID, target_id);
142
143             A_PRINTF("Debug Info:");
144             for (i=0; i<(sizeof(struct register_dump_s)/sizeof(A_UINT32)); i++) {
145                 if ((i%4) == 0) {
146                     A_PRINTF("\n");
147                 }
148                 A_PRINTF("0x%08x ", ((A_UINT32 *)dump)[i]);
149 //                INF_DBG1_LOG(INF_ASSERTION_FAILED, ((A_UINT32 *)dump)[i]);
150             }
151             A_PRINTF("\n");
152         }
153     } else {
154         /*
155          * We must have assfail'ed again while processing the first assfail.
156          * Don't try to print anything -- keep it very simple.
157          */
158
159     }
160 }
161
162 /*!
163  * failure state report
164  */
165 LOCAL void
166 cmnos_report_failure_to_host(struct register_dump_s *dump, int len)
167 {
168         /* TBD: to be removed! */
169 }
170
171 /*!
172  * get target id
173  */
174 LOCAL int
175 cmnos_target_id_get(void)
176 {
177         /* TBD: to be removed! */
178 }
179
180 /*!
181  * get keyboard hit with delay
182  */
183 LOCAL uint8_t
184 cmnos_get_kbhit(uint8_t delay)
185 {
186     uint32_t last_ccount;
187     uint8_t kbhit;
188
189     last_ccount = xthal_get_ccount();
190     while (1)
191     {
192         if( A_GETC(&kbhit) != 0 )
193             break;
194
195         if((xthal_get_ccount() - last_ccount)>=delay*1000*ONE_MSEC)
196         {
197             break;
198         }
199     }
200
201     return kbhit;
202 }
203
204 /*!
205  * host alive & return the hostif type
206  */
207 LOCAL A_HOSTIF
208 cmnos_is_host_present(void)
209 {
210     /*!
211       *  TODO: check the hostif and return the type of host interface
212       */
213         A_HOSTIF mHif = HIF_USB;
214 #if defined(PROJECT_K2)
215     A_PRINTF("5. usb only!!\n");
216     return mHif;
217 #elif defined(PROJECT_MAGPIE)
218     uint32_t mData;
219
220     mData = MAGPIE_REG_RST_BOOTSTRAP;
221
222         //@RYAN@TODO - this one is somehow not working on L5, need to turn on!!!
223 #if 1
224     /* 4:3  of BOOTSTRAP could distinguish the host interfce
225      *
226      *   2'b11 -> gmac
227      *   2'b10 -> pci
228      *   2'b01 -> pcie
229      *   2'b00 -> usb
230      *
231      */
232     if( mData & BIT3 )
233     {
234         if ( mData & BIT2 )
235             mHif = HIF_GMAC;
236         else
237             mHif = HIF_PCI;
238     }    
239     else
240     {
241         if ( mData & BIT2 )
242             mHif = HIF_PCIE;
243         else
244             mHif = HIF_USB;
245     }
246 #endif
247     //A_PRINTF("5. hif (0x%08x) is read!!\n", mData);
248     return mHif;
249 #endif
250 }
251
252 /*!
253  * get ROM code version
254  */
255 LOCAL uint16_t
256 cmnos_rom_version_get(void)
257 {
258 #if SYSTEM_MODULE_USB
259     /* USB Device Descriptor : byte 12, 13 Device BCD -> Device release number in binary-coded decimal. */
260     return UsbDeviceDescriptor[6];
261 #else
262     return 0;
263 #endif
264 }
265
266 void
267 cmnos_misc_module_install(struct misc_api *tbl)
268 {
269     tbl->_system_reset           = cmnos_system_reset;
270     tbl->_mac_reset              = cmnos_mac_reset;
271     tbl->_assfail                = cmnos_assfail;
272     tbl->_misaligned_load_handler= cmnos_misaligned_load_handler;
273     tbl->_report_failure_to_host = cmnos_report_failure_to_host;
274     //tbl->_target_id_get        = cmnos_target_id_get;
275     tbl->_is_host_present        = cmnos_is_host_present;
276     tbl->_kbhit                  = cmnos_get_kbhit;
277     tbl->_rom_version_get        = cmnos_rom_version_get;
278 }
279
280 #endif
281