csi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "FreeRTOS.h"
  2. #include "board.h"
  3. #include "chip.h"
  4. #if VIDEO_IN_FORMAT == VIN_AHD_720P_25
  5. #define HSA 0x2e
  6. #define HBP 0x25
  7. #define HSD 0x02
  8. #define HLINE 0x555
  9. #define VSA 0x06
  10. #define VBP 0x09
  11. #define VFP 0x06
  12. #define VACT 0x2d0
  13. #define LUN_NUM 4
  14. #define CLK_DIV 0x11 //500/17=29M
  15. #endif
  16. #if VIDEO_IN_FORMAT == VIN_AHD_1080P_25
  17. #define HSA 0x2c
  18. #define HBP 0x94
  19. #define HSD 0x02
  20. #define HLINE 0xA50
  21. #define VSA 0x05
  22. #define VBP 0x24
  23. #define VFP 0x04
  24. #define VACT 0x438
  25. #define LUN_NUM 4
  26. #define CLK_DIV 0x6 //500/6=83.3.4M
  27. #endif
  28. #if VIDEO_IN_FORMAT == VIN_CVBS_NTSC
  29. #define HSA 0x2e
  30. #define HBP 0x25
  31. #define HSD 0x02
  32. #define HLINE 0x350
  33. #define VSA 0x06
  34. #define VBP 0x09
  35. #define VFP 0x06
  36. #define VACT 0xf0
  37. #define LUN_NUM 2
  38. #define CLK_DIV 0x20 //500/32=15.6M
  39. #endif
  40. static void ApbWriteFun(unsigned int addr, unsigned int data)
  41. {
  42. * (volatile unsigned int *) addr = data;
  43. }
  44. static unsigned int ApbReadFun(unsigned int addr)
  45. {
  46. return (* (volatile unsigned int *) addr);
  47. }
  48. unsigned char csi_read_reg(unsigned int addr)
  49. {
  50. unsigned int rdata;
  51. unsigned int val;
  52. //read phy
  53. val = 0x10000 | addr;
  54. ApbWriteFun(0x51b00054, val);
  55. ApbWriteFun(0x51b00050, 0x2);
  56. ApbWriteFun(0x51b00050, 0x0);
  57. rdata = ApbReadFun(0x51b00054);
  58. rdata = (rdata >> 8)&0xFF;
  59. return rdata;
  60. }
  61. void csi_write_reg(unsigned int addr,unsigned int regval)
  62. {
  63. unsigned int val = 0;
  64. ApbWriteFun(0x51b00054, addr);
  65. val = 0x10000 | addr;
  66. ApbWriteFun(0x51b00054, val);
  67. ApbWriteFun(0x51b00050, 0x2);
  68. ApbWriteFun(0x51b00050, 0x0);
  69. ApbWriteFun(0x51b00054, regval);
  70. ApbWriteFun(0x51b00050, 0x2);
  71. ApbWriteFun(0x51b00050, 0x0);
  72. }
  73. void csi_init(void)
  74. {
  75. //reset csi
  76. unsigned int regval = 0;
  77. regval = ApbReadFun(0x50000188);
  78. regval &=~(3<<5 | 1<<1 );
  79. ApbWriteFun(0x50000188,regval);
  80. udelay(20);
  81. regval |=(3<<5 | 1<<1);
  82. ApbWriteFun(0x50000188,regval);
  83. udelay(20);
  84. ApbWriteFun(0x500000a0,0x0000101c); //CSI_MIPI_EN = 0
  85. // gpu clk 500M
  86. ApbWriteFun(0x50000198,(CLK_DIV-1));
  87. //==================== host phy cfg=========================//
  88. ApbWriteFun(0x51b00040,0x00000000); //power down phy
  89. ApbWriteFun(0x51b00044,0x00000000); //reset phy
  90. ApbWriteFun(0x51b00040,0x00000001); //power down phy
  91. ApbWriteFun(0x51b00044,0x00000001); //reset phy
  92. //csi phy reset delay 5ms
  93. vTaskDelay(pdMS_TO_TICKS(5));
  94. regval = 0xff;
  95. csi_write_reg(0x34,regval);
  96. csi_write_reg(0x34,0x01);
  97. csi_write_reg(0x44,regval);
  98. csi_write_reg(0x44,0x11); //600M
  99. csi_write_reg(0x54,regval);
  100. csi_write_reg(0x54,0x01);
  101. csi_write_reg(0x84,regval);
  102. csi_write_reg(0x84,0x01);
  103. csi_write_reg(0x94,regval);
  104. //================mipi host=================//
  105. if(LUN_NUM == 4)
  106. ApbWriteFun(0x51b00004,0x00000003); //4 CHANAEL
  107. else if(LUN_NUM == 2)
  108. ApbWriteFun(0x51b00004,0x00000001); //2 CHANAEL
  109. ApbWriteFun(0x51b00080,0x00000000); //ipi mode: 0: camera timing; 1: controller timing
  110. ApbWriteFun(0x51b00088,0x0000001e); //data type
  111. //config csi timing
  112. ApbWriteFun(0x51b00090,HSA); //hsa
  113. ApbWriteFun(0x51b00094,HBP); //hbp
  114. ApbWriteFun(0x51b00098,HSD); //hsd
  115. ApbWriteFun(0x51b0009c,HLINE); //hline
  116. ApbWriteFun(0x51b000b0,VSA); //vsa
  117. ApbWriteFun(0x51b000b4,VBP); //vbp
  118. ApbWriteFun(0x51b000b8,VFP); //vfp
  119. ApbWriteFun(0x51b000bc,VACT); //vact
  120. ApbWriteFun(0x51b00008,0x00000001); //csi2_resetn active low
  121. //================mipi recover=================//
  122. ApbWriteFun(0x50000190,0x00000002); //mipi_ctl_reg[15:0] -> {bist_mode, hsync_mode, de_inv, hsync_inv, vsync_inv, data_type[1:0]}
  123. ApbWriteFun(0x5000019c,HLINE); //csi_cfg1_reg[14:0] -> {csi_total_pix}
  124. regval = (VACT + VSA + VBP)<<16 | VACT;
  125. ApbWriteFun(0x500001a0,regval); //csi_cfg2_reg -> {de_last_line[30:16], csi_act_line[14:0]}
  126. ApbWriteFun(0x500001a4,0x02e00010); //csi_cfg3_reg -> {de_h_fall[30:16],de_h_rise[14:0]}
  127. ApbWriteFun(0x500001a8,0x00300002); //csi_cfg4_reg -> {hsync_h_fall[30:16],hsync_h_rise[14:0]}
  128. //3.3V
  129. ApbWriteFun(0x500000a0,0x0000101d); //CSI_MIPI_EN = 1
  130. vTaskDelay(pdMS_TO_TICKS(500));
  131. printf(">>>>>>>>>csi init over!!!!\n");
  132. }