xref: /rk3399_rockchip-uboot/arch/nios2/cpu/cpu.c (revision 4909f0e16ad85b9bea85d1519884f67e56b7e0cf)
137e4dafaSPeter Tyser /*
237e4dafaSPeter Tyser  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
337e4dafaSPeter Tyser  * Scott McNutt <smcnutt@psyent.com>
437e4dafaSPeter Tyser  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
637e4dafaSPeter Tyser  */
737e4dafaSPeter Tyser 
837e4dafaSPeter Tyser #include <common.h>
9bcae80e9SThomas Chou #include <cpu.h>
10bcae80e9SThomas Chou #include <dm.h>
11bcae80e9SThomas Chou #include <errno.h>
12f956ad98SJoachim Foerster #include <asm/cache.h>
1337e4dafaSPeter Tyser 
145ff10aa7SThomas Chou DECLARE_GLOBAL_DATA_PTR;
155ff10aa7SThomas Chou 
165ff10aa7SThomas Chou #ifdef CONFIG_DISPLAY_CPUINFO
175ff10aa7SThomas Chou int print_cpuinfo(void)
1837e4dafaSPeter Tyser {
1937e4dafaSPeter Tyser 	printf("CPU:   Nios-II\n");
20ca844dd8SThomas Chou 	return 0;
2137e4dafaSPeter Tyser }
225ff10aa7SThomas Chou #endif /* CONFIG_DISPLAY_CPUINFO */
2337e4dafaSPeter Tyser 
24*4909f0e1SThomas Chou #ifdef CONFIG_ALTERA_SYSID
25*4909f0e1SThomas Chou int checkboard(void)
26*4909f0e1SThomas Chou {
27*4909f0e1SThomas Chou 	display_sysid();
28*4909f0e1SThomas Chou 	return 0;
29*4909f0e1SThomas Chou }
30*4909f0e1SThomas Chou #endif
31*4909f0e1SThomas Chou 
32882b7d72SMike Frysinger int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3337e4dafaSPeter Tyser {
3437e4dafaSPeter Tyser 	disable_interrupts();
357a6a7d10SThomas Chou 	/* indirect call to go beyond 256MB limitation of toolchain */
36121e36daSThomas Chou 	nios2_callr(gd->arch.reset_addr);
377a6a7d10SThomas Chou 	return 0;
3837e4dafaSPeter Tyser }
39f956ad98SJoachim Foerster 
40b8112091SThomas Chou /*
41b8112091SThomas Chou  * COPY EXCEPTION TRAMPOLINE -- copy the tramp to the
42b8112091SThomas Chou  * exception address. Define CONFIG_ROM_STUBS to prevent
43b8112091SThomas Chou  * the copy (e.g. exception in flash or in other
44b8112091SThomas Chou  * softare/firmware component).
45b8112091SThomas Chou  */
46b8112091SThomas Chou #ifndef CONFIG_ROM_STUBS
47b8112091SThomas Chou static void copy_exception_trampoline(void)
48b8112091SThomas Chou {
49b8112091SThomas Chou 	extern int _except_start, _except_end;
50b8112091SThomas Chou 	void *except_target = (void *)gd->arch.exception_addr;
51b8112091SThomas Chou 
52b8112091SThomas Chou 	if (&_except_start != except_target) {
53b8112091SThomas Chou 		memcpy(except_target, &_except_start,
54b8112091SThomas Chou 		       &_except_end - &_except_start);
55b8112091SThomas Chou 		flush_cache(gd->arch.exception_addr,
56b8112091SThomas Chou 			    &_except_end - &_except_start);
57b8112091SThomas Chou 	}
58b8112091SThomas Chou }
59b8112091SThomas Chou #endif
60b8112091SThomas Chou 
61bcae80e9SThomas Chou int arch_cpu_init_dm(void)
625ff10aa7SThomas Chou {
63bcae80e9SThomas Chou 	struct udevice *dev;
64bcae80e9SThomas Chou 	int ret;
65bcae80e9SThomas Chou 
66bcae80e9SThomas Chou 	ret = uclass_first_device(UCLASS_CPU, &dev);
67bcae80e9SThomas Chou 	if (ret)
68bcae80e9SThomas Chou 		return ret;
69bcae80e9SThomas Chou 	if (!dev)
70bcae80e9SThomas Chou 		return -ENODEV;
71bcae80e9SThomas Chou 
725ff10aa7SThomas Chou 	gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
73b8112091SThomas Chou #ifndef CONFIG_ROM_STUBS
74b8112091SThomas Chou 	copy_exception_trampoline();
75b8112091SThomas Chou #endif
765ff10aa7SThomas Chou 
775ff10aa7SThomas Chou 	return 0;
785ff10aa7SThomas Chou }
79bcae80e9SThomas Chou 
80bcae80e9SThomas Chou static int altera_nios2_get_desc(struct udevice *dev, char *buf, int size)
81bcae80e9SThomas Chou {
82bcae80e9SThomas Chou 	const char *cpu_name = "Nios-II";
83bcae80e9SThomas Chou 
84bcae80e9SThomas Chou 	if (size < strlen(cpu_name))
85bcae80e9SThomas Chou 		return -ENOSPC;
86bcae80e9SThomas Chou 	strcpy(buf, cpu_name);
87bcae80e9SThomas Chou 
88bcae80e9SThomas Chou 	return 0;
89bcae80e9SThomas Chou }
90bcae80e9SThomas Chou 
91bcae80e9SThomas Chou static int altera_nios2_get_info(struct udevice *dev, struct cpu_info *info)
92bcae80e9SThomas Chou {
93bcae80e9SThomas Chou 	info->cpu_freq = gd->cpu_clk;
94bcae80e9SThomas Chou 	info->features = (1 << CPU_FEAT_L1_CACHE) |
95bcae80e9SThomas Chou 		(gd->arch.has_mmu ? (1 << CPU_FEAT_MMU) : 0);
96bcae80e9SThomas Chou 
97bcae80e9SThomas Chou 	return 0;
98bcae80e9SThomas Chou }
99bcae80e9SThomas Chou 
100bcae80e9SThomas Chou static int altera_nios2_get_count(struct udevice *dev)
101bcae80e9SThomas Chou {
102bcae80e9SThomas Chou 	return 1;
103bcae80e9SThomas Chou }
104bcae80e9SThomas Chou 
105bcae80e9SThomas Chou static int altera_nios2_probe(struct udevice *dev)
106bcae80e9SThomas Chou {
107bcae80e9SThomas Chou 	const void *blob = gd->fdt_blob;
108bcae80e9SThomas Chou 	int node = dev->of_offset;
109bcae80e9SThomas Chou 
110bcae80e9SThomas Chou 	gd->cpu_clk = fdtdec_get_int(blob, node,
111bcae80e9SThomas Chou 		"clock-frequency", 0);
112bcae80e9SThomas Chou 	gd->arch.dcache_line_size = fdtdec_get_int(blob, node,
113bcae80e9SThomas Chou 		"dcache-line-size", 0);
114bcae80e9SThomas Chou 	gd->arch.icache_line_size = fdtdec_get_int(blob, node,
115bcae80e9SThomas Chou 		"icache-line-size", 0);
116bcae80e9SThomas Chou 	gd->arch.dcache_size = fdtdec_get_int(blob, node,
117bcae80e9SThomas Chou 		"dcache-size", 0);
118bcae80e9SThomas Chou 	gd->arch.icache_size = fdtdec_get_int(blob, node,
119bcae80e9SThomas Chou 		"icache-size", 0);
120bcae80e9SThomas Chou 	gd->arch.reset_addr = fdtdec_get_int(blob, node,
121bcae80e9SThomas Chou 		"altr,reset-addr", 0);
122bcae80e9SThomas Chou 	gd->arch.exception_addr = fdtdec_get_int(blob, node,
123bcae80e9SThomas Chou 		"altr,exception-addr", 0);
124bcae80e9SThomas Chou 	gd->arch.has_initda = fdtdec_get_int(blob, node,
125bcae80e9SThomas Chou 		"altr,has-initda", 0);
126bcae80e9SThomas Chou 	gd->arch.has_mmu = fdtdec_get_int(blob, node,
127bcae80e9SThomas Chou 		"altr,has-mmu", 0);
1281ce61cbbSThomas Chou 	gd->arch.io_region_base = gd->arch.has_mmu ? 0xe0000000 : 0x80000000;
1291ce61cbbSThomas Chou 	gd->arch.mem_region_base = gd->arch.has_mmu ? 0xc0000000 : 0x00000000;
1302de4823dSThomas Chou 	gd->arch.physaddr_mask = gd->arch.has_mmu ? 0x1fffffff : 0x7fffffff;
131bcae80e9SThomas Chou 
132bcae80e9SThomas Chou 	return 0;
133bcae80e9SThomas Chou }
134bcae80e9SThomas Chou 
135bcae80e9SThomas Chou static const struct cpu_ops altera_nios2_ops = {
136bcae80e9SThomas Chou 	.get_desc	= altera_nios2_get_desc,
137bcae80e9SThomas Chou 	.get_info	= altera_nios2_get_info,
138bcae80e9SThomas Chou 	.get_count	= altera_nios2_get_count,
139bcae80e9SThomas Chou };
140bcae80e9SThomas Chou 
141bcae80e9SThomas Chou static const struct udevice_id altera_nios2_ids[] = {
142bcae80e9SThomas Chou 	{ .compatible = "altr,nios2-1.0" },
143bcae80e9SThomas Chou 	{ .compatible = "altr,nios2-1.1" },
144bcae80e9SThomas Chou 	{ }
145bcae80e9SThomas Chou };
146bcae80e9SThomas Chou 
147bcae80e9SThomas Chou U_BOOT_DRIVER(altera_nios2) = {
148bcae80e9SThomas Chou 	.name		= "altera_nios2",
149bcae80e9SThomas Chou 	.id		= UCLASS_CPU,
150bcae80e9SThomas Chou 	.of_match	= altera_nios2_ids,
151bcae80e9SThomas Chou 	.probe		= altera_nios2_probe,
152bcae80e9SThomas Chou 	.ops		= &altera_nios2_ops,
153bcae80e9SThomas Chou 	.flags		= DM_FLAG_PRE_RELOC,
154bcae80e9SThomas Chou };
155