/*
 * Copyright (c) 2022, The Linux Foundation. All rights reserved.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "qcom-ipq6018.dtsi"
#include <dt-bindings/input/input.h>

/ {
	#address-cells = <0x2>;
	#size-cells = <0x2>;
	interrupt-parent = <&intc>;
	qcom,msm-id = <0x1A5 0x0>;

	aliases {
		ethernet0 = "/soc/dp1";
		ethernet1 = "/soc/dp2";
		ethernet2 = "/soc/dp3";
		ethernet3 = "/soc/dp4";
		ethernet4 = "/soc/dp5";

		led-boot = &led_green;
		led-running = &led_blue;
		led-upgrade = &led_red;
	};

	chosen {
		bootargs = "console=ttyMSM0,115200,n8 rw init=/init";

#ifdef __IPQ_MEM_PROFILE_256_MB__
                bootargs-append = " console=ttyMSM0,115200n8 panic=10 ubi.mtd=nand root=mtd:ubi_rootfs rootfstype=squashfs swiotlb=1 rootwait";
#else
                bootargs-append = " console=ttyMSM0,115200n8 panic=10 ubi.mtd=nand root=mtd:ubi_rootfs rootfstype=squashfs swiotlb=1 coherent_pool=2M rootwait";
#endif
	};


	/*
	 * +=========+==============+========================+
	 * |        |              |                         |
	 * | Region | Start Offset |          Size           |
	 * |        |              |                         |
	 * +--------+--------------+-------------------------+
	 * |        |              |                         |
	 * |        |              |                         |
	 * |        |              |                         |
	 * |        |              |                         |
	 * | Linux  |  0x41000000  |         139MB           |
	 * |        |              |                         |
	 * |        |              |                         |
	 * |        |              |                         |
	 * +--------+--------------+-------------------------+
	 * | TZ App |  0x49B00000  |           6MB           |
	 * +--------+--------------+-------------------------+
	 *
	 * From the available 145 MB for Linux in the first 256 MB,
	 * we are reserving 6 MB for TZAPP.
	 *
	 * Refer arch/arm64/boot/dts/qcom/qcom-ipq6018-memory.dtsi
	 * for memory layout.
	 */

/* TZAPP is enabled only in default memory profile */
	reserved-memory {
#if !defined(__IPQ_MEM_PROFILE_256_MB__) && !defined(__IPQ_MEM_PROFILE_512_MB__)
		tzapp:tzapp@49B00000 {	/* TZAPPS */
			no-map;
			reg = <0x0 0x49B00000 0x0 0x00600000>;
		};
#endif
		qcn9000_pcie0@50200000 {
			no-map;
			reg = <0x0 0x50200000 0x0 0x03700000>;
		};

		mhi_region0: dma_pool0@53900000 {
			compatible = "shared-dma-pool";
			no-map;
			reg = <0x0 0x53900000 0x0 0x01800000>;
		};
	};
};

