mpc512x_shared.c 13 KB

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