1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 #include <plat/pwr.h>
18 #include <plat/syscfg.h>
19 
20 #define SYSCFG_REG_SHIFT 2
21 
22 struct StmSyscfg
23 {
24     volatile uint32_t MEMRMP;
25     volatile uint32_t PMC;
26     volatile uint32_t EXTICR[4];
27     volatile uint32_t CMPCR;
28 };
29 
syscfgSetExtiPort(const struct Gpio * __restrict gpioHandle)30 void syscfgSetExtiPort(const struct Gpio *__restrict gpioHandle)
31 {
32     if (gpioHandle) {
33         uint32_t gpioNum = (uint32_t)gpioHandle - GPIO_HANDLE_OFFSET;
34         struct StmSyscfg *block = (struct StmSyscfg *)SYSCFG_BASE;
35         const uint32_t bankNo = gpioNum >> GPIO_PORT_SHIFT;
36         const uint32_t pinNo = gpioNum & GPIO_PIN_MASK;
37         const uint32_t regNo = pinNo >> SYSCFG_REG_SHIFT;
38         const uint32_t nibbleNo = pinNo & ((1UL << SYSCFG_REG_SHIFT) - 1UL);
39         const uint32_t shift_4b = nibbleNo << 2UL;
40         const uint32_t mask_4b = 0x0FUL << shift_4b;
41 
42         pwrUnitClock(PERIPH_BUS_APB2, PERIPH_APB2_SYSCFG, true);
43 
44         block->EXTICR[regNo] = (block->EXTICR[regNo] & ~mask_4b) | (bankNo << shift_4b);
45     }
46 }
47