/*
 *  BSD LICENSE
 *
 *  Copyright (C) 2017 Christian Mehlis <christian@m3hlis.de>
 *  Copyright (C) 2018 Mathias Kresin <dev@kresin.me>
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the copyright holders nor the names of any
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "qcom-ipq8064-v1.0.dtsi"

#include <dt-bindings/input/input.h>
#include <dt-bindings/soc/qcom,tcsr.h>

/ {
	compatible = "compex,wpq864", "qcom,ipq8064";
	model = "Compex WPQ864";

	aliases {
		mdio-gpio0 = &mdio0;
		serial0 = &gsbi4_serial;
		ethernet0 = &gmac1;
		ethernet1 = &gmac0;

		led-boot = &led_pass;
		led-failsafe = &led_fail;
		led-running = &led_pass;
		led-upgrade = &led_pass;
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	soc {
		rpm@108000 {
			pinctrl-0 = <&rpm_pins>;
			pinctrl-names = "default";
		};

		nand@1ac00000 {
			status = "okay";

			pinctrl-0 = <&nand_pins>;
			pinctrl-names = "default";

			mt29f2g08abbeah4@0 {
				compatible = "qcom,nandcs";

				reg = <0>;

				nand-ecc-strength = <4>;
				nand-bus-width = <8>;
				nand-ecc-step-size = <512>;

				partitions {
					compatible = "fixed-partitions";
					#address-cells = <1>;
					#size-cells = <1>;

					SBL1@0 {
						label = "SBL1";
						reg = <0x0000000 0x0040000>;
						read-only;
					};

					MIBIB@40000 {
						label = "MIBIB";
						reg = <0x0040000 0x0140000>;
						read-only;
					};

					SBL2@180000 {
						label = "SBL2";
						reg = <0x0180000 0x0140000>;
						read-only;
					};

					SBL3@2c0000 {
						label = "SBL3";
						reg = <0x02c0000 0x0280000>;
						read-only;
					};

					DDRCONFIG@540000 {
						label = "DDRCONFIG";
						reg = <0x0540000 0x0120000>;
						read-only;
					};

					SSD@660000 {
						label = "SSD";
						reg = <0x0660000 0x0120000>;
						read-only;
					};

					TZ@780000 {
						label = "TZ";
						reg = <0x0780000 0x0280000>;
						read-only;
					};

					RPM@a00000 {
						label = "RPM";
						reg = <0x0a00000 0x0280000>;
						read-only;
					};

					APPSBL@c80000 {
						label = "APPSBL";
						reg = <0x0c80000 0x0500000>;
						read-only;
					};

					APPSBLENV@1180000 {
						label = "APPSBLENV";
						reg = <0x1180000 0x0080000>;
					};

					ART@1200000 {
						label = "ART";
						reg = <0x1200000 0x0140000>;
					};

					ubi@1340000 {
						label = "ubi";
						reg = <0x1340000 0x4000000>;
					};

					BOOTCONFIG@1340000 {
						label = "BOOTCONFIG";
						reg = <0x5340000 0x0060000>;
					};

					SBL2-1@53a0000- {
						label = "SBL2_1";
						reg = <0x53a0000 0x0140000>;
						read-only;
					};

					SBL3-1@54e0000 {
						label = "SBL3_1";
						reg = <0x54e0000 0x0280000>;
						read-only;
					};

					DDRCONFIG-1@5760000 {
						label = "DDRCONFIG_1";
						reg = <0x5760000 0x0120000>;
						read-only;
					};

					SSD-1@5880000 {
						label = "SSD_1";
						reg = <0x5880000 0x0120000>;
						read-only;
					};

					TZ-1@59a0000 {
						label = "TZ_1";
						reg = <0x59a0000 0x0280000>;
						read-only;
					};

					RPM-1@5c20000 {
						label = "RPM_1";
						reg = <0x5c20000 0x0280000>;
						read-only;
					};

					BOOTCONFIG1@5ea0000 {
						label = "BOOTCONFIG1";
						reg = <0x5ea0000 0x0060000>;
					};

					APPSBL-1@5f00000 {
						label = "APPSBL_1";
						reg = <0x5f00000 0x0500000>;
						read-only;
					};

					ubi-1@6400000 {
						label = "ubi_1";
						reg = <0x6400000 0x4000000>;
					};

					unused@a400000 {
						label = "unused";
						reg = <0xa400000 0x5c00000>;
					};
				};
			};
		};
	};

	mdio0: mdio {
		#address-cells = <1>;
		#size-cells = <0>;

		compatible = "virtual,mdio-gpio";

		pinctrl-0 = <&mdio0_pins>;
		pinctrl-names = "default";

		gpios = <&qcom_pinmux 1 GPIO_ACTIVE_HIGH &qcom_pinmux 0 GPIO_ACTIVE_HIGH>;

		ethernet-phy@0 {
			reg = <0>;
			qca,ar8327-initvals = <
				0x00004 0x7600000   /* PAD0_MODE */
				0x00008 0x1000000   /* PAD5_MODE */
				0x0000c 0x80        /* PAD6_MODE */
				0x000e4 0x6a545     /* MAC_POWER_SEL */
				0x000e0 0xc74164de  /* SGMII_CTRL */
				0x0007c 0x4e        /* PORT0_STATUS */
				0x00094 0x4e        /* PORT6_STATUS */
				>;
		};

		ethernet-phy@4 {
			reg = <4>;
		};
	};

	gpio-leds {
		compatible = "gpio-leds";

		pinctrl-0 = <&led_pins>;
		pinctrl-names = "default";

		rss4 {
			label = "wpq864:green:rss4";
			gpios = <&qcom_pinmux 23 GPIO_ACTIVE_HIGH>;
		};

		rss3 {
			label = "wpq864:green:rss3";
			gpios = <&qcom_pinmux 24 GPIO_ACTIVE_HIGH>;
			default-state = "keep";
		};

		rss2 {
			label = "wpq864:orange:rss2";
			gpios = <&qcom_pinmux 25 GPIO_ACTIVE_HIGH>;
		};

		rss1 {
			label = "wpq864:red:rss1";
			gpios = <&qcom_pinmux 22 GPIO_ACTIVE_HIGH>;
		};

		led_pass: pass {
			label = "wpq864:green:pass";
			gpios = <&qcom_pinmux 53 GPIO_ACTIVE_HIGH>;
		};

		led_fail: fail {
			label = "wpq864:green:fail";
			gpios = <&qcom_pinmux 9 GPIO_ACTIVE_HIGH>;
		};

		usb {
			label = "wpq864:green:usb";
			gpios = <&qcom_pinmux 7 GPIO_ACTIVE_HIGH>;
		};

		usb-pcie {
			label = "wpq864:green:usb-pcie";
			gpios = <&qcom_pinmux 8 GPIO_ACTIVE_HIGH>;
		};
	};

	gpio-keys {
		compatible = "gpio-keys";

		pinctrl-0 = <&button_pins>;
		pinctrl-names = "default";

		reset {
			label = "reset";
			gpios = <&qcom_pinmux 54 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_RESTART>;
		};
	};

	beeper {
		compatible = "gpio-beeper";

		pinctrl-0 = <&beeper_pins>;
		pinctrl-names = "default";

		gpios = <&qcom_pinmux 55 GPIO_ACTIVE_HIGH>;
	};
};

