tegra.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <fdtdec.h>
  23. #include <lcd.h>
  24. #include <asm/system.h>
  25. #include <asm/gpio.h>
  26. #include <asm/arch/clock.h>
  27. #include <asm/arch/funcmux.h>
  28. #include <asm/arch/pinmux.h>
  29. #include <asm/arch/pwm.h>
  30. #include <asm/arch/display.h>
  31. #include <asm/arch-tegra/timer.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. /* These are the stages we go throuh in enabling the LCD */
  34. enum stage_t {
  35. STAGE_START,
  36. STAGE_PANEL_VDD,
  37. STAGE_LVDS,
  38. STAGE_BACKLIGHT_VDD,
  39. STAGE_PWM,
  40. STAGE_BACKLIGHT_EN,
  41. STAGE_DONE,
  42. };
  43. static enum stage_t stage; /* Current stage we are at */
  44. static unsigned long timer_next; /* Time we can move onto next stage */
  45. /* Our LCD config, set up in handle_stage() */
  46. static struct fdt_panel_config config;
  47. struct fdt_disp_config *disp_config; /* Display controller config */
  48. enum {
  49. /* Maximum LCD size we support */
  50. LCD_MAX_WIDTH = 1366,
  51. LCD_MAX_HEIGHT = 768,
  52. LCD_MAX_LOG2_BPP = 4, /* 2^4 = 16 bpp */
  53. };
  54. int lcd_line_length;
  55. int lcd_color_fg;
  56. int lcd_color_bg;
  57. void *lcd_base; /* Start of framebuffer memory */
  58. void *lcd_console_address; /* Start of console buffer */
  59. short console_col;
  60. short console_row;
  61. vidinfo_t panel_info = {
  62. /* Insert a value here so that we don't end up in the BSS */
  63. .vl_col = -1,
  64. };
  65. char lcd_cursor_enabled;
  66. ushort lcd_cursor_width;
  67. ushort lcd_cursor_height;
  68. #ifndef CONFIG_OF_CONTROL
  69. #error "You must enable CONFIG_OF_CONTROL to get Tegra LCD support"
  70. #endif
  71. void lcd_cursor_size(ushort width, ushort height)
  72. {
  73. lcd_cursor_width = width;
  74. lcd_cursor_height = height;
  75. }
  76. void lcd_toggle_cursor(void)
  77. {
  78. ushort x, y;
  79. uchar *dest;
  80. ushort row;
  81. x = console_col * lcd_cursor_width;
  82. y = console_row * lcd_cursor_height;
  83. dest = (uchar *)(lcd_base + y * lcd_line_length + x * (1 << LCD_BPP) /
  84. 8);
  85. for (row = 0; row < lcd_cursor_height; ++row, dest += lcd_line_length) {
  86. ushort *d = (ushort *)dest;
  87. ushort color;
  88. int i;
  89. for (i = 0; i < lcd_cursor_width; ++i) {
  90. color = *d;
  91. color ^= lcd_color_fg;
  92. *d = color;
  93. ++d;
  94. }
  95. }
  96. }
  97. void lcd_cursor_on(void)
  98. {
  99. lcd_cursor_enabled = 1;
  100. lcd_toggle_cursor();
  101. }
  102. void lcd_cursor_off(void)
  103. {
  104. lcd_cursor_enabled = 0;
  105. lcd_toggle_cursor();
  106. }
  107. char lcd_is_cursor_enabled(void)
  108. {
  109. return lcd_cursor_enabled;
  110. }
  111. static void update_panel_size(struct fdt_disp_config *config)
  112. {
  113. panel_info.vl_col = config->width;
  114. panel_info.vl_row = config->height;
  115. panel_info.vl_bpix = config->log2_bpp;
  116. }
  117. /*
  118. * Main init function called by lcd driver.
  119. * Inits and then prints test pattern if required.
  120. */
  121. void lcd_ctrl_init(void *lcdbase)
  122. {
  123. int line_length, size;
  124. int type = DCACHE_OFF;
  125. assert(disp_config);
  126. lcd_base = (void *)disp_config->frame_buffer;
  127. /* Make sure that we can acommodate the selected LCD */
  128. assert(disp_config->width <= LCD_MAX_WIDTH);
  129. assert(disp_config->height <= LCD_MAX_HEIGHT);
  130. assert(disp_config->log2_bpp <= LCD_MAX_LOG2_BPP);
  131. if (disp_config->width <= LCD_MAX_WIDTH
  132. && disp_config->height <= LCD_MAX_HEIGHT
  133. && disp_config->log2_bpp <= LCD_MAX_LOG2_BPP)
  134. update_panel_size(disp_config);
  135. size = lcd_get_size(&line_length);
  136. /* Set up the LCD caching as requested */
  137. if (config.cache_type & FDT_LCD_CACHE_WRITE_THROUGH)
  138. type = DCACHE_WRITETHROUGH;
  139. else if (config.cache_type & FDT_LCD_CACHE_WRITE_BACK)
  140. type = DCACHE_WRITEBACK;
  141. mmu_set_region_dcache_behaviour(disp_config->frame_buffer, size, type);
  142. /* Enable flushing after LCD writes if requested */
  143. lcd_set_flush_dcache(config.cache_type & FDT_LCD_CACHE_FLUSH);
  144. debug("LCD frame buffer at %p\n", lcd_base);
  145. }
  146. ulong calc_fbsize(void)
  147. {
  148. return (panel_info.vl_col * panel_info.vl_row *
  149. NBITS(panel_info.vl_bpix)) / 8;
  150. }
  151. void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
  152. {
  153. }
  154. void tegra_lcd_early_init(const void *blob)
  155. {
  156. /*
  157. * Go with the maximum size for now. We will fix this up after
  158. * relocation. These values are only used for memory alocation.
  159. */
  160. panel_info.vl_col = LCD_MAX_WIDTH;
  161. panel_info.vl_row = LCD_MAX_HEIGHT;
  162. panel_info.vl_bpix = LCD_MAX_LOG2_BPP;
  163. }
  164. /**
  165. * Decode the panel information from the fdt.
  166. *
  167. * @param blob fdt blob
  168. * @param config structure to store fdt config into
  169. * @return 0 if ok, -ve on error
  170. */
  171. static int fdt_decode_lcd(const void *blob, struct fdt_panel_config *config)
  172. {
  173. int display_node;
  174. disp_config = tegra_display_get_config();
  175. if (!disp_config) {
  176. debug("%s: Display controller is not configured\n", __func__);
  177. return -1;
  178. }
  179. display_node = disp_config->panel_node;
  180. if (display_node < 0) {
  181. debug("%s: No panel configuration available\n", __func__);
  182. return -1;
  183. }
  184. config->pwm_channel = pwm_request(blob, display_node, "nvidia,pwm");
  185. if (config->pwm_channel < 0) {
  186. debug("%s: Unable to request PWM channel\n", __func__);
  187. return -1;
  188. }
  189. config->cache_type = fdtdec_get_int(blob, display_node,
  190. "nvidia,cache-type",
  191. FDT_LCD_CACHE_WRITE_BACK_FLUSH);
  192. /* These GPIOs are all optional */
  193. fdtdec_decode_gpio(blob, display_node, "nvidia,backlight-enable-gpios",
  194. &config->backlight_en);
  195. fdtdec_decode_gpio(blob, display_node, "nvidia,lvds-shutdown-gpios",
  196. &config->lvds_shutdown);
  197. fdtdec_decode_gpio(blob, display_node, "nvidia,backlight-vdd-gpios",
  198. &config->backlight_vdd);
  199. fdtdec_decode_gpio(blob, display_node, "nvidia,panel-vdd-gpios",
  200. &config->panel_vdd);
  201. return fdtdec_get_int_array(blob, display_node, "nvidia,panel-timings",
  202. config->panel_timings, FDT_LCD_TIMINGS);
  203. }
  204. /**
  205. * Handle the next stage of device init
  206. */
  207. static int handle_stage(const void *blob)
  208. {
  209. debug("%s: stage %d\n", __func__, stage);
  210. /* do the things for this stage */
  211. switch (stage) {
  212. case STAGE_START:
  213. /* Initialize the Tegra display controller */
  214. if (tegra_display_probe(gd->fdt_blob, (void *)gd->fb_base)) {
  215. printf("%s: Failed to probe display driver\n",
  216. __func__);
  217. return -1;
  218. }
  219. /* get panel details */
  220. if (fdt_decode_lcd(blob, &config)) {
  221. printf("No valid LCD information in device tree\n");
  222. return -1;
  223. }
  224. /*
  225. * It is possible that the FDT has requested that the LCD be
  226. * disabled. We currently don't support this. It would require
  227. * changes to U-Boot LCD subsystem to have LCD support
  228. * compiled in but not used. An easier option might be to
  229. * still have a frame buffer, but leave the backlight off and
  230. * remove all mention of lcd in the stdout environment
  231. * variable.
  232. */
  233. funcmux_select(PERIPH_ID_DISP1, FUNCMUX_DEFAULT);
  234. fdtdec_setup_gpio(&config.panel_vdd);
  235. fdtdec_setup_gpio(&config.lvds_shutdown);
  236. fdtdec_setup_gpio(&config.backlight_vdd);
  237. fdtdec_setup_gpio(&config.backlight_en);
  238. /*
  239. * TODO: If fdt includes output flag we can omit this code
  240. * since fdtdec_setup_gpio will do it for us.
  241. */
  242. if (fdt_gpio_isvalid(&config.panel_vdd))
  243. gpio_direction_output(config.panel_vdd.gpio, 0);
  244. if (fdt_gpio_isvalid(&config.lvds_shutdown))
  245. gpio_direction_output(config.lvds_shutdown.gpio, 0);
  246. if (fdt_gpio_isvalid(&config.backlight_vdd))
  247. gpio_direction_output(config.backlight_vdd.gpio, 0);
  248. if (fdt_gpio_isvalid(&config.backlight_en))
  249. gpio_direction_output(config.backlight_en.gpio, 0);
  250. break;
  251. case STAGE_PANEL_VDD:
  252. if (fdt_gpio_isvalid(&config.panel_vdd))
  253. gpio_direction_output(config.panel_vdd.gpio, 1);
  254. break;
  255. case STAGE_LVDS:
  256. if (fdt_gpio_isvalid(&config.lvds_shutdown))
  257. gpio_set_value(config.lvds_shutdown.gpio, 1);
  258. break;
  259. case STAGE_BACKLIGHT_VDD:
  260. if (fdt_gpio_isvalid(&config.backlight_vdd))
  261. gpio_set_value(config.backlight_vdd.gpio, 1);
  262. break;
  263. case STAGE_PWM:
  264. /* Enable PWM at 15/16 high, 32768 Hz with divider 1 */
  265. pinmux_set_func(PINGRP_GPU, PMUX_FUNC_PWM);
  266. pinmux_tristate_disable(PINGRP_GPU);
  267. pwm_enable(config.pwm_channel, 32768, 0xdf, 1);
  268. break;
  269. case STAGE_BACKLIGHT_EN:
  270. if (fdt_gpio_isvalid(&config.backlight_en))
  271. gpio_set_value(config.backlight_en.gpio, 1);
  272. break;
  273. case STAGE_DONE:
  274. break;
  275. }
  276. /* set up timer for next stage */
  277. timer_next = timer_get_us();
  278. if (stage < FDT_LCD_TIMINGS)
  279. timer_next += config.panel_timings[stage] * 1000;
  280. /* move to next stage */
  281. stage++;
  282. return 0;
  283. }
  284. int tegra_lcd_check_next_stage(const void *blob, int wait)
  285. {
  286. if (stage == STAGE_DONE)
  287. return 0;
  288. do {
  289. /* wait if we need to */
  290. debug("%s: stage %d\n", __func__, stage);
  291. if (stage != STAGE_START) {
  292. int delay = timer_next - timer_get_us();
  293. if (delay > 0) {
  294. if (wait)
  295. udelay(delay);
  296. else
  297. return 0;
  298. }
  299. }
  300. if (handle_stage(blob))
  301. return -1;
  302. } while (wait && stage != STAGE_DONE);
  303. if (stage == STAGE_DONE)
  304. debug("%s: LCD init complete\n", __func__);
  305. return 0;
  306. }
  307. void lcd_enable(void)
  308. {
  309. /*
  310. * Backlight and power init will be done separately in
  311. * tegra_lcd_check_next_stage(), which should be called in
  312. * board_late_init().
  313. *
  314. * U-Boot code supports only colour depth, selected at compile time.
  315. * The device tree setting should match this. Otherwise the display
  316. * will not look right, and U-Boot may crash.
  317. */
  318. if (disp_config->log2_bpp != LCD_BPP) {
  319. printf("%s: Error: LCD depth configured in FDT (%d = %dbpp)"
  320. " must match setting of LCD_BPP (%d)\n", __func__,
  321. disp_config->log2_bpp, disp_config->bpp, LCD_BPP);
  322. }
  323. }