InPlay API
hal_global.h
1 
13 #ifndef HAL_GLOBAL_H
14 #define HAL_GLOBAL_H
15 #include <stdint.h>
16 #include "in_mmap.h"
17 #include "in_compile.h"
18 #include "./hal/hal_clk.h"
19 #define ENV_MAGIC_WORD 0xB563A832
20 #define ENV_DATA_ADDR 0x7f000
21 
31 /*
32  * Enumeration
33  ****************************************************************************************
34  */
35 
36 enum glb_irq_id {
37  GLB_IRQ_PS_TRANS_DONE,
38  GLB_IRQ_PD_DOO_SLEEP_BLOCK,
39  GLB_IRQ_BLE_CLK_EN,
40  GLB_IRQ_BLE_CLK_DIS,
41  GLB_IRQ_AON_SQ_WAVE_START,
42  GLB_IRQ_AON_SQ_WAVE_END,
43  GLB_IRQ_CM4_PC_MATCH_HIT,
44  GLB_IRQ_CM4_DATA_ADDR_MATCH_HIT,
45  GLB_IRQ_CM4_SYS_ADDR_MATCH_HIT,
46  GLB_IRQ_MAX,
47 };
48 
49 enum nmi_irq_id {
50  NMI_IRQ_PD1_WDT_TIME_OUT,
51  NMI_IRQ_PD0_WDT_TIME_OUT,
52  NMI_IRQ_SEL_0_GPIO_RISE_EDGE,
53  NMI_IRQ_SEL_0_GPIO_FALL_EDGE,
54  NMI_IRQ_SEL_1_GPIO_RISE_EDGE,
55  NMI_IRQ_SEL_1_GPIO_FALL_EDGE,
56  NMI_IRQ_BOD_ASSERT,
57  NMI_IRQ_BOD2_ASSERT,
58  NMI_IRQ_MAX,
59 };
60 
61 enum osc_xo_irq_id {
62  OSC_XO_IRQ_OSC_EN = 0,
63  OSC_XO_IRQ_OSC_EN_INV = 1,
64  OSC_XO_IRQ_XO_READY = 2,
65  OSC_XO_IRQ_XO_EN = 3,
66  OSC_XO_IRQ_XO_STG_1 = 4,
67  OSC_XO_IRQ_XO_DET_0 = 5,
68  OSC_XO_IRQ_XO_ERR_DET_0 = 6,
69  OSC_XO_IRQ_XO_UNDET_0 = 7,
70  OSC_XO_IRQ_XO_DET_1 = 8,
71  OSC_XO_IRQ_XO_ERR_DET_1 = 9,
72  OSC_XO_IRQ_XO_UNDET_1 = 10,
73  OSC_XO_IRQ_CPLL_RST_DONE = 11,
74  OSC_XO_IRQ_CPLL_LOCK = 12,
75  OSC_XO_IRQ_CPLL_UNLOCK = 13,
76  OSC_XO_IRQ_XO_GT_TGT = 14,
77  OSC_XO_IRQ_XO_GT_TGT_INV = 15,
78  OSC_XO_IRQ_BOD_RISE = 16,
79  OSC_XO_IRQ_BOD2_RISE = 17,
80  OSC_XO_IRQ_MAX,
81 };
82 
83 enum data_ram_access_prio {
84  DATA_RAM_ACCESS_PRIO_1=0, // Highest
85  DATA_RAM_ACCESS_PRIO_2=1,
86  DATA_RAM_ACCESS_PRIO_3=2,
87 };
88 
89 enum bod_num {
90  BOD_ID0 = 0,
91  BOD_ID1,
92  BOD_ID_MAX,
93 };
94 
95 enum bod_trig_edge {
96  BOD_THRD_LOWER = -1,
97  BOD_THRD_CLOSEST = 0,
98  BOD_THRD_UPPER = 1,
99 };
100 
101 enum bod_thrd {
102  BOD_THRD_1P60V = 0, //< BOD_ID0 start
103  BOD_THRD_1P65V, //< BOD_ID1 start
104  BOD_THRD_1P70V,
105  BOD_THRD_1P75V, //< BOD_ID0 end
106  BOD_THRD_1P80V, //< BOD_ID1 end
107 };
108 
109 enum {
110  SHM_16K_AS_DRAM = 1,
111  SHM_32K_AS_DRAM = 2,
112 };
113 typedef struct {
114  uint32_t magic_word;
115  uint32_t cap;
116 } env_data_t;
117 /*
118  * Inine
119  ****************************************************************************************
120  */
121 static INLINE uint32_t chip_get_id(void)
122 {
123  return (RD_WORD(GLOBAL_REG_CHIP_ID) & (GLOBAL_REG_CHIP_ID_VERSION|GLOBAL_REG_CHIP_ID_SUBVERSION));
124 }
125 
126 static INLINE void disable_fpu(int disable)
127 {
128  uint32_t reg = RD_WORD(AON_REG_CM4_CTRL);
129 
130  if (disable)
131  reg |= AON_REG_CM4_CTRL_CTL_CM4_DISABLE_FPU;
132  else
133  reg &= ~AON_REG_CM4_CTRL_CTL_CM4_DISABLE_FPU;
134 
135  WR_WORD(AON_REG_CM4_CTRL, reg);
136 }
137 
138 static INLINE void disable_mpu(int disable)
139 {
140  uint32_t reg = RD_WORD(AON_REG_CM4_CTRL);
141 
142  if (disable)
143  reg |= AON_REG_CM4_CTRL_CTL_CM4_DISABLE_MPU;
144  else
145  reg &= ~AON_REG_CM4_CTRL_CTL_CM4_DISABLE_MPU;
146 
147  WR_WORD(AON_REG_CM4_CTRL, reg);
148 }
149 
150 static INLINE void cache_mem_as_dram(int en)
151 {
152  uint32_t reg = RD_WORD(AON_REG_CM4_CTRL);
153 
154  if (en)
155  reg |= AON_REG_CM4_CTRL_CTL_CACHE_MEM_AS_DC_RAM;
156  else
157  reg &= ~AON_REG_CM4_CTRL_CTL_CACHE_MEM_AS_DC_RAM;
158 
159  WR_WORD(AON_REG_CM4_CTRL, reg);
160  RD_WORD(AON_REG_CM4_CTRL);
161 }
162 
163 static INLINE void share_mem_as_dram(int en, int shm_as_dram)
164 {
165  uint32_t reg = RD_WORD(AON_REG_CM4_CTRL);
166 
167  if (en) {
168  if ((shm_as_dram == SHM_16K_AS_DRAM)) {
169  reg |= (1 << 5);
170  }
171  if ((shm_as_dram == SHM_32K_AS_DRAM)) {
172  reg |= (1 << 6);
173  }
174  } else {
175  if ((shm_as_dram == SHM_16K_AS_DRAM)) {
176  reg &= ~(1 << 5);
177  }
178  if ((shm_as_dram == SHM_32K_AS_DRAM)) {
179  reg &= ~(1 << 6);
180  }
181  }
182 
183  WR_WORD(AON_REG_CM4_CTRL, reg);
184  RD_WORD(AON_REG_CM4_CTRL);
185 }
186 
187 static INLINE void enable_all_as_dram(void)
188 {
189  uint32_t reg = RD_WORD(AON_REG_CM4_CTRL);
190 
191  reg |= (0x5 << 4);
192 
193  WR_WORD(AON_REG_CM4_CTRL, reg);
194  RD_WORD(AON_REG_CM4_CTRL);
195 }
196 
197 static FORCEINLINE uint32_t fw_get_version(void)
198 {
200  return RD_WORD(0x5FFF8);
201 }
202 
203 static FORCEINLINE uint32_t chip_sleep(void)
204 {
205  return (WR_WORD(GLOBAL_REG_SLEEP_CTRL, 1));
206 }
207 
208 static FORCEINLINE void aon_reset_pd0(void)
209 {
210  WR_WORD(AON_REG_AON_GLOBAL_RESET_CTRL, 0);
211 }
212 
213 static __inline void aon_reset_pd1(void)
214 {
215  uint32_t reg = RD_WORD(AON_REG_AON_RESET_CTRL);
216 
217  if (reg & AON_REG_AON_RESET_CTRL_CTL_PD1_MANUAL_RST_N) {
219  reg &= ~AON_REG_AON_RESET_CTRL_CTL_PD1_MANUAL_RST_N;
220  WR_WORD(AON_REG_AON_RESET_CTRL, reg);
221  } else {
223  reg |= AON_REG_AON_RESET_CTRL_CTL_PD1_MANUAL_RST_N;
224  WR_WORD(AON_REG_AON_RESET_CTRL, reg);
226  clk_delay(100);
228  reg &= ~AON_REG_AON_RESET_CTRL_CTL_PD1_MANUAL_RST_N;
229  WR_WORD(AON_REG_AON_RESET_CTRL, reg);
230  }
231 }
232 
233 static FORCEINLINE void aon_set_pc(uint32_t pc)
234 {
235  WR_WORD(AON_REG_CPU_PROGRAM_COUNTER_COLD_BOOT, pc);
236 }
237 
238 static FORCEINLINE uint32_t aon_get_pc(void)
239 {
240  return RD_WORD(AON_REG_CPU_PROGRAM_COUNTER_COLD_BOOT);
241 }
242 
243 static FORCEINLINE void aon_set_sp(uint32_t sp)
244 {
245  WR_WORD(AON_REG_CPU_STACK_POINTER, sp);
246 }
247 
248 static FORCEINLINE uint32_t aon_get_sp(void)
249 {
250  return RD_WORD(AON_REG_CPU_STACK_POINTER);
251 }
252 
253 static INLINE void data_ram_access_prio(int cpu_i_prio, int cpu_d_prio, int dma_prio)
254 {
255  uint32_t reg = RD_WORD(GLOBAL_REG_AHB_CTRL_1);
256 
257  reg &= ~GLOBAL_REG_AHB_CTRL_1_CTL_ICM_D2_DATA_CODE_RAM_PRIORITY ;
258  reg |= ((cpu_i_prio & 0x3) << 0) | ((cpu_d_prio & 0x3) << 2) | ((dma_prio & 0x3) << 4);
259  WR_WORD(GLOBAL_REG_AHB_CTRL_1, reg);
260 }
261 
262 static FORCEINLINE uint32_t glb_int_status(void)
263 {
264  return (RD_WORD(GLOBAL_REG_INTR_STATUS));
265 }
266 
267 static FORCEINLINE uint32_t glb_int_mask_status(void)
268 {
269  return (RD_WORD(GLOBAL_REG_INTR_MASK_STATUS));
270 }
271 
272 static FORCEINLINE void glb_int_clear_all(void)
273 {
274  WR_WORD(GLOBAL_REG_INTR_CLEAR, GLOBAL_REG_INTR_CLEAR_IRQ);
275 }
276 
277 static FORCEINLINE void glb_int_clear(uint32_t status)
278 {
279  WR_WORD(GLOBAL_REG_INTR_CLEAR, status);
280 }
281 
282 static FORCEINLINE void glb_int_mask_all(void)
283 {
284  WR_WORD(GLOBAL_REG_INTR_MASK_SET, GLOBAL_REG_INTR_MASK_SET_IRQ);
285 }
286 
287 static FORCEINLINE void glb_int_mask(uint32_t mask)
288 {
289  WR_WORD(GLOBAL_REG_INTR_MASK_SET, mask);
290 }
291 
292 static FORCEINLINE void glb_int_unmask(uint32_t mask)
293 {
294  WR_WORD(GLOBAL_REG_INTR_MASK_CLEAR, mask);
295 }
296 
297 static FORCEINLINE uint32_t swi_status(void)
298 {
299  return (RD_WORD(GLOBAL_REG_INTR_SW_STATUS));
300 }
301 
302 static FORCEINLINE uint32_t swi_mask_status(void)
303 {
304  return (RD_WORD(GLOBAL_REG_INTR_SW_MASK_STATUS));
305 }
306 
307 static FORCEINLINE void swi_clear_all(void)
308 {
309  WR_WORD(GLOBAL_REG_INTR_SW_CLEAR, GLOBAL_REG_INTR_SW_CLEAR_IRQ);
310 }
311 
312 static FORCEINLINE void swi_clear(uint32_t status)
313 {
314  WR_WORD(GLOBAL_REG_INTR_SW_CLEAR, status);
315 }
316 
317 static FORCEINLINE void swi_mask_all(void)
318 {
319  WR_WORD(GLOBAL_REG_INTR_SW_MASK_SET, GLOBAL_REG_INTR_SW_MASK_SET_IRQ);
320 }
321 
322 static FORCEINLINE void swi_mask(uint32_t mask)
323 {
324  WR_WORD(GLOBAL_REG_INTR_SW_MASK_SET, mask);
325 }
326 
327 static FORCEINLINE void swi_unmask(uint32_t mask)
328 {
329  WR_WORD(GLOBAL_REG_INTR_SW_MASK_CLEAR, mask);
330 }
331 
332 static FORCEINLINE void swi_set(int bit)
333 {
334  WR_WORD(GLOBAL_REG_INTR_SW_SET, (1 << bit));
335 }
336 
337 static INLINE void aon_bod_en(int en)
338 {
339  uint32_t reg = RD_WORD(AON_REG_PMU_DOOPD_REG_1TO4);
340  if (en)
341  reg |= (1 << 8);
342  else
343  reg &= ~(1 << 8);
344 
345  WR_WORD(AON_REG_PMU_DOOPD_REG_1TO4, reg);
346 }
347 
348 static INLINE void aon_bod_thrd(int val)
349 {
350  uint32_t reg = RD_WORD(AON_REG_PMU_DOOPD_REG_1TO4);
351 
352  // BOD threshold
353  // bit[29:30]:
354  // 00: 1.65
355  // 01: 1.70
356  // 10: 1.75
357  // 11: 1.8
358  reg &= ~(0x3 << 29);
359  reg |= (val & 0x3) << 29;
360 
361  WR_WORD(AON_REG_PMU_DOOPD_REG_1TO4, reg);
362 }
363 
364 static INLINE void arm_wic(int en)
365 {
367  if (en) {
368  WR_WORD(GLOBAL_REG_CM4_WIC_EN_REQ, 1);
369  while (!(RD_WORD(GLOBAL_REG_CM4_WIC_EN_ACK) & GLOBAL_REG_CM4_WIC_EN_ACK_STS_CM4_WIC_EN_ACK));
370  } else {
371  WR_WORD(GLOBAL_REG_CM4_WIC_EN_REQ, 0);
372  }
373 }
374 
375 static FORCEINLINE uint32_t osc_xo_int_status(void)
376 {
377  return (RD_WORD(GLOBAL2_REG_OSC_XO_INTR_STATUS));
378 }
379 
380 static FORCEINLINE uint32_t osc_xo_int_mask_status(void)
381 {
382  return (RD_WORD(GLOBAL2_REG_OSC_XO_INTR_MASK_STATUS));
383 }
384 
385 static FORCEINLINE void osc_xo_int_clear_all(void)
386 {
387  WR_WORD(GLOBAL2_REG_OSC_XO_INTR_CLEAR, GLOBAL2_REG_OSC_XO_INTR_CLEAR_IRQ);
388 }
389 
390 static FORCEINLINE void osc_xo_int_clear(uint32_t status)
391 {
392  WR_WORD(GLOBAL2_REG_OSC_XO_INTR_CLEAR, status);
393 }
394 
395 static FORCEINLINE void osc_xo_int_mask_all(void)
396 {
397  WR_WORD(GLOBAL2_REG_OSC_XO_INTR_MASK_SET, GLOBAL2_REG_OSC_XO_INTR_MASK_SET_IRQ);
398 }
399 
400 static FORCEINLINE void osc_xo_int_mask(uint32_t mask)
401 {
402  WR_WORD(GLOBAL2_REG_OSC_XO_INTR_MASK_SET, mask);
403 }
404 
405 static FORCEINLINE void osc_xo_int_unmask(uint32_t unmask)
406 {
407  WR_WORD(GLOBAL2_REG_OSC_XO_INTR_MASK_CLEAR, unmask);
408 }
409 
410 static INLINE void xo_stage_delay(uint32_t stage0_delay, uint32_t stage1_delay)
411 {
412  uint32_t reg;
413  reg = RD_WORD(AON_REG_XO_EN_STAGE_CTRL);
414  reg &= ~(AON_REG_XO_EN_STAGE_CTRL_CTL_XO_STAGE0_DURATION | AON_REG_XO_EN_STAGE_CTRL_CTL_XO_STAGE1_DURATION);
415  reg |= (stage0_delay<<AON_REG_XO_EN_STAGE_CTRL_CTL_XO_STAGE0_DURATION_SHIFT);
416  reg |= (stage1_delay<<AON_REG_XO_EN_STAGE_CTRL_CTL_XO_STAGE1_DURATION_SHIFT);
417  WR_WORD(AON_REG_XO_EN_STAGE_CTRL, reg);
418 }
419 
420 static FORCEINLINE uint32_t nmi_int_status(void)
421 {
422  return (RD_WORD(GLOBAL_REG_NMI_STATUS));
423 }
424 
425 static FORCEINLINE uint32_t nmi_int_mask_status(void)
426 {
427  return (RD_WORD(GLOBAL_REG_NMI_MASK_STATUS));
428 }
429 
430 static FORCEINLINE void nmi_int_clear_all(void)
431 {
432  WR_WORD(GLOBAL_REG_NMI_CLEAR, GLOBAL_REG_NMI_CLEAR_NMI);
433 }
434 
435 static FORCEINLINE void nmi_int_clear(int id)
436 {
437  WR_WORD(GLOBAL_REG_NMI_CLEAR, (1 << id));
438 }
439 
440 static FORCEINLINE void nmi_int_mask_all(void)
441 {
442  WR_WORD(GLOBAL_REG_NMI_MASK_SET, GLOBAL_REG_NMI_MASK_SET_NMI);
443 }
444 
445 static FORCEINLINE void nmi_int_mask(uint32_t mask)
446 {
447  WR_WORD(GLOBAL_REG_NMI_MASK_SET, mask);
448 }
449 
450 static FORCEINLINE void nmi_int_unmask(uint32_t unmask)
451 {
452  WR_WORD(GLOBAL_REG_NMI_MASK_CLEAR, unmask);
453 }
454 
455 static INLINE void snapshot_ctrl(uint32_t type, int en)
456 {
457  uint32_t reg = RD_WORD(GLOBAL2_REG_SNAPSHOT_CTRL);
458  if (en) {
459  reg |= 1<<type;
460  } else {
461  reg &= ~(1<<type);
462  }
463  WR_WORD(GLOBAL2_REG_SNAPSHOT_CTRL, reg);
464 }
465 
466 static FORCEINLINE uint32_t snapshot_fr_ipmac_cnt(void)
467 {
468  return RD_WORD(GLOBAL2_REG_SNAPSHOT_VAL_IPMAC_FR);
469 }
470 /*
471  * APIs
472  ****************************************************************************************
473  */
474 
483 void hal_global_pre_init(void);
484 
493 void hal_global_post_init(void);
494 
503 void hal_global_suspend(void);
504 
513 void hal_global_resume(void);
514 
523 void hal_global_cpu_reset(void);
524 
525 
534 void hal_global_sys_reset(void);
535 void hal_global_pd0_reset(void);
536 
545 void hal_global_close_sys_uart(void);
546 
563 int hal_bod_enable(int bod_thrd_mv, void *arg, void (*callback)(void *arg));
564 
574 void hal_bod_disable(void);
575 
584 int RTC_timer_start(void);
585 
594 void RTC_timer_stop(void);
595 
604 uint64_t RTC_timer_get_tick(void);
605 
614 uint64_t RTC_timer_get_time(void);
615 
626 uint64_t RTC_timer_diff(uint64_t s_tick, uint64_t e_tick);
627 
636 uint32_t RTC_timer_get_tick_low(void);
637 
648 uint32_t RTC_timer_diff_low(uint32_t s_tick, uint32_t e_tick);
649 
660 void hal_ble_isr_register(void *arg, void (*callback)(void *));
661 
669 void hal_ble_isr_deregister(void);
670 
683 void hal_glb_isr_register(int irq_id, int prio, void *arg, void (*callback)(void *));
684 
694 void hal_glb_isr_deregister(int irq_id);
695 
696 
707 int hal_swi_int_prio(int prio);
719 int hal_swi_register(void *arg, void (*callback)(void *));
720 
730 void hal_swi_deregister(int index);
731 
741 void hal_swi_set(int index);
742 
755 void hal_osc_xo_isr_register(int irq_id, int prio, void *arg, void (*callback)(void *));
756 
766 void hal_osc_xo_isr_deregister(int irq_id);
767 
779 void hal_nmi_register(int irq_id, void *arg, void (*callback)(void *));
780 
790 void hal_nmi_deregister(int irq_id);
791 
804 void * hal_sys_uart_hdl(void);
805 
815 void RAM_PM delay_us(uint32_t us);
816 
818 
819 #endif // HAL_GLOBAL_H
void hal_osc_xo_isr_register(int irq_id, int prio, void *arg, void(*callback)(void *))
Register Osc and XO shared interupts.
void hal_nmi_deregister(int irq_id)
Deregister a NMI interrupt.
void hal_global_suspend(void)
Globally save system (such as system tick...) registers before deep sleep.
int RTC_timer_start(void)
Start the RTC timer.
void hal_swi_set(int index)
Trigger a Software interrupt.
void hal_nmi_register(int irq_id, void *arg, void(*callback)(void *))
Register a NMI interrupt.
void RAM_PM delay_us(uint32_t us)
Delay in tight loop.
int hal_bod_enable(int bod_thrd_mv, void *arg, void(*callback)(void *arg))
Enable BOD , set a bod_thrd_mv will return a closest value. Detail please see: <<IN602F0 reference ma...
int hal_swi_register(void *arg, void(*callback)(void *))
Register a Software interrupt index. There are max 4 SWI.
void hal_global_cpu_reset(void)
Reset the CPU (cold boot)
void * hal_sys_uart_hdl(void)
Enable/disable external PA.
void RTC_timer_stop(void)
Stop the RTC timer.
void hal_global_post_init(void)
Globally initialize system (such as debug, RTC...) after OS starting.
uint32_t RTC_timer_diff_low(uint32_t s_tick, uint32_t e_tick)
Calculate tick difference convert to time.
int hal_swi_int_prio(int prio)
Set Software interrupt priority. There are max 4 SWI.
uint64_t RTC_timer_diff(uint64_t s_tick, uint64_t e_tick)
Calculate tick difference convert to time.
void hal_swi_deregister(int index)
Deregister a Software interrupt index.
void hal_osc_xo_isr_deregister(int irq_id)
Deregister an Osc and XO shared interupt.
void hal_global_sys_reset(void)
Reset the system (cold boot)
void hal_global_pre_init(void)
Globally initialize system (such as clock...) before OS starting.
void hal_glb_isr_register(int irq_id, int prio, void *arg, void(*callback)(void *))
Globally manage shared interupts.
uint64_t RTC_timer_get_time(void)
Get the RTC timer in micro second.
void hal_glb_isr_deregister(int irq_id)
Deregister global shared interupts.
void hal_global_resume(void)
Globally resume system (such as system tick...) registers after wake up.
void hal_bod_disable(void)
Disable BOD.
void hal_ble_isr_register(void *arg, void(*callback)(void *))
Register BLE interupt.
void hal_ble_isr_deregister(void)
Deregister BLE interupt.
uint32_t RTC_timer_get_tick_low(void)
Get the RTC timer ticks low part.
void hal_global_close_sys_uart(void)
Close debug uart.
uint64_t RTC_timer_get_tick(void)
Get the RTC timer ticks (64 bits)