18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * SGI IP30 miscellaneous setup bits.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2004-2007 Stanislaw Skowronek <skylark@unaligned.org>
68c2ecf20Sopenharmony_ci *               2007 Joshua Kinard <kumba@gentoo.org>
78c2ecf20Sopenharmony_ci *               2009 Johannes Dickgreber <tanzy@gmx.de>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/percpu.h>
158c2ecf20Sopenharmony_ci#include <linux/memblock.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/smp-ops.h>
188c2ecf20Sopenharmony_ci#include <asm/sgialib.h>
198c2ecf20Sopenharmony_ci#include <asm/time.h>
208c2ecf20Sopenharmony_ci#include <asm/sgi/heart.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "ip30-common.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Structure of accessible HEART registers located in XKPHYS space. */
258c2ecf20Sopenharmony_cistruct ip30_heart_regs __iomem *heart_regs = HEART_XKPHYS_BASE;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * ARCS will report up to the first 1GB of
298c2ecf20Sopenharmony_ci * memory if queried.  Anything beyond that
308c2ecf20Sopenharmony_ci * is marked as reserved.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci#define IP30_MAX_PROM_MEMORY	_AC(0x40000000, UL)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Memory in the Octane starts at 512MB
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci#define IP30_MEMORY_BASE	_AC(0x20000000, UL)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * If using ARCS to probe for memory, then
418c2ecf20Sopenharmony_ci * remaining memory will start at this offset.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci#define IP30_REAL_MEMORY_START  (IP30_MEMORY_BASE + IP30_MAX_PROM_MEMORY)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define MEM_SHIFT(x) ((x) >> 20)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic void __init ip30_mem_init(void)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	unsigned long total_mem;
508c2ecf20Sopenharmony_ci	phys_addr_t addr;
518c2ecf20Sopenharmony_ci	phys_addr_t size;
528c2ecf20Sopenharmony_ci	u32 memcfg;
538c2ecf20Sopenharmony_ci	int i;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	total_mem = 0;
568c2ecf20Sopenharmony_ci	for (i = 0; i < HEART_MEMORY_BANKS; i++) {
578c2ecf20Sopenharmony_ci		memcfg = __raw_readl(&heart_regs->mem_cfg.l[i]);
588c2ecf20Sopenharmony_ci		if (!(memcfg & HEART_MEMCFG_VALID))
598c2ecf20Sopenharmony_ci			continue;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci		addr = memcfg & HEART_MEMCFG_ADDR_MASK;
628c2ecf20Sopenharmony_ci		addr <<= HEART_MEMCFG_UNIT_SHIFT;
638c2ecf20Sopenharmony_ci		addr += IP30_MEMORY_BASE;
648c2ecf20Sopenharmony_ci		size = memcfg & HEART_MEMCFG_SIZE_MASK;
658c2ecf20Sopenharmony_ci		size >>= HEART_MEMCFG_SIZE_SHIFT;
668c2ecf20Sopenharmony_ci		size += 1;
678c2ecf20Sopenharmony_ci		size <<= HEART_MEMCFG_UNIT_SHIFT;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci		total_mem += size;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci		if (addr >= IP30_REAL_MEMORY_START)
728c2ecf20Sopenharmony_ci			memblock_free(addr, size);
738c2ecf20Sopenharmony_ci		else if ((addr + size) > IP30_REAL_MEMORY_START)
748c2ecf20Sopenharmony_ci			memblock_free(IP30_REAL_MEMORY_START,
758c2ecf20Sopenharmony_ci				     size - IP30_MAX_PROM_MEMORY);
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci	pr_info("Detected %luMB of physical memory.\n", MEM_SHIFT(total_mem));
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/**
818c2ecf20Sopenharmony_ci * ip30_cpu_time_init - platform time initialization.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_cistatic void __init ip30_cpu_time_init(void)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	int cpu = smp_processor_id();
868c2ecf20Sopenharmony_ci	u64 heart_compare;
878c2ecf20Sopenharmony_ci	unsigned int start, end;
888c2ecf20Sopenharmony_ci	int time_diff;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	heart_compare = (heart_read(&heart_regs->count) +
918c2ecf20Sopenharmony_ci			 (HEART_CYCLES_PER_SEC / 10));
928c2ecf20Sopenharmony_ci	start = read_c0_count();
938c2ecf20Sopenharmony_ci	while ((heart_read(&heart_regs->count) - heart_compare) & 0x800000)
948c2ecf20Sopenharmony_ci		cpu_relax();
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	end = read_c0_count();
978c2ecf20Sopenharmony_ci	time_diff = (int)end - (int)start;
988c2ecf20Sopenharmony_ci	mips_hpt_frequency = time_diff * 10;
998c2ecf20Sopenharmony_ci	pr_info("IP30: CPU%d: %d MHz CPU detected.\n", cpu,
1008c2ecf20Sopenharmony_ci		(mips_hpt_frequency * 2) / 1000000);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_civoid __init ip30_per_cpu_init(void)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	/* Disable all interrupts. */
1068c2ecf20Sopenharmony_ci	clear_c0_status(ST0_IM);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	ip30_cpu_time_init();
1098c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
1108c2ecf20Sopenharmony_ci	ip30_install_ipi();
1118c2ecf20Sopenharmony_ci#endif
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	enable_percpu_irq(IP30_HEART_L0_IRQ, IRQ_TYPE_NONE);
1148c2ecf20Sopenharmony_ci	enable_percpu_irq(IP30_HEART_L1_IRQ, IRQ_TYPE_NONE);
1158c2ecf20Sopenharmony_ci	enable_percpu_irq(IP30_HEART_L2_IRQ, IRQ_TYPE_NONE);
1168c2ecf20Sopenharmony_ci	enable_percpu_irq(IP30_HEART_ERR_IRQ, IRQ_TYPE_NONE);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/**
1208c2ecf20Sopenharmony_ci * plat_mem_setup - despite the name, misc setup happens here.
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_civoid __init plat_mem_setup(void)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	ip30_mem_init();
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* XXX: Hard lock on /sbin/init if this flag isn't specified. */
1278c2ecf20Sopenharmony_ci	prom_flags |= PROM_FLAG_DONT_FREE_TEMP;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
1308c2ecf20Sopenharmony_ci	register_smp_ops(&ip30_smp_ops);
1318c2ecf20Sopenharmony_ci#else
1328c2ecf20Sopenharmony_ci	ip30_per_cpu_init();
1338c2ecf20Sopenharmony_ci#endif
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	ioport_resource.start = 0;
1368c2ecf20Sopenharmony_ci	ioport_resource.end = ~0UL;
1378c2ecf20Sopenharmony_ci	set_io_port_base(IO_BASE);
1388c2ecf20Sopenharmony_ci}
139