#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../../PerlLib";
use BRCM::GenConfig;

# $p will allow us to GET values from the PROFILE
# $c will allow us to SET (and GET and DRIVER_SETUP) on the config
# file as we transform it from a template to the final config

# arguments
# * profile file
# * config  file
my $p    = new BRCM::GenConfig(shift);
my $chip = $p->get('BRCM_CHIP');
my $arch = $p->get('PROFILE_ARCH');

my $c = new BRCM::GenConfig( shift, Chip => $chip, Profile => $p );

############################################################
#          KERNEL FEATURES CONFIGURATION                   #
############################################################

if ( $p->get('BCM_KERNEL_CMDLINE_BOOL') ) {
    $c->set( 'CONFIG_CMDLINE', $p->get('BCM_KERNEL_CMDLINE') );
    if ( $p->get('BCM_KERNEL_CMDLINE_OVERRIDE') ) {
        if ( $arch eq 'mips' ) {
            $c->set( 'CONFIG_CMDLINE_BOOL',     'y' );
            $c->set( 'CONFIG_CMDLINE_OVERRIDE', 'y' );
        }
        else {
            $c->set( 'CONFIG_CMDLINE_FORCE', 'y' );
        }
    }
}

# If we are including debug tools in this build, then make
# the kernel .config file available via /proc/config.gz
# and also enable DEBUG_FS
if ( $p->get('BUILD_DEBUG_TOOLS') ) {
    $c->set( 'CONFIG_IKCONFIG',      'y' );
    $c->set( 'CONFIG_IKCONFIG_PROC', 'y' );
    $c->set( 'CONFIG_DEBUG_FS',      'y' );
}

$c->set( 'CONFIG_BRCM_IKOS',   $p->get('BRCM_IKOS') );
$c->set( 'CONFIG_BCM_ANDROID', $p->get('BRCM_ANDROID') );

