1/* $NetBSD: wii_mmuinit.S,v 1.1 2024/01/20 21:36:00 jmcneill Exp $ */
2
3/*-
4 * Copyright (C) 2012 Margarida Gouveia
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30#include <powerpc/oea/bat.h>
31
32/*
33 * When we are invoked from Wii loaders, the state of the MMU and the BAT
34 * mappings can vary.  In this file we try to reset the MMU to a state
35 * that lets us boot NetBSD.
36 *
37 * This file is being included from wii_locore.S.
38 */
39
40#define	MMU_REALMODE()				\
41	mfmsr	%r12;				\
42	rlwinm	%r12, %r12, 0, ~(PSL_DR|PSL_IR);\
43	sync;					\
44	bl	1f;				\
451:						\
46	mflr    %r11;				\
47	clrlwi  %r11, %r11, 3;	/* XXX why? */	\
48	addi    %r11, %r11, 2f - 1b;		\
49	mtsrr0  %r11;				\
50	mtsrr1	%r12;	/* Disables the MMU */	\
51	isync;					\
52	rfi;					\
532:
54
55#define	MMU_VIRTUALMODE()			\
56	bl	3f;				\
573:						\
58	mflr	%r11;				\
59	addi	%r11, %r11, 4f - 3b;		\
60	mfmsr	%r12;				\
61	ori	%r12, %r12, PSL_DR|PSL_IR;	\
62	mtsrr0	%r11;				\
63	mtsrr1	%r12;	/* Enables the MMU */	\
64	isync;					\
65	rfi;					\
664:
67
68	MMU_REALMODE()
69
70	/* Reset standard BATs */
71	li	%r11, 0
72	mtibatu 0, %r11
73	mtibatl 0, %r11
74	mtdbatu 0, %r11
75	mtdbatl 0, %r11
76	mtibatu 1, %r11
77	mtibatl 1, %r11
78	mtdbatu 1, %r11
79	mtdbatl 1, %r11
80	mtibatu 2, %r11
81	mtibatl 2, %r11
82	mtdbatu 2, %r11
83	mtdbatl 2, %r11
84	mtibatu 3, %r11
85	mtibatl 3, %r11
86	mtdbatu 3, %r11
87	mtdbatl 3, %r11
88
89	/* Reset high BATs. IBAT[4-7][UL] + DBAT[4-7][UL] */
90	mtspr	560, %r11
91	mtspr	561, %r11
92	mtspr	562, %r11
93	mtspr	563, %r11
94	mtspr	564, %r11
95	mtspr	565, %r11
96	mtspr	566, %r11
97	mtspr	567, %r11
98	mtspr	568, %r11
99	mtspr	569, %r11
100	mtspr	570, %r11
101	mtspr	571, %r11
102	mtspr	572, %r11
103	mtspr	573, %r11
104	mtspr	574, %r11
105	mtspr	575, %r11
106
107	/*
108	 * We need to setup BAT0 as in mmu_oea.c.
109	 */
110	li	%r11, BATU(0x00000000, BAT_BL_256M, BAT_Vs)
111	li	%r12, BATL(0x00000000, BAT_M, BAT_PP_RW)
112	mtdbatu	0, %r11
113	mtdbatl	0, %r12
114	mtibatu	0, %r11
115	mtibatl	0, %r12
116	isync
117
118	/*
119	 * We use BAT1 to be able to write I/O memory, including the
120	 * framebuffer registers.
121	 */
122	/* BATU(0x0c000000, BAT_BL_32M, BAT_Vs) */
123	lis	%r11, 0x0c00
124	ori	%r11, %r11, BAT_BL_32M|BAT_Vs
125	/* BATL(0x0c000000, BAT_I|BAT_G, BAT_PP_RW) */
126	lis	%r12, 0x0c00
127	ori	%r12, %r12, BAT_I|BAT_G|BAT_PP_RW
128	mtdbatu	1, %r11
129	mtdbatl	1, %r12
130	isync
131
132	MMU_VIRTUALMODE()
133