aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/kernel/processor.c
blob: 55d1bdf62350656996634f2922185ad2ee4e7cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * arch/ubicom32/kernel/processor.c
 *   Ubicom32 architecture processor info implementation.
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/profile.h>
#include <linux/clocksource.h>
#include <linux/types.h>
#include <linux/seq_file.h>
#include <linux/delay.h>
#include <linux/cpu.h>
#include <asm/devtree.h>
#include <asm/processor.h>
#include <asm/cpu.h>
#include <asm/ocm_size.h>

struct procnode {
	struct devtree_node dn;
	unsigned int threads;
	unsigned int timers;
	unsigned int frequency;
	unsigned int ddr_frequency;
	unsigned int interrupt0;
	unsigned int interrupt1;
	void *socm;
	void *eocm;
	void *sdram;
	void *edram;
	unsigned int arch_version;
	void *os_syscall_begin;
	void *os_syscall_end;
};

struct procnode *pn;

/*
 * show_processorinfo()
 *	Print the actual processor information.
 */
static void show_processorinfo(struct seq_file *m)
{
	char *cpu, *mmu, *fpu;
	unsigned int clockfreq;
	unsigned int chipid;

	cpu = CPU;
	mmu = "none";
	fpu = "none";

	asm volatile (
	"move.4		%0, CHIP_ID	\n\t"
	: "=r" (chipid)
	);

	/*
	 * General Processor Information.
	 */
	seq_printf(m, "Vendor:\t\t%s\n", "Ubicom");
	seq_printf(m, "CPU:\t\t%s\n", cpu);
	seq_printf(m, "MMU:\t\t%s\n", mmu);
	seq_printf(m, "FPU:\t\t%s\n", fpu);
	seq_printf(m, "Arch:\t\t%hx\n", chipid >> 16);
	seq_printf(m, "Rev:\t\t%hx\n", (chipid & 0xffff));

	/*
	 * Now compute the clock frequency in Mhz.
	 */
	clockfreq = processor_frequency();
	seq_printf(m, "Clock Freq:\t%u.0 MHz\n",
		   clockfreq / 1000000);
	seq_printf(m, "DDR Freq:\t%u.0 MHz\n",
		   pn ? pn->ddr_frequency / 1000000 : 0);
	seq_printf(m, "BogoMips:\t%lu.%02lu\n",
		   (loops_per_jiffy * HZ) / 500000,
		   ((loops_per_jiffy * HZ) / 5000) % 100);
	seq_printf(m, "Calibration:\t%lu loops\n", (loops_per_jiffy * HZ));
}

/*
 * show_cpuinfo()
 *	Get CPU information for use by the procfs.
 */
static int show_cpuinfo(struct seq_file *m, void *v)
{
	unsigned long n = (unsigned long)v - 1;

#if defined(CONFIG_SMP)
	struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, n);
#endif

	/*
	 * Print the general processor information on the first
	 * call.
	 */
	if (n == 0) {
		show_processorinfo(m);
	}

#if defined(CONFIG_SMP)
	/*
	 * For each hwthread, print if this hwthread is running Linux
	 * or is an I/O thread.
	 */
	if (cpu_isset(n, cpu_online_map)) {
		seq_printf(m, "cpu[%02lu]:\tthread id - %lu\n", n, p->tid);
	} else {
		seq_printf(m, "cpu[%02lu]:\toff-line\n", n);
	}
#endif
	return 0;

}

static void *c_start(struct seq_file *m, loff_t *pos)
{
	unsigned long i = *pos;

	return i < NR_CPUS ? (void *)(i + 1) : NULL;
}

static void *c_next(struct seq_file *m, void *v, loff_t *pos)
{
	++*pos;
	return c_start(m, pos);
}

static void c_stop(struct seq_file *m, void *v)
{
}

const struct seq_operations cpuinfo_op = {
	.start	= c_start,
	.next	= c_next,
	.stop	= c_stop,
	.show	= show_cpuinfo,
};

/*
 * processor_timers()
 *	Returns the timers available to Linux.
 */
unsigned int processor_timers(void)
{
	if (!pn) {
		return 0;
	}
	return pn->timers;
}

/*
 * processor_threads()
 *	Returns the threads available to Linux.
 */
unsigned int processor_threads(void)
{
	if (!pn) {
		return 0;
	}
	return pn->threads;
}

/*
 * processor_frequency()
 *	Returns the frequency of the system clock.
 */
unsigned int processor_frequency(void)
{
	if (!pn) {
		return 0;
	}
	return pn->frequency;
}
EXPORT_SYMBOL(processor_frequency);

/*
 * processor_interrupts()
 *	Return the interrupts that are setup at boot time.
 */
