xref: /openbmc/u-boot/arch/nios2/cpu/cpu.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
237e4dafaSPeter Tyser /*
337e4dafaSPeter Tyser  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
437e4dafaSPeter Tyser  * Scott McNutt <smcnutt@psyent.com>
537e4dafaSPeter Tyser  */
637e4dafaSPeter Tyser 
737e4dafaSPeter Tyser #include <common.h>
8bcae80e9SThomas Chou #include <cpu.h>
9bcae80e9SThomas Chou #include <dm.h>
10bcae80e9SThomas Chou #include <errno.h>
11f956ad98SJoachim Foerster #include <asm/cache.h>
1237e4dafaSPeter Tyser 
135ff10aa7SThomas Chou DECLARE_GLOBAL_DATA_PTR;
145ff10aa7SThomas Chou 
155ff10aa7SThomas Chou #ifdef CONFIG_DISPLAY_CPUINFO
print_cpuinfo(void)165ff10aa7SThomas Chou int print_cpuinfo(void)
1737e4dafaSPeter Tyser {
1837e4dafaSPeter Tyser 	printf("CPU:   Nios-II\n");
19ca844dd8SThomas Chou 	return 0;
2037e4dafaSPeter Tyser }
215ff10aa7SThomas Chou #endif /* CONFIG_DISPLAY_CPUINFO */
2237e4dafaSPeter Tyser 
234909f0e1SThomas Chou #ifdef CONFIG_ALTERA_SYSID
checkboard(void)244909f0e1SThomas Chou int checkboard(void)
254909f0e1SThomas Chou {
264909f0e1SThomas Chou 	display_sysid();
274909f0e1SThomas Chou 	return 0;
284909f0e1SThomas Chou }
294909f0e1SThomas Chou #endif
304909f0e1SThomas Chou 
do_reset(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])31882b7d72SMike Frysinger int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3237e4dafaSPeter Tyser {
3337e4dafaSPeter Tyser 	disable_interrupts();
347a6a7d10SThomas Chou 	/* indirect call to go beyond 256MB limitation of toolchain */
35121e36daSThomas Chou 	nios2_callr(gd->arch.reset_addr);
367a6a7d10SThomas Chou 	return 0;
3737e4dafaSPeter Tyser }
38f956ad98SJoachim Foerster 
39b8112091SThomas Chou /*
40b8112091SThomas Chou  * COPY EXCEPTION TRAMPOLINE -- copy the tramp to the
41b8112091SThomas Chou  * exception address. Define CONFIG_ROM_STUBS to prevent
42b8112091SThomas Chou  * the copy (e.g. exception in flash or in other
43b8112091SThomas Chou  * softare/firmware component).
44b8112091SThomas Chou  */
45b8112091SThomas Chou #ifndef CONFIG_ROM_STUBS
copy_exception_trampoline(void)46b8112091SThomas Chou static void copy_exception_trampoline(void)
47b8112091SThomas Chou {
48b8112091SThomas Chou 	extern int _except_start, _except_end;
49b8112091SThomas Chou 	void *except_target = (void *)gd->arch.exception_addr;
50b8112091SThomas Chou 
51b8112091SThomas Chou 	if (&_except_start != except_target) {
52b8112091SThomas Chou 		memcpy(except_target, &_except_start,
53b8112091SThomas Chou 		       &_except_end - &_except_start);
54b8112091SThomas Chou 		flush_cache(gd->arch.exception_addr,
55b8112091SThomas Chou 			    &_except_end - &_except_start);
56b8112091SThomas Chou 	}
57b8112091SThomas Chou }
58b8112091SThomas Chou #endif
59b8112091SThomas Chou 
arch_cpu_init_dm(void)60bcae80e9SThomas Chou int arch_cpu_init_dm(void)
615ff10aa7SThomas Chou {
62bcae80e9SThomas Chou 	struct udevice *dev;
63bcae80e9SThomas Chou 	int ret;
64bcae80e9SThomas Chou 
653f603cbbSSimon Glass 	ret = uclass_first_device_err(UCLASS_CPU, &dev);
66bcae80e9SThomas Chou 	if (ret)
67bcae80e9SThomas Chou 		return ret;
68bcae80e9SThomas Chou 
695ff10aa7SThomas Chou 	gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
70b8112091SThomas Chou #ifndef CONFIG_ROM_STUBS
71b8112091SThomas Chou 	copy_exception_trampoline();
72b8112091SThomas Chou #endif
735ff10aa7SThomas Chou 
745ff10aa7SThomas Chou 	return 0;
755ff10aa7SThomas Chou }
76bcae80e9SThomas Chou 
altera_nios2_get_desc(struct udevice * dev,char * buf,int size)77bcae80e9SThomas Chou static int altera_nios2_get_desc(struct udevice *dev, char *buf, int size)
78bcae80e9SThomas Chou {
79bcae80e9SThomas Chou 	const char *cpu_name = "Nios-II";
80bcae80e9SThomas Chou 
81bcae80e9SThomas Chou 	if (size < strlen(cpu_name))
82bcae80e9SThomas Chou 		return -ENOSPC;
83bcae80e9SThomas Chou 	strcpy(buf, cpu_name);
84bcae80e9SThomas Chou 
85bcae80e9SThomas Chou 	return 0;
86bcae80e9SThomas Chou }
87bcae80e9SThomas Chou 
altera_nios2_get_info(struct udevice * dev,struct cpu_info * info)88bcae80e9SThomas Chou static int altera_nios2_get_info(struct udevice *dev, struct cpu_info *info)
89bcae80e9SThomas Chou {
90bcae80e9SThomas Chou 	info->cpu_freq = gd->cpu_clk;
91bcae80e9SThomas Chou 	info->features = (1 << CPU_FEAT_L1_CACHE) |
92bcae80e9SThomas Chou 		(gd->arch.has_mmu ? (1 << CPU_FEAT_MMU) : 0);
93bcae80e9SThomas Chou 
94bcae80e9SThomas Chou 	return 0;
95bcae80e9SThomas Chou }
96bcae80e9SThomas Chou 
altera_nios2_get_count(struct udevice * dev)97bcae80e9SThomas Chou static int altera_nios2_get_count(struct udevice *dev)
98bcae80e9SThomas Chou {
99bcae80e9SThomas Chou 	return 1;
100bcae80e9SThomas Chou }
101bcae80e9SThomas Chou 
altera_nios2_probe(struct udevice * dev)102bcae80e9SThomas Chou static int altera_nios2_probe(struct udevice *dev)
103bcae80e9SThomas Chou {
104bcae80e9SThomas Chou 	const void *blob = gd->fdt_blob;
105e160f7d4SSimon Glass 	int node = dev_of_offset(dev);
106bcae80e9SThomas Chou 
107bcae80e9SThomas Chou 	gd->cpu_clk = fdtdec_get_int(blob, node,
108bcae80e9SThomas Chou 		"clock-frequency", 0);
109bcae80e9SThomas Chou 	gd->arch.dcache_line_size = fdtdec_get_int(blob, node,
110bcae80e9SThomas Chou 		"dcache-line-size", 0);
111bcae80e9SThomas Chou 	gd->arch.icache_line_size = fdtdec_get_int(blob, node,
112bcae80e9SThomas Chou 		"icache-line-size", 0);
113bcae80e9SThomas Chou 	gd->arch.dcache_size = fdtdec_get_int(blob, node,
114bcae80e9SThomas Chou 		"dcache-size", 0);
115bcae80e9SThomas Chou 	gd->arch.icache_size = fdtdec_get_int(blob, node,
116bcae80e9SThomas Chou 		"icache-size", 0);
117bcae80e9SThomas Chou 	gd->arch.reset_addr = fdtdec_get_int(blob, node,
118bcae80e9SThomas Chou 		"altr,reset-addr", 0);
119bcae80e9SThomas Chou 	gd->arch.exception_addr = fdtdec_get_int(blob, node,
120bcae80e9SThomas Chou 		"altr,exception-addr", 0);
121bcae80e9SThomas Chou 	gd->arch.has_initda = fdtdec_get_int(blob, node,
122bcae80e9SThomas Chou 		"altr,has-initda", 0);
123bcae80e9SThomas Chou 	gd->arch.has_mmu = fdtdec_get_int(blob, node,
124bcae80e9SThomas Chou 		"altr,has-mmu", 0);
1251ce61cbbSThomas Chou 	gd->arch.io_region_base = gd->arch.has_mmu ? 0xe0000000 : 0x80000000;
1261ce61cbbSThomas Chou 	gd->arch.mem_region_base = gd->arch.has_mmu ? 0xc0000000 : 0x00000000;
1272de4823dSThomas Chou 	gd->arch.physaddr_mask = gd->arch.has_mmu ? 0x1fffffff : 0x7fffffff;
128bcae80e9SThomas Chou 
129bcae80e9SThomas Chou 	return 0;
130bcae80e9SThomas Chou }
131bcae80e9SThomas Chou 
132bcae80e9SThomas Chou static const struct cpu_ops altera_nios2_ops = {
133bcae80e9SThomas Chou 	.get_desc	= altera_nios2_get_desc,
134bcae80e9SThomas Chou 	.get_info	= altera_nios2_get_info,
135bcae80e9SThomas Chou 	.get_count	= altera_nios2_get_count,
136bcae80e9SThomas Chou };
137bcae80e9SThomas Chou 
138bcae80e9SThomas Chou static const struct udevice_id altera_nios2_ids[] = {
139bcae80e9SThomas Chou 	{ .compatible = "altr,nios2-1.0" },
140bcae80e9SThomas Chou 	{ .compatible = "altr,nios2-1.1" },
141bcae80e9SThomas Chou 	{ }
142bcae80e9SThomas Chou };
143bcae80e9SThomas Chou 
144bcae80e9SThomas Chou U_BOOT_DRIVER(altera_nios2) = {
145bcae80e9SThomas Chou 	.name		= "altera_nios2",
146bcae80e9SThomas Chou 	.id		= UCLASS_CPU,
147bcae80e9SThomas Chou 	.of_match	= altera_nios2_ids,
148bcae80e9SThomas Chou 	.probe		= altera_nios2_probe,
149bcae80e9SThomas Chou 	.ops		= &altera_nios2_ops,
150bcae80e9SThomas Chou 	.flags		= DM_FLAG_PRE_RELOC,
151bcae80e9SThomas Chou };
152f1683aa7SSimon Glass 
153f1683aa7SSimon Glass /* This is a dummy function on nios2 */
dram_init(void)154f1683aa7SSimon Glass int dram_init(void)
155f1683aa7SSimon Glass {
156f1683aa7SSimon Glass 	return 0;
157f1683aa7SSimon Glass }
158