coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
gpio.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <device/mmio.h>
4 #include <gpio.h>
5 
6 enum {
9 };
10 
11 enum {
12  GPIO_MODE = 0,
13 };
14 
15 static void pos_bit_calc(gpio_t gpio, u32 *pos, u32 *bit)
16 {
17  *pos = gpio.id / MAX_GPIO_REG_BITS;
18  *bit = gpio.id % MAX_GPIO_REG_BITS;
19 }
20 
21 static void pos_bit_calc_for_mode(gpio_t gpio, u32 *pos, u32 *bit)
22 {
23  *pos = gpio.id / MAX_GPIO_MODE_PER_REG;
25 }
26 
28 {
29  u32 pos;
30  u32 bit;
31  u32 *reg;
32 
33  pos_bit_calc(gpio, &pos, &bit);
34 
35  if (dir == GPIO_DIRECTION_IN)
36  reg = &mtk_gpio->dir[pos].rst;
37  else
38  reg = &mtk_gpio->dir[pos].set;
39 
40  write32(reg, 1L << bit);
41 
42  return 0;
43 }
44 
45 void gpio_set_mode(gpio_t gpio, int mode)
46 {
47  u32 pos;
48  u32 bit;
49  u32 mask = (1L << GPIO_MODE_BITS) - 1;
50 
51  pos_bit_calc_for_mode(gpio, &pos, &bit);
52 
53  clrsetbits32(&mtk_gpio->mode[pos].val, mask << bit, mode << bit);
54 }
55 
57 {
58  u32 pos;
59  u32 bit;
60  u32 *reg;
61  u32 data;
62 
63  pos_bit_calc(gpio, &pos, &bit);
64 
65  reg = &mtk_gpio->din[pos].val;
66  data = read32(reg);
67 
68  return (data & (1L << bit)) ? 1 : 0;
69 }
70 
71 void gpio_set(gpio_t gpio, int output)
72 {
73  u32 pos;
74  u32 bit;
75  u32 *reg;
76 
77  pos_bit_calc(gpio, &pos, &bit);
78 
79  if (output == 0)
80  reg = &mtk_gpio->dout[pos].rst;
81  else
82  reg = &mtk_gpio->dout[pos].set;
83 
84  write32(reg, 1L << bit);
85 }
86 
88 {
92 }
93 
95 {
99 }
100 
102 {
106 }
107 
109 {
111  gpio_set(gpio, value);
114 }
115 
116 enum {
118 };
119 
120 static void pos_bit_calc_for_eint(gpio_t gpio, u32 *pos, u32 *bit)
121 {
122  *pos = gpio.id / MAX_EINT_REG_BITS;
123  *bit = gpio.id % MAX_EINT_REG_BITS;
124 }
125 
127 {
128  u32 pos;
129  u32 bit;
130  u32 status;
131 
132  pos_bit_calc_for_eint(gpio, &pos, &bit);
133 
134  status = (read32(&mtk_eint->sta.regs[pos]) >> bit) & 0x1;
135 
136  if (status)
137  write32(&mtk_eint->ack.regs[pos], 1 << bit);
138 
139  return status;
140 }
141 
143 {
144  u32 pos;
145  u32 bit, mask;
146 
147  pos_bit_calc_for_eint(gpio, &pos, &bit);
148  mask = 1 << bit;
149 
150  /* Make it an input first. */
152 
153  write32(&mtk_eint->d0en[pos], mask);
154 
155  switch (type) {
157  write32(&mtk_eint->sens_clr.regs[pos], mask);
158  write32(&mtk_eint->pol_clr.regs[pos], mask);
159  break;
161  write32(&mtk_eint->sens_clr.regs[pos], mask);
162  write32(&mtk_eint->pol_set.regs[pos], mask);
163  break;
164  case IRQ_TYPE_LEVEL_LOW:
165  write32(&mtk_eint->sens_set.regs[pos], mask);
166  write32(&mtk_eint->pol_clr.regs[pos], mask);
167  break;
168  case IRQ_TYPE_LEVEL_HIGH:
169  write32(&mtk_eint->sens_set.regs[pos], mask);
170  write32(&mtk_eint->pol_set.regs[pos], mask);
171  break;
172  }
173 
174  write32(&mtk_eint->mask_clr.regs[pos], mask);
175 }
pte_t value
Definition: mmu.c:91
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
#define clrsetbits32(addr, clear, set)
Definition: mmio.h:16
unsigned int type
Definition: edid.c:57
gpio_irq_type
Definition: gpio_common.h:54
@ IRQ_TYPE_EDGE_RISING
Definition: gpio_common.h:55
@ IRQ_TYPE_EDGE_FALLING
Definition: gpio_common.h:56
@ IRQ_TYPE_LEVEL_HIGH
Definition: gpio_common.h:57
@ IRQ_TYPE_LEVEL_LOW
Definition: gpio_common.h:58
@ GPIO_PULL_ENABLE
Definition: gpio_common.h:13
@ GPIO_PULL_DISABLE
Definition: gpio_common.h:12
static struct eint_regs *const mtk_eint
Definition: gpio_common.h:90
void gpio_set(gpio_t gpio_num, int value)
Definition: gpio.c:174
void gpio_output(gpio_t gpio_num, int value)
Definition: gpio.c:194
int gpio_get(gpio_t gpio_num)
Definition: gpio.c:166
void gpio_input_pullup(gpio_t gpio_num)
Definition: gpio.c:184
void gpio_input_pulldown(gpio_t gpio_num)
Definition: gpio.c:179
void gpio_input(gpio_t gpio_num)
Definition: gpio.c:189
static const int mask[4]
Definition: gpio.c:308
static s32 gpio_set_dir(gpio_t gpio, u32 dir)
Definition: gpio.c:27
static void pos_bit_calc_for_eint(gpio_t gpio, u32 *pos, u32 *bit)
Definition: gpio.c:120
int gpio_eint_poll(gpio_t gpio)
Definition: gpio.c:126
void gpio_set_mode(gpio_t gpio, int mode)
Definition: gpio.c:45
@ MAX_EINT_REG_BITS
Definition: gpio.c:117
void gpio_eint_configure(gpio_t gpio, enum gpio_irq_type type)
Definition: gpio.c:142
static void pos_bit_calc(gpio_t gpio, u32 *pos, u32 *bit)
Definition: gpio.c:15
@ GPIO_DIRECTION_OUT
Definition: gpio.c:8
@ GPIO_DIRECTION_IN
Definition: gpio.c:7
@ GPIO_MODE
Definition: gpio.c:12
static void pos_bit_calc_for_mode(gpio_t gpio, u32 *pos, u32 *bit)
Definition: gpio.c:21
void gpio_set_pull(gpio_t gpio, enum pull_enable enable, enum pull_select select)
Definition: gpio.c:17
@ MAX_GPIO_MODE_PER_REG
Definition: gpio.h:11
@ GPIO_MODE_BITS
Definition: gpio.h:12
@ MAX_GPIO_REG_BITS
Definition: gpio.h:10
static struct gpio_regs *const mtk_gpio
Definition: gpio.h:347
#define GPIO_PULL_UP
Definition: gpio.h:24
#define GPIO_PULL_DOWN
Definition: gpio.h:23
uint32_t u32
Definition: stdint.h:51
int32_t s32
Definition: stdint.h:50
struct eint_section sta
Definition: gpio_common.h:67
struct eint_section ack
Definition: gpio_common.h:68
struct eint_section sens_set
Definition: gpio_common.h:73
struct eint_section pol_set
Definition: gpio_common.h:80
struct eint_section sens_clr
Definition: gpio_common.h:74
uint32_t d0en[7]
Definition: gpio_common.h:83
struct eint_section mask_clr
Definition: gpio_common.h:71
struct eint_section pol_clr
Definition: gpio_common.h:81
uint32_t regs[7]
Definition: gpio_common.h:62
struct val_regs din[9]
Definition: gpio.h:322
struct val_regs mode[27]
Definition: gpio.h:324
struct val_regs dout[9]
Definition: gpio.h:320
struct val_regs dir[9]
Definition: gpio.h:313
Definition: pinmux.c:36
uint32_t set
Definition: gpio.h:307
uint32_t val
Definition: gpio.h:306
uint32_t rst
Definition: gpio.h:308