GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / xtensa / platforms / xtfpga / setup.c
1 /*
2  *
3  * arch/xtensa/platform/xtavnet/setup.c
4  *
5  * ...
6  *
7  * Authors:     Chris Zankel <chris@zankel.net>
8  *              Joe Taylor <joe@tensilica.com>
9  *
10  * Copyright 2001 - 2006 Tensilica Inc.
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  *
17  */
18 #include <linux/stddef.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/reboot.h>
23 #include <linux/kdev_t.h>
24 #include <linux/types.h>
25 #include <linux/major.h>
26 #include <linux/console.h>
27 #include <linux/delay.h>
28 #include <linux/of.h>
29 #include <linux/clk-provider.h>
30 #include <linux/of_address.h>
31
32 #include <asm/timex.h>
33 #include <asm/processor.h>
34 #include <asm/platform.h>
35 #include <asm/bootparam.h>
36 #include <platform/lcd.h>
37 #include <platform/hardware.h>
38
39 void platform_halt(void)
40 {
41         lcd_disp_at_pos(" HALT ", 0);
42         local_irq_disable();
43         while (1)
44                 cpu_relax();
45 }
46
47 void platform_power_off(void)
48 {
49         lcd_disp_at_pos("POWEROFF", 0);
50         local_irq_disable();
51         while (1)
52                 cpu_relax();
53 }
54
55 void platform_restart(void)
56 {
57         /* Try software reset first. */
58         WRITE_ONCE(*(u32 *)XTFPGA_SWRST_VADDR, 0xdead);
59
60         /* If software reset did not work, flush and reset the mmu,
61          * simulate a processor reset, and jump to the reset vector.
62          */
63         cpu_reset();
64         /* control never gets here */
65 }
66
67 void __init platform_setup(char **cmdline)
68 {
69 }
70
71 /* early initialization */
72
73 void __init platform_init(bp_tag_t *first)
74 {
75 }
76
77 /* Heartbeat. */
78
79 void platform_heartbeat(void)
80 {
81 }
82
83 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
84
85 void __init platform_calibrate_ccount(void)
86 {
87         ccount_freq = *(long *)XTFPGA_CLKFRQ_VADDR;
88 }
89
90 #endif
91
92 #ifdef CONFIG_USE_OF
93
94 static void __init xtfpga_clk_setup(struct device_node *np)
95 {
96         void __iomem *base = of_iomap(np, 0);
97         struct clk *clk;
98         u32 freq;
99
100         if (!base) {
101                 pr_err("%s: invalid address\n", np->name);
102                 return;
103         }
104
105         freq = __raw_readl(base);
106         iounmap(base);
107         clk = clk_register_fixed_rate(NULL, np->name, NULL, 0, freq);
108
109         if (IS_ERR(clk)) {
110                 pr_err("%s: clk registration failed\n", np->name);
111                 return;
112         }
113
114         if (of_clk_add_provider(np, of_clk_src_simple_get, clk)) {
115                 pr_err("%s: clk provider registration failed\n", np->name);
116                 return;
117         }
118 }
119 CLK_OF_DECLARE(xtfpga_clk, "cdns,xtfpga-clock", xtfpga_clk_setup);
120
121 #define MAC_LEN 6
122 static void __init update_local_mac(struct device_node *node)
123 {
124         struct property *newmac;
125         const u8* macaddr;
126         int prop_len;
127
128         macaddr = of_get_property(node, "local-mac-address", &prop_len);
129         if (macaddr == NULL || prop_len != MAC_LEN)
130                 return;
131
132         newmac = kzalloc(sizeof(*newmac) + MAC_LEN, GFP_KERNEL);
133         if (newmac == NULL)
134                 return;
135
136         newmac->value = newmac + 1;
137         newmac->length = MAC_LEN;
138         newmac->name = kstrdup("local-mac-address", GFP_KERNEL);
139         if (newmac->name == NULL) {
140                 kfree(newmac);
141                 return;
142         }
143
144         memcpy(newmac->value, macaddr, MAC_LEN);
145         ((u8*)newmac->value)[5] = (*(u32*)DIP_SWITCHES_VADDR) & 0x3f;
146         of_update_property(node, newmac);
147 }
148
149 static int __init machine_setup(void)
150 {
151         struct device_node *eth = NULL;
152
153         if ((eth = of_find_compatible_node(eth, NULL, "opencores,ethoc")))
154                 update_local_mac(eth);
155         of_node_put(eth);
156         return 0;
157 }
158 arch_initcall(machine_setup);
159
160 #else
161
162 #include <linux/serial_8250.h>
163 #include <linux/if.h>
164 #include <net/ethoc.h>
165 #include <linux/usb/c67x00.h>
166
167 /*----------------------------------------------------------------------------
168  *  Ethernet -- OpenCores Ethernet MAC (ethoc driver)
169  */
170
171 static struct resource ethoc_res[] = {
172         [0] = { /* register space */
173                 .start = OETH_REGS_PADDR,
174                 .end   = OETH_REGS_PADDR + OETH_REGS_SIZE - 1,
175                 .flags = IORESOURCE_MEM,
176         },
177         [1] = { /* buffer space */
178                 .start = OETH_SRAMBUFF_PADDR,
179                 .end   = OETH_SRAMBUFF_PADDR + OETH_SRAMBUFF_SIZE - 1,
180                 .flags = IORESOURCE_MEM,
181         },
182         [2] = { /* IRQ number */
183                 .start = XTENSA_PIC_LINUX_IRQ(OETH_IRQ),
184                 .end   = XTENSA_PIC_LINUX_IRQ(OETH_IRQ),
185                 .flags = IORESOURCE_IRQ,
186         },
187 };
188
189 static struct ethoc_platform_data ethoc_pdata = {
190         /*
191          * The MAC address for these boards is 00:50:c2:13:6f:xx.
192          * The last byte (here as zero) is read from the DIP switches on the
193          * board.
194          */
195         .hwaddr = { 0x00, 0x50, 0xc2, 0x13, 0x6f, 0 },
196         .phy_id = -1,
197         .big_endian = XCHAL_HAVE_BE,
198 };
199
200 static struct platform_device ethoc_device = {
201         .name = "ethoc",
202         .id = -1,
203         .num_resources = ARRAY_SIZE(ethoc_res),
204         .resource = ethoc_res,
205         .dev = {
206                 .platform_data = &ethoc_pdata,
207         },
208 };
209
210 /*----------------------------------------------------------------------------
211  *  USB Host/Device -- Cypress CY7C67300
212  */
213
214 static struct resource c67x00_res[] = {
215         [0] = { /* register space */
216                 .start = C67X00_PADDR,
217                 .end   = C67X00_PADDR + C67X00_SIZE - 1,
218                 .flags = IORESOURCE_MEM,
219         },
220         [1] = { /* IRQ number */
221                 .start = XTENSA_PIC_LINUX_IRQ(C67X00_IRQ),
222                 .end   = XTENSA_PIC_LINUX_IRQ(C67X00_IRQ),
223                 .flags = IORESOURCE_IRQ,
224         },
225 };
226
227 static struct c67x00_platform_data c67x00_pdata = {
228         .sie_config = C67X00_SIE1_HOST | C67X00_SIE2_UNUSED,
229         .hpi_regstep = 4,
230 };
231
232 static struct platform_device c67x00_device = {
233         .name = "c67x00",
234         .id = -1,
235         .num_resources = ARRAY_SIZE(c67x00_res),
236         .resource = c67x00_res,
237         .dev = {
238                 .platform_data = &c67x00_pdata,
239         },
240 };
241
242 /*----------------------------------------------------------------------------
243  *  UART
244  */
245
246 static struct resource serial_resource = {
247         .start  = DUART16552_PADDR,
248         .end    = DUART16552_PADDR + 0x1f,
249         .flags  = IORESOURCE_MEM,
250 };
251
252 static struct plat_serial8250_port serial_platform_data[] = {
253         [0] = {
254                 .mapbase        = DUART16552_PADDR,
255                 .irq            = XTENSA_PIC_LINUX_IRQ(DUART16552_INTNUM),
256                 .flags          = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST |
257                                   UPF_IOREMAP,
258                 .iotype         = XCHAL_HAVE_BE ? UPIO_MEM32BE : UPIO_MEM32,
259                 .regshift       = 2,
260                 .uartclk        = 0,    /* set in xtavnet_init() */
261         },
262         { },
263 };
264
265 static struct platform_device xtavnet_uart = {
266         .name           = "serial8250",
267         .id             = PLAT8250_DEV_PLATFORM,
268         .dev            = {
269                 .platform_data  = serial_platform_data,
270         },
271         .num_resources  = 1,
272         .resource       = &serial_resource,
273 };
274
275 /* platform devices */
276 static struct platform_device *platform_devices[] __initdata = {
277         &ethoc_device,
278         &c67x00_device,
279         &xtavnet_uart,
280 };
281
282
283 static int __init xtavnet_init(void)
284 {
285         /* Ethernet MAC address.  */
286         ethoc_pdata.hwaddr[5] = *(u32 *)DIP_SWITCHES_VADDR;
287
288         /* Clock rate varies among FPGA bitstreams; board specific FPGA register
289          * reports the actual clock rate.
290          */
291         serial_platform_data[0].uartclk = *(long *)XTFPGA_CLKFRQ_VADDR;
292
293
294         /* register platform devices */
295         platform_add_devices(platform_devices, ARRAY_SIZE(platform_devices));
296
297         /* ETHOC driver is a bit quiet; at least display Ethernet MAC, so user
298          * knows whether they set it correctly on the DIP switches.
299          */
300         pr_info("XTFPGA: Ethernet MAC %pM\n", ethoc_pdata.hwaddr);
301         ethoc_pdata.eth_clkfreq = *(long *)XTFPGA_CLKFRQ_VADDR;
302
303         return 0;
304 }
305
306 /*
307  * Register to be done during do_initcalls().
308  */
309 arch_initcall(xtavnet_init);
310
311 #endif /* CONFIG_USE_OF */