mpc512x_shared.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. struct fsl_diu_shared_fb {
  55. u8 gamma[0x300]; /* 32-bit aligned! */
  56. struct diu_ad ad0; /* 32-bit aligned! */
  57. phys_addr_t fb_phys;
  58. size_t fb_len;
  59. bool in_use;
  60. };
  61. #define DIU_DIV_MASK 0x000000ff
  62. static void mpc512x_set_pixel_clock(unsigned int pixclock)
  63. {
  64. unsigned long bestval, bestfreq, speed, busfreq;
  65. unsigned long minpixclock, maxpixclock, pixval;
  66. struct mpc512x_ccm __iomem *ccm;
  67. struct device_node *np;
  68. u32 temp;
  69. long err;
  70. int i;
  71. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
  72. if (!np) {
  73. pr_err("Can't find clock control module.\n");
  74. return;
  75. }
  76. ccm = of_iomap(np, 0);
  77. of_node_put(np);
  78. if (!ccm) {
  79. pr_err("Can't map clock control module reg.\n");
  80. return;
  81. }
  82. np = of_find_node_by_type(NULL, "cpu");
  83. if (np) {
  84. const unsigned int *prop =
  85. of_get_property(np, "bus-frequency", NULL);
  86. of_node_put(np);
  87. if (prop) {
  88. busfreq = *prop;
  89. } else {
  90. pr_err("Can't get bus-frequency property\n");
  91. return;
  92. }
  93. } else {
  94. pr_err("Can't find 'cpu' node.\n");
  95. return;
  96. }
  97. /* Pixel Clock configuration */
  98. pr_debug("DIU: Bus Frequency = %lu\n", busfreq);
  99. speed = busfreq * 4; /* DIU_DIV ratio is 4 * CSB_CLK / DIU_CLK */
  100. /* Calculate the pixel clock with the smallest error */
  101. /* calculate the following in steps to avoid overflow */
  102. pr_debug("DIU pixclock in ps - %d\n", pixclock);
  103. temp = (1000000000 / pixclock) * 1000;
  104. pixclock = temp;
  105. pr_debug("DIU pixclock freq - %u\n", pixclock);
  106. temp = temp / 20; /* pixclock * 0.05 */
  107. pr_debug("deviation = %d\n", temp);
  108. minpixclock = pixclock - temp;
  109. maxpixclock = pixclock + temp;
  110. pr_debug("DIU minpixclock - %lu\n", minpixclock);
  111. pr_debug("DIU maxpixclock - %lu\n", maxpixclock);
  112. pixval = speed/pixclock;
  113. pr_debug("DIU pixval = %lu\n", pixval);
  114. err = LONG_MAX;
  115. bestval = pixval;
  116. pr_debug("DIU bestval = %lu\n", bestval);
  117. bestfreq = 0;
  118. for (i = -1; i <= 1; i++) {
  119. temp = speed / (pixval+i);
  120. pr_debug("DIU test pixval i=%d, pixval=%lu, temp freq. = %u\n",
  121. i, pixval, temp);
  122. if ((temp < minpixclock) || (temp > maxpixclock))
  123. pr_debug("DIU exceeds monitor range (%lu to %lu)\n",
  124. minpixclock, maxpixclock);
  125. else if (abs(temp - pixclock) < err) {
  126. pr_debug("Entered the else if block %d\n", i);
  127. err = abs(temp - pixclock);
  128. bestval = pixval + i;
  129. bestfreq = temp;
  130. }
  131. }
  132. pr_debug("DIU chose = %lx\n", bestval);
  133. pr_debug("DIU error = %ld\n NomPixClk ", err);
  134. pr_debug("DIU: Best Freq = %lx\n", bestfreq);
  135. /* Modify DIU_DIV in CCM SCFR1 */
  136. temp = in_be32(&ccm->scfr1);
  137. pr_debug("DIU: Current value of SCFR1: 0x%08x\n", temp);
  138. temp &= ~DIU_DIV_MASK;
  139. temp |= (bestval & DIU_DIV_MASK);
  140. out_be32(&ccm->scfr1, temp);
  141. pr_debug("DIU: Modified value of SCFR1: 0x%08x\n", temp);
  142. iounmap(ccm);
  143. }
  144. static enum fsl_diu_monitor_port
  145. mpc512x_valid_monitor_port(enum fsl_diu_monitor_port port)
  146. {
  147. return FSL_DIU_PORT_DVI;
  148. }
  149. static struct fsl_diu_shared_fb __attribute__ ((__aligned__(8))) diu_shared_fb;
  150. static inline void mpc512x_free_bootmem(struct page *page)
  151. {
  152. BUG_ON(PageTail(page));
  153. BUG_ON(atomic_read(&page->_count) > 1);
  154. free_reserved_page(page);
  155. }
  156. static void mpc512x_release_bootmem(void)
  157. {
  158. unsigned long addr = diu_shared_fb.fb_phys & PAGE_MASK;
  159. unsigned long size = diu_shared_fb.fb_len;
  160. unsigned long start, end;
  161. if (diu_shared_fb.in_use) {
  162. start = PFN_UP(addr);
  163. end = PFN_DOWN(addr + size);
  164. for (; start < end; start++)
  165. mpc512x_free_bootmem(pfn_to_page(start));
  166. diu_shared_fb.in_use = false;
  167. }
  168. diu_ops.release_bootmem = NULL;
  169. }
  170. /*
  171. * Check if DIU was pre-initialized. If so, perform steps
  172. * needed to continue displaying through the whole boot process.
  173. * Move area descriptor and gamma table elsewhere, they are
  174. * destroyed by bootmem allocator otherwise. The frame buffer
  175. * address range will be reserved in setup_arch() after bootmem
  176. * allocator is up.
  177. */
  178. static void __init mpc512x_init_diu(void)
  179. {
  180. struct device_node *np;
  181. struct diu __iomem *diu_reg;
  182. phys_addr_t desc;
  183. void __iomem *vaddr;
  184. unsigned long mode, pix_fmt, res, bpp;
  185. unsigned long dst;
  186. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");
  187. if (!np) {
  188. pr_err("No DIU node\n");
  189. return;
  190. }
  191. diu_reg = of_iomap(np, 0);
  192. of_node_put(np);
  193. if (!diu_reg) {
  194. pr_err("Can't map DIU\n");
  195. return;
  196. }
  197. mode = in_be32(&diu_reg->diu_mode);
  198. if (mode == MFB_MODE0) {
  199. pr_info("%s: DIU OFF\n", __func__);
  200. goto out;
  201. }
  202. desc = in_be32(&diu_reg->desc[0]);
  203. vaddr = ioremap(desc, sizeof(struct diu_ad));
  204. if (!vaddr) {
  205. pr_err("Can't map DIU area desc.\n");
  206. goto out;
  207. }
  208. memcpy(&diu_shared_fb.ad0, vaddr, sizeof(struct diu_ad));
  209. /* flush fb area descriptor */
  210. dst = (unsigned long)&diu_shared_fb.ad0;
  211. flush_dcache_range(dst, dst + sizeof(struct diu_ad) - 1);
  212. res = in_be32(&diu_reg->disp_size);
  213. pix_fmt = in_le32(vaddr);
  214. bpp = ((pix_fmt >> 16) & 0x3) + 1;
  215. diu_shared_fb.fb_phys = in_le32(vaddr + 4);
  216. diu_shared_fb.fb_len = ((res & 0xfff0000) >> 16) * (res & 0xfff) * bpp;
  217. diu_shared_fb.in_use = true;
  218. iounmap(vaddr);
  219. desc = in_be32(&diu_reg->gamma);
  220. vaddr = ioremap(desc, sizeof(diu_shared_fb.gamma));
  221. if (!vaddr) {
  222. pr_err("Can't map DIU area desc.\n");
  223. diu_shared_fb.in_use = false;
  224. goto out;
  225. }
  226. memcpy(&diu_shared_fb.gamma, vaddr, sizeof(diu_shared_fb.gamma));
  227. /* flush gamma table */
  228. dst = (unsigned long)&diu_shared_fb.gamma;
  229. flush_dcache_range(dst, dst + sizeof(diu_shared_fb.gamma) - 1);
  230. iounmap(vaddr);
  231. out_be32(&diu_reg->gamma, virt_to_phys(&diu_shared_fb.gamma));
  232. out_be32(&diu_reg->desc[1], 0);
  233. out_be32(&diu_reg->desc[2], 0);
  234. out_be32(&diu_reg->desc[0], virt_to_phys(&diu_shared_fb.ad0));
  235. out:
  236. iounmap(diu_reg);
  237. }
  238. static void __init mpc512x_setup_diu(void)
  239. {
  240. int ret;
  241. /*
  242. * We do not allocate and configure new area for bitmap buffer
  243. * because it would requere copying bitmap data (splash image)
  244. * and so negatively affect boot time. Instead we reserve the
  245. * already configured frame buffer area so that it won't be
  246. * destroyed. The starting address of the area to reserve and
  247. * also it's length is passed to reserve_bootmem(). It will be
  248. * freed later on first open of fbdev, when splash image is not
  249. * needed any more.
  250. */
  251. if (diu_shared_fb.in_use) {
  252. ret = reserve_bootmem(diu_shared_fb.fb_phys,
  253. diu_shared_fb.fb_len,
  254. BOOTMEM_EXCLUSIVE);
  255. if (ret) {
  256. pr_err("%s: reserve bootmem failed\n", __func__);
  257. diu_shared_fb.in_use = false;
  258. }
  259. }
  260. diu_ops.set_pixel_clock = mpc512x_set_pixel_clock;
  261. diu_ops.valid_monitor_port = mpc512x_valid_monitor_port;
  262. diu_ops.release_bootmem = mpc512x_release_bootmem;
  263. }
  264. void __init mpc512x_init_IRQ(void)
  265. {
  266. struct device_node *np;
  267. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-ipic");
  268. if (!np)
  269. return;
  270. ipic_init(np, 0);
  271. of_node_put(np);
  272. /*
  273. * Initialize the default interrupt mapping priorities,
  274. * in case the boot rom changed something on us.
  275. */
  276. ipic_set_default_priority();
  277. }
  278. /*
  279. * Nodes to do bus probe on, soc and localbus
  280. */
  281. static struct of_device_id __initdata of_bus_ids[] = {
  282. { .compatible = "fsl,mpc5121-immr", },
  283. { .compatible = "fsl,mpc5121-localbus", },
  284. { .compatible = "fsl,mpc5121-mbx", },
  285. { .compatible = "fsl,mpc5121-nfc", },
  286. { .compatible = "fsl,mpc5121-sram", },
  287. { .compatible = "fsl,mpc5121-pci", },
  288. { .compatible = "gpio-leds", },
  289. {},
  290. };
  291. static void __init mpc512x_declare_of_platform_devices(void)
  292. {
  293. if (of_platform_bus_probe(NULL, of_bus_ids, NULL))
  294. printk(KERN_ERR __FILE__ ": "
  295. "Error while probing of_platform bus\n");
  296. }
  297. #define DEFAULT_FIFO_SIZE 16
  298. const char *mpc512x_select_psc_compat(void)
  299. {
  300. if (of_machine_is_compatible("fsl,mpc5121"))
  301. return "fsl,mpc5121-psc";
  302. if (of_machine_is_compatible("fsl,mpc5125"))
  303. return "fsl,mpc5125-psc";
  304. return NULL;
  305. }
  306. const char *mpc512x_select_reset_compat(void)
  307. {
  308. if (of_machine_is_compatible("fsl,mpc5121"))
  309. return "fsl,mpc5121-reset";
  310. if (of_machine_is_compatible("fsl,mpc5125"))
  311. return "fsl,mpc5125-reset";
  312. return NULL;
  313. }
  314. static unsigned int __init get_fifo_size(struct device_node *np,
  315. char *prop_name)
  316. {
  317. const unsigned int *fp;
  318. fp = of_get_property(np, prop_name, NULL);
  319. if (fp)
  320. return *fp;
  321. pr_warning("no %s property in %s node, defaulting to %d\n",
  322. prop_name, np->full_name, DEFAULT_FIFO_SIZE);
  323. return DEFAULT_FIFO_SIZE;
  324. }
  325. #define FIFOC(_base) ((struct mpc512x_psc_fifo __iomem *) \
  326. ((u32)(_base) + sizeof(struct mpc52xx_psc)))
  327. /* Init PSC FIFO space for TX and RX slices */
  328. static void __init mpc512x_psc_fifo_init(void)
  329. {
  330. struct device_node *np;
  331. void __iomem *psc;
  332. unsigned int tx_fifo_size;
  333. unsigned int rx_fifo_size;
  334. const char *psc_compat;
  335. int fifobase = 0; /* current fifo address in 32 bit words */
  336. psc_compat = mpc512x_select_psc_compat();
  337. if (!psc_compat) {
  338. pr_err("%s: no compatible devices found\n", __func__);
  339. return;
  340. }
  341. for_each_compatible_node(np, NULL, psc_compat) {
  342. tx_fifo_size = get_fifo_size(np, "fsl,tx-fifo-size");
  343. rx_fifo_size = get_fifo_size(np, "fsl,rx-fifo-size");
  344. /* size in register is in 4 byte units */
  345. tx_fifo_size /= 4;
  346. rx_fifo_size /= 4;
  347. if (!tx_fifo_size)
  348. tx_fifo_size = 1;
  349. if (!rx_fifo_size)
  350. rx_fifo_size = 1;
  351. psc = of_iomap(np, 0);
  352. if (!psc) {
  353. pr_err("%s: Can't map %s device\n",
  354. __func__, np->full_name);
  355. continue;
  356. }
  357. /* FIFO space is 4KiB, check if requested size is available */
  358. if ((fifobase + tx_fifo_size + rx_fifo_size) > 0x1000) {
  359. pr_err("%s: no fifo space available for %s\n",
  360. __func__, np->full_name);
  361. iounmap(psc);
  362. /*
  363. * chances are that another device requests less
  364. * fifo space, so we continue.
  365. */
  366. continue;
  367. }
  368. /* set tx and rx fifo size registers */
  369. out_be32(&FIFOC(psc)->txsz, (fifobase << 16) | tx_fifo_size);
  370. fifobase += tx_fifo_size;
  371. out_be32(&FIFOC(psc)->rxsz, (fifobase << 16) | rx_fifo_size);
  372. fifobase += rx_fifo_size;
  373. /* reset and enable the slices */
  374. out_be32(&FIFOC(psc)->txcmd, 0x80);
  375. out_be32(&FIFOC(psc)->txcmd, 0x01);
  376. out_be32(&FIFOC(psc)->rxcmd, 0x80);
  377. out_be32(&FIFOC(psc)->rxcmd, 0x01);
  378. iounmap(psc);
  379. }
  380. }
  381. void __init mpc512x_init_early(void)
  382. {
  383. mpc512x_restart_init();
  384. if (IS_ENABLED(CONFIG_FB_FSL_DIU))
  385. mpc512x_init_diu();
  386. }
  387. void __init mpc512x_init(void)
  388. {
  389. mpc5121_clk_init();
  390. mpc512x_declare_of_platform_devices();
  391. mpc512x_psc_fifo_init();
  392. }
  393. void __init mpc512x_setup_arch(void)
  394. {
  395. if (IS_ENABLED(CONFIG_FB_FSL_DIU))
  396. mpc512x_setup_diu();
  397. }
  398. /**
  399. * mpc512x_cs_config - Setup chip select configuration
  400. * @cs: chip select number
  401. * @val: chip select configuration value
  402. *
  403. * Perform chip select configuration for devices on LocalPlus Bus.
  404. * Intended to dynamically reconfigure the chip select parameters
  405. * for configurable devices on the bus.
  406. */
  407. int mpc512x_cs_config(unsigned int cs, u32 val)
  408. {
  409. static struct mpc512x_lpc __iomem *lpc;
  410. struct device_node *np;
  411. if (cs > 7)
  412. return -EINVAL;
  413. if (!lpc) {
  414. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-lpc");
  415. lpc = of_iomap(np, 0);
  416. of_node_put(np);
  417. if (!lpc)
  418. return -ENOMEM;
  419. }
  420. out_be32(&lpc->cs_cfg[cs], val);
  421. return 0;
  422. }
  423. EXPORT_SYMBOL(mpc512x_cs_config);