diff options
Diffstat (limited to 'firmware/target/mips/ingenic_x1000/gpio-x1000.c')
-rw-r--r-- | firmware/target/mips/ingenic_x1000/gpio-x1000.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/firmware/target/mips/ingenic_x1000/gpio-x1000.c b/firmware/target/mips/ingenic_x1000/gpio-x1000.c new file mode 100644 index 0000000000..a47865e397 --- /dev/null +++ b/firmware/target/mips/ingenic_x1000/gpio-x1000.c | |||
@@ -0,0 +1,84 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "gpio-x1000.h" | ||
23 | #include "kernel.h" | ||
24 | |||
25 | #ifndef BOOTLOADER_SPL | ||
26 | struct mutex gpio_z_mutex; | ||
27 | #endif | ||
28 | |||
29 | void gpio_init(void) | ||
30 | { | ||
31 | #ifndef BOOTLOADER_SPL | ||
32 | mutex_init(&gpio_z_mutex); | ||
33 | #endif | ||
34 | |||
35 | /* Set all pins to input state */ | ||
36 | for(int i = 0; i < 4; ++i) { | ||
37 | jz_clr(GPIO_INT(GPIO_Z), 0xffffffff); | ||
38 | jz_set(GPIO_MSK(GPIO_Z), 0xffffffff); | ||
39 | jz_set(GPIO_PAT1(GPIO_Z), 0xffffffff); | ||
40 | jz_clr(GPIO_PAT0(GPIO_Z), 0xffffffff); | ||
41 | REG_GPIO_Z_GID2LD = i; | ||
42 | } | ||
43 | |||
44 | /* Clear flag and disable pull resistor */ | ||
45 | for(int i = 0; i < 4; ++i) { | ||
46 | jz_clr(GPIO_FLAG(i), 0xffffffff); | ||
47 | jz_set(GPIO_PULL(i), 0xffffffff); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | void gpio_lock(void) | ||
52 | { | ||
53 | #ifndef BOOTLOADER_SPL | ||
54 | mutex_lock(&gpio_z_mutex); | ||
55 | #endif | ||
56 | } | ||
57 | |||
58 | void gpio_unlock(void) | ||
59 | { | ||
60 | #ifndef BOOTLOADER_SPL | ||
61 | mutex_unlock(&gpio_z_mutex); | ||
62 | #endif | ||
63 | } | ||
64 | |||
65 | void gpio_config(int port, unsigned pinmask, int func) | ||
66 | { | ||
67 | unsigned intr = REG_GPIO_INT(port); | ||
68 | unsigned mask = REG_GPIO_MSK(port); | ||
69 | unsigned pat1 = REG_GPIO_PAT1(port); | ||
70 | unsigned pat0 = REG_GPIO_PAT0(port); | ||
71 | |||
72 | gpio_lock(); | ||
73 | if(func & 8) jz_set(GPIO_INT(GPIO_Z), (intr & pinmask) ^ pinmask); | ||
74 | else jz_clr(GPIO_INT(GPIO_Z), (~intr & pinmask) ^ pinmask); | ||
75 | if(func & 4) jz_set(GPIO_MSK(GPIO_Z), (mask & pinmask) ^ pinmask); | ||
76 | else jz_clr(GPIO_MSK(GPIO_Z), (~mask & pinmask) ^ pinmask); | ||
77 | if(func & 2) jz_set(GPIO_PAT1(GPIO_Z), (pat1 & pinmask) ^ pinmask); | ||
78 | else jz_clr(GPIO_PAT1(GPIO_Z), (~pat1 & pinmask) ^ pinmask); | ||
79 | if(func & 1) jz_set(GPIO_PAT0(GPIO_Z), (pat0 & pinmask) ^ pinmask); | ||
80 | else jz_clr(GPIO_PAT0(GPIO_Z), (~pat0 & pinmask) ^ pinmask); | ||
81 | REG_GPIO_Z_GID2LD = port; | ||
82 | gpio_unlock(); | ||
83 | gpio_set_pull(port, pinmask, func & 16); | ||
84 | } | ||