intel_bios.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright (c) 2006 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Authors:
  18. * Eric Anholt <eric@anholt.net>
  19. *
  20. */
  21. #include <drm/drmP.h>
  22. #include <drm/drm.h>
  23. #include "gma_drm.h"
  24. #include "psb_drv.h"
  25. #include "psb_intel_drv.h"
  26. #include "psb_intel_reg.h"
  27. #include "intel_bios.h"
  28. #define SLAVE_ADDR1 0x70
  29. #define SLAVE_ADDR2 0x72
  30. static void *find_section(struct bdb_header *bdb, int section_id)
  31. {
  32. u8 *base = (u8 *)bdb;
  33. int index = 0;
  34. u16 total, current_size;
  35. u8 current_id;
  36. /* skip to first section */
  37. index += bdb->header_size;
  38. total = bdb->bdb_size;
  39. /* walk the sections looking for section_id */
  40. while (index < total) {
  41. current_id = *(base + index);
  42. index++;
  43. current_size = *((u16 *)(base + index));
  44. index += 2;
  45. if (current_id == section_id)
  46. return base + index;
  47. index += current_size;
  48. }
  49. return NULL;
  50. }
  51. static u16
  52. get_blocksize(void *p)
  53. {
  54. u16 *block_ptr, block_size;
  55. block_ptr = (u16 *)((char *)p - 2);
  56. block_size = *block_ptr;
  57. return block_size;
  58. }
  59. static void fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
  60. struct lvds_dvo_timing *dvo_timing)
  61. {
  62. panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
  63. dvo_timing->hactive_lo;
  64. panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
  65. ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
  66. panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
  67. dvo_timing->hsync_pulse_width;
  68. panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
  69. ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
  70. panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
  71. dvo_timing->vactive_lo;
  72. panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
  73. dvo_timing->vsync_off;
  74. panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
  75. dvo_timing->vsync_pulse_width;
  76. panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
  77. ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
  78. panel_fixed_mode->clock = dvo_timing->clock * 10;
  79. panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
  80. if (dvo_timing->hsync_positive)
  81. panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
  82. else
  83. panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
  84. if (dvo_timing->vsync_positive)
  85. panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
  86. else
  87. panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
  88. /* Some VBTs have bogus h/vtotal values */
  89. if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
  90. panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
  91. if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
  92. panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
  93. drm_mode_set_name(panel_fixed_mode);
  94. }
  95. static void parse_backlight_data(struct drm_psb_private *dev_priv,
  96. struct bdb_header *bdb)
  97. {
  98. struct bdb_lvds_backlight *vbt_lvds_bl = NULL;
  99. struct bdb_lvds_backlight *lvds_bl;
  100. u8 p_type = 0;
  101. void *bl_start = NULL;
  102. struct bdb_lvds_options *lvds_opts
  103. = find_section(bdb, BDB_LVDS_OPTIONS);
  104. dev_priv->lvds_bl = NULL;
  105. if (lvds_opts)
  106. p_type = lvds_opts->panel_type;
  107. else
  108. return;
  109. bl_start = find_section(bdb, BDB_LVDS_BACKLIGHT);
  110. vbt_lvds_bl = (struct bdb_lvds_backlight *)(bl_start + 1) + p_type;
  111. lvds_bl = kzalloc(sizeof(*vbt_lvds_bl), GFP_KERNEL);
  112. if (!lvds_bl) {
  113. dev_err(dev_priv->dev->dev, "out of memory for backlight data\n");
  114. return;
  115. }
  116. memcpy(lvds_bl, vbt_lvds_bl, sizeof(*vbt_lvds_bl));
  117. dev_priv->lvds_bl = lvds_bl;
  118. }
  119. /* Try to find integrated panel data */
  120. static void parse_lfp_panel_data(struct drm_psb_private *dev_priv,
  121. struct bdb_header *bdb)
  122. {
  123. struct bdb_lvds_options *lvds_options;
  124. struct bdb_lvds_lfp_data *lvds_lfp_data;
  125. struct bdb_lvds_lfp_data_entry *entry;
  126. struct lvds_dvo_timing *dvo_timing;
  127. struct drm_display_mode *panel_fixed_mode;
  128. /* Defaults if we can't find VBT info */
  129. dev_priv->lvds_dither = 0;
  130. dev_priv->lvds_vbt = 0;
  131. lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
  132. if (!lvds_options)
  133. return;
  134. dev_priv->lvds_dither = lvds_options->pixel_dither;
  135. if (lvds_options->panel_type == 0xff)
  136. return;
  137. lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
  138. if (!lvds_lfp_data)
  139. return;
  140. entry = &lvds_lfp_data->data[lvds_options->panel_type];
  141. dvo_timing = &entry->dvo_timing;
  142. panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode),
  143. GFP_KERNEL);
  144. if (panel_fixed_mode == NULL) {
  145. dev_err(dev_priv->dev->dev, "out of memory for fixed panel mode\n");
  146. return;
  147. }
  148. dev_priv->lvds_vbt = 1;
  149. fill_detail_timing_data(panel_fixed_mode, dvo_timing);
  150. if (panel_fixed_mode->htotal > 0 && panel_fixed_mode->vtotal > 0) {
  151. dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
  152. drm_mode_debug_printmodeline(panel_fixed_mode);
  153. } else {
  154. dev_dbg(dev_priv->dev->dev, "ignoring invalid LVDS VBT\n");
  155. dev_priv->lvds_vbt = 0;
  156. kfree(panel_fixed_mode);
  157. }
  158. return;
  159. }
  160. /* Try to find sdvo panel data */
  161. static void parse_sdvo_panel_data(struct drm_psb_private *dev_priv,
  162. struct bdb_header *bdb)
  163. {
  164. struct bdb_sdvo_lvds_options *sdvo_lvds_options;
  165. struct lvds_dvo_timing *dvo_timing;
  166. struct drm_display_mode *panel_fixed_mode;
  167. dev_priv->sdvo_lvds_vbt_mode = NULL;
  168. sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
  169. if (!sdvo_lvds_options)
  170. return;
  171. dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
  172. if (!dvo_timing)
  173. return;
  174. panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
  175. if (!panel_fixed_mode)
  176. return;
  177. fill_detail_timing_data(panel_fixed_mode,
  178. dvo_timing + sdvo_lvds_options->panel_type);
  179. dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
  180. return;
  181. }
  182. static void parse_general_features(struct drm_psb_private *dev_priv,
  183. struct bdb_header *bdb)
  184. {
  185. struct bdb_general_features *general;
  186. /* Set sensible defaults in case we can't find the general block */
  187. dev_priv->int_tv_support = 1;
  188. dev_priv->int_crt_support = 1;
  189. general = find_section(bdb, BDB_GENERAL_FEATURES);
  190. if (general) {
  191. dev_priv->int_tv_support = general->int_tv_support;
  192. dev_priv->int_crt_support = general->int_crt_support;
  193. dev_priv->lvds_use_ssc = general->enable_ssc;
  194. if (dev_priv->lvds_use_ssc) {
  195. dev_priv->lvds_ssc_freq
  196. = general->ssc_freq ? 100 : 96;
  197. }
  198. }
  199. }
  200. static void
  201. parse_sdvo_device_mapping(struct drm_psb_private *dev_priv,
  202. struct bdb_header *bdb)
  203. {
  204. struct sdvo_device_mapping *p_mapping;
  205. struct bdb_general_definitions *p_defs;
  206. struct child_device_config *p_child;
  207. int i, child_device_num, count;
  208. u16 block_size;
  209. p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
  210. if (!p_defs) {
  211. DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
  212. return;
  213. }
  214. /* judge whether the size of child device meets the requirements.
  215. * If the child device size obtained from general definition block
  216. * is different with sizeof(struct child_device_config), skip the
  217. * parsing of sdvo device info
  218. */
  219. if (p_defs->child_dev_size != sizeof(*p_child)) {
  220. /* different child dev size . Ignore it */
  221. DRM_DEBUG_KMS("different child size is found. Invalid.\n");
  222. return;
  223. }
  224. /* get the block size of general definitions */
  225. block_size = get_blocksize(p_defs);
  226. /* get the number of child device */
  227. child_device_num = (block_size - sizeof(*p_defs)) /
  228. sizeof(*p_child);
  229. count = 0;
  230. for (i = 0; i < child_device_num; i++) {
  231. p_child = &(p_defs->devices[i]);
  232. if (!p_child->device_type) {
  233. /* skip the device block if device type is invalid */
  234. continue;
  235. }
  236. if (p_child->slave_addr != SLAVE_ADDR1 &&
  237. p_child->slave_addr != SLAVE_ADDR2) {
  238. /*
  239. * If the slave address is neither 0x70 nor 0x72,
  240. * it is not a SDVO device. Skip it.
  241. */
  242. continue;
  243. }
  244. if (p_child->dvo_port != DEVICE_PORT_DVOB &&
  245. p_child->dvo_port != DEVICE_PORT_DVOC) {
  246. /* skip the incorrect SDVO port */
  247. DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
  248. continue;
  249. }
  250. DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
  251. " %s port\n",
  252. p_child->slave_addr,
  253. (p_child->dvo_port == DEVICE_PORT_DVOB) ?
  254. "SDVOB" : "SDVOC");
  255. p_mapping = &(dev_priv->sdvo_mappings[p_child->dvo_port - 1]);
  256. if (!p_mapping->initialized) {
  257. p_mapping->dvo_port = p_child->dvo_port;
  258. p_mapping->slave_addr = p_child->slave_addr;
  259. p_mapping->dvo_wiring = p_child->dvo_wiring;
  260. p_mapping->ddc_pin = p_child->ddc_pin;
  261. p_mapping->i2c_pin = p_child->i2c_pin;
  262. p_mapping->initialized = 1;
  263. DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
  264. p_mapping->dvo_port,
  265. p_mapping->slave_addr,
  266. p_mapping->dvo_wiring,
  267. p_mapping->ddc_pin,
  268. p_mapping->i2c_pin);
  269. } else {
  270. DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
  271. "two SDVO device.\n");
  272. }
  273. if (p_child->slave2_addr) {
  274. /* Maybe this is a SDVO device with multiple inputs */
  275. /* And the mapping info is not added */
  276. DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
  277. " is a SDVO device with multiple inputs.\n");
  278. }
  279. count++;
  280. }
  281. if (!count) {
  282. /* No SDVO device info is found */
  283. DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
  284. }
  285. return;
  286. }
  287. static void
  288. parse_driver_features(struct drm_psb_private *dev_priv,
  289. struct bdb_header *bdb)
  290. {
  291. struct bdb_driver_features *driver;
  292. driver = find_section(bdb, BDB_DRIVER_FEATURES);
  293. if (!driver)
  294. return;
  295. /* This bit means to use 96Mhz for DPLL_A or not */
  296. if (driver->primary_lfp_id)
  297. dev_priv->dplla_96mhz = true;
  298. else
  299. dev_priv->dplla_96mhz = false;
  300. }
  301. static void
  302. parse_device_mapping(struct drm_psb_private *dev_priv,
  303. struct bdb_header *bdb)
  304. {
  305. struct bdb_general_definitions *p_defs;
  306. struct child_device_config *p_child, *child_dev_ptr;
  307. int i, child_device_num, count;
  308. u16 block_size;
  309. p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
  310. if (!p_defs) {
  311. DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
  312. return;
  313. }
  314. /* judge whether the size of child device meets the requirements.
  315. * If the child device size obtained from general definition block
  316. * is different with sizeof(struct child_device_config), skip the
  317. * parsing of sdvo device info
  318. */
  319. if (p_defs->child_dev_size != sizeof(*p_child)) {
  320. /* different child dev size . Ignore it */
  321. DRM_DEBUG_KMS("different child size is found. Invalid.\n");
  322. return;
  323. }
  324. /* get the block size of general definitions */
  325. block_size = get_blocksize(p_defs);
  326. /* get the number of child device */
  327. child_device_num = (block_size - sizeof(*p_defs)) /
  328. sizeof(*p_child);
  329. count = 0;
  330. /* get the number of child devices that are present */
  331. for (i = 0; i < child_device_num; i++) {
  332. p_child = &(p_defs->devices[i]);
  333. if (!p_child->device_type) {
  334. /* skip the device block if device type is invalid */
  335. continue;
  336. }
  337. count++;
  338. }
  339. if (!count) {
  340. DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
  341. return;
  342. }
  343. dev_priv->child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
  344. if (!dev_priv->child_dev) {
  345. DRM_DEBUG_KMS("No memory space for child devices\n");
  346. return;
  347. }
  348. dev_priv->child_dev_num = count;
  349. count = 0;
  350. for (i = 0; i < child_device_num; i++) {
  351. p_child = &(p_defs->devices[i]);
  352. if (!p_child->device_type) {
  353. /* skip the device block if device type is invalid */
  354. continue;
  355. }
  356. child_dev_ptr = dev_priv->child_dev + count;
  357. count++;
  358. memcpy((void *)child_dev_ptr, (void *)p_child,
  359. sizeof(*p_child));
  360. }
  361. return;
  362. }
  363. /**
  364. * psb_intel_init_bios - initialize VBIOS settings & find VBT
  365. * @dev: DRM device
  366. *
  367. * Loads the Video BIOS and checks that the VBT exists. Sets scratch registers
  368. * to appropriate values.
  369. *
  370. * VBT existence is a sanity check that is relied on by other i830_bios.c code.
  371. * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
  372. * feed an updated VBT back through that, compared to what we'll fetch using
  373. * this method of groping around in the BIOS data.
  374. *
  375. * Returns 0 on success, nonzero on failure.
  376. */
  377. bool psb_intel_init_bios(struct drm_device *dev)
  378. {
  379. struct drm_psb_private *dev_priv = dev->dev_private;
  380. struct pci_dev *pdev = dev->pdev;
  381. struct vbt_header *vbt = NULL;
  382. struct bdb_header *bdb = NULL;
  383. u8 __iomem *bios = NULL;
  384. size_t size;
  385. int i;
  386. /* XXX Should this validation be moved to intel_opregion.c? */
  387. if (dev_priv->opregion.vbt) {
  388. struct vbt_header *vbt = dev_priv->opregion.vbt;
  389. if (memcmp(vbt->signature, "$VBT", 4) == 0) {
  390. DRM_DEBUG_KMS("Using VBT from OpRegion: %20s\n",
  391. vbt->signature);
  392. bdb = (struct bdb_header *)((char *)vbt + vbt->bdb_offset);
  393. } else
  394. dev_priv->opregion.vbt = NULL;
  395. }
  396. if (bdb == NULL) {
  397. bios = pci_map_rom(pdev, &size);
  398. if (!bios)
  399. return -1;
  400. /* Scour memory looking for the VBT signature */
  401. for (i = 0; i + 4 < size; i++) {
  402. if (!memcmp(bios + i, "$VBT", 4)) {
  403. vbt = (struct vbt_header *)(bios + i);
  404. break;
  405. }
  406. }
  407. if (!vbt) {
  408. dev_err(dev->dev, "VBT signature missing\n");
  409. pci_unmap_rom(pdev, bios);
  410. return -1;
  411. }
  412. bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
  413. }
  414. /* Grab useful general dxefinitions */
  415. parse_general_features(dev_priv, bdb);
  416. parse_driver_features(dev_priv, bdb);
  417. parse_lfp_panel_data(dev_priv, bdb);
  418. parse_sdvo_panel_data(dev_priv, bdb);
  419. parse_sdvo_device_mapping(dev_priv, bdb);
  420. parse_device_mapping(dev_priv, bdb);
  421. parse_backlight_data(dev_priv, bdb);
  422. if (bios)
  423. pci_unmap_rom(pdev, bios);
  424. return 0;
  425. }
  426. /**
  427. * Destroy and free VBT data
  428. */
  429. void psb_intel_destroy_bios(struct drm_device *dev)
  430. {
  431. struct drm_psb_private *dev_priv = dev->dev_private;
  432. kfree(dev_priv->sdvo_lvds_vbt_mode);
  433. kfree(dev_priv->lfp_lvds_vbt_mode);
  434. kfree(dev_priv->lvds_bl);
  435. }