mpc512x_shared.c 14 KB

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