if ( $chip =~ /^(6858|4908|63158|6856)$/ ) {
    if ( $chip =~ /^(6858|4908|63158)$/ ) {
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME', '"armb53"' );
    }
    else {
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME', '"arma53"' );
    }
    $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"aarch64"' );
    $c->set( 'CONFIG_FRAME_POINTER', 'y' );

    if ( $chip =~ /^(6858)$/ ) {
        $c->set( 'CONFIG_BCM96858',                  'y' );
        $c->set( 'CONFIG_BCM_COHERENT_OUTER_SHARED', 'y' );
        $c->set( 'CONFIG_RCU_KTHREAD_PRIO',          '10' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM6858_SIM',   'y' );
            $c->set( 'CONFIG_CMDLINE_FORCE', 'y' );
        }
        $c->set( 'CONFIG_HOTPLUG_CPU',                    'y' );
    }

    if ( $chip =~ /^(6856)$/ ) {
        $c->set( 'CONFIG_ETHERNET',         'y' );
        $c->set( 'CONFIG_NET_VENDOR_INTEL', 'y' );
        $c->set( 'CONFIG_IXGBE',            'm' );
        $c->set( 'CONFIG_BCM96856',         'y' );

        #        $c->set( 'CONFIG_BCM_COHERENT_OUTER_SHARED',   'y' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM6856_SIM',   'y' );
            $c->set( 'CONFIG_CMDLINE_FORCE', 'y' );
        }
    }

    if ( $chip =~ /^(4908)$/ ) {
        $c->set( 'CONFIG_BCM94908', 'y' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM4908_SIM', 'y' );
        }
        if ( $p->get('BRCM_CPU_FREQ_PWRSAVE') ) {
            $c->set( 'CONFIG_BCM_CPUFREQ',                    'y' );
            $c->set( 'CONFIG_CPU_FREQ',                       'y' );
            $c->set( 'CONFIG_CPU_FREQ_STAT',                  'y' );
            $c->set( 'CONFIG_CPU_FREQ_GOV_ONDEMAND',          'y' );
            $c->set( 'CONFIG_CPU_FREQ_GOV_INTERACTIVE',       'y' );
            $c->set( 'CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE', 'y' );
        }
    }
	
    if ( $chip =~ /^(4908|63158|6858|6856)$/ ) {
        $c->set( 'CONFIG_BCM_THERMAL',           'm' );
        $c->set( 'CONFIG_THERMAL',               'y' );
        $c->set( 'CONFIG_THERMAL_OF',            'y' );
        $c->set( 'CONFIG_THERMAL_GOV_STEP_WISE', 'y' );
        if ( $chip =~ /^(4908|63158)$/ ) {
            $c->set( 'CONFIG_HOTPLUG_CPU',           'y' );
            $c->set( 'CONFIG_HZ',                    '1000' );
        }
    }

    if ( $chip =~ /^(63158)$/ ) {

        $c->set( 'CONFIG_BCM963158',   'y' );

        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM63158_SIM',  'y' );
            $c->set( 'CONFIG_CMDLINE_FORCE', 'y' );
        }
    }

    $c->set( 'CONFIG_HIGHMEM', 'y' );
    if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
        $c->set( 'CONFIG_EXT2_FS',    'y' );
        $c->set( 'CONFIG_KALLSYMS',   'y' );
        $c->set( 'CONFIG_DEBUG_INFO', 'y' );
    }
    else {
        $c->set( 'CONFIG_ARCH_SPARSEMEM_ENABLE', 'y' );
    }

    $c->set( 'CONFIG_64BIT',                    'y' );
    $c->set( 'CONFIG_ARCH_PHYS_ADDR_T_64BIT',   'y' );
    $c->set( 'CONFIG_ARM_GIC_V3',               'y' );
    $c->set( 'CONFIG_ZONE_DMA',                 'y' );
    $c->set( 'CONFIG_ARCH_DMA_ADDR_T_64BIT',    'y' );
    $c->set( 'CONFIG_SWIOTLB',                  'y' );
    $c->set( 'CONFIG_IOMMU_HELPER',             'y' );
    $c->set( 'CONFIG_PGTABLE_LEVELS',           '3' );
    $c->set( 'CONFIG_GENERIC_IRQ_SHOW_LEVEL',   'y' );
    $c->set( 'CONFIG_ARCH_HAS_TICK_BROADCAST',  'y' );
    $c->set( 'CONFIG_TICK_CPU_ACCOUNTING',      'y' );
    $c->set( 'CONFIG_PREEMPT_COUNT',            'y' );
    $c->set( 'CONFIG_ARM_AMBA',                 'y' );
    $c->set( 'CONFIG_DTC',                      'y' );
    $c->set( 'CONFIG_OF',                       'y' );
    $c->set( 'CONFIG_OF_FLATTREE',              'y' );
    $c->set( 'CONFIG_OF_EARLY_FLATTREE',        'y' );
    $c->set( 'CONFIG_OF_ADDRESS',               'y' );
    $c->set( 'CONFIG_OF_IRQ',                   'y' );
    $c->set( 'CONFIG_OF_RESERVED_MEM',          'y' );
    $c->set( 'CONFIG_CLKSRC_OF',                'y' );
    $c->set( 'CONFIG_ARM_ARCH_TIMER',           'y' );
    $c->set( 'CONFIG_ARM_ARCH_TIMER_EVTSTREAM', 'y' );
    $c->set( 'CONFIG_IRQCHIP',                  'y' );

    # 32 bit app compatibility
    $c->set( 'CONFIG_COMPAT',   'y' );
    $c->set( 'CONFIG_UID16',    'y' );
    $c->set( "CONFIG_ELF_CORE", 'y' );

    # also defined for other chips
    $c->set( 'CONFIG_NEED_DMA_MAP_STATE',            'y' );    # exist in 63138
    $c->set( 'CONFIG_GENERIC_CLOCKEVENTS',           'y' );    # exist in 63138
    $c->set( 'CONFIG_GENERIC_CLOCKEVENTS_BROADCAST', 'y' );    # exist in 63138
    $c->set( 'CONFIG_NR_CPUS',                       '4' );    # exist

    # not set in other chips
    $c->set( 'CONFIG_NO_IOPORT_MAP',             'y' );
    $c->set( 'CONFIG_GENERIC_CSUM',              'y' );
    $c->set( 'CONFIG_HAVE_GENERIC_RCU_GUP',      'y' );
    $c->set( 'CONFIG_NEED_SG_DMA_LENGTH',        'y' );
    $c->set( 'CONFIG_KERNEL_MODE_NEON',          'y' );
    $c->set( 'CONFIG_FIX_EARLYCON_MEM',          'y' );
    $c->set( 'CONFIG_SPARSE_IRQ',                'y' );
    $c->set( 'CONFIG_GENERIC_TIME_VSYSCALL',     'y' );
    $c->set( 'CONFIG_NO_HZ_COMMON',              'y' );
    $c->set( 'CONFIG_NO_HZ_IDLE',                'y' );
    $c->set( 'CONFIG_HW_PERF_EVENTS',            'y' );
    $c->set( 'CONFIG_SYS_SUPPORTS_HUGETLBFS',    'y' );
    $c->set( 'CONFIG_ARCH_WANT_GENERAL_HUGETLB', 'y' );
    $c->set( 'CONFIG_ARCH_WANT_HUGE_PMD_SHARE',  'y' );
    $c->set( 'CONFIG_ARCH_HAS_CACHE_LINE_SIZE',  'y' );
    $c->set( 'CONFIG_HAVE_MEMBLOCK',             'y' );
    $c->set( 'CONFIG_NO_BOOTMEM',                'y' );
    $c->set( 'CONFIG_PAGEFLAGS_EXTENDED',        'y' );
    $c->set( 'CONFIG_COMPACTION',                'y' );
    $c->set( 'CONFIG_MIGRATION',                 'y' );
    $c->set( 'CONFIG_PHYS_ADDR_T_64BIT',         'y' );
    $c->set( 'CONFIG_BOUNCE',                    'y' );
    $c->set( 'CONFIG_KSM',                       'y' );
    $c->set( 'CONFIG_GENERIC_EARLY_IOREMAP',     'y' );
    $c->set( 'HAVE_ARCH_TRANSPARENT_HUGEPAGE',   'y' );
    $c->set( 'CONFIG_CPU_IDLE',                  'y' );
    $c->set( 'CONFIG_BCM_ARM_CPUIDLE',           'y' );
    $c->set( 'CONFIG_BCM_COMMON_CLK',            'y' );

    # taken from another chips
    $c->set( 'CONFIG_BCM_KERNEL_CUSTOM',              'y' );
    $c->set( 'CONFIG_LOG_BUF_SHIFT',                  '14' );
    $c->set( 'CONFIG_GENERIC_CLOCKEVENTS_BUILD',      'y' );
    $c->set( 'CONFIG_HAVE_FUNCTION_TRACER',           'y' );
    $c->set( 'CONFIG_HAVE_FUNCTION_GRAPH_TRACER',     'y' );
    $c->set( 'CONFIG_HAVE_DYNAMIC_FTRACE',            'y' );
    $c->set( 'CONFIG_HAVE_FTRACE_MCOUNT_RECORD',      'y' );
    $c->set( 'CONFIG_HAVE_C_RECORDMCOUNT',            'y' );
    $c->set( 'CONFIG_TRACING_SUPPORT',                'y' );
    $c->set( 'CONFIG_HAVE_ARCH_KGDB',                 'y' );
    $c->set( 'CONFIG_ARM_UNWIND',                     'y' );
    $c->set( 'CONFIG_DEBUG_USER',                     'y' );
    $c->set( 'CONFIG_DEBUG_LL',                       'y' );
    $c->set( 'CONFIG_CMDLINE_EXTEND',                 'y' );
    $c->set( 'CONFIG_DEBUG_LL_UART_NONE',             'y' );
    $c->set( 'CONFIG_EARLY_PRINTK',                   'y' );
    $c->set( 'CONFIG_SERIAL_CORE',                    'y' );
    $c->set( 'CONFIG_SERIAL_CORE_CONSOLE',            'y' );
    $c->set( 'CONFIG_SQUASHFS_XZ',                    'y' );
    $c->set( 'CONFIG_SQUASHFS_ZLIB',                  'y' );
    $c->set( 'CONFIG_FUTEX',                          'y' );
    $c->set( 'CONFIG_RCU_CPU_STALL_TIMEOUT',          '60' );
    $c->set( 'CONFIG_FRAME_WARN',                     '2048' );
    $c->set( 'CONFIG_ARCH_SUSPEND_POSSIBLE',          'y' );
    $c->set( 'CONFIG_ARCH_BINFMT_ELF_RAMDOMIZE_PIE',  'y' );
    $c->set( 'CONFIG_HAVE_AOUT',                      'y' );
    $c->set( 'CONFIG_USE_GENERIC_SMP_HELPERS',        'y' );
    $c->set( 'CONFIG_HAVE_REGS_AND_STACK_ACCESS_API', 'y' );
    $c->set( 'CONFIG_HAVE_CLK',                       'y' );
    $c->set( 'CONFIG_RT_MUTEXES',                     'y' );
    $c->set( 'CONFIG_SWAP',                           'y' );
    $c->set( 'CONFIG_HAVE_MEMBLOCK',                  'y' );
    $c->set( 'CONFIG_PAGEFLAGS_EXTENDED',             'y' );
    $c->set( 'CONFIG_VIRT_TO_BUS',                    'y' );
    $c->set( 'CONFIG_NEED_DMA_MAP_STATE',             'y' );
    $c->set( 'CONFIG_RCU_FANOUT',                     '64' );
    $c->set( 'CONFIG_CPU_LITTLE_ENDIAN',              'y' );
    $c->set( 'CONFIG_CPU_BIG_ENDIAN',                 undef );
}

