From a9493b5a554849bb7d81c37aec34a1dc2456373b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Iv=C3=A1n=20=C3=81valos?= <avalos@disroot.org>
Date: Wed, 13 Jul 2022 02:52:15 -0500
Subject: [PATCH] [wip] uboot-pinebookpro failing build

---
 .../0001-PBP-Fix-panel-reset.patch            | 63 ++++++++++++++
 ...Correct-boot-order-to-be-USB-SD-eMMC.patch | 28 ++++++
 ...Turn-power-and-standby-LEDs-on-early.patch | 48 ++++++++++
 ...mc-sdhci-allow-disabling-sdma-in-spl.patch | 67 ++++++++++++++
 ...dts-Work-around-daughterboard-issues.patch | 35 ++++++++
 libre/uboot-pinebookpro/PKGBUILD              | 87 +++++++++++++++++++
 .../uboot-pinebookpro.install                 | 41 +++++++++
 7 files changed, 369 insertions(+)
 create mode 100644 libre/uboot-pinebookpro/0001-PBP-Fix-panel-reset.patch
 create mode 100644 libre/uboot-pinebookpro/0002-Correct-boot-order-to-be-USB-SD-eMMC.patch
 create mode 100644 libre/uboot-pinebookpro/0003-Turn-power-and-standby-LEDs-on-early.patch
 create mode 100644 libre/uboot-pinebookpro/0004-mmc-sdhci-allow-disabling-sdma-in-spl.patch
 create mode 100644 libre/uboot-pinebookpro/0005-arm-dts-Work-around-daughterboard-issues.patch
 create mode 100644 libre/uboot-pinebookpro/PKGBUILD
 create mode 100644 libre/uboot-pinebookpro/uboot-pinebookpro.install

