• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 // Copyright (C) 2022 Beken Corporation
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 <common/bk_include.h>
17 #include "bk_intc.h"
18 #include "bk_icu.h"
19 #include <os/mem.h>
20 #include "gpio_hal.h"
21 #include "gpio_map.h"
22 #include "gpio_driver.h"
23 #include "gpio_driver_base.h"
24 #include "icu_driver.h"
25 #include <driver/gpio_types.h>
26 
27 extern gpio_driver_t s_gpio;
28 
29 
30 #define GPIO_RETURN_ON_INVALID_PERIAL_MODE(mode, mode_max) do {\
31 				if ((mode) >= (mode_max)) {\
32 					return BK_ERR_GPIO_SET_INVALID_FUNC_MODE;\
33 				}\
34 			} while(0)
35 
gpio_dev_map(gpio_id_t gpio_id,gpio_dev_t dev)36 bk_err_t gpio_dev_map(gpio_id_t gpio_id, gpio_dev_t dev)
37 {
38 	/* Restore a configuration that is not a secondary function to its initial state. */
39 	gpio_hal_output_enable(&s_gpio.hal, gpio_id, 0);
40 	gpio_hal_input_enable(&s_gpio.hal, gpio_id, 0);
41 	gpio_hal_pull_enable(&s_gpio.hal, gpio_id, 0);
42 	gpio_hal_disable_interrupt(&s_gpio.hal, gpio_id);
43 	gpio_hal_func_map(&s_gpio.hal, gpio_id, dev);
44 
45 	return BK_OK;
46 }
47 
gpio_dev_unmap(gpio_id_t gpio_id)48 bk_err_t gpio_dev_unmap(gpio_id_t gpio_id)
49 {
50 	/* Restore a configuration that is not a secondary function to its initial state. */
51 	gpio_hal_output_enable(&s_gpio.hal, gpio_id, 0);
52 	gpio_hal_input_enable(&s_gpio.hal, gpio_id, 0);
53 	gpio_hal_pull_enable(&s_gpio.hal, gpio_id, 0);
54 	gpio_hal_disable_interrupt(&s_gpio.hal, gpio_id);
55 	gpio_hal_func_unmap(&s_gpio.hal, gpio_id);
56 
57 	return BK_OK;
58 }
59 
gpio_i2c1_sel(gpio_i2c1_map_mode_t mode)60 bk_err_t gpio_i2c1_sel(gpio_i2c1_map_mode_t mode)
61 {
62 	GPIO_RETURN_ON_INVALID_PERIAL_MODE(mode, GPIO_I2C1_MAP_MODE_MAX);
63 
64 	GPIO_MAP_TABLE(GPIO_I2C1_USED_GPIO_NUM, GPIO_I2C1_MAP_MODE_MAX, i2c1_gpio_map) = GPIO_I2C1_MAP_TABLE;
65 
66 	gpio_hal_devs_map(&s_gpio.hal, i2c1_gpio_map[mode].gpio_bits, i2c1_gpio_map[mode].devs, GPIO_I2C1_USED_GPIO_NUM);
67 
68 	GPIO_LOGI("Warning bk7256 USE PLIC  NOT icu\n");
69 
70 	return BK_OK;
71 }
72 
gpio_spi_sel(gpio_spi1_map_mode_t mode)73 bk_err_t gpio_spi_sel(gpio_spi1_map_mode_t mode)
74 {
75 	GPIO_RETURN_ON_INVALID_PERIAL_MODE(mode, GPIO_SPI_MAP_MODE_MAX);
76 
77 	GPIO_MAP_TABLE(GPIO_SPI0_USED_GPIO_NUM, GPIO_SPI_MAP_MODE_MAX, spi_gpio_map) = GPIO_SPI0_MAP_TABLE;
78 
79 	gpio_hal_devs_map(&s_gpio.hal, spi_gpio_map[mode].gpio_bits, spi_gpio_map[mode].devs, GPIO_SPI0_USED_GPIO_NUM);
80 
81 	GPIO_LOGI("Warning bk7256 USE PLIC  NOT icu\n");
82 
83 	return BK_OK;
84 }
85 
86 static gpio_jtag_map_mode_t gpio_jtag_default_mode = GPIO_JTAG_MAP_MODE;
gpio_jtag_sel(gpio_jtag_map_mode_t mode)87 bk_err_t gpio_jtag_sel(gpio_jtag_map_mode_t mode)
88 {
89 	GPIO_RETURN_ON_INVALID_PERIAL_MODE(mode, GPIO_JTAG_MAP_MODE_MAX);
90 	if(gpio_jtag_default_mode == mode)
91 		return BK_OK;
92 	else
93 		gpio_jtag_default_mode = mode;
94 
95 	GPIO_MAP_TABLE(GPIO_JTAG_USED_GPIO_NUM, GPIO_JTAG_MAP_MODE_MAX, jtag_gpio_map) = GPIO_JTAG_MAP_TABLE;
96 
97 	for(gpio_jtag_map_mode_t sel_mode = GPIO_JTAG_MAP_MODE; sel_mode < GPIO_JTAG_MAP_MODE_MAX; sel_mode++)
98 	{
99 		for(int gpio_index = 0; gpio_index < GPIO_NUM; gpio_index++)
100 		{
101 			if (jtag_gpio_map[sel_mode].gpio_bits & BIT64(gpio_index)) {
102 				GPIO_LOGI("gpio_jtag_sel unmap gpio_index = %d\r\n", gpio_index);
103 				gpio_hal_func_unmap(&s_gpio.hal, gpio_index);
104 			}
105 		}
106 	}
107 
108 	gpio_hal_devs_map(&s_gpio.hal, jtag_gpio_map[mode].gpio_bits, jtag_gpio_map[mode].devs, GPIO_JTAG_USED_GPIO_NUM);
109 
110 	GPIO_LOGD("Warning bk7256 USE PLIC  NOT icu\n");
111 
112 	return BK_OK;
113 }
114 
115