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