1/*
2 * This file contains low level CPU setup functions.
3 *    Copyright (C) 2003 Benjamin Herrenschmidt (benh@kernel.crashing.org)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 */
11
12#include <asm/processor.h>
13#include <asm/page.h>
14#include <asm/cputable.h>
15#include <asm/ppc_asm.h>
16#include <asm/asm-offsets.h>
17#include <asm/cache.h>
18#include <asm/mmu.h>
19
20_GLOBAL(__setup_cpu_603)
21	mflr	r4
22BEGIN_MMU_FTR_SECTION
23	li	r10,0
24	mtspr	SPRN_SPRG4,r10		/* init SW LRU tracking */
25END_MMU_FTR_SECTION_IFSET(MMU_FTR_NEED_DTLB_SW_LRU)
26BEGIN_FTR_SECTION
27	bl	__init_fpu_registers
28END_FTR_SECTION_IFCLR(CPU_FTR_FPU_UNAVAILABLE)
29	bl	setup_common_caches
30	mtlr	r4
31	blr
32_GLOBAL(__setup_cpu_604)
33	mflr	r4
34	bl	setup_common_caches
35	bl	setup_604_hid0
36	mtlr	r4
37	blr
38_GLOBAL(__setup_cpu_750)
39	mflr	r4
40	bl	__init_fpu_registers
41	bl	setup_common_caches
42	bl	setup_750_7400_hid0
43	mtlr	r4
44	blr
45_GLOBAL(__setup_cpu_750cx)
46	mflr	r4
47	bl	__init_fpu_registers
48	bl	setup_common_caches
49	bl	setup_750_7400_hid0
50	bl	setup_750cx
51	mtlr	r4
52	blr
53_GLOBAL(__setup_cpu_750fx)
54	mflr	r4
55	bl	__init_fpu_registers
56	bl	setup_common_caches
57	bl	setup_750_7400_hid0
58	bl	setup_750fx
59	mtlr	r4
60	blr
61_GLOBAL(__setup_cpu_7400)
62	mflr	r4
63	bl	__init_fpu_registers
64	bl	setup_7400_workarounds
65	bl	setup_common_caches
66	bl	setup_750_7400_hid0
67	mtlr	r4
68	blr
69_GLOBAL(__setup_cpu_7410)
70	mflr	r4
71	bl	__init_fpu_registers
72	bl	setup_7410_workarounds
73	bl	setup_common_caches
74	bl	setup_750_7400_hid0
75	li	r3,0
76	mtspr	SPRN_L2CR2,r3
77	mtlr	r4
78	blr
79_GLOBAL(__setup_cpu_745x)
80	mflr	r4
81	bl	setup_common_caches
82	bl	setup_745x_specifics
83	mtlr	r4
84	blr
85
86/* Enable caches for 603's, 604, 750 & 7400 */
87setup_common_caches:
88	mfspr	r11,SPRN_HID0
89	andi.	r0,r11,HID0_DCE
90	ori	r11,r11,HID0_ICE|HID0_DCE
91	ori	r8,r11,HID0_ICFI
92	bne	1f			/* don't invalidate the D-cache */
93	ori	r8,r8,HID0_DCI		/* unless it wasn't enabled */
941:	sync
95	mtspr	SPRN_HID0,r8		/* enable and invalidate caches */
96	sync
97	mtspr	SPRN_HID0,r11		/* enable caches */
98	sync
99	isync
100	blr
101
102/* 604, 604e, 604ev, ...
103 * Enable superscalar execution & branch history table
104 */
105setup_604_hid0:
106	mfspr	r11,SPRN_HID0
107	ori	r11,r11,HID0_SIED|HID0_BHTE
108	ori	r8,r11,HID0_BTCD
109	sync
110	mtspr	SPRN_HID0,r8	/* flush branch target address cache */
111	sync			/* on 604e/604r */
112	mtspr	SPRN_HID0,r11
113	sync
114	isync
115	blr
116
117/* 7400 <= rev 2.7 and 7410 rev = 1.0 suffer from some
118 * erratas we work around here.
119 * Moto MPC710CE.pdf describes them, those are errata
120 * #3, #4 and #5
121 * Note that we assume the firmware didn't choose to
122 * apply other workarounds (there are other ones documented
123 * in the .pdf). It appear that Apple firmware only works
124 * around #3 and with the same fix we use. We may want to
125 * check if the CPU is using 60x bus mode in which case
126 * the workaround for errata #4 is useless. Also, we may
127 * want to explicitly clear HID0_NOPDST as this is not
128 * needed once we have applied workaround #5 (though it's
129 * not set by Apple's firmware at least).
130 */
131setup_7400_workarounds:
132	mfpvr	r3
133	rlwinm	r3,r3,0,20,31
134	cmpwi	0,r3,0x0207
135	ble	1f
136	blr
137setup_7410_workarounds:
138	mfpvr	r3
139	rlwinm	r3,r3,0,20,31
140	cmpwi	0,r3,0x0100
141	bnelr
1421:
143	mfspr	r11,SPRN_MSSSR0
144	/* Errata #3: Set L1OPQ_SIZE to 0x10 */
145	rlwinm	r11,r11,0,9,6
146	oris	r11,r11,0x0100
147	/* Errata #4: Set L2MQ_SIZE to 1 (check for MPX mode first ?) */
148	oris	r11,r11,0x0002
149	/* Errata #5: Set DRLT_SIZE to 0x01 */
150	rlwinm	r11,r11,0,5,2
151	oris	r11,r11,0x0800
152	sync
153	mtspr	SPRN_MSSSR0,r11
154	sync
155	isync
156	blr
157
158/* 740/750/7400/7410
159 * Enable Store Gathering (SGE), Address Brodcast (ABE),
160 * Branch History Table (BHTE), Branch Target ICache (BTIC)
161 * Dynamic Power Management (DPM), Speculative (SPD)
162 * Clear Instruction cache throttling (ICTC)
163 */
164setup_750_7400_hid0:
165	mfspr	r11,SPRN_HID0
166	ori	r11,r11,HID0_SGE | HID0_ABE | HID0_BHTE | HID0_BTIC
167	oris	r11,r11,HID0_DPM@h
168BEGIN_FTR_SECTION
169	xori	r11,r11,HID0_BTIC
170END_FTR_SECTION_IFSET(CPU_FTR_NO_BTIC)
171BEGIN_FTR_SECTION
172	xoris	r11,r11,HID0_DPM@h	/* disable dynamic power mgmt */
173END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
174	li	r3,HID0_SPD
175	andc	r11,r11,r3		/* clear SPD: enable speculative */
176 	li	r3,0
177 	mtspr	SPRN_ICTC,r3		/* Instruction Cache Throttling off */
178	isync
179	mtspr	SPRN_HID0,r11
180	sync
181	isync
182	blr
183
184/* 750cx specific
185 * Looks like we have to disable NAP feature for some PLL settings...
186 * (waiting for confirmation)
187 */
188setup_750cx:
189	mfspr	r10, SPRN_HID1
190	rlwinm	r10,r10,4,28,31
191	cmpwi	cr0,r10,7
192	cmpwi	cr1,r10,9
193	cmpwi	cr2,r10,11
194	cror	4*cr0+eq,4*cr0+eq,4*cr1+eq
195	cror	4*cr0+eq,4*cr0+eq,4*cr2+eq
196	bnelr
197	lwz	r6,CPU_SPEC_FEATURES(r5)
198	li	r7,CPU_FTR_CAN_NAP
199	andc	r6,r6,r7
200	stw	r6,CPU_SPEC_FEATURES(r5)
201	blr
202
203/* 750fx specific
204 */
205setup_750fx:
206	blr
207
208/* MPC 745x
209 * Enable Store Gathering (SGE), Branch Folding (FOLD)
210 * Branch History Table (BHTE), Branch Target ICache (BTIC)
211 * Dynamic Power Management (DPM), Speculative (SPD)
212 * Ensure our data cache instructions really operate.
213 * Timebase has to be running or we wouldn't have made it here,
214 * just ensure we don't disable it.
215 * Clear Instruction cache throttling (ICTC)
216 * Enable L2 HW prefetch
217 */
218setup_745x_specifics:
219	/* We check for the presence of an L3 cache setup by
220	 * the firmware. If any, we disable NAP capability as
221	 * it's known to be bogus on rev 2.1 and earlier
222	 */
223BEGIN_FTR_SECTION
224	mfspr	r11,SPRN_L3CR
225	andis.	r11,r11,L3CR_L3E@h
226	beq	1f
227END_FTR_SECTION_IFSET(CPU_FTR_L3CR)
228	lwz	r6,CPU_SPEC_FEATURES(r5)
229	andi.	r0,r6,CPU_FTR_L3_DISABLE_NAP
230	beq	1f
231	li	r7,CPU_FTR_CAN_NAP
232	andc	r6,r6,r7
233	stw	r6,CPU_SPEC_FEATURES(r5)
2341:
235	mfspr	r11,SPRN_HID0
236
237	/* All of the bits we have to set.....
238	 */
239	ori	r11,r11,HID0_SGE | HID0_FOLD | HID0_BHTE
240	ori	r11,r11,HID0_LRSTK | HID0_BTIC
241	oris	r11,r11,HID0_DPM@h
242BEGIN_FTR_SECTION
243	xori	r11,r11,HID0_BTIC
244END_FTR_SECTION_IFSET(CPU_FTR_NO_BTIC)
245BEGIN_FTR_SECTION
246	xoris	r11,r11,HID0_DPM@h	/* disable dynamic power mgmt */
247END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
248
249	/* All of the bits we have to clear....
250	 */
251	li	r3,HID0_SPD | HID0_NOPDST | HID0_NOPTI
252	andc	r11,r11,r3		/* clear SPD: enable speculative */
253 	li	r3,0
254
255 	mtspr	SPRN_ICTC,r3		/* Instruction Cache Throttling off */
256	isync
257	mtspr	SPRN_HID0,r11
258	sync
259	isync
260
261	/* Enable L2 HW prefetch, if L2 is enabled
262	 */
263	mfspr	r3,SPRN_L2CR
264	andis.	r3,r3,L2CR_L2E@h
265	beqlr
266	mfspr	r3,SPRN_MSSCR0
267	ori	r3,r3,3
268	sync
269	mtspr	SPRN_MSSCR0,r3
270	sync
271	isync
272	blr
273
274/*
275 * Initialize the FPU registers. This is needed to work around an errata
276 * in some 750 cpus where using a not yet initialized FPU register after
277 * power on reset may hang the CPU
278 */
279_GLOBAL(__init_fpu_registers)
280	mfmsr	r10
281	ori	r11,r10,MSR_FP
282	mtmsr	r11
283	isync
284	addis	r9,r3,empty_zero_page@ha
285	addi	r9,r9,empty_zero_page@l
286	REST_32FPRS(0,r9)
287	sync
288	mtmsr	r10
289	isync
290	blr
291
292
293/* Definitions for the table use to save CPU states */
294#define CS_HID0		0
295#define CS_HID1		4
296#define CS_HID2		8
297#define	CS_MSSCR0	12
298#define CS_MSSSR0	16
299#define CS_ICTRL	20
300#define CS_LDSTCR	24
301#define CS_LDSTDB	28
302#define CS_SIZE		32
303
304	.data
305	.balign	L1_CACHE_BYTES
306cpu_state_storage:
307	.space	CS_SIZE
308	.balign	L1_CACHE_BYTES,0
309	.text
310
311/* Called in normal context to backup CPU 0 state. This
312 * does not include cache settings. This function is also
313 * called for machine sleep. This does not include the MMU
314 * setup, BATs, etc... but rather the "special" registers
315 * like HID0, HID1, MSSCR0, etc...
316 */
317_GLOBAL(__save_cpu_setup)
318	/* Some CR fields are volatile, we back it up all */
319	mfcr	r7
320
321	/* Get storage ptr */
322	lis	r5,cpu_state_storage@h
323	ori	r5,r5,cpu_state_storage@l
324
325	/* Save HID0 (common to all CONFIG_6xx cpus) */
326	mfspr	r3,SPRN_HID0
327	stw	r3,CS_HID0(r5)
328
329	/* Now deal with CPU type dependent registers */
330	mfspr	r3,SPRN_PVR
331	srwi	r3,r3,16
332	cmplwi	cr0,r3,0x8000	/* 7450 */
333	cmplwi	cr1,r3,0x000c	/* 7400 */
334	cmplwi	cr2,r3,0x800c	/* 7410 */
335	cmplwi	cr3,r3,0x8001	/* 7455 */
336	cmplwi	cr4,r3,0x8002	/* 7457 */
337	cmplwi	cr5,r3,0x8003	/* 7447A */
338	cmplwi	cr6,r3,0x7000	/* 750FX */
339	cmplwi	cr7,r3,0x8004	/* 7448 */
340	/* cr1 is 7400 || 7410 */
341	cror	4*cr1+eq,4*cr1+eq,4*cr2+eq
342	/* cr0 is 74xx */
343	cror	4*cr0+eq,4*cr0+eq,4*cr3+eq
344	cror	4*cr0+eq,4*cr0+eq,4*cr4+eq
345	cror	4*cr0+eq,4*cr0+eq,4*cr1+eq
346	cror	4*cr0+eq,4*cr0+eq,4*cr5+eq
347	cror	4*cr0+eq,4*cr0+eq,4*cr7+eq
348	bne	1f
349	/* Backup 74xx specific regs */
350	mfspr	r4,SPRN_MSSCR0
351	stw	r4,CS_MSSCR0(r5)
352	mfspr	r4,SPRN_MSSSR0
353	stw	r4,CS_MSSSR0(r5)
354	beq	cr1,1f
355	/* Backup 745x specific registers */
356	mfspr	r4,SPRN_HID1
357	stw	r4,CS_HID1(r5)
358	mfspr	r4,SPRN_ICTRL
359	stw	r4,CS_ICTRL(r5)
360	mfspr	r4,SPRN_LDSTCR
361	stw	r4,CS_LDSTCR(r5)
362	mfspr	r4,SPRN_LDSTDB
363	stw	r4,CS_LDSTDB(r5)
3641:
365	bne	cr6,1f
366	/* Backup 750FX specific registers */
367	mfspr	r4,SPRN_HID1
368	stw	r4,CS_HID1(r5)
369	/* If rev 2.x, backup HID2 */
370	mfspr	r3,SPRN_PVR
371	andi.	r3,r3,0xff00
372	cmpwi	cr0,r3,0x0200
373	bne	1f
374	mfspr	r4,SPRN_HID2
375	stw	r4,CS_HID2(r5)
3761:
377	mtcr	r7
378	blr
379
380/* Called with no MMU context (typically MSR:IR/DR off) to
381 * restore CPU state as backed up by the previous
382 * function. This does not include cache setting
383 */
384_GLOBAL(__restore_cpu_setup)
385	/* Some CR fields are volatile, we back it up all */
386	mfcr	r7
387
388	/* Get storage ptr */
389	lis	r5,(cpu_state_storage-KERNELBASE)@h
390	ori	r5,r5,cpu_state_storage@l
391
392	/* Restore HID0 */
393	lwz	r3,CS_HID0(r5)
394	sync
395	isync
396	mtspr	SPRN_HID0,r3
397	sync
398	isync
399
400	/* Now deal with CPU type dependent registers */
401	mfspr	r3,SPRN_PVR
402	srwi	r3,r3,16
403	cmplwi	cr0,r3,0x8000	/* 7450 */
404	cmplwi	cr1,r3,0x000c	/* 7400 */
405	cmplwi	cr2,r3,0x800c	/* 7410 */
406	cmplwi	cr3,r3,0x8001	/* 7455 */
407	cmplwi	cr4,r3,0x8002	/* 7457 */
408	cmplwi	cr5,r3,0x8003	/* 7447A */
409	cmplwi	cr6,r3,0x7000	/* 750FX */
410	cmplwi	cr7,r3,0x8004	/* 7448 */
411	/* cr1 is 7400 || 7410 */
412	cror	4*cr1+eq,4*cr1+eq,4*cr2+eq
413	/* cr0 is 74xx */
414	cror	4*cr0+eq,4*cr0+eq,4*cr3+eq
415	cror	4*cr0+eq,4*cr0+eq,4*cr4+eq
416	cror	4*cr0+eq,4*cr0+eq,4*cr1+eq
417	cror	4*cr0+eq,4*cr0+eq,4*cr5+eq
418	cror	4*cr0+eq,4*cr0+eq,4*cr7+eq
419	bne	2f
420	/* Restore 74xx specific regs */
421	lwz	r4,CS_MSSCR0(r5)
422	sync
423	mtspr	SPRN_MSSCR0,r4
424	sync
425	isync
426	lwz	r4,CS_MSSSR0(r5)
427	sync
428	mtspr	SPRN_MSSSR0,r4
429	sync
430	isync
431	bne	cr2,1f
432	/* Clear 7410 L2CR2 */
433	li	r4,0
434	mtspr	SPRN_L2CR2,r4
4351:	beq	cr1,2f
436	/* Restore 745x specific registers */
437	lwz	r4,CS_HID1(r5)
438	sync
439	mtspr	SPRN_HID1,r4
440	isync
441	sync
442	lwz	r4,CS_ICTRL(r5)
443	sync
444	mtspr	SPRN_ICTRL,r4
445	isync
446	sync
447	lwz	r4,CS_LDSTCR(r5)
448	sync
449	mtspr	SPRN_LDSTCR,r4
450	isync
451	sync
452	lwz	r4,CS_LDSTDB(r5)
453	sync
454	mtspr	SPRN_LDSTDB,r4
455	isync
456	sync
4572:	bne	cr6,1f
458	/* Restore 750FX specific registers
459	 * that is restore HID2 on rev 2.x and PLL config & switch
460	 * to PLL 0 on all
461	 */
462	/* If rev 2.x, restore HID2 with low voltage bit cleared */
463	mfspr	r3,SPRN_PVR
464	andi.	r3,r3,0xff00
465	cmpwi	cr0,r3,0x0200
466	bne	4f
467	lwz	r4,CS_HID2(r5)
468	rlwinm	r4,r4,0,19,17
469	mtspr	SPRN_HID2,r4
470	sync
4714:
472	lwz	r4,CS_HID1(r5)
473	rlwinm  r5,r4,0,16,14
474	mtspr	SPRN_HID1,r5
475		/* Wait for PLL to stabilize */
476	mftbl	r5
4773:	mftbl	r6
478	sub	r6,r6,r5
479	cmplwi	cr0,r6,10000
480	ble	3b
481	/* Setup final PLL */
482	mtspr	SPRN_HID1,r4
4831:
484	mtcr	r7
485	blr
486
487