blob: 5d13595aa027447dc9b933f4a5132c6d9f75407c [file] [log] [blame]
Magnus Damm50c517d2013-09-12 09:32:49 +09001/*
2 * R-Car Generation 2 support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Copyright (C) 2013 Magnus Damm
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +02006 * Copyright (C) 2014 Ulrich Hecht
Magnus Damm50c517d2013-09-12 09:32:49 +09007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Magnus Damm50c517d2013-09-12 09:32:49 +090016 */
17
Laurent Pinchart4b5c2112013-12-11 15:13:51 +010018#include <linux/clk/shmobile.h>
Magnus Damm50c517d2013-09-12 09:32:49 +090019#include <linux/clocksource.h>
Magnus Dammf8e81932014-06-09 21:38:45 +090020#include <linux/device.h>
21#include <linux/dma-contiguous.h>
Magnus Damm50c517d2013-09-12 09:32:49 +090022#include <linux/io.h>
23#include <linux/kernel.h>
Magnus Damm744cadb2015-01-21 14:01:37 +090024#include <linux/memblock.h>
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +020025#include <linux/of.h>
Magnus Dammf8e81932014-06-09 21:38:45 +090026#include <linux/of_fdt.h>
Magnus Damm50c517d2013-09-12 09:32:49 +090027#include <asm/mach/arch.h>
Magnus Dammfd44aa52014-06-17 16:47:37 +090028#include "common.h"
Magnus Damm62872982014-06-17 16:48:01 +090029#include "rcar-gen2.h"
Magnus Damm50c517d2013-09-12 09:32:49 +090030
31#define MODEMR 0xe6160060
32
Magnus Damme7509f62014-02-17 15:35:10 +090033u32 rcar_gen2_read_mode_pins(void)
Magnus Damm50c517d2013-09-12 09:32:49 +090034{
Geert Uytterhoeven835d7372014-03-12 19:44:49 +010035 static u32 mode;
36 static bool mode_valid;
Magnus Damm50c517d2013-09-12 09:32:49 +090037
Geert Uytterhoeven835d7372014-03-12 19:44:49 +010038 if (!mode_valid) {
39 void __iomem *modemr = ioremap_nocache(MODEMR, 4);
40 BUG_ON(!modemr);
41 mode = ioread32(modemr);
42 iounmap(modemr);
43 mode_valid = true;
44 }
Magnus Damm50c517d2013-09-12 09:32:49 +090045
46 return mode;
47}
48
49#define CNTCR 0
50#define CNTFID0 0x20
51
52void __init rcar_gen2_timer_init(void)
53{
Magnus Damm50c517d2013-09-12 09:32:49 +090054 u32 mode = rcar_gen2_read_mode_pins();
Laurent Pinchart4b5c2112013-12-11 15:13:51 +010055#ifdef CONFIG_ARM_ARCH_TIMER
Magnus Damm50c517d2013-09-12 09:32:49 +090056 void __iomem *base;
57 int extal_mhz = 0;
58 u32 freq;
59
Magnus Damm95abc9d2014-12-03 20:48:04 +090060 if (of_machine_is_compatible("renesas,r8a7794")) {
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +020061 freq = 260000000 / 8; /* ZS / 8 */
62 /* CNTVOFF has to be initialized either from non-secure
63 * Hypervisor mode or secure Monitor mode with SCR.NS==1.
64 * If TrustZone is enabled then it should be handled by the
65 * secure code.
66 */
67 asm volatile(
68 " cps 0x16\n"
69 " mrc p15, 0, r1, c1, c1, 0\n"
70 " orr r0, r1, #1\n"
71 " mcr p15, 0, r0, c1, c1, 0\n"
72 " isb\n"
73 " mov r0, #0\n"
74 " mcrr p15, 4, r0, r0, c14\n"
75 " isb\n"
76 " mcr p15, 0, r1, c1, c1, 0\n"
77 " isb\n"
78 " cps 0x13\n"
79 : : : "r0", "r1");
80 } else {
81 /* At Linux boot time the r8a7790 arch timer comes up
82 * with the counter disabled. Moreover, it may also report
83 * a potentially incorrect fixed 13 MHz frequency. To be
84 * correct these registers need to be updated to use the
85 * frequency EXTAL / 2 which can be determined by the MD pins.
86 */
Magnus Damm50c517d2013-09-12 09:32:49 +090087
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +020088 switch (mode & (MD(14) | MD(13))) {
89 case 0:
90 extal_mhz = 15;
91 break;
92 case MD(13):
93 extal_mhz = 20;
94 break;
95 case MD(14):
96 extal_mhz = 26;
97 break;
98 case MD(13) | MD(14):
99 extal_mhz = 30;
100 break;
101 }
102
103 /* The arch timer frequency equals EXTAL / 2 */
104 freq = extal_mhz * (1000000 / 2);
Magnus Damm50c517d2013-09-12 09:32:49 +0900105 }
106
Magnus Damm50c517d2013-09-12 09:32:49 +0900107 /* Remap "armgcnt address map" space */
108 base = ioremap(0xe6080000, PAGE_SIZE);
109
Ben Dooks0fe35072013-12-11 10:07:42 +0000110 /*
111 * Update the timer if it is either not running, or is not at the
112 * right frequency. The timer is only configurable in secure mode
113 * so this avoids an abort if the loader started the timer and
114 * entered the kernel in non-secure mode.
115 */
Magnus Damm50c517d2013-09-12 09:32:49 +0900116
Ben Dooks0fe35072013-12-11 10:07:42 +0000117 if ((ioread32(base + CNTCR) & 1) == 0 ||
118 ioread32(base + CNTFID0) != freq) {
119 /* Update registers with correct frequency */
120 iowrite32(freq, base + CNTFID0);
121 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
122
123 /* make sure arch timer is started by setting bit 0 of CNTCR */
124 iowrite32(1, base + CNTCR);
125 }
126
Magnus Damm50c517d2013-09-12 09:32:49 +0900127 iounmap(base);
128#endif /* CONFIG_ARM_ARCH_TIMER */
129
Laurent Pinchart4b5c2112013-12-11 15:13:51 +0100130 rcar_gen2_clocks_init(mode);
Magnus Damm77cf5162015-01-29 16:25:32 +0900131#ifdef CONFIG_ARCH_SHMOBILE_MULTI
Magnus Damm50c517d2013-09-12 09:32:49 +0900132 clocksource_of_init();
Magnus Damm77cf5162015-01-29 16:25:32 +0900133#endif
Magnus Damm50c517d2013-09-12 09:32:49 +0900134}
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200135
136struct memory_reserve_config {
137 u64 reserved;
138 u64 base, size;
139};
140
141static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
142 int depth, void *data)
143{
144 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
145 const __be32 *reg, *endp;
146 int l;
147 struct memory_reserve_config *mrc = data;
Geert Uytterhoevenb69f47c2014-06-12 10:42:23 +0200148 u64 lpae_start = 1ULL << 32;
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200149
150 /* We are scanning "memory" nodes only */
Geert Uytterhoevenea2a0d582014-06-12 10:42:24 +0200151 if (type == NULL || strcmp(type, "memory"))
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200152 return 0;
153
154 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
155 if (reg == NULL)
156 reg = of_get_flat_dt_prop(node, "reg", &l);
157 if (reg == NULL)
158 return 0;
159
160 endp = reg + (l / sizeof(__be32));
161 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
162 u64 base, size;
163
164 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
165 size = dt_mem_next_cell(dt_root_size_cells, &reg);
166
167 if (base >= lpae_start)
168 continue;
169
170 if ((base + size) >= lpae_start)
171 size = lpae_start - base;
172
173 if (size < mrc->reserved)
174 continue;
175
176 if (base < mrc->base)
177 continue;
178
179 /* keep the area at top near the 32-bit legacy limit */
180 mrc->base = base + size - mrc->reserved;
181 mrc->size = mrc->reserved;
182 }
183
184 return 0;
185}
186
187struct cma *rcar_gen2_dma_contiguous;
188
189void __init rcar_gen2_reserve(void)
190{
191 struct memory_reserve_config mrc;
192
193 /* reserve 256 MiB at the top of the physical legacy 32-bit space */
194 memset(&mrc, 0, sizeof(mrc));
195 mrc.reserved = SZ_256M;
196
197 of_scan_flat_dt(rcar_gen2_scan_mem, &mrc);
198#ifdef CONFIG_DMA_CMA
Magnus Damm744cadb2015-01-21 14:01:37 +0900199 if (mrc.size && memblock_is_region_memory(mrc.base, mrc.size))
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200200 dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
Vincent Stehlé14a5e922014-06-19 11:31:10 +0200201 &rcar_gen2_dma_contiguous, true);
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200202#endif
203}