mpc512x_shared.c 11 KB

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