xref: /openbmc/u-boot/arch/powerpc/cpu/mpc83xx/law.c (revision c5f18a0b)
1 /*
2  * Copyright 2011 Freescale Semiconductor, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * Version 2 as published by the Free Software Foundation.
7  */
8 
9 #include <common.h>
10 #include <asm/fsl_law.h>
11 #include <asm/mmu.h>
12 #include <linux/log2.h>
13 
14 int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
15 {
16 	immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
17 	law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
18 	u64 start_align, law_sz;
19 	int law_sz_enc;
20 
21 	if (start == 0)
22 		start_align = 1ull << (LAW_SIZE_2G + 1);
23 	else
24 		start_align = 1ull << (__ffs64(start) - 1);
25 	law_sz = min(start_align, sz);
26 	law_sz_enc = __ilog2_u64(law_sz) - 1;
27 
28 	/*
29 	 * Set up LAWBAR for all of DDR.
30 	 */
31 	ecm->bar = start & 0xfffff000;
32 	ecm->ar  = (LAWAR_EN | (id << 20) | (LAWAR_SIZE & law_sz_enc));
33 	debug("DDR:bar=0x%08x\n", ecm->bar);
34 	debug("DDR:ar=0x%08x\n", ecm->ar);
35 
36 	/* recalculate size based on what was actually covered by the law */
37 	law_sz = 1ull << __ilog2_u64(law_sz);
38 
39 	/* do we still have anything to map */
40 	sz = sz - law_sz;
41 	if (sz) {
42 		start += law_sz;
43 
44 		start_align = 1ull << (__ffs64(start) - 1);
45 		law_sz = min(start_align, sz);
46 		law_sz_enc = __ilog2_u64(law_sz) - 1;
47 		ecm = &immap->sysconf.ddrlaw[1];
48 		ecm->bar = start & 0xfffff000;
49 		ecm->ar  = (LAWAR_EN | (id << 20) | (LAWAR_SIZE & law_sz_enc));
50 		debug("DDR:bar=0x%08x\n", ecm->bar);
51 		debug("DDR:ar=0x%08x\n", ecm->ar);
52 	} else {
53 		return 0;
54 	}
55 
56 	/* do we still have anything to map */
57 	sz = sz - law_sz;
58 	if (sz)
59 		return 1;
60 
61 	return 0;
62 }
63