elsif ( $chip =~ /^(63138|63148|47189|47622|63178|6846|6878|6855)$/ ) {
    if ( $chip =~ /^(47622|63178|6846|6878|6855)$/ ) {
        $c->set( 'CONFIG_BCM_THERMAL',           'm' );
        $c->set( 'CONFIG_THERMAL',               'y' );
        $c->set( 'CONFIG_THERMAL_OF',            'y' );
        $c->set( 'CONFIG_THERMAL_GOV_STEP_WISE', 'y' );
        if ( $chip =~ /^(47622|63178)$/ ) {
            $c->set( 'CONFIG_HOTPLUG_CPU',           'y' );
            $c->set( 'CONFIG_HZ_1000',               'y' );
        }
    }

    if ( $chip !~ /^(63178|47622|6878|6855)$/ ) {
        $c->set( 'CONFIG_ARCH_BCM63XX', 'y' );
    }

    if ( $p->get("BRCM_SPECTRE_PATCH_ENABLE") ) {
        $c->set( 'CONFIG_HARDEN_BRANCH_PREDICTOR',  'y' );
        $c->set( 'CONFIG_BCM_SPECTRE_PATCH_ENABLE', 'y' );
    }

    if ( $chip =~ /^(47622)$/ or $chip =~ /^(63178)$/ or $chip =~ /^(6878)$/ or $chip =~ /^(6855)$/ ) {
          $c->set("CONFIG_ARCH_MULTIPLATFORM",'y');
    }

    $c->set( 'CONFIG_BOARD_ZRELADDR',                 '0x00008000' );
    $c->set( 'CONFIG_BOARD_PARAMS_PHYS',              '0x00000000' );
    $c->set( 'CONFIG_BCM_KERNEL_CUSTOM',              'y' );
    $c->set( 'CONFIG_LOG_BUF_SHIFT',                   '16' );
    $c->set( 'CONFIG_HIGH_RES_TIMERS',                 'y' );
    $c->set( 'CONFIG_EXPERT',                         'y' );
    $c->set( 'CONFIG_GENERIC_CLOCKEVENTS_BUILD',      'y' );
    $c->set( 'CONFIG_GENERIC_CLOCKEVENTS',            'y' );
    $c->set( 'CONFIG_GENERIC_CLOCKEVENTS_BROADCAST',  'y' );
    $c->set( 'CONFIG_HAVE_FUNCTION_TRACER',           'y' );
    $c->set( 'CONFIG_HAVE_FUNCTION_GRAPH_TRACER',     'y' );
    $c->set( 'CONFIG_HAVE_DYNAMIC_FTRACE',            'y' );
    $c->set( 'CONFIG_HAVE_FTRACE_MCOUNT_RECORD',      'y' );
    $c->set( 'CONFIG_HAVE_C_RECORDMCOUNT',            'y' );
    $c->set( 'CONFIG_TRACING_SUPPORT',                'y' );
    $c->set( 'CONFIG_HAVE_ARCH_KGDB',                 'y' );
    $c->set( 'CONFIG_ARM_UNWIND',                     'y' );
    $c->set( 'CONFIG_DEBUG_USER',                     'y' );
    $c->set( 'CONFIG_DEBUG_LL',                       'y' );
    $c->set( 'CONFIG_CMDLINE_EXTEND',                 'y' );
    $c->set( 'CONFIG_DEBUG_LL_UART_NONE',             'y' );
    $c->set( 'CONFIG_EARLY_PRINTK',                   'y' );
    $c->set( 'CONFIG_SERIAL_CORE',                    'y' );
    $c->set( 'CONFIG_SERIAL_CORE_CONSOLE',            'y' );
    $c->set( 'CONFIG_SQUASHFS_XZ',                    'y' );
    $c->set( 'CONFIG_SQUASHFS_ZLIB',                  'y' );
    $c->set( 'CONFIG_FUTEX',                          'y' );
    $c->set( 'CONFIG_RCU_CPU_STALL_TIMEOUT',          '60' );
    $c->set( 'CONFIG_FRAME_WARN',                     '2048' );
    $c->set( 'CONFIG_ARCH_SUSPEND_POSSIBLE',          'y' );
    $c->set( 'CONFIG_ARCH_BINFMT_ELF_RAMDOMIZE_PIE',  'y' );
    $c->set( 'CONFIG_HAVE_AOUT',                      'y' );
    $c->set( 'CONFIG_USE_GENERIC_SMP_HELPERS',        'y' );
    $c->set( 'CONFIG_HAVE_REGS_AND_STACK_ACCESS_API', 'y' );
    $c->set( 'CONFIG_HAVE_CLK',                       'y' );
    $c->set( 'CONFIG_RT_MUTEXES',                     'y' );
    $c->set( 'CONFIG_SWAP',                           'y' );
    $c->set( 'CONFIG_HAVE_MEMBLOCK',                  'y' );
    $c->set( 'CONFIG_PAGEFLAGS_EXTENDED',             'y' );
    $c->set( 'CONFIG_VIRT_TO_BUS',                    'y' );
    $c->set( 'CONFIG_NEED_DMA_MAP_STATE',             'y' );
    $c->set( 'CONFIG_RCU_FANOUT',                     '32' );
    $c->set( 'CONFIG_ATAGS',                           'y' );
    $c->set( 'CONFIG_USE_OF',                         'y' );
    $c->set( 'CONFIG_DTC',                            'y' );
    $c->set( 'CONFIG_OF',                             'y' );
    $c->set( 'CONFIG_OF_FLATTREE',                    'y' );
    $c->set( 'CONFIG_OF_EARLY_FLATTREE',              'y' );
    $c->set( 'CONFIG_OF_ADDRESS',                     'y' );
    $c->set( 'CONFIG_OF_IRQ',                         'y' );
    $c->set( 'CONFIG_OF_RESERVED_MEM',                'y' );
    $c->set( 'CONFIG_BCM_COMMON_CLK',                 'y' );
    if ( $chip =~ /^(63138)$/ ) {
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"arma9"' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"arm32"' );
        $c->set( 'CONFIG_BCM963138',             'y' );
        $c->set( 'CONFIG_MACH_BCM963138',        'y' );
        $c->set( 'CONFIG_PLAT_CA9_MPCORE',       'y' );
        $c->set( 'CONFIG_PLAT_BCM63138',         'y' );
        $c->set( 'CONFIG_NR_CPUS',               '2' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM63138_SIM', 'y' );
        }
        $c->set( 'CONFIG_CPU_IDLE',        'y' );
        $c->set( 'CONFIG_BCM_ARM_CPUIDLE', 'y' );
    }
    if ( $chip =~ /^(47189)$/ ) {

        $c->set( 'CONFIG_ARCH_BCM63XX',         'n' );
        $c->set( 'BCM_MTD_BCM963XX',            'y' );
        $c->set( 'CONFIG_MTD_BRCMNAND',         'n' );
        $c->set( 'CONFIG_PLAT_CA9_MPCORE',      'n' );
        $c->set( 'CONFIG_BCM_B15_MEGA_BARRIER', 'n' );
        $c->set( 'CONFIG_PLAT_B15_CORE',        'n' );

        #armv7
        $c->set( 'CONFIG_ARCH_BCM947XXX',        'y' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"arma7"' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"arm32"' );

        #       $c->set( 'CONFIG_ARM_APPENDED_DTB',		'y' );
#        $c->set( 'CONFIG_DEBUG_INFO', 'y' );
        $c->set( 'CONFIG_PRINTK',     'y' );

        #       $c->set( 'CONFIG_INITRAMFS_SOURCE',		'"rootfs_arm.cpio"' );
        #       $c->set( 'CONFIG_INITRAMFS_ROOT_UID',		'1000' );
        #	$c->set( 'CONFIG_INITRAMFS_ROOT_GID',		'1000' );
        $c->set( 'CONFIG_SERIAL_8250',               'y' );
        $c->set( 'CONFIG_SERIAL_8250_CONSOLE',       'y' );
        $c->set( 'CONFIG_SERIAL_8250_FSL',           'y' );
        $c->set( 'CONFIG_SERIAL_8250_NR_UARTS',      '2' );
        $c->set( 'CONFIG_SERIAL_8250_RUNTIME_UARTS', '2' );
        $c->set( 'CONFIG_BCMA',                      'n' );
        $c->set( 'CONFIG_SERIAL_OF_PLATFORM',        'y' );
        $c->set( 'CONFIG_GENERIC_TIME_VSYSCALL',     'y' );
        $c->set( 'CONFIG_VDSO',                      'n' );
        $c->set( 'CONFIG_CACHE_L2X0',                'n' );
        $c->set( 'CONFIG_NEW_LEDS',                  'n' );

        $c->unset('CONFIG_DEBUG_LL');
        $c->unset('CONFIG_EARLY_PRINTK');
    }
    if ( $chip =~ /^(63148)$/ ) {
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"armb15"' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"arm32"' );
        $c->set( 'CONFIG_BCM963148',             'y' );
        $c->set( 'CONFIG_MACH_BCM963148',        'y' );
        $c->set( 'CONFIG_PLAT_B15_CORE',         'y' );
        $c->set( 'CONFIG_PLAT_BCM63148',         'y' );
        $c->set( 'CONFIG_NR_CPUS',               '2' );
        $c->set( 'CONFIG_HIGHMEM',               'y' );

        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM63148_SIM', 'y' );
        }
        $c->set( 'CONFIG_VFP',   'y' );
        $c->set( 'CONFIG_VFPv3', 'y' );
        $c->set( 'CONFIG_NEON',  'y' );
    }
    if ( $chip =~ /^(6846)$/ ) {
        $c->set( 'CONFIG_MACH_BCM96846', 'y' );
        $c->unset('CONFIG_CACHE_L2X0');
        $c->unset('CONFIG_PLAT_CA9_MPCORE');
        $c->unset('CONFIG_BCM_B15_MEGA_BARRIER');
        $c->unset('CONFIG_PLAT_B15_CORE');
        $c->unset('CONFIG_DEBUG_LL');
        $c->unset('CONFIG_EARLY_PRINTK');
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"arma7"' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"arm32"' );
        $c->set( 'CONFIG_BCM96846',              'y' );

        $c->set( 'CONFIG_PLAT_CA7_CORE', 'y' );
        $c->set( 'CONFIG_PLAT_BCM6846',  'y' );
        $c->set( 'CONFIG_NR_CPUS',       '2' );
        $c->unset('CONFIG_ATAGS');
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM6846_SIM', 'y' );
        }
        $c->set( 'CONFIG_CPU_IDLE',        'y' );
        $c->set( 'CONFIG_BCM_ARM_CPUIDLE', 'y' );
    }
    if ( $chip =~ /^(6878)$/ ) {
        $c->set( 'CONFIG_BCM96878', 'y' );
        $c->set( 'CONFIG_NR_CPUS',   '2' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM6878_SIM', 'y' );
        }
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"arma7"' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"arm32"' );
        $c->unset('CONFIG_ATAGS');
        $c->unset('CONFIG_CACHE_L2X0');
        $c->set( 'CONFIG_CPU_IDLE',        'y' );
        $c->set( 'CONFIG_BCM_ARM_CPUIDLE', 'y' );
    }
    if ( $chip =~ /^(6855)$/ ) {
        $c->set( 'CONFIG_BCM96855', 'y' );
        $c->set( 'CONFIG_NR_CPUS',   '3' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM6855_SIM', 'y' );
        }

        $c->set( 'CONFIG_VFP',                            'y' );
        $c->set( 'CONFIG_VFPv3',                        'y' );
        $c->set( 'CONFIG_NEON',                        'y' );

        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"arma7"' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"arm32"' );
        $c->unset('CONFIG_ATAGS');
        $c->unset('CONFIG_CACHE_L2X0');
        $c->set( 'CONFIG_CPU_IDLE',        'y' );
        $c->set( 'CONFIG_BCM_ARM_CPUIDLE', 'y' );
        $c->set( 'CONFIG_HIGHMEM',         'y' );
    }
    if ( $chip =~ /^(47622)$/ ) {
        $c->set( 'CONFIG_BCM947622', 'y' );
        $c->set( 'CONFIG_NR_CPUS',   '4' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM47622_SIM', 'y' );
        }
        $c->set( 'CONFIG_VFP',                            'y' );
        $c->set( 'CONFIG_VFPv3',                        'y' );
        $c->set( 'CONFIG_NEON',                        'y' );
    }
    if ( $chip =~ /^(63178)$/ ) {
        $c->set( 'CONFIG_BCM963178', 'y' );
        $c->set( 'CONFIG_NR_CPUS',   '3' );
        if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
            $c->set( 'CONFIG_BCM63178_SIM', 'y' );
        }
        $c->set( 'CONFIG_VFP',   'y' );
        $c->set( 'CONFIG_VFPv3', 'y' );
        $c->set( 'CONFIG_NEON',  'y' );
    }
    if ( $chip =~ /^(47622)$/ or $chip =~ /^(63178)$/ ) {
        $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"arma7"' );
        $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"arm32"' );
        $c->unset('CONFIG_ATAGS');
        $c->set( 'CONFIG_CPU_IDLE',        'y' );
        $c->set( 'CONFIG_BCM_ARM_CPUIDLE', 'y' );
        $c->set( 'CONFIG_HIGHMEM',         'y' );
        $c->unset('CONFIG_CACHE_L2X0');
    }
    if ( $p->get('BRCM_BOARD_ID') eq "\"ikos\"" ) {
        $c->set( 'CONFIG_CMDLINE_FORCE', 'y' );
        $c->set( 'CONFIG_EXT2_FS',       'y' );
        $c->set( 'CONFIG_KALLSYMS',      'y' );
        $c->set( 'CONFIG_DEBUG_INFO',    'y' );
        $c->unset('CONFIG_BCM_CPUFREQ');
    }
    $c->set( 'CONFIG_CPU_LITTLE_ENDIAN', 'y' );
    $c->set( 'CONFIG_CPU_BIG_ENDIAN',    undef );
    $c->set( 'CONFIG_LBDAF',             'y' );
    $c->set( 'CONFIG_HZ_1000',           'y' );

    if ( $p->get('BRCM_CPU_FREQ_PWRSAVE') ) {
        $c->set( 'CONFIG_BCM_CPUFREQ',                    'y' );
        $c->set( 'CONFIG_CPU_FREQ',                       'y' );
        $c->set( 'CONFIG_CPU_FREQ_STAT',                  'y' );
        $c->set( 'CONFIG_CPU_FREQ_GOV_ONDEMAND',          'y' );
        $c->set( 'CONFIG_CPU_FREQ_GOV_INTERACTIVE',       'y' );
        $c->set( 'CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE', 'y' );
    }
}
else {
    $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',                     '"mips32"' );
    $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME',                 '"bemips"' );
    $c->set( 'CONFIG_RCU_CPU_STALL_TIMEOUT',                 '60' );
    $c->set( 'CONFIG_CRYPTO_ECB',                            'm' );
    $c->set( 'CONFIG_BCM_PKTDMA_RX_SPLITTING',               'y' );
    $c->set( 'CONFIG_USE_GENERIC_SMP_HELPERS',               'y' );
    $c->set( 'CONFIG_NET_ETHERNET',                          'y' );
    $c->set( 'CONFIG_NR_CPUS',                               '2' );
    $c->set( 'CONFIG_INET_TCP_DIAG',                         'y' );
    $c->set( 'CONFIG_BCM_KERNEL_CUSTOM',                     'y' );
    $c->set( 'CONFIG_LOG_BUF_SHIFT',                         '14' );
    $c->set( 'CONFIG_MTD_ROM',                               'y' );
    $c->set( 'CONFIG_DMA_NEED_PCI_MAP_STATE',                'y' );
    $c->set( 'CONFIG_BRCM_MEMORY_RESTRICTION_OFF',           'y' );
    $c->set( 'CONFIG_MIPS32',                                'y' );
    $c->set( 'CONFIG_MIPS_BRCM',                             'y' );
    $c->set( 'CONFIG_MISC_DEVICES',                          'y' );
    $c->set( 'CONFIG_NETDEV_1000',                           'y' );
    $c->set( 'CONFIG_CRYPTO_AES',                            'y' );
    $c->set( 'CONFIG_MODULE_FORCE_UNLOAD',                   'y' );
    $c->set( 'CONFIG_USB_PRINTER',                           'y' );
    $c->set( 'CONFIG_BCM_BPM_BUF_MEM_PRCNT',                 '15' );
    $c->set( 'CONFIG_GENERIC_FIND_NEXT_BIT',                 'y' );
    $c->set( 'CONFIG_BRCMNAND_MINOR_VERS',                   '1' );
    $c->set( 'CONFIG_NETDEV_10000',                          'y' );
    $c->set( 'CONFIG_KMOD',                                  'y' );
    $c->set( 'CONFIG_MTD_BRCMNAND_CORRECTABLE_ERR_HANDLING', 'y' );
    $c->set( 'CONFIG_NF_CONNTRACK_ENABLED',                  'm' );
    $c->set( 'CONFIG_MIPS_L1_CACHE_SHIFT',                   '4' );
    $c->set( 'CONFIG_SYS_SUPPORTS_ARBIT_HZ',                 'y' );
    $c->set( 'CONFIG_BRCMNAND_MAJOR_VERS',                   '2' );
    $c->set( 'CONFIG_ATM_DRIVERS',                           'y' );
    $c->set( 'CONFIG_BCM_PKTDMA_TX_SPLITTING',               'y' );
    $c->set( 'CONFIG_NF_CONNTRACK_SUPPORT',                  'y' );
    $c->set( 'CONFIG_RCU_CPU_STALL_VERBOSE',                 'y' );
    $c->set( 'CONFIG_GENERIC_TIME',                          'y' );
    $c->set( 'CONFIG_IP_FIB_HASH',                           'y' );
    $c->set( 'CONFIG_BRANCH_PROFILE_NONE',                   'y' );
    $c->set( 'CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE',       '0' );
    $c->set( 'CONFIG_PREEMPT_VOLUNTARY',                     'y' );
    $c->set( 'CONFIG_TMPFS',                                 'y' );
    $c->set( 'CONFIG_IP_MROUTE',                             'y' );
    $c->set( 'CONFIG_COMPAT_NET_DEV_OPS',                    'y' );
    $c->set( 'CONFIG_XZ_DEC_X86',                            'y' );
    $c->set( 'CONFIG_CLEAN_COMPILE',                         'y' );
    $c->set( 'CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER',        'y' );
    $c->set( 'CONFIG_XZ_DEC',                                'y' );
    $c->set( 'CONFIG_TINY_SHMEM',                            'y' );
    $c->set( 'CONFIG_I2C_HELPER_AUTO',                       'y' );
    $c->set( 'CONFIG_HZ_1000',                               'y' );
    $c->set( 'CONFIG_CROSSCOMPILE',                          'y' );
    $c->set( 'CONFIG_INET_DIAG',                             'y' );
    $c->set( 'CONFIG_OBSOLETE_MODPARM',                      'y' );
    $c->set( 'CONFIG_SYSCTL_SYSCALL_CHECK',                  'y' );
    $c->set( 'CONFIG_RAMFS',                                 'y' );
    $c->set( 'CONFIG_LBDAF',                                 'y' );
    $c->set( 'CONFIG_SQUASHFS_XZ',                           'y' );
    $c->set( 'CONFIG_KLOB',                                  'y' );
    $c->set( 'CONFIG_ENABLE_MUST_CHECK',                     'y' );
    $c->set( 'CONFIG_CRYPTO_PCBC',                           'm' );
    $c->set( 'CONFIG_FRAME_WARN',                            '1024' );
    $c->set( 'CONFIG_I2C_COMPAT',                            'y' );
    $c->set( 'CONFIG_NETFILTER_XT_MARK',                     'm' );
    $c->set( 'CONFIG_DEVKMEM',                               'y' );
    $c->set( 'CONFIG_MTD_BCM963XX',                          'y' );
    $c->set( 'CONFIG_MIPS_BCM963XX',                         'y' );
    $c->set( 'CONFIG_CLASSIC_RCU',                           'y' );
    $c->set( 'CONFIG_VM_EVENT_COUNTERS',                     'y' );
    $c->set( 'CONFIG_FTRACE',                                'y' );
    $c->set( 'CONFIG_PREEMPT_SOFTIRQS',                      'y' );
    $c->set( 'CONFIG_DEVPTS_MULTIPLE_INSTANCES',             'y' );
    $c->set( 'CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE',      '0' );
    $c->set( 'CONFIG_RCU_FANOUT',                            '32' );
    $c->set( 'CONFIG_BCM_DCACHE_SHARED',                     'y' );
    $c->set( 'CONFIG_USE_OF',                                'y' );
    $c->set( 'CONFIG_DTC',                                   'y' );
    $c->set( 'CONFIG_OF',                                    'y' );
    $c->set( 'CONFIG_OF_FLATTREE',                           'y' );
    $c->set( 'CONFIG_OF_EARLY_FLATTREE',                     'y' );
    $c->set( 'CONFIG_OF_ADDRESS',                            'y' );
    $c->set( 'CONFIG_OF_IRQ',                                'y' );
    $c->set( 'CONFIG_OF_RESERVED_MEM',                       'y' );

    if ( $p->get("BCM_SUPPORT_512MB_DDR") ) {
        $c->set( 'CONFIG_PAGE_SIZE_16KB', 'y' );
        $c->set( 'CONFIG_BCM_512MB_DDR',  'y' );
    }
    else {
        $c->set( 'CONFIG_PAGE_SIZE_4KB', 'y' );
    }
    $c->set( 'CONFIG_FUTEX', 'y' );
}
if ( $chip eq '60333' ) {
    $c->set( 'CONFIG_BCM_CPU_ARCH_NAME',     '"mips32el"' );
    $c->set( 'CONFIG_BCM_CPU_ARCH_ABI_NAME', '"mipsel"'   );
    $c->set( 'CONFIG_CPU_LITTLE_ENDIAN',     'y' );
    $c->set( 'CONFIG_CPU_BIG_ENDIAN',        undef );
}

