diu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  3. * Authors: Timur Tabi <timur@freescale.com>
  4. *
  5. * FSL DIU Framebuffer driver
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. #include <common.h>
  13. #include <command.h>
  14. #include <asm/io.h>
  15. #include <stdio_dev.h>
  16. #include <video_fb.h>
  17. #include "../common/ngpixis.h"
  18. #include <fsl_diu_fb.h>
  19. /* The CTL register is called 'csr' in the ngpixis_t structure */
  20. #define PX_CTL_ALTACC 0x80
  21. #define PX_BRDCFG0_ELBC_SPI_MASK 0xc0
  22. #define PX_BRDCFG0_ELBC_SPI_ELBC 0x00
  23. #define PX_BRDCFG0_ELBC_SPI_NULL 0xc0
  24. #define PX_BRDCFG0_ELBC_DIU 0x02
  25. #define PX_BRDCFG1_DVIEN 0x80
  26. #define PX_BRDCFG1_DFPEN 0x40
  27. #define PX_BRDCFG1_BACKLIGHT 0x20
  28. #define PMUXCR_ELBCDIU_MASK 0xc0000000
  29. #define PMUXCR_ELBCDIU_NOR16 0x80000000
  30. #define PMUXCR_ELBCDIU_DIU 0x40000000
  31. /*
  32. * DIU Area Descriptor
  33. *
  34. * Note that we need to byte-swap the value before it's written to the AD
  35. * register. So even though the registers don't look like they're in the same
  36. * bit positions as they are on the MPC8610, the same value is written to the
  37. * AD register on the MPC8610 and on the P1022.
  38. */
  39. #define AD_BYTE_F 0x10000000
  40. #define AD_ALPHA_C_SHIFT 25
  41. #define AD_BLUE_C_SHIFT 23
  42. #define AD_GREEN_C_SHIFT 21
  43. #define AD_RED_C_SHIFT 19
  44. #define AD_PIXEL_S_SHIFT 16
  45. #define AD_COMP_3_SHIFT 12
  46. #define AD_COMP_2_SHIFT 8
  47. #define AD_COMP_1_SHIFT 4
  48. #define AD_COMP_0_SHIFT 0
  49. /*
  50. * Variables used by the DIU/LBC switching code. It's safe to makes these
  51. * global, because the DIU requires DDR, so we'll only run this code after
  52. * relocation.
  53. */
  54. static u8 px_brdcfg0;
  55. static u32 pmuxcr;
  56. static void *lbc_lcs0_ba;
  57. static void *lbc_lcs1_ba;
  58. void diu_set_pixel_clock(unsigned int pixclock)
  59. {
  60. ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  61. unsigned long speed_ccb, temp;
  62. u32 pixval;
  63. speed_ccb = get_bus_freq(0);
  64. temp = 1000000000 / pixclock;
  65. temp *= 1000;
  66. pixval = speed_ccb / temp;
  67. debug("DIU pixval = %lu\n", pixval);
  68. /* Modify PXCLK in GUTS CLKDVDR */
  69. temp = in_be32(&gur->clkdvdr) & 0x2000FFFF;
  70. out_be32(&gur->clkdvdr, temp); /* turn off clock */
  71. out_be32(&gur->clkdvdr, temp | 0x80000000 | ((pixval & 0x1F) << 16));
  72. }
  73. int platform_diu_init(unsigned int *xres, unsigned int *yres)
  74. {
  75. ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  76. char *monitor_port;
  77. u32 pixel_format;
  78. u8 temp;
  79. /* Save the LBC LCS0 and LCS1 addresses for the DIU mux functions */
  80. lbc_lcs0_ba = (void *)(get_lbc_br(0) & get_lbc_or(0) & 0xFFFF8000);
  81. lbc_lcs1_ba = (void *)(get_lbc_br(1) & get_lbc_or(1) & 0xFFFF8000);
  82. pixel_format = cpu_to_le32(AD_BYTE_F | (3 << AD_ALPHA_C_SHIFT) |
  83. (0 << AD_BLUE_C_SHIFT) | (1 << AD_GREEN_C_SHIFT) |
  84. (2 << AD_RED_C_SHIFT) | (8 << AD_COMP_3_SHIFT) |
  85. (8 << AD_COMP_2_SHIFT) | (8 << AD_COMP_1_SHIFT) |
  86. (8 << AD_COMP_0_SHIFT) | (3 << AD_PIXEL_S_SHIFT));
  87. temp = in_8(&pixis->brdcfg1);
  88. monitor_port = getenv("monitor");
  89. if (!strncmp(monitor_port, "1", 1)) { /* 1 - Single link LVDS */
  90. *xres = 1024;
  91. *yres = 768;
  92. /* Enable the DFP port, disable the DVI and the backlight */
  93. temp &= ~(PX_BRDCFG1_DVIEN | PX_BRDCFG1_BACKLIGHT);
  94. temp |= PX_BRDCFG1_DFPEN;
  95. } else { /* DVI */
  96. *xres = 1280;
  97. *yres = 1024;
  98. /* Enable the DVI port, disable the DFP and the backlight */
  99. temp &= ~(PX_BRDCFG1_DFPEN | PX_BRDCFG1_BACKLIGHT);
  100. temp |= PX_BRDCFG1_DVIEN;
  101. }
  102. out_8(&pixis->brdcfg1, temp);
  103. /*
  104. * Enable PIXIS indirect access mode. This is a hack that allows us to
  105. * access PIXIS registers even when the LBC pins have been muxed to the
  106. * DIU.
  107. */
  108. setbits_8(&pixis->csr, PX_CTL_ALTACC);
  109. /*
  110. * Route the LAD pins to the DIU. This will disable access to the eLBC,
  111. * which means we won't be able to read/write any NOR flash addresses!
  112. */
  113. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, brdcfg0));
  114. px_brdcfg0 = in_8(lbc_lcs1_ba);
  115. out_8(lbc_lcs1_ba, px_brdcfg0 | PX_BRDCFG0_ELBC_DIU);
  116. /* Set PMUXCR to switch the muxed pins from the LBC to the DIU */
  117. clrsetbits_be32(&gur->pmuxcr, PMUXCR_ELBCDIU_MASK, PMUXCR_ELBCDIU_DIU);
  118. pmuxcr = in_be32(&gur->pmuxcr);
  119. return fsl_diu_init(*xres, pixel_format, 0);
  120. }
  121. /*
  122. * set_mux_to_lbc - disable the DIU so that we can read/write to elbc
  123. *
  124. * On the Freescale P1022, the DIU video signal and the LBC address/data lines
  125. * share the same pins, which means that when the DIU is active (e.g. the
  126. * console is on the DVI display), NOR flash cannot be accessed. So we use the
  127. * weak accessor feature of the CFI flash code to temporarily switch the pin
  128. * mux from DIU to LBC whenever we want to read or write flash. This has a
  129. * significant performance penalty, but it's the only way to make it work.
  130. *
  131. * There are two muxes: one on the chip, and one on the board. The chip mux
  132. * controls whether the pins are used for the DIU or the LBC, and it is
  133. * set via PMUXCR. The board mux controls whether those signals go to
  134. * the video connector or the NOR flash chips, and it is set via the ngPIXIS.
  135. */
  136. static int set_mux_to_lbc(void)
  137. {
  138. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  139. /* Switch the muxes only if they're currently set to DIU mode */
  140. if ((in_be32(&gur->pmuxcr) & PMUXCR_ELBCDIU_MASK) !=
  141. PMUXCR_ELBCDIU_NOR16) {
  142. /*
  143. * In DIU mode, the PIXIS can only be accessed indirectly
  144. * since we can't read/write the LBC directly.
  145. */
  146. /* Set the board mux to LBC. This will disable the display. */
  147. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, brdcfg0));
  148. px_brdcfg0 = in_8(lbc_lcs1_ba);
  149. out_8(lbc_lcs1_ba, (px_brdcfg0 & ~(PX_BRDCFG0_ELBC_SPI_MASK
  150. | PX_BRDCFG0_ELBC_DIU)) | PX_BRDCFG0_ELBC_SPI_ELBC);
  151. /* Disable indirect PIXIS mode */
  152. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, csr));
  153. clrbits_8(lbc_lcs1_ba, PX_CTL_ALTACC);
  154. /* Set the chip mux to LBC mode, so that writes go to flash. */
  155. out_be32(&gur->pmuxcr, (pmuxcr & ~PMUXCR_ELBCDIU_MASK) |
  156. PMUXCR_ELBCDIU_NOR16);
  157. in_be32(&gur->pmuxcr);
  158. return 1;
  159. }
  160. return 0;
  161. }
  162. /*
  163. * set_mux_to_diu - re-enable the DIU muxing
  164. *
  165. * This function restores the chip and board muxing to point to the DIU.
  166. */
  167. static void set_mux_to_diu(void)
  168. {
  169. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  170. /* Enable indirect PIXIS mode */
  171. setbits_8(&pixis->csr, PX_CTL_ALTACC);
  172. /* Set the board mux to DIU. This will enable the display. */
  173. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, brdcfg0));
  174. out_8(lbc_lcs1_ba, px_brdcfg0);
  175. in_8(lbc_lcs1_ba);
  176. /* Set the chip mux to DIU mode. */
  177. out_be32(&gur->pmuxcr, pmuxcr);
  178. in_be32(&gur->pmuxcr);
  179. }
  180. /*
  181. * pixis_read - board-specific function to read from the PIXIS
  182. *
  183. * This function overrides the generic pixis_read() function, so that it can
  184. * use PIXIS indirect mode if necessary.
  185. */
  186. u8 pixis_read(unsigned int reg)
  187. {
  188. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  189. /* Use indirect mode if the mux is currently set to DIU mode */
  190. if ((in_be32(&gur->pmuxcr) & PMUXCR_ELBCDIU_MASK) !=
  191. PMUXCR_ELBCDIU_NOR16) {
  192. out_8(lbc_lcs0_ba, reg);
  193. return in_8(lbc_lcs1_ba);
  194. } else {
  195. void *p = (void *)PIXIS_BASE;
  196. return in_8(p + reg);
  197. }
  198. }
  199. /*
  200. * pixis_write - board-specific function to write to the PIXIS
  201. *
  202. * This function overrides the generic pixis_write() function, so that it can
  203. * use PIXIS indirect mode if necessary.
  204. */
  205. void pixis_write(unsigned int reg, u8 value)
  206. {
  207. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  208. /* Use indirect mode if the mux is currently set to DIU mode */
  209. if ((in_be32(&gur->pmuxcr) & PMUXCR_ELBCDIU_MASK) !=
  210. PMUXCR_ELBCDIU_NOR16) {
  211. out_8(lbc_lcs0_ba, reg);
  212. out_8(lbc_lcs1_ba, value);
  213. /* Do a read-back to ensure the write completed */
  214. in_8(lbc_lcs1_ba);
  215. } else {
  216. void *p = (void *)PIXIS_BASE;
  217. out_8(p + reg, value);
  218. }
  219. }
  220. void pixis_bank_reset(void)
  221. {
  222. /*
  223. * For some reason, a PIXIS bank reset does not work if the PIXIS is
  224. * in indirect mode, so switch to direct mode first.
  225. */
  226. set_mux_to_lbc();
  227. out_8(&pixis->vctl, 0);
  228. out_8(&pixis->vctl, 1);
  229. while (1);
  230. }
  231. #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
  232. void flash_write8(u8 value, void *addr)
  233. {
  234. int sw = set_mux_to_lbc();
  235. __raw_writeb(value, addr);
  236. if (sw) {
  237. /*
  238. * To ensure the post-write is completed to eLBC, software must
  239. * perform a dummy read from one valid address from eLBC space
  240. * before changing the eLBC_DIU from NOR mode to DIU mode.
  241. * set_mux_to_diu() includes a sync that will ensure the
  242. * __raw_readb() completes before it switches the mux.
  243. */
  244. __raw_readb(addr);
  245. set_mux_to_diu();
  246. }
  247. }
  248. void flash_write16(u16 value, void *addr)
  249. {
  250. int sw = set_mux_to_lbc();
  251. __raw_writew(value, addr);
  252. if (sw) {
  253. /*
  254. * To ensure the post-write is completed to eLBC, software must
  255. * perform a dummy read from one valid address from eLBC space
  256. * before changing the eLBC_DIU from NOR mode to DIU mode.
  257. * set_mux_to_diu() includes a sync that will ensure the
  258. * __raw_readb() completes before it switches the mux.
  259. */
  260. __raw_readb(addr);
  261. set_mux_to_diu();
  262. }
  263. }
  264. void flash_write32(u32 value, void *addr)
  265. {
  266. int sw = set_mux_to_lbc();
  267. __raw_writel(value, addr);
  268. if (sw) {
  269. /*
  270. * To ensure the post-write is completed to eLBC, software must
  271. * perform a dummy read from one valid address from eLBC space
  272. * before changing the eLBC_DIU from NOR mode to DIU mode.
  273. * set_mux_to_diu() includes a sync that will ensure the
  274. * __raw_readb() completes before it switches the mux.
  275. */
  276. __raw_readb(addr);
  277. set_mux_to_diu();
  278. }
  279. }
  280. void flash_write64(u64 value, void *addr)
  281. {
  282. int sw = set_mux_to_lbc();
  283. uint32_t *p = addr;
  284. /*
  285. * There is no __raw_writeq(), so do the write manually. We don't trust
  286. * the compiler, so we use inline assembly.
  287. */
  288. __asm__ __volatile__(
  289. "stw%U0%X0 %2,%0;\n"
  290. "stw%U1%X1 %3,%1;\n"
  291. : "=m" (*p), "=m" (*(p + 1))
  292. : "r" ((uint32_t) (value >> 32)), "r" ((uint32_t) (value)));
  293. if (sw) {
  294. /*
  295. * To ensure the post-write is completed to eLBC, software must
  296. * perform a dummy read from one valid address from eLBC space
  297. * before changing the eLBC_DIU from NOR mode to DIU mode. We
  298. * read addr+4 because we just wrote to addr+4, so that's how we
  299. * maintain execution order. set_mux_to_diu() includes a sync
  300. * that will ensure the __raw_readb() completes before it
  301. * switches the mux.
  302. */
  303. __raw_readb(addr + 4);
  304. set_mux_to_diu();
  305. }
  306. }
  307. u8 flash_read8(void *addr)
  308. {
  309. u8 ret;
  310. int sw = set_mux_to_lbc();
  311. ret = __raw_readb(addr);
  312. if (sw)
  313. set_mux_to_diu();
  314. return ret;
  315. }
  316. u16 flash_read16(void *addr)
  317. {
  318. u16 ret;
  319. int sw = set_mux_to_lbc();
  320. ret = __raw_readw(addr);
  321. if (sw)
  322. set_mux_to_diu();
  323. return ret;
  324. }
  325. u32 flash_read32(void *addr)
  326. {
  327. u32 ret;
  328. int sw = set_mux_to_lbc();
  329. ret = __raw_readl(addr);
  330. if (sw)
  331. set_mux_to_diu();
  332. return ret;
  333. }
  334. u64 flash_read64(void *addr)
  335. {
  336. u64 ret;
  337. int sw = set_mux_to_lbc();
  338. /* There is no __raw_readq(), so do the read manually */
  339. ret = *(volatile u64 *)addr;
  340. if (sw)
  341. set_mux_to_diu();
  342. return ret;
  343. }
  344. #endif