1 /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * 1. Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * 2. Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
12 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
16 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <rtthread.h>
27 #include <rthw.h>
28 #include <rtdevice.h>
29 #include <riscv_io.h>
30 #include <string.h>
31 #include "ioremap.h"
32 #include "board.h"
33 #include "drv_hardlock.h"
34 #include <rtdbg.h>
35
36 #define DBG_TAG "HARDLOCK"
37
38 #ifdef RT_DEBUG
39 #define DBG_LVL DBG_LOG
40 #else
41 #define DBG_LVL DBG_WARNING
42 #endif
43 #define DBG_COLOR
44
45 struct device_hardlock
46 {
47 volatile void *hw_base;
48 char used[HARDLOCK_MAX];
49 };
50 static struct device_hardlock hardlock;
51
kd_hardlock_lock(hardlock_type num)52 int kd_hardlock_lock(hardlock_type num)
53 {
54 if(num < 0 || num >= HARDLOCK_MAX)
55 return -1;
56
57 if(!readl(hardlock.hw_base + num * 0x4))
58 {
59 LOG_D("hardlock-%d locked\n", num);
60 return 0;
61 }
62
63 LOG_D("hardlock-%d is busy\n", num);
64 return -1;
65 }
66 RTM_EXPORT(kd_hardlock_lock);
67
kd_hardlock_unlock(hardlock_type num)68 void kd_hardlock_unlock(hardlock_type num)
69 {
70 if(num < 0 || num >= HARDLOCK_MAX)
71 return;
72
73 if(readl(hardlock.hw_base + num * 0x4))
74 {
75 writel(0x0, hardlock.hw_base + num * 0x4);
76 }
77 LOG_D("hardlock-%d unlock\n", num);
78 }
79 RTM_EXPORT(kd_hardlock_unlock);
80
kd_request_lock(hardlock_type num)81 int kd_request_lock(hardlock_type num)
82 {
83 if(num < 0 || num >= HARDLOCK_MAX)
84 return -1;
85
86 if(!hardlock.used[num])
87 {
88 hardlock.used[num] = 1;
89 return 0;
90 }
91
92 LOG_E("request hardlock failed, hardlock-%d is used\n", num);
93 return -1;
94 }
95 RTM_EXPORT(kd_request_lock);
96
rt_hw_hardlock_init(void)97 int rt_hw_hardlock_init(void)
98 {
99 struct device_hardlock *hard = &hardlock;
100 hard->hw_base = 0xA0 + rt_ioremap((void *)MAILBOX_BASE_ADDR, MAILBOX_IO_SIZE);
101 if(hard->hw_base == RT_NULL)
102 {
103 rt_kprintf("hardlock ioremap error\n");
104 return -1;
105 }
106
107 memset(hard->used, 0, sizeof(hard->used));
108 #if 0
109 rt_kprintf("canaan hardlock init OK\n");
110 #endif
111 return 0;
112 }
113 INIT_BOARD_EXPORT(rt_hw_hardlock_init);