blob: e996e007bc44cef80ab30541f4b045bd5e87158c [file] [log] [blame]
John Rigbyfb180322008-07-09 14:54:03 -06001/*
2 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc. All rights reserved.
3 *
4 * Author: John Rigby <jrigby@freescale.com>
5 *
6 * Description:
7 * MPC512x Shared code
8 *
9 * This is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
Gerhard Sittigba218122013-11-30 23:51:36 +010015#include <linux/clk.h>
John Rigbyfb180322008-07-09 14:54:03 -060016#include <linux/kernel.h>
17#include <linux/io.h>
18#include <linux/irq.h>
19#include <linux/of_platform.h>
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000020#include <linux/fsl-diu-fb.h>
21#include <linux/bootmem.h>
22#include <sysdev/fsl_soc.h>
John Rigbyfb180322008-07-09 14:54:03 -060023
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000024#include <asm/cacheflush.h>
John Rigbyfb180322008-07-09 14:54:03 -060025#include <asm/machdep.h>
26#include <asm/ipic.h>
27#include <asm/prom.h>
28#include <asm/time.h>
Anatolij Gustschina8dbceb72010-02-16 10:36:26 -070029#include <asm/mpc5121.h>
Anatolij Gustschin2da8cb62010-04-30 13:21:26 +000030#include <asm/mpc52xx_psc.h>
John Rigbyfb180322008-07-09 14:54:03 -060031
32#include "mpc512x.h"
33
Anatolij Gustschina8dbceb72010-02-16 10:36:26 -070034static struct mpc512x_reset_module __iomem *reset_module_base;
35
36static void __init mpc512x_restart_init(void)
37{
38 struct device_node *np;
Matteo Facchinetti0875a882013-06-10 10:13:52 +020039 const char *reset_compat;
Anatolij Gustschina8dbceb72010-02-16 10:36:26 -070040
Matteo Facchinetti0875a882013-06-10 10:13:52 +020041 reset_compat = mpc512x_select_reset_compat();
42 np = of_find_compatible_node(NULL, NULL, reset_compat);
Anatolij Gustschina8dbceb72010-02-16 10:36:26 -070043 if (!np)
44 return;
45
46 reset_module_base = of_iomap(np, 0);
47 of_node_put(np);
48}
49
50void mpc512x_restart(char *cmd)
51{
52 if (reset_module_base) {
53 /* Enable software reset "RSTE" */
54 out_be32(&reset_module_base->rpr, 0x52535445);
55 /* Set software hard reset */
56 out_be32(&reset_module_base->rcr, 0x2);
57 } else {
58 pr_err("Restart module not mapped.\n");
59 }
60 for (;;)
61 ;
62}
63
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000064struct fsl_diu_shared_fb {
65 u8 gamma[0x300]; /* 32-bit aligned! */
66 struct diu_ad ad0; /* 32-bit aligned! */
67 phys_addr_t fb_phys;
68 size_t fb_len;
69 bool in_use;
70};
71
Gerhard Sittigba218122013-11-30 23:51:36 +010072/* receives a pixel clock spec in pico seconds, adjusts the DIU clock rate */
Brian Norris7e1981972013-10-11 10:37:38 -070073static void mpc512x_set_pixel_clock(unsigned int pixclock)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000074{
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000075 struct device_node *np;
Gerhard Sittigba218122013-11-30 23:51:36 +010076 struct clk *clk_diu;
77 unsigned long epsilon, minpixclock, maxpixclock;
78 unsigned long offset, want, got, delta;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000079
Gerhard Sittigba218122013-11-30 23:51:36 +010080 /* lookup and enable the DIU clock */
81 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000082 if (!np) {
Gerhard Sittigba218122013-11-30 23:51:36 +010083 pr_err("Could not find DIU device tree node.\n");
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000084 return;
85 }
Gerhard Sittigba218122013-11-30 23:51:36 +010086 clk_diu = of_clk_get(np, 0);
87 if (IS_ERR(clk_diu)) {
88 /* backwards compat with device trees that lack clock specs */
89 clk_diu = clk_get_sys(np->name, "ipg");
90 }
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000091 of_node_put(np);
Gerhard Sittigba218122013-11-30 23:51:36 +010092 if (IS_ERR(clk_diu)) {
93 pr_err("Could not lookup DIU clock.\n");
94 return;
95 }
96 if (clk_prepare_enable(clk_diu)) {
97 pr_err("Could not enable DIU clock.\n");
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +000098 return;
99 }
100
Gerhard Sittigba218122013-11-30 23:51:36 +0100101 /*
102 * convert the picoseconds spec into the desired clock rate,
103 * determine the acceptable clock range for the monitor (+/- 5%),
104 * do the calculation in steps to avoid integer overflow
105 */
106 pr_debug("DIU pixclock in ps - %u\n", pixclock);
107 pixclock = (1000000000 / pixclock) * 1000;
108 pr_debug("DIU pixclock freq - %u\n", pixclock);
109 epsilon = pixclock / 20; /* pixclock * 0.05 */
110 pr_debug("DIU deviation - %lu\n", epsilon);
111 minpixclock = pixclock - epsilon;
112 maxpixclock = pixclock + epsilon;
113 pr_debug("DIU minpixclock - %lu\n", minpixclock);
114 pr_debug("DIU maxpixclock - %lu\n", maxpixclock);
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000115
Gerhard Sittigba218122013-11-30 23:51:36 +0100116 /*
117 * check whether the DIU supports the desired pixel clock
118 *
119 * - simply request the desired clock and see what the
120 * platform's clock driver will make of it, assuming that it
121 * will setup the best approximation of the requested value
122 * - try other candidate frequencies in the order of decreasing
123 * preference (i.e. with increasing distance from the desired
124 * pixel clock, and checking the lower frequency before the
125 * higher frequency to not overload the hardware) until the
126 * first match is found -- any potential subsequent match
127 * would only be as good as the former match or typically
128 * would be less preferrable
129 *
130 * the offset increment of pixelclock divided by 64 is an
131 * arbitrary choice -- it's simple to calculate, in the typical
132 * case we expect the first check to succeed already, in the
133 * worst case seven frequencies get tested (the exact center and
134 * three more values each to the left and to the right) before
135 * the 5% tolerance window is exceeded, resulting in fast enough
136 * execution yet high enough probability of finding a suitable
137 * value, while the error rate will be in the order of single
138 * percents
139 */
140 for (offset = 0; offset <= epsilon; offset += pixclock / 64) {
141 want = pixclock - offset;
142 pr_debug("DIU checking clock - %lu\n", want);
143 clk_set_rate(clk_diu, want);
144 got = clk_get_rate(clk_diu);
145 delta = abs(pixclock - got);
146 if (delta < epsilon)
147 break;
148 if (!offset)
149 continue;
150 want = pixclock + offset;
151 pr_debug("DIU checking clock - %lu\n", want);
152 clk_set_rate(clk_diu, want);
153 got = clk_get_rate(clk_diu);
154 delta = abs(pixclock - got);
155 if (delta < epsilon)
156 break;
157 }
158 if (offset <= epsilon) {
159 pr_debug("DIU clock accepted - %lu\n", want);
160 pr_debug("DIU pixclock want %u, got %lu, delta %lu, eps %lu\n",
161 pixclock, got, delta, epsilon);
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000162 return;
163 }
Gerhard Sittigba218122013-11-30 23:51:36 +0100164 pr_warn("DIU pixclock auto search unsuccessful\n");
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000165
Gerhard Sittigba218122013-11-30 23:51:36 +0100166 /*
167 * what is the most appropriate action to take when the search
168 * for an available pixel clock which is acceptable to the
169 * monitor has failed? disable the DIU (clock) or just provide
170 * a "best effort"? we go with the latter
171 */
172 pr_warn("DIU pixclock best effort fallback (backend's choice)\n");
173 clk_set_rate(clk_diu, pixclock);
174 got = clk_get_rate(clk_diu);
175 delta = abs(pixclock - got);
176 pr_debug("DIU pixclock want %u, got %lu, delta %lu, eps %lu\n",
177 pixclock, got, delta, epsilon);
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000178}
179
Brian Norris7e1981972013-10-11 10:37:38 -0700180static enum fsl_diu_monitor_port
Timur Tabi7653aaa2011-07-09 15:38:14 -0500181mpc512x_valid_monitor_port(enum fsl_diu_monitor_port port)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000182{
Timur Tabi7653aaa2011-07-09 15:38:14 -0500183 return FSL_DIU_PORT_DVI;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000184}
185
186static struct fsl_diu_shared_fb __attribute__ ((__aligned__(8))) diu_shared_fb;
187
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000188static inline void mpc512x_free_bootmem(struct page *page)
189{
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000190 BUG_ON(PageTail(page));
191 BUG_ON(atomic_read(&page->_count) > 1);
Jiang Liu5d585e52013-04-29 15:06:47 -0700192 free_reserved_page(page);
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000193}
194
Brian Norris7e1981972013-10-11 10:37:38 -0700195static void mpc512x_release_bootmem(void)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000196{
197 unsigned long addr = diu_shared_fb.fb_phys & PAGE_MASK;
198 unsigned long size = diu_shared_fb.fb_len;
199 unsigned long start, end;
200
201 if (diu_shared_fb.in_use) {
202 start = PFN_UP(addr);
203 end = PFN_DOWN(addr + size);
204
205 for (; start < end; start++)
206 mpc512x_free_bootmem(pfn_to_page(start));
207
208 diu_shared_fb.in_use = false;
209 }
210 diu_ops.release_bootmem = NULL;
211}
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000212
213/*
214 * Check if DIU was pre-initialized. If so, perform steps
215 * needed to continue displaying through the whole boot process.
216 * Move area descriptor and gamma table elsewhere, they are
217 * destroyed by bootmem allocator otherwise. The frame buffer
218 * address range will be reserved in setup_arch() after bootmem
219 * allocator is up.
220 */
Brian Norris7e1981972013-10-11 10:37:38 -0700221static void __init mpc512x_init_diu(void)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000222{
223 struct device_node *np;
224 struct diu __iomem *diu_reg;
225 phys_addr_t desc;
226 void __iomem *vaddr;
227 unsigned long mode, pix_fmt, res, bpp;
228 unsigned long dst;
229
230 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");
231 if (!np) {
232 pr_err("No DIU node\n");
233 return;
234 }
235
236 diu_reg = of_iomap(np, 0);
237 of_node_put(np);
238 if (!diu_reg) {
239 pr_err("Can't map DIU\n");
240 return;
241 }
242
243 mode = in_be32(&diu_reg->diu_mode);
Timur Tabic4e5a022011-09-28 16:19:53 -0500244 if (mode == MFB_MODE0) {
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000245 pr_info("%s: DIU OFF\n", __func__);
246 goto out;
247 }
248
249 desc = in_be32(&diu_reg->desc[0]);
250 vaddr = ioremap(desc, sizeof(struct diu_ad));
251 if (!vaddr) {
252 pr_err("Can't map DIU area desc.\n");
253 goto out;
254 }
255 memcpy(&diu_shared_fb.ad0, vaddr, sizeof(struct diu_ad));
256 /* flush fb area descriptor */
257 dst = (unsigned long)&diu_shared_fb.ad0;
258 flush_dcache_range(dst, dst + sizeof(struct diu_ad) - 1);
259
260 res = in_be32(&diu_reg->disp_size);
261 pix_fmt = in_le32(vaddr);
262 bpp = ((pix_fmt >> 16) & 0x3) + 1;
263 diu_shared_fb.fb_phys = in_le32(vaddr + 4);
264 diu_shared_fb.fb_len = ((res & 0xfff0000) >> 16) * (res & 0xfff) * bpp;
265 diu_shared_fb.in_use = true;
266 iounmap(vaddr);
267
268 desc = in_be32(&diu_reg->gamma);
269 vaddr = ioremap(desc, sizeof(diu_shared_fb.gamma));
270 if (!vaddr) {
271 pr_err("Can't map DIU area desc.\n");
272 diu_shared_fb.in_use = false;
273 goto out;
274 }
275 memcpy(&diu_shared_fb.gamma, vaddr, sizeof(diu_shared_fb.gamma));
276 /* flush gamma table */
277 dst = (unsigned long)&diu_shared_fb.gamma;
278 flush_dcache_range(dst, dst + sizeof(diu_shared_fb.gamma) - 1);
279
280 iounmap(vaddr);
281 out_be32(&diu_reg->gamma, virt_to_phys(&diu_shared_fb.gamma));
282 out_be32(&diu_reg->desc[1], 0);
283 out_be32(&diu_reg->desc[2], 0);
284 out_be32(&diu_reg->desc[0], virt_to_phys(&diu_shared_fb.ad0));
285
286out:
287 iounmap(diu_reg);
288}
289
Brian Norris7e1981972013-10-11 10:37:38 -0700290static void __init mpc512x_setup_diu(void)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000291{
292 int ret;
293
294 /*
295 * We do not allocate and configure new area for bitmap buffer
296 * because it would requere copying bitmap data (splash image)
297 * and so negatively affect boot time. Instead we reserve the
298 * already configured frame buffer area so that it won't be
299 * destroyed. The starting address of the area to reserve and
300 * also it's length is passed to reserve_bootmem(). It will be
301 * freed later on first open of fbdev, when splash image is not
302 * needed any more.
303 */
304 if (diu_shared_fb.in_use) {
305 ret = reserve_bootmem(diu_shared_fb.fb_phys,
306 diu_shared_fb.fb_len,
307 BOOTMEM_EXCLUSIVE);
308 if (ret) {
309 pr_err("%s: reserve bootmem failed\n", __func__);
310 diu_shared_fb.in_use = false;
311 }
312 }
313
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000314 diu_ops.set_pixel_clock = mpc512x_set_pixel_clock;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500315 diu_ops.valid_monitor_port = mpc512x_valid_monitor_port;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000316 diu_ops.release_bootmem = mpc512x_release_bootmem;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +0000317}
318
John Rigbyfb180322008-07-09 14:54:03 -0600319void __init mpc512x_init_IRQ(void)
320{
321 struct device_node *np;
322
323 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-ipic");
324 if (!np)
325 return;
326
327 ipic_init(np, 0);
328 of_node_put(np);
329
330 /*
331 * Initialize the default interrupt mapping priorities,
332 * in case the boot rom changed something on us.
333 */
334 ipic_set_default_priority();
335}
336
337/*
338 * Nodes to do bus probe on, soc and localbus
339 */
Uwe Kleine-Königce6d73c2014-09-10 21:56:38 +0200340static const struct of_device_id of_bus_ids[] __initconst = {
John Rigbyfb180322008-07-09 14:54:03 -0600341 { .compatible = "fsl,mpc5121-immr", },
342 { .compatible = "fsl,mpc5121-localbus", },
Anatolij Gustschin534ada52013-01-24 10:51:18 +0100343 { .compatible = "fsl,mpc5121-mbx", },
344 { .compatible = "fsl,mpc5121-nfc", },
345 { .compatible = "fsl,mpc5121-sram", },
346 { .compatible = "fsl,mpc5121-pci", },
347 { .compatible = "gpio-leds", },
John Rigbyfb180322008-07-09 14:54:03 -0600348 {},
349};
350
Brian Norris7e1981972013-10-11 10:37:38 -0700351static void __init mpc512x_declare_of_platform_devices(void)
John Rigbyfb180322008-07-09 14:54:03 -0600352{
353 if (of_platform_bus_probe(NULL, of_bus_ids, NULL))
354 printk(KERN_ERR __FILE__ ": "
355 "Error while probing of_platform bus\n");
356}
357
Anatolij Gustschin2da8cb62010-04-30 13:21:26 +0000358#define DEFAULT_FIFO_SIZE 16
359
Matteo Facchinettia9b6aae2013-04-04 03:57:30 +0000360const char *mpc512x_select_psc_compat(void)
361{
362 if (of_machine_is_compatible("fsl,mpc5121"))
363 return "fsl,mpc5121-psc";
364
365 if (of_machine_is_compatible("fsl,mpc5125"))
366 return "fsl,mpc5125-psc";
367
368 return NULL;
369}
370
Matteo Facchinetti0875a882013-06-10 10:13:52 +0200371const char *mpc512x_select_reset_compat(void)
372{
373 if (of_machine_is_compatible("fsl,mpc5121"))
374 return "fsl,mpc5121-reset";
375
376 if (of_machine_is_compatible("fsl,mpc5125"))
377 return "fsl,mpc5125-reset";
378
379 return NULL;
380}
381
Anatolij Gustschin2da8cb62010-04-30 13:21:26 +0000382static unsigned int __init get_fifo_size(struct device_node *np,
383 char *prop_name)
384{
385 const unsigned int *fp;
386
387 fp = of_get_property(np, prop_name, NULL);
388 if (fp)
389 return *fp;
390
391 pr_warning("no %s property in %s node, defaulting to %d\n",
392 prop_name, np->full_name, DEFAULT_FIFO_SIZE);
393
394 return DEFAULT_FIFO_SIZE;
395}
396
397#define FIFOC(_base) ((struct mpc512x_psc_fifo __iomem *) \
398 ((u32)(_base) + sizeof(struct mpc52xx_psc)))
399
400/* Init PSC FIFO space for TX and RX slices */
Brian Norris7e1981972013-10-11 10:37:38 -0700401static void __init mpc512x_psc_fifo_init(void)
Anatolij Gustschin2da8cb62010-04-30 13:21:26 +0000402{
403 struct device_node *np;
404 void __iomem *psc;
405 unsigned int tx_fifo_size;
406 unsigned int rx_fifo_size;
Matteo Facchinettia9b6aae2013-04-04 03:57:30 +0000407 const char *psc_compat;
Anatolij Gustschin2da8cb62010-04-30 13:21:26 +0000408 int fifobase = 0; /* current fifo address in 32 bit words */
409
Matteo Facchinettia9b6aae2013-04-04 03:57:30 +0000410 psc_compat = mpc512x_select_psc_compat();
411 if (!psc_compat) {
412 pr_err("%s: no compatible devices found\n", __func__);
413 return;
414 }
415
416 for_each_compatible_node(np, NULL, psc_compat) {
Anatolij Gustschin2da8cb62010-04-30 13:21:26 +0000417 tx_fifo_size = get_fifo_size(np, "fsl,tx-fifo-size");
418 rx_fifo_size = get_fifo_size(np, "fsl,rx-fifo-size");
419
420 /* size in register is in 4 byte units */
421 tx_fifo_size /= 4;
422 rx_fifo_size /= 4;
423 if (!tx_fifo_size)
424 tx_fifo_size = 1;
425 if (!rx_fifo_size)
426 rx_fifo_size = 1;
427
428 psc = of_iomap(np, 0);
429 if (!psc) {
430 pr_err("%s: Can't map %s device\n",
431 __func__, np->full_name);
432 continue;
433 }
434
435 /* FIFO space is 4KiB, check if requested size is available */
436 if ((fifobase + tx_fifo_size + rx_fifo_size) > 0x1000) {
437 pr_err("%s: no fifo space available for %s\n",
438 __func__, np->full_name);
439 iounmap(psc);
440 /*
441 * chances are that another device requests less
442 * fifo space, so we continue.
443 */
444 continue;
445 }
446
447 /* set tx and rx fifo size registers */
448 out_be32(&FIFOC(psc)->txsz, (fifobase << 16) | tx_fifo_size);
449 fifobase += tx_fifo_size;
450 out_be32(&FIFOC(psc)->rxsz, (fifobase << 16) | rx_fifo_size);
451 fifobase += rx_fifo_size;
452
453 /* reset and enable the slices */
454 out_be32(&FIFOC(psc)->txcmd, 0x80);
455 out_be32(&FIFOC(psc)->txcmd, 0x01);
456 out_be32(&FIFOC(psc)->rxcmd, 0x80);
457 out_be32(&FIFOC(psc)->rxcmd, 0x01);
458
459 iounmap(psc);
460 }
461}
462
Gerhard Sittig2abbbb62013-05-14 04:40:53 +0000463void __init mpc512x_init_early(void)
464{
Gerhard Sittiga4f41242013-05-14 04:40:54 +0000465 mpc512x_restart_init();
Gerhard Sittig2abbbb62013-05-14 04:40:53 +0000466 if (IS_ENABLED(CONFIG_FB_FSL_DIU))
467 mpc512x_init_diu();
468}
469
Anatolij Gustschin284ed662010-02-16 10:35:13 -0700470void __init mpc512x_init(void)
471{
Anatolij Gustschin284ed662010-02-16 10:35:13 -0700472 mpc5121_clk_init();
Anatolij Gustschinf29bc0a2013-01-25 17:29:21 +0100473 mpc512x_declare_of_platform_devices();
Anatolij Gustschin2da8cb62010-04-30 13:21:26 +0000474 mpc512x_psc_fifo_init();
Anatolij Gustschin284ed662010-02-16 10:35:13 -0700475}
Anatolij Gustschinedfcf332013-02-04 11:16:02 +0100476
Gerhard Sittig2abbbb62013-05-14 04:40:53 +0000477void __init mpc512x_setup_arch(void)
478{
479 if (IS_ENABLED(CONFIG_FB_FSL_DIU))
480 mpc512x_setup_diu();
481}
482
Anatolij Gustschinedfcf332013-02-04 11:16:02 +0100483/**
484 * mpc512x_cs_config - Setup chip select configuration
485 * @cs: chip select number
486 * @val: chip select configuration value
487 *
488 * Perform chip select configuration for devices on LocalPlus Bus.
489 * Intended to dynamically reconfigure the chip select parameters
490 * for configurable devices on the bus.
491 */
492int mpc512x_cs_config(unsigned int cs, u32 val)
493{
494 static struct mpc512x_lpc __iomem *lpc;
495 struct device_node *np;
496
497 if (cs > 7)
498 return -EINVAL;
499
500 if (!lpc) {
501 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-lpc");
502 lpc = of_iomap(np, 0);
503 of_node_put(np);
504 if (!lpc)
505 return -ENOMEM;
506 }
507
508 out_be32(&lpc->cs_cfg[cs], val);
509 return 0;
510}
511EXPORT_SYMBOL(mpc512x_cs_config);