&tlmm {
	pinctrl-0 = <&sd_ldo_pins &generic_gpios &pcie_pins>;
	pinctrl-names = "default";

	uart_pins: uart_pins {
		mux {
			pins = "gpio44", "gpio45";
			function = "blsp2_uart";
			drive-strength = <8>;
			bias-pull-down;
		};
	};

	sd_ldo_pins: sd_ldo_pins {
		mux {
			pins = "gpio66";
			function = "gpio";
			drive-strength = <2>;
			bias-disable;
			output-low;
		};
	};

	spi_0_pins: spi_0_pins {
		mux {
			pins = "gpio38", "gpio39", "gpio40", "gpio41";
			function = "blsp0_spi";
			drive-strength = <8>;
			bias-pull-down;
		};
	};

	spi_1_pins: spi_1_pins {
		mux {
			pins = "gpio69", "gpio71", "gpio72";
			function = "blsp1_spi";
			drive-strength = <8>;
			bias-pull-down;
		};
		spi_cs {
			pins = "gpio70";
			function = "blsp1_spi";
			drive-strength = <8>;
			bias-disable;
		};
		quartz_interrupt {
			pins = "gpio78";
			function = "gpio";
			input;
			bias-disable;
		};
		quartz_reset {
			pins = "gpio79";
			function = "gpio";
			output-low;
			bias-disable;
		};
	};

	button_pins: button_pins {
		switch_button {
			pins = "gpio9";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-up;
		};
		reset_button {
			pins = "gpio19";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-up;
		};
	};

	mdio_pins: mdio_pinmux {
		mux_0 {
			pins = "gpio64";
			function = "mdc";
			drive-strength = <8>;
			bias-pull-up;
		};
		mux_1 {
			pins = "gpio65";
			function = "mdio";
			drive-strength = <8>;
			bias-pull-up;
		};
		mux_2 {
			pins = "gpio75";
			function = "gpio";
			bias-pull-up;
		};
		mux_3 {
			pins = "gpio77";
			function = "gpio";
			bias-pull-up;
		};
	};

	leds_pins: leds_pins {
		led_5g {
			pins = "gpio35";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};
		led_2g {
			pins = "gpio37";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};

		m2_1_regulator {
			pins = "gpio29";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};

		m2_1_pwr_en {
			pins = "gpio49";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};

		m2_1_reset_n {
			pins = "gpio32";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};

		led_red: red {
			pins = "gpio50";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};

		led_green: green {
			pins = "gpio54";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};

		led_blue: blue {
			pins = "gpio57";
			function = "gpio";
			drive-strength = <8>;
			bias-pull-down;
		};
	};

	sd_pins: sd_pins {
		mux_1 {
			pins = "gpio62";
			function = "sd_card";
			drive-strength = <8>;
			bias-pull-up;
		};

		mux_2 {
			pins = "gpio23";
			function = "gpio";
			drive_strength = <8>;
			bias-disable;
			output-high;
		};
	};

	extcon_usb_pins: extcon_usb_pins {
		mux {
			pins = "gpio26";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-down;
		};
	};

	generic_gpios: generic_gpios {
		gpio42 {
			pins = "gpio42";
			function = "gpio";
			drive-strength = <8>;
			bias-disable;
			output-high;
		};
	};

	pcie_pins: pcie_pins {
		pcie0_enable {
			pins = "gpio34";
			function = "gpio";
			drive-strength = <8>;
			bias-disable;
			output-high;
		};
	};

        i2c_2_pins: i2c_2_pins {
                mux {
                        pins = "gpio55", "gpio56";
                        function = "gpio";
                        drive-strength = <16>;
                        bias-pull-up;
                        input-enable;
                };

                mux_2 {
                        pins = "gpio48";
                        function = "gpio";
                        drive_strength = <16>;
                        output-high;
                };

                mux_3 {
                        pins = "gpio73";
                        function = "gpio";
                        drive-stength = <16>;
                        output-low;
                };
        };
};

