xref: /rk3399_rockchip-uboot/arch/powerpc/lib/bat_rw.c (revision 8a33201d0c48c7b05a7b2ee978d3a56d1b2d81a7)
1a47a12beSStefan Roese /*
2a47a12beSStefan Roese  * (C) Copyright 2002
3a47a12beSStefan Roese  * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
4a47a12beSStefan Roese  *
5a47a12beSStefan Roese  * See file CREDITS for list of people who contributed to this
6a47a12beSStefan Roese  * project.
7a47a12beSStefan Roese  *
8a47a12beSStefan Roese  * This program is free software; you can redistribute it and/or
9a47a12beSStefan Roese  * modify it under the terms of the GNU General Public License as
10a47a12beSStefan Roese  * published by the Free Software Foundation; either version 2 of
11a47a12beSStefan Roese  * the License, or (at your option) any later version.
12a47a12beSStefan Roese  *
13a47a12beSStefan Roese  * This program is distributed in the hope that it will be useful,
14a47a12beSStefan Roese  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15a47a12beSStefan Roese  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16a47a12beSStefan Roese  * GNU General Public License for more details.
17a47a12beSStefan Roese  *
18a47a12beSStefan Roese  * You should have received a copy of the GNU General Public License
19a47a12beSStefan Roese  * along with this program; if not, write to the Free Software
20a47a12beSStefan Roese  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21a47a12beSStefan Roese  * MA 02111-1307 USA
22a47a12beSStefan Roese  *
23a47a12beSStefan Roese  */
24a47a12beSStefan Roese 
25a47a12beSStefan Roese #include <common.h>
26a47a12beSStefan Roese #include <asm/processor.h>
27a47a12beSStefan Roese #include <asm/mmu.h>
28a47a12beSStefan Roese #include <asm/io.h>
29*8a33201dSWolfgang Denk #include <linux/compiler.h>
30a47a12beSStefan Roese 
31a47a12beSStefan Roese #ifdef CONFIG_ADDR_MAP
32a47a12beSStefan Roese #include <addr_map.h>
33a47a12beSStefan Roese #endif
34a47a12beSStefan Roese 
35a47a12beSStefan Roese DECLARE_GLOBAL_DATA_PTR;
36a47a12beSStefan Roese 
37a47a12beSStefan Roese int write_bat (ppc_bat_t bat, unsigned long upper, unsigned long lower)
38a47a12beSStefan Roese {
39*8a33201dSWolfgang Denk 	__maybe_unused int batn = -1;
40a47a12beSStefan Roese 
41a47a12beSStefan Roese 	sync();
42a47a12beSStefan Roese 
43a47a12beSStefan Roese 	switch (bat) {
44a47a12beSStefan Roese 	case DBAT0:
45a47a12beSStefan Roese 		mtspr (DBAT0L, lower);
46a47a12beSStefan Roese 		mtspr (DBAT0U, upper);
47a47a12beSStefan Roese 		batn = 0;
48a47a12beSStefan Roese 		break;
49a47a12beSStefan Roese 	case IBAT0:
50a47a12beSStefan Roese 		mtspr (IBAT0L, lower);
51a47a12beSStefan Roese 		mtspr (IBAT0U, upper);
52a47a12beSStefan Roese 		break;
53a47a12beSStefan Roese 	case DBAT1:
54a47a12beSStefan Roese 		mtspr (DBAT1L, lower);
55a47a12beSStefan Roese 		mtspr (DBAT1U, upper);
56a47a12beSStefan Roese 		batn = 1;
57a47a12beSStefan Roese 		break;
58a47a12beSStefan Roese 	case IBAT1:
59a47a12beSStefan Roese 		mtspr (IBAT1L, lower);
60a47a12beSStefan Roese 		mtspr (IBAT1U, upper);
61a47a12beSStefan Roese 		break;
62a47a12beSStefan Roese 	case DBAT2:
63a47a12beSStefan Roese 		mtspr (DBAT2L, lower);
64a47a12beSStefan Roese 		mtspr (DBAT2U, upper);
65a47a12beSStefan Roese 		batn = 2;
66a47a12beSStefan Roese 		break;
67a47a12beSStefan Roese 	case IBAT2:
68a47a12beSStefan Roese 		mtspr (IBAT2L, lower);
69a47a12beSStefan Roese 		mtspr (IBAT2U, upper);
70a47a12beSStefan Roese 		break;
71a47a12beSStefan Roese 	case DBAT3:
72a47a12beSStefan Roese 		mtspr (DBAT3L, lower);
73a47a12beSStefan Roese 		mtspr (DBAT3U, upper);
74a47a12beSStefan Roese 		batn = 3;
75a47a12beSStefan Roese 		break;
76a47a12beSStefan Roese 	case IBAT3:
77a47a12beSStefan Roese 		mtspr (IBAT3L, lower);
78a47a12beSStefan Roese 		mtspr (IBAT3U, upper);
79a47a12beSStefan Roese 		break;
80a47a12beSStefan Roese #ifdef CONFIG_HIGH_BATS
81a47a12beSStefan Roese 	case DBAT4:
82a47a12beSStefan Roese 		mtspr (DBAT4L, lower);
83a47a12beSStefan Roese 		mtspr (DBAT4U, upper);
84a47a12beSStefan Roese 		batn = 4;
85a47a12beSStefan Roese 		break;
86a47a12beSStefan Roese 	case IBAT4:
87a47a12beSStefan Roese 		mtspr (IBAT4L, lower);
88a47a12beSStefan Roese 		mtspr (IBAT4U, upper);
89a47a12beSStefan Roese 		break;
90a47a12beSStefan Roese 	case DBAT5:
91a47a12beSStefan Roese 		mtspr (DBAT5L, lower);
92a47a12beSStefan Roese 		mtspr (DBAT5U, upper);
93a47a12beSStefan Roese 		batn = 5;
94a47a12beSStefan Roese 		break;
95a47a12beSStefan Roese 	case IBAT5:
96a47a12beSStefan Roese 		mtspr (IBAT5L, lower);
97a47a12beSStefan Roese 		mtspr (IBAT5U, upper);
98a47a12beSStefan Roese 		break;
99a47a12beSStefan Roese 	case DBAT6:
100a47a12beSStefan Roese 		mtspr (DBAT6L, lower);
101a47a12beSStefan Roese 		mtspr (DBAT6U, upper);
102a47a12beSStefan Roese 		batn = 6;
103a47a12beSStefan Roese 		break;
104a47a12beSStefan Roese 	case IBAT6:
105a47a12beSStefan Roese 		mtspr (IBAT6L, lower);
106a47a12beSStefan Roese 		mtspr (IBAT6U, upper);
107a47a12beSStefan Roese 		break;
108a47a12beSStefan Roese 	case DBAT7:
109a47a12beSStefan Roese 		mtspr (DBAT7L, lower);
110a47a12beSStefan Roese 		mtspr (DBAT7U, upper);
111a47a12beSStefan Roese 		batn = 7;
112a47a12beSStefan Roese 		break;
113a47a12beSStefan Roese 	case IBAT7:
114a47a12beSStefan Roese 		mtspr (IBAT7L, lower);
115a47a12beSStefan Roese 		mtspr (IBAT7U, upper);
116a47a12beSStefan Roese 		break;
117a47a12beSStefan Roese #endif
118a47a12beSStefan Roese 	default:
119a47a12beSStefan Roese 		return (-1);
120a47a12beSStefan Roese 	}
121a47a12beSStefan Roese 
122a47a12beSStefan Roese #ifdef CONFIG_ADDR_MAP
123a47a12beSStefan Roese 	if ((gd->flags & GD_FLG_RELOC) && (batn >= 0)) {
124a47a12beSStefan Roese 		phys_size_t size;
125a47a12beSStefan Roese 		if (!BATU_VALID(upper))
126a47a12beSStefan Roese 			size = 0;
127a47a12beSStefan Roese 		else
128a47a12beSStefan Roese 			size = BATU_SIZE(upper);
129a47a12beSStefan Roese 		addrmap_set_entry(BATU_VADDR(upper), BATL_PADDR(lower),
130a47a12beSStefan Roese 				  size, batn);
131a47a12beSStefan Roese 	}
132a47a12beSStefan Roese #endif
133a47a12beSStefan Roese 
134a47a12beSStefan Roese 	sync();
135a47a12beSStefan Roese 	isync();
136a47a12beSStefan Roese 
137a47a12beSStefan Roese 	return (0);
138a47a12beSStefan Roese }
139a47a12beSStefan Roese 
140a47a12beSStefan Roese int read_bat (ppc_bat_t bat, unsigned long *upper, unsigned long *lower)
141a47a12beSStefan Roese {
142a47a12beSStefan Roese 	unsigned long register u;
143a47a12beSStefan Roese 	unsigned long register l;
144a47a12beSStefan Roese 
145a47a12beSStefan Roese 	switch (bat) {
146a47a12beSStefan Roese 	case DBAT0:
147a47a12beSStefan Roese 		l = mfspr (DBAT0L);
148a47a12beSStefan Roese 		u = mfspr (DBAT0U);
149a47a12beSStefan Roese 		break;
150a47a12beSStefan Roese 	case IBAT0:
151a47a12beSStefan Roese 		l = mfspr (IBAT0L);
152a47a12beSStefan Roese 		u = mfspr (IBAT0U);
153a47a12beSStefan Roese 		break;
154a47a12beSStefan Roese 	case DBAT1:
155a47a12beSStefan Roese 		l = mfspr (DBAT1L);
156a47a12beSStefan Roese 		u = mfspr (DBAT1U);
157a47a12beSStefan Roese 		break;
158a47a12beSStefan Roese 	case IBAT1:
159a47a12beSStefan Roese 		l = mfspr (IBAT1L);
160a47a12beSStefan Roese 		u = mfspr (IBAT1U);
161a47a12beSStefan Roese 		break;
162a47a12beSStefan Roese 	case DBAT2:
163a47a12beSStefan Roese 		l = mfspr (DBAT2L);
164a47a12beSStefan Roese 		u = mfspr (DBAT2U);
165a47a12beSStefan Roese 		break;
166a47a12beSStefan Roese 	case IBAT2:
167a47a12beSStefan Roese 		l = mfspr (IBAT2L);
168a47a12beSStefan Roese 		u = mfspr (IBAT2U);
169a47a12beSStefan Roese 		break;
170a47a12beSStefan Roese 	case DBAT3:
171a47a12beSStefan Roese 		l = mfspr (DBAT3L);
172a47a12beSStefan Roese 		u = mfspr (DBAT3U);
173a47a12beSStefan Roese 		break;
174a47a12beSStefan Roese 	case IBAT3:
175a47a12beSStefan Roese 		l = mfspr (IBAT3L);
176a47a12beSStefan Roese 		u = mfspr (IBAT3U);
177a47a12beSStefan Roese 		break;
178a47a12beSStefan Roese #ifdef CONFIG_HIGH_BATS
179a47a12beSStefan Roese 	case DBAT4:
180a47a12beSStefan Roese 		l = mfspr (DBAT4L);
181a47a12beSStefan Roese 		u = mfspr (DBAT4U);
182a47a12beSStefan Roese 		break;
183a47a12beSStefan Roese 	case IBAT4:
184a47a12beSStefan Roese 		l = mfspr (IBAT4L);
185a47a12beSStefan Roese 		u = mfspr (IBAT4U);
186a47a12beSStefan Roese 		break;
187a47a12beSStefan Roese 	case DBAT5:
188a47a12beSStefan Roese 		l = mfspr (DBAT5L);
189a47a12beSStefan Roese 		u = mfspr (DBAT5U);
190a47a12beSStefan Roese 		break;
191a47a12beSStefan Roese 	case IBAT5:
192a47a12beSStefan Roese 		l = mfspr (IBAT5L);
193a47a12beSStefan Roese 		u = mfspr (IBAT5U);
194a47a12beSStefan Roese 		break;
195a47a12beSStefan Roese 	case DBAT6:
196a47a12beSStefan Roese 		l = mfspr (DBAT6L);
197a47a12beSStefan Roese 		u = mfspr (DBAT6U);
198a47a12beSStefan Roese 		break;
199a47a12beSStefan Roese 	case IBAT6:
200a47a12beSStefan Roese 		l = mfspr (IBAT6L);
201a47a12beSStefan Roese 		u = mfspr (IBAT6U);
202a47a12beSStefan Roese 		break;
203a47a12beSStefan Roese 	case DBAT7:
204a47a12beSStefan Roese 		l = mfspr (DBAT7L);
205a47a12beSStefan Roese 		u = mfspr (DBAT7U);
206a47a12beSStefan Roese 		break;
207a47a12beSStefan Roese 	case IBAT7:
208a47a12beSStefan Roese 		l = mfspr (IBAT7L);
209a47a12beSStefan Roese 		u = mfspr (IBAT7U);
210a47a12beSStefan Roese 		break;
211a47a12beSStefan Roese #endif
212a47a12beSStefan Roese 	default:
213a47a12beSStefan Roese 		return (-1);
214a47a12beSStefan Roese 	}
215a47a12beSStefan Roese 
216a47a12beSStefan Roese 	*upper = u;
217a47a12beSStefan Roese 	*lower = l;
218a47a12beSStefan Roese 
219a47a12beSStefan Roese 	return (0);
220a47a12beSStefan Roese }
221a47a12beSStefan Roese 
222a47a12beSStefan Roese void print_bats(void)
223a47a12beSStefan Roese {
224a47a12beSStefan Roese 	printf("BAT registers:\n");
225a47a12beSStefan Roese 
226a47a12beSStefan Roese 	printf ("\tIBAT0L = 0x%08X ", mfspr (IBAT0L));
227a47a12beSStefan Roese 	printf ("\tIBAT0U = 0x%08X\n", mfspr (IBAT0U));
228a47a12beSStefan Roese 	printf ("\tDBAT0L = 0x%08X ", mfspr (DBAT0L));
229a47a12beSStefan Roese 	printf ("\tDBAT0U = 0x%08X\n", mfspr (DBAT0U));
230a47a12beSStefan Roese 	printf ("\tIBAT1L = 0x%08X ", mfspr (IBAT1L));
231a47a12beSStefan Roese 	printf ("\tIBAT1U = 0x%08X\n", mfspr (IBAT1U));
232a47a12beSStefan Roese 	printf ("\tDBAT1L = 0x%08X ", mfspr (DBAT1L));
233a47a12beSStefan Roese 	printf ("\tDBAT1U = 0x%08X\n", mfspr (DBAT1U));
234a47a12beSStefan Roese 	printf ("\tIBAT2L = 0x%08X ", mfspr (IBAT2L));
235a47a12beSStefan Roese 	printf ("\tIBAT2U = 0x%08X\n", mfspr (IBAT2U));
236a47a12beSStefan Roese 	printf ("\tDBAT2L = 0x%08X ", mfspr (DBAT2L));
237a47a12beSStefan Roese 	printf ("\tDBAT2U = 0x%08X\n", mfspr (DBAT2U));
238a47a12beSStefan Roese 	printf ("\tIBAT3L = 0x%08X ", mfspr (IBAT3L));
239a47a12beSStefan Roese 	printf ("\tIBAT3U = 0x%08X\n", mfspr (IBAT3U));
240a47a12beSStefan Roese 	printf ("\tDBAT3L = 0x%08X ", mfspr (DBAT3L));
241a47a12beSStefan Roese 	printf ("\tDBAT3U = 0x%08X\n", mfspr (DBAT3U));
242a47a12beSStefan Roese 
243a47a12beSStefan Roese #ifdef CONFIG_HIGH_BATS
244a47a12beSStefan Roese 	printf ("\tIBAT4L = 0x%08X ", mfspr (IBAT4L));
245a47a12beSStefan Roese 	printf ("\tIBAT4U = 0x%08X\n", mfspr (IBAT4U));
246a47a12beSStefan Roese 	printf ("\tDBAT4L = 0x%08X ", mfspr (DBAT4L));
247a47a12beSStefan Roese 	printf ("\tDBAT4U = 0x%08X\n", mfspr (DBAT4U));
248a47a12beSStefan Roese 	printf ("\tIBAT5L = 0x%08X ", mfspr (IBAT5L));
249a47a12beSStefan Roese 	printf ("\tIBAT5U = 0x%08X\n", mfspr (IBAT5U));
250a47a12beSStefan Roese 	printf ("\tDBAT5L = 0x%08X ", mfspr (DBAT5L));
251a47a12beSStefan Roese 	printf ("\tDBAT5U = 0x%08X\n", mfspr (DBAT5U));
252a47a12beSStefan Roese 	printf ("\tIBAT6L = 0x%08X ", mfspr (IBAT6L));
253a47a12beSStefan Roese 	printf ("\tIBAT6U = 0x%08X\n", mfspr (IBAT6U));
254a47a12beSStefan Roese 	printf ("\tDBAT6L = 0x%08X ", mfspr (DBAT6L));
255a47a12beSStefan Roese 	printf ("\tDBAT6U = 0x%08X\n", mfspr (DBAT6U));
256a47a12beSStefan Roese 	printf ("\tIBAT7L = 0x%08X ", mfspr (IBAT7L));
257a47a12beSStefan Roese 	printf ("\tIBAT7U = 0x%08X\n", mfspr (IBAT7U));
258a47a12beSStefan Roese 	printf ("\tDBAT7L = 0x%08X ", mfspr (DBAT7L));
259a47a12beSStefan Roese 	printf ("\tDBAT7U = 0x%08X\n", mfspr (DBAT7U));
260a47a12beSStefan Roese #endif
261a47a12beSStefan Roese }
262