-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprog-flash.sh
executable file
·172 lines (134 loc) · 4.31 KB
/
prog-flash.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# To program the FLASH:
#
# sudo ./prog-flash.sh myfile.bin
# The 2057-ICE40HX4K-TQ144-breakout Rev 3.0 is connected to the PI like this:
#
# Signal PI function
# CRESET* GPIO24
# CDONE GPIO23
# FPGA_SS GPIO12 (special case for booting)
# FPGA_SDI SPI_MOSI
# FPGA_SDO SPI_MISO
# FPGA_SCK SPI_SCK
# FPGA_CE0 SPI_CE0 (special case not used for booting)
# FRESET GPIO16 (used to reset the flash)
CRESET=24
CDONE=23
SSEL=12
FRESET=16
SPI_DEV=/dev/spidev0.0
######################################
echo ""
if [ $# -ne 1 ]; then
echo "Usage: $0 FPGA-bin-file "
exit 1
fi
if [ $EUID -ne 0 ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
######################################
if [ ! -d /sys/class/gpio/gpio${SSEL} ]; then
echo "GPIO ${SSEL} not exported, trying to export..."
echo ${SSEL} > /sys/class/gpio/export
if [ ! -d /sys/class/gpio/gpio${SSEL} ]; then
echo "ERROR: directory /sys/class/gpio/gpio${SSEL} does not exist"
exit 1
fi
else
echo "OK: GPIO ${SSEL} exported"
fi
######################################
if [ ! -d /sys/class/gpio/gpio${CRESET} ]; then
echo "GPIO ${CRESET} not exported, trying to export..."
echo ${CRESET} > /sys/class/gpio/export
if [ ! -d /sys/class/gpio/gpio${CRESET} ]; then
echo "ERROR: directory /sys/class/gpio/gpio${CRESET} does not exist"
exit 1
fi
else
echo "OK: GPIO ${CRESET} exported"
fi
######################################
if [ ! -d /sys/class/gpio/gpio${FRESET} ]; then
echo "GPIO ${FRESET} not exported, trying to export..."
echo ${FRESET} > /sys/class/gpio/export
if [ ! -d /sys/class/gpio/gpio${FRESET} ]; then
echo "ERROR: directory /sys/class/gpio/gpio${FRESET} does not exist"
exit 1
fi
else
echo "OK: GPIO ${FRESET} exported"
fi
######################################
echo ""
if [ -e ${SPI_DEV} ]; then
echo "OK: SPI driver loaded"
else
echo "spidev does not exist"
#lsmod | grep spi_bcm2708 >& /dev/null
lsmod | grep spi_bcm2835 >& /dev/null
if [ $? -ne 0 ]; then
echo "SPI driver not loaded, try to load it..."
modprobe spi_bcm2835
if [ $? -eq 0 ]; then
echo "OK: SPI driver loaded"
else
echo "Could not load SPI driver"
exit 1
fi
fi
fi
######################################
#
# Float the SSEL pin at this point so that it won't mess
# with the FPGA's ability to boot from flash!
echo "changing SSEL to an input so is not driven by the PI"
echo in > /sys/class/gpio/gpio${SSEL}/direction
cat /sys/class/gpio/gpio${SSEL}/direction
######################################
# set the CRESET to low
echo ""
echo "Changing direction to out"
echo out > /sys/class/gpio/gpio${CRESET}/direction
cat /sys/class/gpio/gpio${CRESET}/direction
echo "Resetting the FPGA (should be 0)"
echo 0 > /sys/class/gpio/gpio${CRESET}/value
cat /sys/class/gpio/gpio${CRESET}/value
sleep 1
# Note that we KEEP the reset asserted here so the
# FPGA does not try to mess with the SPI bus while
# we are talking to the FLASH.
######################################
# Cycle FRESET low and back hi to reset the FLASH
echo ""
echo "Changing FRESET direction to out"
echo out > /sys/class/gpio/gpio${FRESET}/direction
cat /sys/class/gpio/gpio${FRESET}/direction
echo "Resetting the FLASH (should be 0)"
echo 0 > /sys/class/gpio/gpio${FRESET}/value
cat /sys/class/gpio/gpio${FRESET}/value
sleep 1
echo "Floating the FRESET so it is not driven by the PI"
echo in > /sys/class/gpio/gpio${FRESET}/direction
cat /sys/class/gpio/gpio${FRESET}/direction
######################################
# program the FLASH
#
# AT45DB081 1081344 (264-byte page mode)
# AT45DB081 1048576 (256-byte page mode)
TMPBIN=$$.bin
#dd if=/dev/zero bs=1081344 count=1 of=${TMPBIN}
dd if=/dev/zero bs=1048576 count=1 of=${TMPBIN} # AT45DB081E Digikey: 1265-1092-1-ND
#dd if=/dev/zero bs=2162688 count=1 of=${TMPBIN} # AT45DB161E-SHD-T Digikey: 1265-1035-1-ND
dd if=$1 of=${TMPBIN} conv=notrunc
flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=8000 --write ${TMPBIN}
rm -f ${TMPBIN}
######################################
echo "Releasing CRESET... (should be 1)"
echo 1 > /sys/class/gpio/gpio${CRESET}/value
cat /sys/class/gpio/gpio${CRESET}/value
echo "Floating the CRESET so it is not driven by the PI"
echo in > /sys/class/gpio/gpio${CRESET}/direction
cat /sys/class/gpio/gpio${CRESET}/direction