framebuffer-coreboot.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * framebuffer-coreboot.c
  4. *
  5. * Memory based framebuffer accessed through coreboot table.
  6. *
  7. * Copyright 2012-2013 David Herrmann <dh.herrmann@gmail.com>
  8. * Copyright 2017 Google Inc.
  9. * Copyright 2017 Samuel Holland <samuel@sholland.org>
  10. */
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_data/simplefb.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/sysfb.h>
  18. #include "coreboot_table.h"
  19. #define CB_TAG_FRAMEBUFFER 0x12
  20. static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
  21. static int framebuffer_probe(struct coreboot_device *dev)
  22. {
  23. int i;
  24. u32 length;
  25. struct lb_framebuffer *fb = &dev->framebuffer;
  26. struct platform_device *pdev;
  27. struct resource res;
  28. struct simplefb_platform_data pdata = {
  29. .width = fb->x_resolution,
  30. .height = fb->y_resolution,
  31. .stride = fb->bytes_per_line,
  32. .format = NULL,
  33. };
  34. /*
  35. * On coreboot systems, the advertised LB_TAG_FRAMEBUFFER entry
  36. * in the coreboot table should only be used if the payload did
  37. * not pass a framebuffer information to the Linux kernel.
  38. *
  39. * If the global screen_info data has been filled, the Generic
  40. * System Framebuffers (sysfb) will already register a platform
  41. * device and pass that screen_info as platform_data to a driver
  42. * that can scan-out using the system provided framebuffer.
  43. */
  44. if (sysfb_handles_screen_info())
  45. return -ENODEV;
  46. if (!fb->physical_address)
  47. return -ENODEV;
  48. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  49. if (fb->bits_per_pixel == formats[i].bits_per_pixel &&
  50. fb->red_mask_pos == formats[i].red.offset &&
  51. fb->red_mask_size == formats[i].red.length &&
  52. fb->green_mask_pos == formats[i].green.offset &&
  53. fb->green_mask_size == formats[i].green.length &&
  54. fb->blue_mask_pos == formats[i].blue.offset &&
  55. fb->blue_mask_size == formats[i].blue.length)
  56. pdata.format = formats[i].name;
  57. }
  58. if (!pdata.format)
  59. return -ENODEV;
  60. memset(&res, 0, sizeof(res));
  61. res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  62. res.name = "Coreboot Framebuffer";
  63. res.start = fb->physical_address;
  64. length = PAGE_ALIGN(fb->y_resolution * fb->bytes_per_line);
  65. res.end = res.start + length - 1;
  66. if (res.end <= res.start)
  67. return -EINVAL;
  68. pdev = platform_device_register_resndata(&dev->dev,
  69. "simple-framebuffer", 0,
  70. &res, 1, &pdata,
  71. sizeof(pdata));
  72. if (IS_ERR(pdev))
  73. pr_warn("coreboot: could not register framebuffer\n");
  74. else
  75. dev_set_drvdata(&dev->dev, pdev);
  76. return PTR_ERR_OR_ZERO(pdev);
  77. }
  78. static void framebuffer_remove(struct coreboot_device *dev)
  79. {
  80. struct platform_device *pdev = dev_get_drvdata(&dev->dev);
  81. platform_device_unregister(pdev);
  82. }
  83. static const struct coreboot_device_id framebuffer_ids[] = {
  84. { .tag = CB_TAG_FRAMEBUFFER },
  85. { /* sentinel */ }
  86. };
  87. MODULE_DEVICE_TABLE(coreboot, framebuffer_ids);
  88. static struct coreboot_driver framebuffer_driver = {
  89. .probe = framebuffer_probe,
  90. .remove = framebuffer_remove,
  91. .drv = {
  92. .name = "framebuffer",
  93. },
  94. .id_table = framebuffer_ids,
  95. };
  96. module_coreboot_driver(framebuffer_driver);
  97. MODULE_AUTHOR("Samuel Holland <samuel@sholland.org>");
  98. MODULE_DESCRIPTION("Memory based framebuffer accessed through coreboot table");
  99. MODULE_LICENSE("GPL");