if ( $chip =~ /^(6838|6848|6858|6846|6856)$/ ) {
    $c->set( 'CONFIG_BCM_BRIDGE_MAC_FDB_LIMIT', 'y' );
    $c->set( 'CONFIG_HW_PERF_EVENTS',           'y' );
}

if ( $chip =~ /^(6846|6856)$/ ) {
    $c->set( 'CONFIG_BCM_UBUS_DECODE_REMAP', 'y' );
}

if ( $chip =~ /^(6846|6858|6856|63158|63178|47622|6855)$/ ) {
    $c->set( 'CONFIG_BCM_GLB_COHERENCY',     'y' );
    $c->set( 'CONFIG_BCM_UBUS_DECODE_REMAP', 'y' );
    if ( $chip eq '6858' ) {

        # In 6858 FPM pool memory have to be non-coherent since
        # CCI-400 isn't able to sustain 10G traffic
        $c->set( 'CONFIG_BCM_FPM_COHERENCY_EXCLUDE', 'y' );
    }
}

if ( $p->get("BCM_OPTEE") ) {
    $c->set( "CONFIG_TEE",            "y" );
    $c->set( "CONFIG_OPTEE",          "y" );
    $c->set( "CONFIG_ARM_PSCI",       "y" );
    $c->set( "CONFIG_HAVE_ARM_SMCCC", "y" );
    if ( $p->get("BCM_KERNEL_CODE_PROTECTION") ) {
       $c->set( "CONFIG_BCM_KERNEL_CODE_PROTECTION", "y" );
    }
}

