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