1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Joshua Henderson, joshua.henderson@microchip.com
4 * Copyright (C) 2015 Microchip Technology Inc.  All rights reserved.
5 */
6#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/of_address.h>
9#include <linux/of_fdt.h>
10#include <linux/of_platform.h>
11#include <linux/platform_data/sdhci-pic32.h>
12
13#include <asm/fw/fw.h>
14#include <asm/mips-boards/generic.h>
15#include <asm/prom.h>
16
17#include "pic32mzda.h"
18
19const char *get_system_type(void)
20{
21	return "PIC32MZDA";
22}
23
24static ulong get_fdtaddr(void)
25{
26	ulong ftaddr = 0;
27
28	if (fw_passed_dtb && !fw_arg2 && !fw_arg3)
29		return (ulong)fw_passed_dtb;
30
31	if (&__dtb_start < &__dtb_end)
32		ftaddr = (ulong)__dtb_start;
33
34	return ftaddr;
35}
36
37void __init plat_mem_setup(void)
38{
39	void *dtb;
40
41	dtb = (void *)get_fdtaddr();
42	if (!dtb) {
43		pr_err("pic32: no DTB found.\n");
44		return;
45	}
46
47	/*
48	 * Load the builtin device tree. This causes the chosen node to be
49	 * parsed resulting in our memory appearing.
50	 */
51	__dt_setup_arch(dtb);
52
53	pr_info("Found following command lines\n");
54	pr_info(" boot_command_line: %s\n", boot_command_line);
55	pr_info(" arcs_cmdline     : %s\n", arcs_cmdline);
56#ifdef CONFIG_CMDLINE_BOOL
57	pr_info(" builtin_cmdline  : %s\n", CONFIG_CMDLINE);
58#endif
59	if (dtb != __dtb_start)
60		strlcpy(arcs_cmdline, boot_command_line, COMMAND_LINE_SIZE);
61
62#ifdef CONFIG_EARLY_PRINTK
63	fw_init_early_console();
64#endif
65	pic32_config_init();
66}
67
68static __init void pic32_init_cmdline(int argc, char *argv[])
69{
70	unsigned int count = COMMAND_LINE_SIZE - 1;
71	int i;
72	char *dst = &(arcs_cmdline[0]);
73	char *src;
74
75	for (i = 1; i < argc && count; ++i) {
76		src = argv[i];
77		while (*src && count) {
78			*dst++ = *src++;
79			--count;
80		}
81		*dst++ = ' ';
82	}
83	if (i > 1)
84		--dst;
85
86	*dst = 0;
87}
88
89void __init prom_init(void)
90{
91	pic32_init_cmdline((int)fw_arg0, (char **)fw_arg1);
92}
93
94void __init prom_free_prom_memory(void)
95{
96}
97
98void __init device_tree_init(void)
99{
100	if (!initial_boot_params)
101		return;
102
103	unflatten_and_copy_device_tree();
104}
105
106static struct pic32_sdhci_platform_data sdhci_data = {
107	.setup_dma = pic32_set_sdhci_adma_fifo_threshold,
108};
109
110static struct of_dev_auxdata pic32_auxdata_lookup[] __initdata = {
111	OF_DEV_AUXDATA("microchip,pic32mzda-sdhci", 0, "sdhci", &sdhci_data),
112	{ /* sentinel */}
113};
114
115static int __init pic32_of_prepare_platform_data(struct of_dev_auxdata *lookup)
116{
117	struct device_node *root, *np;
118	struct resource res;
119
120	root = of_find_node_by_path("/");
121
122	for (; lookup->compatible; lookup++) {
123		np = of_find_compatible_node(NULL, NULL, lookup->compatible);
124		if (np) {
125			lookup->name = (char *)np->name;
126			if (lookup->phys_addr)
127				continue;
128			if (!of_address_to_resource(np, 0, &res))
129				lookup->phys_addr = res.start;
130		}
131	}
132
133	return 0;
134}
135
136static int __init plat_of_setup(void)
137{
138	if (!of_have_populated_dt())
139		panic("Device tree not present");
140
141	pic32_of_prepare_platform_data(pic32_auxdata_lookup);
142	if (of_platform_default_populate(NULL, pic32_auxdata_lookup, NULL))
143		panic("Failed to populate DT");
144
145	return 0;
146}
147arch_initcall(plat_of_setup);
148