1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
4 #include <console/console.h>
8 * Return family number and internal pad number in that community by pad number
9 * and which community it is in.
11 uint16_t gpio_family_number(uint8_t community
, uint8_t pad
)
14 * Refer to BSW BIOS Writers Guide, Table "Family Number". BSW has 4 GPIO communities.
15 * Each community has up to 7 families and each family contains a range of Pad numbers.
16 * The number in the array is the maximum no. of that range.
17 * For example: East community, family 0, Pad 0~11.
19 static const uint8_t community_base
[GPIO_COMMUNITY_COUNT
]
20 [GPIO_FAMILIES_MAX_PER_COMM
+ 1] = {
21 {0, 8, 16, 24, 32, 40, 48, 56}, /* Southwest */
22 {0, 9, 22, 34, 46, 59, 59, 59}, /* North */
23 {0, 12, 24, 24, 24, 24, 24, 24}, /* East */
24 {0, 8, 20, 26, 34, 44, 55, 55} /* Southeast */
29 /* Validate the pad number */
30 if (pad
> community_base
[community
][7])
31 die("Pad number is out of range!");
33 /* Locate the family number for the pad */
34 base
= &community_base
[community
][0];
35 for (i
= 0; i
< 7; i
++) {
36 if ((pad
>= base
[0]) && (pad
< base
[1]))
41 /* Family number in high byte and inner pad number in lowest byte */
42 return (i
<< 8) + pad
- *base
;
46 * Return pad configuration register offset by pad number and which community it is in.
48 uint32_t *gpio_pad_config_reg(uint8_t community
, uint8_t pad
)
51 uint32_t *pad_config_reg
;
53 /* Get the GPIO family number */
54 fpad
= gpio_family_number(community
, pad
);
57 * Refer to BSW BIOS Writers Guide, Table "Per Pad Memory Space Registers Addresses"
58 * for the Pad configuration register calculation.
60 pad_config_reg
= (uint32_t *)(COMMUNITY_BASE(community
) + FAMILY_PAD_REGS_OFF
+
61 (FAMILY_PAD_REGS_SIZE
* (fpad
>> 8)) + (GPIO_REGS_SIZE
* (fpad
& 0xff)));
63 return pad_config_reg
;
66 static int gpio_get_community_num(gpio_t gpio_num
, int *pad
)
70 if (gpio_num
>= GP_SW_00
&& gpio_num
<= GP_SW_97
) {
72 *pad
= gpio_num
% GP_SOUTHWEST_COUNT
;
74 } else if (gpio_num
>= GP_NC_00
&& gpio_num
<= GP_NC_72
) {
76 *pad
= gpio_num
% GP_SOUTHWEST_COUNT
;
78 } else if (gpio_num
>= GP_E_00
&& gpio_num
<= GP_E_26
) {
80 *pad
= gpio_num
% (GP_SOUTHWEST_COUNT
+ GP_NORTH_COUNT
);
84 *pad
= gpio_num
% (GP_SOUTHWEST_COUNT
+ GP_NORTH_COUNT
+ GP_EAST_COUNT
);
89 static void gpio_config_pad(gpio_t gpio_num
, const struct soc_gpio_map
*cfg
)
93 uint32_t *pad_config0_reg
;
94 uint32_t *pad_config1_reg
;
96 if (gpio_num
> MAX_GPIO_CNT
)
98 /* Get GPIO Community based on GPIO_NUMBER */
99 comm
= gpio_get_community_num(gpio_num
, &pad_num
);
101 pad_config0_reg
= gpio_pad_config_reg(comm
, pad_num
);
103 pad_config1_reg
= pad_config0_reg
+ 1;
105 write32(pad_config0_reg
, cfg
->pad_conf0
);
106 write32(pad_config1_reg
, cfg
->pad_conf1
);
109 void gpio_input_pullup(gpio_t gpio_num
)
111 struct soc_gpio_map cfg
= GPIO_INPUT_PU_20K
;
112 gpio_config_pad(gpio_num
, &cfg
);
115 void gpio_input_pulldown(gpio_t gpio_num
)
117 struct soc_gpio_map cfg
= GPIO_INPUT_PD_20K
;
118 gpio_config_pad(gpio_num
, &cfg
);
121 void gpio_input(gpio_t gpio_num
)
123 struct soc_gpio_map cfg
= GPIO_INPUT_NO_PULL
;
124 gpio_config_pad(gpio_num
, &cfg
);
127 int gpio_get(gpio_t gpio_num
)
131 uint32_t *pad_config0_reg
;
134 if (gpio_num
> MAX_GPIO_CNT
)
137 /* Get GPIO Community based on GPIO_NUMBER */
138 comm
= gpio_get_community_num(gpio_num
, &pad_num
);
140 pad_config0_reg
= gpio_pad_config_reg(comm
, pad_num
);
142 pad_value
= read32(pad_config0_reg
);
144 return pad_value
& PAD_RX_BIT
;