drv_clcd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <rtthread.h>
  5. #include <lwp.h>
  6. #include <board.h>
  7. #include <lwp_user_mm.h>
  8. #include "drv_clcd.h"
  9. #include "rt_lcd.h"
  10. #define CLCD_WIDTH (BSP_LCD_WIDTH)
  11. #define CLCD_HEIGHT (BSP_LCD_HEIGHT)
  12. #define CLCD_DEVICE(dev) (struct drv_clcd_device*)(dev)
  13. #define PL111_CR_EN 0x001
  14. #define PL111_CR_PWR 0x800
  15. #define PL111_IOBASE 0x10020000
  16. #define PL111_PALBASE (PL111_IOBASE + 0x200)
  17. typedef struct _PL111MMIO
  18. {
  19. uint32_t volatile tim0; //0
  20. uint32_t volatile tim1; //4
  21. uint32_t volatile tim2; //8
  22. uint32_t volatile tim3; //c
  23. uint32_t volatile upbase; //10
  24. uint32_t volatile f; //14
  25. uint32_t volatile control; //18
  26. uint32_t volatile g; //1c
  27. } PL111MMIO;
  28. struct drv_clcd_device
  29. {
  30. struct rt_device parent;
  31. int width;
  32. int height;
  33. uint8_t *fb;
  34. uint8_t *fb_virt;
  35. };
  36. struct drv_clcd_device _lcd;
  37. static rt_err_t drv_clcd_init(struct rt_device *device)
  38. {
  39. struct drv_clcd_device *lcd = CLCD_DEVICE(device);
  40. (void)lcd; /* nothing, right now */
  41. return RT_EOK;
  42. }
  43. static rt_err_t drv_clcd_control(struct rt_device *device, int cmd, void *args)
  44. {
  45. struct drv_clcd_device *lcd = CLCD_DEVICE(device);
  46. switch (cmd)
  47. {
  48. case RTGRAPHIC_CTRL_RECT_UPDATE:
  49. {
  50. struct rt_device_rect_info *info = (struct rt_device_rect_info*)args;
  51. (void)info; /* nothing, right now */
  52. rt_kprintf("update screen...\n");
  53. }
  54. break;
  55. case RTGRAPHIC_CTRL_GET_INFO:
  56. {
  57. struct rt_device_graphic_info* info = (struct rt_device_graphic_info*)args;
  58. RT_ASSERT(info != RT_NULL);
  59. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
  60. info->bits_per_pixel= 16;
  61. info->width = lcd->width;
  62. info->height = lcd->height;
  63. info->framebuffer = lcd->fb;
  64. }
  65. break;
  66. case FBIOGET_FSCREENINFO:
  67. {
  68. #ifdef RT_USING_USERSPACE
  69. struct fb_fix_screeninfo *info = (struct fb_fix_screeninfo *)args;
  70. strncpy(info->id, "lcd", sizeof(info->id));
  71. info->smem_len = lcd->width * lcd->height * 2;
  72. info->smem_start = (uint32_t)lwp_map_user_phy(lwp_self(), RT_NULL, lcd->fb,
  73. info->smem_len, 1);
  74. info->line_length = lcd->width * 2;
  75. #endif
  76. }
  77. break;
  78. case FBIOGET_VSCREENINFO:
  79. {
  80. struct fb_var_screeninfo *info = (struct fb_var_screeninfo *)args;
  81. info->bits_per_pixel = 16;
  82. info->xres = lcd->width;
  83. info->yres = lcd->height;
  84. }
  85. break;
  86. case FBIOGET_DISPINFO:
  87. break;
  88. }
  89. return RT_EOK;
  90. }
  91. #ifdef RT_USING_DEVICE_OPS
  92. const static struct rt_device_ops clcd_ops =
  93. {
  94. drv_clcd_init,
  95. RT_NULL,
  96. RT_NULL,
  97. RT_NULL,
  98. RT_NULL,
  99. drv_clcd_control
  100. };
  101. #endif
  102. int drv_clcd_hw_init(void)
  103. {
  104. PL111MMIO *plio;
  105. struct rt_device *device = &_lcd.parent;
  106. /* memset _lcd to zero */
  107. memset(&_lcd, 0x0, sizeof(_lcd));
  108. _lcd.width = CLCD_WIDTH;
  109. _lcd.height = CLCD_HEIGHT;
  110. rt_kprintf("try to allocate fb... | w - %d, h - %d | ", _lcd.width, _lcd.height);
  111. #ifdef RT_USING_USERSPACE
  112. _lcd.fb_virt= rt_pages_alloc (rt_page_bits(_lcd.width * _lcd.height * 2));
  113. _lcd.fb = _lcd.fb_virt + PV_OFFSET;
  114. rt_kprintf("done!\n");
  115. rt_kprintf("fb => 0x%08x\n", _lcd.fb);
  116. if (_lcd.fb == NULL)
  117. {
  118. rt_kprintf("initialize frame buffer failed!\n");
  119. return -1;
  120. }
  121. memset(_lcd.fb_virt, 0xff, _lcd.width * _lcd.height * 2);
  122. #else
  123. _lcd.fb = rt_malloc(_lcd.width * _lcd.height * 2);
  124. if (_lcd.fb == NULL)
  125. {
  126. rt_kprintf("initialize frame buffer failed!\n");
  127. return -1;
  128. }
  129. memset(_lcd.fb, 0xff, _lcd.width * _lcd.height * 2);
  130. #endif
  131. plio = (PL111MMIO *)rt_ioremap((void*)PL111_IOBASE, 0x1000);
  132. plio->tim0 = 0x3F1F3C00 | ((CLCD_WIDTH / 16 - 1) << 2);
  133. plio->tim1 = 0x080B6000 | (CLCD_HEIGHT - 1);
  134. plio->upbase = (uint32_t)_lcd.fb;
  135. /* 16-bit 565 color */
  136. plio->control = 0x1921 | (0x6 << 1);
  137. device->type = RT_Device_Class_Graphic;
  138. #ifdef RT_USING_DEVICE_OPS
  139. device->ops = &clcd_ops;
  140. #else
  141. device->init = drv_clcd_init;
  142. device->control = drv_clcd_control;
  143. #endif
  144. rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
  145. return 0;
  146. }
  147. INIT_DEVICE_EXPORT(drv_clcd_hw_init);