xref: /OK3568_Linux_fs/kernel/arch/powerpc/platforms/512x/mpc512x_shared.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2007,2008 Freescale Semiconductor, Inc. All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Author: John Rigby <jrigby@freescale.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Description:
8*4882a593Smuzhiyun  * MPC512x Shared code
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/clk.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/io.h>
14*4882a593Smuzhiyun #include <linux/irq.h>
15*4882a593Smuzhiyun #include <linux/of_platform.h>
16*4882a593Smuzhiyun #include <linux/fsl-diu-fb.h>
17*4882a593Smuzhiyun #include <linux/memblock.h>
18*4882a593Smuzhiyun #include <sysdev/fsl_soc.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <asm/cacheflush.h>
21*4882a593Smuzhiyun #include <asm/machdep.h>
22*4882a593Smuzhiyun #include <asm/ipic.h>
23*4882a593Smuzhiyun #include <asm/prom.h>
24*4882a593Smuzhiyun #include <asm/time.h>
25*4882a593Smuzhiyun #include <asm/mpc5121.h>
26*4882a593Smuzhiyun #include <asm/mpc52xx_psc.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include "mpc512x.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static struct mpc512x_reset_module __iomem *reset_module_base;
31*4882a593Smuzhiyun 
mpc512x_restart_init(void)32*4882a593Smuzhiyun static void __init mpc512x_restart_init(void)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	struct device_node *np;
35*4882a593Smuzhiyun 	const char *reset_compat;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	reset_compat = mpc512x_select_reset_compat();
38*4882a593Smuzhiyun 	np = of_find_compatible_node(NULL, NULL, reset_compat);
39*4882a593Smuzhiyun 	if (!np)
40*4882a593Smuzhiyun 		return;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	reset_module_base = of_iomap(np, 0);
43*4882a593Smuzhiyun 	of_node_put(np);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
mpc512x_restart(char * cmd)46*4882a593Smuzhiyun void __noreturn mpc512x_restart(char *cmd)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	if (reset_module_base) {
49*4882a593Smuzhiyun 		/* Enable software reset "RSTE" */
50*4882a593Smuzhiyun 		out_be32(&reset_module_base->rpr, 0x52535445);
51*4882a593Smuzhiyun 		/* Set software hard reset */
52*4882a593Smuzhiyun 		out_be32(&reset_module_base->rcr, 0x2);
53*4882a593Smuzhiyun 	} else {
54*4882a593Smuzhiyun 		pr_err("Restart module not mapped.\n");
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 	for (;;)
57*4882a593Smuzhiyun 		;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun struct fsl_diu_shared_fb {
61*4882a593Smuzhiyun 	u8		gamma[0x300];	/* 32-bit aligned! */
62*4882a593Smuzhiyun 	struct diu_ad	ad0;		/* 32-bit aligned! */
63*4882a593Smuzhiyun 	phys_addr_t	fb_phys;
64*4882a593Smuzhiyun 	size_t		fb_len;
65*4882a593Smuzhiyun 	bool		in_use;
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /* receives a pixel clock spec in pico seconds, adjusts the DIU clock rate */
mpc512x_set_pixel_clock(unsigned int pixclock)69*4882a593Smuzhiyun static void mpc512x_set_pixel_clock(unsigned int pixclock)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	struct device_node *np;
72*4882a593Smuzhiyun 	struct clk *clk_diu;
73*4882a593Smuzhiyun 	unsigned long epsilon, minpixclock, maxpixclock;
74*4882a593Smuzhiyun 	unsigned long offset, want, got, delta;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	/* lookup and enable the DIU clock */
77*4882a593Smuzhiyun 	np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");
78*4882a593Smuzhiyun 	if (!np) {
79*4882a593Smuzhiyun 		pr_err("Could not find DIU device tree node.\n");
80*4882a593Smuzhiyun 		return;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 	clk_diu = of_clk_get(np, 0);
83*4882a593Smuzhiyun 	if (IS_ERR(clk_diu)) {
84*4882a593Smuzhiyun 		/* backwards compat with device trees that lack clock specs */
85*4882a593Smuzhiyun 		clk_diu = clk_get_sys(np->name, "ipg");
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 	of_node_put(np);
88*4882a593Smuzhiyun 	if (IS_ERR(clk_diu)) {
89*4882a593Smuzhiyun 		pr_err("Could not lookup DIU clock.\n");
90*4882a593Smuzhiyun 		return;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 	if (clk_prepare_enable(clk_diu)) {
93*4882a593Smuzhiyun 		pr_err("Could not enable DIU clock.\n");
94*4882a593Smuzhiyun 		return;
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/*
98*4882a593Smuzhiyun 	 * convert the picoseconds spec into the desired clock rate,
99*4882a593Smuzhiyun 	 * determine the acceptable clock range for the monitor (+/- 5%),
100*4882a593Smuzhiyun 	 * do the calculation in steps to avoid integer overflow
101*4882a593Smuzhiyun 	 */
102*4882a593Smuzhiyun 	pr_debug("DIU pixclock in ps - %u\n", pixclock);
103*4882a593Smuzhiyun 	pixclock = (1000000000 / pixclock) * 1000;
104*4882a593Smuzhiyun 	pr_debug("DIU pixclock freq  - %u\n", pixclock);
105*4882a593Smuzhiyun 	epsilon = pixclock / 20; /* pixclock * 0.05 */
106*4882a593Smuzhiyun 	pr_debug("DIU deviation      - %lu\n", epsilon);
107*4882a593Smuzhiyun 	minpixclock = pixclock - epsilon;
108*4882a593Smuzhiyun 	maxpixclock = pixclock + epsilon;
109*4882a593Smuzhiyun 	pr_debug("DIU minpixclock    - %lu\n", minpixclock);
110*4882a593Smuzhiyun 	pr_debug("DIU maxpixclock    - %lu\n", maxpixclock);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/*
113*4882a593Smuzhiyun 	 * check whether the DIU supports the desired pixel clock
114*4882a593Smuzhiyun 	 *
115*4882a593Smuzhiyun 	 * - simply request the desired clock and see what the
116*4882a593Smuzhiyun 	 *   platform's clock driver will make of it, assuming that it
117*4882a593Smuzhiyun 	 *   will setup the best approximation of the requested value
118*4882a593Smuzhiyun 	 * - try other candidate frequencies in the order of decreasing
119*4882a593Smuzhiyun 	 *   preference (i.e. with increasing distance from the desired
120*4882a593Smuzhiyun 	 *   pixel clock, and checking the lower frequency before the
121*4882a593Smuzhiyun 	 *   higher frequency to not overload the hardware) until the
122*4882a593Smuzhiyun 	 *   first match is found -- any potential subsequent match
123*4882a593Smuzhiyun 	 *   would only be as good as the former match or typically
124*4882a593Smuzhiyun 	 *   would be less preferrable
125*4882a593Smuzhiyun 	 *
126*4882a593Smuzhiyun 	 * the offset increment of pixelclock divided by 64 is an
127*4882a593Smuzhiyun 	 * arbitrary choice -- it's simple to calculate, in the typical
128*4882a593Smuzhiyun 	 * case we expect the first check to succeed already, in the
129*4882a593Smuzhiyun 	 * worst case seven frequencies get tested (the exact center and
130*4882a593Smuzhiyun 	 * three more values each to the left and to the right) before
131*4882a593Smuzhiyun 	 * the 5% tolerance window is exceeded, resulting in fast enough
132*4882a593Smuzhiyun 	 * execution yet high enough probability of finding a suitable
133*4882a593Smuzhiyun 	 * value, while the error rate will be in the order of single
134*4882a593Smuzhiyun 	 * percents
135*4882a593Smuzhiyun 	 */
136*4882a593Smuzhiyun 	for (offset = 0; offset <= epsilon; offset += pixclock / 64) {
137*4882a593Smuzhiyun 		want = pixclock - offset;
138*4882a593Smuzhiyun 		pr_debug("DIU checking clock - %lu\n", want);
139*4882a593Smuzhiyun 		clk_set_rate(clk_diu, want);
140*4882a593Smuzhiyun 		got = clk_get_rate(clk_diu);
141*4882a593Smuzhiyun 		delta = abs(pixclock - got);
142*4882a593Smuzhiyun 		if (delta < epsilon)
143*4882a593Smuzhiyun 			break;
144*4882a593Smuzhiyun 		if (!offset)
145*4882a593Smuzhiyun 			continue;
146*4882a593Smuzhiyun 		want = pixclock + offset;
147*4882a593Smuzhiyun 		pr_debug("DIU checking clock - %lu\n", want);
148*4882a593Smuzhiyun 		clk_set_rate(clk_diu, want);
149*4882a593Smuzhiyun 		got = clk_get_rate(clk_diu);
150*4882a593Smuzhiyun 		delta = abs(pixclock - got);
151*4882a593Smuzhiyun 		if (delta < epsilon)
152*4882a593Smuzhiyun 			break;
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 	if (offset <= epsilon) {
155*4882a593Smuzhiyun 		pr_debug("DIU clock accepted - %lu\n", want);
156*4882a593Smuzhiyun 		pr_debug("DIU pixclock want %u, got %lu, delta %lu, eps %lu\n",
157*4882a593Smuzhiyun 			 pixclock, got, delta, epsilon);
158*4882a593Smuzhiyun 		return;
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun 	pr_warn("DIU pixclock auto search unsuccessful\n");
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	/*
163*4882a593Smuzhiyun 	 * what is the most appropriate action to take when the search
164*4882a593Smuzhiyun 	 * for an available pixel clock which is acceptable to the
165*4882a593Smuzhiyun 	 * monitor has failed?  disable the DIU (clock) or just provide
166*4882a593Smuzhiyun 	 * a "best effort"?  we go with the latter
167*4882a593Smuzhiyun 	 */
168*4882a593Smuzhiyun 	pr_warn("DIU pixclock best effort fallback (backend's choice)\n");
169*4882a593Smuzhiyun 	clk_set_rate(clk_diu, pixclock);
170*4882a593Smuzhiyun 	got = clk_get_rate(clk_diu);
171*4882a593Smuzhiyun 	delta = abs(pixclock - got);
172*4882a593Smuzhiyun 	pr_debug("DIU pixclock want %u, got %lu, delta %lu, eps %lu\n",
173*4882a593Smuzhiyun 		 pixclock, got, delta, epsilon);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun static enum fsl_diu_monitor_port
mpc512x_valid_monitor_port(enum fsl_diu_monitor_port port)177*4882a593Smuzhiyun mpc512x_valid_monitor_port(enum fsl_diu_monitor_port port)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	return FSL_DIU_PORT_DVI;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun static struct fsl_diu_shared_fb __attribute__ ((__aligned__(8))) diu_shared_fb;
183*4882a593Smuzhiyun 
mpc512x_free_bootmem(struct page * page)184*4882a593Smuzhiyun static inline void mpc512x_free_bootmem(struct page *page)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	BUG_ON(PageTail(page));
187*4882a593Smuzhiyun 	BUG_ON(page_ref_count(page) > 1);
188*4882a593Smuzhiyun 	free_reserved_page(page);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
mpc512x_release_bootmem(void)191*4882a593Smuzhiyun static void mpc512x_release_bootmem(void)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun 	unsigned long addr = diu_shared_fb.fb_phys & PAGE_MASK;
194*4882a593Smuzhiyun 	unsigned long size = diu_shared_fb.fb_len;
195*4882a593Smuzhiyun 	unsigned long start, end;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (diu_shared_fb.in_use) {
198*4882a593Smuzhiyun 		start = PFN_UP(addr);
199*4882a593Smuzhiyun 		end = PFN_DOWN(addr + size);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 		for (; start < end; start++)
202*4882a593Smuzhiyun 			mpc512x_free_bootmem(pfn_to_page(start));
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 		diu_shared_fb.in_use = false;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 	diu_ops.release_bootmem	= NULL;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun /*
210*4882a593Smuzhiyun  * Check if DIU was pre-initialized. If so, perform steps
211*4882a593Smuzhiyun  * needed to continue displaying through the whole boot process.
212*4882a593Smuzhiyun  * Move area descriptor and gamma table elsewhere, they are
213*4882a593Smuzhiyun  * destroyed by bootmem allocator otherwise. The frame buffer
214*4882a593Smuzhiyun  * address range will be reserved in setup_arch() after bootmem
215*4882a593Smuzhiyun  * allocator is up.
216*4882a593Smuzhiyun  */
mpc512x_init_diu(void)217*4882a593Smuzhiyun static void __init mpc512x_init_diu(void)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	struct device_node *np;
220*4882a593Smuzhiyun 	struct diu __iomem *diu_reg;
221*4882a593Smuzhiyun 	phys_addr_t desc;
222*4882a593Smuzhiyun 	void __iomem *vaddr;
223*4882a593Smuzhiyun 	unsigned long mode, pix_fmt, res, bpp;
224*4882a593Smuzhiyun 	unsigned long dst;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");
227*4882a593Smuzhiyun 	if (!np) {
228*4882a593Smuzhiyun 		pr_err("No DIU node\n");
229*4882a593Smuzhiyun 		return;
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	diu_reg = of_iomap(np, 0);
233*4882a593Smuzhiyun 	of_node_put(np);
234*4882a593Smuzhiyun 	if (!diu_reg) {
235*4882a593Smuzhiyun 		pr_err("Can't map DIU\n");
236*4882a593Smuzhiyun 		return;
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	mode = in_be32(&diu_reg->diu_mode);
240*4882a593Smuzhiyun 	if (mode == MFB_MODE0) {
241*4882a593Smuzhiyun 		pr_info("%s: DIU OFF\n", __func__);
242*4882a593Smuzhiyun 		goto out;
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	desc = in_be32(&diu_reg->desc[0]);
246*4882a593Smuzhiyun 	vaddr = ioremap(desc, sizeof(struct diu_ad));
247*4882a593Smuzhiyun 	if (!vaddr) {
248*4882a593Smuzhiyun 		pr_err("Can't map DIU area desc.\n");
249*4882a593Smuzhiyun 		goto out;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 	memcpy(&diu_shared_fb.ad0, vaddr, sizeof(struct diu_ad));
252*4882a593Smuzhiyun 	/* flush fb area descriptor */
253*4882a593Smuzhiyun 	dst = (unsigned long)&diu_shared_fb.ad0;
254*4882a593Smuzhiyun 	flush_dcache_range(dst, dst + sizeof(struct diu_ad) - 1);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	res = in_be32(&diu_reg->disp_size);
257*4882a593Smuzhiyun 	pix_fmt = in_le32(vaddr);
258*4882a593Smuzhiyun 	bpp = ((pix_fmt >> 16) & 0x3) + 1;
259*4882a593Smuzhiyun 	diu_shared_fb.fb_phys = in_le32(vaddr + 4);
260*4882a593Smuzhiyun 	diu_shared_fb.fb_len = ((res & 0xfff0000) >> 16) * (res & 0xfff) * bpp;
261*4882a593Smuzhiyun 	diu_shared_fb.in_use = true;
262*4882a593Smuzhiyun 	iounmap(vaddr);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	desc = in_be32(&diu_reg->gamma);
265*4882a593Smuzhiyun 	vaddr = ioremap(desc, sizeof(diu_shared_fb.gamma));
266*4882a593Smuzhiyun 	if (!vaddr) {
267*4882a593Smuzhiyun 		pr_err("Can't map DIU area desc.\n");
268*4882a593Smuzhiyun 		diu_shared_fb.in_use = false;
269*4882a593Smuzhiyun 		goto out;
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 	memcpy(&diu_shared_fb.gamma, vaddr, sizeof(diu_shared_fb.gamma));
272*4882a593Smuzhiyun 	/* flush gamma table */
273*4882a593Smuzhiyun 	dst = (unsigned long)&diu_shared_fb.gamma;
274*4882a593Smuzhiyun 	flush_dcache_range(dst, dst + sizeof(diu_shared_fb.gamma) - 1);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	iounmap(vaddr);
277*4882a593Smuzhiyun 	out_be32(&diu_reg->gamma, virt_to_phys(&diu_shared_fb.gamma));
278*4882a593Smuzhiyun 	out_be32(&diu_reg->desc[1], 0);
279*4882a593Smuzhiyun 	out_be32(&diu_reg->desc[2], 0);
280*4882a593Smuzhiyun 	out_be32(&diu_reg->desc[0], virt_to_phys(&diu_shared_fb.ad0));
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun out:
283*4882a593Smuzhiyun 	iounmap(diu_reg);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
mpc512x_setup_diu(void)286*4882a593Smuzhiyun static void __init mpc512x_setup_diu(void)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	int ret;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/*
291*4882a593Smuzhiyun 	 * We do not allocate and configure new area for bitmap buffer
292*4882a593Smuzhiyun 	 * because it would requere copying bitmap data (splash image)
293*4882a593Smuzhiyun 	 * and so negatively affect boot time. Instead we reserve the
294*4882a593Smuzhiyun 	 * already configured frame buffer area so that it won't be
295*4882a593Smuzhiyun 	 * destroyed. The starting address of the area to reserve and
296*4882a593Smuzhiyun 	 * also it's length is passed to memblock_reserve(). It will be
297*4882a593Smuzhiyun 	 * freed later on first open of fbdev, when splash image is not
298*4882a593Smuzhiyun 	 * needed any more.
299*4882a593Smuzhiyun 	 */
300*4882a593Smuzhiyun 	if (diu_shared_fb.in_use) {
301*4882a593Smuzhiyun 		ret = memblock_reserve(diu_shared_fb.fb_phys,
302*4882a593Smuzhiyun 				       diu_shared_fb.fb_len);
303*4882a593Smuzhiyun 		if (ret) {
304*4882a593Smuzhiyun 			pr_err("%s: reserve bootmem failed\n", __func__);
305*4882a593Smuzhiyun 			diu_shared_fb.in_use = false;
306*4882a593Smuzhiyun 		}
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	diu_ops.set_pixel_clock		= mpc512x_set_pixel_clock;
310*4882a593Smuzhiyun 	diu_ops.valid_monitor_port	= mpc512x_valid_monitor_port;
311*4882a593Smuzhiyun 	diu_ops.release_bootmem		= mpc512x_release_bootmem;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun 
mpc512x_init_IRQ(void)314*4882a593Smuzhiyun void __init mpc512x_init_IRQ(void)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct device_node *np;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-ipic");
319*4882a593Smuzhiyun 	if (!np)
320*4882a593Smuzhiyun 		return;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	ipic_init(np, 0);
323*4882a593Smuzhiyun 	of_node_put(np);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	/*
326*4882a593Smuzhiyun 	 * Initialize the default interrupt mapping priorities,
327*4882a593Smuzhiyun 	 * in case the boot rom changed something on us.
328*4882a593Smuzhiyun 	 */
329*4882a593Smuzhiyun 	ipic_set_default_priority();
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun /*
333*4882a593Smuzhiyun  * Nodes to do bus probe on, soc and localbus
334*4882a593Smuzhiyun  */
335*4882a593Smuzhiyun static const struct of_device_id of_bus_ids[] __initconst = {
336*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5121-immr", },
337*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5121-localbus", },
338*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5121-mbx", },
339*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5121-nfc", },
340*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5121-sram", },
341*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5121-pci", },
342*4882a593Smuzhiyun 	{ .compatible = "gpio-leds", },
343*4882a593Smuzhiyun 	{},
344*4882a593Smuzhiyun };
345*4882a593Smuzhiyun 
mpc512x_declare_of_platform_devices(void)346*4882a593Smuzhiyun static void __init mpc512x_declare_of_platform_devices(void)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	if (of_platform_bus_probe(NULL, of_bus_ids, NULL))
349*4882a593Smuzhiyun 		printk(KERN_ERR __FILE__ ": "
350*4882a593Smuzhiyun 			"Error while probing of_platform bus\n");
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun #define DEFAULT_FIFO_SIZE 16
354*4882a593Smuzhiyun 
mpc512x_select_psc_compat(void)355*4882a593Smuzhiyun const char *mpc512x_select_psc_compat(void)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	if (of_machine_is_compatible("fsl,mpc5121"))
358*4882a593Smuzhiyun 		return "fsl,mpc5121-psc";
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	if (of_machine_is_compatible("fsl,mpc5125"))
361*4882a593Smuzhiyun 		return "fsl,mpc5125-psc";
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	return NULL;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
mpc512x_select_reset_compat(void)366*4882a593Smuzhiyun const char *mpc512x_select_reset_compat(void)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	if (of_machine_is_compatible("fsl,mpc5121"))
369*4882a593Smuzhiyun 		return "fsl,mpc5121-reset";
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	if (of_machine_is_compatible("fsl,mpc5125"))
372*4882a593Smuzhiyun 		return "fsl,mpc5125-reset";
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	return NULL;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
get_fifo_size(struct device_node * np,char * prop_name)377*4882a593Smuzhiyun static unsigned int __init get_fifo_size(struct device_node *np,
378*4882a593Smuzhiyun 					 char *prop_name)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	const unsigned int *fp;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	fp = of_get_property(np, prop_name, NULL);
383*4882a593Smuzhiyun 	if (fp)
384*4882a593Smuzhiyun 		return *fp;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	pr_warn("no %s property in %pOF node, defaulting to %d\n",
387*4882a593Smuzhiyun 		prop_name, np, DEFAULT_FIFO_SIZE);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	return DEFAULT_FIFO_SIZE;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun #define FIFOC(_base) ((struct mpc512x_psc_fifo __iomem *) \
393*4882a593Smuzhiyun 		    ((u32)(_base) + sizeof(struct mpc52xx_psc)))
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun /* Init PSC FIFO space for TX and RX slices */
mpc512x_psc_fifo_init(void)396*4882a593Smuzhiyun static void __init mpc512x_psc_fifo_init(void)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun 	struct device_node *np;
399*4882a593Smuzhiyun 	void __iomem *psc;
400*4882a593Smuzhiyun 	unsigned int tx_fifo_size;
401*4882a593Smuzhiyun 	unsigned int rx_fifo_size;
402*4882a593Smuzhiyun 	const char *psc_compat;
403*4882a593Smuzhiyun 	int fifobase = 0; /* current fifo address in 32 bit words */
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	psc_compat = mpc512x_select_psc_compat();
406*4882a593Smuzhiyun 	if (!psc_compat) {
407*4882a593Smuzhiyun 		pr_err("%s: no compatible devices found\n", __func__);
408*4882a593Smuzhiyun 		return;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	for_each_compatible_node(np, NULL, psc_compat) {
412*4882a593Smuzhiyun 		tx_fifo_size = get_fifo_size(np, "fsl,tx-fifo-size");
413*4882a593Smuzhiyun 		rx_fifo_size = get_fifo_size(np, "fsl,rx-fifo-size");
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 		/* size in register is in 4 byte units */
416*4882a593Smuzhiyun 		tx_fifo_size /= 4;
417*4882a593Smuzhiyun 		rx_fifo_size /= 4;
418*4882a593Smuzhiyun 		if (!tx_fifo_size)
419*4882a593Smuzhiyun 			tx_fifo_size = 1;
420*4882a593Smuzhiyun 		if (!rx_fifo_size)
421*4882a593Smuzhiyun 			rx_fifo_size = 1;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 		psc = of_iomap(np, 0);
424*4882a593Smuzhiyun 		if (!psc) {
425*4882a593Smuzhiyun 			pr_err("%s: Can't map %pOF device\n",
426*4882a593Smuzhiyun 				__func__, np);
427*4882a593Smuzhiyun 			continue;
428*4882a593Smuzhiyun 		}
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 		/* FIFO space is 4KiB, check if requested size is available */
431*4882a593Smuzhiyun 		if ((fifobase + tx_fifo_size + rx_fifo_size) > 0x1000) {
432*4882a593Smuzhiyun 			pr_err("%s: no fifo space available for %pOF\n",
433*4882a593Smuzhiyun 				__func__, np);
434*4882a593Smuzhiyun 			iounmap(psc);
435*4882a593Smuzhiyun 			/*
436*4882a593Smuzhiyun 			 * chances are that another device requests less
437*4882a593Smuzhiyun 			 * fifo space, so we continue.
438*4882a593Smuzhiyun 			 */
439*4882a593Smuzhiyun 			continue;
440*4882a593Smuzhiyun 		}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 		/* set tx and rx fifo size registers */
443*4882a593Smuzhiyun 		out_be32(&FIFOC(psc)->txsz, (fifobase << 16) | tx_fifo_size);
444*4882a593Smuzhiyun 		fifobase += tx_fifo_size;
445*4882a593Smuzhiyun 		out_be32(&FIFOC(psc)->rxsz, (fifobase << 16) | rx_fifo_size);
446*4882a593Smuzhiyun 		fifobase += rx_fifo_size;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 		/* reset and enable the slices */
449*4882a593Smuzhiyun 		out_be32(&FIFOC(psc)->txcmd, 0x80);
450*4882a593Smuzhiyun 		out_be32(&FIFOC(psc)->txcmd, 0x01);
451*4882a593Smuzhiyun 		out_be32(&FIFOC(psc)->rxcmd, 0x80);
452*4882a593Smuzhiyun 		out_be32(&FIFOC(psc)->rxcmd, 0x01);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 		iounmap(psc);
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
mpc512x_init_early(void)458*4882a593Smuzhiyun void __init mpc512x_init_early(void)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	mpc512x_restart_init();
461*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_FB_FSL_DIU))
462*4882a593Smuzhiyun 		mpc512x_init_diu();
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
mpc512x_init(void)465*4882a593Smuzhiyun void __init mpc512x_init(void)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun 	mpc5121_clk_init();
468*4882a593Smuzhiyun 	mpc512x_declare_of_platform_devices();
469*4882a593Smuzhiyun 	mpc512x_psc_fifo_init();
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
mpc512x_setup_arch(void)472*4882a593Smuzhiyun void __init mpc512x_setup_arch(void)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_FB_FSL_DIU))
475*4882a593Smuzhiyun 		mpc512x_setup_diu();
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun /**
479*4882a593Smuzhiyun  * mpc512x_cs_config - Setup chip select configuration
480*4882a593Smuzhiyun  * @cs: chip select number
481*4882a593Smuzhiyun  * @val: chip select configuration value
482*4882a593Smuzhiyun  *
483*4882a593Smuzhiyun  * Perform chip select configuration for devices on LocalPlus Bus.
484*4882a593Smuzhiyun  * Intended to dynamically reconfigure the chip select parameters
485*4882a593Smuzhiyun  * for configurable devices on the bus.
486*4882a593Smuzhiyun  */
mpc512x_cs_config(unsigned int cs,u32 val)487*4882a593Smuzhiyun int mpc512x_cs_config(unsigned int cs, u32 val)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	static struct mpc512x_lpc __iomem *lpc;
490*4882a593Smuzhiyun 	struct device_node *np;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	if (cs > 7)
493*4882a593Smuzhiyun 		return -EINVAL;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	if (!lpc) {
496*4882a593Smuzhiyun 		np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-lpc");
497*4882a593Smuzhiyun 		lpc = of_iomap(np, 0);
498*4882a593Smuzhiyun 		of_node_put(np);
499*4882a593Smuzhiyun 		if (!lpc)
500*4882a593Smuzhiyun 			return -ENOMEM;
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	out_be32(&lpc->cs_cfg[cs], val);
504*4882a593Smuzhiyun 	return 0;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun EXPORT_SYMBOL(mpc512x_cs_config);
507