diu.c 11 KB

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