blob: 14c4663124d8ea8a10b28623cfcf2304f21a5103 [file] [log] [blame]
Lee Leahy32471722015-04-20 15:20:28 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015 Intel Corp.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Lee Leahy32471722015-04-20 15:20:28 -070014 */
Subrata Baniked7275f2015-08-22 10:36:41 +053015#include <gpio.h>
Lee Leahy32471722015-04-20 15:20:28 -070016#include <console/console.h>
17#include <soc/gpio.h>
18
19/*
20 * Return family number and internal pad number in that community by pad number
21 * and which community it is in.
22 */
23uint16_t gpio_family_number(uint8_t community, uint8_t pad)
24{
25 /*
26 * Refer to BSW BIOS Writers Guide, Table "Family Number".
27 * BSW has 4 GPIO communities. Each community has up to 7 families and
28 * each family contains a range of Pad numbers. The number in the array
29 * is the maximum no. of that range.
30 * For example: East community, family 0, Pad 0~11.
31 */
32 static const uint8_t community_base[GPIO_COMMUNITY_COUNT]
33 [GPIO_FAMILIES_MAX_PER_COMM + 1] = {
34 {0, 8, 16, 24, 32, 40, 48, 56}, /* Southwest */
35 {0, 9, 22, 34, 46, 59, 59, 59}, /* North */
36 {0, 12, 24, 24, 24, 24, 24, 24}, /* East */
37 {0, 8, 20, 26, 34, 44, 55, 55} /* Southeast */
38 };
39 const uint8_t *base;
40 uint8_t i;
41
42 /* Validate the pad number */
43 if (pad > community_base[community][7])
44 die("Pad number is out of range!");
45
46 /* Locate the family number for the pad */
47 base = &community_base[community][0];
48 for (i = 0; i < 7; i++) {
49 if ((pad >= base[0]) && (pad < base[1]))
50 break;
51 base++;
52 }
53
54 /* Family number in high byte and inner pad number in lowest byte */
55 return (i << 8) + pad - *base;
56}
57
58/*
59 * Return pad configuration register offset by pad number and which community
60 * it is in.
61 */
62uint32_t *gpio_pad_config_reg(uint8_t community, uint8_t pad)
63{
64 uint16_t fpad;
65 uint32_t *pad_config_reg;
66
67 /* Get the GPIO family number */
68 fpad = gpio_family_number(community, pad);
69
70 /*
71 * Refer to BSW BIOS Writers Guide, Table "Per Pad Memory Space
72 * Registers Addresses" for the Pad configuration register calculation.
73 */
Subrata Baniked7275f2015-08-22 10:36:41 +053074 pad_config_reg = (uint32_t *)(COMMUNITY_BASE(community)
75 + FAMILY_PAD_REGS_OFF + (FAMILY_PAD_REGS_SIZE * (fpad >> 8))
76 + (GPIO_REGS_SIZE * (fpad & 0xff)));
Lee Leahy32471722015-04-20 15:20:28 -070077
78 return pad_config_reg;
79}
80
Subrata Baniked7275f2015-08-22 10:36:41 +053081static int gpio_get_community_num(gpio_t gpio_num, int *pad)
82{
83 int comm = 0;
84
85 if (gpio_num >= GP_SW_00 && gpio_num <= GP_SW_97) {
86 comm = GP_SOUTHWEST;
87 *pad = gpio_num % GP_SOUTHWEST_COUNT;
88 } else if (gpio_num >= GP_NC_00 && gpio_num <= GP_NC_72) {
89 comm = GP_NORTH;
90 *pad = gpio_num % GP_SOUTHWEST_COUNT;
91 } else if (gpio_num >= GP_E_00 && gpio_num <= GP_E_26) {
92 comm = GP_EAST;
93 *pad = gpio_num %
94 (GP_SOUTHWEST_COUNT + GP_NORTH_COUNT);
95 } else {
96 comm = GP_SOUTHEAST;
97 *pad = gpio_num % (GP_SOUTHWEST_COUNT +
98 GP_NORTH_COUNT + GP_EAST_COUNT);
99 }
100 return comm;
101}
102
103static void gpio_config_pad(gpio_t gpio_num, const struct soc_gpio_map *cfg)
104{
105 int comm = 0;
106 int pad_num =0;
107 uint32_t *pad_config0_reg;
108 uint32_t *pad_config1_reg;
109 int max_gpio_cnt = GP_SOUTHWEST_COUNT + GP_NORTH_COUNT + GP_EAST_COUNT
110 + GP_SOUTHEAST_COUNT;
111
112 if(gpio_num > max_gpio_cnt)
113 return;
114 /* Get GPIO Community based on GPIO_NUMBER */
115 comm = gpio_get_community_num(gpio_num, &pad_num);
116 /* CONF0 */
117 pad_config0_reg = gpio_pad_config_reg(comm, pad_num);
118 /* CONF1 */
119 pad_config1_reg = pad_config0_reg + 1;
120
121 write32(pad_config0_reg, cfg->pad_conf0);
122 write32(pad_config1_reg, cfg->pad_conf1);
123}
124
125void gpio_input_pullup(gpio_t gpio_num)
126{
127 struct soc_gpio_map cfg = GPIO_INPUT_PU_20K;
128 gpio_config_pad(gpio_num, &cfg);
129}
130
131void gpio_input_pulldown(gpio_t gpio_num)
132{
133 struct soc_gpio_map cfg = GPIO_INPUT_PD_20K;
134 gpio_config_pad(gpio_num, &cfg);
135}
136
137void gpio_input(gpio_t gpio_num)
138{
139 struct soc_gpio_map cfg = GPIO_INPUT_NO_PULL;
140 gpio_config_pad(gpio_num, &cfg);
141}
142
143int gpio_get(gpio_t gpio_num)
144{
145 int comm = 0;
146 int pad_num =0;
147 uint32_t *pad_config0_reg;
148 u32 pad_value;
149 int max_gpio_cnt = GP_SOUTHWEST_COUNT + GP_NORTH_COUNT + GP_EAST_COUNT
150 + GP_SOUTHEAST_COUNT;
151
152 if(gpio_num > max_gpio_cnt)
153 return -1;
154
155 /* Get GPIO Community based on GPIO_NUMBER */
156 comm = gpio_get_community_num(gpio_num, &pad_num);
157 /* CONF0 */
158 pad_config0_reg = gpio_pad_config_reg(comm, pad_num);
159
160 pad_value = read32(pad_config0_reg);
161
162 return pad_value & PAD_RX_BIT;
163}
164
Lee Leahy32471722015-04-20 15:20:28 -0700165int get_gpio(int community_base, int pad0_offset)
166{
167 return (read32((void *)(community_base + pad0_offset))) & PAD_RX_BIT;
168}