&soc {
	extcon_usb: extcon_usb {
		pinctrl-0 = <&extcon_usb_pins>;
		pinctrl-names = "default";
		id-gpio = <&tlmm 26 GPIO_ACTIVE_LOW>;
		status = "ok";
	};

	mdio0: mdio@90000 {
		pinctrl-0 = <&mdio_pins>;
		pinctrl-names = "default";
		phy-reset-gpio = <&tlmm 75 0 &tlmm 77 1>;
		status = "ok";
		phy0: ethernet-phy@0 {
			reg = <0>;
		};
		phy1: ethernet-phy@1 {
			reg = <1>;
		};
		phy2: ethernet-phy@2 {
			reg = <2>;
		};
                phy3: ethernet-phy@3 {
                        reg = <3>;
                };
                phy4: ethernet-phy@4 {
                        reg = <4>;
                };
	};

	ess0: ess-switch@3a000000 {
		switch_cpu_bmp = <0x1>;  /* cpu port bitmap */
		switch_lan_bmp = <0x1e>; /* lan port bitmap */
		switch_wan_bmp = <0x20>; /* wan port bitmap */
		switch_inner_bmp = <0xc0>; /*inner port bitmap*/
		switch_mac_mode = <0x0>; /* mac mode for uniphy instance0*/
		switch_mac_mode1 = <0xff>; /* mac mode for uniphy instance1*/
		switch_mac_mode2 = <0xff>; /* mac mode for uniphy instance2*/
		qcom,port_phyinfo {
			port@0 {
				port_id = <1>;
				phy_address = <0>;
			};
			port@1 {
				port_id = <2>;
				phy_address = <1>;
			};
			port@2 {
				port_id = <3>;
				phy_address = <2>;
			};
			port@3 {
				port_id = <4>;
				phy_address = <3>;
			};
			port@4 {
				port_id = <5>;
				phy_address = <4>;
			};
		};
	};

	dp1 {
		device_type = "network";
		compatible = "qcom,nss-dp";
		qcom,id = <1>;
		reg = <0x3a001000 0x200>;
		qcom,mactype = <0>;
		local-mac-address = [000000000000];
		qcom,link-poll = <1>;
		qcom,phy-mdio-addr = <0>;
		phy-mode = "sgmii";
	};

	dp2 {
		device_type = "network";
		compatible = "qcom,nss-dp";
		qcom,id = <2>;
		reg = <0x3a001200 0x200>;
		qcom,mactype = <0>;
		local-mac-address = [000000000000];
		qcom,link-poll = <1>;
		qcom,phy-mdio-addr = <1>;
		phy-mode = "sgmii";
	};

	dp3 {
		device_type = "network";
		compatible = "qcom,nss-dp";
		qcom,id = <3>;
		reg = <0x3a001400 0x200>;
		qcom,mactype = <0>;
		local-mac-address = [000000000000];
		qcom,link-poll = <1>;
		qcom,phy-mdio-addr = <2>;
		phy-mode = "sgmii";
	};

	dp4 {
		device_type = "network";
		compatible = "qcom,nss-dp";
		qcom,id = <4>;
		reg = <0x3a001600 0x200>;
		qcom,mactype = <0>;
		local-mac-address = [000000000000];
		qcom,link-poll = <1>;
		qcom,phy-mdio-addr = <3>;
		phy-mode = "sgmii";
	};

	dp5 {
		device_type = "network";
		compatible = "qcom,nss-dp";
		qcom,id = <5>;
		reg = <0x3a001800 0x200>;
		qcom,mactype = <0>;
		local-mac-address = [000000000000];
		qcom,link-poll = <1>;
		qcom,phy-mdio-addr = <4>;
		phy-mode = "sgmii";
	};

	leds {
		compatible = "gpio-leds";
		pinctrl-0 = <&leds_pins>;
		pinctrl-names = "default";

		led@29 {
			label = "m2_1_regulator";
			gpios = <&tlmm 29 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "none";
			default-state = "on";
		};

		led@49 {
			label = "m2_1_pwr_en";
			gpios = <&tlmm 49 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "none";
			default-state = "on";
		};

		led@32 {
			label = "m2_1_reset_n";
			gpios = <&tlmm 32 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "none";
			default-state = "off";
		};

		led@50 {
			label = "red";
			gpios = <&tlmm 50 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "none";
			default-state = "on";
		};

		led@54 {
			label = "green";
			gpios = <&tlmm 54 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "none";
			default-state = "on";
		};

		led@57 {
			label = "blue";
			gpios = <&tlmm 57 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "none";
			default-state = "off";
		};
	};

	gpio_keys {
		compatible = "gpio-keys";
		pinctrl-0 = <&button_pins>;
		pinctrl-names = "default";

		switch {
			label = "switch";
			linux,code = <KEY_WPS_BUTTON>;
			gpios = <&tlmm 9 GPIO_ACTIVE_LOW>;
			linux,input-type = <1>;
			debounce-interval = <60>;
		};
		reset {
			label = "reset";
			linux,code = <KEY_RESTART>;
			gpios = <&tlmm 18 GPIO_ACTIVE_LOW>;
			linux,input-type = <1>;
			debounce-interval = <60>;
		};
	};

	i2c_2: i2c@0 { /* i2c gpio */
		pinctrl-0 = <&i2c_2_pins>;
	        pinctrl-names = "default";
		compatible = "i2c-gpio";
		gpios = <&tlmm 56 0>, <&tlmm 55 0>;
		i2c-gpio,scl-open-drain;
		i2c-gpio,delay-us = <7>;
		#address-cells = <1>;
		#size-cells = <0>;
		status = "ok";
	};

	wifi3: wifi3@f00000 {
		qcom,wlan-ramdump-dynamic = <0x400000>;
		mhi,max-channels = <30>;
		mhi,timeout = <10000>;
		#address-cells = <1>;
		#size-cells = <0>;
		qrtr_node_id = <0x20>;
		qca,auto-restart;
#ifdef __IPQ_MEM_PROFILE_512_MB__

	      /* QCN9000 tgt-mem-mode=1 layout - 30MB
	       * +=========+==============+=========+
	       * |  Region | Start Offset |   Size  |
	       * +---------+--------------+---------+
	       * | BASE    |  0x4E400000  |   20MB  |
	       * +---------+--------------+---------+
	       * | M3 Dump |  0x4F800000  |   1MB   |
	       * +---------+--------------+---------+
	       * |  Caldb  |  0x4FA00000  |   8MB   |
	       * +==================================+
	       */

		base-addr = <0x4E400000>;
		m3-dump-addr = <0x4F800000>;
		qcom,caldb-addr = <0x4FA00000>;
#else

	      /* QCN9000 tgt-mem-mode=0 layout - 55MB
	       * +=========+==============+=========+
	       * |  Region | Start Offset |   Size  |
	       * +---------+--------------+---------+
	       * | BASE    |  0x50200000  |   45MB  |
	       * +---------+--------------+---------+
	       * | M3 Dump |  0x52F00000  |   1MB   |
	       * +---------+--------------+---------+
	       * |  Caldb  |  0x53100000  |   8MB   |
	       * +==================================+
	       */

		base-addr = <0x50200000>;
		m3-dump-addr = <0x52F00000>;
		qcom,caldb-addr = <0x53100000>;
#endif
		status = "ok";

		mhi_channels {
			mhi_chan@0 {
				reg = <0>;
				label = "LOOPBACK";
				mhi,num-elements = <32>;
				mhi,event-ring = <1>;
				mhi,chan-dir = <1>;
				mhi,data-type = <0>;
				mhi,doorbell-mode = <2>;
				mhi,ee = <0x14>;
			};

			mhi_chan@1 {
				reg = <1>;
				label = "LOOPBACK";
				mhi,num-elements = <32>;
				mhi,event-ring = <1>;
				mhi,chan-dir = <2>;
				mhi,data-type = <0>;
				mhi,doorbell-mode = <2>;
				mhi,ee = <0x14>;
			};

			mhi_chan@4 {
				reg = <4>;
				label = "DIAG";
				mhi,num-elements = <32>;
				mhi,event-ring = <1>;
				mhi,chan-dir = <1>;
				mhi,data-type = <0>;
				mhi,doorbell-mode = <2>;
				mhi,ee = <0x14>;
			};

			mhi_chan@5 {
				reg = <5>;
				label = "DIAG";
				mhi,num-elements = <32>;
				mhi,event-ring = <1>;
				mhi,chan-dir = <2>;
				mhi,data-type = <0>;
				mhi,doorbell-mode = <2>;
				mhi,ee = <0x14>;
			};

			mhi_chan@20 {
				reg = <20>;
				label = "IPCR";
				mhi,num-elements = <32>;
				mhi,event-ring = <1>;
				mhi,chan-dir = <1>;
				mhi,data-type = <1>;
				mhi,doorbell-mode = <2>;
				mhi,ee = <0x14>;
				mhi,auto-start;
			};

			mhi_chan@21 {
				reg = <21>;
				label = "IPCR";
				mhi,num-elements = <32>;
				mhi,event-ring = <1>;
				mhi,chan-dir = <2>;
				mhi,data-type = <0>;
				mhi,doorbell-mode = <2>;
				mhi,ee = <0x14>;
				mhi,auto-queue;
				mhi,auto-start;
			};
		};

		mhi_events {
			mhi_event@0 {
				mhi,num-elements = <32>;
				mhi,intmod = <1>;
				mhi,msi = <1>;
				mhi,priority = <1>;
				mhi,brstmode = <2>;
				mhi,data-type = <1>;
			};

			mhi_event@1 {
				mhi,num-elements = <256>;
				mhi,intmod = <1>;
				mhi,msi = <2>;
				mhi,priority = <1>;
				mhi,brstmode = <2>;
			};
		};

		mhi_devices {
			mhi_qrtr {
				mhi,chan = "IPCR";
				qcom,net-id = <0>;
			};
		};
	};
};

