LcdGraphicsOutputDxe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /** @file
  2. Copyright (c) 2011-2014, ARM Ltd. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "LcdGraphicsOutputDxe.h"
  6. BOOLEAN mDisplayInitialized = FALSE;
  7. LCD_MODE LcdModes[] = {
  8. {
  9. 0, 640, 480,
  10. 9, 4,
  11. 96, 16, 48,
  12. 2, 10, 33
  13. },
  14. {
  15. 1, 800, 600,
  16. 11, 2,
  17. 120, 56, 64,
  18. 5, 37, 22
  19. },
  20. {
  21. 2, 1024, 768,
  22. 6, 2,
  23. 96, 16, 48,
  24. 2, 10, 33
  25. },
  26. };
  27. LCD_INSTANCE mLcdTemplate = {
  28. LCD_INSTANCE_SIGNATURE,
  29. NULL, // Handle
  30. { // ModeInfo
  31. 0, // Version
  32. 0, // HorizontalResolution
  33. 0, // VerticalResolution
  34. PixelBltOnly, // PixelFormat
  35. {
  36. 0xF800, //RedMask;
  37. 0x7E0, //GreenMask;
  38. 0x1F, //BlueMask;
  39. 0x0//ReservedMask
  40. }, // PixelInformation
  41. 0, // PixelsPerScanLine
  42. },
  43. { // Mode
  44. 3, // MaxMode;
  45. 0, // Mode;
  46. NULL, // Info;
  47. 0, // SizeOfInfo;
  48. 0, // FrameBufferBase;
  49. 0 // FrameBufferSize;
  50. },
  51. { // Gop
  52. LcdGraphicsQueryMode, // QueryMode
  53. LcdGraphicsSetMode, // SetMode
  54. LcdGraphicsBlt, // Blt
  55. NULL // *Mode
  56. },
  57. { // DevicePath
  58. {
  59. {
  60. HARDWARE_DEVICE_PATH, HW_VENDOR_DP,
  61. { (UINT8) (sizeof(VENDOR_DEVICE_PATH)), (UINT8) ((sizeof(VENDOR_DEVICE_PATH)) >> 8) },
  62. },
  63. // Hardware Device Path for Lcd
  64. EFI_CALLER_ID_GUID // Use the driver's GUID
  65. },
  66. {
  67. END_DEVICE_PATH_TYPE,
  68. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  69. { sizeof(EFI_DEVICE_PATH_PROTOCOL), 0}
  70. }
  71. }
  72. };
  73. EFI_STATUS
  74. LcdInstanceContructor (
  75. OUT LCD_INSTANCE** NewInstance
  76. )
  77. {
  78. LCD_INSTANCE* Instance;
  79. Instance = AllocateCopyPool (sizeof(LCD_INSTANCE), &mLcdTemplate);
  80. if (Instance == NULL) {
  81. return EFI_OUT_OF_RESOURCES;
  82. }
  83. Instance->Gop.Mode = &Instance->Mode;
  84. Instance->Mode.Info = &Instance->ModeInfo;
  85. *NewInstance = Instance;
  86. return EFI_SUCCESS;
  87. }
  88. EFI_STATUS
  89. LcdPlatformGetVram (
  90. OUT EFI_PHYSICAL_ADDRESS* VramBaseAddress,
  91. OUT UINTN* VramSize
  92. )
  93. {
  94. EFI_STATUS Status;
  95. EFI_CPU_ARCH_PROTOCOL *Cpu;
  96. UINTN MaxSize;
  97. MaxSize = 0x500000;
  98. *VramSize = MaxSize;
  99. // Allocate VRAM from DRAM
  100. Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES((MaxSize)), VramBaseAddress);
  101. if (EFI_ERROR(Status)) {
  102. return Status;
  103. }
  104. // Ensure the Cpu architectural protocol is already installed
  105. Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&Cpu);
  106. ASSERT_EFI_ERROR(Status);
  107. // Mark the VRAM as un-cacheable. The VRAM is inside the DRAM, which is cacheable.
  108. Status = Cpu->SetMemoryAttributes (Cpu, *VramBaseAddress, *VramSize, EFI_MEMORY_UC);
  109. if (EFI_ERROR(Status)) {
  110. gBS->FreePool (VramBaseAddress);
  111. return Status;
  112. }
  113. return EFI_SUCCESS;
  114. }
  115. EFI_STATUS
  116. DssSetMode (
  117. UINT32 VramBaseAddress,
  118. UINTN ModeNumber
  119. )
  120. {
  121. // Make sure the interface clock is running
  122. MmioWrite32 (CM_ICLKEN_DSS, EN_DSS);
  123. // Stop the functional clocks
  124. MmioAnd32 (CM_FCLKEN_DSS, ~(EN_DSS1 | EN_DSS2 | EN_TV));
  125. // Program the DSS clock divisor
  126. MmioWrite32 (CM_CLKSEL_DSS, 0x1000 | (LcdModes[ModeNumber].DssDivisor));
  127. // Start the functional clocks
  128. MmioOr32 (CM_FCLKEN_DSS, (EN_DSS1 | EN_DSS2 | EN_TV));
  129. // Wait for DSS to stabilize
  130. gBS->Stall(1);
  131. // Reset the subsystem
  132. MmioWrite32(DSS_SYSCONFIG, DSS_SOFTRESET);
  133. while (!(MmioRead32 (DSS_SYSSTATUS) & DSS_RESETDONE));
  134. // Configure LCD parameters
  135. MmioWrite32 (DISPC_SIZE_LCD,
  136. ((LcdModes[ModeNumber].HorizontalResolution - 1)
  137. | ((LcdModes[ModeNumber].VerticalResolution - 1) << 16))
  138. );
  139. MmioWrite32 (DISPC_TIMING_H,
  140. ( (LcdModes[ModeNumber].HSync - 1)
  141. | ((LcdModes[ModeNumber].HFrontPorch - 1) << 8)
  142. | ((LcdModes[ModeNumber].HBackPorch - 1) << 20))
  143. );
  144. MmioWrite32 (DISPC_TIMING_V,
  145. ( (LcdModes[ModeNumber].VSync - 1)
  146. | ((LcdModes[ModeNumber].VFrontPorch - 1) << 8)
  147. | ((LcdModes[ModeNumber].VBackPorch - 1) << 20))
  148. );
  149. // Set the framebuffer to only load frames (no gamma tables)
  150. MmioAnd32 (DISPC_CONFIG, CLEARLOADMODE);
  151. MmioOr32 (DISPC_CONFIG, LOAD_FRAME_ONLY);
  152. // Divisor for the pixel clock
  153. MmioWrite32(DISPC_DIVISOR, ((1 << 16) | LcdModes[ModeNumber].DispcDivisor) );
  154. // Set up the graphics layer
  155. MmioWrite32 (DISPC_GFX_PRELD, 0x2D8);
  156. MmioWrite32 (DISPC_GFX_BA0, VramBaseAddress);
  157. MmioWrite32 (DISPC_GFX_SIZE,
  158. ((LcdModes[ModeNumber].HorizontalResolution - 1)
  159. | ((LcdModes[ModeNumber].VerticalResolution - 1) << 16))
  160. );
  161. MmioWrite32(DISPC_GFX_ATTR, (GFXENABLE | RGB16 | BURSTSIZE16));
  162. // Start it all
  163. MmioOr32 (DISPC_CONTROL, (LCDENABLE | ACTIVEMATRIX | DATALINES24 | BYPASS_MODE | LCDENABLESIGNAL));
  164. MmioOr32 (DISPC_CONTROL, GOLCD);
  165. return EFI_SUCCESS;
  166. }
  167. EFI_STATUS
  168. HwInitializeDisplay (
  169. UINTN VramBaseAddress,
  170. UINTN VramSize
  171. )
  172. {
  173. EFI_STATUS Status;
  174. UINT8 Data;
  175. EFI_TPL OldTpl;
  176. EMBEDDED_EXTERNAL_DEVICE *gTPS65950;
  177. // Enable power lines used by TFP410
  178. Status = gBS->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid, NULL, (VOID **)&gTPS65950);
  179. ASSERT_EFI_ERROR (Status);
  180. OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
  181. Data = VAUX_DEV_GRP_P1;
  182. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, VPLL2_DEV_GRP), 1, &Data);
  183. ASSERT_EFI_ERROR(Status);
  184. Data = VAUX_DEDICATED_18V;
  185. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, VPLL2_DEDICATED), 1, &Data);
  186. ASSERT_EFI_ERROR (Status);
  187. // Power up TFP410 (set GPIO2 on TPS - for BeagleBoard-xM)
  188. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2, GPIODATADIR1), 1, &Data);
  189. ASSERT_EFI_ERROR (Status);
  190. Data |= BIT2;
  191. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2, GPIODATADIR1), 1, &Data);
  192. ASSERT_EFI_ERROR (Status);
  193. Data = BIT2;
  194. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2, SETGPIODATAOUT1), 1, &Data);
  195. ASSERT_EFI_ERROR (Status);
  196. gBS->RestoreTPL(OldTpl);
  197. // Power up TFP410 (set GPIO 170 - for older BeagleBoards)
  198. MmioAnd32 (GPIO6_BASE + GPIO_OE, ~BIT10);
  199. MmioOr32 (GPIO6_BASE + GPIO_SETDATAOUT, BIT10);
  200. return EFI_SUCCESS;
  201. }
  202. EFI_STATUS
  203. InitializeDisplay (
  204. IN LCD_INSTANCE* Instance
  205. )
  206. {
  207. EFI_STATUS Status;
  208. UINTN VramSize;
  209. EFI_PHYSICAL_ADDRESS VramBaseAddress;
  210. Status = LcdPlatformGetVram (&VramBaseAddress, &VramSize);
  211. if (EFI_ERROR (Status)) {
  212. return Status;
  213. }
  214. Instance->Mode.FrameBufferBase = VramBaseAddress;
  215. Instance->Mode.FrameBufferSize = VramSize;
  216. Status = HwInitializeDisplay((UINTN)VramBaseAddress, VramSize);
  217. if (!EFI_ERROR (Status)) {
  218. mDisplayInitialized = TRUE;
  219. }
  220. return Status;
  221. }
  222. EFI_STATUS
  223. EFIAPI
  224. LcdGraphicsQueryMode (
  225. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  226. IN UINT32 ModeNumber,
  227. OUT UINTN *SizeOfInfo,
  228. OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info
  229. )
  230. {
  231. LCD_INSTANCE *Instance;
  232. Instance = LCD_INSTANCE_FROM_GOP_THIS(This);
  233. if (!mDisplayInitialized) {
  234. InitializeDisplay (Instance);
  235. }
  236. // Error checking
  237. if ( (This == NULL) || (Info == NULL) || (SizeOfInfo == NULL) || (ModeNumber >= This->Mode->MaxMode) ) {
  238. DEBUG((DEBUG_ERROR, "LcdGraphicsQueryMode: ERROR - For mode number %d : Invalid Parameter.\n", ModeNumber ));
  239. return EFI_INVALID_PARAMETER;
  240. }
  241. *Info = AllocateCopyPool(sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION), &Instance->ModeInfo);
  242. if (*Info == NULL) {
  243. return EFI_OUT_OF_RESOURCES;
  244. }
  245. *SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
  246. (*Info)->Version = 0;
  247. (*Info)->HorizontalResolution = LcdModes[ModeNumber].HorizontalResolution;
  248. (*Info)->VerticalResolution = LcdModes[ModeNumber].VerticalResolution;
  249. (*Info)->PixelFormat = PixelBltOnly;
  250. return EFI_SUCCESS;
  251. }
  252. EFI_STATUS
  253. EFIAPI
  254. LcdGraphicsSetMode (
  255. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  256. IN UINT32 ModeNumber
  257. )
  258. {
  259. LCD_INSTANCE *Instance;
  260. Instance = LCD_INSTANCE_FROM_GOP_THIS(This);
  261. if (ModeNumber >= Instance->Mode.MaxMode) {
  262. return EFI_UNSUPPORTED;
  263. }
  264. if (!mDisplayInitialized) {
  265. InitializeDisplay (Instance);
  266. }
  267. DssSetMode((UINT32)Instance->Mode.FrameBufferBase, ModeNumber);
  268. Instance->Mode.Mode = ModeNumber;
  269. Instance->ModeInfo.HorizontalResolution = LcdModes[ModeNumber].HorizontalResolution;
  270. Instance->ModeInfo.VerticalResolution = LcdModes[ModeNumber].VerticalResolution;
  271. return EFI_SUCCESS;
  272. }
  273. EFI_STATUS
  274. EFIAPI
  275. LcdGraphicsOutputDxeInitialize (
  276. IN EFI_HANDLE ImageHandle,
  277. IN EFI_SYSTEM_TABLE *SystemTable
  278. )
  279. {
  280. EFI_STATUS Status = EFI_SUCCESS;
  281. LCD_INSTANCE* Instance;
  282. Status = LcdInstanceContructor (&Instance);
  283. if (EFI_ERROR(Status)) {
  284. goto EXIT;
  285. }
  286. // Install the Graphics Output Protocol and the Device Path
  287. Status = gBS->InstallMultipleProtocolInterfaces(
  288. &Instance->Handle,
  289. &gEfiGraphicsOutputProtocolGuid, &Instance->Gop,
  290. &gEfiDevicePathProtocolGuid, &Instance->DevicePath,
  291. NULL
  292. );
  293. if (EFI_ERROR(Status)) {
  294. DEBUG((DEBUG_ERROR, "GraphicsOutputDxeInitialize: Can not install the protocol. Exit Status=%r\n", Status));
  295. goto EXIT;
  296. }
  297. // Register for an ExitBootServicesEvent
  298. // When ExitBootServices starts, this function here will make sure that the graphics driver will shut down properly,
  299. // i.e. it will free up all allocated memory and perform any necessary hardware re-configuration.
  300. /*Status = gBS->CreateEvent (
  301. EVT_SIGNAL_EXIT_BOOT_SERVICES,
  302. TPL_NOTIFY,
  303. LcdGraphicsExitBootServicesEvent, NULL,
  304. &Instance->ExitBootServicesEvent
  305. );
  306. if (EFI_ERROR(Status)) {
  307. DEBUG((DEBUG_ERROR, "GraphicsOutputDxeInitialize: Can not install the ExitBootServicesEvent handler. Exit Status=%r\n", Status));
  308. goto EXIT_ERROR_UNINSTALL_PROTOCOL;
  309. }*/
  310. // To get here, everything must be fine, so just exit
  311. goto EXIT;
  312. //EXIT_ERROR_UNINSTALL_PROTOCOL:
  313. /* The following function could return an error message,
  314. * however, to get here something must have gone wrong already,
  315. * so preserve the original error, i.e. don't change
  316. * the Status variable, even it fails to uninstall the protocol.
  317. */
  318. /* gBS->UninstallMultipleProtocolInterfaces (
  319. Instance->Handle,
  320. &gEfiGraphicsOutputProtocolGuid, &Instance->Gop, // Uninstall Graphics Output protocol
  321. &gEfiDevicePathProtocolGuid, &Instance->DevicePath, // Uninstall device path
  322. NULL
  323. );*/
  324. EXIT:
  325. return Status;
  326. }