diff --git a/libre/uboot-pinebookpro/0001-PBP-Fix-panel-reset.patch b/libre/uboot-pinebookpro/0001-PBP-Fix-panel-reset.patch
new file mode 100644
index 000000000..697599654
--- /dev/null
+++ b/libre/uboot-pinebookpro/0001-PBP-Fix-panel-reset.patch
@@ -0,0 +1,63 @@
+From 352cb7b28bf4a16330f148043e8d10b0141bbfcb Mon Sep 17 00:00:00 2001
+From: Arnaud Patard <arnaud.patard@rtp-net.org>
+Date: Wed, 8 Jul 2020 21:43:36 -0400
+Subject: [PATCH 4/4] PBP: Fix panel reset
+
+On warm reset, the pinebook pro panel is not working correctly.
+The issue is not yet debugged so, for now, this hack seems to be
+enough. It toggles the GPIO1_C6 gpio [ LCDVCC_EN signal in the
+schematics ] used by the vcc3v3_panel regulator.
+
+There's no gpio_request, since the gpio is already in use at this
+stage, so it can only fail.
+
+Origin: http://people.hupstream.com/~rtp/pbp/20200706/patches/hack-reset.patch
+---
+ board/pine64/pinebook-pro-rk3399/pinebook-pro-rk3399.c | 10 +++++++++-
+ 1 file changed, 9 insertions(+), 1 deletion(-)
+
+diff --git a/board/pine64/pinebook-pro-rk3399/pinebook-pro-rk3399.c b/board/pine64/pinebook-pro-rk3399/pinebook-pro-rk3399.c
+index 516292aaa59..ff9c916bcb7 100644
+--- a/board/pine64/pinebook-pro-rk3399/pinebook-pro-rk3399.c
++++ b/board/pine64/pinebook-pro-rk3399/pinebook-pro-rk3399.c
+@@ -7,13 +7,15 @@
+ #include <common.h>
+ #include <dm.h>
+ #include <syscon.h>
++#include <linux/delay.h>
++#include <asm/gpio.h>
+ #include <asm/io.h>
+ #include <asm/arch-rockchip/clock.h>
+ #include <asm/arch-rockchip/grf_rk3399.h>
++#include <asm/arch-rockchip/gpio.h>
+ #include <asm/arch-rockchip/hardware.h>
+ #include <asm/arch-rockchip/misc.h>
+ #include <power/regulator.h>
+-
+ #define GRF_IO_VSEL_BT565_SHIFT 0
+ #define PMUGRF_CON0_VSEL_SHIFT 8
+ 
+@@ -59,6 +61,7 @@ int misc_init_r(void)
+ 	const u32 cpuid_length = 0x10;
+ 	u8 cpuid[cpuid_length];
+ 	int ret;
++	unsigned int gpio;
+ 
+ 	setup_iodomain();
+ 
+@@ -70,6 +73,11 @@ int misc_init_r(void)
+ 	if (ret)
+ 		return ret;
+ 
++	gpio_lookup_name("B22", NULL, NULL, &gpio);
++	gpio_direction_output(gpio, 0);
++	udelay(500000);
++	gpio_direction_output(gpio, 1);
++
+ 	return ret;
+ }
+ #endif
+-- 
+2.25.4
+
+
diff --git a/libre/uboot-pinebookpro/0002-Correct-boot-order-to-be-USB-SD-eMMC.patch b/libre/uboot-pinebookpro/0002-Correct-boot-order-to-be-USB-SD-eMMC.patch
new file mode 100644
index 000000000..5b218b53c
--- /dev/null
+++ b/libre/uboot-pinebookpro/0002-Correct-boot-order-to-be-USB-SD-eMMC.patch
@@ -0,0 +1,28 @@
+From f97401137daa1cb75532c373bbcb5011f1e03585 Mon Sep 17 00:00:00 2001
+From: Dan Johansen <strit@manjaro.org>
+Date: Sun, 10 Oct 2021 20:19:02 +0200
+Subject: [PATCH] Correct boot order to be USB -> SD -> eMMC
+
+Signed-off-by: Dan Johansen <strit@manjaro.org>
+---
+ include/configs/rockchip-common.h | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/include/configs/rockchip-common.h b/include/configs/rockchip-common.h
+index ba7061a287..5dc92373b2 100644
+--- a/include/configs/rockchip-common.h
++++ b/include/configs/rockchip-common.h
+@@ -55,9 +55,9 @@
+ 
+ #ifdef CONFIG_ROCKCHIP_RK3399
+ #define BOOT_TARGET_DEVICES(func) \
++	BOOT_TARGET_USB(func) \
+ 	BOOT_TARGET_MMC(func) \
+ 	BOOT_TARGET_NVME(func) \
+-	BOOT_TARGET_USB(func) \
+ 	BOOT_TARGET_PXE(func) \
+ 	BOOT_TARGET_DHCP(func) \
+ 	BOOT_TARGET_SF(func)
+-- 
+2.33.0
+
diff --git a/libre/uboot-pinebookpro/0003-Turn-power-and-standby-LEDs-on-early.patch b/libre/uboot-pinebookpro/0003-Turn-power-and-standby-LEDs-on-early.patch
new file mode 100644
index 000000000..34ab50665
--- /dev/null
+++ b/libre/uboot-pinebookpro/0003-Turn-power-and-standby-LEDs-on-early.patch
@@ -0,0 +1,48 @@
+From 354d4f9072b5c41e91d70d1569ba364603889868 Mon Sep 17 00:00:00 2001
+From: Dragan Simic <dragan.simic@gmail.com>
+Date: Fri, 22 Apr 2022 03:51:56 +0200
+Subject: [PATCH] Turn power and standby LEDs on early
+
+Temporary (and ugly) hack to turn the Pinebook Pro's RGB status
+LED on as early as possible during boot.
+
+Based on a patch created by dhivael <dhivael.git@eno.space>.
+---
+ arch/arm/mach-rockchip/rk3399/rk3399.c | 14 ++++++++++++++
+ 1 file changed, 14 insertions(+)
+
+diff --git a/arch/arm/mach-rockchip/rk3399/rk3399.c b/arch/arm/mach-rockchip/rk3399/rk3399.c
+index 01a05599cd..58b5cd1ce7 100644
+--- a/arch/arm/mach-rockchip/rk3399/rk3399.c
++++ b/arch/arm/mach-rockchip/rk3399/rk3399.c
+@@ -20,6 +20,7 @@
+ #include <asm/arch-rockchip/hardware.h>
+ #include <linux/bitops.h>
+ #include <power/regulator.h>
++#include <dt-bindings/pinctrl/rockchip.h>
+ 
+ DECLARE_GLOBAL_DATA_PTR;
+ 
+@@ -157,6 +158,19 @@ void board_debug_uart_init(void)
+ 				  GPIO_PULL_NORMAL);
+ 	}
+ 
++	{
++		/*
++		 * Temporary (and ugly) hack to turn the Pinebook Pro's
++		 * RGB status LED on as early as possible during boot:
++		 * - Set GPIO0_A2/B3 to GPIO_ACTIVE_HIGH
++		 * - Set GPIO0_A2/B3 to OUTPUT
++		 */
++		int mask = (1UL << RK_PA2) | (1UL << RK_PB3);
++
++		setbits_le32(&gpio->swport_dr, mask);
++		setbits_le32(&gpio->swport_ddr, mask);
++	}
++
+ 	/* Enable early UART2 channel C on the RK3399 */
+ 	rk_clrsetreg(&grf->gpio4c_iomux,
+ 		     GRF_GPIO4C3_SEL_MASK,
+-- 
+2.33.1
+
diff --git a/libre/uboot-pinebookpro/0004-mmc-sdhci-allow-disabling-sdma-in-spl.patch b/libre/uboot-pinebookpro/0004-mmc-sdhci-allow-disabling-sdma-in-spl.patch
new file mode 100644
index 000000000..ef6d9b44c
--- /dev/null
+++ b/libre/uboot-pinebookpro/0004-mmc-sdhci-allow-disabling-sdma-in-spl.patch
@@ -0,0 +1,67 @@
+From: Peter Geis <pgwipeout@gmail.com>
+To: Peng Fan <peng.fan@nxp.com>, Jaehoon Chung <jh80.chung@samsung.com>
+Cc: Peter Geis <pgwipeout@gmail.com>, u-boot@lists.denx.de
+Subject: [PATCH v1 02/11] mmc: sdhci: allow disabling sdma in spl
+Date: Mon, 21 Feb 2022 20:31:21 -0500
+
+Rockchip emmc devices have a similar issue to Rockchip dwmmc devices,
+where performing dma to sram causes errors with suspend/resume.
+Allow us to toggle sdma in spl for sdhci similar to adma support, so we
+can ensure dma is not used when loading the sram code.
+
+Signed-off-by: Peter Geis <pgwipeout@gmail.com>
+Reviewed-by: Jaehoon Chung <jh80.chung@samsung.com>
+---
+ drivers/mmc/Kconfig | 7 +++++++
+ drivers/mmc/sdhci.c | 6 +++---
+ 2 files changed, 10 insertions(+), 3 deletions(-)
+
+diff --git a/drivers/mmc/Kconfig b/drivers/mmc/Kconfig
+index f04cc44e1973..1e4342285ce7 100644
+--- a/drivers/mmc/Kconfig
++++ b/drivers/mmc/Kconfig
+@@ -468,6 +468,13 @@ config MMC_SDHCI_SDMA
+ 	  This enables support for the SDMA (Single Operation DMA) defined
+ 	  in the SD Host Controller Standard Specification Version 1.00 .
+ 
++config SPL_MMC_SDHCI_SDMA
++	bool "Support SDHCI SDMA in SPL"
++	depends on MMC_SDHCI
++	help
++	  This enables support for the SDMA (Single Operation DMA) defined
++	  in the SD Host Controller Standard Specification Version 1.00 in SPL.
++
+ config MMC_SDHCI_ADMA
+ 	bool "Support SDHCI ADMA2"
+ 	depends on MMC_SDHCI
+diff --git a/drivers/mmc/sdhci.c b/drivers/mmc/sdhci.c
+index 766e4a6b0c5e..6285e53d12a2 100644
+--- a/drivers/mmc/sdhci.c
++++ b/drivers/mmc/sdhci.c
+@@ -70,7 +70,7 @@ static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
+ 	}
+ }
+ 
+-#if (defined(CONFIG_MMC_SDHCI_SDMA) || CONFIG_IS_ENABLED(MMC_SDHCI_ADMA))
++#if (CONFIG_IS_ENABLED(MMC_SDHCI_SDMA) || CONFIG_IS_ENABLED(MMC_SDHCI_ADMA))
+ static void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data,
+ 			      int *is_aligned, int trans_bytes)
+ {
+@@ -177,7 +177,7 @@ static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data)
+ 		}
+ 	} while (!(stat & SDHCI_INT_DATA_END));
+ 
+-#if (defined(CONFIG_MMC_SDHCI_SDMA) || CONFIG_IS_ENABLED(MMC_SDHCI_ADMA))
++#if (CONFIG_IS_ENABLED(MMC_SDHCI_SDMA) || CONFIG_IS_ENABLED(MMC_SDHCI_ADMA))
+ 	dma_unmap_single(host->start_addr, data->blocks * data->blocksize,
+ 			 mmc_get_dma_dir(data));
+ #endif
+@@ -836,7 +836,7 @@ int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host,
+ #endif
+ 	debug("%s, caps: 0x%x\n", __func__, caps);
+ 
+-#ifdef CONFIG_MMC_SDHCI_SDMA
++#if CONFIG_IS_ENABLED(MMC_SDHCI_SDMA)
+ 	if ((caps & SDHCI_CAN_DO_SDMA)) {
+ 		host->flags |= USE_SDMA;
+ 	} else {
diff --git a/libre/uboot-pinebookpro/0005-arm-dts-Work-around-daughterboard-issues.patch b/libre/uboot-pinebookpro/0005-arm-dts-Work-around-daughterboard-issues.patch
new file mode 100644
index 000000000..837e5896a
--- /dev/null
+++ b/libre/uboot-pinebookpro/0005-arm-dts-Work-around-daughterboard-issues.patch
@@ -0,0 +1,35 @@
+From 30cd68f2fc79b37bf0e416d026bfa06b0de921b8 Mon Sep 17 00:00:00 2001
+From: Dragan Simic <dragan.simic@gmail.com>
+Date: Mon, 16 May 2022 03:46:44 +0200
+Subject: [PATCH] arm: dts: Work around daughterboard issues
+
+MicroSD card slot in the Pinebook Pro is located on a separate
+daughterboard that's connected to the mainboard using a rather
+long flat cable.  The resulting signal degradation causes many
+perfectly fine microSD cards not to work in the Pinebook Pro,
+which is a common source of frustration among the owners.
+
+Changing the mode and lowering the speed reportedly fixes this
+issue and makes many microSD cards work as expected.
+
+Tested-by: JR Gonzalez <jrg@scientiam.org>
+---
+ arch/arm64/boot/dts/rockchip/rk3399-pinebook-pro.dts | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/arch/arm/dts/rk3399-pinebook-pro.dts b/arch/arm/dts/rk3399-pinebook-pro.dts
+index c2f021a1a18f..5894cb6bd0a8 100644
+--- a/arch/arm/dts/rk3399-pinebook-pro.dts
++++ b/arch/arm/dts/rk3399-pinebook-pro.dts
+@@ -945,7 +945,7 @@ &sdmmc {
+ 	disable-wp;
+ 	pinctrl-names = "default";
+ 	pinctrl-0 = <&sdmmc_clk &sdmmc_cmd &sdmmc_bus4>;
+-	sd-uhs-sdr104;
++	sd-uhs-sdr50;
+ 	vmmc-supply = <&vcc3v0_sd>;
+ 	vqmmc-supply = <&vcc_sdio>;
+ 	status = "okay";
+-- 
+2.33.1
+
diff --git a/libre/uboot-pinebookpro/PKGBUILD b/libre/uboot-pinebookpro/PKGBUILD
new file mode 100644
index 000000000..9f220b927
--- /dev/null
+++ b/libre/uboot-pinebookpro/PKGBUILD
@@ -0,0 +1,87 @@
+# U-Boot: Pinebook Pro based on PKGBUILD for Rock64
+# Maintainer: Sven Kiljan <sven dot archlinuxarm dash pbp at kiljan dot org>
+# Contributor: Dan Johansen <strit@manjaro.org>
+# Contributor: Kevin Mihelich 
+# Contributor: Adam <adam900710@gmail.com>
+# Contributor: Dragan Simic <dsimic@buserror.io>
+
+pkgname=uboot-pinebookpro
+pkgver=2022.04
+pkgrel=3
+_tfaver=2.6
+pkgdesc="U-Boot for Pine64 Pinebook Pro"
+arch=('armv7h')
+url='http://www.denx.de/wiki/U-Boot/WebHome'
+license=('GPL')
+makedepends=('git' 'arm-none-eabi-gcc' 'dtc' 'bc')
+provides=('uboot')
+conflicts=('uboot')
+replaces=('uboot-pinebookpro-bsp')
+install=${pkgname}.install
+source=("ftp://ftp.denx.de/pub/u-boot/u-boot-${pkgver/rc/-rc}.tar.bz2"
+        "https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/snapshot/trusted-firmware-a-${_tfaver}.tar.gz"
+        "0001-PBP-Fix-panel-reset.patch"
+        "0002-Correct-boot-order-to-be-USB-SD-eMMC.patch"
+        "0003-Turn-power-and-standby-LEDs-on-early.patch"
+        "0004-mmc-sdhci-allow-disabling-sdma-in-spl.patch"       # From list: https://patchwork.ozlabs.org/project/uboot/patch/20220222013131.3114990-3-pgwipeout@gmail.com/
+        "0005-arm-dts-Work-around-daughterboard-issues.patch")   # Will be upstreamed by the author
+sha256sums=('68e065413926778e276ec3abd28bb32fa82abaa4a6898d570c1f48fbdb08bcd0'
+            '4e59f02ccb042d5d18c89c849701b96e6cf4b788709564405354b5d313d173f7'
+            'c3ea09a18b766a3ce0728234b097b29e2ed610c7f04b138b7fba42e118a7ae33'
+            '017d33aac55f8a5ed22170c97b4792ba755a4dad04f6c0cdd85119bbc81e87b3'
+            'e82b207c56f41c0cc7b85f9057936816ea2ed58533e2dd2f6d665e28c415be60'
+            '7014c3f1ada93536787a4ce30b484dfe651c339391bd46869c61933825a0edcc'
+            'ff082f1bfc8666ca372d5e43924c47e60df637f5d2722b47a19b30a1306e8c74')
+
+export CROSS_COMPILE=arm-none-eabi-
+
+prepare() {
+  cd u-boot-${pkgver/rc/-rc}
+
+  patch -N -p1 -i "${srcdir}/0001-PBP-Fix-panel-reset.patch"                         # Panel reset fix
+  patch -N -p1 -i "${srcdir}/0002-Correct-boot-order-to-be-USB-SD-eMMC.patch"        # Boot from USB first
+  patch -N -p1 -i "${srcdir}/0003-Turn-power-and-standby-LEDs-on-early.patch"        # Orange status LED
+  patch -N -p1 -i "${srcdir}/0004-mmc-sdhci-allow-disabling-sdma-in-spl.patch"       # RK3399 suspend/resume
+  patch -N -p1 -i "${srcdir}/0005-arm-dts-Work-around-daughterboard-issues.patch"    # MicroSD issues
+}
+
+build() {
+  # Avoid build warnings by editing a .config option in place instead of
+  # appending an option to .config, if an option is already present
+  update_config() {
+    if ! grep -q "^$1=$2$" .config; then
+      if grep -q "^# $1 is not set$" .config; then
+        sed -i -e "s/^# $1 is not set$/$1=$2/g" .config
+      elif grep -q "^$1=" .config; then
+        sed -i -e "s/^$1=.*/$1=$2/g" .config
+      else
+        echo "$1=$2" >> .config
+      fi
+    fi
+  }
+
+  unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
+
+  cd trusted-firmware-a-${_tfaver}
+
+  echo -e "\nBuilding TF-A for Pine64 Pinebook Pro...\n"
+  make PLAT=rk3399
+  cp build/rk3399/release/bl31/bl31.elf ../u-boot-${pkgver/rc/-rc}/
+
+  cd ../u-boot-${pkgver/rc/-rc}
+
+  echo -e "\nBuilding U-Boot for Pine64 Pinebook Pro...\n"
+  make pinebook-pro-rk3399_defconfig
+
+  update_config 'CONFIG_IDENT_STRING' '" Parabola"'
+  update_config 'CONFIG_SPL_MMC_SDHCI_SDMA' 'n'
+
+  make EXTRAVERSION=-${pkgrel}
+}
+
+package() {
+  cd u-boot-${pkgver/rc/-rc}
+
+  mkdir -p "${pkgdir}/boot/extlinux"
+  install -D -m 0644 idbloader.img u-boot.itb -t "${pkgdir}/boot"
+}
diff --git a/libre/uboot-pinebookpro/uboot-pinebookpro.install b/libre/uboot-pinebookpro/uboot-pinebookpro.install
new file mode 100644
index 000000000..2d34d7b44
--- /dev/null
+++ b/libre/uboot-pinebookpro/uboot-pinebookpro.install
@@ -0,0 +1,41 @@
+notice_uboot() {
+  echo "New version of U-Boot firmware can be flashed to your microSD card (mmcblk1)"
+  echo "or eMMC module (mmcblk2).  You can do that by running:"
+  echo "# dd if=/boot/idbloader.img of=/dev/mmcblkX seek=64 conv=notrunc,fsync"
+  echo "# dd if=/boot/u-boot.itb of=/dev/mmcblkX seek=16384 conv=notrunc,fsync"
+}
+
+create_config() {
+  mkdir -p /boot/extlinux
+  echo "timeout 1
+DEFAULT linux-libre-64
+LABEL Parabola
+KERNEL /boot/vmlinuz-linux-libre-64
+FDTDIR /boot/dtbs/linux-libre-64
+APPEND initrd=/initramfs-linux-libre-64.img console=ttyS2,1500000 root=LABEL=PARABOLA rw rootwait audit=0 splash plymouth.ignore-serial-consoles" \
+    > /boot/extlinux/extlinux.conf
+}
+
+post_install() {
+  notice_uboot
+  if [ -f /boot/extlinux/extlinux.conf.pacsave ]; then
+    mv /boot/extlinux/extlinux.conf.pacsave /boot/extlinux/extlinux.conf
+  fi
+  if [ -f /boot/extlinux/extlinux.conf ]; then
+    echo "Keeping old extlinux.conf file..."
+  else
+    create_config
+  fi
+}
+
+post_upgrade() {
+  notice_uboot
+  if [ -f /boot/extlinux/extlinux.conf.pacsave ]; then
+    mv /boot/extlinux/extlinux.conf.pacsave /boot/extlinux/extlinux.conf
+  fi
+  if [ -f /boot/extlinux/extlinux.conf ]; then
+    echo "Keeping old extlinux.conf file..."
+  else
+    create_config
+  fi
+}
-- 
2.37.0

