1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * ip22-mc.c: Routines for manipulating SGI Memory Controller.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
6*4882a593Smuzhiyun * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu) - Indigo2 changes
7*4882a593Smuzhiyun * Copyright (C) 2003 Ladislav Michl (ladis@linux-mips.org)
8*4882a593Smuzhiyun * Copyright (C) 2004 Peter Fuerst (pf@net.alphadv.de) - IP28
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/export.h>
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/memblock.h>
15*4882a593Smuzhiyun #include <linux/spinlock.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <asm/io.h>
18*4882a593Smuzhiyun #include <asm/bootinfo.h>
19*4882a593Smuzhiyun #include <asm/sgialib.h>
20*4882a593Smuzhiyun #include <asm/sgi/mc.h>
21*4882a593Smuzhiyun #include <asm/sgi/hpc3.h>
22*4882a593Smuzhiyun #include <asm/sgi/ip22.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun struct sgimc_regs *sgimc;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun EXPORT_SYMBOL(sgimc);
27*4882a593Smuzhiyun
get_bank_addr(unsigned int memconfig)28*4882a593Smuzhiyun static inline unsigned long get_bank_addr(unsigned int memconfig)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun return (memconfig & SGIMC_MCONFIG_BASEADDR) << ((sgimc->systemid & SGIMC_SYSID_MASKREV) >= 5 ? 24 : 22);
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun
get_bank_size(unsigned int memconfig)33*4882a593Smuzhiyun static inline unsigned long get_bank_size(unsigned int memconfig)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun return ((memconfig & SGIMC_MCONFIG_RMASK) + 0x0100) << ((sgimc->systemid & SGIMC_SYSID_MASKREV) >= 5 ? 16 : 14);
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
get_bank_config(int bank)38*4882a593Smuzhiyun static inline unsigned int get_bank_config(int bank)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun unsigned int res = bank > 1 ? sgimc->mconfig1 : sgimc->mconfig0;
41*4882a593Smuzhiyun return bank % 2 ? res & 0xffff : res >> 16;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #if defined(CONFIG_SGI_IP28) || defined(CONFIG_32BIT)
probe_memory(void)45*4882a593Smuzhiyun static void __init probe_memory(void)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun /* prom detects all usable memory */
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun #else
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * Detect installed memory, which PROM misses
52*4882a593Smuzhiyun */
probe_memory(void)53*4882a593Smuzhiyun static void __init probe_memory(void)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun unsigned long addr, size;
56*4882a593Smuzhiyun int i;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun printk(KERN_INFO "MC: Probing memory configuration:\n");
59*4882a593Smuzhiyun for (i = 0; i < 4; i++) {
60*4882a593Smuzhiyun unsigned int tmp = get_bank_config(i);
61*4882a593Smuzhiyun if (!(tmp & SGIMC_MCONFIG_BVALID))
62*4882a593Smuzhiyun continue;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun size = get_bank_size(tmp);
65*4882a593Smuzhiyun addr = get_bank_addr(tmp);
66*4882a593Smuzhiyun printk(KERN_INFO " bank%d: %3ldM @ %08lx\n",
67*4882a593Smuzhiyun i, size / 1024 / 1024, addr);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun if (addr >= SGIMC_SEG1_BADDR)
70*4882a593Smuzhiyun memblock_add(addr, size);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun
sgimc_init(void)75*4882a593Smuzhiyun void __init sgimc_init(void)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun u32 tmp;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* ioremap can't fail */
80*4882a593Smuzhiyun sgimc = (struct sgimc_regs *)
81*4882a593Smuzhiyun ioremap(SGIMC_BASE, sizeof(struct sgimc_regs));
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun printk(KERN_INFO "MC: SGI memory controller Revision %d\n",
84*4882a593Smuzhiyun (int) sgimc->systemid & SGIMC_SYSID_MASKREV);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* Place the MC into a known state. This must be done before
87*4882a593Smuzhiyun * interrupts are first enabled etc.
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* Step 0: Make sure we turn off the watchdog in case it's
91*4882a593Smuzhiyun * still running (which might be the case after a
92*4882a593Smuzhiyun * soft reboot).
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun tmp = sgimc->cpuctrl0;
95*4882a593Smuzhiyun tmp &= ~SGIMC_CCTRL0_WDOG;
96*4882a593Smuzhiyun sgimc->cpuctrl0 = tmp;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* Step 1: The CPU/GIO error status registers will not latch
99*4882a593Smuzhiyun * up a new error status until the register has been
100*4882a593Smuzhiyun * cleared by the cpu. These status registers are
101*4882a593Smuzhiyun * cleared by writing any value to them.
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun sgimc->cstat = sgimc->gstat = 0;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /* Step 2: Enable all parity checking in cpu control register
106*4882a593Smuzhiyun * zero.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun /* don't touch parity settings for IP28 */
109*4882a593Smuzhiyun tmp = sgimc->cpuctrl0;
110*4882a593Smuzhiyun #ifndef CONFIG_SGI_IP28
111*4882a593Smuzhiyun tmp |= SGIMC_CCTRL0_EPERRGIO | SGIMC_CCTRL0_EPERRMEM;
112*4882a593Smuzhiyun #endif
113*4882a593Smuzhiyun tmp |= SGIMC_CCTRL0_R4KNOCHKPARR;
114*4882a593Smuzhiyun sgimc->cpuctrl0 = tmp;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /* Step 3: Setup the MC write buffer depth, this is controlled
117*4882a593Smuzhiyun * in cpu control register 1 in the lower 4 bits.
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun tmp = sgimc->cpuctrl1;
120*4882a593Smuzhiyun tmp &= ~0xf;
121*4882a593Smuzhiyun tmp |= 0xd;
122*4882a593Smuzhiyun sgimc->cpuctrl1 = tmp;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /* Step 4: Initialize the RPSS divider register to run as fast
125*4882a593Smuzhiyun * as it can correctly operate. The register is laid
126*4882a593Smuzhiyun * out as follows:
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * ----------------------------------------
129*4882a593Smuzhiyun * | RESERVED | INCREMENT | DIVIDER |
130*4882a593Smuzhiyun * ----------------------------------------
131*4882a593Smuzhiyun * 31 16 15 8 7 0
132*4882a593Smuzhiyun *
133*4882a593Smuzhiyun * DIVIDER determines how often a 'tick' happens,
134*4882a593Smuzhiyun * INCREMENT determines by how the RPSS increment
135*4882a593Smuzhiyun * registers value increases at each 'tick'. Thus,
136*4882a593Smuzhiyun * for IP22 we get INCREMENT=1, DIVIDER=1 == 0x101
137*4882a593Smuzhiyun */
138*4882a593Smuzhiyun sgimc->divider = 0x101;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /* Step 5: Initialize GIO64 arbitrator configuration register.
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * NOTE: HPC init code in sgihpc_init() must run before us because
143*4882a593Smuzhiyun * we need to know Guiness vs. FullHouse and the board
144*4882a593Smuzhiyun * revision on this machine. You have been warned.
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* First the basic invariants across all GIO64 implementations. */
148*4882a593Smuzhiyun tmp = sgimc->giopar & SGIMC_GIOPAR_GFX64; /* keep gfx 64bit settings */
149*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_HPC64; /* All 1st HPC's interface at 64bits */
150*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_ONEBUS; /* Only one physical GIO bus exists */
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (ip22_is_fullhouse()) {
153*4882a593Smuzhiyun /* Fullhouse specific settings. */
154*4882a593Smuzhiyun if (SGIOC_SYSID_BOARDREV(sgioc->sysid) < 2) {
155*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_HPC264; /* 2nd HPC at 64bits */
156*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_PLINEEXP0; /* exp0 pipelines */
157*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_MASTEREXP1; /* exp1 masters */
158*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_RTIMEEXP0; /* exp0 is realtime */
159*4882a593Smuzhiyun } else {
160*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_HPC264; /* 2nd HPC 64bits */
161*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_PLINEEXP0; /* exp[01] pipelined */
162*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_PLINEEXP1;
163*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_MASTEREISA; /* EISA masters */
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun } else {
166*4882a593Smuzhiyun /* Guiness specific settings. */
167*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_EISA64; /* MC talks to EISA at 64bits */
168*4882a593Smuzhiyun tmp |= SGIMC_GIOPAR_MASTEREISA; /* EISA bus can act as master */
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun sgimc->giopar = tmp; /* poof */
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun probe_memory();
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun #ifdef CONFIG_SGI_IP28
prom_cleanup(void)176*4882a593Smuzhiyun void __init prom_cleanup(void)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun u32 mconfig1;
179*4882a593Smuzhiyun unsigned long flags;
180*4882a593Smuzhiyun spinlock_t lock;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * because ARCS accesses memory uncached we wait until ARCS
184*4882a593Smuzhiyun * isn't needed any longer, before we switch from slow to
185*4882a593Smuzhiyun * normal mode
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun spin_lock_irqsave(&lock, flags);
188*4882a593Smuzhiyun mconfig1 = sgimc->mconfig1;
189*4882a593Smuzhiyun /* map ECC register */
190*4882a593Smuzhiyun sgimc->mconfig1 = (mconfig1 & 0xffff0000) | 0x2060;
191*4882a593Smuzhiyun iob();
192*4882a593Smuzhiyun /* switch to normal mode */
193*4882a593Smuzhiyun *(unsigned long *)PHYS_TO_XKSEG_UNCACHED(0x60000000) = 0;
194*4882a593Smuzhiyun iob();
195*4882a593Smuzhiyun /* reduce WR_COL */
196*4882a593Smuzhiyun sgimc->cmacc = (sgimc->cmacc & ~0xf) | 4;
197*4882a593Smuzhiyun iob();
198*4882a593Smuzhiyun /* restore old config */
199*4882a593Smuzhiyun sgimc->mconfig1 = mconfig1;
200*4882a593Smuzhiyun iob();
201*4882a593Smuzhiyun spin_unlock_irqrestore(&lock, flags);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun #endif
204