Angel Pons | ba38f37 | 2020-04-05 15:46:45 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 2 | |
| 3 | #include <device/mmio.h> |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 4 | #include <gpio.h> |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <soc/gpio.h> |
| 7 | |
| 8 | /* |
| 9 | * Return family number and internal pad number in that community by pad number |
| 10 | * and which community it is in. |
| 11 | */ |
| 12 | uint16_t gpio_family_number(uint8_t community, uint8_t pad) |
| 13 | { |
| 14 | /* |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 15 | * Refer to BSW BIOS Writers Guide, Table "Family Number". BSW has 4 GPIO communities. |
| 16 | * Each community has up to 7 families and each family contains a range of Pad numbers. |
| 17 | * The number in the array is the maximum no. of that range. |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 18 | * For example: East community, family 0, Pad 0~11. |
| 19 | */ |
| 20 | static const uint8_t community_base[GPIO_COMMUNITY_COUNT] |
| 21 | [GPIO_FAMILIES_MAX_PER_COMM + 1] = { |
| 22 | {0, 8, 16, 24, 32, 40, 48, 56}, /* Southwest */ |
| 23 | {0, 9, 22, 34, 46, 59, 59, 59}, /* North */ |
| 24 | {0, 12, 24, 24, 24, 24, 24, 24}, /* East */ |
| 25 | {0, 8, 20, 26, 34, 44, 55, 55} /* Southeast */ |
| 26 | }; |
| 27 | const uint8_t *base; |
| 28 | uint8_t i; |
| 29 | |
| 30 | /* Validate the pad number */ |
| 31 | if (pad > community_base[community][7]) |
| 32 | die("Pad number is out of range!"); |
| 33 | |
| 34 | /* Locate the family number for the pad */ |
| 35 | base = &community_base[community][0]; |
| 36 | for (i = 0; i < 7; i++) { |
| 37 | if ((pad >= base[0]) && (pad < base[1])) |
| 38 | break; |
| 39 | base++; |
| 40 | } |
| 41 | |
| 42 | /* Family number in high byte and inner pad number in lowest byte */ |
| 43 | return (i << 8) + pad - *base; |
| 44 | } |
| 45 | |
| 46 | /* |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 47 | * Return pad configuration register offset by pad number and which community it is in. |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 48 | */ |
| 49 | uint32_t *gpio_pad_config_reg(uint8_t community, uint8_t pad) |
| 50 | { |
| 51 | uint16_t fpad; |
| 52 | uint32_t *pad_config_reg; |
| 53 | |
| 54 | /* Get the GPIO family number */ |
| 55 | fpad = gpio_family_number(community, pad); |
| 56 | |
| 57 | /* |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 58 | * Refer to BSW BIOS Writers Guide, Table "Per Pad Memory Space Registers Addresses" |
| 59 | * for the Pad configuration register calculation. |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 60 | */ |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 61 | pad_config_reg = (uint32_t *)(COMMUNITY_BASE(community) + FAMILY_PAD_REGS_OFF + |
| 62 | (FAMILY_PAD_REGS_SIZE * (fpad >> 8)) + (GPIO_REGS_SIZE * (fpad & 0xff))); |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 63 | |
| 64 | return pad_config_reg; |
| 65 | } |
| 66 | |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 67 | static int gpio_get_community_num(gpio_t gpio_num, int *pad) |
| 68 | { |
| 69 | int comm = 0; |
| 70 | |
| 71 | if (gpio_num >= GP_SW_00 && gpio_num <= GP_SW_97) { |
| 72 | comm = GP_SOUTHWEST; |
| 73 | *pad = gpio_num % GP_SOUTHWEST_COUNT; |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 74 | |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 75 | } else if (gpio_num >= GP_NC_00 && gpio_num <= GP_NC_72) { |
| 76 | comm = GP_NORTH; |
| 77 | *pad = gpio_num % GP_SOUTHWEST_COUNT; |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 78 | |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 79 | } else if (gpio_num >= GP_E_00 && gpio_num <= GP_E_26) { |
| 80 | comm = GP_EAST; |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 81 | *pad = gpio_num % (GP_SOUTHWEST_COUNT + GP_NORTH_COUNT); |
| 82 | |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 83 | } else { |
| 84 | comm = GP_SOUTHEAST; |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 85 | *pad = gpio_num % (GP_SOUTHWEST_COUNT + GP_NORTH_COUNT + GP_EAST_COUNT); |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 86 | } |
| 87 | return comm; |
| 88 | } |
| 89 | |
| 90 | static void gpio_config_pad(gpio_t gpio_num, const struct soc_gpio_map *cfg) |
| 91 | { |
| 92 | int comm = 0; |
Lee Leahy | 6598b91 | 2017-03-16 17:30:09 -0700 | [diff] [blame] | 93 | int pad_num = 0; |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 94 | uint32_t *pad_config0_reg; |
| 95 | uint32_t *pad_config1_reg; |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 96 | |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 97 | if (gpio_num > MAX_GPIO_CNT) |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 98 | return; |
| 99 | /* Get GPIO Community based on GPIO_NUMBER */ |
| 100 | comm = gpio_get_community_num(gpio_num, &pad_num); |
| 101 | /* CONF0 */ |
| 102 | pad_config0_reg = gpio_pad_config_reg(comm, pad_num); |
| 103 | /* CONF1 */ |
| 104 | pad_config1_reg = pad_config0_reg + 1; |
| 105 | |
| 106 | write32(pad_config0_reg, cfg->pad_conf0); |
| 107 | write32(pad_config1_reg, cfg->pad_conf1); |
| 108 | } |
| 109 | |
| 110 | void gpio_input_pullup(gpio_t gpio_num) |
| 111 | { |
| 112 | struct soc_gpio_map cfg = GPIO_INPUT_PU_20K; |
| 113 | gpio_config_pad(gpio_num, &cfg); |
| 114 | } |
| 115 | |
| 116 | void gpio_input_pulldown(gpio_t gpio_num) |
| 117 | { |
| 118 | struct soc_gpio_map cfg = GPIO_INPUT_PD_20K; |
| 119 | gpio_config_pad(gpio_num, &cfg); |
| 120 | } |
| 121 | |
| 122 | void gpio_input(gpio_t gpio_num) |
| 123 | { |
| 124 | struct soc_gpio_map cfg = GPIO_INPUT_NO_PULL; |
| 125 | gpio_config_pad(gpio_num, &cfg); |
| 126 | } |
| 127 | |
| 128 | int gpio_get(gpio_t gpio_num) |
| 129 | { |
| 130 | int comm = 0; |
Lee Leahy | 6598b91 | 2017-03-16 17:30:09 -0700 | [diff] [blame] | 131 | int pad_num = 0; |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 132 | uint32_t *pad_config0_reg; |
| 133 | u32 pad_value; |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 134 | |
Angel Pons | aee7ab2 | 2020-03-19 00:31:58 +0100 | [diff] [blame] | 135 | if (gpio_num > MAX_GPIO_CNT) |
Subrata Banik | ed7275f | 2015-08-22 10:36:41 +0530 | [diff] [blame] | 136 | return -1; |
| 137 | |
| 138 | /* Get GPIO Community based on GPIO_NUMBER */ |
| 139 | comm = gpio_get_community_num(gpio_num, &pad_num); |
| 140 | /* CONF0 */ |
| 141 | pad_config0_reg = gpio_pad_config_reg(comm, pad_num); |
| 142 | |
| 143 | pad_value = read32(pad_config0_reg); |
| 144 | |
| 145 | return pad_value & PAD_RX_BIT; |
| 146 | } |
| 147 | |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 148 | int get_gpio(int community_base, int pad0_offset) |
| 149 | { |
| 150 | return (read32((void *)(community_base + pad0_offset))) & PAD_RX_BIT; |
| 151 | } |