mpc512x_shared.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/kernel.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/fsl-diu-fb.h>
  19. #include <linux/bootmem.h>
  20. #include <sysdev/fsl_soc.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/machdep.h>
  23. #include <asm/ipic.h>
  24. #include <asm/prom.h>
  25. #include <asm/time.h>
  26. #include <asm/mpc5121.h>
  27. #include <asm/mpc52xx_psc.h>
  28. #include "mpc512x.h"
  29. static struct mpc512x_reset_module __iomem *reset_module_base;
  30. static void __init mpc512x_restart_init(void)
  31. {
  32. struct device_node *np;
  33. const char *reset_compat;
  34. reset_compat = mpc512x_select_reset_compat();
  35. np = of_find_compatible_node(NULL, NULL, reset_compat);
  36. if (!np)
  37. return;
  38. reset_module_base = of_iomap(np, 0);
  39. of_node_put(np);
  40. }
  41. void mpc512x_restart(char *cmd)
  42. {
  43. if (reset_module_base) {
  44. /* Enable software reset "RSTE" */
  45. out_be32(&reset_module_base->rpr, 0x52535445);
  46. /* Set software hard reset */
  47. out_be32(&reset_module_base->rcr, 0x2);
  48. } else {
  49. pr_err("Restart module not mapped.\n");
  50. }
  51. for (;;)
  52. ;
  53. }
  54. #if IS_ENABLED(CONFIG_FB_FSL_DIU)
  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. #define DIU_DIV_MASK 0x000000ff
  63. void mpc512x_set_pixel_clock(unsigned int pixclock)
  64. {
  65. unsigned long bestval, bestfreq, speed, busfreq;
  66. unsigned long minpixclock, maxpixclock, pixval;
  67. struct mpc512x_ccm __iomem *ccm;
  68. struct device_node *np;
  69. u32 temp;
  70. long err;
  71. int i;
  72. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
  73. if (!np) {
  74. pr_err("Can't find clock control module.\n");
  75. return;
  76. }
  77. ccm = of_iomap(np, 0);
  78. of_node_put(np);
  79. if (!ccm) {
  80. pr_err("Can't map clock control module reg.\n");
  81. return;
  82. }
  83. np = of_find_node_by_type(NULL, "cpu");
  84. if (np) {
  85. const unsigned int *prop =
  86. of_get_property(np, "bus-frequency", NULL);
  87. of_node_put(np);
  88. if (prop) {
  89. busfreq = *prop;
  90. } else {
  91. pr_err("Can't get bus-frequency property\n");
  92. return;
  93. }
  94. } else {
  95. pr_err("Can't find 'cpu' node.\n");
  96. return;
  97. }
  98. /* Pixel Clock configuration */
  99. pr_debug("DIU: Bus Frequency = %lu\n", busfreq);
  100. speed = busfreq * 4; /* DIU_DIV ratio is 4 * CSB_CLK / DIU_CLK */
  101. /* Calculate the pixel clock with the smallest error */
  102. /* calculate the following in steps to avoid overflow */
  103. pr_debug("DIU pixclock in ps - %d\n", pixclock);
  104. temp = (1000000000 / pixclock) * 1000;
  105. pixclock = temp;
  106. pr_debug("DIU pixclock freq - %u\n", pixclock);
  107. temp = temp / 20; /* pixclock * 0.05 */
  108. pr_debug("deviation = %d\n", temp);
  109. minpixclock = pixclock - temp;
  110. maxpixclock = pixclock + temp;
  111. pr_debug("DIU minpixclock - %lu\n", minpixclock);
  112. pr_debug("DIU maxpixclock - %lu\n", maxpixclock);
  113. pixval = speed/pixclock;
  114. pr_debug("DIU pixval = %lu\n", pixval);
  115. err = LONG_MAX;
  116. bestval = pixval;
  117. pr_debug("DIU bestval = %lu\n", bestval);
  118. bestfreq = 0;
  119. for (i = -1; i <= 1; i++) {
  120. temp = speed / (pixval+i);
  121. pr_debug("DIU test pixval i=%d, pixval=%lu, temp freq. = %u\n",
  122. i, pixval, temp);
  123. if ((temp < minpixclock) || (temp > maxpixclock))
  124. pr_debug("DIU exceeds monitor range (%lu to %lu)\n",
  125. minpixclock, maxpixclock);
  126. else if (abs(temp - pixclock) < err) {
  127. pr_debug("Entered the else if block %d\n", i);
  128. err = abs(temp - pixclock);
  129. bestval = pixval + i;
  130. bestfreq = temp;
  131. }
  132. }
  133. pr_debug("DIU chose = %lx\n", bestval);
  134. pr_debug("DIU error = %ld\n NomPixClk ", err);
  135. pr_debug("DIU: Best Freq = %lx\n", bestfreq);
  136. /* Modify DIU_DIV in CCM SCFR1 */
  137. temp = in_be32(&ccm->scfr1);
  138. pr_debug("DIU: Current value of SCFR1: 0x%08x\n", temp);
  139. temp &= ~DIU_DIV_MASK;
  140. temp |= (bestval & DIU_DIV_MASK);
  141. out_be32(&ccm->scfr1, temp);
  142. pr_debug("DIU: Modified value of SCFR1: 0x%08x\n", temp);
  143. iounmap(ccm);
  144. }
  145. enum fsl_diu_monitor_port
  146. mpc512x_valid_monitor_port(enum fsl_diu_monitor_port port)
  147. {
  148. return FSL_DIU_PORT_DVI;
  149. }
  150. static struct fsl_diu_shared_fb __attribute__ ((__aligned__(8))) diu_shared_fb;
  151. static inline void mpc512x_free_bootmem(struct page *page)
  152. {
  153. BUG_ON(PageTail(page));
  154. BUG_ON(atomic_read(&page->_count) > 1);
  155. free_reserved_page(page);
  156. }
  157. void mpc512x_release_bootmem(void)
  158. {
  159. unsigned long addr = diu_shared_fb.fb_phys & PAGE_MASK;
  160. unsigned long size = diu_shared_fb.fb_len;
  161. unsigned long start, end;
  162. if (diu_shared_fb.in_use) {
  163. start = PFN_UP(addr);
  164. end = PFN_DOWN(addr + size);
  165. for (; start < end; start++)
  166. mpc512x_free_bootmem(pfn_to_page(start));
  167. diu_shared_fb.in_use = false;
  168. }
  169. diu_ops.release_bootmem = NULL;
  170. }
  171. /*
  172. * Check if DIU was pre-initialized. If so, perform steps
  173. * needed to continue displaying through the whole boot process.
  174. * Move area descriptor and gamma table elsewhere, they are
  175. * destroyed by bootmem allocator otherwise. The frame buffer
  176. * address range will be reserved in setup_arch() after bootmem
  177. * allocator is up.
  178. */
  179. void __init mpc512x_init_diu(void)
  180. {
  181. struct device_node *np;
  182. struct diu __iomem *diu_reg;
  183. phys_addr_t desc;
  184. void __iomem *vaddr;
  185. unsigned long mode, pix_fmt, res, bpp;
  186. unsigned long dst;
  187. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");
  188. if (!np) {
  189. pr_err("No DIU node\n");
  190. return;
  191. }
  192. diu_reg = of_iomap(np, 0);
  193. of_node_put(np);
  194. if (!diu_reg) {
  195. pr_err("Can't map DIU\n");
  196. return;
  197. }
  198. mode = in_be32(&diu_reg->diu_mode);
  199. if (mode == MFB_MODE0) {
  200. pr_info("%s: DIU OFF\n", __func__);
  201. goto out;
  202. }
  203. desc = in_be32(&diu_reg->desc[0]);
  204. vaddr = ioremap(desc, sizeof(struct diu_ad));
  205. if (!vaddr) {
  206. pr_err("Can't map DIU area desc.\n");
  207. goto out;
  208. }
  209. memcpy(&diu_shared_fb.ad0, vaddr, sizeof(struct diu_ad));
  210. /* flush fb area descriptor */
  211. dst = (unsigned long)&diu_shared_fb.ad0;
  212. flush_dcache_range(dst, dst + sizeof(struct diu_ad) - 1);
  213. res = in_be32(&diu_reg->disp_size);
  214. pix_fmt = in_le32(vaddr);
  215. bpp = ((pix_fmt >> 16) & 0x3) + 1;
  216. diu_shared_fb.fb_phys = in_le32(vaddr + 4);
  217. diu_shared_fb.fb_len = ((res & 0xfff0000) >> 16) * (res & 0xfff) * bpp;
  218. diu_shared_fb.in_use = true;
  219. iounmap(vaddr);
  220. desc = in_be32(&diu_reg->gamma);
  221. vaddr = ioremap(desc, sizeof(diu_shared_fb.gamma));
  222. if (!vaddr) {
  223. pr_err("Can't map DIU area desc.\n");
  224. diu_shared_fb.in_use = false;
  225. goto out;
  226. }
  227. memcpy(&diu_shared_fb.gamma, vaddr, sizeof(diu_shared_fb.gamma));
  228. /* flush gamma table */
  229. dst = (unsigned long)&diu_shared_fb.gamma;
  230. flush_dcache_range(dst, dst + sizeof(diu_shared_fb.gamma) - 1);
  231. iounmap(vaddr);
  232. out_be32(&diu_reg->gamma, virt_to_phys(&diu_shared_fb.gamma));
  233. out_be32(&diu_reg->desc[1], 0);
  234. out_be32(&diu_reg->desc[2], 0);
  235. out_be32(&diu_reg->desc[0], virt_to_phys(&diu_shared_fb.ad0));
  236. out:
  237. iounmap(diu_reg);
  238. }
  239. void __init mpc512x_setup_diu(void)
  240. {
  241. int ret;
  242. /*
  243. * We do not allocate and configure new area for bitmap buffer
  244. * because it would requere copying bitmap data (splash image)
  245. * and so negatively affect boot time. Instead we reserve the
  246. * already configured frame buffer area so that it won't be
  247. * destroyed. The starting address of the area to reserve and
  248. * also it's length is passed to reserve_bootmem(). It will be
  249. * freed later on first open of fbdev, when splash image is not
  250. * needed any more.
  251. */
  252. if (diu_shared_fb.in_use) {
  253. ret = reserve_bootmem(diu_shared_fb.fb_phys,
  254. diu_shared_fb.fb_len,
  255. BOOTMEM_EXCLUSIVE);
  256. if (ret) {
  257. pr_err("%s: reserve bootmem failed\n", __func__);
  258. diu_shared_fb.in_use = false;
  259. }
  260. }
  261. diu_ops.set_pixel_clock = mpc512x_set_pixel_clock;
  262. diu_ops.valid_monitor_port = mpc512x_valid_monitor_port;
  263. diu_ops.release_bootmem = mpc512x_release_bootmem;
  264. }
  265. #else
  266. void __init mpc512x_setup_diu(void) { /* EMPTY */ }
  267. void __init mpc512x_init_diu(void) { /* EMPTY */ }
  268. #endif
  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 struct of_device_id __initdata of_bus_ids[] = {
  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. 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 *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. const char *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_warning("no %s property in %s node, defaulting to %d\n",
  327. prop_name, np->full_name, 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. 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 %s device\n",
  359. __func__, np->full_name);
  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 %s\n",
  365. __func__, np->full_name);
  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. void __init mpc512x_init_early(void)
  387. {
  388. mpc512x_restart_init();
  389. if (IS_ENABLED(CONFIG_FB_FSL_DIU))
  390. mpc512x_init_diu();
  391. }
  392. void __init mpc512x_init(void)
  393. {
  394. mpc5121_clk_init();
  395. mpc512x_declare_of_platform_devices();
  396. mpc512x_psc_fifo_init();
  397. }
  398. void __init mpc512x_setup_arch(void)
  399. {
  400. if (IS_ENABLED(CONFIG_FB_FSL_DIU))
  401. mpc512x_setup_diu();
  402. }
  403. /**
  404. * mpc512x_cs_config - Setup chip select configuration
  405. * @cs: chip select number
  406. * @val: chip select configuration value
  407. *
  408. * Perform chip select configuration for devices on LocalPlus Bus.
  409. * Intended to dynamically reconfigure the chip select parameters
  410. * for configurable devices on the bus.
  411. */
  412. int mpc512x_cs_config(unsigned int cs, u32 val)
  413. {
  414. static struct mpc512x_lpc __iomem *lpc;
  415. struct device_node *np;
  416. if (cs > 7)
  417. return -EINVAL;
  418. if (!lpc) {
  419. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-lpc");
  420. lpc = of_iomap(np, 0);
  421. of_node_put(np);
  422. if (!lpc)
  423. return -ENOMEM;
  424. }
  425. out_be32(&lpc->cs_cfg[cs], val);
  426. return 0;
  427. }
  428. EXPORT_SYMBOL(mpc512x_cs_config);