display.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Extracted from Chromium coreboot commit 3f59b13d
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <edid.h>
  11. #include <errno.h>
  12. #include <display.h>
  13. #include <edid.h>
  14. #include <fdtdec.h>
  15. #include <lcd.h>
  16. #include <video.h>
  17. #include <asm/gpio.h>
  18. #include <asm/io.h>
  19. #include <asm/arch/clock.h>
  20. #include <asm/arch/pwm.h>
  21. #include <asm/arch-tegra/dc.h>
  22. #include <dm/uclass-internal.h>
  23. #include "displayport.h"
  24. DECLARE_GLOBAL_DATA_PTR;
  25. /* return in 1000ths of a Hertz */
  26. static int tegra_dc_calc_refresh(const struct display_timing *timing)
  27. {
  28. int h_total, v_total, refresh;
  29. int pclk = timing->pixelclock.typ;
  30. h_total = timing->hactive.typ + timing->hfront_porch.typ +
  31. timing->hback_porch.typ + timing->hsync_len.typ;
  32. v_total = timing->vactive.typ + timing->vfront_porch.typ +
  33. timing->vback_porch.typ + timing->vsync_len.typ;
  34. if (!pclk || !h_total || !v_total)
  35. return 0;
  36. refresh = pclk / h_total;
  37. refresh *= 1000;
  38. refresh /= v_total;
  39. return refresh;
  40. }
  41. static void print_mode(const struct display_timing *timing)
  42. {
  43. int refresh = tegra_dc_calc_refresh(timing);
  44. debug("MODE:%dx%d@%d.%03uHz pclk=%d\n",
  45. timing->hactive.typ, timing->vactive.typ, refresh / 1000,
  46. refresh % 1000, timing->pixelclock.typ);
  47. }
  48. static int update_display_mode(struct dc_ctlr *disp_ctrl,
  49. const struct display_timing *timing,
  50. int href_to_sync, int vref_to_sync)
  51. {
  52. print_mode(timing);
  53. writel(0x1, &disp_ctrl->disp.disp_timing_opt);
  54. writel(vref_to_sync << 16 | href_to_sync,
  55. &disp_ctrl->disp.ref_to_sync);
  56. writel(timing->vsync_len.typ << 16 | timing->hsync_len.typ,
  57. &disp_ctrl->disp.sync_width);
  58. writel(((timing->vback_porch.typ - vref_to_sync) << 16) |
  59. timing->hback_porch.typ, &disp_ctrl->disp.back_porch);
  60. writel(((timing->vfront_porch.typ + vref_to_sync) << 16) |
  61. timing->hfront_porch.typ, &disp_ctrl->disp.front_porch);
  62. writel(timing->hactive.typ | (timing->vactive.typ << 16),
  63. &disp_ctrl->disp.disp_active);
  64. /**
  65. * We want to use PLLD_out0, which is PLLD / 2:
  66. * PixelClock = (PLLD / 2) / ShiftClockDiv / PixelClockDiv.
  67. *
  68. * Currently most panels work inside clock range 50MHz~100MHz, and PLLD
  69. * has some requirements to have VCO in range 500MHz~1000MHz (see
  70. * clock.c for more detail). To simplify calculation, we set
  71. * PixelClockDiv to 1 and ShiftClockDiv to 1. In future these values
  72. * may be calculated by clock_display, to allow wider frequency range.
  73. *
  74. * Note ShiftClockDiv is a 7.1 format value.
  75. */
  76. const u32 shift_clock_div = 1;
  77. writel((PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT) |
  78. ((shift_clock_div - 1) * 2) << SHIFT_CLK_DIVIDER_SHIFT,
  79. &disp_ctrl->disp.disp_clk_ctrl);
  80. debug("%s: PixelClock=%u, ShiftClockDiv=%u\n", __func__,
  81. timing->pixelclock.typ, shift_clock_div);
  82. return 0;
  83. }
  84. static u32 tegra_dc_poll_register(void *reg,
  85. u32 mask, u32 exp_val, u32 poll_interval_us, u32 timeout_us)
  86. {
  87. u32 temp = timeout_us;
  88. u32 reg_val = 0;
  89. do {
  90. udelay(poll_interval_us);
  91. reg_val = readl(reg);
  92. if (timeout_us > poll_interval_us)
  93. timeout_us -= poll_interval_us;
  94. else
  95. break;
  96. } while ((reg_val & mask) != exp_val);
  97. if ((reg_val & mask) == exp_val)
  98. return 0; /* success */
  99. return temp;
  100. }
  101. int tegra_dc_sor_general_act(struct dc_ctlr *disp_ctrl)
  102. {
  103. writel(GENERAL_ACT_REQ, &disp_ctrl->cmd.state_ctrl);
  104. if (tegra_dc_poll_register(&disp_ctrl->cmd.state_ctrl,
  105. GENERAL_ACT_REQ, 0, 100,
  106. DC_POLL_TIMEOUT_MS * 1000)) {
  107. debug("dc timeout waiting for DC to stop\n");
  108. return -ETIMEDOUT;
  109. }
  110. return 0;
  111. }
  112. static struct display_timing min_mode = {
  113. .hsync_len = { .typ = 1 },
  114. .vsync_len = { .typ = 1 },
  115. .hback_porch = { .typ = 20 },
  116. .vback_porch = { .typ = 0 },
  117. .hactive = { .typ = 16 },
  118. .vactive = { .typ = 16 },
  119. .hfront_porch = { .typ = 1 },
  120. .vfront_porch = { .typ = 2 },
  121. };
  122. /* Disable windows and set minimum raster timings */
  123. void tegra_dc_sor_disable_win_short_raster(struct dc_ctlr *disp_ctrl,
  124. int *dc_reg_ctx)
  125. {
  126. const int href_to_sync = 0, vref_to_sync = 1;
  127. int selected_windows, i;
  128. selected_windows = readl(&disp_ctrl->cmd.disp_win_header);
  129. /* Store and clear window options */
  130. for (i = 0; i < DC_N_WINDOWS; ++i) {
  131. writel(WINDOW_A_SELECT << i, &disp_ctrl->cmd.disp_win_header);
  132. dc_reg_ctx[i] = readl(&disp_ctrl->win.win_opt);
  133. writel(0, &disp_ctrl->win.win_opt);
  134. writel(WIN_A_ACT_REQ << i, &disp_ctrl->cmd.state_ctrl);
  135. }
  136. writel(selected_windows, &disp_ctrl->cmd.disp_win_header);
  137. /* Store current raster timings and set minimum timings */
  138. dc_reg_ctx[i++] = readl(&disp_ctrl->disp.ref_to_sync);
  139. writel(href_to_sync | (vref_to_sync << 16),
  140. &disp_ctrl->disp.ref_to_sync);
  141. dc_reg_ctx[i++] = readl(&disp_ctrl->disp.sync_width);
  142. writel(min_mode.hsync_len.typ | (min_mode.vsync_len.typ << 16),
  143. &disp_ctrl->disp.sync_width);
  144. dc_reg_ctx[i++] = readl(&disp_ctrl->disp.back_porch);
  145. writel(min_mode.hback_porch.typ | (min_mode.vback_porch.typ << 16),
  146. &disp_ctrl->disp.back_porch);
  147. dc_reg_ctx[i++] = readl(&disp_ctrl->disp.front_porch);
  148. writel(min_mode.hfront_porch.typ | (min_mode.vfront_porch.typ << 16),
  149. &disp_ctrl->disp.front_porch);
  150. dc_reg_ctx[i++] = readl(&disp_ctrl->disp.disp_active);
  151. writel(min_mode.hactive.typ | (min_mode.vactive.typ << 16),
  152. &disp_ctrl->disp.disp_active);
  153. writel(GENERAL_ACT_REQ, &disp_ctrl->cmd.state_ctrl);
  154. }
  155. /* Restore previous windows status and raster timings */
  156. void tegra_dc_sor_restore_win_and_raster(struct dc_ctlr *disp_ctrl,
  157. int *dc_reg_ctx)
  158. {
  159. int selected_windows, i;
  160. selected_windows = readl(&disp_ctrl->cmd.disp_win_header);
  161. for (i = 0; i < DC_N_WINDOWS; ++i) {
  162. writel(WINDOW_A_SELECT << i, &disp_ctrl->cmd.disp_win_header);
  163. writel(dc_reg_ctx[i], &disp_ctrl->win.win_opt);
  164. writel(WIN_A_ACT_REQ << i, &disp_ctrl->cmd.state_ctrl);
  165. }
  166. writel(selected_windows, &disp_ctrl->cmd.disp_win_header);
  167. writel(dc_reg_ctx[i++], &disp_ctrl->disp.ref_to_sync);
  168. writel(dc_reg_ctx[i++], &disp_ctrl->disp.sync_width);
  169. writel(dc_reg_ctx[i++], &disp_ctrl->disp.back_porch);
  170. writel(dc_reg_ctx[i++], &disp_ctrl->disp.front_porch);
  171. writel(dc_reg_ctx[i++], &disp_ctrl->disp.disp_active);
  172. writel(GENERAL_UPDATE, &disp_ctrl->cmd.state_ctrl);
  173. }
  174. static int tegra_depth_for_bpp(int bpp)
  175. {
  176. switch (bpp) {
  177. case 32:
  178. return COLOR_DEPTH_R8G8B8A8;
  179. case 16:
  180. return COLOR_DEPTH_B5G6R5;
  181. default:
  182. debug("Unsupported LCD bit depth");
  183. return -1;
  184. }
  185. }
  186. static int update_window(struct dc_ctlr *disp_ctrl,
  187. u32 frame_buffer, int fb_bits_per_pixel,
  188. const struct display_timing *timing)
  189. {
  190. const u32 colour_white = 0xffffff;
  191. int colour_depth;
  192. u32 val;
  193. writel(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
  194. writel(((timing->vactive.typ << 16) | timing->hactive.typ),
  195. &disp_ctrl->win.size);
  196. writel(((timing->vactive.typ << 16) |
  197. (timing->hactive.typ * fb_bits_per_pixel / 8)),
  198. &disp_ctrl->win.prescaled_size);
  199. writel(((timing->hactive.typ * fb_bits_per_pixel / 8 + 31) /
  200. 32 * 32), &disp_ctrl->win.line_stride);
  201. colour_depth = tegra_depth_for_bpp(fb_bits_per_pixel);
  202. if (colour_depth == -1)
  203. return -EINVAL;
  204. writel(colour_depth, &disp_ctrl->win.color_depth);
  205. writel(frame_buffer, &disp_ctrl->winbuf.start_addr);
  206. writel(0x1000 << V_DDA_INC_SHIFT | 0x1000 << H_DDA_INC_SHIFT,
  207. &disp_ctrl->win.dda_increment);
  208. writel(colour_white, &disp_ctrl->disp.blend_background_color);
  209. writel(CTRL_MODE_C_DISPLAY << CTRL_MODE_SHIFT,
  210. &disp_ctrl->cmd.disp_cmd);
  211. writel(WRITE_MUX_ACTIVE, &disp_ctrl->cmd.state_access);
  212. val = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  213. val |= GENERAL_UPDATE | WIN_A_UPDATE;
  214. writel(val, &disp_ctrl->cmd.state_ctrl);
  215. /* Enable win_a */
  216. val = readl(&disp_ctrl->win.win_opt);
  217. writel(val | WIN_ENABLE, &disp_ctrl->win.win_opt);
  218. return 0;
  219. }
  220. static int tegra_dc_init(struct dc_ctlr *disp_ctrl)
  221. {
  222. /* do not accept interrupts during initialization */
  223. writel(0x00000000, &disp_ctrl->cmd.int_mask);
  224. writel(WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
  225. &disp_ctrl->cmd.state_access);
  226. writel(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
  227. writel(0x00000000, &disp_ctrl->win.win_opt);
  228. writel(0x00000000, &disp_ctrl->win.byte_swap);
  229. writel(0x00000000, &disp_ctrl->win.buffer_ctrl);
  230. writel(0x00000000, &disp_ctrl->win.pos);
  231. writel(0x00000000, &disp_ctrl->win.h_initial_dda);
  232. writel(0x00000000, &disp_ctrl->win.v_initial_dda);
  233. writel(0x00000000, &disp_ctrl->win.dda_increment);
  234. writel(0x00000000, &disp_ctrl->win.dv_ctrl);
  235. writel(0x01000000, &disp_ctrl->win.blend_layer_ctrl);
  236. writel(0x00000000, &disp_ctrl->win.blend_match_select);
  237. writel(0x00000000, &disp_ctrl->win.blend_nomatch_select);
  238. writel(0x00000000, &disp_ctrl->win.blend_alpha_1bit);
  239. writel(0x00000000, &disp_ctrl->winbuf.start_addr_hi);
  240. writel(0x00000000, &disp_ctrl->winbuf.addr_h_offset);
  241. writel(0x00000000, &disp_ctrl->winbuf.addr_v_offset);
  242. writel(0x00000000, &disp_ctrl->com.crc_checksum);
  243. writel(0x00000000, &disp_ctrl->com.pin_output_enb[0]);
  244. writel(0x00000000, &disp_ctrl->com.pin_output_enb[1]);
  245. writel(0x00000000, &disp_ctrl->com.pin_output_enb[2]);
  246. writel(0x00000000, &disp_ctrl->com.pin_output_enb[3]);
  247. writel(0x00000000, &disp_ctrl->disp.disp_signal_opt0);
  248. return 0;
  249. }
  250. static void dump_config(int panel_bpp, struct display_timing *timing)
  251. {
  252. printf("timing->hactive.typ = %d\n", timing->hactive.typ);
  253. printf("timing->vactive.typ = %d\n", timing->vactive.typ);
  254. printf("timing->pixelclock.typ = %d\n", timing->pixelclock.typ);
  255. printf("timing->hfront_porch.typ = %d\n", timing->hfront_porch.typ);
  256. printf("timing->hsync_len.typ = %d\n", timing->hsync_len.typ);
  257. printf("timing->hback_porch.typ = %d\n", timing->hback_porch.typ);
  258. printf("timing->vfront_porch.typ %d\n", timing->vfront_porch.typ);
  259. printf("timing->vsync_len.typ = %d\n", timing->vsync_len.typ);
  260. printf("timing->vback_porch.typ = %d\n", timing->vback_porch.typ);
  261. printf("panel_bits_per_pixel = %d\n", panel_bpp);
  262. }
  263. static int display_update_config_from_edid(struct udevice *dp_dev,
  264. int *panel_bppp,
  265. struct display_timing *timing)
  266. {
  267. return display_read_timing(dp_dev, timing);
  268. }
  269. static int display_init(struct udevice *dev, void *lcdbase,
  270. int fb_bits_per_pixel, struct display_timing *timing)
  271. {
  272. struct display_plat *disp_uc_plat;
  273. struct dc_ctlr *dc_ctlr;
  274. const void *blob = gd->fdt_blob;
  275. struct udevice *dp_dev;
  276. const int href_to_sync = 1, vref_to_sync = 1;
  277. int panel_bpp = 18; /* default 18 bits per pixel */
  278. u32 plld_rate;
  279. int ret;
  280. /*
  281. * Before we probe the display device (eDP), tell it that this device
  282. * is the source of the display data.
  283. */
  284. ret = uclass_find_first_device(UCLASS_DISPLAY, &dp_dev);
  285. if (ret) {
  286. debug("%s: device '%s' display not found (ret=%d)\n", __func__,
  287. dev->name, ret);
  288. return ret;
  289. }
  290. disp_uc_plat = dev_get_uclass_platdata(dp_dev);
  291. debug("Found device '%s', disp_uc_priv=%p\n", dp_dev->name,
  292. disp_uc_plat);
  293. disp_uc_plat->src_dev = dev;
  294. ret = uclass_get_device(UCLASS_DISPLAY, 0, &dp_dev);
  295. if (ret) {
  296. debug("%s: Failed to probe eDP, ret=%d\n", __func__, ret);
  297. return ret;
  298. }
  299. dc_ctlr = (struct dc_ctlr *)fdtdec_get_addr(blob, dev->of_offset,
  300. "reg");
  301. if (fdtdec_decode_display_timing(blob, dev->of_offset, 0, timing)) {
  302. debug("%s: Failed to decode display timing\n", __func__);
  303. return -EINVAL;
  304. }
  305. ret = display_update_config_from_edid(dp_dev, &panel_bpp, timing);
  306. if (ret) {
  307. debug("%s: Failed to decode EDID, using defaults\n", __func__);
  308. dump_config(panel_bpp, timing);
  309. }
  310. /*
  311. * The plld is programmed with the assumption of the SHIFT_CLK_DIVIDER
  312. * and PIXEL_CLK_DIVIDER are zero (divide by 1). See the
  313. * update_display_mode() for detail.
  314. */
  315. plld_rate = clock_set_display_rate(timing->pixelclock.typ * 2);
  316. if (plld_rate == 0) {
  317. printf("dc: clock init failed\n");
  318. return -EIO;
  319. } else if (plld_rate != timing->pixelclock.typ * 2) {
  320. debug("dc: plld rounded to %u\n", plld_rate);
  321. timing->pixelclock.typ = plld_rate / 2;
  322. }
  323. /* Init dc */
  324. ret = tegra_dc_init(dc_ctlr);
  325. if (ret) {
  326. debug("dc: init failed\n");
  327. return ret;
  328. }
  329. /* Configure dc mode */
  330. ret = update_display_mode(dc_ctlr, timing, href_to_sync, vref_to_sync);
  331. if (ret) {
  332. debug("dc: failed to configure display mode\n");
  333. return ret;
  334. }
  335. /* Enable dp */
  336. ret = display_enable(dp_dev, panel_bpp, timing);
  337. if (ret) {
  338. debug("dc: failed to enable display: ret=%d\n", ret);
  339. return ret;
  340. }
  341. ret = update_window(dc_ctlr, (ulong)lcdbase, fb_bits_per_pixel, timing);
  342. if (ret) {
  343. debug("dc: failed to update window\n");
  344. return ret;
  345. }
  346. return 0;
  347. }
  348. enum {
  349. /* Maximum LCD size we support */
  350. LCD_MAX_WIDTH = 1920,
  351. LCD_MAX_HEIGHT = 1200,
  352. LCD_MAX_LOG2_BPP = 4, /* 2^4 = 16 bpp */
  353. };
  354. static int tegra124_lcd_init(struct udevice *dev, void *lcdbase,
  355. enum video_log2_bpp l2bpp)
  356. {
  357. struct video_priv *uc_priv = dev_get_uclass_priv(dev);
  358. struct display_timing timing;
  359. int ret;
  360. clock_set_up_plldp();
  361. clock_start_periph_pll(PERIPH_ID_HOST1X, CLOCK_ID_PERIPH, 408000000);
  362. clock_enable(PERIPH_ID_HOST1X);
  363. clock_enable(PERIPH_ID_DISP1);
  364. clock_enable(PERIPH_ID_PWM);
  365. clock_enable(PERIPH_ID_DPAUX);
  366. clock_enable(PERIPH_ID_SOR0);
  367. udelay(2);
  368. reset_set_enable(PERIPH_ID_HOST1X, 0);
  369. reset_set_enable(PERIPH_ID_DISP1, 0);
  370. reset_set_enable(PERIPH_ID_PWM, 0);
  371. reset_set_enable(PERIPH_ID_DPAUX, 0);
  372. reset_set_enable(PERIPH_ID_SOR0, 0);
  373. ret = display_init(dev, lcdbase, 1 << l2bpp, &timing);
  374. if (ret)
  375. return ret;
  376. uc_priv->xsize = roundup(timing.hactive.typ, 16);
  377. uc_priv->ysize = timing.vactive.typ;
  378. uc_priv->bpix = l2bpp;
  379. video_set_flush_dcache(dev, 1);
  380. debug("%s: done\n", __func__);
  381. return 0;
  382. }
  383. static int tegra124_lcd_probe(struct udevice *dev)
  384. {
  385. struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
  386. ulong start;
  387. int ret;
  388. start = get_timer(0);
  389. ret = tegra124_lcd_init(dev, (void *)plat->base, VIDEO_BPP16);
  390. debug("LCD init took %lu ms\n", get_timer(start));
  391. if (ret)
  392. printf("%s: Error %d\n", __func__, ret);
  393. return 0;
  394. }
  395. static int tegra124_lcd_bind(struct udevice *dev)
  396. {
  397. struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
  398. uc_plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
  399. (1 << VIDEO_BPP16) / 8;
  400. debug("%s: Frame buffer size %x\n", __func__, uc_plat->size);
  401. return 0;
  402. }
  403. static const struct udevice_id tegra124_lcd_ids[] = {
  404. { .compatible = "nvidia,tegra124-dc" },
  405. { }
  406. };
  407. U_BOOT_DRIVER(tegra124_dc) = {
  408. .name = "tegra124-dc",
  409. .id = UCLASS_VIDEO,
  410. .of_match = tegra124_lcd_ids,
  411. .bind = tegra124_lcd_bind,
  412. .probe = tegra124_lcd_probe,
  413. };