display.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * (C) Copyright 2010
  3. * NVIDIA Corporation <www.nvidia.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/clock.h>
  26. #include <asm/arch/tegra.h>
  27. #include <asm/arch/display.h>
  28. #include <asm/arch/dc.h>
  29. #include <asm/arch-tegra/clk_rst.h>
  30. #include <asm/arch-tegra/timer.h>
  31. static struct fdt_disp_config config;
  32. static void update_window(struct dc_ctlr *dc, struct disp_ctl_win *win)
  33. {
  34. unsigned h_dda, v_dda;
  35. unsigned long val;
  36. val = readl(&dc->cmd.disp_win_header);
  37. val |= WINDOW_A_SELECT;
  38. writel(val, &dc->cmd.disp_win_header);
  39. writel(win->fmt, &dc->win.color_depth);
  40. clrsetbits_le32(&dc->win.byte_swap, BYTE_SWAP_MASK,
  41. BYTE_SWAP_NOSWAP << BYTE_SWAP_SHIFT);
  42. val = win->out_x << H_POSITION_SHIFT;
  43. val |= win->out_y << V_POSITION_SHIFT;
  44. writel(val, &dc->win.pos);
  45. val = win->out_w << H_SIZE_SHIFT;
  46. val |= win->out_h << V_SIZE_SHIFT;
  47. writel(val, &dc->win.size);
  48. val = (win->w * win->bpp / 8) << H_PRESCALED_SIZE_SHIFT;
  49. val |= win->h << V_PRESCALED_SIZE_SHIFT;
  50. writel(val, &dc->win.prescaled_size);
  51. writel(0, &dc->win.h_initial_dda);
  52. writel(0, &dc->win.v_initial_dda);
  53. h_dda = (win->w * 0x1000) / max(win->out_w - 1, 1);
  54. v_dda = (win->h * 0x1000) / max(win->out_h - 1, 1);
  55. val = h_dda << H_DDA_INC_SHIFT;
  56. val |= v_dda << V_DDA_INC_SHIFT;
  57. writel(val, &dc->win.dda_increment);
  58. writel(win->stride, &dc->win.line_stride);
  59. writel(0, &dc->win.buf_stride);
  60. val = WIN_ENABLE;
  61. if (win->bpp < 24)
  62. val |= COLOR_EXPAND;
  63. writel(val, &dc->win.win_opt);
  64. writel((unsigned long)win->phys_addr, &dc->winbuf.start_addr);
  65. writel(win->x, &dc->winbuf.addr_h_offset);
  66. writel(win->y, &dc->winbuf.addr_v_offset);
  67. writel(0xff00, &dc->win.blend_nokey);
  68. writel(0xff00, &dc->win.blend_1win);
  69. val = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  70. val |= GENERAL_UPDATE | WIN_A_UPDATE;
  71. writel(val, &dc->cmd.state_ctrl);
  72. }
  73. static void write_pair(struct fdt_disp_config *config, int item, u32 *reg)
  74. {
  75. writel(config->horiz_timing[item] |
  76. (config->vert_timing[item] << 16), reg);
  77. }
  78. static int update_display_mode(struct dc_disp_reg *disp,
  79. struct fdt_disp_config *config)
  80. {
  81. unsigned long val;
  82. unsigned long rate;
  83. unsigned long div;
  84. writel(0x0, &disp->disp_timing_opt);
  85. write_pair(config, FDT_LCD_TIMING_REF_TO_SYNC, &disp->ref_to_sync);
  86. write_pair(config, FDT_LCD_TIMING_SYNC_WIDTH, &disp->sync_width);
  87. write_pair(config, FDT_LCD_TIMING_BACK_PORCH, &disp->back_porch);
  88. write_pair(config, FDT_LCD_TIMING_FRONT_PORCH, &disp->front_porch);
  89. writel(config->width | (config->height << 16), &disp->disp_active);
  90. val = DE_SELECT_ACTIVE << DE_SELECT_SHIFT;
  91. val |= DE_CONTROL_NORMAL << DE_CONTROL_SHIFT;
  92. writel(val, &disp->data_enable_opt);
  93. val = DATA_FORMAT_DF1P1C << DATA_FORMAT_SHIFT;
  94. val |= DATA_ALIGNMENT_MSB << DATA_ALIGNMENT_SHIFT;
  95. val |= DATA_ORDER_RED_BLUE << DATA_ORDER_SHIFT;
  96. writel(val, &disp->disp_interface_ctrl);
  97. /*
  98. * The pixel clock divider is in 7.1 format (where the bottom bit
  99. * represents 0.5). Here we calculate the divider needed to get from
  100. * the display clock (typically 600MHz) to the pixel clock. We round
  101. * up or down as requried.
  102. */
  103. rate = clock_get_periph_rate(PERIPH_ID_DISP1, CLOCK_ID_CGENERAL);
  104. div = ((rate * 2 + config->pixel_clock / 2) / config->pixel_clock) - 2;
  105. debug("Display clock %lu, divider %lu\n", rate, div);
  106. writel(0x00010001, &disp->shift_clk_opt);
  107. val = PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT;
  108. val |= div << SHIFT_CLK_DIVIDER_SHIFT;
  109. writel(val, &disp->disp_clk_ctrl);
  110. return 0;
  111. }
  112. /* Start up the display and turn on power to PWMs */
  113. static void basic_init(struct dc_cmd_reg *cmd)
  114. {
  115. u32 val;
  116. writel(0x00000100, &cmd->gen_incr_syncpt_ctrl);
  117. writel(0x0000011a, &cmd->cont_syncpt_vsync);
  118. writel(0x00000000, &cmd->int_type);
  119. writel(0x00000000, &cmd->int_polarity);
  120. writel(0x00000000, &cmd->int_mask);
  121. writel(0x00000000, &cmd->int_enb);
  122. val = PW0_ENABLE | PW1_ENABLE | PW2_ENABLE;
  123. val |= PW3_ENABLE | PW4_ENABLE | PM0_ENABLE;
  124. val |= PM1_ENABLE;
  125. writel(val, &cmd->disp_pow_ctrl);
  126. val = readl(&cmd->disp_cmd);
  127. val |= CTRL_MODE_C_DISPLAY << CTRL_MODE_SHIFT;
  128. writel(val, &cmd->disp_cmd);
  129. }
  130. static void basic_init_timer(struct dc_disp_reg *disp)
  131. {
  132. writel(0x00000020, &disp->mem_high_pri);
  133. writel(0x00000001, &disp->mem_high_pri_timer);
  134. }
  135. static const u32 rgb_enb_tab[PIN_REG_COUNT] = {
  136. 0x00000000,
  137. 0x00000000,
  138. 0x00000000,
  139. 0x00000000,
  140. };
  141. static const u32 rgb_polarity_tab[PIN_REG_COUNT] = {
  142. 0x00000000,
  143. 0x01000000,
  144. 0x00000000,
  145. 0x00000000,
  146. };
  147. static const u32 rgb_data_tab[PIN_REG_COUNT] = {
  148. 0x00000000,
  149. 0x00000000,
  150. 0x00000000,
  151. 0x00000000,
  152. };
  153. static const u32 rgb_sel_tab[PIN_OUTPUT_SEL_COUNT] = {
  154. 0x00000000,
  155. 0x00000000,
  156. 0x00000000,
  157. 0x00000000,
  158. 0x00210222,
  159. 0x00002200,
  160. 0x00020000,
  161. };
  162. static void rgb_enable(struct dc_com_reg *com)
  163. {
  164. int i;
  165. for (i = 0; i < PIN_REG_COUNT; i++) {
  166. writel(rgb_enb_tab[i], &com->pin_output_enb[i]);
  167. writel(rgb_polarity_tab[i], &com->pin_output_polarity[i]);
  168. writel(rgb_data_tab[i], &com->pin_output_data[i]);
  169. }
  170. for (i = 0; i < PIN_OUTPUT_SEL_COUNT; i++)
  171. writel(rgb_sel_tab[i], &com->pin_output_sel[i]);
  172. }
  173. int setup_window(struct disp_ctl_win *win, struct fdt_disp_config *config)
  174. {
  175. win->x = 0;
  176. win->y = 0;
  177. win->w = config->width;
  178. win->h = config->height;
  179. win->out_x = 0;
  180. win->out_y = 0;
  181. win->out_w = config->width;
  182. win->out_h = config->height;
  183. win->phys_addr = config->frame_buffer;
  184. win->stride = config->width * (1 << config->log2_bpp) / 8;
  185. debug("%s: depth = %d\n", __func__, config->log2_bpp);
  186. switch (config->log2_bpp) {
  187. case 5:
  188. case 24:
  189. win->fmt = COLOR_DEPTH_R8G8B8A8;
  190. win->bpp = 32;
  191. break;
  192. case 4:
  193. win->fmt = COLOR_DEPTH_B5G6R5;
  194. win->bpp = 16;
  195. break;
  196. default:
  197. debug("Unsupported LCD bit depth");
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. struct fdt_disp_config *tegra_display_get_config(void)
  203. {
  204. return config.valid ? &config : NULL;
  205. }
  206. static void debug_timing(const char *name, unsigned int timing[])
  207. {
  208. #ifdef DEBUG
  209. int i;
  210. debug("%s timing: ", name);
  211. for (i = 0; i < FDT_LCD_TIMING_COUNT; i++)
  212. debug("%d ", timing[i]);
  213. debug("\n");
  214. #endif
  215. }
  216. /**
  217. * Decode panel information from the fdt, according to a standard binding
  218. *
  219. * @param blob fdt blob
  220. * @param node offset of fdt node to read from
  221. * @param config structure to store fdt config into
  222. * @return 0 if ok, -ve on error
  223. */
  224. static int tegra_decode_panel(const void *blob, int node,
  225. struct fdt_disp_config *config)
  226. {
  227. int front, back, ref;
  228. config->width = fdtdec_get_int(blob, node, "xres", -1);
  229. config->height = fdtdec_get_int(blob, node, "yres", -1);
  230. config->pixel_clock = fdtdec_get_int(blob, node, "clock", 0);
  231. if (!config->pixel_clock || config->width == -1 ||
  232. config->height == -1) {
  233. debug("%s: Pixel parameters missing\n", __func__);
  234. return -FDT_ERR_NOTFOUND;
  235. }
  236. back = fdtdec_get_int(blob, node, "left-margin", -1);
  237. front = fdtdec_get_int(blob, node, "right-margin", -1);
  238. ref = fdtdec_get_int(blob, node, "hsync-len", -1);
  239. if ((back | front | ref) == -1) {
  240. debug("%s: Horizontal parameters missing\n", __func__);
  241. return -FDT_ERR_NOTFOUND;
  242. }
  243. /* Use a ref-to-sync of 1 always, and take this from the front porch */
  244. config->horiz_timing[FDT_LCD_TIMING_REF_TO_SYNC] = 1;
  245. config->horiz_timing[FDT_LCD_TIMING_SYNC_WIDTH] = ref;
  246. config->horiz_timing[FDT_LCD_TIMING_BACK_PORCH] = back;
  247. config->horiz_timing[FDT_LCD_TIMING_FRONT_PORCH] = front -
  248. config->horiz_timing[FDT_LCD_TIMING_REF_TO_SYNC];
  249. debug_timing("horiz", config->horiz_timing);
  250. back = fdtdec_get_int(blob, node, "upper-margin", -1);
  251. front = fdtdec_get_int(blob, node, "lower-margin", -1);
  252. ref = fdtdec_get_int(blob, node, "vsync-len", -1);
  253. if ((back | front | ref) == -1) {
  254. debug("%s: Vertical parameters missing\n", __func__);
  255. return -FDT_ERR_NOTFOUND;
  256. }
  257. config->vert_timing[FDT_LCD_TIMING_REF_TO_SYNC] = 1;
  258. config->vert_timing[FDT_LCD_TIMING_SYNC_WIDTH] = ref;
  259. config->vert_timing[FDT_LCD_TIMING_BACK_PORCH] = back;
  260. config->vert_timing[FDT_LCD_TIMING_FRONT_PORCH] = front -
  261. config->vert_timing[FDT_LCD_TIMING_REF_TO_SYNC];
  262. debug_timing("vert", config->vert_timing);
  263. return 0;
  264. }
  265. /**
  266. * Decode the display controller information from the fdt.
  267. *
  268. * @param blob fdt blob
  269. * @param config structure to store fdt config into
  270. * @return 0 if ok, -ve on error
  271. */
  272. static int tegra_display_decode_config(const void *blob,
  273. struct fdt_disp_config *config)
  274. {
  275. int node, rgb;
  276. int bpp, bit;
  277. /* TODO: Support multiple controllers */
  278. node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA20_DC);
  279. if (node < 0) {
  280. debug("%s: Cannot find display controller node in fdt\n",
  281. __func__);
  282. return node;
  283. }
  284. config->disp = (struct disp_ctlr *)fdtdec_get_addr(blob, node, "reg");
  285. if (!config->disp) {
  286. debug("%s: No display controller address\n", __func__);
  287. return -1;
  288. }
  289. rgb = fdt_subnode_offset(blob, node, "rgb");
  290. config->panel_node = fdtdec_lookup_phandle(blob, rgb, "nvidia,panel");
  291. if (!config->panel_node < 0) {
  292. debug("%s: Cannot find panel information\n", __func__);
  293. return -1;
  294. }
  295. if (tegra_decode_panel(blob, config->panel_node, config)) {
  296. debug("%s: Failed to decode panel information\n", __func__);
  297. return -1;
  298. }
  299. bpp = fdtdec_get_int(blob, config->panel_node, "nvidia,bits-per-pixel",
  300. -1);
  301. bit = ffs(bpp) - 1;
  302. if (bpp == (1 << bit))
  303. config->log2_bpp = bit;
  304. else
  305. config->log2_bpp = bpp;
  306. if (bpp == -1) {
  307. debug("%s: Pixel bpp parameters missing\n", __func__);
  308. return -FDT_ERR_NOTFOUND;
  309. }
  310. config->bpp = bpp;
  311. config->valid = 1; /* we have a valid configuration */
  312. return 0;
  313. }
  314. int tegra_display_probe(const void *blob, void *default_lcd_base)
  315. {
  316. struct disp_ctl_win window;
  317. struct dc_ctlr *dc;
  318. if (tegra_display_decode_config(blob, &config))
  319. return -1;
  320. config.frame_buffer = (u32)default_lcd_base;
  321. dc = (struct dc_ctlr *)config.disp;
  322. /*
  323. * A header file for clock constants was NAKed upstream.
  324. * TODO: Put this into the FDT and fdt_lcd struct when we have clock
  325. * support there
  326. */
  327. clock_start_periph_pll(PERIPH_ID_HOST1X, CLOCK_ID_PERIPH,
  328. 144 * 1000000);
  329. clock_start_periph_pll(PERIPH_ID_DISP1, CLOCK_ID_CGENERAL,
  330. 600 * 1000000);
  331. basic_init(&dc->cmd);
  332. basic_init_timer(&dc->disp);
  333. rgb_enable(&dc->com);
  334. if (config.pixel_clock)
  335. update_display_mode(&dc->disp, &config);
  336. if (setup_window(&window, &config))
  337. return -1;
  338. update_window(dc, &window);
  339. return 0;
  340. }