13271e610SSimon Arlott #ifndef __LINUX_BCM963XX_NVRAM_H__
23271e610SSimon Arlott #define __LINUX_BCM963XX_NVRAM_H__
33271e610SSimon Arlott 
43271e610SSimon Arlott #include <linux/crc32.h>
53271e610SSimon Arlott #include <linux/if_ether.h>
63271e610SSimon Arlott #include <linux/sizes.h>
73271e610SSimon Arlott #include <linux/types.h>
83271e610SSimon Arlott 
93271e610SSimon Arlott /*
103271e610SSimon Arlott  * Broadcom BCM963xx SoC board nvram data structure.
113271e610SSimon Arlott  *
123271e610SSimon Arlott  * The nvram structure varies in size depending on the SoC board version. Use
133271e610SSimon Arlott  * the appropriate minimum BCM963XX_NVRAM_*_SIZE define for the information
143271e610SSimon Arlott  * you need instead of sizeof(struct bcm963xx_nvram) as this may change.
153271e610SSimon Arlott  */
163271e610SSimon Arlott 
173271e610SSimon Arlott #define BCM963XX_NVRAM_V4_SIZE		300
183271e610SSimon Arlott #define BCM963XX_NVRAM_V5_SIZE		(1 * SZ_1K)
193271e610SSimon Arlott 
203271e610SSimon Arlott #define BCM963XX_DEFAULT_PSI_SIZE	64
213271e610SSimon Arlott 
223271e610SSimon Arlott enum bcm963xx_nvram_nand_part {
233271e610SSimon Arlott 	BCM963XX_NVRAM_NAND_PART_BOOT = 0,
243271e610SSimon Arlott 	BCM963XX_NVRAM_NAND_PART_ROOTFS_1,
253271e610SSimon Arlott 	BCM963XX_NVRAM_NAND_PART_ROOTFS_2,
263271e610SSimon Arlott 	BCM963XX_NVRAM_NAND_PART_DATA,
273271e610SSimon Arlott 	BCM963XX_NVRAM_NAND_PART_BBT,
283271e610SSimon Arlott 
293271e610SSimon Arlott 	__BCM963XX_NVRAM_NAND_NR_PARTS
303271e610SSimon Arlott };
313271e610SSimon Arlott 
323271e610SSimon Arlott struct bcm963xx_nvram {
333271e610SSimon Arlott 	u32	version;
343271e610SSimon Arlott 	char	bootline[256];
353271e610SSimon Arlott 	char	name[16];
363271e610SSimon Arlott 	u32	main_tp_number;
373271e610SSimon Arlott 	u32	psi_size;
383271e610SSimon Arlott 	u32	mac_addr_count;
393271e610SSimon Arlott 	u8	mac_addr_base[ETH_ALEN];
403271e610SSimon Arlott 	u8	__reserved1[2];
413271e610SSimon Arlott 	u32	checksum_v4;
423271e610SSimon Arlott 
433271e610SSimon Arlott 	u8	__reserved2[292];
443271e610SSimon Arlott 	u32	nand_part_offset[__BCM963XX_NVRAM_NAND_NR_PARTS];
453271e610SSimon Arlott 	u32	nand_part_size[__BCM963XX_NVRAM_NAND_NR_PARTS];
463271e610SSimon Arlott 	u8	__reserved3[388];
473271e610SSimon Arlott 	u32	checksum_v5;
483271e610SSimon Arlott };
493271e610SSimon Arlott 
503271e610SSimon Arlott #define BCM963XX_NVRAM_NAND_PART_OFFSET(nvram, part) \
513271e610SSimon Arlott 	bcm963xx_nvram_nand_part_offset(nvram, BCM963XX_NVRAM_NAND_PART_ ##part)
523271e610SSimon Arlott 
533271e610SSimon Arlott static inline u64 __pure bcm963xx_nvram_nand_part_offset(
543271e610SSimon Arlott 	const struct bcm963xx_nvram *nvram,
553271e610SSimon Arlott 	enum bcm963xx_nvram_nand_part part)
563271e610SSimon Arlott {
573271e610SSimon Arlott 	return nvram->nand_part_offset[part] * SZ_1K;
583271e610SSimon Arlott }
593271e610SSimon Arlott 
603271e610SSimon Arlott #define BCM963XX_NVRAM_NAND_PART_SIZE(nvram, part) \
613271e610SSimon Arlott 	bcm963xx_nvram_nand_part_size(nvram, BCM963XX_NVRAM_NAND_PART_ ##part)
623271e610SSimon Arlott 
633271e610SSimon Arlott static inline u64 __pure bcm963xx_nvram_nand_part_size(
643271e610SSimon Arlott 	const struct bcm963xx_nvram *nvram,
653271e610SSimon Arlott 	enum bcm963xx_nvram_nand_part part)
663271e610SSimon Arlott {
673271e610SSimon Arlott 	return nvram->nand_part_size[part] * SZ_1K;
683271e610SSimon Arlott }
693271e610SSimon Arlott 
703271e610SSimon Arlott /*
713271e610SSimon Arlott  * bcm963xx_nvram_checksum - Verify nvram checksum
723271e610SSimon Arlott  *
733271e610SSimon Arlott  * @nvram: pointer to full size nvram data structure
743271e610SSimon Arlott  * @expected_out: optional pointer to store expected checksum value
753271e610SSimon Arlott  * @actual_out: optional pointer to store actual checksum value
763271e610SSimon Arlott  *
773271e610SSimon Arlott  * Return: 0 if the checksum is valid, otherwise -EINVAL
783271e610SSimon Arlott  */
793271e610SSimon Arlott static int __maybe_unused bcm963xx_nvram_checksum(
803271e610SSimon Arlott 	const struct bcm963xx_nvram *nvram,
813271e610SSimon Arlott 	u32 *expected_out, u32 *actual_out)
823271e610SSimon Arlott {
833271e610SSimon Arlott 	u32 expected, actual;
843271e610SSimon Arlott 	size_t len;
853271e610SSimon Arlott 
863271e610SSimon Arlott 	if (nvram->version <= 4) {
873271e610SSimon Arlott 		expected = nvram->checksum_v4;
883271e610SSimon Arlott 		len = BCM963XX_NVRAM_V4_SIZE - sizeof(u32);
893271e610SSimon Arlott 	} else {
903271e610SSimon Arlott 		expected = nvram->checksum_v5;
913271e610SSimon Arlott 		len = BCM963XX_NVRAM_V5_SIZE - sizeof(u32);
923271e610SSimon Arlott 	}
933271e610SSimon Arlott 
943271e610SSimon Arlott 	/*
953271e610SSimon Arlott 	 * Calculate the CRC32 value for the nvram with a checksum value
963271e610SSimon Arlott 	 * of 0 without modifying or copying the nvram by combining:
973271e610SSimon Arlott 	 * - The CRC32 of the nvram without the checksum value
983271e610SSimon Arlott 	 * - The CRC32 of a zero checksum value (which is also 0)
993271e610SSimon Arlott 	 */
1003271e610SSimon Arlott 	actual = crc32_le_combine(
1013271e610SSimon Arlott 		crc32_le(~0, (u8 *)nvram, len), 0, sizeof(u32));
1023271e610SSimon Arlott 
1033271e610SSimon Arlott 	if (expected_out)
1043271e610SSimon Arlott 		*expected_out = expected;
1053271e610SSimon Arlott 
1063271e610SSimon Arlott 	if (actual_out)
1073271e610SSimon Arlott 		*actual_out = actual;
1083271e610SSimon Arlott 
1093271e610SSimon Arlott 	return expected == actual ? 0 : -EINVAL;
1103271e610SSimon Arlott };
1113271e610SSimon Arlott 
1123271e610SSimon Arlott #endif /* __LINUX_BCM963XX_NVRAM_H__ */
113