]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/T-HEAD_CB2201_CDK/csi/csi_driver/csky/hobbit1_2/pinmux.c
Introduce a port for T-HEAD CK802. A simple demo for T-HEAD CB2201 is also included.
[freertos] / FreeRTOS / Demo / T-HEAD_CB2201_CDK / csi / csi_driver / csky / hobbit1_2 / pinmux.c
1 /*
2  * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /******************************************************************************
18  * @file     pinmux.c
19  * @brief    source file for the pinmux
20  * @version  V1.0
21  * @date     02. June 2017
22  ******************************************************************************/
23 #include <stdint.h>
24 #include "pinmux.h"
25 #include "pin_name.h"
26
27 #define readl(addr) \
28     ({ unsigned int __v = (*(volatile unsigned int *) (addr)); __v; })
29
30 #define writel(b,addr) (void)((*(volatile unsigned int *) (addr)) = (b))
31
32 /*******************************************************************************
33  * function: hobbit_ioreuse_inital
34  *
35  * description:
36  *   initial hobbit_pinmux
37  *******************************************************************************/
38
39 void hobbit_ioreuse_initial(void)
40 {
41     unsigned int value;
42
43     value = readl(HOBBIT1_2_GIPO0_PORTCTL_REG);
44     value &= ~(GPIO0_REUSE_DIS);
45     writel(value, HOBBIT1_2_GIPO0_PORTCTL_REG);
46 }
47
48 int32_t pin_mux(pin_name_t pin, uint16_t function)
49 {
50     unsigned int val = 0;
51     unsigned int reg_val = 0;
52
53     uint8_t offset;
54
55     if (function > 3) {
56         if (pin <= PB3_SPI0MISO_PWM5_I2SSD) {
57             if (pin <= PA5_RTS0_PWM1_SPI0SSN_TRIG1) {
58                 offset = pin;
59                 /* gpio data source select */
60                 val = readl(HOBBIT1_2_GIPO0_PORTCTL_REG);
61                 val &= ~(1 << offset);
62                 writel(val, HOBBIT1_2_GIPO0_PORTCTL_REG);
63                 return 0;
64             } else if (pin >= PB0_SCL0_PWM2_I2SMCLK) {
65                 offset = pin - 6;
66                 /* gpio data source select */
67                 val = readl(HOBBIT1_2_GIPO1_PORTCTL_REG);
68                 val &= ~(1 << offset);
69                 writel(val, HOBBIT1_2_GIPO1_PORTCTL_REG);
70                 return 0;
71             }
72         }
73         if ((pin >= PA6_SPI0MOSI_PWM6_SCL0) && (pin <= PA27_RTS2_I2SSD_ADC13)) {
74             offset = pin - 4;
75             /* gpio data source select */
76             val = readl(HOBBIT1_2_GIPO0_PORTCTL_REG);
77             val &= ~(1 << offset);
78             writel(val, HOBBIT1_2_GIPO0_PORTCTL_REG);
79             return 0;
80         }
81         return -1;
82     }
83
84     if ((pin >= PA6_SPI0MOSI_PWM6_SCL0) && (pin <= PA27_RTS2_I2SSD_ADC13)) {
85         offset = pin - 4;
86
87         /* gpio data source select */
88         val = readl(HOBBIT1_2_GIPO0_PORTCTL_REG);
89         val |= (1 << offset);
90         writel(val, HOBBIT1_2_GIPO0_PORTCTL_REG);
91
92         if (pin <= PA11_ACMP0N_ADC3_RXD0) {
93             offset = pin;
94             reg_val = (0x3 << (offset * 2));
95             /* reuse function select */
96             val = readl(HOBBIT1_2_IOMUX0L_REG);
97             val &= ~(reg_val);
98             val |= (function << (2 * offset));
99             writel(val, HOBBIT1_2_IOMUX0L_REG);
100             return 0;
101         } else {
102             offset = pin - 16;
103             reg_val = (0x3 << (offset * 2));
104             /* reuse function select */
105             val = readl(HOBBIT1_2_IOMUX0H_REG);
106             val &= ~(reg_val);
107             val |= (function << (2 * offset));
108             writel(val, HOBBIT1_2_IOMUX0H_REG);
109             return 0;
110         }
111     }
112
113     if ((pin >= PA0_TRIG0_ACMP1P_TCK) && (pin <= PB3_SPI0MISO_PWM5_I2SSD)) {
114         if (pin >= PB0_SCL0_PWM2_I2SMCLK) {
115             offset = pin - 6;
116             val = readl(HOBBIT1_2_GIPO1_PORTCTL_REG);
117             val |= (1 << offset);
118             writel(val, HOBBIT1_2_GIPO1_PORTCTL_REG);
119
120             offset = pin;
121             reg_val = (0x3 << (offset * 2));
122             /* reuse function select */
123             val = readl(HOBBIT1_2_IOMUX0L_REG);
124             val &= ~(reg_val);
125             val |= (function << (2 * offset));
126             writel(val, HOBBIT1_2_IOMUX0L_REG);
127             return 0;
128         }
129
130         if (pin <= PA5_RTS0_PWM1_SPI0SSN_TRIG1) {
131             offset = pin;
132             /* gpio data source select */
133             val = readl(HOBBIT1_2_GIPO0_PORTCTL_REG);
134             val |= (1 << offset);
135             writel(val, HOBBIT1_2_GIPO0_PORTCTL_REG);
136
137             reg_val = (0x3 << (offset * 2));
138             /* reuse function select */
139             val = readl(HOBBIT1_2_IOMUX0L_REG);
140             val &= ~(reg_val);
141             val |= (function << (2 * offset));
142             writel(val, HOBBIT1_2_IOMUX0L_REG);
143             return 0;
144         }
145     }
146
147     if (pin > PA27_RTS2_I2SSD_ADC13) {
148         offset = pin - PC0_SCL1_CTS1_PWM10_ADC14;
149         reg_val = (0x3 << (offset *2));
150         val = readl(HOBBIT1_2_IOMUX1L_REG);
151         val &= ~(reg_val);
152         val |= (function << (2 * offset));
153         writel(val, HOBBIT1_2_IOMUX1L_REG);
154     }
155
156     return -1;
157 }