#!/usr/bin/env bash
#
#   Script for various external toolchain tasks, refer to
#   the --help output for more information.
#
#   Copyright (C) 2012 Jo-Philipp Wich <jo@mein.io>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

CC=""
CXX=""
CPP=""

CFLAGS=""
TOOLCHAIN="."

LIBC_TYPE=""


# Library specs
LIB_SPECS="
	c:        ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
	rt:       librt-* librt
	pthread:  libpthread-* libpthread
	stdcpp:   libstdc++
	gcc:      libgcc_s
	ssp:      libssp
	gfortran: libgfortran
"

# Binary specs
BIN_SPECS="
	ldd:       ldd
	ldconfig:  ldconfig
	gdb:       gdb
	gdbserver: gdbserver
"


test_c() {
	cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
		#include <stdio.h>

		int main(int argc, char **argv)
		{
			printf("Hello, world!\n");
			return 0;
		}
	EOT
}

test_cxx() {
	cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
		#include <iostream>

		using namespace std;

		int main()
		{
			cout << "Hello, world!" << endl;
			return 0;
		}
	EOT
}

test_softfloat() {
	cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
		int main(int argc, char **argv)
		{
			double a = 0.1;
			double b = 0.2;
			double c = (a + b) / (a * b);
			return 1;
		}
	EOT
}

test_uclibc() {
	local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
	if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
		local lib
		for lib in "${sysroot:-$TOOLCHAIN}"/{lib,usr/lib,usr/local/lib}/ld*-uClibc*.so*; do
			if [ -f "$lib" ] && [ ! -h "$lib" ]; then
				return 0
			fi
		done
	fi
	return 1
}

test_feature() {
	local feature="$1"; shift

	# find compilers, libc type
	probe_cc
	probe_cxx
	probe_libc

	# common toolchain feature tests
	case "$feature" in
		c)     test_c;         return $? ;;
		c++)   test_cxx;       return $? ;;
		soft*) test_softfloat; return $? ;;
	esac

	# assume eglibc/glibc supports all libc features
	if [ "$LIBC_TYPE" != "uclibc" ]; then
		return 0
	fi

	# uclibc feature tests
	local inc
	local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
	for inc in "include" "usr/include" "usr/local/include"; do
		local conf="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
		if [ -f "$conf" ]; then
			case "$feature" in
				lfs)     grep -q '__UCLIBC_HAS_LFS__ 1'     "$conf"; return $?;;
				ipv6)    grep -q '__UCLIBC_HAS_IPV6__ 1'    "$conf"; return $?;;
				rpc)     grep -q '__UCLIBC_HAS_RPC__ 1'     "$conf"; return $?;;
				locale)  grep -q '__UCLIBC_HAS_LOCALE__ 1'  "$conf"; return $?;;
				wchar)   grep -q '__UCLIBC_HAS_WCHAR__ 1'   "$conf"; return $?;;
				threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
			esac
		fi
	done

	return 1
}


find_libs() {
	local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"

	if [ -n "$spec" ] && probe_cpp; then
		local libdir libdirs
		for libdir in $(
			"$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
				sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
		); do
			if [ -d "$libdir" ]; then
				libdirs="$libdirs $(cd "$libdir"; pwd)/"
			fi
		done

		local pattern
		for pattern in $(eval echo $spec); do
			find $libdirs -name "$pattern.so*" | sort -u
		done

		return 0
	fi

	return 1
}

find_bins() {
	local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"

	if [ -n "$spec" ] && probe_cpp; then
		local sysroot="$("$CPP" -print-sysroot)"

		local bindir bindirs
		for bindir in $(
			echo "${sysroot:-$TOOLCHAIN}/bin";
			echo "${sysroot:-$TOOLCHAIN}/usr/bin";
			echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
 			"$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
				sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
		); do
			if [ -d "$bindir" ]; then
				bindirs="$bindirs $(cd "$bindir"; pwd)/"
			fi
		done

		local pattern
		for pattern in $(eval echo $spec); do
			find $bindirs -name "$pattern" | sort -u
		done

		return 0
	fi

	return 1
}


