Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / nios2 / cpu / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
4  * Scott McNutt <smcnutt@psyent.com>
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <cpu.h>
10 #include <cpu_func.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <init.h>
14 #include <irq_func.h>
15 #include <asm/cache.h>
16 #include <asm/system.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 #ifdef CONFIG_DISPLAY_CPUINFO
21 int print_cpuinfo(void)
22 {
23         printf("CPU:   Nios-II\n");
24         return 0;
25 }
26 #endif /* CONFIG_DISPLAY_CPUINFO */
27
28 #ifdef CONFIG_ALTERA_SYSID
29 int checkboard(void)
30 {
31         display_sysid();
32         return 0;
33 }
34 #endif
35
36 int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
37 {
38         disable_interrupts();
39         /* indirect call to go beyond 256MB limitation of toolchain */
40         nios2_callr(gd->arch.reset_addr);
41         return 0;
42 }
43
44 /*
45  * COPY EXCEPTION TRAMPOLINE -- copy the tramp to the
46  * exception address. Define CONFIG_ROM_STUBS to prevent
47  * the copy (e.g. exception in flash or in other
48  * softare/firmware component).
49  */
50 #ifndef CONFIG_ROM_STUBS
51 static void copy_exception_trampoline(void)
52 {
53         extern int _except_start, _except_end;
54         void *except_target = (void *)gd->arch.exception_addr;
55
56         if (&_except_start != except_target) {
57                 memcpy(except_target, &_except_start,
58                        &_except_end - &_except_start);
59                 flush_cache(gd->arch.exception_addr,
60                             &_except_end - &_except_start);
61         }
62 }
63 #endif
64
65 int arch_cpu_init_dm(void)
66 {
67         struct udevice *dev;
68         int ret;
69
70         ret = uclass_first_device_err(UCLASS_CPU, &dev);
71         if (ret)
72                 return ret;
73
74         gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
75 #ifndef CONFIG_ROM_STUBS
76         copy_exception_trampoline();
77 #endif
78
79         return 0;
80 }
81
82 static int altera_nios2_get_desc(struct udevice *dev, char *buf, int size)
83 {
84         const char *cpu_name = "Nios-II";
85
86         if (size < strlen(cpu_name))
87                 return -ENOSPC;
88         strcpy(buf, cpu_name);
89
90         return 0;
91 }
92
93 static int altera_nios2_get_info(struct udevice *dev, struct cpu_info *info)
94 {
95         info->cpu_freq = gd->cpu_clk;
96         info->features = (1 << CPU_FEAT_L1_CACHE) |
97                 (gd->arch.has_mmu ? (1 << CPU_FEAT_MMU) : 0);
98
99         return 0;
100 }
101
102 static int altera_nios2_get_count(struct udevice *dev)
103 {
104         return 1;
105 }
106
107 static int altera_nios2_probe(struct udevice *dev)
108 {
109         const void *blob = gd->fdt_blob;
110         int node = dev_of_offset(dev);
111
112         gd->cpu_clk = fdtdec_get_int(blob, node,
113                 "clock-frequency", 0);
114         gd->arch.dcache_line_size = fdtdec_get_int(blob, node,
115                 "dcache-line-size", 0);
116         gd->arch.icache_line_size = fdtdec_get_int(blob, node,
117                 "icache-line-size", 0);
118         gd->arch.dcache_size = fdtdec_get_int(blob, node,
119                 "dcache-size", 0);
120         gd->arch.icache_size = fdtdec_get_int(blob, node,
121                 "icache-size", 0);
122         gd->arch.reset_addr = fdtdec_get_int(blob, node,
123                 "altr,reset-addr", 0);
124         gd->arch.exception_addr = fdtdec_get_int(blob, node,
125                 "altr,exception-addr", 0);
126         gd->arch.has_initda = fdtdec_get_int(blob, node,
127                 "altr,has-initda", 0);
128         gd->arch.has_mmu = fdtdec_get_int(blob, node,
129                 "altr,has-mmu", 0);
130         gd->arch.io_region_base = gd->arch.has_mmu ? 0xe0000000 : 0x80000000;
131         gd->arch.mem_region_base = gd->arch.has_mmu ? 0xc0000000 : 0x00000000;
132         gd->arch.physaddr_mask = gd->arch.has_mmu ? 0x1fffffff : 0x7fffffff;
133
134         return 0;
135 }
136
137 static const struct cpu_ops altera_nios2_ops = {
138         .get_desc       = altera_nios2_get_desc,
139         .get_info       = altera_nios2_get_info,
140         .get_count      = altera_nios2_get_count,
141 };
142
143 static const struct udevice_id altera_nios2_ids[] = {
144         { .compatible = "altr,nios2-1.0" },
145         { .compatible = "altr,nios2-1.1" },
146         { }
147 };
148
149 U_BOOT_DRIVER(altera_nios2) = {
150         .name           = "altera_nios2",
151         .id             = UCLASS_CPU,
152         .of_match       = altera_nios2_ids,
153         .probe          = altera_nios2_probe,
154         .ops            = &altera_nios2_ops,
155         .flags          = DM_FLAG_PRE_RELOC,
156 };
157
158 /* This is a dummy function on nios2 */
159 int dram_init(void)
160 {
161         return 0;
162 }