videomodes.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * (C) Copyright 2004
  3. * Pierre Aubert, Staubli Faverges , <p.aubert@staubli.com>
  4. * Copyright 2011 Freescale Semiconductor, Inc.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /************************************************************************
  9. Get Parameters for the video mode:
  10. The default video mode can be defined in CONFIG_SYS_DEFAULT_VIDEO_MODE.
  11. If undefined, default video mode is set to 0x301
  12. Parameters can be set via the variable "videomode" in the environment.
  13. 2 diferent ways are possible:
  14. "videomode=301" - 301 is a hexadecimal number describing the VESA
  15. mode. Following modes are implemented:
  16. Colors 640x480 800x600 1024x768 1152x864 1280x1024
  17. --------+---------------------------------------------
  18. 8 bits | 0x301 0x303 0x305 0x161 0x307
  19. 15 bits | 0x310 0x313 0x316 0x162 0x319
  20. 16 bits | 0x311 0x314 0x317 0x163 0x31A
  21. 24 bits | 0x312 0x315 0x318 ? 0x31B
  22. --------+---------------------------------------------
  23. "videomode=bootargs"
  24. - the parameters are parsed from the bootargs.
  25. The format is "NAME:VALUE,NAME:VALUE" etc.
  26. Ex.:
  27. "bootargs=video=ctfb:x:800,y:600,depth:16,pclk:25000"
  28. Parameters not included in the list will be taken from
  29. the default mode, which is one of the following:
  30. mode:0 640x480x24
  31. mode:1 800x600x16
  32. mode:2 1024x768x8
  33. mode:3 960x720x24
  34. mode:4 1152x864x16
  35. mode:5 1280x1024x8
  36. if "mode" is not provided within the parameter list,
  37. mode:0 is assumed.
  38. Following parameters are supported:
  39. x xres = visible resolution horizontal
  40. y yres = visible resolution vertical
  41. pclk pixelclocks in pico sec
  42. le left_marging time from sync to picture in pixelclocks
  43. ri right_marging time from picture to sync in pixelclocks
  44. up upper_margin time from sync to picture
  45. lo lower_margin
  46. hs hsync_len length of horizontal sync
  47. vs vsync_len length of vertical sync
  48. sync see FB_SYNC_*
  49. vmode see FB_VMODE_*
  50. depth Color depth in bits per pixel
  51. All other parameters in the variable bootargs are ignored.
  52. It is also possible to set the parameters direct in the
  53. variable "videomode", or in another variable i.e.
  54. "myvideo" and setting the variable "videomode=myvideo"..
  55. ****************************************************************************/
  56. #include <common.h>
  57. #include <linux/ctype.h>
  58. #include "videomodes.h"
  59. const struct ctfb_vesa_modes vesa_modes[VESA_MODES_COUNT] = {
  60. {0x301, RES_MODE_640x480, 8},
  61. {0x310, RES_MODE_640x480, 15},
  62. {0x311, RES_MODE_640x480, 16},
  63. {0x312, RES_MODE_640x480, 24},
  64. {0x303, RES_MODE_800x600, 8},
  65. {0x313, RES_MODE_800x600, 15},
  66. {0x314, RES_MODE_800x600, 16},
  67. {0x315, RES_MODE_800x600, 24},
  68. {0x305, RES_MODE_1024x768, 8},
  69. {0x316, RES_MODE_1024x768, 15},
  70. {0x317, RES_MODE_1024x768, 16},
  71. {0x318, RES_MODE_1024x768, 24},
  72. {0x161, RES_MODE_1152x864, 8},
  73. {0x162, RES_MODE_1152x864, 15},
  74. {0x163, RES_MODE_1152x864, 16},
  75. {0x307, RES_MODE_1280x1024, 8},
  76. {0x319, RES_MODE_1280x1024, 15},
  77. {0x31A, RES_MODE_1280x1024, 16},
  78. {0x31B, RES_MODE_1280x1024, 24},
  79. };
  80. const struct ctfb_res_modes res_mode_init[RES_MODES_COUNT] = {
  81. /* x y pixclk le ri up lo hs vs s vmode */
  82. {640, 480, 39721, 40, 24, 32, 11, 96, 2, 0, FB_VMODE_NONINTERLACED},
  83. {800, 600, 27778, 64, 24, 22, 1, 72, 2, 0, FB_VMODE_NONINTERLACED},
  84. {1024, 768, 15384, 168, 8, 29, 3, 144, 4, 0, FB_VMODE_NONINTERLACED},
  85. {960, 720, 13100, 160, 40, 32, 8, 80, 4, 0, FB_VMODE_NONINTERLACED},
  86. {1152, 864, 12004, 200, 64, 32, 16, 80, 4, 0, FB_VMODE_NONINTERLACED},
  87. {1280, 1024, 9090, 200, 48, 26, 1, 184, 3, 0, FB_VMODE_NONINTERLACED},
  88. };
  89. /************************************************************************
  90. * Get Parameters for the video mode:
  91. */
  92. /*********************************************************************
  93. * returns the length to the next seperator
  94. */
  95. static int
  96. video_get_param_len (char *start, char sep)
  97. {
  98. int i = 0;
  99. while ((*start != 0) && (*start != sep)) {
  100. start++;
  101. i++;
  102. }
  103. return i;
  104. }
  105. static int
  106. video_search_param (char *start, char *param)
  107. {
  108. int len, totallen, i;
  109. char *p = start;
  110. len = strlen (param);
  111. totallen = len + strlen (start);
  112. for (i = 0; i < totallen; i++) {
  113. if (strncmp (p++, param, len) == 0)
  114. return (i);
  115. }
  116. return -1;
  117. }
  118. /***************************************************************
  119. * Get parameter via the environment as it is done for the
  120. * linux kernel i.e:
  121. * video=ctfb:x:800,xv:1280,y:600,yv:1024,depth:16,mode:0,pclk:25000,
  122. * le:56,ri:48,up:26,lo:5,hs:152,vs:2,sync:0,vmode:0,accel:0
  123. *
  124. * penv is a pointer to the environment, containing the string, or the name of
  125. * another environment variable. It could even be the term "bootargs"
  126. */
  127. #define GET_OPTION(name,var) \
  128. if(strncmp(p,name,strlen(name))==0) { \
  129. val_s=p+strlen(name); \
  130. var=simple_strtoul(val_s, NULL, 10); \
  131. }
  132. int video_get_params (struct ctfb_res_modes *pPar, char *penv)
  133. {
  134. char *p, *s, *val_s;
  135. int i = 0;
  136. int bpp;
  137. int mode;
  138. /* first search for the environment containing the real param string */
  139. s = penv;
  140. if ((p = getenv (s)) != NULL)
  141. s = p;
  142. /*
  143. * in case of the bootargs line, we have to start
  144. * after "video=ctfb:"
  145. */
  146. i = video_search_param (s, "video=ctfb:");
  147. if (i >= 0) {
  148. s += i;
  149. s += strlen ("video=ctfb:");
  150. }
  151. /* search for mode as a default value */
  152. p = s;
  153. mode = 0; /* default */
  154. while ((i = video_get_param_len (p, ',')) != 0) {
  155. GET_OPTION ("mode:", mode)
  156. p += i;
  157. if (*p != 0)
  158. p++; /* skip ',' */
  159. }
  160. if (mode >= RES_MODES_COUNT)
  161. mode = 0;
  162. *pPar = res_mode_init[mode]; /* copy default values */
  163. bpp = 24 - ((mode % 3) * 8);
  164. p = s; /* restart */
  165. while ((i = video_get_param_len (p, ',')) != 0) {
  166. GET_OPTION ("x:", pPar->xres)
  167. GET_OPTION ("y:", pPar->yres)
  168. GET_OPTION ("le:", pPar->left_margin)
  169. GET_OPTION ("ri:", pPar->right_margin)
  170. GET_OPTION ("up:", pPar->upper_margin)
  171. GET_OPTION ("lo:", pPar->lower_margin)
  172. GET_OPTION ("hs:", pPar->hsync_len)
  173. GET_OPTION ("vs:", pPar->vsync_len)
  174. GET_OPTION ("sync:", pPar->sync)
  175. GET_OPTION ("vmode:", pPar->vmode)
  176. GET_OPTION ("pclk:", pPar->pixclock)
  177. GET_OPTION ("depth:", bpp)
  178. p += i;
  179. if (*p != 0)
  180. p++; /* skip ',' */
  181. }
  182. return bpp;
  183. }
  184. /*
  185. * Parse the 'video-mode' environment variable
  186. *
  187. * Example: "video-mode=fslfb:1280x1024-32@60,monitor=dvi". See
  188. * doc/README.video for more information on how to set the variable.
  189. *
  190. * @xres: returned value of X-resolution
  191. * @yres: returned value of Y-resolution
  192. * @depth: returned value of color depth
  193. * @freq: returned value of monitor frequency
  194. * @options: pointer to any remaining options, or NULL
  195. *
  196. * Returns 1 if valid values were found, 0 otherwise
  197. */
  198. int video_get_video_mode(unsigned int *xres, unsigned int *yres,
  199. unsigned int *depth, unsigned int *freq, const char **options)
  200. {
  201. char *p = getenv("video-mode");
  202. if (!p)
  203. return 0;
  204. /* Skip over the driver name, which we don't care about. */
  205. p = strchr(p, ':');
  206. if (!p)
  207. return 0;
  208. /* Get the X-resolution*/
  209. while (*p && !isdigit(*p))
  210. p++;
  211. *xres = simple_strtoul(p, &p, 10);
  212. if (!*xres)
  213. return 0;
  214. /* Get the Y-resolution */
  215. while (*p && !isdigit(*p))
  216. p++;
  217. *yres = simple_strtoul(p, &p, 10);
  218. if (!*yres)
  219. return 0;
  220. /* Get the depth */
  221. while (*p && !isdigit(*p))
  222. p++;
  223. *depth = simple_strtoul(p, &p, 10);
  224. if (!*depth)
  225. return 0;
  226. /* Get the frequency */
  227. while (*p && !isdigit(*p))
  228. p++;
  229. *freq = simple_strtoul(p, &p, 10);
  230. if (!*freq)
  231. return 0;
  232. /* Find the extra options, if any */
  233. p = strchr(p, ',');
  234. *options = p ? p + 1 : NULL;
  235. return 1;
  236. }