diu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  3. * Authors: Timur Tabi <timur@freescale.com>
  4. *
  5. * FSL DIU Framebuffer driver
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <linux/ctype.h>
  12. #include <asm/io.h>
  13. #include <stdio_dev.h>
  14. #include <video_fb.h>
  15. #include "../common/ngpixis.h"
  16. #include <fsl_diu_fb.h>
  17. /* The CTL register is called 'csr' in the ngpixis_t structure */
  18. #define PX_CTL_ALTACC 0x80
  19. #define PX_BRDCFG0_ELBC_SPI_MASK 0xc0
  20. #define PX_BRDCFG0_ELBC_SPI_ELBC 0x00
  21. #define PX_BRDCFG0_ELBC_SPI_NULL 0xc0
  22. #define PX_BRDCFG0_ELBC_DIU 0x02
  23. #define PX_BRDCFG1_DVIEN 0x80
  24. #define PX_BRDCFG1_DFPEN 0x40
  25. #define PX_BRDCFG1_BACKLIGHT 0x20
  26. #define PMUXCR_ELBCDIU_MASK 0xc0000000
  27. #define PMUXCR_ELBCDIU_NOR16 0x80000000
  28. #define PMUXCR_ELBCDIU_DIU 0x40000000
  29. /*
  30. * DIU Area Descriptor
  31. *
  32. * Note that we need to byte-swap the value before it's written to the AD
  33. * register. So even though the registers don't look like they're in the same
  34. * bit positions as they are on the MPC8610, the same value is written to the
  35. * AD register on the MPC8610 and on the P1022.
  36. */
  37. #define AD_BYTE_F 0x10000000
  38. #define AD_ALPHA_C_SHIFT 25
  39. #define AD_BLUE_C_SHIFT 23
  40. #define AD_GREEN_C_SHIFT 21
  41. #define AD_RED_C_SHIFT 19
  42. #define AD_PIXEL_S_SHIFT 16
  43. #define AD_COMP_3_SHIFT 12
  44. #define AD_COMP_2_SHIFT 8
  45. #define AD_COMP_1_SHIFT 4
  46. #define AD_COMP_0_SHIFT 0
  47. /*
  48. * Variables used by the DIU/LBC switching code. It's safe to makes these
  49. * global, because the DIU requires DDR, so we'll only run this code after
  50. * relocation.
  51. */
  52. static u8 px_brdcfg0;
  53. static u32 pmuxcr;
  54. static void *lbc_lcs0_ba;
  55. static void *lbc_lcs1_ba;
  56. static u32 old_br0, old_or0, old_br1, old_or1;
  57. static u32 new_br0, new_or0, new_br1, new_or1;
  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 = %u\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, const char *port)
  74. {
  75. ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  76. const char *name;
  77. u32 pixel_format;
  78. u8 temp;
  79. phys_addr_t phys0, phys1; /* BR0/BR1 physical addresses */
  80. /*
  81. * Indirect mode requires both BR0 and BR1 to be set to "GPCM",
  82. * otherwise writes to these addresses won't actually appear on the
  83. * local bus, and so the PIXIS won't see them.
  84. *
  85. * In FCM mode, writes go to the NAND controller, which does not pass
  86. * them to the localbus directly. So we force BR0 and BR1 into GPCM
  87. * mode, since we don't care about what's behind the localbus any
  88. * more. However, we save those registers first, so that we can
  89. * restore them when necessary.
  90. */
  91. new_br0 = old_br0 = get_lbc_br(0);
  92. new_br1 = old_br1 = get_lbc_br(1);
  93. new_or0 = old_or0 = get_lbc_or(0);
  94. new_or1 = old_or1 = get_lbc_or(1);
  95. /*
  96. * Use the existing BRx/ORx values if it's already GPCM. Otherwise,
  97. * force the values to simple 32KB GPCM windows with the most
  98. * conservative timing.
  99. */
  100. if ((old_br0 & BR_MSEL) != BR_MS_GPCM) {
  101. new_br0 = (get_lbc_br(0) & BR_BA) | BR_V;
  102. new_or0 = OR_AM_32KB | 0xFF7;
  103. set_lbc_br(0, new_br0);
  104. set_lbc_or(0, new_or0);
  105. }
  106. if ((old_br1 & BR_MSEL) != BR_MS_GPCM) {
  107. new_br1 = (get_lbc_br(1) & BR_BA) | BR_V;
  108. new_or1 = OR_AM_32KB | 0xFF7;
  109. set_lbc_br(1, new_br1);
  110. set_lbc_or(1, new_or1);
  111. }
  112. /*
  113. * Determine the physical addresses for Chip Selects 0 and 1. The
  114. * BR0/BR1 registers contain the truncated physical addresses for the
  115. * chip selects, mapped via the localbus LAW. Since the BRx registers
  116. * only contain the lower 32 bits of the address, we have to determine
  117. * the upper 4 bits some other way. The proper way is to scan the LAW
  118. * table looking for a matching localbus address. Instead, we cheat.
  119. * We know that the upper bits are 0 for 32-bit addressing, or 0xF for
  120. * 36-bit addressing.
  121. */
  122. #ifdef CONFIG_PHYS_64BIT
  123. phys0 = 0xf00000000ULL | (old_br0 & old_or0 & BR_BA);
  124. phys1 = 0xf00000000ULL | (old_br1 & old_or1 & BR_BA);
  125. #else
  126. phys0 = old_br0 & old_or0 & BR_BA;
  127. phys1 = old_br1 & old_or1 & BR_BA;
  128. #endif
  129. /* Save the LBC LCS0 and LCS1 addresses for the DIU mux functions */
  130. lbc_lcs0_ba = map_physmem(phys0, 1, 0);
  131. lbc_lcs1_ba = map_physmem(phys1, 1, 0);
  132. pixel_format = cpu_to_le32(AD_BYTE_F | (3 << AD_ALPHA_C_SHIFT) |
  133. (0 << AD_BLUE_C_SHIFT) | (1 << AD_GREEN_C_SHIFT) |
  134. (2 << AD_RED_C_SHIFT) | (8 << AD_COMP_3_SHIFT) |
  135. (8 << AD_COMP_2_SHIFT) | (8 << AD_COMP_1_SHIFT) |
  136. (8 << AD_COMP_0_SHIFT) | (3 << AD_PIXEL_S_SHIFT));
  137. temp = in_8(&pixis->brdcfg1);
  138. if (strncmp(port, "lvds", 4) == 0) {
  139. /* Single link LVDS */
  140. temp &= ~PX_BRDCFG1_DVIEN;
  141. /*
  142. * LVDS also needs backlight enabled, otherwise the display
  143. * will be blank.
  144. */
  145. temp |= (PX_BRDCFG1_DFPEN | PX_BRDCFG1_BACKLIGHT);
  146. name = "Single-Link LVDS";
  147. } else { /* DVI */
  148. /* Enable the DVI port, disable the DFP and the backlight */
  149. temp &= ~(PX_BRDCFG1_DFPEN | PX_BRDCFG1_BACKLIGHT);
  150. temp |= PX_BRDCFG1_DVIEN;
  151. name = "DVI";
  152. }
  153. printf("DIU: Switching to %s monitor @ %ux%u\n", name, xres, yres);
  154. out_8(&pixis->brdcfg1, temp);
  155. /*
  156. * Enable PIXIS indirect access mode. This is a hack that allows us to
  157. * access PIXIS registers even when the LBC pins have been muxed to the
  158. * DIU.
  159. */
  160. setbits_8(&pixis->csr, PX_CTL_ALTACC);
  161. /*
  162. * Route the LAD pins to the DIU. This will disable access to the eLBC,
  163. * which means we won't be able to read/write any NOR flash addresses!
  164. */
  165. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, brdcfg0));
  166. px_brdcfg0 = in_8(lbc_lcs1_ba);
  167. out_8(lbc_lcs1_ba, px_brdcfg0 | PX_BRDCFG0_ELBC_DIU);
  168. in_8(lbc_lcs1_ba);
  169. /* Set PMUXCR to switch the muxed pins from the LBC to the DIU */
  170. clrsetbits_be32(&gur->pmuxcr, PMUXCR_ELBCDIU_MASK, PMUXCR_ELBCDIU_DIU);
  171. pmuxcr = in_be32(&gur->pmuxcr);
  172. return fsl_diu_init(xres, yres, pixel_format, 0);
  173. }
  174. /*
  175. * set_mux_to_lbc - disable the DIU so that we can read/write to elbc
  176. *
  177. * On the Freescale P1022, the DIU video signal and the LBC address/data lines
  178. * share the same pins, which means that when the DIU is active (e.g. the
  179. * console is on the DVI display), NOR flash cannot be accessed. So we use the
  180. * weak accessor feature of the CFI flash code to temporarily switch the pin
  181. * mux from DIU to LBC whenever we want to read or write flash. This has a
  182. * significant performance penalty, but it's the only way to make it work.
  183. *
  184. * There are two muxes: one on the chip, and one on the board. The chip mux
  185. * controls whether the pins are used for the DIU or the LBC, and it is
  186. * set via PMUXCR. The board mux controls whether those signals go to
  187. * the video connector or the NOR flash chips, and it is set via the ngPIXIS.
  188. */
  189. static int set_mux_to_lbc(void)
  190. {
  191. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  192. /* Switch the muxes only if they're currently set to DIU mode */
  193. if ((in_be32(&gur->pmuxcr) & PMUXCR_ELBCDIU_MASK) !=
  194. PMUXCR_ELBCDIU_NOR16) {
  195. /*
  196. * In DIU mode, the PIXIS can only be accessed indirectly
  197. * since we can't read/write the LBC directly.
  198. */
  199. /* Set the board mux to LBC. This will disable the display. */
  200. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, brdcfg0));
  201. out_8(lbc_lcs1_ba, px_brdcfg0);
  202. in_8(lbc_lcs1_ba);
  203. /* Disable indirect PIXIS mode */
  204. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, csr));
  205. clrbits_8(lbc_lcs1_ba, PX_CTL_ALTACC);
  206. /* Set the chip mux to LBC mode, so that writes go to flash. */
  207. out_be32(&gur->pmuxcr, (pmuxcr & ~PMUXCR_ELBCDIU_MASK) |
  208. PMUXCR_ELBCDIU_NOR16);
  209. in_be32(&gur->pmuxcr);
  210. /* Restore the BR0 and BR1 settings */
  211. set_lbc_br(0, old_br0);
  212. set_lbc_or(0, old_or0);
  213. set_lbc_br(1, old_br1);
  214. set_lbc_or(1, old_or1);
  215. return 1;
  216. }
  217. return 0;
  218. }
  219. /*
  220. * set_mux_to_diu - re-enable the DIU muxing
  221. *
  222. * This function restores the chip and board muxing to point to the DIU.
  223. */
  224. static void set_mux_to_diu(void)
  225. {
  226. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  227. /* Set BR0 and BR1 to GPCM mode */
  228. set_lbc_br(0, new_br0);
  229. set_lbc_or(0, new_or0);
  230. set_lbc_br(1, new_br1);
  231. set_lbc_or(1, new_or1);
  232. /* Enable indirect PIXIS mode */
  233. setbits_8(&pixis->csr, PX_CTL_ALTACC);
  234. /* Set the board mux to DIU. This will enable the display. */
  235. out_8(lbc_lcs0_ba, offsetof(ngpixis_t, brdcfg0));
  236. out_8(lbc_lcs1_ba, px_brdcfg0 | PX_BRDCFG0_ELBC_DIU);
  237. in_8(lbc_lcs1_ba);
  238. /* Set the chip mux to DIU mode. */
  239. out_be32(&gur->pmuxcr, pmuxcr);
  240. in_be32(&gur->pmuxcr);
  241. }
  242. /*
  243. * pixis_read - board-specific function to read from the PIXIS
  244. *
  245. * This function overrides the generic pixis_read() function, so that it can
  246. * use PIXIS indirect mode if necessary.
  247. */
  248. u8 pixis_read(unsigned int reg)
  249. {
  250. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  251. /* Use indirect mode if the mux is currently set to DIU mode */
  252. if ((in_be32(&gur->pmuxcr) & PMUXCR_ELBCDIU_MASK) !=
  253. PMUXCR_ELBCDIU_NOR16) {
  254. out_8(lbc_lcs0_ba, reg);
  255. return in_8(lbc_lcs1_ba);
  256. } else {
  257. void *p = (void *)PIXIS_BASE;
  258. return in_8(p + reg);
  259. }
  260. }
  261. /*
  262. * pixis_write - board-specific function to write to the PIXIS
  263. *
  264. * This function overrides the generic pixis_write() function, so that it can
  265. * use PIXIS indirect mode if necessary.
  266. */
  267. void pixis_write(unsigned int reg, u8 value)
  268. {
  269. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
  270. /* Use indirect mode if the mux is currently set to DIU mode */
  271. if ((in_be32(&gur->pmuxcr) & PMUXCR_ELBCDIU_MASK) !=
  272. PMUXCR_ELBCDIU_NOR16) {
  273. out_8(lbc_lcs0_ba, reg);
  274. out_8(lbc_lcs1_ba, value);
  275. /* Do a read-back to ensure the write completed */
  276. in_8(lbc_lcs1_ba);
  277. } else {
  278. void *p = (void *)PIXIS_BASE;
  279. out_8(p + reg, value);
  280. }
  281. }
  282. void pixis_bank_reset(void)
  283. {
  284. /*
  285. * For some reason, a PIXIS bank reset does not work if the PIXIS is
  286. * in indirect mode, so switch to direct mode first.
  287. */
  288. set_mux_to_lbc();
  289. out_8(&pixis->vctl, 0);
  290. out_8(&pixis->vctl, 1);
  291. while (1);
  292. }
  293. #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
  294. void flash_write8(u8 value, void *addr)
  295. {
  296. int sw = set_mux_to_lbc();
  297. __raw_writeb(value, addr);
  298. if (sw) {
  299. /*
  300. * To ensure the post-write is completed to eLBC, software must
  301. * perform a dummy read from one valid address from eLBC space
  302. * before changing the eLBC_DIU from NOR mode to DIU mode.
  303. * set_mux_to_diu() includes a sync that will ensure the
  304. * __raw_readb() completes before it switches the mux.
  305. */
  306. __raw_readb(addr);
  307. set_mux_to_diu();
  308. }
  309. }
  310. void flash_write16(u16 value, void *addr)
  311. {
  312. int sw = set_mux_to_lbc();
  313. __raw_writew(value, addr);
  314. if (sw) {
  315. /*
  316. * To ensure the post-write is completed to eLBC, software must
  317. * perform a dummy read from one valid address from eLBC space
  318. * before changing the eLBC_DIU from NOR mode to DIU mode.
  319. * set_mux_to_diu() includes a sync that will ensure the
  320. * __raw_readb() completes before it switches the mux.
  321. */
  322. __raw_readb(addr);
  323. set_mux_to_diu();
  324. }
  325. }
  326. void flash_write32(u32 value, void *addr)
  327. {
  328. int sw = set_mux_to_lbc();
  329. __raw_writel(value, addr);
  330. if (sw) {
  331. /*
  332. * To ensure the post-write is completed to eLBC, software must
  333. * perform a dummy read from one valid address from eLBC space
  334. * before changing the eLBC_DIU from NOR mode to DIU mode.
  335. * set_mux_to_diu() includes a sync that will ensure the
  336. * __raw_readb() completes before it switches the mux.
  337. */
  338. __raw_readb(addr);
  339. set_mux_to_diu();
  340. }
  341. }
  342. void flash_write64(u64 value, void *addr)
  343. {
  344. int sw = set_mux_to_lbc();
  345. uint32_t *p = addr;
  346. /*
  347. * There is no __raw_writeq(), so do the write manually. We don't trust
  348. * the compiler, so we use inline assembly.
  349. */
  350. __asm__ __volatile__(
  351. "stw%U0%X0 %2,%0;\n"
  352. "stw%U1%X1 %3,%1;\n"
  353. : "=m" (*p), "=m" (*(p + 1))
  354. : "r" ((uint32_t) (value >> 32)), "r" ((uint32_t) (value)));
  355. if (sw) {
  356. /*
  357. * To ensure the post-write is completed to eLBC, software must
  358. * perform a dummy read from one valid address from eLBC space
  359. * before changing the eLBC_DIU from NOR mode to DIU mode. We
  360. * read addr+4 because we just wrote to addr+4, so that's how we
  361. * maintain execution order. set_mux_to_diu() includes a sync
  362. * that will ensure the __raw_readb() completes before it
  363. * switches the mux.
  364. */
  365. __raw_readb(addr + 4);
  366. set_mux_to_diu();
  367. }
  368. }
  369. u8 flash_read8(void *addr)
  370. {
  371. u8 ret;
  372. int sw = set_mux_to_lbc();
  373. ret = __raw_readb(addr);
  374. if (sw)
  375. set_mux_to_diu();
  376. return ret;
  377. }
  378. u16 flash_read16(void *addr)
  379. {
  380. u16 ret;
  381. int sw = set_mux_to_lbc();
  382. ret = __raw_readw(addr);
  383. if (sw)
  384. set_mux_to_diu();
  385. return ret;
  386. }
  387. u32 flash_read32(void *addr)
  388. {
  389. u32 ret;
  390. int sw = set_mux_to_lbc();
  391. ret = __raw_readl(addr);
  392. if (sw)
  393. set_mux_to_diu();
  394. return ret;
  395. }
  396. u64 flash_read64(void *addr)
  397. {
  398. u64 ret;
  399. int sw = set_mux_to_lbc();
  400. /* There is no __raw_readq(), so do the read manually */
  401. ret = *(volatile u64 *)addr;
  402. if (sw)
  403. set_mux_to_diu();
  404. return ret;
  405. }
  406. #endif