&adm_dma {
	status = "okay";
};

&gmac1 {
	status = "okay";

	pinctrl-0 = <&rgmii2_pins>;
	pinctrl-names = "default";

	phy-mode = "rgmii";
	qcom,id = <1>;

	fixed-link {
		speed = <1000>;
		full-duplex;
	};
};

&gmac2 {
	status = "okay";

	phy-mode = "sgmii";
	qcom,id = <2>;

	fixed-link {
		speed = <1000>;
		full-duplex;
	};
};

&gsbi4 {
	status = "okay";
	qcom,mode = <GSBI_PROT_I2C_UART>;
};

&gsbi4_serial {
	status = "okay";

	pinctrl-0 = <&uart0_pins>;
	pinctrl-names = "default";
};

&gsbi5 {
	status = "okay";

	qcom,mode = <GSBI_PROT_SPI>;

	spi@1a280000 {
		status = "okay";

		pinctrl-0 = <&spi_pins>;
		pinctrl-names = "default";

		cs-gpios = <&qcom_pinmux 20 GPIO_ACTIVE_HIGH>;

		s25fl256s1@0 {
			compatible = "jedec,spi-nor";
			#address-cells = <1>;
			#size-cells = <1>;
			reg = <0>;
			spi-max-frequency = <50000000>;
		};
	};
};