int processor_interrupts(unsigned int *int0, unsigned int *int1)
{
	if (!pn) {
		return -EFAULT;
	}

	if (int0) {
		*int0 = pn->interrupt0;
	}

	if (int1) {
		*int1 = pn->interrupt1;
	}
	return 0;
}

/*
 * processor_ocm()
 *	Returns the start and end of OCM available to Linux.
 */
void processor_ocm(unsigned long *socm, unsigned long *eocm)
{
	*socm = (unsigned long)pn->socm;
	*eocm = (unsigned long)pn->eocm;
}

/*
 * processor_dram()
 *	Returns the start and end of dram available to Linux.
 */
void processor_dram(unsigned long *sdram, unsigned long *edram)
{
	*sdram = (unsigned long)pn->sdram;
	*edram = (unsigned long)pn->edram;
}

/*
 * processor_validate_failed()
 *	Returns the dram available to Linux.
 */
static noinline void processor_validate_failed(void)
{
	while (1)
		THREAD_STALL;
}

/*
 * processor_validate()
 *	Validates the procnode against limitations of this link/built.
 */
static void processor_validate(void)
{
	void *dram_start = (void *)(KERNELSTART);
	void *dram_end   = (void *)(SDRAMSTART + CONFIG_MIN_RAMSIZE);
#if APP_OCM_CODE_SIZE || APP_OCM_DATA_SIZE
	void *ocm_code_start = (void *)(OCMSTART + APP_OCM_CODE_SIZE);
	void *ocm_data_end   = (void *)(OCMEND   - APP_OCM_DATA_SIZE);
#endif
	extern void __os_syscall_begin;
	extern void __os_syscall_end;
	int proc_node_valid = 1;

	if (!pn) {
		printk(KERN_ERR "ERROR: processor node not found\n");
		goto error;
	}


	if (dram_start < pn->sdram || dram_end > pn->edram) {
		printk(KERN_ERR "ERROR: processor dram mismatch %p-%p "
		       "available but we are expecting %p-%p\n",
		       pn->sdram, pn->edram, dram_start, dram_end);
		proc_node_valid = 0;
	} else {
		printk(KERN_ERR "processor dram %p-%p, expecting %p-%p\n",
		       pn->sdram, pn->edram, dram_start, dram_end);
	}
	if (&__os_syscall_begin < pn->os_syscall_begin ||
	    &__os_syscall_end > pn->os_syscall_end) {
		printk(KERN_ERR "ERROR: processor syscall area mismatch "
		       "%p-%p available but we are expecting %p-%p\n",
		       pn->os_syscall_begin, pn->os_syscall_end,
		       &__os_syscall_begin, &__os_syscall_end);
		proc_node_valid = 0;
	} else {
		printk(KERN_ERR "processor dram %p-%p, expecting %p-%p\n",
		       pn->sdram, pn->edram, dram_start, dram_end);
	}
#if APP_OCM_CODE_SIZE || APP_OCM_DATA_SIZE
	if (ocm_code_start < pn->socm ||  ocm_data_end > pn->eocm) {
		printk(KERN_ERR "ERROR: processor ocm mismatch %p-%p "
		       "available but we are expecting %p-%p\n",
		       pn->socm, pn->eocm, ocm_code_start, ocm_data_end);
		proc_node_valid = 0;
	} else {
		printk(KERN_INFO "processor ocm %p-%p, expecting %p-%p\n",
		       pn->socm, pn->eocm, ocm_code_start, ocm_data_end);

	}
#endif

	if (UBICOM32_ARCH_VERSION != pn->arch_version) {
		printk(KERN_ERR "ERROR: processor arch mismatch, kernel"
		       "compiled for %d found %d\n",
		       UBICOM32_ARCH_VERSION, pn->arch_version);
		proc_node_valid = 0;
	}

	if (proc_node_valid)
		return;
error:
	processor_validate_failed();
}

void __init processor_init(void)
{
	/*
	 * If we do not have a trap node in the device tree, we leave the fault
	 * handling to the underlying hardware.
	 */
	pn = (struct procnode *)devtree_find_node("processor");

	processor_validate();

	/*
	 * If necessary correct the initial range registers to cover the
	 * complete physical space
	 */
	if (pn->edram > (void *)(SDRAMSTART + CONFIG_MIN_RAMSIZE)) {
		printk(KERN_INFO "updating range registers for expanded dram\n");
		asm volatile (
			"	move.4 D_RANGE1_HI, %0		\t\n"
			"	move.4 I_RANGE0_HI, %0		\t\n"
#ifdef CONFIG_PROTECT_KERNEL
			"	move.4 D_RANGE2_HI, %0		\t\n"
			"	move.4 I_RANGE2_HI, %0		\t\n"
#endif
		: : "a"((unsigned long)pn->edram - 4)
			);
	}

}