videomodes.c 8.4 KB

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