&hs_phy_0 {		/* USB3 port 0 HS phy */
	status = "okay";
};

&hs_phy_1 {		/* USB3 port 1 HS phy */
	status = "okay";
};

&ss_phy_0 {		/* USB3 port 0 SS phy */
	status = "okay";

	rx_eq = <2>;
	tx_deamp_3_5db = <32>;
	mpll = <160>;
};

&ss_phy_1 {		/* USB3 port 1 SS phy */
	status = "okay";

	rx_eq = <2>;
	tx_deamp_3_5db = <32>;
	mpll = <160>;
};

&pcie0 {
	status = "okay";

	/delete-property/ pinctrl-0;
	/delete-property/ pinctrl-names;
	/delete-property/ perst-gpios;
};

&pcie1 {
	status = "okay";
};

&pcie2 {
	status = "okay";

	/delete-property/ pinctrl-0;
	/delete-property/ pinctrl-names;
	/delete-property/ perst-gpios;
};

&qcom_pinmux {
	pinctrl-names = "default";
	pinctrl-0 = <&state_default>;

	state_default: pinctrl0 {
		pcie0_pcie2_perst {
			pins = "gpio3";
			function = "gpio";
			drive-strength = <2>;
			bias-disable;
			output-high;
		};
	};

	led_pins: led_pins {
		mux {
			pins = "gpio7", "gpio8", "gpio9", "gpio22",
			       "gpio23", "gpio24", "gpio25", "gpio53";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-up;
		};
	};

	button_pins: button_pins {
		mux {
			pins = "gpio54";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-up;
		};
	};

	beeper_pins: beeper_pins {
		mux {
			pins = "gpio55";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-up;
		};
	};

	rpm_pins: rpm_pins {
		mux {
			pins = "gpio12", "gpio13";
			function = "gsbi4";
			drive-strength = <10>;
			bias-disable;
		};
	};

	uart0_pins: uart0_pins {
		mux {
			pins = "gpio10", "gpio11";
			function = "gsbi4";
			drive-strength = <10>;
			bias-disable;
		};
	};

	spi_pins: spi_pins {
		mux {
			pins = "gpio18", "gpio19";
			function = "gsbi5";
			drive-strength = <10>;
			bias-pull-down;
		};

		clk {
			pins = "gpio21";
			function = "gsbi5";
			drive-strength = <12>;
			bias-pull-down;
		};

		cs {
			pins = "gpio20";
			function = "gpio";
			drive-strength = <10>;
			bias-pull-up;
		};
	};

	nand_pins: nand_pins {
		mux {
			pins = "gpio34", "gpio35", "gpio36", "gpio37",
			       "gpio38", "gpio39", "gpio40", "gpio41",
			       "gpio42", "gpio43", "gpio44", "gpio45",
			       "gpio46", "gpio47";
			function = "nand";
			drive-strength = <10>;
			bias-disable;
		};

		pullups {
			pins = "gpio39";
			bias-pull-up;
		};

		hold {
			pins = "gpio40", "gpio41", "gpio42", "gpio43",
			       "gpio44", "gpio45", "gpio46", "gpio47";
			bias-bus-hold;
		};
	};

	mdio0_pins: mdio0_pins {
		mux {
			pins = "gpio0", "gpio1";
			function = "gpio";
			drive-strength = <8>;
			bias-disable;
		};
	};

	rgmii2_pins: rgmii2_pins {
		mux {
			pins = "gpio27", "gpio28", "gpio29", "gpio30",
			       "gpio31", "gpio32", "gpio51", "gpio52",
			       "gpio59", "gpio60", "gpio61", "gpio62";
			function = "rgmii2";
			drive-strength = <8>;
			bias-disable;
		};
	};
};

&usb3_0 {
	status = "okay";
};


&usb3_1 {
	status = "okay";
};

&tcsr {
	qcom,usb-ctrl-select = <TCSR_USB_SELECT_USB3_DUAL>;
};
