mpc512x_shared.c 11 KB

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