Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Copyright (C) 2019-2020 Linaro Limited */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "xhci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "xhci-trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "xhci-pci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define RENESAS_FW_VERSION				0x6C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define RENESAS_ROM_CONFIG				0xF0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define RENESAS_FW_STATUS				0xF4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define RENESAS_FW_STATUS_MSB				0xF5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define RENESAS_ROM_STATUS				0xF6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define RENESAS_ROM_STATUS_MSB				0xF7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define RENESAS_DATA0					0xF8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define RENESAS_DATA1					0xFC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define RENESAS_FW_VERSION_FIELD			GENMASK(23, 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define RENESAS_FW_VERSION_OFFSET			8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define RENESAS_FW_STATUS_DOWNLOAD_ENABLE		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define RENESAS_FW_STATUS_LOCK				BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define RENESAS_FW_STATUS_RESULT			GENMASK(6, 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)   #define RENESAS_FW_STATUS_INVALID			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)   #define RENESAS_FW_STATUS_SUCCESS			BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)   #define RENESAS_FW_STATUS_ERROR			BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define RENESAS_FW_STATUS_SET_DATA0			BIT(8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define RENESAS_FW_STATUS_SET_DATA1			BIT(9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define RENESAS_ROM_STATUS_ACCESS			BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define RENESAS_ROM_STATUS_ERASE			BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define RENESAS_ROM_STATUS_RELOAD			BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define RENESAS_ROM_STATUS_RESULT			GENMASK(6, 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)   #define RENESAS_ROM_STATUS_NO_RESULT			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)   #define RENESAS_ROM_STATUS_SUCCESS			BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)   #define RENESAS_ROM_STATUS_ERROR			BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define RENESAS_ROM_STATUS_SET_DATA0			BIT(8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define RENESAS_ROM_STATUS_SET_DATA1			BIT(9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define RENESAS_ROM_STATUS_ROM_EXISTS			BIT(15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define RENESAS_ROM_ERASE_MAGIC				0x5A65726F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define RENESAS_ROM_WRITE_MAGIC				0x53524F4D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define RENESAS_RETRY	10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define RENESAS_DELAY	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int renesas_fw_download_image(struct pci_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 				     const u32 *fw, size_t step, bool rom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8 fw_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	bool data0_or_data1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u32 status_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (rom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		status_reg = RENESAS_ROM_STATUS_MSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		status_reg = RENESAS_FW_STATUS_MSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 * The hardware does alternate between two 32-bit pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * (This is because each row of the firmware is 8 bytes).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	 * for even steps we use DATA0, for odd steps DATA1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	data0_or_data1 = (step & 1) == 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* step+1. Read "Set DATAX" and confirm it is cleared. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	for (i = 0; i < RENESAS_RETRY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		err = pci_read_config_byte(dev, status_reg, &fw_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			dev_err(&dev->dev, "Read Status failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				pcibios_err_to_errno(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (!(fw_status & BIT(data0_or_data1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		udelay(RENESAS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (i == RENESAS_RETRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		dev_err(&dev->dev, "Timeout for Set DATAX step: %zd\n", step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * step+2. Write FW data to "DATAX".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * "LSB is left" => force little endian
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	err = pci_write_config_dword(dev, data0_or_data1 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 				     RENESAS_DATA1 : RENESAS_DATA0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				     (__force u32)cpu_to_le32(fw[step]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		dev_err(&dev->dev, "Write to DATAX failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			pcibios_err_to_errno(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	udelay(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* step+3. Set "Set DATAX". */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	err = pci_write_config_byte(dev, status_reg, BIT(data0_or_data1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		dev_err(&dev->dev, "Write config for DATAX failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			pcibios_err_to_errno(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int renesas_fw_verify(const void *fw_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			     size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	u16 fw_version_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	u16 fw_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 * The Firmware's Data Format is describe in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 * "6.3 Data Format" R19UH0078EJ0500 Rev.5.00 page 124
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * The bootrom chips of the big brother have sizes up to 64k, let's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * assume that's the biggest the firmware can get.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (length < 0x1000 || length >= 0x10000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		pr_err("firmware is size %zd is not (4k - 64k).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* The First 2 bytes are fixed value (55aa). "LSB on Left" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (get_unaligned_le16(fw_data) != 0x55aa) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		pr_err("no valid firmware header found.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	/* verify the firmware version position and print it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	fw_version_pointer = get_unaligned_le16(fw_data + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (fw_version_pointer + 2 >= length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		pr_err("fw ver pointer is outside of the firmware image");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	fw_version = get_unaligned_le16(fw_data + fw_version_pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	pr_err("got firmware version: %02x.", fw_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static bool renesas_check_rom(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	u16 rom_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	/* Check if external ROM exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	retval = pci_read_config_word(pdev, RENESAS_ROM_STATUS, &rom_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	rom_status &= RENESAS_ROM_STATUS_ROM_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (rom_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		dev_dbg(&pdev->dev, "External ROM exists\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return true; /* External ROM exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int renesas_check_rom_state(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	u16 rom_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	u32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/* check FW version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	err = pci_read_config_dword(pdev, RENESAS_FW_VERSION, &version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	version &= RENESAS_FW_VERSION_FIELD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	version = version >> RENESAS_FW_VERSION_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	dev_dbg(&pdev->dev, "Found ROM version: %x\n", version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 * Test if ROM is present and loaded, if so we can skip everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	err = pci_read_config_word(pdev, RENESAS_ROM_STATUS, &rom_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (rom_state & BIT(15)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		/* ROM exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		dev_dbg(&pdev->dev, "ROM exists\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		/* Check the "Result Code" Bits (6:4) and act accordingly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		switch (rom_state & RENESAS_ROM_STATUS_RESULT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		case RENESAS_ROM_STATUS_SUCCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		case RENESAS_ROM_STATUS_NO_RESULT: /* No result yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			dev_dbg(&pdev->dev, "Unknown ROM status ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		case RENESAS_ROM_STATUS_ERROR: /* Error State */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		default: /* All other states are marked as "Reserved states" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			dev_err(&pdev->dev, "Invalid ROM..");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int renesas_fw_check_running(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	u8 fw_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 * Test if the device is actually needing the firmware. As most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 * BIOSes will initialize the device for us. If the device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	 * initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 * Check if "FW Download Lock" is locked. If it is and the FW is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	 * ready we can simply continue. If the FW is not ready, we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	 * to give up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (fw_state & RENESAS_FW_STATUS_LOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		dev_dbg(&pdev->dev, "FW Download Lock is engaged.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (fw_state & RENESAS_FW_STATUS_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			"FW Download Lock is set and FW is not ready. Giving Up.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	 * Check if "FW Download Enable" is set. If someone (us?) tampered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	 * with it and it can't be reset, we have to give up too... and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 * ask for a forgiveness and a reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (fw_state & RENESAS_FW_STATUS_DOWNLOAD_ENABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			"FW Download Enable is stale. Giving Up (poweroff/reboot needed).");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	/* Otherwise, Check the "Result Code" Bits (6:4) and act accordingly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	switch (fw_state & RENESAS_FW_STATUS_RESULT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	case 0: /* No result yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		dev_dbg(&pdev->dev, "FW is not ready/loaded yet.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		/* tell the caller, that this device needs the firmware. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	case RENESAS_FW_STATUS_SUCCESS: /* Success, device should be working. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		dev_dbg(&pdev->dev, "FW is ready.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	case RENESAS_FW_STATUS_ERROR: /* Error State */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			"hardware is in an error state. Giving up (poweroff/reboot needed).");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	default: /* All other states are marked as "Reserved states" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			"hardware is in an invalid state %lx. Giving up (poweroff/reboot needed).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			(fw_state & RENESAS_FW_STATUS_RESULT) >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int renesas_fw_download(struct pci_dev *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			       const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	const u32 *fw_data = (const u32 *)fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	u8 fw_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	 * For more information and the big picture: please look at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 * "Firmware Download Sequence" in "7.1 FW Download Interface"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	 * of R19UH0078EJ0500 Rev.5.00 page 131
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * 0. Set "FW Download Enable" bit in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 * "FW Download Control & Status Register" at 0xF4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	err = pci_write_config_byte(pdev, RENESAS_FW_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				    RENESAS_FW_STATUS_DOWNLOAD_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	/* 1 - 10 follow one step after the other. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	for (i = 0; i < fw->size / 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		err = renesas_fw_download_image(pdev, fw_data, i, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				"Firmware Download Step %zd failed at position %zd bytes with (%d).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 				i, i * 4, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	 * This sequence continues until the last data is written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	 * "DATA0" or "DATA1". Naturally, we wait until "SET DATA0/1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	 * is cleared by the hardware beforehand.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	for (i = 0; i < RENESAS_RETRY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		err = pci_read_config_byte(pdev, RENESAS_FW_STATUS_MSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 					   &fw_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		if (!(fw_status & (BIT(0) | BIT(1))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		udelay(RENESAS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (i == RENESAS_RETRY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		dev_warn(&pdev->dev, "Final Firmware Download step timed out.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 * 11. After finishing writing the last data of FW, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	 * System Software must clear "FW Download Enable"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	err = pci_write_config_byte(pdev, RENESAS_FW_STATUS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* 12. Read "Result Code" and confirm it is good. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	for (i = 0; i < RENESAS_RETRY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		err = pci_read_config_byte(pdev, RENESAS_FW_STATUS, &fw_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			return pcibios_err_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		if (fw_status & RENESAS_FW_STATUS_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		udelay(RENESAS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (i == RENESAS_RETRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		/* Timed out / Error - let's see if we can fix this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		err = renesas_fw_check_running(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		case 0: /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			 * we shouldn't end up here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			 * maybe it took a little bit longer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			 * But all should be well?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		case 1: /* (No result yet! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			dev_err(&pdev->dev, "FW Load timedout");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static void renesas_rom_erase(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int retval, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	dev_dbg(&pdev->dev, "Performing ROM Erase...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	retval = pci_write_config_dword(pdev, RENESAS_DATA0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 					RENESAS_ROM_ERASE_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		dev_err(&pdev->dev, "ROM erase, magic word write failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			pcibios_err_to_errno(retval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		dev_err(&pdev->dev, "ROM status read failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			pcibios_err_to_errno(retval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	status |= RENESAS_ROM_STATUS_ERASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	retval = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		dev_err(&pdev->dev, "ROM erase set word write failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	/* sleep a bit while ROM is erased */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	for (i = 0; i < RENESAS_RETRY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		retval = pci_read_config_byte(pdev, RENESAS_ROM_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 					      &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		status &= RENESAS_ROM_STATUS_ERASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		mdelay(RENESAS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (i == RENESAS_RETRY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		dev_dbg(&pdev->dev, "Chip erase timedout: %x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	dev_dbg(&pdev->dev, "ROM Erase... Done success\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static bool renesas_setup_rom(struct pci_dev *pdev, const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	const u32 *fw_data = (const u32 *)fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	/* 2. Write magic word to Data0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	err = pci_write_config_dword(pdev, RENESAS_DATA0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 				     RENESAS_ROM_WRITE_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	/* 3. Set External ROM access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 				    RENESAS_ROM_STATUS_ACCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		goto remove_bypass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	/* 4. Check the result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		goto remove_bypass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	status &= GENMASK(6, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			"setting external rom failed: %x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		goto remove_bypass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	/* 5 to 16 Write FW to DATA0/1 while checking SetData0/1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	for (i = 0; i < fw->size / 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		err = renesas_fw_download_image(pdev, fw_data, i, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				"ROM Download Step %d failed at position %d bytes with (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 				 i, i * 4, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			goto remove_bypass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	 * wait till DATA0/1 is cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	for (i = 0; i < RENESAS_RETRY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS_MSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 					   &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			goto remove_bypass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		if (!(status & (BIT(0) | BIT(1))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		udelay(RENESAS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (i == RENESAS_RETRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		dev_err(&pdev->dev, "Final Firmware ROM Download step timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		goto remove_bypass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	/* 17. Remove bypass */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	udelay(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	/* 18. check result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	for (i = 0; i < RENESAS_RETRY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			dev_err(&pdev->dev, "Read ROM status failed:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 				pcibios_err_to_errno(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		status &= RENESAS_ROM_STATUS_RESULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		if (status ==  RENESAS_ROM_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			dev_dbg(&pdev->dev, "Download ROM success\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		udelay(RENESAS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (i == RENESAS_RETRY) { /* Timed out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			"Download to external ROM TO: %x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	dev_dbg(&pdev->dev, "Download to external ROM succeeded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	/* Last step set Reload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	err = pci_write_config_byte(pdev, RENESAS_ROM_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 				    RENESAS_ROM_STATUS_RELOAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		dev_err(&pdev->dev, "Set ROM execute failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			pcibios_err_to_errno(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	 * wait till Reload is cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	for (i = 0; i < RENESAS_RETRY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		err = pci_read_config_byte(pdev, RENESAS_ROM_STATUS, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		if (!(status & RENESAS_ROM_STATUS_RELOAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		udelay(RENESAS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (i == RENESAS_RETRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		dev_err(&pdev->dev, "ROM Exec timed out: %x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) remove_bypass:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	pci_write_config_byte(pdev, RENESAS_ROM_STATUS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static int renesas_load_fw(struct pci_dev *pdev, const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	bool rom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	/* Check if the device has external ROM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	rom = renesas_check_rom(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (rom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		/* perform chip erase first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		renesas_rom_erase(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		/* lets try loading fw on ROM first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		rom = renesas_setup_rom(pdev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		if (!rom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			dev_dbg(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 				"ROM load failed, falling back on FW load\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			dev_dbg(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 				"ROM load success\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	err = renesas_fw_download(pdev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		dev_err(&pdev->dev, "firmware failed to download (%d).", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) int renesas_xhci_check_request_fw(struct pci_dev *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 				  const struct pci_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	struct xhci_driver_data *driver_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			(struct xhci_driver_data *)id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	const char *fw_name = driver_data->firmware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	const struct firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	bool has_rom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	/* Check if device has ROM and loaded, if so skip everything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	has_rom = renesas_check_rom(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	if (has_rom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		err = renesas_check_rom_state(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		else if (err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 			has_rom = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	err = renesas_fw_check_running(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	/* Continue ahead, if the firmware is already running. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	/* no firmware interface available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	if (err != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		return has_rom ? 0 : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	pci_dev_get(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	err = firmware_request_nowarn(&fw, fw_name, &pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	pci_dev_put(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		if (has_rom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			dev_info(&pdev->dev, "failed to load firmware %s, fallback to ROM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 				 fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		dev_err(&pdev->dev, "failed to load firmware %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			fw_name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	err = renesas_fw_verify(fw->data, fw->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	err = renesas_load_fw(pdev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) EXPORT_SYMBOL_GPL(renesas_xhci_check_request_fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) void renesas_xhci_pci_exit(struct pci_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) EXPORT_SYMBOL_GPL(renesas_xhci_pci_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) MODULE_LICENSE("GPL v2");