162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * ip22-mc.c: Routines for manipulating SGI Memory Controller.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
662306a36Sopenharmony_ci * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu) - Indigo2 changes
762306a36Sopenharmony_ci * Copyright (C) 2003 Ladislav Michl  (ladis@linux-mips.org)
862306a36Sopenharmony_ci * Copyright (C) 2004 Peter Fuerst    (pf@net.alphadv.de) - IP28
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/init.h>
1262306a36Sopenharmony_ci#include <linux/export.h>
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/memblock.h>
1562306a36Sopenharmony_ci#include <linux/spinlock.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <asm/io.h>
1862306a36Sopenharmony_ci#include <asm/bootinfo.h>
1962306a36Sopenharmony_ci#include <asm/sgialib.h>
2062306a36Sopenharmony_ci#include <asm/sgi/mc.h>
2162306a36Sopenharmony_ci#include <asm/sgi/hpc3.h>
2262306a36Sopenharmony_ci#include <asm/sgi/ip22.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistruct sgimc_regs *sgimc;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciEXPORT_SYMBOL(sgimc);
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_cistatic inline unsigned long get_bank_addr(unsigned int memconfig)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	return (memconfig & SGIMC_MCONFIG_BASEADDR) << ((sgimc->systemid & SGIMC_SYSID_MASKREV) >= 5 ? 24 : 22);
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic inline unsigned long get_bank_size(unsigned int memconfig)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	return ((memconfig & SGIMC_MCONFIG_RMASK) + 0x0100) << ((sgimc->systemid & SGIMC_SYSID_MASKREV) >= 5 ? 16 : 14);
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic inline unsigned int get_bank_config(int bank)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	unsigned int res = bank > 1 ? sgimc->mconfig1 : sgimc->mconfig0;
4162306a36Sopenharmony_ci	return bank % 2 ? res & 0xffff : res >> 16;
4262306a36Sopenharmony_ci}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#if defined(CONFIG_SGI_IP28) || defined(CONFIG_32BIT)
4562306a36Sopenharmony_cistatic void __init probe_memory(void)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	/* prom detects all usable memory */
4862306a36Sopenharmony_ci}
4962306a36Sopenharmony_ci#else
5062306a36Sopenharmony_ci/*
5162306a36Sopenharmony_ci * Detect installed memory, which PROM misses
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_cistatic void __init probe_memory(void)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	unsigned long addr, size;
5662306a36Sopenharmony_ci	int i;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	printk(KERN_INFO "MC: Probing memory configuration:\n");
5962306a36Sopenharmony_ci	for (i = 0; i < 4; i++) {
6062306a36Sopenharmony_ci		unsigned int tmp = get_bank_config(i);
6162306a36Sopenharmony_ci		if (!(tmp & SGIMC_MCONFIG_BVALID))
6262306a36Sopenharmony_ci			continue;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci		size = get_bank_size(tmp);
6562306a36Sopenharmony_ci		addr = get_bank_addr(tmp);
6662306a36Sopenharmony_ci		printk(KERN_INFO " bank%d: %3ldM @ %08lx\n",
6762306a36Sopenharmony_ci			i, size / 1024 / 1024, addr);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci		if (addr >= SGIMC_SEG1_BADDR)
7062306a36Sopenharmony_ci			memblock_add(addr, size);
7162306a36Sopenharmony_ci	}
7262306a36Sopenharmony_ci}
7362306a36Sopenharmony_ci#endif
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_civoid __init sgimc_init(void)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	u32 tmp;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/* ioremap can't fail */
8062306a36Sopenharmony_ci	sgimc = (struct sgimc_regs *)
8162306a36Sopenharmony_ci		ioremap(SGIMC_BASE, sizeof(struct sgimc_regs));
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	printk(KERN_INFO "MC: SGI memory controller Revision %d\n",
8462306a36Sopenharmony_ci	       (int) sgimc->systemid & SGIMC_SYSID_MASKREV);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	/* Place the MC into a known state.  This must be done before
8762306a36Sopenharmony_ci	 * interrupts are first enabled etc.
8862306a36Sopenharmony_ci	 */
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/* Step 0: Make sure we turn off the watchdog in case it's
9162306a36Sopenharmony_ci	 *	   still running (which might be the case after a
9262306a36Sopenharmony_ci	 *	   soft reboot).
9362306a36Sopenharmony_ci	 */
9462306a36Sopenharmony_ci	tmp = sgimc->cpuctrl0;
9562306a36Sopenharmony_ci	tmp &= ~SGIMC_CCTRL0_WDOG;
9662306a36Sopenharmony_ci	sgimc->cpuctrl0 = tmp;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	/* Step 1: The CPU/GIO error status registers will not latch
9962306a36Sopenharmony_ci	 *	   up a new error status until the register has been
10062306a36Sopenharmony_ci	 *	   cleared by the cpu.	These status registers are
10162306a36Sopenharmony_ci	 *	   cleared by writing any value to them.
10262306a36Sopenharmony_ci	 */
10362306a36Sopenharmony_ci	sgimc->cstat = sgimc->gstat = 0;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	/* Step 2: Enable all parity checking in cpu control register
10662306a36Sopenharmony_ci	 *	   zero.
10762306a36Sopenharmony_ci	 */
10862306a36Sopenharmony_ci	/* don't touch parity settings for IP28 */
10962306a36Sopenharmony_ci	tmp = sgimc->cpuctrl0;
11062306a36Sopenharmony_ci#ifndef CONFIG_SGI_IP28
11162306a36Sopenharmony_ci	tmp |= SGIMC_CCTRL0_EPERRGIO | SGIMC_CCTRL0_EPERRMEM;
11262306a36Sopenharmony_ci#endif
11362306a36Sopenharmony_ci	tmp |= SGIMC_CCTRL0_R4KNOCHKPARR;
11462306a36Sopenharmony_ci	sgimc->cpuctrl0 = tmp;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	/* Step 3: Setup the MC write buffer depth, this is controlled
11762306a36Sopenharmony_ci	 *	   in cpu control register 1 in the lower 4 bits.
11862306a36Sopenharmony_ci	 */
11962306a36Sopenharmony_ci	tmp = sgimc->cpuctrl1;
12062306a36Sopenharmony_ci	tmp &= ~0xf;
12162306a36Sopenharmony_ci	tmp |= 0xd;
12262306a36Sopenharmony_ci	sgimc->cpuctrl1 = tmp;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/* Step 4: Initialize the RPSS divider register to run as fast
12562306a36Sopenharmony_ci	 *	   as it can correctly operate.	 The register is laid
12662306a36Sopenharmony_ci	 *	   out as follows:
12762306a36Sopenharmony_ci	 *
12862306a36Sopenharmony_ci	 *	   ----------------------------------------
12962306a36Sopenharmony_ci	 *	   |  RESERVED	|   INCREMENT	| DIVIDER |
13062306a36Sopenharmony_ci	 *	   ----------------------------------------
13162306a36Sopenharmony_ci	 *	    31	      16 15	       8 7	 0
13262306a36Sopenharmony_ci	 *
13362306a36Sopenharmony_ci	 *	   DIVIDER determines how often a 'tick' happens,
13462306a36Sopenharmony_ci	 *	   INCREMENT determines by how the RPSS increment
13562306a36Sopenharmony_ci	 *	   registers value increases at each 'tick'. Thus,
13662306a36Sopenharmony_ci	 *	   for IP22 we get INCREMENT=1, DIVIDER=1 == 0x101
13762306a36Sopenharmony_ci	 */
13862306a36Sopenharmony_ci	sgimc->divider = 0x101;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* Step 5: Initialize GIO64 arbitrator configuration register.
14162306a36Sopenharmony_ci	 *
14262306a36Sopenharmony_ci	 * NOTE: HPC init code in sgihpc_init() must run before us because
14362306a36Sopenharmony_ci	 *	 we need to know Guiness vs. FullHouse and the board
14462306a36Sopenharmony_ci	 *	 revision on this machine. You have been warned.
14562306a36Sopenharmony_ci	 */
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	/* First the basic invariants across all GIO64 implementations. */
14862306a36Sopenharmony_ci	tmp = sgimc->giopar & SGIMC_GIOPAR_GFX64; /* keep gfx 64bit settings */
14962306a36Sopenharmony_ci	tmp |= SGIMC_GIOPAR_HPC64;	/* All 1st HPC's interface at 64bits */
15062306a36Sopenharmony_ci	tmp |= SGIMC_GIOPAR_ONEBUS;	/* Only one physical GIO bus exists */
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	if (ip22_is_fullhouse()) {
15362306a36Sopenharmony_ci		/* Fullhouse specific settings. */
15462306a36Sopenharmony_ci		if (SGIOC_SYSID_BOARDREV(sgioc->sysid) < 2) {
15562306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_HPC264;	/* 2nd HPC at 64bits */
15662306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_PLINEEXP0;	/* exp0 pipelines */
15762306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_MASTEREXP1; /* exp1 masters */
15862306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_RTIMEEXP0;	/* exp0 is realtime */
15962306a36Sopenharmony_ci		} else {
16062306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_HPC264;	/* 2nd HPC 64bits */
16162306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_PLINEEXP0;	/* exp[01] pipelined */
16262306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_PLINEEXP1;
16362306a36Sopenharmony_ci			tmp |= SGIMC_GIOPAR_MASTEREISA; /* EISA masters */
16462306a36Sopenharmony_ci		}
16562306a36Sopenharmony_ci	} else {
16662306a36Sopenharmony_ci		/* Guiness specific settings. */
16762306a36Sopenharmony_ci		tmp |= SGIMC_GIOPAR_EISA64;	/* MC talks to EISA at 64bits */
16862306a36Sopenharmony_ci		tmp |= SGIMC_GIOPAR_MASTEREISA; /* EISA bus can act as master */
16962306a36Sopenharmony_ci	}
17062306a36Sopenharmony_ci	sgimc->giopar = tmp;	/* poof */
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	probe_memory();
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci#ifdef CONFIG_SGI_IP28
17662306a36Sopenharmony_civoid __init prom_cleanup(void)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	u32 mconfig1;
17962306a36Sopenharmony_ci	unsigned long flags;
18062306a36Sopenharmony_ci	spinlock_t lock;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	/*
18362306a36Sopenharmony_ci	 * because ARCS accesses memory uncached we wait until ARCS
18462306a36Sopenharmony_ci	 * isn't needed any longer, before we switch from slow to
18562306a36Sopenharmony_ci	 * normal mode
18662306a36Sopenharmony_ci	 */
18762306a36Sopenharmony_ci	spin_lock_irqsave(&lock, flags);
18862306a36Sopenharmony_ci	mconfig1 = sgimc->mconfig1;
18962306a36Sopenharmony_ci	/* map ECC register */
19062306a36Sopenharmony_ci	sgimc->mconfig1 = (mconfig1 & 0xffff0000) | 0x2060;
19162306a36Sopenharmony_ci	iob();
19262306a36Sopenharmony_ci	/* switch to normal mode */
19362306a36Sopenharmony_ci	*(unsigned long *)PHYS_TO_XKSEG_UNCACHED(0x60000000) = 0;
19462306a36Sopenharmony_ci	iob();
19562306a36Sopenharmony_ci	/* reduce WR_COL */
19662306a36Sopenharmony_ci	sgimc->cmacc = (sgimc->cmacc & ~0xf) | 4;
19762306a36Sopenharmony_ci	iob();
19862306a36Sopenharmony_ci	/* restore old config */
19962306a36Sopenharmony_ci	sgimc->mconfig1 = mconfig1;
20062306a36Sopenharmony_ci	iob();
20162306a36Sopenharmony_ci	spin_unlock_irqrestore(&lock, flags);
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci#endif
204