&blsp1_uart3 {
	pinctrl-0 = <&uart_pins>;
	pinctrl-names = "default";
	status = "ok";
};

&qpic_bam {
	status = "ok";
};

&ssphy_0 {
	status = "ok";
};

&qusb_phy_0 {
	status = "ok";
};

&qusb_phy_1 {
	status = "ok";
};

&usb2 {
	status = "ok";
};

&usb3 {
	status = "ok";
};

&nss_crypto {
	status = "ok";
};

&CPU0 {
	operating-points = <
		/* kHz   uV (fixed) */
		864000   1100000
		1056000  1100000
		1200000  1100000
		1608000  1100000
	>;
	clock-latency = <200000>;
};

&CPU1 {
	operating-points = <
		/* kHz   uV (fixed) */
		864000   1100000
		1056000  1100000
		1200000  1100000
		1608000  1100000
	>;
	clock-latency = <200000>;
};

&CPU2 {
	operating-points = <
		/* kHz   uV (fixed) */
		864000   1100000
		1056000  1100000
		1200000  1100000
		1608000  1100000
	>;
	clock-latency = <200000>;
};

&CPU3 {
	operating-points = <
		/* kHz   uV (fixed) */
		864000   1100000
		1056000  1100000
		1200000  1100000
		1608000  1100000
	>;
	clock-latency = <200000>;
};

&wifi0 {
        qcom,board_id = <0x10>;
        status = "ok";
};

&pcie0 {
	status = "ok";

	pcie0_rp {
		reg = <0 0 0 0 0>;
		status = "ok";

		mhi_0: qcom,mhi@0 {
			reg = <0 0 0 0 0 >;
			qrtr_instance_id = <0x20>;
			qcom,board_id = <0xa4>;
#if !defined(__IPQ_MEM_PROFILE_256_MB__)
			memory-region = <&mhi_region0>;
#endif
		};
	};
};

&pcie_phy {
        status = "ok";
};

&spi_0 {
	pinctrl-0 = <&spi_0_pins>;
	pinctrl-names = "default";
	cs-select = <0>;
	status = "ok";

	m25p80@0 {
		#address-cells = <1>;
		#size-cells = <1>;
		reg = <0>;
		compatible = "n25q128a11";
		linux,modalias = "m25p80", "n25q128a11";
		spi-max-frequency = <50000000>;
		use-default-sizes;
	};
};
