nxp_gpio.c 3 KB
Newer Older
Pankaj Gupta's avatar
Pankaj Gupta committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright 2021 NXP
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include <common/debug.h>
#include <lib/mmio.h>
#include <nxp_gpio.h>

static gpio_init_info_t *gpio_init_info;

void gpio_init(gpio_init_info_t *gpio_init_data)
{
	gpio_init_info = gpio_init_data;
}

/* This function set GPIO pin for raising POVDD. */
int set_gpio_bit(uint32_t *gpio_base_addr,
		 uint32_t bit_num)
{
	uint32_t val = 0U;
	uint32_t *gpdir = NULL;
	uint32_t *gpdat = NULL;

	if (gpio_init_info == NULL) {
		ERROR("GPIO is not initialized.\n");
		return GPIO_FAILURE;
	}

	gpdir = gpio_base_addr + GPDIR_REG_OFFSET;
	gpdat = gpio_base_addr + (GPDAT_REG_OFFSET >> 2);

	/*
	 * Set the corresponding bit in direction register
	 * to configure the GPIO as output.
	 */
	val = gpio_read32(gpdir);
	val = val | bit_num;
	gpio_write32(gpdir, val);

	/* Set the corresponding bit in GPIO data register */
	val = gpio_read32(gpdat);
	val = val | bit_num;
	gpio_write32(gpdat, val);

	val = gpio_read32(gpdat);

	if ((val & bit_num) == 0U) {
		return GPIO_FAILURE;
	}

	return GPIO_SUCCESS;
}

/* This function reset GPIO pin set for raising POVDD. */
int clr_gpio_bit(uint32_t *gpio_base_addr, uint32_t bit_num)
{
	uint32_t val = 0U;
	uint32_t *gpdir = NULL;
	uint32_t *gpdat = NULL;


	if (gpio_init_info == NULL) {
		ERROR("GPIO is not initialized.\n");
		return GPIO_FAILURE;
	}

	gpdir = gpio_base_addr + GPDIR_REG_OFFSET;
	gpdat = gpio_base_addr + GPDAT_REG_OFFSET;

	/*
	 * Reset the corresponding bit in direction and data register
	 * to configure the GPIO as input.
	 */
	val = gpio_read32(gpdat);
	val = val & ~(bit_num);
	gpio_write32(gpdat, val);

	val = gpio_read32(gpdat);

	val = gpio_read32(gpdir);
	val = val & ~(bit_num);
	gpio_write32(gpdir, val);

	val = gpio_read32(gpdat);

	if ((val & bit_num) != 0U) {
		return GPIO_FAILURE;
	}

	return GPIO_SUCCESS;
}

uint32_t *select_gpio_n_bitnum(uint32_t povdd_gpio, uint32_t *bit_num)
{
	uint32_t *ret_gpio;
	uint32_t povdd_gpio_val = 0U;
	uint32_t gpio_num = 0U;

	if (gpio_init_info == NULL) {
		ERROR("GPIO is not initialized.\n");
	}
	/*
	 * Subtract 1 from fuse_hdr povdd_gpio value as
	 * for 0x1 value, bit 0 is to be set
	 * for 0x20 value i.e 32, bit 31 i.e. 0x1f is to be set.
	 * 0x1f - 0x00 : GPIO_1
	 * 0x3f - 0x20 : GPIO_2
	 * 0x5f - 0x40 : GPIO_3
	 * 0x7f - 0x60 : GPIO_4
	 */
	povdd_gpio_val = (povdd_gpio - 1U) & GPIO_SEL_MASK;

	/* Right shift by 5 to divide by 32 */
	gpio_num = povdd_gpio_val >> GPIO_ID_BASE_ADDR_SHIFT;
	*bit_num = 1U << (GPIO_BITS_PER_BASE_REG
			  - (povdd_gpio_val & GPIO_BIT_MASK)
			  - 1U);

	switch (gpio_num) {
	case GPIO_0:
		ret_gpio = (uint32_t *) gpio_init_info->gpio1_base_addr;
		break;
	case GPIO_1:
		ret_gpio = (uint32_t *) gpio_init_info->gpio2_base_addr;
		break;
	case GPIO_2:
		ret_gpio = (uint32_t *) gpio_init_info->gpio3_base_addr;
		break;
	case GPIO_3:
		ret_gpio = (uint32_t *) gpio_init_info->gpio4_base_addr;
		break;
	default:
		ret_gpio = NULL;
	}

	if (ret_gpio == NULL) {
		INFO("GPIO_NUM = %d doesn't exist.\n", gpio_num);
	}

	return ret_gpio;
}