$c->set( 'CONFIG_MULTIUSER',                  'y' );
$c->set( 'CONFIG_UEVENT_HELPER',              'y' );
$c->set( 'CONFIG_UEVENT_HELPER_PATH',         '"/sbin/hotplug"' );
$c->set( 'CONFIG_PANIC_TIMEOUT',              '5' );
$c->set( 'CONFIG_PANIC_ON_OOPS',              'y' );
$c->set( 'CONFIG_LOCKUP_DETECTOR',            'y' );
$c->set( 'CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC', 'y' );

if ( $p->get('BUILD_BRCM_HNDROUTER') ) {
    $c->set( 'CONFIG_BCM_HNDROUTER', 'y' );
}

if ( $p->get('BUILD_BRCM_AIRIQ') ) {
    $c->set( 'CONFIG_BCM_AIRIQ', 'y' );
    $c->set( 'CONFIG_EPOLL',     'y' );
    $c->set( 'CONFIG_EXPERT',    'y' );
}

if ( $p->get('BUILD_BRCM_HOSTAPD') ) {
    $c->set( 'CONFIG_BCM_HOSTAPD', 'y' );
}

if ( $p->get('BUILD_MTD_OOPS') ) {
    $c->set( "CONFIG_MTD_OOPS", 'm' );
}

