rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018 MediaTek Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @file mtk_wrapper.h |
| 26 | * @brief Header file of LK 2.0 wrapper for post-init |
| 27 | * |
| 28 | * LK 2.0 wrapper includes interface functions, which provides software |
| 29 | * environment functions. Several interfaces, virtual/physical address |
| 30 | * translation, muxtex/semaphore and debug are provided to execute the |
| 31 | * corresponding system calls. |
| 32 | */ |
| 33 | |
| 34 | #pragma once |
| 35 | |
| 36 | #include <stddef.h> |
| 37 | #include <stdint.h> |
| 38 | #include <kernel/mutex.h> |
| 39 | #include <kernel/semaphore.h> |
| 40 | #include <kernel/vm.h> |
| 41 | #include <debug.h> |
| 42 | #include <platform.h> |
| 43 | |
| 44 | #ifndef __REG_H |
| 45 | /* low level macros for accessing memory mapped hardware registers */ |
| 46 | #define REG64(addr) ((volatile uint64_t *)(uintptr_t)(addr)) |
| 47 | #define REG32(addr) ((volatile uint32_t *)(uintptr_t)(addr)) |
| 48 | #define REG16(addr) ((volatile uint16_t *)(uintptr_t)(addr)) |
| 49 | #define REG8(addr) ((volatile uint8_t *)(uintptr_t)(addr)) |
| 50 | |
| 51 | #define RMWREG64(addr, startbit, width, val) *REG64(addr) = (*REG64(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit)) |
| 52 | #define RMWREG32(addr, startbit, width, val) *REG32(addr) = (*REG32(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit)) |
| 53 | #define RMWREG16(addr, startbit, width, val) *REG16(addr) = (*REG16(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit)) |
| 54 | #define RMWREG8(addr, startbit, width, val) *REG8(addr) = (*REG8(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit)) |
| 55 | |
| 56 | #define writel(v, a) (*REG32(a) = (v)) |
| 57 | #define readl(a) (*REG32(a)) |
| 58 | #define writeb(v, a) (*REG8(a) = (v)) |
| 59 | #define readb(a) (*REG8(a)) |
| 60 | #endif |
| 61 | |
| 62 | #ifndef __DEBUG_H |
| 63 | #if !defined(LK_DEBUGLEVEL) |
| 64 | #define LK_DEBUGLEVEL 0 |
| 65 | #endif |
| 66 | |
| 67 | /* debug levels */ |
| 68 | #define CRITICAL 0 |
| 69 | #define ALWAYS 0 |
| 70 | #define INFO 1 |
| 71 | #define SPEW 2 |
| 72 | |
| 73 | #define dprintf(level, x...) do { if ((level) <= LK_DEBUGLEVEL) { _dprintf(x); } } while (0) |
| 74 | #endif |
| 75 | |
| 76 | enum wrap_handler_return { |
| 77 | WRAP_INT_NO_RESCHEDULE = 0, |
| 78 | WRAP_INT_RESCHEDULE, |
| 79 | }; |
| 80 | |
| 81 | typedef mutex_t wrap_mutex; |
| 82 | typedef semaphore_t wrap_sem; |
| 83 | typedef enum wrap_handler_return (*wrap_int_handler)(void *arg); |
| 84 | |
| 85 | //mutex interface functions |
| 86 | void plat_wrap_mutex_init(wrap_mutex *m); |
| 87 | void plat_wrap_mutex_destroy(wrap_mutex *m); |
| 88 | status_t plat_wrap_mutex_acquire_timeout(wrap_mutex *m, uint32_t timeout); |
| 89 | status_t plat_wrap_mutex_acquire(wrap_mutex *m); |
| 90 | status_t plat_wrap_mutex_release(wrap_mutex *m); |
| 91 | //semaphore interface functions |
| 92 | void plat_wrap_sem_init(wrap_sem *sem, unsigned int value); |
| 93 | void plat_wrap_sem_destroy(wrap_sem *sem); |
| 94 | int plat_wrap_sem_post(wrap_sem *sem); |
| 95 | status_t plat_wrap_sem_wait(wrap_sem *sem); |
| 96 | status_t plat_wrap_sem_trywait(wrap_sem *sem); |
| 97 | status_t plat_wrap_sem_timedwait(wrap_sem *sem, uint32_t timeout); |
| 98 | //virtual/physical address translation function |
| 99 | void *plat_wrap_paddr_to_kvaddr(unsigned long pa); |
| 100 | unsigned long plat_wrap_kvaddr_to_paddr(void *ptr); |
| 101 | //system delay function |
| 102 | void plat_wrap_delay(uint32_t usecs); |
| 103 | //interrupt function |
| 104 | void plat_wrap_register_int_handler(unsigned int vector, wrap_int_handler handler, void *arg); |
| 105 | int plat_wrap_unmask_interrupt(unsigned int vector); |
| 106 | int plat_wrap_mask_interrupt(unsigned int vector); |
| 107 | void plat_wrap_irq_set_polarity(unsigned int vector, unsigned int polarity); |
| 108 | void plat_wrap_irq_set_sensitive(unsigned int vector, unsigned int sens); |