3a2e253665bbf67c3ed231387c9d53f766148242
[rpikern] / src / gpio.h
1 #ifndef GPIO_H_
2 #define GPIO_H_
3
4 #include "config.h"
5 #include "asm.h"
6
7 #define GPIO_BASE       (IO_BASE | 0x200000)
8 #define GPIO_REG(x)     (*(volatile uint32_t*)(GPIO_BASE | (x)))
9
10 /* function select */
11 #define REG_GPFSEL(x)   GPIO_REG((x) << 2)
12 #define REG_GPFSEL0             GPIO_REG(0x00)
13 #define REG_GPFSEL1             GPIO_REG(0x04)
14 #define REG_GPFSEL2             GPIO_REG(0x08)
15 #define REG_GPFSEL3             GPIO_REG(0x0c)
16 #define REG_GPFSEL4             GPIO_REG(0x10)
17 #define REG_GPFSEL5             GPIO_REG(0x14)
18 /* pin output set */
19 #define REG_GPSET0              GPIO_REG(0x1c)
20 #define REG_GPSET1              GPIO_REG(0x20)
21 /* pin output clear */
22 #define REG_GPCLR0              GPIO_REG(0x28)
23 #define REG_GPCLR1              GPIO_REG(0x2c)
24 /* pin level */
25 #define REG_GPLEV0              GPIO_REG(0x34)
26 #define REG_GPLEV1              GPIO_REG(0x38)
27 /* pin event detect status */
28 #define REG_GPEDS0              GPIO_REG(0x40)
29 #define REG_GPEDS1              GPIO_REG(0x44)
30 /* pin rising edge detect enable */
31 #define REG_GPREN0              GPIO_REG(0x4c)
32 #define REG_GPREN1              GPIO_REG(0x50)
33 /* pin falling edge detect enable */
34 #define REG_GPFEN0              GPIO_REG(0x58)
35 #define REG_GPFEN1              GPIO_REG(0x5c)
36 /* pin high detect enable */
37 #define REG_GPHEN0              GPIO_REG(0x64)
38 #define REG_GPHEN1              GPIO_REG(0x68)
39 /* pin low detect enable */
40 #define REG_GPLEN0              GPIO_REG(0x70)
41 #define REG_GPLEN1              GPIO_REG(0x74)
42 /* pin async rising edge detect */
43 #define REG_GPAREN0             GPIO_REG(0x7c)
44 #define REG_GPAREN1             GPIO_REG(0x80)
45 /* pin async falling edge detect */
46 #define REG_GPAFEN0             GPIO_REG(0x88)
47 #define REG_GPAFEN1             GPIO_REG(0x8c)
48 /* pin pull-up/down enable */
49 #define REG_GPPUD               GPIO_REG(0x94)
50 /* pin pull-up/down enable clock */
51 #define REG_GPPUDCLK0   GPIO_REG(0x98)
52 #define REG_GPPUDCLK1   GPIO_REG(0x9c)
53
54 /* function select bits */
55 #define FSEL_IN         0
56 #define FSEL_OUT        1
57 #define FSEL_ALT0       4
58 #define FSEL_ALT1       5
59 #define FSEL_ALT2       6
60 #define FSEL_ALT3       7
61 #define FSEL_ALT4       3
62 #define FSEL_ALT5       2
63
64 /* pull-up/down bits */
65 #define PUD_DISABLE     0
66 #define PUD_DOWN        1
67 #define PUD_UP          2
68
69 static inline void gpio_fsel(int x, int f)
70 {
71         static const unsigned int fseltab[54][2] = {
72                 {0, 0}, {0, 3}, {0, 6}, {0, 9}, {0, 12}, {0, 15}, {0, 18}, {0, 21}, {0, 24}, {0, 27},
73                 {4, 0}, {4, 3}, {4, 6}, {4, 9}, {4, 12}, {4, 15}, {4, 18}, {4, 21}, {4, 24}, {4, 27},
74                 {8, 0}, {8, 3}, {8, 6}, {8, 9}, {8, 12}, {8, 15}, {8, 18}, {8, 21}, {8, 24}, {8, 27},
75                 {12, 0}, {12, 3}, {12, 6}, {12, 9}, {12, 12}, {12, 15}, {12, 18}, {12, 21}, {12, 24}, {12, 27},
76                 {16, 0}, {16, 3}, {16, 6}, {16, 9}, {16, 12}, {16, 15}, {16, 18}, {16, 21}, {16, 24}, {16, 27},
77                 {20, 0}, {20, 3}, {20, 6}, {20, 9}
78         };
79         uint32_t val;
80
81         if(x <= 53) {
82                 val = f << fseltab[x][1];
83                 *(volatile uint32_t*)(GPIO_BASE | fseltab[x][0]) = val;
84         }
85 }
86
87 void delay(uint32_t x); /* in startup.s */
88
89 static inline void gpio_pullups(uint32_t mask0, uint32_t mask1, int state)
90 {
91         REG_GPPUD = state;
92         delay(150);
93
94         if(mask0) REG_GPPUDCLK0 = mask0;
95         if(mask1) REG_GPPUDCLK1 = mask1;
96         delay(150);
97
98         if(mask0) REG_GPPUDCLK0 = 0;
99         if(mask1) REG_GPPUDCLK1 = 0;
100 }
101
102 #endif  /* GPIO_H_ */