ARM: shmobile: Remove FSF address from copyright headers
[cascardo/linux.git] / arch / arm / mach-shmobile / setup-rcar-gen2.c
1 /*
2  * R-Car Generation 2 support
3  *
4  * Copyright (C) 2013  Renesas Solutions Corp.
5  * Copyright (C) 2013  Magnus Damm
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/clk/shmobile.h>
18 #include <linux/clocksource.h>
19 #include <linux/device.h>
20 #include <linux/dma-contiguous.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/of_fdt.h>
24 #include <asm/mach/arch.h>
25 #include "common.h"
26 #include "rcar-gen2.h"
27
28 #define MODEMR 0xe6160060
29
30 u32 rcar_gen2_read_mode_pins(void)
31 {
32         static u32 mode;
33         static bool mode_valid;
34
35         if (!mode_valid) {
36                 void __iomem *modemr = ioremap_nocache(MODEMR, 4);
37                 BUG_ON(!modemr);
38                 mode = ioread32(modemr);
39                 iounmap(modemr);
40                 mode_valid = true;
41         }
42
43         return mode;
44 }
45
46 #define CNTCR 0
47 #define CNTFID0 0x20
48
49 void __init rcar_gen2_timer_init(void)
50 {
51 #if defined(CONFIG_ARM_ARCH_TIMER) || defined(CONFIG_COMMON_CLK)
52         u32 mode = rcar_gen2_read_mode_pins();
53 #endif
54 #ifdef CONFIG_ARM_ARCH_TIMER
55         void __iomem *base;
56         int extal_mhz = 0;
57         u32 freq;
58
59         /* At Linux boot time the r8a7790 arch timer comes up
60          * with the counter disabled. Moreover, it may also report
61          * a potentially incorrect fixed 13 MHz frequency. To be
62          * correct these registers need to be updated to use the
63          * frequency EXTAL / 2 which can be determined by the MD pins.
64          */
65
66         switch (mode & (MD(14) | MD(13))) {
67         case 0:
68                 extal_mhz = 15;
69                 break;
70         case MD(13):
71                 extal_mhz = 20;
72                 break;
73         case MD(14):
74                 extal_mhz = 26;
75                 break;
76         case MD(13) | MD(14):
77                 extal_mhz = 30;
78                 break;
79         }
80
81         /* The arch timer frequency equals EXTAL / 2 */
82         freq = extal_mhz * (1000000 / 2);
83
84         /* Remap "armgcnt address map" space */
85         base = ioremap(0xe6080000, PAGE_SIZE);
86
87         /*
88          * Update the timer if it is either not running, or is not at the
89          * right frequency. The timer is only configurable in secure mode
90          * so this avoids an abort if the loader started the timer and
91          * entered the kernel in non-secure mode.
92          */
93
94         if ((ioread32(base + CNTCR) & 1) == 0 ||
95             ioread32(base + CNTFID0) != freq) {
96                 /* Update registers with correct frequency */
97                 iowrite32(freq, base + CNTFID0);
98                 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
99
100                 /* make sure arch timer is started by setting bit 0 of CNTCR */
101                 iowrite32(1, base + CNTCR);
102         }
103
104         iounmap(base);
105 #endif /* CONFIG_ARM_ARCH_TIMER */
106
107 #ifdef CONFIG_COMMON_CLK
108         rcar_gen2_clocks_init(mode);
109 #endif
110         clocksource_of_init();
111 }
112
113 struct memory_reserve_config {
114         u64 reserved;
115         u64 base, size;
116 };
117
118 static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
119                                      int depth, void *data)
120 {
121         const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
122         const __be32 *reg, *endp;
123         int l;
124         struct memory_reserve_config *mrc = data;
125         u64 lpae_start = 1ULL << 32;
126
127         /* We are scanning "memory" nodes only */
128         if (type == NULL || strcmp(type, "memory"))
129                 return 0;
130
131         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
132         if (reg == NULL)
133                 reg = of_get_flat_dt_prop(node, "reg", &l);
134         if (reg == NULL)
135                 return 0;
136
137         endp = reg + (l / sizeof(__be32));
138         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
139                 u64 base, size;
140
141                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
142                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
143
144                 if (base >= lpae_start)
145                         continue;
146
147                 if ((base + size) >= lpae_start)
148                         size = lpae_start - base;
149
150                 if (size < mrc->reserved)
151                         continue;
152
153                 if (base < mrc->base)
154                         continue;
155
156                 /* keep the area at top near the 32-bit legacy limit */
157                 mrc->base = base + size - mrc->reserved;
158                 mrc->size = mrc->reserved;
159         }
160
161         return 0;
162 }
163
164 struct cma *rcar_gen2_dma_contiguous;
165
166 void __init rcar_gen2_reserve(void)
167 {
168         struct memory_reserve_config mrc;
169
170         /* reserve 256 MiB at the top of the physical legacy 32-bit space */
171         memset(&mrc, 0, sizeof(mrc));
172         mrc.reserved = SZ_256M;
173
174         of_scan_flat_dt(rcar_gen2_scan_mem, &mrc);
175 #ifdef CONFIG_DMA_CMA
176         if (mrc.size)
177                 dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
178                                             &rcar_gen2_dma_contiguous, true);
179 #endif
180 }