mpc512x_shared.c 12 KB

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