wrap_bin_cc() {
	local out="$1"
	local bin="$2"

	echo    '#!/bin/sh'                                                > "$out"
	echo    'for arg in "$@"; do'                                     >> "$out"
	echo    ' case "$arg" in -l*|-L*|-shared|-static)'                >> "$out"
	echo -n '  exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+'           >> "$out"
	echo -n '-idirafter "$STAGING_DIR/usr/include" '                  >> "$out"
	echo -n '-L "$STAGING_DIR/usr/lib" '                              >> "$out"
	echo    '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;'         >> "$out"
	echo    ' esac'                                                   >> "$out"
	echo    'done'                                                    >> "$out"
	echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+'             >> "$out"
	echo    '-idirafter "$STAGING_DIR/usr/include"} "$@"'             >> "$out"

	chmod +x "$out"
}

wrap_bin_ld() {
	local out="$1"
	local bin="$2"

	echo    '#!/bin/sh'                                                > "$out"
	echo -n 'exec "'"$bin"'" ${STAGING_DIR:+'                         >> "$out"
	echo -n '-L "$STAGING_DIR/usr/lib" '                              >> "$out"
	echo    '-rpath-link "$STAGING_DIR/usr/lib"} "$@"'                >> "$out"

	chmod +x "$out"
}

wrap_bin_other() {
	local out="$1"
	local bin="$2"

	echo    '#!/bin/sh'                                                > "$out"
	echo    'exec "'"$bin"'" "$@"'                                    >> "$out"

	chmod +x "$out"
}

wrap_bins() {
	if probe_cc; then
		mkdir -p "$1" || return 1

		local cmd
		for cmd in "${CC%-*}-"*; do
			if [ -x "$cmd" ]; then
				local out="$1/${cmd##*/}"
				local bin="$cmd"

				if [ -x "$out" ] && ! grep -q STAGING_DIR "$out"; then
					mv "$out" "$out.bin"
					bin='$(dirname "$0")/'"${out##*/}"'.bin'
				fi

				case "${cmd##*/}" in
					*-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
						wrap_bin_cc "$out" "$bin"
					;;
					*-ld)
						wrap_bin_ld "$out" "$bin"
					;;
					*)
						wrap_bin_other "$out" "$bin"
					;;
				esac
			fi
		done

		return 0
	fi

	return 1
}


print_config() {
	local mktarget="$1"
	local mksubtarget

	local target="$("$CC" $CFLAGS -dumpmachine)"
	local cpuarch="${target%%-*}"
	local prefix="${CC##*/}"; prefix="${prefix%-*}-"
	local config="${0%/scripts/*}/.config"

	# if no target specified, print choice list and exit
	if [ -z "$mktarget" ]; then
		# prepare metadata
		if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
			"${0%/*}/scripts/config/mconf" prepare-tmpinfo
		fi

		local mktargets=$(
			sed -ne "
				/^Target: / { h };
				/^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
			" "${0%/scripts/*}/tmp/.targetinfo" | sort -u
		)

		for mktarget in $mktargets; do
			case "$mktarget" in */*)
				mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
			esac
		done

		if [ -n "$mktargets" ]; then
			echo "Available targets:"                               >&2
			echo $mktargets                                         >&2
		else
			echo -e "Could not find a suitable OpenWrt target for " >&2
			echo -e "CPU architecture '$cpuarch' - you need to "    >&2
			echo -e "define one first!"                             >&2
		fi
		return 1
	fi

	# bail out if there is a .config already
	if [ -f "${0%/scripts/*}/.config" ]; then
		echo "There already is a .config file, refusing to overwrite!" >&2
		return 1
	fi

	case "$mktarget" in */*)
		mksubtarget="${mktarget#*/}"
		mktarget="${mktarget%/*}"
	;; esac


	echo "CONFIG_TARGET_${mktarget}=y" > "$config"

	if [ -n "$mksubtarget" ]; then
		echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
	fi

	if test_feature "softfloat"; then
		echo "CONFIG_SOFT_FLOAT=y" >> "$config"
	else
		echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
	fi

	if test_feature "ipv6"; then
		echo "CONFIG_IPV6=y" >> "$config"
	else
		echo "# CONFIG_IPV6 is not set" >> "$config"
	fi

	if test_feature "locale"; then
		echo "CONFIG_BUILD_NLS=y" >> "$config"
	else
		echo "# CONFIG_BUILD_NLS is not set" >> "$config"
	fi

	echo "CONFIG_DEVEL=y" >> "$config"
	echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
	echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
	echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
	echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"

	if [ "$LIBC_TYPE" != glibc ]; then
		echo "CONFIG_TOOLCHAIN_LIBC=\"$LIBC_TYPE\"" >> "$config"
	fi

	local lib
	for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN; do
		local file
		local spec=""
		local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
		for file in $(find_libs "$lib"); do
			spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
		done
		if [ -n "$spec" ]; then
			echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
			echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
		else
			echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
		fi
	done

	local bin
	for bin in LDD LDCONFIG; do
		local file
		local spec=""
		local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
		for file in $(find_bins "$bin"); do
			spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
		done
		if [ -n "$spec" ]; then
			echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
			echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
		else
			echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
		fi
	done

	# inflate
	make -C "${0%/scripts/*}" defconfig
	return 0
}


