blob: 231e8b21adca4e74a33d443e92b70873b544ccbf [file] [log] [blame]
Gabe Black08d5a892013-10-03 04:35:01 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2013 Google Inc.
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.
Gabe Black08d5a892013-10-03 04:35:01 -070014 */
15
16#include <arch/io.h>
17#include <console/console.h>
Julius Wernereaa9c452014-09-24 15:40:49 -070018#include <gpio.h>
Gabe Black08d5a892013-10-03 04:35:01 -070019#include <soc/addressmap.h>
20#include <stddef.h>
21#include <stdint.h>
Tom Warren64982c502014-01-23 13:37:50 -070022#include <delay.h>
Gabe Black08d5a892013-10-03 04:35:01 -070023
Gabe Black08d5a892013-10-03 04:35:01 -070024#include "pinmux.h"
25
Julius Wernereaa9c452014-09-24 15:40:49 -070026static void __gpio_input(gpio_t gpio, u32 pull)
Gabe Black08d5a892013-10-03 04:35:01 -070027{
Ken Chang41359bd2014-04-21 17:54:28 +080028 u32 pinmux_config = PINMUX_INPUT_ENABLE | pull;
Gabe Blackd40be112013-10-09 23:45:07 -070029
30 gpio_set_int_enable(gpio, 0);
31 gpio_set_out_enable(gpio, 0);
32 gpio_set_mode(gpio, GPIO_MODE_GPIO);
33 pinmux_set_config(gpio >> GPIO_PINMUX_SHIFT, pinmux_config);
Gabe Black08d5a892013-10-03 04:35:01 -070034}
35
Julius Wernereaa9c452014-09-24 15:40:49 -070036static void __gpio_output(gpio_t gpio, int value, u32 od)
Gabe Black08d5a892013-10-03 04:35:01 -070037{
Gabe Blackd40be112013-10-09 23:45:07 -070038 gpio_set_int_enable(gpio, 0);
Julius Wernereaa9c452014-09-24 15:40:49 -070039 gpio_set(gpio, value);
Gabe Blackd40be112013-10-09 23:45:07 -070040 gpio_set_out_enable(gpio, 1);
41 gpio_set_mode(gpio, GPIO_MODE_GPIO);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080042 pinmux_set_config(gpio >> GPIO_PINMUX_SHIFT, PINMUX_PULL_NONE | od);
Gabe Black08d5a892013-10-03 04:35:01 -070043}
44
Gabe Black08d5a892013-10-03 04:35:01 -070045static const struct gpio_bank *gpio_banks = (void *)TEGRA_GPIO_BASE;
46
Gabe Blackd40be112013-10-09 23:45:07 -070047static u32 gpio_read_port(int index, size_t offset)
Gabe Black08d5a892013-10-03 04:35:01 -070048{
49 int bank = index / GPIO_GPIOS_PER_BANK;
50 int port = (index - bank * GPIO_GPIOS_PER_BANK) / GPIO_GPIOS_PER_PORT;
51
Gabe Blackd40be112013-10-09 23:45:07 -070052 return read32((u8 *)&gpio_banks[bank] + offset +
53 port * sizeof(u32));
Gabe Black08d5a892013-10-03 04:35:01 -070054}
55
Gabe Blackd40be112013-10-09 23:45:07 -070056static void gpio_write_port(int index, size_t offset, u32 mask, u32 value)
Gabe Black08d5a892013-10-03 04:35:01 -070057{
58 int bank = index / GPIO_GPIOS_PER_BANK;
59 int port = (index - bank * GPIO_GPIOS_PER_BANK) / GPIO_GPIOS_PER_PORT;
60
Gabe Blackd40be112013-10-09 23:45:07 -070061 u32 reg = read32((u8 *)&gpio_banks[bank] + offset +
62 port * sizeof(u32));
63 u32 new_reg = (reg & ~mask) | (value & mask);
Gabe Black08d5a892013-10-03 04:35:01 -070064
65 if (new_reg != reg) {
Julius Werner2f37bd62015-02-19 14:51:15 -080066 write32((u8 *)&gpio_banks[bank] + offset + port * sizeof(u32),
67 new_reg);
Gabe Black08d5a892013-10-03 04:35:01 -070068 }
69}
70
Gabe Blackd40be112013-10-09 23:45:07 -070071void gpio_set_mode(gpio_t gpio, enum gpio_mode mode)
Gabe Black08d5a892013-10-03 04:35:01 -070072{
Gabe Blackd40be112013-10-09 23:45:07 -070073 int bit = gpio % GPIO_GPIOS_PER_PORT;
74 gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
75 offsetof(struct gpio_bank, config),
Gabe Black08d5a892013-10-03 04:35:01 -070076 1 << bit, mode ? (1 << bit) : 0);
77}
78
Gabe Blackd40be112013-10-09 23:45:07 -070079int gpio_get_mode(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -070080{
Gabe Blackd40be112013-10-09 23:45:07 -070081 int bit = gpio % GPIO_GPIOS_PER_PORT;
82 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
83 offsetof(struct gpio_bank, config));
Gabe Black08d5a892013-10-03 04:35:01 -070084 return (port & (1 << bit)) != 0;
85}
86
Gabe Blackd40be112013-10-09 23:45:07 -070087void gpio_set_lock(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -070088{
Gabe Blackd40be112013-10-09 23:45:07 -070089 int bit = gpio % GPIO_GPIOS_PER_PORT + GPIO_GPIOS_PER_PORT;
90 gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
91 offsetof(struct gpio_bank, config),
Gabe Black08d5a892013-10-03 04:35:01 -070092 1 << bit, 1 << bit);
93}
94
Gabe Blackd40be112013-10-09 23:45:07 -070095int gpio_get_lock(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -070096{
Gabe Blackd40be112013-10-09 23:45:07 -070097 int bit = gpio % GPIO_GPIOS_PER_PORT + GPIO_GPIOS_PER_PORT;
98 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
99 offsetof(struct gpio_bank, config));
Gabe Black08d5a892013-10-03 04:35:01 -0700100 return (port & (1 << bit)) != 0;
101}
102
Gabe Blackd40be112013-10-09 23:45:07 -0700103void gpio_set_out_enable(gpio_t gpio, int enable)
Gabe Black08d5a892013-10-03 04:35:01 -0700104{
Gabe Blackd40be112013-10-09 23:45:07 -0700105 int bit = gpio % GPIO_GPIOS_PER_PORT;
106 gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
107 offsetof(struct gpio_bank, out_enable),
Gabe Black08d5a892013-10-03 04:35:01 -0700108 1 << bit, enable ? (1 << bit) : 0);
109}
110
Gabe Blackd40be112013-10-09 23:45:07 -0700111int gpio_get_out_enable(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -0700112{
Gabe Blackd40be112013-10-09 23:45:07 -0700113 int bit = gpio % GPIO_GPIOS_PER_PORT;
114 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
115 offsetof(struct gpio_bank, out_enable));
Gabe Black08d5a892013-10-03 04:35:01 -0700116 return (port & (1 << bit)) != 0;
117}
118
Julius Wernereaa9c452014-09-24 15:40:49 -0700119void gpio_set(gpio_t gpio, int value)
Gabe Black08d5a892013-10-03 04:35:01 -0700120{
Gabe Blackd40be112013-10-09 23:45:07 -0700121 int bit = gpio % GPIO_GPIOS_PER_PORT;
122 gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
123 offsetof(struct gpio_bank, out_value),
Gabe Black08d5a892013-10-03 04:35:01 -0700124 1 << bit, value ? (1 << bit) : 0);
125}
126
Gabe Blackd40be112013-10-09 23:45:07 -0700127int gpio_get_out_value(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -0700128{
Gabe Blackd40be112013-10-09 23:45:07 -0700129 int bit = gpio % GPIO_GPIOS_PER_PORT;
130 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
131 offsetof(struct gpio_bank, out_value));
Gabe Black08d5a892013-10-03 04:35:01 -0700132 return (port & (1 << bit)) != 0;
133}
134
Julius Wernereaa9c452014-09-24 15:40:49 -0700135int gpio_get(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -0700136{
Gabe Blackd40be112013-10-09 23:45:07 -0700137 int bit = gpio % GPIO_GPIOS_PER_PORT;
138 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
139 offsetof(struct gpio_bank, in_value));
Gabe Black08d5a892013-10-03 04:35:01 -0700140 return (port & (1 << bit)) != 0;
141}
142
Gabe Blackd40be112013-10-09 23:45:07 -0700143int gpio_get_int_status(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -0700144{
Gabe Blackd40be112013-10-09 23:45:07 -0700145 int bit = gpio % GPIO_GPIOS_PER_PORT;
146 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
147 offsetof(struct gpio_bank, int_status));
Gabe Black08d5a892013-10-03 04:35:01 -0700148 return (port & (1 << bit)) != 0;
149}
150
Gabe Blackd40be112013-10-09 23:45:07 -0700151void gpio_set_int_enable(gpio_t gpio, int enable)
Gabe Black08d5a892013-10-03 04:35:01 -0700152{
Gabe Blackd40be112013-10-09 23:45:07 -0700153 int bit = gpio % GPIO_GPIOS_PER_PORT;
154 gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
155 offsetof(struct gpio_bank, int_enable),
Gabe Black08d5a892013-10-03 04:35:01 -0700156 1 << bit, enable ? (1 << bit) : 0);
157}
158
Gabe Blackd40be112013-10-09 23:45:07 -0700159int gpio_get_int_enable(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -0700160{
Gabe Blackd40be112013-10-09 23:45:07 -0700161 int bit = gpio % GPIO_GPIOS_PER_PORT;
162 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
163 offsetof(struct gpio_bank, int_enable));
Gabe Black08d5a892013-10-03 04:35:01 -0700164 return (port & (1 << bit)) != 0;
165}
166
Gabe Blackd40be112013-10-09 23:45:07 -0700167void gpio_set_int_level(gpio_t gpio, int high_rise, int edge, int delta)
Gabe Black08d5a892013-10-03 04:35:01 -0700168{
Gabe Blackd40be112013-10-09 23:45:07 -0700169 int bit = gpio % GPIO_GPIOS_PER_PORT;
170 u32 value = (high_rise ? (0x000001 << bit) : 0) |
Gabe Black08d5a892013-10-03 04:35:01 -0700171 (edge ? (0x000100 << bit) : 0) |
Gabe Blackd40be112013-10-09 23:45:07 -0700172 (delta ? (0x010000 << bit) : 0);
173 gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
174 offsetof(struct gpio_bank, config),
Gabe Black08d5a892013-10-03 04:35:01 -0700175 0x010101 << bit, value);
176}
177
Gabe Blackd40be112013-10-09 23:45:07 -0700178void gpio_get_int_level(gpio_t gpio, int *high_rise, int *edge, int *delta)
Gabe Black08d5a892013-10-03 04:35:01 -0700179{
Gabe Blackd40be112013-10-09 23:45:07 -0700180 int bit = gpio % GPIO_GPIOS_PER_PORT;
181 u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
182 offsetof(struct gpio_bank, int_level));
Gabe Black08d5a892013-10-03 04:35:01 -0700183 *high_rise = ((port & (0x000001 << bit)) != 0);
184 *edge = ((port & (0x000100 << bit)) != 0);
185 *delta = ((port & (0x010000 << bit)) != 0);
186}
187
Gabe Blackd40be112013-10-09 23:45:07 -0700188void gpio_set_int_clear(gpio_t gpio)
Gabe Black08d5a892013-10-03 04:35:01 -0700189{
Gabe Blackd40be112013-10-09 23:45:07 -0700190 int bit = gpio % GPIO_GPIOS_PER_PORT;
191 gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1),
192 offsetof(struct gpio_bank, int_clear),
Gabe Black08d5a892013-10-03 04:35:01 -0700193 1 << bit, 1 << bit);
194}
Vadim Bendebury9c9c3362014-07-23 09:40:02 -0700195
196void gpio_input_pulldown(gpio_t gpio)
197{
198 __gpio_input(gpio, PINMUX_PULL_DOWN);
199}
200
201void gpio_input_pullup(gpio_t gpio)
202{
203 __gpio_input(gpio, PINMUX_PULL_UP);
204}
205
206void gpio_input(gpio_t gpio)
207{
208 __gpio_input(gpio, PINMUX_PULL_NONE);
209}
Julius Wernereaa9c452014-09-24 15:40:49 -0700210
211void gpio_output(gpio_t gpio, int value)
212{
213 __gpio_output(gpio, value, 0);
214}
215
216void gpio_output_open_drain(gpio_t gpio, int value)
217{
218 __gpio_output(gpio, value, PINMUX_OPEN_DRAIN);
219}