GNU Linux-libre 4.14.266-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         return 0;
156 }
157 arch_initcall(machine_setup);
158
159 #else
160
161 #include <linux/serial_8250.h>
162 #include <linux/if.h>
163 #include <net/ethoc.h>
164 #include <linux/usb/c67x00.h>
165
166 /*----------------------------------------------------------------------------
167  *  Ethernet -- OpenCores Ethernet MAC (ethoc driver)
168  */
169
170 static struct resource ethoc_res[] = {
171         [0] = { /* register space */
172                 .start = OETH_REGS_PADDR,
173                 .end   = OETH_REGS_PADDR + OETH_REGS_SIZE - 1,
174                 .flags = IORESOURCE_MEM,
175         },
176         [1] = { /* buffer space */
177                 .start = OETH_SRAMBUFF_PADDR,
178                 .end   = OETH_SRAMBUFF_PADDR + OETH_SRAMBUFF_SIZE - 1,
179                 .flags = IORESOURCE_MEM,
180         },
181         [2] = { /* IRQ number */
182                 .start = XTENSA_PIC_LINUX_IRQ(OETH_IRQ),
183                 .end   = XTENSA_PIC_LINUX_IRQ(OETH_IRQ),
184                 .flags = IORESOURCE_IRQ,
185         },
186 };
187
188 static struct ethoc_platform_data ethoc_pdata = {
189         /*
190          * The MAC address for these boards is 00:50:c2:13:6f:xx.
191          * The last byte (here as zero) is read from the DIP switches on the
192          * board.
193          */
194         .hwaddr = { 0x00, 0x50, 0xc2, 0x13, 0x6f, 0 },
195         .phy_id = -1,
196         .big_endian = XCHAL_HAVE_BE,
197 };
198
199 static struct platform_device ethoc_device = {
200         .name = "ethoc",
201         .id = -1,
202         .num_resources = ARRAY_SIZE(ethoc_res),
203         .resource = ethoc_res,
204         .dev = {
205                 .platform_data = &ethoc_pdata,
206         },
207 };
208
209 /*----------------------------------------------------------------------------
210  *  USB Host/Device -- Cypress CY7C67300
211  */
212
213 static struct resource c67x00_res[] = {
214         [0] = { /* register space */
215                 .start = C67X00_PADDR,
216                 .end   = C67X00_PADDR + C67X00_SIZE - 1,
217                 .flags = IORESOURCE_MEM,
218         },
219         [1] = { /* IRQ number */
220                 .start = XTENSA_PIC_LINUX_IRQ(C67X00_IRQ),
221                 .end   = XTENSA_PIC_LINUX_IRQ(C67X00_IRQ),
222                 .flags = IORESOURCE_IRQ,
223         },
224 };
225
226 static struct c67x00_platform_data c67x00_pdata = {
227         .sie_config = C67X00_SIE1_HOST | C67X00_SIE2_UNUSED,
228         .hpi_regstep = 4,
229 };
230
231 static struct platform_device c67x00_device = {
232         .name = "c67x00",
233         .id = -1,
234         .num_resources = ARRAY_SIZE(c67x00_res),
235         .resource = c67x00_res,
236         .dev = {
237                 .platform_data = &c67x00_pdata,
238         },
239 };
240
241 /*----------------------------------------------------------------------------
242  *  UART
243  */
244
245 static struct resource serial_resource = {
246         .start  = DUART16552_PADDR,
247         .end    = DUART16552_PADDR + 0x1f,
248         .flags  = IORESOURCE_MEM,
249 };
250
251 static struct plat_serial8250_port serial_platform_data[] = {
252         [0] = {
253                 .mapbase        = DUART16552_PADDR,
254                 .irq            = XTENSA_PIC_LINUX_IRQ(DUART16552_INTNUM),
255                 .flags          = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST |
256                                   UPF_IOREMAP,
257                 .iotype         = XCHAL_HAVE_BE ? UPIO_MEM32BE : UPIO_MEM32,
258                 .regshift       = 2,
259                 .uartclk        = 0,    /* set in xtavnet_init() */
260         },
261         { },
262 };
263
264 static struct platform_device xtavnet_uart = {
265         .name           = "serial8250",
266         .id             = PLAT8250_DEV_PLATFORM,
267         .dev            = {
268                 .platform_data  = serial_platform_data,
269         },
270         .num_resources  = 1,
271         .resource       = &serial_resource,
272 };
273
274 /* platform devices */
275 static struct platform_device *platform_devices[] __initdata = {
276         &ethoc_device,
277         &c67x00_device,
278         &xtavnet_uart,
279 };
280
281
282 static int __init xtavnet_init(void)
283 {
284         /* Ethernet MAC address.  */
285         ethoc_pdata.hwaddr[5] = *(u32 *)DIP_SWITCHES_VADDR;
286
287         /* Clock rate varies among FPGA bitstreams; board specific FPGA register
288          * reports the actual clock rate.
289          */
290         serial_platform_data[0].uartclk = *(long *)XTFPGA_CLKFRQ_VADDR;
291
292
293         /* register platform devices */
294         platform_add_devices(platform_devices, ARRAY_SIZE(platform_devices));
295
296         /* ETHOC driver is a bit quiet; at least display Ethernet MAC, so user
297          * knows whether they set it correctly on the DIP switches.
298          */
299         pr_info("XTFPGA: Ethernet MAC %pM\n", ethoc_pdata.hwaddr);
300         ethoc_pdata.eth_clkfreq = *(long *)XTFPGA_CLKFRQ_VADDR;
301
302         return 0;
303 }
304
305 /*
306  * Register to be done during do_initcalls().
307  */
308 arch_initcall(xtavnet_init);
309
310 #endif /* CONFIG_USE_OF */