1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hi_flashboot_gpio.h"
17
18 #include <hi_types.h>
19 #include <hi3861_platform.h>
20
21 #define GPIO_SWPORT_DR 0x00
22 #define GPIO_SWPORT_DDR 0x04
23 #define GPIO_INTEN 0x30
24 #define GPIO_INTMASK 0x34
25 #define GPIO_INTTYPE_LEVEL 0x38
26 #define GPIO_INT_POLARITY 0x3c
27 #define GPIO_INTSTATUS 0x40
28 #define GPIO_RAWINTSTATUS 0x44
29 #define GPIO_PORT_EOI 0x4c
30 #define GPIO_EXT_PORT 0x50
31
hi_gpio_get_dir(hi_gpio_idx id,hi_gpio_dir * dir)32 hi_u32 hi_gpio_get_dir(hi_gpio_idx id, hi_gpio_dir *dir)
33 {
34 if (id >= HI_GPIO_IDX_MAX || dir == HI_NULL) {
35 return HI_ERR_GPIO_INVALID_PARAMETER;
36 }
37
38 hi_u16 reg_val = 0;
39 // Obtains the input or output. The default value is 0.
40 hi_reg_read16((HI_GPIO_REG_BASE + GPIO_SWPORT_DDR), reg_val);
41 hi_io_dir_get(reg_val, (hi_u16) id, dir);
42
43 return HI_ERR_SUCCESS;
44 }
45
hi_gpio_get_output_val(hi_gpio_idx id,hi_gpio_value * val)46 hi_u32 hi_gpio_get_output_val(hi_gpio_idx id, hi_gpio_value* val)
47 {
48 if ((id >= HI_GPIO_IDX_MAX) || (val == HI_NULL)) {
49 return HI_ERR_GPIO_INVALID_PARAMETER;
50 }
51
52 hi_u16 reg_val = 0;
53
54 hi_reg_read16((HI_GPIO_REG_BASE + GPIO_SWPORT_DR), reg_val);
55 hi_io_val_get(reg_val, (hi_u16) id, val);
56
57 return HI_ERR_SUCCESS;
58 }
59
hi_gpio_set_output_val(hi_gpio_idx id,hi_gpio_value val)60 hi_u32 hi_gpio_set_output_val(hi_gpio_idx id, hi_gpio_value val)
61 {
62 if (id >= HI_GPIO_IDX_MAX || val > HI_GPIO_VALUE1) {
63 return HI_ERR_GPIO_INVALID_PARAMETER;
64 }
65
66 hi_u16 reg_val = 0;
67
68 hi_reg_read16((HI_GPIO_REG_BASE + GPIO_SWPORT_DR), reg_val);
69 hi_io_val_set(val, (hi_u16) id, reg_val);
70 hi_reg_write16((HI_GPIO_REG_BASE + GPIO_SWPORT_DR), reg_val);
71
72 return HI_ERR_SUCCESS;
73 }
74
hi_gpio_get_input_val(hi_gpio_idx id,hi_gpio_value * val)75 hi_u32 hi_gpio_get_input_val(hi_gpio_idx id, hi_gpio_value* val)
76 {
77 if ((id >= HI_GPIO_IDX_MAX) || (val == HI_NULL)) {
78 return HI_ERR_GPIO_INVALID_PARAMETER;
79 }
80 hi_u16 reg_val = 0;
81
82 hi_reg_read16((HI_GPIO_REG_BASE + GPIO_EXT_PORT), reg_val);
83 hi_io_val_get(reg_val, (hi_u16) id, val);
84
85 return HI_ERR_SUCCESS;
86 }
87
hi_gpio_set_dir(hi_gpio_idx id,hi_gpio_dir dir)88 hi_u32 hi_gpio_set_dir(hi_gpio_idx id, hi_gpio_dir dir)
89 {
90 if (id >= HI_GPIO_IDX_MAX || dir > HI_GPIO_DIR_OUT) {
91 return HI_ERR_GPIO_INVALID_PARAMETER;
92 }
93 hi_u16 dir_val = 0;
94
95 hi_reg_read16((HI_GPIO_REG_BASE + GPIO_SWPORT_DDR), dir_val);
96 hi_io_val_set(dir, (hi_u16) id, dir_val);
97 hi_reg_write16((HI_GPIO_REG_BASE + GPIO_SWPORT_DDR), dir_val);
98
99 return HI_ERR_SUCCESS;
100 }
101