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 <bl.h>
18 #include <hostIntf.h>
19 #include <hostIntf_priv.h>
20 
21 #include <variant/variant.h>
22 
23 #include <plat/cmsis.h>
24 #include <plat/spi.h>
25 #include <plat/exti.h>
26 #include <plat/syscfg.h>
27 
28 static struct Gpio *mShWakeupGpio;
29 static struct ChainedIsr mShWakeupIsr;
30 
platWakeupIsr(struct ChainedIsr * isr)31 static bool platWakeupIsr(struct ChainedIsr *isr)
32 {
33     if (!extiIsPendingGpio(mShWakeupGpio))
34         return false;
35 
36     extiClearPendingGpio(mShWakeupGpio);
37 
38     hostIntfRxPacket(!gpioGet(mShWakeupGpio));
39 
40     return true;
41 }
42 
platHostIntfInit()43 const struct HostIntfComm *platHostIntfInit()
44 {
45     uint32_t priorityGroup = NVIC_GetPriorityGrouping();
46     uint32_t priority, preemptPriority, subPriority;
47     enum IRQn rx, tx;
48     const struct HostIntfComm *ret = NULL;
49 
50 #if defined(PLATFORM_HOST_INTF_I2C_BUS)
51     ret = hostIntfI2cInit(PLATFORM_HOST_INTF_I2C_BUS);
52 #elif defined(PLATFORM_HOST_INTF_SPI_BUS)
53     ret = hostIntfSpiInit(PLATFORM_HOST_INTF_SPI_BUS);
54 
55     /* get the rx and tx irq numbers used by the host spi bus */
56     ret->request();
57     rx = spiRxIrq(PLATFORM_HOST_INTF_SPI_BUS);
58     tx = spiTxIrq(PLATFORM_HOST_INTF_SPI_BUS);
59     ret->release();
60 
61     /* make the tx and rx dma isrs execute before other isrs when multiple
62        interrupts are pending (if avaliable subpriority bits).
63        We do not change the preempt priority */
64     priority = NVIC_GetPriority(rx);
65     NVIC_DecodePriority (priority, priorityGroup, &preemptPriority, &subPriority);
66     if (&subPriority > 0)
67         subPriority --;
68     NVIC_SetPriority(rx, NVIC_EncodePriority(priorityGroup, preemptPriority, subPriority));
69 
70     priority = NVIC_GetPriority(tx);
71     NVIC_DecodePriority (priority, priorityGroup, &preemptPriority, &subPriority);
72     if (&subPriority > 0)
73         subPriority --;
74     NVIC_SetPriority(tx, NVIC_EncodePriority(priorityGroup, preemptPriority, subPriority));
75 
76     mShWakeupGpio = gpioRequest(SH_INT_WAKEUP);
77     gpioConfigInput(mShWakeupGpio, GPIO_SPEED_LOW, GPIO_PULL_NONE);
78     syscfgSetExtiPort(mShWakeupGpio);
79     extiEnableIntGpio(mShWakeupGpio, EXTI_TRIGGER_BOTH);
80     mShWakeupIsr.func = platWakeupIsr;
81     extiChainIsr(SH_EXTI_WAKEUP_IRQ, &mShWakeupIsr);
82 #else
83 #error "No host interface bus specified"
84 #endif
85     return ret;
86 }
87 
platHwType(void)88 uint16_t platHwType(void)
89 {
90     return PLATFORM_HW_TYPE;
91 }
92 
platHwVer(void)93 uint16_t platHwVer(void)
94 {
95     return PLATFORM_HW_VER;
96 }
97 
platBlVer()98 uint16_t platBlVer()
99 {
100     return BL.blGetVersion();
101 }
102