if ( $p->get('BUILD_ARM_PLTS') ) {
    $c->set( "CONFIG_ARM_MODULE_PLTS", 'y' );
}

if ( $p->get('BUILD_HND_EAP') ) {
    $c->set( "CONFIG_GPIO_DEVRES",           'y' );
    $c->set( "CONFIG_GPIOLIB",               'y' );
    $c->set( "CONFIG_OF_GPIO",               'y' );
    $c->set( "CONFIG_GPIO_SYSFS",            'y' );
    $c->set( "CONFIG_GPIO_GENERIC_PLATFORM", 'y' );
    $c->set( "CONFIG_SPI_BCM63XX_HSSPI",     'y' );
    $c->set( "CONFIG_SPI_SPIDEV",            'y' );
    $c->set( "CONFIG_MTD_SPI_NOR",           'y' );
    $c->set( "CONFIG_MTD_M25P80",            'y' );
    $c->set( 'CONFIG_BCM_HND_EAP',           'y' );
}

if ( $p->get('BRCM_KERNEL_BONDING') ) {
    $c->set( 'CONFIG_BONDING', 'm' );
}

if ( $p->get('BRCM_RUNNER_FLOODING') ) {
    $c->set( 'CONFIG_RUNNER_FLOODING', 'y' );
}

if ( $p->get('BUILD_LINUX_PERF') ) {
    $c->set( 'CONFIG_PERF_EVENTS',     'y' );
    $c->set( "CONFIG_HIGH_RES_TIMERS", 'y' );
    $c->set( "CONFIG_KPROBES",         "y" );
    $c->set( "CONFIG_UPROBES",         "y" );

    if ( $chip =~ /^(63138|63148|6846|47622|63178)$/ ) {
        $c->set( 'CONFIG_HW_PERF_EVENTS', 'y' );
    }
}

#OPTEE reserve memory does not work with CMA. Disable it CMA for OPTEE temporarily 
if ( $p->get('BUILD_SECURE_OS') ne "y") {
    if( $p->get('BRCM_CMA_RSVMEM') || $p->get("LINUX_4_19_0") eq 'y' ) {
        if ( $arch ne 'mips' ) {
            $c->set( 'CONFIG_DMA_CMA',          'y' );
            $c->set( 'CONFIG_CMA',              'y' );
            $c->set( 'CONFIG_BCM_CMA_RSVMEM',   'y' );
        }
    }
}

$c->write();

