sunxi_display.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. /*
  2. * Display driver for Allwinner SoCs.
  3. *
  4. * (C) Copyright 2013-2014 Luc Verhaegen <libv@skynet.be>
  5. * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <asm/arch/clock.h>
  11. #include <asm/arch/display.h>
  12. #include <asm/arch/gpio.h>
  13. #include <asm/global_data.h>
  14. #include <asm/gpio.h>
  15. #include <asm/io.h>
  16. #include <errno.h>
  17. #include <fdtdec.h>
  18. #include <fdt_support.h>
  19. #include <i2c.h>
  20. #include <video_fb.h>
  21. #include "videomodes.h"
  22. #include "hitachi_tx18d42vm_lcd.h"
  23. #include "ssd2828.h"
  24. #ifdef CONFIG_VIDEO_LCD_BL_PWM_ACTIVE_LOW
  25. #define PWM_ON 0
  26. #define PWM_OFF 1
  27. #else
  28. #define PWM_ON 1
  29. #define PWM_OFF 0
  30. #endif
  31. DECLARE_GLOBAL_DATA_PTR;
  32. enum sunxi_monitor {
  33. sunxi_monitor_none,
  34. sunxi_monitor_dvi,
  35. sunxi_monitor_hdmi,
  36. sunxi_monitor_lcd,
  37. sunxi_monitor_vga,
  38. };
  39. #define SUNXI_MONITOR_LAST sunxi_monitor_vga
  40. struct sunxi_display {
  41. GraphicDevice graphic_device;
  42. enum sunxi_monitor monitor;
  43. unsigned int depth;
  44. unsigned int fb_size;
  45. } sunxi_display;
  46. #ifdef CONFIG_VIDEO_HDMI
  47. /*
  48. * Wait up to 200ms for value to be set in given part of reg.
  49. */
  50. static int await_completion(u32 *reg, u32 mask, u32 val)
  51. {
  52. unsigned long tmo = timer_get_us() + 200000;
  53. while ((readl(reg) & mask) != val) {
  54. if (timer_get_us() > tmo) {
  55. printf("DDC: timeout reading EDID\n");
  56. return -ETIME;
  57. }
  58. }
  59. return 0;
  60. }
  61. static int sunxi_hdmi_hpd_detect(int hpd_delay)
  62. {
  63. struct sunxi_ccm_reg * const ccm =
  64. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  65. struct sunxi_hdmi_reg * const hdmi =
  66. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  67. unsigned long tmo = timer_get_us() + hpd_delay * 1000;
  68. /* Set pll3 to 300MHz */
  69. clock_set_pll3(300000000);
  70. /* Set hdmi parent to pll3 */
  71. clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
  72. CCM_HDMI_CTRL_PLL3);
  73. /* Set ahb gating to pass */
  74. #ifdef CONFIG_SUNXI_GEN_SUN6I
  75. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
  76. #endif
  77. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
  78. /* Clock on */
  79. setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
  80. writel(SUNXI_HDMI_CTRL_ENABLE, &hdmi->ctrl);
  81. writel(SUNXI_HDMI_PAD_CTRL0_HDP, &hdmi->pad_ctrl0);
  82. while (timer_get_us() < tmo) {
  83. if (readl(&hdmi->hpd) & SUNXI_HDMI_HPD_DETECT)
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. static void sunxi_hdmi_shutdown(void)
  89. {
  90. struct sunxi_ccm_reg * const ccm =
  91. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  92. struct sunxi_hdmi_reg * const hdmi =
  93. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  94. clrbits_le32(&hdmi->ctrl, SUNXI_HDMI_CTRL_ENABLE);
  95. clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
  96. clrbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
  97. #ifdef CONFIG_SUNXI_GEN_SUN6I
  98. clrbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
  99. #endif
  100. clock_set_pll3(0);
  101. }
  102. static int sunxi_hdmi_ddc_do_command(u32 cmnd, int offset, int n)
  103. {
  104. struct sunxi_hdmi_reg * const hdmi =
  105. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  106. setbits_le32(&hdmi->ddc_fifo_ctrl, SUNXI_HDMI_DDC_FIFO_CTRL_CLEAR);
  107. writel(SUNXI_HMDI_DDC_ADDR_EDDC_SEGMENT(offset >> 8) |
  108. SUNXI_HMDI_DDC_ADDR_EDDC_ADDR |
  109. SUNXI_HMDI_DDC_ADDR_OFFSET(offset) |
  110. SUNXI_HMDI_DDC_ADDR_SLAVE_ADDR, &hdmi->ddc_addr);
  111. #ifndef CONFIG_MACH_SUN6I
  112. writel(n, &hdmi->ddc_byte_count);
  113. writel(cmnd, &hdmi->ddc_cmnd);
  114. #else
  115. writel(n << 16 | cmnd, &hdmi->ddc_cmnd);
  116. #endif
  117. setbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START);
  118. return await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START, 0);
  119. }
  120. static int sunxi_hdmi_ddc_read(int offset, u8 *buf, int count)
  121. {
  122. struct sunxi_hdmi_reg * const hdmi =
  123. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  124. int i, n;
  125. while (count > 0) {
  126. if (count > 16)
  127. n = 16;
  128. else
  129. n = count;
  130. if (sunxi_hdmi_ddc_do_command(
  131. SUNXI_HDMI_DDC_CMND_EXPLICIT_EDDC_READ,
  132. offset, n))
  133. return -ETIME;
  134. for (i = 0; i < n; i++)
  135. *buf++ = readb(&hdmi->ddc_fifo_data);
  136. offset += n;
  137. count -= n;
  138. }
  139. return 0;
  140. }
  141. static int sunxi_hdmi_edid_get_block(int block, u8 *buf)
  142. {
  143. int r, retries = 2;
  144. do {
  145. r = sunxi_hdmi_ddc_read(block * 128, buf, 128);
  146. if (r)
  147. continue;
  148. r = edid_check_checksum(buf);
  149. if (r) {
  150. printf("EDID block %d: checksum error%s\n",
  151. block, retries ? ", retrying" : "");
  152. }
  153. } while (r && retries--);
  154. return r;
  155. }
  156. static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode)
  157. {
  158. struct edid1_info edid1;
  159. struct edid_cea861_info cea681[4];
  160. struct edid_detailed_timing *t =
  161. (struct edid_detailed_timing *)edid1.monitor_details.timing;
  162. struct sunxi_hdmi_reg * const hdmi =
  163. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  164. struct sunxi_ccm_reg * const ccm =
  165. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  166. int i, r, ext_blocks = 0;
  167. /* SUNXI_HDMI_CTRL_ENABLE & PAD_CTRL0 are already set by hpd_detect */
  168. writel(SUNXI_HDMI_PAD_CTRL1 | SUNXI_HDMI_PAD_CTRL1_HALVE,
  169. &hdmi->pad_ctrl1);
  170. writel(SUNXI_HDMI_PLL_CTRL | SUNXI_HDMI_PLL_CTRL_DIV(15),
  171. &hdmi->pll_ctrl);
  172. writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
  173. /* Reset i2c controller */
  174. setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
  175. writel(SUNXI_HMDI_DDC_CTRL_ENABLE |
  176. SUNXI_HMDI_DDC_CTRL_SDA_ENABLE |
  177. SUNXI_HMDI_DDC_CTRL_SCL_ENABLE |
  178. SUNXI_HMDI_DDC_CTRL_RESET, &hdmi->ddc_ctrl);
  179. if (await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_RESET, 0))
  180. return -EIO;
  181. writel(SUNXI_HDMI_DDC_CLOCK, &hdmi->ddc_clock);
  182. #ifndef CONFIG_MACH_SUN6I
  183. writel(SUNXI_HMDI_DDC_LINE_CTRL_SDA_ENABLE |
  184. SUNXI_HMDI_DDC_LINE_CTRL_SCL_ENABLE, &hdmi->ddc_line_ctrl);
  185. #endif
  186. r = sunxi_hdmi_edid_get_block(0, (u8 *)&edid1);
  187. if (r == 0) {
  188. r = edid_check_info(&edid1);
  189. if (r) {
  190. printf("EDID: invalid EDID data\n");
  191. r = -EINVAL;
  192. }
  193. }
  194. if (r == 0) {
  195. ext_blocks = edid1.extension_flag;
  196. if (ext_blocks > 4)
  197. ext_blocks = 4;
  198. for (i = 0; i < ext_blocks; i++) {
  199. if (sunxi_hdmi_edid_get_block(1 + i,
  200. (u8 *)&cea681[i]) != 0) {
  201. ext_blocks = i;
  202. break;
  203. }
  204. }
  205. }
  206. /* Disable DDC engine, no longer needed */
  207. clrbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_ENABLE);
  208. clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
  209. if (r)
  210. return r;
  211. /* We want version 1.3 or 1.2 with detailed timing info */
  212. if (edid1.version != 1 || (edid1.revision < 3 &&
  213. !EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(edid1))) {
  214. printf("EDID: unsupported version %d.%d\n",
  215. edid1.version, edid1.revision);
  216. return -EINVAL;
  217. }
  218. /* Take the first usable detailed timing */
  219. for (i = 0; i < 4; i++, t++) {
  220. r = video_edid_dtd_to_ctfb_res_modes(t, mode);
  221. if (r == 0)
  222. break;
  223. }
  224. if (i == 4) {
  225. printf("EDID: no usable detailed timing found\n");
  226. return -ENOENT;
  227. }
  228. /* Check for basic audio support, if found enable hdmi output */
  229. sunxi_display.monitor = sunxi_monitor_dvi;
  230. for (i = 0; i < ext_blocks; i++) {
  231. if (cea681[i].extension_tag != EDID_CEA861_EXTENSION_TAG ||
  232. cea681[i].revision < 2)
  233. continue;
  234. if (EDID_CEA861_SUPPORTS_BASIC_AUDIO(cea681[i]))
  235. sunxi_display.monitor = sunxi_monitor_hdmi;
  236. }
  237. return 0;
  238. }
  239. #endif /* CONFIG_VIDEO_HDMI */
  240. #ifdef CONFIG_MACH_SUN4I
  241. /*
  242. * Testing has shown that on sun4i the display backend engine does not have
  243. * deep enough fifo-s causing flickering / tearing in full-hd mode due to
  244. * fifo underruns. So on sun4i we use the display frontend engine to do the
  245. * dma from memory, as the frontend does have deep enough fifo-s.
  246. */
  247. static const u32 sun4i_vert_coef[32] = {
  248. 0x00004000, 0x000140ff, 0x00033ffe, 0x00043ffd,
  249. 0x00063efc, 0xff083dfc, 0x000a3bfb, 0xff0d39fb,
  250. 0xff0f37fb, 0xff1136fa, 0xfe1433fb, 0xfe1631fb,
  251. 0xfd192ffb, 0xfd1c2cfb, 0xfd1f29fb, 0xfc2127fc,
  252. 0xfc2424fc, 0xfc2721fc, 0xfb291ffd, 0xfb2c1cfd,
  253. 0xfb2f19fd, 0xfb3116fe, 0xfb3314fe, 0xfa3611ff,
  254. 0xfb370fff, 0xfb390dff, 0xfb3b0a00, 0xfc3d08ff,
  255. 0xfc3e0600, 0xfd3f0400, 0xfe3f0300, 0xff400100,
  256. };
  257. static const u32 sun4i_horz_coef[64] = {
  258. 0x40000000, 0x00000000, 0x40fe0000, 0x0000ff03,
  259. 0x3ffd0000, 0x0000ff05, 0x3ffc0000, 0x0000ff06,
  260. 0x3efb0000, 0x0000ff08, 0x3dfb0000, 0x0000ff09,
  261. 0x3bfa0000, 0x0000fe0d, 0x39fa0000, 0x0000fe0f,
  262. 0x38fa0000, 0x0000fe10, 0x36fa0000, 0x0000fe12,
  263. 0x33fa0000, 0x0000fd16, 0x31fa0000, 0x0000fd18,
  264. 0x2ffa0000, 0x0000fd1a, 0x2cfa0000, 0x0000fc1e,
  265. 0x29fa0000, 0x0000fc21, 0x27fb0000, 0x0000fb23,
  266. 0x24fb0000, 0x0000fb26, 0x21fb0000, 0x0000fb29,
  267. 0x1ffc0000, 0x0000fa2b, 0x1cfc0000, 0x0000fa2e,
  268. 0x19fd0000, 0x0000fa30, 0x16fd0000, 0x0000fa33,
  269. 0x14fd0000, 0x0000fa35, 0x11fe0000, 0x0000fa37,
  270. 0x0ffe0000, 0x0000fa39, 0x0dfe0000, 0x0000fa3b,
  271. 0x0afe0000, 0x0000fa3e, 0x08ff0000, 0x0000fb3e,
  272. 0x06ff0000, 0x0000fb40, 0x05ff0000, 0x0000fc40,
  273. 0x03ff0000, 0x0000fd41, 0x01ff0000, 0x0000fe42,
  274. };
  275. static void sunxi_frontend_init(void)
  276. {
  277. struct sunxi_ccm_reg * const ccm =
  278. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  279. struct sunxi_de_fe_reg * const de_fe =
  280. (struct sunxi_de_fe_reg *)SUNXI_DE_FE0_BASE;
  281. int i;
  282. /* Clocks on */
  283. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_FE0);
  284. setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_FE0);
  285. clock_set_de_mod_clock(&ccm->fe0_clk_cfg, 300000000);
  286. setbits_le32(&de_fe->enable, SUNXI_DE_FE_ENABLE_EN);
  287. for (i = 0; i < 32; i++) {
  288. writel(sun4i_horz_coef[2 * i], &de_fe->ch0_horzcoef0[i]);
  289. writel(sun4i_horz_coef[2 * i + 1], &de_fe->ch0_horzcoef1[i]);
  290. writel(sun4i_vert_coef[i], &de_fe->ch0_vertcoef[i]);
  291. writel(sun4i_horz_coef[2 * i], &de_fe->ch1_horzcoef0[i]);
  292. writel(sun4i_horz_coef[2 * i + 1], &de_fe->ch1_horzcoef1[i]);
  293. writel(sun4i_vert_coef[i], &de_fe->ch1_vertcoef[i]);
  294. }
  295. setbits_le32(&de_fe->frame_ctrl, SUNXI_DE_FE_FRAME_CTRL_COEF_RDY);
  296. }
  297. static void sunxi_frontend_mode_set(const struct ctfb_res_modes *mode,
  298. unsigned int address)
  299. {
  300. struct sunxi_de_fe_reg * const de_fe =
  301. (struct sunxi_de_fe_reg *)SUNXI_DE_FE0_BASE;
  302. setbits_le32(&de_fe->bypass, SUNXI_DE_FE_BYPASS_CSC_BYPASS);
  303. writel(CONFIG_SYS_SDRAM_BASE + address, &de_fe->ch0_addr);
  304. writel(mode->xres * 4, &de_fe->ch0_stride);
  305. writel(SUNXI_DE_FE_INPUT_FMT_ARGB8888, &de_fe->input_fmt);
  306. writel(SUNXI_DE_FE_OUTPUT_FMT_ARGB8888, &de_fe->output_fmt);
  307. writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
  308. &de_fe->ch0_insize);
  309. writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
  310. &de_fe->ch0_outsize);
  311. writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch0_horzfact);
  312. writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch0_vertfact);
  313. writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
  314. &de_fe->ch1_insize);
  315. writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
  316. &de_fe->ch1_outsize);
  317. writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch1_horzfact);
  318. writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch1_vertfact);
  319. setbits_le32(&de_fe->frame_ctrl, SUNXI_DE_FE_FRAME_CTRL_REG_RDY);
  320. }
  321. static void sunxi_frontend_enable(void)
  322. {
  323. struct sunxi_de_fe_reg * const de_fe =
  324. (struct sunxi_de_fe_reg *)SUNXI_DE_FE0_BASE;
  325. setbits_le32(&de_fe->frame_ctrl, SUNXI_DE_FE_FRAME_CTRL_FRM_START);
  326. }
  327. #else
  328. static void sunxi_frontend_init(void) {}
  329. static void sunxi_frontend_mode_set(const struct ctfb_res_modes *mode,
  330. unsigned int address) {}
  331. static void sunxi_frontend_enable(void) {}
  332. #endif
  333. /*
  334. * This is the entity that mixes and matches the different layers and inputs.
  335. * Allwinner calls it the back-end, but i like composer better.
  336. */
  337. static void sunxi_composer_init(void)
  338. {
  339. struct sunxi_ccm_reg * const ccm =
  340. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  341. struct sunxi_de_be_reg * const de_be =
  342. (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
  343. int i;
  344. sunxi_frontend_init();
  345. #ifdef CONFIG_SUNXI_GEN_SUN6I
  346. /* Reset off */
  347. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE_BE0);
  348. #endif
  349. /* Clocks on */
  350. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_BE0);
  351. #ifndef CONFIG_MACH_SUN4I /* On sun4i the frontend does the dma */
  352. setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_BE0);
  353. #endif
  354. clock_set_de_mod_clock(&ccm->be0_clk_cfg, 300000000);
  355. /* Engine bug, clear registers after reset */
  356. for (i = 0x0800; i < 0x1000; i += 4)
  357. writel(0, SUNXI_DE_BE0_BASE + i);
  358. setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_ENABLE);
  359. }
  360. static void sunxi_composer_mode_set(const struct ctfb_res_modes *mode,
  361. unsigned int address)
  362. {
  363. struct sunxi_de_be_reg * const de_be =
  364. (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
  365. sunxi_frontend_mode_set(mode, address);
  366. writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
  367. &de_be->disp_size);
  368. writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
  369. &de_be->layer0_size);
  370. #ifndef CONFIG_MACH_SUN4I /* On sun4i the frontend does the dma */
  371. writel(SUNXI_DE_BE_LAYER_STRIDE(mode->xres), &de_be->layer0_stride);
  372. writel(address << 3, &de_be->layer0_addr_low32b);
  373. writel(address >> 29, &de_be->layer0_addr_high4b);
  374. #else
  375. writel(SUNXI_DE_BE_LAYER_ATTR0_SRC_FE0, &de_be->layer0_attr0_ctrl);
  376. #endif
  377. writel(SUNXI_DE_BE_LAYER_ATTR1_FMT_XRGB8888, &de_be->layer0_attr1_ctrl);
  378. setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_LAYER0_ENABLE);
  379. }
  380. static void sunxi_composer_enable(void)
  381. {
  382. struct sunxi_de_be_reg * const de_be =
  383. (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
  384. sunxi_frontend_enable();
  385. setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
  386. setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
  387. }
  388. /*
  389. * LCDC, what allwinner calls a CRTC, so timing controller and serializer.
  390. */
  391. static void sunxi_lcdc_pll_set(int tcon, int dotclock,
  392. int *clk_div, int *clk_double)
  393. {
  394. struct sunxi_ccm_reg * const ccm =
  395. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  396. int value, n, m, min_m, max_m, diff;
  397. int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
  398. int best_double = 0;
  399. if (tcon == 0) {
  400. #ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
  401. min_m = 6;
  402. max_m = 127;
  403. #endif
  404. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  405. min_m = max_m = 7;
  406. #endif
  407. } else {
  408. min_m = 1;
  409. max_m = 15;
  410. }
  411. /*
  412. * Find the lowest divider resulting in a matching clock, if there
  413. * is no match, pick the closest lower clock, as monitors tend to
  414. * not sync to higher frequencies.
  415. */
  416. for (m = min_m; m <= max_m; m++) {
  417. n = (m * dotclock) / 3000;
  418. if ((n >= 9) && (n <= 127)) {
  419. value = (3000 * n) / m;
  420. diff = dotclock - value;
  421. if (diff < best_diff) {
  422. best_diff = diff;
  423. best_m = m;
  424. best_n = n;
  425. best_double = 0;
  426. }
  427. }
  428. /* These are just duplicates */
  429. if (!(m & 1))
  430. continue;
  431. n = (m * dotclock) / 6000;
  432. if ((n >= 9) && (n <= 127)) {
  433. value = (6000 * n) / m;
  434. diff = dotclock - value;
  435. if (diff < best_diff) {
  436. best_diff = diff;
  437. best_m = m;
  438. best_n = n;
  439. best_double = 1;
  440. }
  441. }
  442. }
  443. debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
  444. dotclock, (best_double + 1) * 3000 * best_n / best_m,
  445. best_double + 1, best_n, best_m);
  446. clock_set_pll3(best_n * 3000000);
  447. if (tcon == 0) {
  448. writel(CCM_LCD_CH0_CTRL_GATE | CCM_LCD_CH0_CTRL_RST |
  449. (best_double ? CCM_LCD_CH0_CTRL_PLL3_2X :
  450. CCM_LCD_CH0_CTRL_PLL3),
  451. &ccm->lcd0_ch0_clk_cfg);
  452. } else {
  453. writel(CCM_LCD_CH1_CTRL_GATE |
  454. (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X :
  455. CCM_LCD_CH1_CTRL_PLL3) |
  456. CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
  457. }
  458. *clk_div = best_m;
  459. *clk_double = best_double;
  460. }
  461. static void sunxi_lcdc_init(void)
  462. {
  463. struct sunxi_ccm_reg * const ccm =
  464. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  465. struct sunxi_lcdc_reg * const lcdc =
  466. (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
  467. /* Reset off */
  468. #ifdef CONFIG_SUNXI_GEN_SUN6I
  469. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
  470. #else
  471. setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_RST);
  472. #endif
  473. /* Clock on */
  474. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
  475. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  476. setbits_le32(&ccm->lvds_clk_cfg, CCM_LVDS_CTRL_RST);
  477. #endif
  478. /* Init lcdc */
  479. writel(0, &lcdc->ctrl); /* Disable tcon */
  480. writel(0, &lcdc->int0); /* Disable all interrupts */
  481. /* Disable tcon0 dot clock */
  482. clrbits_le32(&lcdc->tcon0_dclk, SUNXI_LCDC_TCON0_DCLK_ENABLE);
  483. /* Set all io lines to tristate */
  484. writel(0xffffffff, &lcdc->tcon0_io_tristate);
  485. writel(0xffffffff, &lcdc->tcon1_io_tristate);
  486. }
  487. static void sunxi_lcdc_enable(void)
  488. {
  489. struct sunxi_lcdc_reg * const lcdc =
  490. (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
  491. setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
  492. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  493. setbits_le32(&lcdc->tcon0_lvds_intf, SUNXI_LCDC_TCON0_LVDS_INTF_ENABLE);
  494. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0);
  495. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_UPDATE);
  496. udelay(2); /* delay at least 1200 ns */
  497. setbits_le32(&lcdc->lvds_ana1, SUNXI_LCDC_LVDS_ANA1_INIT1);
  498. udelay(1); /* delay at least 120 ns */
  499. setbits_le32(&lcdc->lvds_ana1, SUNXI_LCDC_LVDS_ANA1_INIT2);
  500. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_UPDATE);
  501. #endif
  502. }
  503. static void sunxi_lcdc_panel_enable(void)
  504. {
  505. int pin, reset_pin;
  506. /*
  507. * Start with backlight disabled to avoid the screen flashing to
  508. * white while the lcd inits.
  509. */
  510. pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_EN);
  511. if (pin >= 0) {
  512. gpio_request(pin, "lcd_backlight_enable");
  513. gpio_direction_output(pin, 0);
  514. }
  515. pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_PWM);
  516. if (pin >= 0) {
  517. gpio_request(pin, "lcd_backlight_pwm");
  518. gpio_direction_output(pin, PWM_OFF);
  519. }
  520. reset_pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_RESET);
  521. if (reset_pin >= 0) {
  522. gpio_request(reset_pin, "lcd_reset");
  523. gpio_direction_output(reset_pin, 0); /* Assert reset */
  524. }
  525. /* Give the backlight some time to turn off and power up the panel. */
  526. mdelay(40);
  527. pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_POWER);
  528. if (pin >= 0) {
  529. gpio_request(pin, "lcd_power");
  530. gpio_direction_output(pin, 1);
  531. }
  532. if (reset_pin >= 0)
  533. gpio_direction_output(reset_pin, 1); /* De-assert reset */
  534. }
  535. static void sunxi_lcdc_backlight_enable(void)
  536. {
  537. int pin;
  538. /*
  539. * We want to have scanned out at least one frame before enabling the
  540. * backlight to avoid the screen flashing to white when we enable it.
  541. */
  542. mdelay(40);
  543. pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_EN);
  544. if (pin >= 0)
  545. gpio_direction_output(pin, 1);
  546. pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_PWM);
  547. if (pin >= 0)
  548. gpio_direction_output(pin, PWM_ON);
  549. }
  550. static int sunxi_lcdc_get_clk_delay(const struct ctfb_res_modes *mode)
  551. {
  552. int delay;
  553. delay = mode->lower_margin + mode->vsync_len + mode->upper_margin - 2;
  554. return (delay > 30) ? 30 : delay;
  555. }
  556. static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode,
  557. bool for_ext_vga_dac)
  558. {
  559. struct sunxi_lcdc_reg * const lcdc =
  560. (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
  561. int bp, clk_delay, clk_div, clk_double, pin, total, val;
  562. for (pin = SUNXI_GPD(0); pin <= SUNXI_GPD(27); pin++)
  563. #ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
  564. sunxi_gpio_set_cfgpin(pin, SUNXI_GPD_LCD0);
  565. #endif
  566. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  567. sunxi_gpio_set_cfgpin(pin, SUNXI_GPD_LVDS0);
  568. #endif
  569. sunxi_lcdc_pll_set(0, mode->pixclock_khz, &clk_div, &clk_double);
  570. /* Use tcon0 */
  571. clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
  572. SUNXI_LCDC_CTRL_IO_MAP_TCON0);
  573. clk_delay = sunxi_lcdc_get_clk_delay(mode);
  574. writel(SUNXI_LCDC_TCON0_CTRL_ENABLE |
  575. SUNXI_LCDC_TCON0_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon0_ctrl);
  576. writel(SUNXI_LCDC_TCON0_DCLK_ENABLE |
  577. SUNXI_LCDC_TCON0_DCLK_DIV(clk_div), &lcdc->tcon0_dclk);
  578. writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
  579. &lcdc->tcon0_timing_active);
  580. bp = mode->hsync_len + mode->left_margin;
  581. total = mode->xres + mode->right_margin + bp;
  582. writel(SUNXI_LCDC_TCON0_TIMING_H_TOTAL(total) |
  583. SUNXI_LCDC_TCON0_TIMING_H_BP(bp), &lcdc->tcon0_timing_h);
  584. bp = mode->vsync_len + mode->upper_margin;
  585. total = mode->yres + mode->lower_margin + bp;
  586. writel(SUNXI_LCDC_TCON0_TIMING_V_TOTAL(total) |
  587. SUNXI_LCDC_TCON0_TIMING_V_BP(bp), &lcdc->tcon0_timing_v);
  588. #ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
  589. writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
  590. &lcdc->tcon0_timing_sync);
  591. writel(0, &lcdc->tcon0_hv_intf);
  592. writel(0, &lcdc->tcon0_cpu_intf);
  593. #endif
  594. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  595. val = (sunxi_display.depth == 18) ? 1 : 0;
  596. writel(SUNXI_LCDC_TCON0_LVDS_INTF_BITWIDTH(val), &lcdc->tcon0_lvds_intf);
  597. #endif
  598. if (sunxi_display.depth == 18 || sunxi_display.depth == 16) {
  599. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[0]);
  600. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[1]);
  601. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[2]);
  602. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[3]);
  603. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[4]);
  604. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[5]);
  605. writel(SUNXI_LCDC_TCON0_FRM_TAB0, &lcdc->tcon0_frm_table[0]);
  606. writel(SUNXI_LCDC_TCON0_FRM_TAB1, &lcdc->tcon0_frm_table[1]);
  607. writel(SUNXI_LCDC_TCON0_FRM_TAB2, &lcdc->tcon0_frm_table[2]);
  608. writel(SUNXI_LCDC_TCON0_FRM_TAB3, &lcdc->tcon0_frm_table[3]);
  609. writel(((sunxi_display.depth == 18) ?
  610. SUNXI_LCDC_TCON0_FRM_CTRL_RGB666 :
  611. SUNXI_LCDC_TCON0_FRM_CTRL_RGB565),
  612. &lcdc->tcon0_frm_ctrl);
  613. }
  614. val = SUNXI_LCDC_TCON0_IO_POL_DCLK_PHASE(CONFIG_VIDEO_LCD_DCLK_PHASE);
  615. if (!(mode->sync & FB_SYNC_HOR_HIGH_ACT))
  616. val |= SUNXI_LCDC_TCON_HSYNC_MASK;
  617. if (!(mode->sync & FB_SYNC_VERT_HIGH_ACT))
  618. val |= SUNXI_LCDC_TCON_VSYNC_MASK;
  619. #ifdef CONFIG_VIDEO_VGA_VIA_LCD_FORCE_SYNC_ACTIVE_HIGH
  620. if (for_ext_vga_dac)
  621. val = 0;
  622. #endif
  623. writel(val, &lcdc->tcon0_io_polarity);
  624. writel(0, &lcdc->tcon0_io_tristate);
  625. }
  626. #if defined CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA
  627. static void sunxi_lcdc_tcon1_mode_set(const struct ctfb_res_modes *mode,
  628. int *clk_div, int *clk_double,
  629. bool use_portd_hvsync)
  630. {
  631. struct sunxi_lcdc_reg * const lcdc =
  632. (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
  633. int bp, clk_delay, total, val;
  634. /* Use tcon1 */
  635. clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
  636. SUNXI_LCDC_CTRL_IO_MAP_TCON1);
  637. clk_delay = sunxi_lcdc_get_clk_delay(mode);
  638. writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
  639. SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon1_ctrl);
  640. writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
  641. &lcdc->tcon1_timing_source);
  642. writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
  643. &lcdc->tcon1_timing_scale);
  644. writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
  645. &lcdc->tcon1_timing_out);
  646. bp = mode->hsync_len + mode->left_margin;
  647. total = mode->xres + mode->right_margin + bp;
  648. writel(SUNXI_LCDC_TCON1_TIMING_H_TOTAL(total) |
  649. SUNXI_LCDC_TCON1_TIMING_H_BP(bp), &lcdc->tcon1_timing_h);
  650. bp = mode->vsync_len + mode->upper_margin;
  651. total = mode->yres + mode->lower_margin + bp;
  652. writel(SUNXI_LCDC_TCON1_TIMING_V_TOTAL(total) |
  653. SUNXI_LCDC_TCON1_TIMING_V_BP(bp), &lcdc->tcon1_timing_v);
  654. writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
  655. &lcdc->tcon1_timing_sync);
  656. if (use_portd_hvsync) {
  657. sunxi_gpio_set_cfgpin(SUNXI_GPD(26), SUNXI_GPD_LCD0);
  658. sunxi_gpio_set_cfgpin(SUNXI_GPD(27), SUNXI_GPD_LCD0);
  659. val = 0;
  660. if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
  661. val |= SUNXI_LCDC_TCON_HSYNC_MASK;
  662. if (mode->sync & FB_SYNC_VERT_HIGH_ACT)
  663. val |= SUNXI_LCDC_TCON_VSYNC_MASK;
  664. writel(val, &lcdc->tcon1_io_polarity);
  665. clrbits_le32(&lcdc->tcon1_io_tristate,
  666. SUNXI_LCDC_TCON_VSYNC_MASK |
  667. SUNXI_LCDC_TCON_HSYNC_MASK);
  668. }
  669. sunxi_lcdc_pll_set(1, mode->pixclock_khz, clk_div, clk_double);
  670. }
  671. #endif /* CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA */
  672. #ifdef CONFIG_VIDEO_HDMI
  673. static void sunxi_hdmi_setup_info_frames(const struct ctfb_res_modes *mode)
  674. {
  675. struct sunxi_hdmi_reg * const hdmi =
  676. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  677. u8 checksum = 0;
  678. u8 avi_info_frame[17] = {
  679. 0x82, 0x02, 0x0d, 0x00, 0x12, 0x00, 0x88, 0x00,
  680. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  681. 0x00
  682. };
  683. u8 vendor_info_frame[19] = {
  684. 0x81, 0x01, 0x06, 0x29, 0x03, 0x0c, 0x00, 0x40,
  685. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  686. 0x00, 0x00, 0x00
  687. };
  688. int i;
  689. if (mode->pixclock_khz <= 27000)
  690. avi_info_frame[5] = 0x40; /* SD-modes, ITU601 colorspace */
  691. else
  692. avi_info_frame[5] = 0x80; /* HD-modes, ITU709 colorspace */
  693. if (mode->xres * 100 / mode->yres < 156)
  694. avi_info_frame[5] |= 0x18; /* 4 : 3 */
  695. else
  696. avi_info_frame[5] |= 0x28; /* 16 : 9 */
  697. for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
  698. checksum += avi_info_frame[i];
  699. avi_info_frame[3] = 0x100 - checksum;
  700. for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
  701. writeb(avi_info_frame[i], &hdmi->avi_info_frame[i]);
  702. writel(SUNXI_HDMI_QCP_PACKET0, &hdmi->qcp_packet0);
  703. writel(SUNXI_HDMI_QCP_PACKET1, &hdmi->qcp_packet1);
  704. for (i = 0; i < ARRAY_SIZE(vendor_info_frame); i++)
  705. writeb(vendor_info_frame[i], &hdmi->vendor_info_frame[i]);
  706. writel(SUNXI_HDMI_PKT_CTRL0, &hdmi->pkt_ctrl0);
  707. writel(SUNXI_HDMI_PKT_CTRL1, &hdmi->pkt_ctrl1);
  708. setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_HDMI);
  709. }
  710. static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
  711. int clk_div, int clk_double)
  712. {
  713. struct sunxi_hdmi_reg * const hdmi =
  714. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  715. int x, y;
  716. /* Write clear interrupt status bits */
  717. writel(SUNXI_HDMI_IRQ_STATUS_BITS, &hdmi->irq);
  718. if (sunxi_display.monitor == sunxi_monitor_hdmi)
  719. sunxi_hdmi_setup_info_frames(mode);
  720. /* Set input sync enable */
  721. writel(SUNXI_HDMI_UNKNOWN_INPUT_SYNC, &hdmi->unknown);
  722. /* Init various registers, select pll3 as clock source */
  723. writel(SUNXI_HDMI_VIDEO_POL_TX_CLK, &hdmi->video_polarity);
  724. writel(SUNXI_HDMI_PAD_CTRL0_RUN, &hdmi->pad_ctrl0);
  725. writel(SUNXI_HDMI_PAD_CTRL1, &hdmi->pad_ctrl1);
  726. writel(SUNXI_HDMI_PLL_CTRL, &hdmi->pll_ctrl);
  727. writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
  728. /* Setup clk div and doubler */
  729. clrsetbits_le32(&hdmi->pll_ctrl, SUNXI_HDMI_PLL_CTRL_DIV_MASK,
  730. SUNXI_HDMI_PLL_CTRL_DIV(clk_div));
  731. if (!clk_double)
  732. setbits_le32(&hdmi->pad_ctrl1, SUNXI_HDMI_PAD_CTRL1_HALVE);
  733. /* Setup timing registers */
  734. writel(SUNXI_HDMI_Y(mode->yres) | SUNXI_HDMI_X(mode->xres),
  735. &hdmi->video_size);
  736. x = mode->hsync_len + mode->left_margin;
  737. y = mode->vsync_len + mode->upper_margin;
  738. writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_bp);
  739. x = mode->right_margin;
  740. y = mode->lower_margin;
  741. writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_fp);
  742. x = mode->hsync_len;
  743. y = mode->vsync_len;
  744. writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_spw);
  745. if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
  746. setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_HOR);
  747. if (mode->sync & FB_SYNC_VERT_HIGH_ACT)
  748. setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_VER);
  749. }
  750. static void sunxi_hdmi_enable(void)
  751. {
  752. struct sunxi_hdmi_reg * const hdmi =
  753. (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
  754. udelay(100);
  755. setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
  756. }
  757. #endif /* CONFIG_VIDEO_HDMI */
  758. #ifdef CONFIG_VIDEO_VGA
  759. static void sunxi_vga_mode_set(void)
  760. {
  761. struct sunxi_ccm_reg * const ccm =
  762. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  763. struct sunxi_tve_reg * const tve =
  764. (struct sunxi_tve_reg *)SUNXI_TVE0_BASE;
  765. /* Clock on */
  766. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_TVE0);
  767. /* Set TVE in VGA mode */
  768. writel(SUNXI_TVE_GCTRL_DAC_INPUT(0, 1) |
  769. SUNXI_TVE_GCTRL_DAC_INPUT(1, 2) |
  770. SUNXI_TVE_GCTRL_DAC_INPUT(2, 3), &tve->gctrl);
  771. writel(SUNXI_TVE_GCTRL_CFG0_VGA, &tve->cfg0);
  772. writel(SUNXI_TVE_GCTRL_DAC_CFG0_VGA, &tve->dac_cfg0);
  773. writel(SUNXI_TVE_GCTRL_UNKNOWN1_VGA, &tve->unknown1);
  774. }
  775. static void sunxi_vga_enable(void)
  776. {
  777. struct sunxi_tve_reg * const tve =
  778. (struct sunxi_tve_reg *)SUNXI_TVE0_BASE;
  779. setbits_le32(&tve->gctrl, SUNXI_TVE_GCTRL_ENABLE);
  780. }
  781. #endif /* CONFIG_VIDEO_VGA */
  782. static void sunxi_drc_init(void)
  783. {
  784. #ifdef CONFIG_SUNXI_GEN_SUN6I
  785. struct sunxi_ccm_reg * const ccm =
  786. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  787. /* On sun6i the drc must be clocked even when in pass-through mode */
  788. #ifdef CONFIG_MACH_SUN8I_A33
  789. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_SAT);
  790. #endif
  791. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
  792. clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
  793. #endif
  794. }
  795. #ifdef CONFIG_VIDEO_VGA_VIA_LCD
  796. static void sunxi_vga_external_dac_enable(void)
  797. {
  798. int pin;
  799. pin = sunxi_name_to_gpio(CONFIG_VIDEO_VGA_EXTERNAL_DAC_EN);
  800. if (pin >= 0) {
  801. gpio_request(pin, "vga_enable");
  802. gpio_direction_output(pin, 1);
  803. }
  804. }
  805. #endif /* CONFIG_VIDEO_VGA_VIA_LCD */
  806. #ifdef CONFIG_VIDEO_LCD_SSD2828
  807. static int sunxi_ssd2828_init(const struct ctfb_res_modes *mode)
  808. {
  809. struct ssd2828_config cfg = {
  810. .csx_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_CS),
  811. .sck_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_SCLK),
  812. .sdi_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_MOSI),
  813. .sdo_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_MISO),
  814. .reset_pin = name_to_gpio(CONFIG_VIDEO_LCD_SSD2828_RESET),
  815. .ssd2828_tx_clk_khz = CONFIG_VIDEO_LCD_SSD2828_TX_CLK * 1000,
  816. .ssd2828_color_depth = 24,
  817. #ifdef CONFIG_VIDEO_LCD_PANEL_MIPI_4_LANE_513_MBPS_VIA_SSD2828
  818. .mipi_dsi_number_of_data_lanes = 4,
  819. .mipi_dsi_bitrate_per_data_lane_mbps = 513,
  820. .mipi_dsi_delay_after_exit_sleep_mode_ms = 100,
  821. .mipi_dsi_delay_after_set_display_on_ms = 200
  822. #else
  823. #error MIPI LCD panel needs configuration parameters
  824. #endif
  825. };
  826. if (cfg.csx_pin == -1 || cfg.sck_pin == -1 || cfg.sdi_pin == -1) {
  827. printf("SSD2828: SPI pins are not properly configured\n");
  828. return 1;
  829. }
  830. if (cfg.reset_pin == -1) {
  831. printf("SSD2828: Reset pin is not properly configured\n");
  832. return 1;
  833. }
  834. return ssd2828_init(&cfg, mode);
  835. }
  836. #endif /* CONFIG_VIDEO_LCD_SSD2828 */
  837. static void sunxi_engines_init(void)
  838. {
  839. sunxi_composer_init();
  840. sunxi_lcdc_init();
  841. sunxi_drc_init();
  842. }
  843. static void sunxi_mode_set(const struct ctfb_res_modes *mode,
  844. unsigned int address)
  845. {
  846. int __maybe_unused clk_div, clk_double;
  847. switch (sunxi_display.monitor) {
  848. case sunxi_monitor_none:
  849. break;
  850. case sunxi_monitor_dvi:
  851. case sunxi_monitor_hdmi:
  852. #ifdef CONFIG_VIDEO_HDMI
  853. sunxi_composer_mode_set(mode, address);
  854. sunxi_lcdc_tcon1_mode_set(mode, &clk_div, &clk_double, 0);
  855. sunxi_hdmi_mode_set(mode, clk_div, clk_double);
  856. sunxi_composer_enable();
  857. sunxi_lcdc_enable();
  858. sunxi_hdmi_enable();
  859. #endif
  860. break;
  861. case sunxi_monitor_lcd:
  862. sunxi_lcdc_panel_enable();
  863. if (IS_ENABLED(CONFIG_VIDEO_LCD_HITACHI_TX18D42VM)) {
  864. mdelay(50); /* Wait for lcd controller power on */
  865. hitachi_tx18d42vm_init();
  866. }
  867. if (IS_ENABLED(CONFIG_VIDEO_LCD_TL059WV5C0)) {
  868. unsigned int orig_i2c_bus = i2c_get_bus_num();
  869. i2c_set_bus_num(CONFIG_VIDEO_LCD_I2C_BUS);
  870. i2c_reg_write(0x5c, 0x04, 0x42); /* Turn on the LCD */
  871. i2c_set_bus_num(orig_i2c_bus);
  872. }
  873. sunxi_composer_mode_set(mode, address);
  874. sunxi_lcdc_tcon0_mode_set(mode, false);
  875. sunxi_composer_enable();
  876. sunxi_lcdc_enable();
  877. #ifdef CONFIG_VIDEO_LCD_SSD2828
  878. sunxi_ssd2828_init(mode);
  879. #endif
  880. sunxi_lcdc_backlight_enable();
  881. break;
  882. case sunxi_monitor_vga:
  883. #ifdef CONFIG_VIDEO_VGA
  884. sunxi_composer_mode_set(mode, address);
  885. sunxi_lcdc_tcon1_mode_set(mode, &clk_div, &clk_double, 1);
  886. sunxi_vga_mode_set();
  887. sunxi_composer_enable();
  888. sunxi_lcdc_enable();
  889. sunxi_vga_enable();
  890. #elif defined CONFIG_VIDEO_VGA_VIA_LCD
  891. sunxi_composer_mode_set(mode, address);
  892. sunxi_lcdc_tcon0_mode_set(mode, true);
  893. sunxi_composer_enable();
  894. sunxi_lcdc_enable();
  895. sunxi_vga_external_dac_enable();
  896. #endif
  897. break;
  898. }
  899. }
  900. static const char *sunxi_get_mon_desc(enum sunxi_monitor monitor)
  901. {
  902. switch (monitor) {
  903. case sunxi_monitor_none: return "none";
  904. case sunxi_monitor_dvi: return "dvi";
  905. case sunxi_monitor_hdmi: return "hdmi";
  906. case sunxi_monitor_lcd: return "lcd";
  907. case sunxi_monitor_vga: return "vga";
  908. }
  909. return NULL; /* never reached */
  910. }
  911. ulong board_get_usable_ram_top(ulong total_size)
  912. {
  913. return gd->ram_top - CONFIG_SUNXI_MAX_FB_SIZE;
  914. }
  915. void *video_hw_init(void)
  916. {
  917. static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
  918. const struct ctfb_res_modes *mode;
  919. struct ctfb_res_modes custom;
  920. const char *options;
  921. #ifdef CONFIG_VIDEO_HDMI
  922. int ret, hpd, hpd_delay, edid;
  923. #endif
  924. char mon[16];
  925. char *lcd_mode = CONFIG_VIDEO_LCD_MODE;
  926. int i;
  927. memset(&sunxi_display, 0, sizeof(struct sunxi_display));
  928. video_get_ctfb_res_modes(RES_MODE_1024x768, 24, &mode,
  929. &sunxi_display.depth, &options);
  930. #ifdef CONFIG_VIDEO_HDMI
  931. hpd = video_get_option_int(options, "hpd", 1);
  932. hpd_delay = video_get_option_int(options, "hpd_delay", 500);
  933. edid = video_get_option_int(options, "edid", 1);
  934. sunxi_display.monitor = sunxi_monitor_dvi;
  935. #elif defined CONFIG_VIDEO_VGA_VIA_LCD
  936. sunxi_display.monitor = sunxi_monitor_vga;
  937. #else
  938. sunxi_display.monitor = sunxi_monitor_lcd;
  939. #endif
  940. video_get_option_string(options, "monitor", mon, sizeof(mon),
  941. sunxi_get_mon_desc(sunxi_display.monitor));
  942. for (i = 0; i <= SUNXI_MONITOR_LAST; i++) {
  943. if (strcmp(mon, sunxi_get_mon_desc(i)) == 0) {
  944. sunxi_display.monitor = i;
  945. break;
  946. }
  947. }
  948. if (i > SUNXI_MONITOR_LAST)
  949. printf("Unknown monitor: '%s', falling back to '%s'\n",
  950. mon, sunxi_get_mon_desc(sunxi_display.monitor));
  951. #ifdef CONFIG_VIDEO_HDMI
  952. /* If HDMI/DVI is selected do HPD & EDID, and handle fallback */
  953. if (sunxi_display.monitor == sunxi_monitor_dvi ||
  954. sunxi_display.monitor == sunxi_monitor_hdmi) {
  955. /* Always call hdp_detect, as it also enables clocks, etc. */
  956. ret = sunxi_hdmi_hpd_detect(hpd_delay);
  957. if (ret) {
  958. printf("HDMI connected: ");
  959. if (edid && sunxi_hdmi_edid_get_mode(&custom) == 0)
  960. mode = &custom;
  961. } else if (hpd) {
  962. sunxi_hdmi_shutdown();
  963. /* Fallback to lcd / vga / none */
  964. if (lcd_mode[0]) {
  965. sunxi_display.monitor = sunxi_monitor_lcd;
  966. } else {
  967. #if defined CONFIG_VIDEO_VGA_VIA_LCD || defined CONFIG_VIDEO_VGA
  968. sunxi_display.monitor = sunxi_monitor_vga;
  969. #else
  970. sunxi_display.monitor = sunxi_monitor_none;
  971. #endif
  972. }
  973. } /* else continue with hdmi/dvi without a cable connected */
  974. }
  975. #endif
  976. switch (sunxi_display.monitor) {
  977. case sunxi_monitor_none:
  978. return NULL;
  979. case sunxi_monitor_dvi:
  980. case sunxi_monitor_hdmi:
  981. #ifdef CONFIG_VIDEO_HDMI
  982. break;
  983. #else
  984. printf("HDMI/DVI not supported on this board\n");
  985. sunxi_display.monitor = sunxi_monitor_none;
  986. return NULL;
  987. #endif
  988. case sunxi_monitor_lcd:
  989. if (lcd_mode[0]) {
  990. sunxi_display.depth = video_get_params(&custom, lcd_mode);
  991. mode = &custom;
  992. break;
  993. }
  994. printf("LCD not supported on this board\n");
  995. sunxi_display.monitor = sunxi_monitor_none;
  996. return NULL;
  997. case sunxi_monitor_vga:
  998. #if defined CONFIG_VIDEO_VGA_VIA_LCD || defined CONFIG_VIDEO_VGA
  999. sunxi_display.depth = 18;
  1000. break;
  1001. #else
  1002. printf("VGA not supported on this board\n");
  1003. sunxi_display.monitor = sunxi_monitor_none;
  1004. return NULL;
  1005. #endif
  1006. }
  1007. if (mode->vmode != FB_VMODE_NONINTERLACED) {
  1008. printf("Only non-interlaced modes supported, falling back to 1024x768\n");
  1009. mode = &res_mode_init[RES_MODE_1024x768];
  1010. } else {
  1011. printf("Setting up a %dx%d %s console\n", mode->xres,
  1012. mode->yres, sunxi_get_mon_desc(sunxi_display.monitor));
  1013. }
  1014. sunxi_display.fb_size =
  1015. (mode->xres * mode->yres * 4 + 0xfff) & ~0xfff;
  1016. if (sunxi_display.fb_size > CONFIG_SUNXI_MAX_FB_SIZE) {
  1017. printf("Error need %dkB for fb, but only %dkB is reserved\n",
  1018. sunxi_display.fb_size >> 10,
  1019. CONFIG_SUNXI_MAX_FB_SIZE >> 10);
  1020. return NULL;
  1021. }
  1022. gd->fb_base = gd->bd->bi_dram[0].start +
  1023. gd->bd->bi_dram[0].size - sunxi_display.fb_size;
  1024. sunxi_engines_init();
  1025. sunxi_mode_set(mode, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
  1026. /*
  1027. * These are the only members of this structure that are used. All the
  1028. * others are driver specific. There is nothing to decribe pitch or
  1029. * stride, but we are lucky with our hw.
  1030. */
  1031. graphic_device->frameAdrs = gd->fb_base;
  1032. graphic_device->gdfIndex = GDF_32BIT_X888RGB;
  1033. graphic_device->gdfBytesPP = 4;
  1034. graphic_device->winSizeX = mode->xres;
  1035. graphic_device->winSizeY = mode->yres;
  1036. return graphic_device;
  1037. }
  1038. /*
  1039. * Simplefb support.
  1040. */
  1041. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_VIDEO_DT_SIMPLEFB)
  1042. int sunxi_simplefb_setup(void *blob)
  1043. {
  1044. static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
  1045. int offset, ret;
  1046. u64 start, size;
  1047. const char *pipeline = NULL;
  1048. #ifdef CONFIG_MACH_SUN4I
  1049. #define PIPELINE_PREFIX "de_fe0-"
  1050. #else
  1051. #define PIPELINE_PREFIX
  1052. #endif
  1053. switch (sunxi_display.monitor) {
  1054. case sunxi_monitor_none:
  1055. return 0;
  1056. case sunxi_monitor_dvi:
  1057. case sunxi_monitor_hdmi:
  1058. pipeline = PIPELINE_PREFIX "de_be0-lcd0-hdmi";
  1059. break;
  1060. case sunxi_monitor_lcd:
  1061. pipeline = PIPELINE_PREFIX "de_be0-lcd0";
  1062. break;
  1063. case sunxi_monitor_vga:
  1064. #ifdef CONFIG_VIDEO_VGA
  1065. pipeline = PIPELINE_PREFIX "de_be0-lcd0-tve0";
  1066. #elif defined CONFIG_VIDEO_VGA_VIA_LCD
  1067. pipeline = PIPELINE_PREFIX "de_be0-lcd0";
  1068. #endif
  1069. break;
  1070. }
  1071. /* Find a prefilled simpefb node, matching out pipeline config */
  1072. offset = fdt_node_offset_by_compatible(blob, -1,
  1073. "allwinner,simple-framebuffer");
  1074. while (offset >= 0) {
  1075. ret = fdt_find_string(blob, offset, "allwinner,pipeline",
  1076. pipeline);
  1077. if (ret == 0)
  1078. break;
  1079. offset = fdt_node_offset_by_compatible(blob, offset,
  1080. "allwinner,simple-framebuffer");
  1081. }
  1082. if (offset < 0) {
  1083. eprintf("Cannot setup simplefb: node not found\n");
  1084. return 0; /* Keep older kernels working */
  1085. }
  1086. /*
  1087. * Do not report the framebuffer as free RAM to the OS, note we cannot
  1088. * use fdt_add_mem_rsv() here, because then it is still seen as RAM,
  1089. * and e.g. Linux refuses to iomap RAM on ARM, see:
  1090. * linux/arch/arm/mm/ioremap.c around line 301.
  1091. */
  1092. start = gd->bd->bi_dram[0].start;
  1093. size = gd->bd->bi_dram[0].size - sunxi_display.fb_size;
  1094. ret = fdt_fixup_memory_banks(blob, &start, &size, 1);
  1095. if (ret) {
  1096. eprintf("Cannot setup simplefb: Error reserving memory\n");
  1097. return ret;
  1098. }
  1099. ret = fdt_setup_simplefb_node(blob, offset, gd->fb_base,
  1100. graphic_device->winSizeX, graphic_device->winSizeY,
  1101. graphic_device->winSizeX * graphic_device->gdfBytesPP,
  1102. "x8r8g8b8");
  1103. if (ret)
  1104. eprintf("Cannot setup simplefb: Error setting properties\n");
  1105. return ret;
  1106. }
  1107. #endif /* CONFIG_OF_BOARD_SETUP && CONFIG_VIDEO_DT_SIMPLEFB */