summaryrefslogtreecommitdiff
path: root/scripts/fixup-dts
blob: e1f7d5773d40e243055b7859ab7632213031396f (plain)
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
#!/bin/bash

set -e
set -o pipefail

unset dts
unset SED

while [ "$1" != "" ]
do
    case "$1"
    in
    --dts)      dts="$2";       shift 2;;
    *) echo "$0: Unknown argument $1"; exit 1;;
    esac
done

if [ "${dts}" == "" ]
then
    echo "$0: please provide '--dts' option" >&2
    exit 1
fi

# When installed on MacOS with Homebrew, GNU sed is named gsed.
# If gsed exists, use it.
if [ "`which gsed`" == "" ]; then
    SED=sed
else
    SED=gsed
fi

# Add numintbits for the clic node if it doesn't exist

if [ `grep -c 'sifive,clic0' ${dts}` -ne 0 ]; then
    if [ `grep -c 'sifive,numintbits' ${dts}` -eq 0 ]; then
        echo "$0: Clic missing numintbits in  ${dts}."

        ${SED} -i 's/interrupt-controller@2000000 {/interrupt-controller@2000000 {\n\t\t\tsifive,numintbits = <2>;/g' ${dts}

        echo -e "$0: \tAdded numintbits to clic."
    fi
fi

# Add compat string for the global-interrupt node if it doesn't exist

if [ `grep -c 'global-external-interrupts {' ${dts}` -ne 0 ]; then
    if [ `grep -c 'sifive,global-external-interrupts0' ${dts}` -eq 0 ]; then
        echo "$0: Global external interrupts missing compat string in ${dts}."

        ${SED} -i 's/global-external-interrupts {/global-external-interrupts {\n\t\t\tcompatible = "sifive,global-external-interrupts0";/g' ${dts}

        echo -e "$0: \tAdded compat string to global-external-interrupts."
    fi
fi

# Add compat string for the local-interrupt node if it doesn't exist

if [ `grep -c 'local-external-interrupts-0 {' ${dts}` -ne 0 ]; then
    if [ `grep -c 'sifive,local-external-interrupts0' ${dts}` -eq 0 ]; then
        echo "$0: Local external interrupts missing compat string in ${dts}."

        ${SED} -i 's/local-external-interrupts-0 {/local-external-interrupts {\n\t\t\tcompatible = "sifive,local-external-interrupts0";/g' ${dts}

        echo -e "$0: \tAdded compat string to local-external-interrupts-0."
    fi
fi

# Add a stdout-path to the chosen node if one doesn't exist and a serial port does

if [ `grep -c 'stdout-path' ${dts}` -eq 0 ]; then
    if [ `grep -c 'sifive,uart0' ${dts}` -ne 0 ]; then
        echo "$0: stdout-path property not given, but a UART device exists."

        serial_node=`grep -oP "serial@\d+" ${dts} | sort | uniq | head -n 1`
        serial_path="/soc/${serial_node}:115200"

        if [ `grep -c 'chosen' ${dts}` -eq 0 ]; then
            ${SED} -i "/cpus/i chosen {\n};" ${dts}
        fi

        ${SED} -i "/chosen/a stdout-path=\"${serial_path}\";" ${dts}

        echo -e "$0: \tAdded stdout-path ${serial_path}"
    fi
fi


# FPGA targets need a 0x400000 byte offset into their SPI flash

# Is the target an FPGA?
if [ `echo ${dts} | grep -c 'arty'` -ne 0 -o `echo ${dts} | grep -c 'fpga'` -ne 0 ] ; then
    # Is there a metal,entry property in the chosen node?
    if [ `grep -c 'metal,entry' ${dts}` -eq 0 ] ; then
        echo "$0: FPGA target needs metal,entry property to set boot offset."

        # Find the SPI device with the lowest base address
        first_spi_node=`grep -oE "spi@[[:xdigit:]]+?" ${dts} | sort | head -n 1`

        # Get the DTS node label of that SPI device
        spi_entry_label=`grep -oE "[^[:space:]]+?:\s+?${first_spi_node}" ${dts} | cut -d ':' -f 1`

        if [ `grep -c 'chosen' ${dts}` -eq 0 ]; then
            ${SED} -i "/cpus/i chosen {\n};" ${dts}
        fi

        ${SED} -i "/chosen/a metal,entry = <&${spi_entry_label} 0x400000>;" ${dts}

        echo -e "$0: \tAdded metal,entry for SPI node ${first_spi_node} with label ${spi_entry_label}"
    fi
fi