blob: ad3df4e4447d3bb7ccbca11ea4159f6690a1a6c9 [file] [log] [blame]
Thang Q. Nguyenfe4a6072023-06-09 08:55:58 +07001#!/bin/bash
2
3# shellcheck disable=SC2046
4# shellcheck source=meta-ampere/meta-mitchell/recipes-ampere/platform/ampere-platform-init/gpio-lib.sh
5source /usr/sbin/gpio-lib.sh
6
7spi_address="1e630000.spi"
8spi_bind="/sys/bus/platform/drivers/spi-aspeed-smc/bind"
9spi_unbind="/sys/bus/platform/drivers/spi-aspeed-smc/unbind"
10spi_lock="/run/platform/spi.lock"
11spi_lock_dir="/run/platform"
12
13bind_aspeed_smc_driver() {
14 if [ -f "${spi_lock}" ]; then
15 pid=$1
16 pid_lock=$(cat "${spi_lock}")
17 if [[ "${pid}" != "${pid_lock}" ]]; then
18 echo "SPI-NOR resoure is lock by process $pid_lock"
19 return 1
20 fi
21 fi
22
23 # BMC access SPI-NOR resource
24 gpio_name_set spi0-program-sel 1
25 sleep 0.1
26 echo "Bind the ASpeed SMC driver"
27 echo "${spi_address}" > "${spi_bind}" 2>/dev/null
28 # Check the HNOR partition available
29 HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
30 if [ -z "$HOST_MTD" ]; then
31 echo "${spi_address}" > "${spi_unbind}"
32 sleep 0.1
33 echo "${spi_address}" > "${spi_bind}"
34 fi
35 # BMC release SPI-NOR resource
36 gpio_name_set spi0-program-sel 0
37 return 0
38}
39
40unbind_aspeed_smc_driver() {
41 if [ -f "${spi_lock}" ]; then
42 pid=$1
43 pid_lock=$(cat "${spi_lock}")
44 if [[ "${pid}" != "${pid_lock}" ]]; then
45 echo "SPI-NOR resoure is lock by process $pid_lock . Wait 10s"
46 # Wait maximum 10 seconds for unlock SPI-NOR
47 cnt=10
48 while [ $cnt -gt 0 ]
49 do
50 if [ -f "${spi_lock}" ]; then
51 sleep 1
52 cnt=$((cnt - 1))
53 else
54 break
55 fi
56 done
57 if [ "$cnt" -eq "0" ]; then
58 echo "Timeout 10 seconds, SPI-NOR still busy. Force unlock to access SPI"
59 rm -f "${spi_lock}"
60 fi
61 fi
62 fi
63
64 HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
65 if [ -n "$HOST_MTD" ]; then
66 # If the HNOR partition is available, then unbind driver
67 # BMC access SPI-NOR resource
68 gpio_name_set spi0-program-sel 1
69 sleep 0.1
70 echo "Unbind the ASpeed SMC driver"
71 echo "${spi_address}" > "${spi_unbind}"
72 fi
73 # BMC release SPI-NOR resource
74 gpio_name_set spi0-program-sel 0
75 # Deassert BMC access SPI-NOR pin
76 gpio_name_set spi-nor-access 0
77 sleep 0.5
78 return 0
79}
80
81lock_spi_resource() {
82 # Wait maximum 10 seconds to lock SPI-NOR
83 cnt=10
84 while [ $cnt -gt 0 ]
85 do
86 if [ -f "${spi_lock}" ]; then
87 sleep 1
88 cnt=$((cnt - 1))
89 else
90 echo "$1" > "${spi_lock}"
91 break
92 fi
93 done
94
95 if [ "$cnt" -eq "0" ]; then
96 echo "Timeout 10 seconds, SPI-NOR is still locked by another process"
97 return 1
98 fi
99 return 0
100}
101
102unlock_spi_resource() {
103 if [ ! -f "${spi_lock}" ]; then
104 echo "SPI-NOR is already unlocked"
105 return 0
106 fi
107
108 pid=$1
109 pid_lock=$(cat "${spi_lock}")
110 if [[ "${pid}" == "${pid_lock}" ]]; then
111 rm -f "${spi_lock}"
112 else
113 echo "Cannot unlock, SPI-NOR is locked by another process"
114 return 1
115 fi
116 return 0
117}
118
119start_handshake_spi() {
120 if [ -f "${spi_lock}" ]; then
121 pid=$1
122 pid_lock=$(cat "${spi_lock}")
123 if [[ "${pid}" != "${pid_lock}" ]]; then
124 echo "SPI-NOR resoure is lock by process $pid_lock"
125 return 1
126 fi
127 fi
128
129 # Wait maximum 10 seconds to grant access SPI
130 cnt=10
131 while [ $cnt -gt 0 ]
132 do
133 spinor_access=$(gpio_name_get soc-spi-nor-access)
134 if [ "$spinor_access" == "1" ]; then
135 sleep 1
136 cnt=$((cnt - 1))
137 else
138 break
139 fi
140 done
141
142 if [ "$cnt" -eq "0" ]; then
143 echo "Timeout 10 seconds, host is still hold SPI-NOR."
144 return 1
145 fi
146 echo "Start handshake SPI-NOR"
147 # Grant BMC access SPI-NOR. The process call the scripts should only
148 # claim the bus for only maximum period 500ms.
149 gpio_name_set spi-nor-access 1
150 # Switch the Host SPI-NOR to BMC
151 gpio_name_set spi0-program-sel 1
152}
153
154stop_handshake_spi() {
155 if [ -f "${spi_lock}" ]; then
156 pid=$1
157 pid_lock=$(cat "${spi_lock}")
158 if [[ "${pid}" != "${pid_lock}" ]]; then
159 echo "SPI-NOR resoure is lock by process $pid_lock"
160 return 1
161 fi
162 fi
163 echo "Stop handshake SPI-NOR"
164 # Switch the Host SPI-NOR to HOST
165 gpio_name_set spi0-program-sel 0
166 # Deassert BMC access SPI-NOR pin
167 gpio_name_set spi-nor-access 0
168}
169
170
171if [ $# -eq 0 ]; then
172 echo "Usage:"
173 echo " - Handshake access SPI-NOR "
174 echo " $(basename "$0") cmd pid"
175 echo " <cmd>:"
176 echo " lock - lock the SPI-NOR resource"
177 echo " unlock - unlock the SPI-NOR resource"
178 echo " bind - bind the SPI-NOR resource"
179 echo " unbind - unbind the SPI-NOR resource"
180 echo " start_handshake - start handshake between BMC and Host"
181 echo " stop_handshake - release handshake between BMC and Host"
182 echo " <pid>: Optional - PID of the process call script"
183 exit 0
184fi
185
186CMD=$1
187
188if [ ! -d "${spi_lock_dir}" ]; then
189 mkdir -p "${spi_lock_dir}"
190fi
191
192if [ -z "$2" ]; then
193 PID=$$
194else
195 PID=$2
196fi
197
198if [[ "${CMD}" == "lock" ]]; then
199 lock_spi_resource "${PID}"
200 ret=$?
201 if [[ "${ret}" == "1" ]]; then
202 echo "Cannot lock SPI-NOR, the resource is busy"
203 exit 1
204 fi
205elif [[ "${CMD}" == "unlock" ]]; then
206 unlock_spi_resource "${PID}"
207 ret=$?
208 if [[ "${ret}" == "1" ]]; then
209 echo "Cannot unlock SPI-NOR, the resource is busy"
210 exit 1
211 fi
212elif [[ "${CMD}" == "bind" ]]; then
213 bind_aspeed_smc_driver "${PID}"
214 ret=$?
215 if [[ "${ret}" == "1" ]]; then
216 echo "Cannot bind SPI-NOR, the resource is busy"
217 exit 1
218 fi
219elif [[ "${CMD}" == "unbind" ]]; then
220 unbind_aspeed_smc_driver "${PID}"
221 ret=$?
222 if [[ "${ret}" == "1" ]]; then
223 echo "Cannot unbind SPI-NOR, the resource is busy"
224 exit 1
225 fi
226elif [[ "${CMD}" == "start_handshake" ]]; then
227 start_handshake_spi "${PID}"
228 ret=$?
229 if [[ "${ret}" == "1" ]]; then
230 echo "Cannot start handshake SPI-NOR"
231 exit 1
232 fi
233elif [[ "${CMD}" == "stop_handshake" ]]; then
234 stop_handshake_spi "${PID}"
235 ret=$?
236 if [[ "${ret}" == "1" ]]; then
237 echo "Cannot stop handshake SPI-NOR"
238 exit 1
239 fi
240fi
241
242exit 0