display.c 14 KB

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