Skip to content

Commit fd739c2

Browse files
unicornxRbb666
authored andcommitted
bsp: k230: add hardlock driver
This hardlock is required by some other drivers, such as sysctl power. This driver can be enable/disabled by configuration. Signed-off-by: Wang Chen <[email protected]>
1 parent 3775ea0 commit fd739c2

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-0
lines changed

bsp/k230/.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,7 @@ CONFIG_PKG_ZLIB_VER="latest"
14891489
#
14901490
# Drivers Configuration
14911491
#
1492+
CONFIG_BSP_USING_HARDLOCK=y
14921493
# CONFIG_BSP_USING_SDIO is not set
14931494
# end of Drivers Configuration
14941495

bsp/k230/board/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
menu "Drivers Configuration"
22

3+
config BSP_USING_HARDLOCK
4+
bool "Enable Hard-Lock"
5+
default y
6+
37
menuconfig BSP_USING_SDIO
48
bool "Enable SDIO"
59
select RT_USING_SDIO
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# RT-Thread building script for component
2+
3+
from building import *
4+
5+
cwd = GetCurrentDir()
6+
src = []
7+
CPPPATH = [cwd]
8+
9+
if GetDepend('BSP_USING_HARDLOCK'):
10+
src += ['drv_hardlock.c']
11+
12+
group = DefineGroup('HARDLOCK', src, depend = [], CPPPATH = CPPPATH)
13+
14+
Return('group')
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
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+
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+
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+
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);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
#ifndef DRV_HARDLOCK_H__
27+
#define DRV_HARDLOCK_H__
28+
#include <rtdef.h>
29+
30+
typedef enum k230_hardlock_type
31+
{
32+
HARDLOCK_GPIO = 0,
33+
HARDLOCK_KPU = 1,
34+
HARDLOCK_TS = 2,
35+
HARDLOCK_DISP = 3,
36+
HARDLOCK_DISP_CPU0 = 4,
37+
HARDLOCK_DISP_CPU1 = 5,
38+
HARDLOCK_HASH = 6,
39+
HARDLOCK_AES = 7,
40+
HARDLOCK_SM4 = 8,
41+
HARDLOCK_PDMA = 9,
42+
HARDLOCK_MAX = 128
43+
} hardlock_type;
44+
45+
#ifdef BSP_USING_HARDLOCK
46+
extern int kd_hardlock_lock(hardlock_type num);
47+
extern void kd_hardlock_unlock(hardlock_type num);
48+
extern int kd_request_lock(hardlock_type num);
49+
#else
50+
rt_inline int kd_hardlock_lock(hardlock_type num)
51+
{ return 0; }
52+
rt_inline void kd_hardlock_unlock(hardlock_type num)
53+
{}
54+
rt_inline int kd_request_lock(hardlock_type num)
55+
{ return num; }
56+
#endif
57+
58+
#endif

bsp/k230/rtconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@
559559

560560
/* Drivers Configuration */
561561

562+
#define BSP_USING_HARDLOCK
562563
/* end of Drivers Configuration */
563564
#define BOARD_fpgac908
564565
#define __STACKSIZE__ 65536

0 commit comments

Comments
 (0)