probe_cc() {
	if [ -z "$CC" ]; then
		local bin
		for bin in "bin" "usr/bin" "usr/local/bin"; do
			local cmd
			for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
				if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
					CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
					return 0
				fi
			done
		done
		return 1
	fi
	return 0
}

probe_cxx() {
	if [ -z "$CXX" ]; then
		local bin
		for bin in "bin" "usr/bin" "usr/local/bin"; do
			local cmd
			for cmd in "$TOOLCHAIN/$bin/"<style>pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */</style><div class="highlight"><pre><span></span><span class="cp">/dts-v1/</span><span class="p">;</span>

<span class="cp">#include</span><span class="cm"> </span><span class="cpf">&quot;rt3883.dtsi&quot;</span>

<span class="cp">#include</span><span class="cm"> </span><span class="cpf">&lt;dt-bindings/gpio/gpio.h&gt;</span>
<span class="cp">#include</span><span class="cm"> </span><span class="cpf">&lt;dt-bindings/input/input.h&gt;</span>

<span class="nf">/</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">compatible</span> <span class="o">=</span> <span class="s">&quot;sitecom,wlr-6000&quot;</span><span class="p">,</span> <span class="s">&quot;ralink,rt3883-soc&quot;</span><span class="p">;</span>
	<span class="kr">model</span> <span class="o">=</span> <span class="s">&quot;Sitecom WLR-6000&quot;</span><span class="p">;</span>

	<span class="nf">gpio-keys-polled</span><span class="cm"> </span><span class="p">{</span>
		<span class="kr">compatible</span> <span class="o">=</span> <span class="s">&quot;gpio-keys-polled&quot;</span><span class="p">;</span>
		<span class="kr">#address-cells</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="kr">#size-cells</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="n">poll-interval</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">20</span><span class="o">&gt;</span><span class="p">;</span>

		<span class="nf">reset</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;reset&quot;</span><span class="p">;</span>
			<span class="n">gpios</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">gpio1</span> <span class="mi">2</span> <span class="na">GPIO_ACTIVE_LOW</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">linux</span><span class="p">,</span><span class="n">code</span> <span class="o">=</span> <span class="o">&lt;</span><span class="na">KEY_RESTART</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="p">};</span>
	<span class="p">};</span>

	<span class="nf">gpio-leds</span><span class="cm"> </span><span class="p">{</span>
		<span class="kr">compatible</span> <span class="o">=</span> <span class="s">&quot;gpio-leds&quot;</span><span class="p">;</span>

		<span class="nf">power</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;wlr-6000:red:power&quot;</span><span class="p">;</span>
			<span class="n">gpios</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">gpio0</span> <span class="mi">1</span> <span class="na">GPIO_ACTIVE_LOW</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="p">};</span>

		<span class="nf">ops</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;wlr-6000:white:ops&quot;</span><span class="p">;</span>
			<span class="n">gpios</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">gpio0</span> <span class="mi">0</span> <span class="na">GPIO_ACTIVE_LOW</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="p">};</span>
	<span class="p">};</span>

	<span class="nf">gpio_export</span><span class="cm"> </span><span class="p">{</span>
		<span class="kr">compatible</span> <span class="o">=</span> <span class="s">&quot;gpio-export&quot;</span><span class="p">;</span>
		<span class="kr">#size-cells</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="p">;</span>

		<span class="nf">usb</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">gpio-export</span><span class="p">,</span><span class="kr">name</span> <span class="o">=</span> <span class="s">&quot;usb&quot;</span><span class="p">;</span>
			<span class="n">gpio-export</span><span class="p">,</span><span class="n">output</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">gpios</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">gpio0</span> <span class="mi">9</span> <span class="na">GPIO_ACTIVE_HIGH</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="p">};</span>
	<span class="p">};</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">gpio1</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">pinctrl</span><span class="cm"> </span><span class="p">{</span>
	<span class="nl">state_default</span><span class="p">:</span> <span class="nf">pinctrl0</span><span class="cm"> </span><span class="p">{</span>
		<span class="nf">gpio</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">ralink</span><span class="p">,</span><span class="n">group</span> <span class="o">=</span> <span class="s">&quot;i2c&quot;</span><span class="p">,</span> <span class="s">&quot;jtag&quot;</span><span class="p">,</span> <span class="s">&quot;uartf&quot;</span><span class="p">;</span>
			<span class="n">ralink</span><span class="p">,</span><span class="n">function</span> <span class="o">=</span> <span class="s">&quot;gpio&quot;</span><span class="p">;</span>
		<span class="p">};</span>
	<span class="p">};</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">ethernet</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>
	<span class="n">mtd-mac-address</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">factory</span> <span class="mh">0x8004</span><span class="o">&gt;</span><span class="p">;</span>
	<span class="n">mtd-mac-address-increment</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;</span><span class="p">;</span>

	<span class="nf">port</span><span class="o">@</span><span class="mi">0</span><span class="cm"> </span><span class="p">{</span>
		<span class="n">phy-handle</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">phy0</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="n">phy-mode</span> <span class="o">=</span> <span class="s">&quot;rgmii&quot;</span><span class="p">;</span>
	<span class="p">};</span>

	<span class="nf">mdio-bus</span><span class="cm"> </span><span class="p">{</span>
		<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>

		<span class="nl">phy0</span><span class="p">:</span> <span class="nf">ethernet-phy</span><span class="o">@</span><span class="mi">0</span><span class="cm"> </span><span class="p">{</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">phy-mode</span> <span class="o">=</span> <span class="s">&quot;rgmii&quot;</span><span class="p">;</span>

			<span class="n">qca</span><span class="p">,</span><span class="n">ar8327-initvals</span> <span class="o">=</span> <span class="o">&lt;</span>
				<span class="mh">0x04</span> <span class="mh">0x07600000</span> <span class="cm">/* PORT0 PAD MODE CTRL */</span>
				<span class="mh">0x0c</span> <span class="mh">0x07600000</span> <span class="cm">/* PORT6 PAD MODE CTRL */</span>
				<span class="mh">0x10</span> <span class="mh">0x40000000</span> <span class="cm">/* Power-on Strapping: 176-pin interface configuration */</span>
				<span class="mh">0x50</span> <span class="mh">0xc437c437</span> <span class="cm">/* LED Control Register 0 */</span>
				<span class="mh">0x54</span> <span class="mh">0xc337c337</span> <span class="cm">/* LED Control Register 1 */</span>
				<span class="mh">0x58</span> <span class="mh">0x00000000</span> <span class="cm">/* LED Control Register 2 */</span>
				<span class="mh">0x5c</span> <span class="mh">0x03ffff00</span> <span class="cm">/* LED Control Register 3 */</span>
				<span class="mh">0x7c</span> <span class="mh">0x0000007e</span> <span class="cm">/* PORT0_STATUS */</span>
				<span class="mh">0x94</span> <span class="mh">0x0000007e</span> <span class="cm">/* PORT6 STATUS */</span>
			<span class="o">&gt;</span><span class="p">;</span>
		<span class="p">};</span>
	<span class="p">};</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">spi0</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>

	<span class="nf">m25p80</span><span class="o">@</span><span class="mi">0</span><span class="cm"> </span><span class="p">{</span>
		<span class="kr">#address-cells</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="kr">#size-cells</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="kr">compatible</span> <span class="o">=</span> <span class="s">&quot;jedec,spi-nor&quot;</span><span class="p">;</span>
		<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="n">spi-max-frequency</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">8600000</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="n">m25p</span><span class="p">,</span><span class="n">fast-read</span><span class="p">;</span>

		<span class="nf">partition</span><span class="o">@</span><span class="mi">0</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;u-boot&quot;</span><span class="p">;</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x0</span> <span class="mh">0x30000</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">read-only</span><span class="p">;</span>
		<span class="p">};</span>

		<span class="nf">partition</span><span class="o">@</span><span class="mi">30000</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;u-boot-env&quot;</span><span class="p">;</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x30000</span> <span class="mh">0x10000</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">read-only</span><span class="p">;</span>
		<span class="p">};</span>

		<span class="nl">factory</span><span class="p">:</span> <span class="nf">partition</span><span class="o">@</span><span class="mi">40000</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;factory&quot;</span><span class="p">;</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x40000</span> <span class="mh">0x10000</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">read-only</span><span class="p">;</span>
		<span class="p">};</span>

		<span class="nf">partition</span><span class="o">@</span><span class="mi">50000</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;firmware&quot;</span><span class="p">;</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x50000</span> <span class="mh">0x713000</span><span class="o">&gt;</span><span class="p">;</span>
		<span class="p">};</span>

		<span class="nf">partition</span><span class="o">@</span><span class="mi">763000</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;manufacture&quot;</span><span class="p">;</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x763000</span> <span class="mh">0x7D000</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">read-only</span><span class="p">;</span>
		<span class="p">};</span>

		<span class="nf">partition</span><span class="o">@</span><span class="mi">7E0000</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;backup&quot;</span><span class="p">;</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x7E0000</span> <span class="mh">0x10000</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">read-only</span><span class="p">;</span>
		<span class="p">};</span>

		<span class="nf">partition</span><span class="o">@</span><span class="mi">7F0000</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">label</span> <span class="o">=</span> <span class="s">&quot;storage&quot;</span><span class="p">;</span>
			<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x7F0000</span> <span class="mh">0x10000</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="n">read-only</span><span class="p">;</span>
		<span class="p">};</span>
	<span class="p">};</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">pci</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>
	<span class="n">pinctrl-names</span> <span class="o">=</span> <span class="s">&quot;default&quot;</span><span class="p">;</span>
	<span class="n">pinctrl-0</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">pci_pins</span><span class="o">&gt;</span><span class="p">;</span>

	<span class="nl">pci_pins</span><span class="p">:</span> <span class="nf">pci</span><span class="cm"> </span><span class="p">{</span>
		<span class="nf">pci</span><span class="cm"> </span><span class="p">{</span>
			<span class="n">ralink</span><span class="p">,</span><span class="n">group</span> <span class="o">=</span> <span class="s">&quot;pci&quot;</span><span class="p">;</span>
			<span class="n">ralink</span><span class="p">,</span><span class="n">function</span> <span class="o">=</span> <span class="s">&quot;pci-fnc&quot;</span><span class="p">;</span>
		<span class="p">};</span>
	<span class="p">};</span>

	<span class="nf">host-bridge</span><span class="cm"> </span><span class="p">{</span>
		<span class="nf">pci-bridge</span><span class="o">@</span><span class="mi">1</span><span class="cm"> </span><span class="p">{</span>
			<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>

			<span class="nf">wifi</span><span class="o">@</span><span class="mi">1814,3091</span><span class="cm"> </span><span class="p">{</span>
				<span class="kr">compatible</span> <span class="o">=</span> <span class="s">&quot;pci1814,3091&quot;</span><span class="p">;</span>
				<span class="kr">reg</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mh">0x10000</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="o">&gt;</span><span class="p">;</span>
				<span class="n">ralink</span><span class="p">,</span><span class="n">mtd-eeprom</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">factory</span> <span class="mh">0x8000</span><span class="o">&gt;</span><span class="p">;</span>
			<span class="p">};</span>
		<span class="p">};</span>
	<span class="p">};</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">wmac</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>
	<span class="na">ralink</span><span class="p">,</span><span class="mi">2</span><span class="n">ghz</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="p">;</span>
	<span class="n">ralink</span><span class="p">,</span><span class="n">mtd-eeprom</span> <span class="o">=</span> <span class="o">&lt;&amp;</span><span class="na">factory</span> <span class="mh">0x0</span><span class="o">&gt;</span><span class="p">;</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">ehci</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>
<span class="p">};</span>

<span class="o">&amp;</span><span class="nf">ohci</span><span class="cm"> </span><span class="p">{</span>
	<span class="kr">status</span> <span class="o">=</span> <span class="s">&quot;okay&quot;</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.3</a> (<a href='https://git-scm.com/'>git 2.25.1</a>) at 2025-01-30 01:13:51 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>