sti_hqvdp.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Authors: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <linux/component.h>
  7. #include <linux/firmware.h>
  8. #include <linux/reset.h>
  9. #include <linux/seq_file.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_fb_cma_helper.h>
  12. #include <drm/drm_gem_cma_helper.h>
  13. #include "sti_compositor.h"
  14. #include "sti_hqvdp_lut.h"
  15. #include "sti_plane.h"
  16. #include "sti_vtg.h"
  17. /* Firmware name */
  18. #define HQVDP_FMW_NAME "hqvdp-stih407.bin"
  19. /* Regs address */
  20. #define HQVDP_DMEM 0x00000000 /* 0x00000000 */
  21. #define HQVDP_PMEM 0x00040000 /* 0x00040000 */
  22. #define HQVDP_RD_PLUG 0x000E0000 /* 0x000E0000 */
  23. #define HQVDP_RD_PLUG_CONTROL (HQVDP_RD_PLUG + 0x1000) /* 0x000E1000 */
  24. #define HQVDP_RD_PLUG_PAGE_SIZE (HQVDP_RD_PLUG + 0x1004) /* 0x000E1004 */
  25. #define HQVDP_RD_PLUG_MIN_OPC (HQVDP_RD_PLUG + 0x1008) /* 0x000E1008 */
  26. #define HQVDP_RD_PLUG_MAX_OPC (HQVDP_RD_PLUG + 0x100C) /* 0x000E100C */
  27. #define HQVDP_RD_PLUG_MAX_CHK (HQVDP_RD_PLUG + 0x1010) /* 0x000E1010 */
  28. #define HQVDP_RD_PLUG_MAX_MSG (HQVDP_RD_PLUG + 0x1014) /* 0x000E1014 */
  29. #define HQVDP_RD_PLUG_MIN_SPACE (HQVDP_RD_PLUG + 0x1018) /* 0x000E1018 */
  30. #define HQVDP_WR_PLUG 0x000E2000 /* 0x000E2000 */
  31. #define HQVDP_WR_PLUG_CONTROL (HQVDP_WR_PLUG + 0x1000) /* 0x000E3000 */
  32. #define HQVDP_WR_PLUG_PAGE_SIZE (HQVDP_WR_PLUG + 0x1004) /* 0x000E3004 */
  33. #define HQVDP_WR_PLUG_MIN_OPC (HQVDP_WR_PLUG + 0x1008) /* 0x000E3008 */
  34. #define HQVDP_WR_PLUG_MAX_OPC (HQVDP_WR_PLUG + 0x100C) /* 0x000E300C */
  35. #define HQVDP_WR_PLUG_MAX_CHK (HQVDP_WR_PLUG + 0x1010) /* 0x000E3010 */
  36. #define HQVDP_WR_PLUG_MAX_MSG (HQVDP_WR_PLUG + 0x1014) /* 0x000E3014 */
  37. #define HQVDP_WR_PLUG_MIN_SPACE (HQVDP_WR_PLUG + 0x1018) /* 0x000E3018 */
  38. #define HQVDP_MBX 0x000E4000 /* 0x000E4000 */
  39. #define HQVDP_MBX_IRQ_TO_XP70 (HQVDP_MBX + 0x0000) /* 0x000E4000 */
  40. #define HQVDP_MBX_INFO_HOST (HQVDP_MBX + 0x0004) /* 0x000E4004 */
  41. #define HQVDP_MBX_IRQ_TO_HOST (HQVDP_MBX + 0x0008) /* 0x000E4008 */
  42. #define HQVDP_MBX_INFO_XP70 (HQVDP_MBX + 0x000C) /* 0x000E400C */
  43. #define HQVDP_MBX_SW_RESET_CTRL (HQVDP_MBX + 0x0010) /* 0x000E4010 */
  44. #define HQVDP_MBX_STARTUP_CTRL1 (HQVDP_MBX + 0x0014) /* 0x000E4014 */
  45. #define HQVDP_MBX_STARTUP_CTRL2 (HQVDP_MBX + 0x0018) /* 0x000E4018 */
  46. #define HQVDP_MBX_GP_STATUS (HQVDP_MBX + 0x001C) /* 0x000E401C */
  47. #define HQVDP_MBX_NEXT_CMD (HQVDP_MBX + 0x0020) /* 0x000E4020 */
  48. #define HQVDP_MBX_CURRENT_CMD (HQVDP_MBX + 0x0024) /* 0x000E4024 */
  49. #define HQVDP_MBX_SOFT_VSYNC (HQVDP_MBX + 0x0028) /* 0x000E4028 */
  50. /* Plugs config */
  51. #define PLUG_CONTROL_ENABLE 0x00000001
  52. #define PLUG_PAGE_SIZE_256 0x00000002
  53. #define PLUG_MIN_OPC_8 0x00000003
  54. #define PLUG_MAX_OPC_64 0x00000006
  55. #define PLUG_MAX_CHK_2X 0x00000001
  56. #define PLUG_MAX_MSG_1X 0x00000000
  57. #define PLUG_MIN_SPACE_1 0x00000000
  58. /* SW reset CTRL */
  59. #define SW_RESET_CTRL_FULL BIT(0)
  60. #define SW_RESET_CTRL_CORE BIT(1)
  61. /* Startup ctrl 1 */
  62. #define STARTUP_CTRL1_RST_DONE BIT(0)
  63. #define STARTUP_CTRL1_AUTH_IDLE BIT(2)
  64. /* Startup ctrl 2 */
  65. #define STARTUP_CTRL2_FETCH_EN BIT(1)
  66. /* Info xP70 */
  67. #define INFO_XP70_FW_READY BIT(15)
  68. #define INFO_XP70_FW_PROCESSING BIT(14)
  69. #define INFO_XP70_FW_INITQUEUES BIT(13)
  70. /* SOFT_VSYNC */
  71. #define SOFT_VSYNC_HW 0x00000000
  72. #define SOFT_VSYNC_SW_CMD 0x00000001
  73. #define SOFT_VSYNC_SW_CTRL_IRQ 0x00000003
  74. /* Reset & boot poll config */
  75. #define POLL_MAX_ATTEMPT 50
  76. #define POLL_DELAY_MS 20
  77. #define SCALE_FACTOR 8192
  78. #define SCALE_MAX_FOR_LEG_LUT_F 4096
  79. #define SCALE_MAX_FOR_LEG_LUT_E 4915
  80. #define SCALE_MAX_FOR_LEG_LUT_D 6654
  81. #define SCALE_MAX_FOR_LEG_LUT_C 8192
  82. enum sti_hvsrc_orient {
  83. HVSRC_HORI,
  84. HVSRC_VERT
  85. };
  86. /* Command structures */
  87. struct sti_hqvdp_top {
  88. u32 config;
  89. u32 mem_format;
  90. u32 current_luma;
  91. u32 current_enh_luma;
  92. u32 current_right_luma;
  93. u32 current_enh_right_luma;
  94. u32 current_chroma;
  95. u32 current_enh_chroma;
  96. u32 current_right_chroma;
  97. u32 current_enh_right_chroma;
  98. u32 output_luma;
  99. u32 output_chroma;
  100. u32 luma_src_pitch;
  101. u32 luma_enh_src_pitch;
  102. u32 luma_right_src_pitch;
  103. u32 luma_enh_right_src_pitch;
  104. u32 chroma_src_pitch;
  105. u32 chroma_enh_src_pitch;
  106. u32 chroma_right_src_pitch;
  107. u32 chroma_enh_right_src_pitch;
  108. u32 luma_processed_pitch;
  109. u32 chroma_processed_pitch;
  110. u32 input_frame_size;
  111. u32 input_viewport_ori;
  112. u32 input_viewport_ori_right;
  113. u32 input_viewport_size;
  114. u32 left_view_border_width;
  115. u32 right_view_border_width;
  116. u32 left_view_3d_offset_width;
  117. u32 right_view_3d_offset_width;
  118. u32 side_stripe_color;
  119. u32 crc_reset_ctrl;
  120. };
  121. /* Configs for interlaced : no IT, no pass thru, 3 fields */
  122. #define TOP_CONFIG_INTER_BTM 0x00000000
  123. #define TOP_CONFIG_INTER_TOP 0x00000002
  124. /* Config for progressive : no IT, no pass thru, 3 fields */
  125. #define TOP_CONFIG_PROGRESSIVE 0x00000001
  126. /* Default MemFormat: in=420_raster_dual out=444_raster;opaque Mem2Tv mode */
  127. #define TOP_MEM_FORMAT_DFLT 0x00018060
  128. /* Min/Max size */
  129. #define MAX_WIDTH 0x1FFF
  130. #define MAX_HEIGHT 0x0FFF
  131. #define MIN_WIDTH 0x0030
  132. #define MIN_HEIGHT 0x0010
  133. struct sti_hqvdp_vc1re {
  134. u32 ctrl_prv_csdi;
  135. u32 ctrl_cur_csdi;
  136. u32 ctrl_nxt_csdi;
  137. u32 ctrl_cur_fmd;
  138. u32 ctrl_nxt_fmd;
  139. };
  140. struct sti_hqvdp_fmd {
  141. u32 config;
  142. u32 viewport_ori;
  143. u32 viewport_size;
  144. u32 next_next_luma;
  145. u32 next_next_right_luma;
  146. u32 next_next_next_luma;
  147. u32 next_next_next_right_luma;
  148. u32 threshold_scd;
  149. u32 threshold_rfd;
  150. u32 threshold_move;
  151. u32 threshold_cfd;
  152. };
  153. struct sti_hqvdp_csdi {
  154. u32 config;
  155. u32 config2;
  156. u32 dcdi_config;
  157. u32 prev_luma;
  158. u32 prev_enh_luma;
  159. u32 prev_right_luma;
  160. u32 prev_enh_right_luma;
  161. u32 next_luma;
  162. u32 next_enh_luma;
  163. u32 next_right_luma;
  164. u32 next_enh_right_luma;
  165. u32 prev_chroma;
  166. u32 prev_enh_chroma;
  167. u32 prev_right_chroma;
  168. u32 prev_enh_right_chroma;
  169. u32 next_chroma;
  170. u32 next_enh_chroma;
  171. u32 next_right_chroma;
  172. u32 next_enh_right_chroma;
  173. u32 prev_motion;
  174. u32 prev_right_motion;
  175. u32 cur_motion;
  176. u32 cur_right_motion;
  177. u32 next_motion;
  178. u32 next_right_motion;
  179. };
  180. /* Config for progressive: by pass */
  181. #define CSDI_CONFIG_PROG 0x00000000
  182. /* Config for directional deinterlacing without motion */
  183. #define CSDI_CONFIG_INTER_DIR 0x00000016
  184. /* Additional configs for fader, blender, motion,... deinterlace algorithms */
  185. #define CSDI_CONFIG2_DFLT 0x000001B3
  186. #define CSDI_DCDI_CONFIG_DFLT 0x00203803
  187. struct sti_hqvdp_hvsrc {
  188. u32 hor_panoramic_ctrl;
  189. u32 output_picture_size;
  190. u32 init_horizontal;
  191. u32 init_vertical;
  192. u32 param_ctrl;
  193. u32 yh_coef[NB_COEF];
  194. u32 ch_coef[NB_COEF];
  195. u32 yv_coef[NB_COEF];
  196. u32 cv_coef[NB_COEF];
  197. u32 hori_shift;
  198. u32 vert_shift;
  199. };
  200. /* Default ParamCtrl: all controls enabled */
  201. #define HVSRC_PARAM_CTRL_DFLT 0xFFFFFFFF
  202. struct sti_hqvdp_iqi {
  203. u32 config;
  204. u32 demo_wind_size;
  205. u32 pk_config;
  206. u32 coeff0_coeff1;
  207. u32 coeff2_coeff3;
  208. u32 coeff4;
  209. u32 pk_lut;
  210. u32 pk_gain;
  211. u32 pk_coring_level;
  212. u32 cti_config;
  213. u32 le_config;
  214. u32 le_lut[64];
  215. u32 con_bri;
  216. u32 sat_gain;
  217. u32 pxf_conf;
  218. u32 default_color;
  219. };
  220. /* Default Config : IQI bypassed */
  221. #define IQI_CONFIG_DFLT 0x00000001
  222. /* Default Contrast & Brightness gain = 256 */
  223. #define IQI_CON_BRI_DFLT 0x00000100
  224. /* Default Saturation gain = 256 */
  225. #define IQI_SAT_GAIN_DFLT 0x00000100
  226. /* Default PxfConf : P2I bypassed */
  227. #define IQI_PXF_CONF_DFLT 0x00000001
  228. struct sti_hqvdp_top_status {
  229. u32 processing_time;
  230. u32 input_y_crc;
  231. u32 input_uv_crc;
  232. };
  233. struct sti_hqvdp_fmd_status {
  234. u32 fmd_repeat_move_status;
  235. u32 fmd_scene_count_status;
  236. u32 cfd_sum;
  237. u32 field_sum;
  238. u32 next_y_fmd_crc;
  239. u32 next_next_y_fmd_crc;
  240. u32 next_next_next_y_fmd_crc;
  241. };
  242. struct sti_hqvdp_csdi_status {
  243. u32 prev_y_csdi_crc;
  244. u32 cur_y_csdi_crc;
  245. u32 next_y_csdi_crc;
  246. u32 prev_uv_csdi_crc;
  247. u32 cur_uv_csdi_crc;
  248. u32 next_uv_csdi_crc;
  249. u32 y_csdi_crc;
  250. u32 uv_csdi_crc;
  251. u32 uv_cup_crc;
  252. u32 mot_csdi_crc;
  253. u32 mot_cur_csdi_crc;
  254. u32 mot_prev_csdi_crc;
  255. };
  256. struct sti_hqvdp_hvsrc_status {
  257. u32 y_hvsrc_crc;
  258. u32 u_hvsrc_crc;
  259. u32 v_hvsrc_crc;
  260. };
  261. struct sti_hqvdp_iqi_status {
  262. u32 pxf_it_status;
  263. u32 y_iqi_crc;
  264. u32 u_iqi_crc;
  265. u32 v_iqi_crc;
  266. };
  267. /* Main commands. We use 2 commands one being processed by the firmware, one
  268. * ready to be fetched upon next Vsync*/
  269. #define NB_VDP_CMD 2
  270. struct sti_hqvdp_cmd {
  271. struct sti_hqvdp_top top;
  272. struct sti_hqvdp_vc1re vc1re;
  273. struct sti_hqvdp_fmd fmd;
  274. struct sti_hqvdp_csdi csdi;
  275. struct sti_hqvdp_hvsrc hvsrc;
  276. struct sti_hqvdp_iqi iqi;
  277. struct sti_hqvdp_top_status top_status;
  278. struct sti_hqvdp_fmd_status fmd_status;
  279. struct sti_hqvdp_csdi_status csdi_status;
  280. struct sti_hqvdp_hvsrc_status hvsrc_status;
  281. struct sti_hqvdp_iqi_status iqi_status;
  282. };
  283. /*
  284. * STI HQVDP structure
  285. *
  286. * @dev: driver device
  287. * @drm_dev: the drm device
  288. * @regs: registers
  289. * @plane: plane structure for hqvdp it self
  290. * @clk: IP clock
  291. * @clk_pix_main: pix main clock
  292. * @reset: reset control
  293. * @vtg_nb: notifier to handle VTG Vsync
  294. * @btm_field_pending: is there any bottom field (interlaced frame) to display
  295. * @hqvdp_cmd: buffer of commands
  296. * @hqvdp_cmd_paddr: physical address of hqvdp_cmd
  297. * @vtg: vtg for main data path
  298. * @xp70_initialized: true if xp70 is already initialized
  299. */
  300. struct sti_hqvdp {
  301. struct device *dev;
  302. struct drm_device *drm_dev;
  303. void __iomem *regs;
  304. struct sti_plane plane;
  305. struct clk *clk;
  306. struct clk *clk_pix_main;
  307. struct reset_control *reset;
  308. struct notifier_block vtg_nb;
  309. bool btm_field_pending;
  310. void *hqvdp_cmd;
  311. u32 hqvdp_cmd_paddr;
  312. struct sti_vtg *vtg;
  313. bool xp70_initialized;
  314. };
  315. #define to_sti_hqvdp(x) container_of(x, struct sti_hqvdp, plane)
  316. static const uint32_t hqvdp_supported_formats[] = {
  317. DRM_FORMAT_NV12,
  318. };
  319. /**
  320. * sti_hqvdp_get_free_cmd
  321. * @hqvdp: hqvdp structure
  322. *
  323. * Look for a hqvdp_cmd that is not being used (or about to be used) by the FW.
  324. *
  325. * RETURNS:
  326. * the offset of the command to be used.
  327. * -1 in error cases
  328. */
  329. static int sti_hqvdp_get_free_cmd(struct sti_hqvdp *hqvdp)
  330. {
  331. u32 curr_cmd, next_cmd;
  332. u32 cmd = hqvdp->hqvdp_cmd_paddr;
  333. int i;
  334. curr_cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
  335. next_cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  336. for (i = 0; i < NB_VDP_CMD; i++) {
  337. if ((cmd != curr_cmd) && (cmd != next_cmd))
  338. return i * sizeof(struct sti_hqvdp_cmd);
  339. cmd += sizeof(struct sti_hqvdp_cmd);
  340. }
  341. return -1;
  342. }
  343. /**
  344. * sti_hqvdp_get_curr_cmd
  345. * @hqvdp: hqvdp structure
  346. *
  347. * Look for the hqvdp_cmd that is being used by the FW.
  348. *
  349. * RETURNS:
  350. * the offset of the command to be used.
  351. * -1 in error cases
  352. */
  353. static int sti_hqvdp_get_curr_cmd(struct sti_hqvdp *hqvdp)
  354. {
  355. u32 curr_cmd;
  356. u32 cmd = hqvdp->hqvdp_cmd_paddr;
  357. unsigned int i;
  358. curr_cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
  359. for (i = 0; i < NB_VDP_CMD; i++) {
  360. if (cmd == curr_cmd)
  361. return i * sizeof(struct sti_hqvdp_cmd);
  362. cmd += sizeof(struct sti_hqvdp_cmd);
  363. }
  364. return -1;
  365. }
  366. /**
  367. * sti_hqvdp_get_next_cmd
  368. * @hqvdp: hqvdp structure
  369. *
  370. * Look for the next hqvdp_cmd that will be used by the FW.
  371. *
  372. * RETURNS:
  373. * the offset of the next command that will be used.
  374. * -1 in error cases
  375. */
  376. static int sti_hqvdp_get_next_cmd(struct sti_hqvdp *hqvdp)
  377. {
  378. int next_cmd;
  379. dma_addr_t cmd = hqvdp->hqvdp_cmd_paddr;
  380. unsigned int i;
  381. next_cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  382. for (i = 0; i < NB_VDP_CMD; i++) {
  383. if (cmd == next_cmd)
  384. return i * sizeof(struct sti_hqvdp_cmd);
  385. cmd += sizeof(struct sti_hqvdp_cmd);
  386. }
  387. return -1;
  388. }
  389. #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
  390. readl(hqvdp->regs + reg))
  391. static const char *hqvdp_dbg_get_lut(u32 *coef)
  392. {
  393. if (!memcmp(coef, coef_lut_a_legacy, 16))
  394. return "LUT A";
  395. if (!memcmp(coef, coef_lut_b, 16))
  396. return "LUT B";
  397. if (!memcmp(coef, coef_lut_c_y_legacy, 16))
  398. return "LUT C Y";
  399. if (!memcmp(coef, coef_lut_c_c_legacy, 16))
  400. return "LUT C C";
  401. if (!memcmp(coef, coef_lut_d_y_legacy, 16))
  402. return "LUT D Y";
  403. if (!memcmp(coef, coef_lut_d_c_legacy, 16))
  404. return "LUT D C";
  405. if (!memcmp(coef, coef_lut_e_y_legacy, 16))
  406. return "LUT E Y";
  407. if (!memcmp(coef, coef_lut_e_c_legacy, 16))
  408. return "LUT E C";
  409. if (!memcmp(coef, coef_lut_f_y_legacy, 16))
  410. return "LUT F Y";
  411. if (!memcmp(coef, coef_lut_f_c_legacy, 16))
  412. return "LUT F C";
  413. return "<UNKNOWN>";
  414. }
  415. static void hqvdp_dbg_dump_cmd(struct seq_file *s, struct sti_hqvdp_cmd *c)
  416. {
  417. int src_w, src_h, dst_w, dst_h;
  418. seq_puts(s, "\n\tTOP:");
  419. seq_printf(s, "\n\t %-20s 0x%08X", "Config", c->top.config);
  420. switch (c->top.config) {
  421. case TOP_CONFIG_PROGRESSIVE:
  422. seq_puts(s, "\tProgressive");
  423. break;
  424. case TOP_CONFIG_INTER_TOP:
  425. seq_puts(s, "\tInterlaced, top field");
  426. break;
  427. case TOP_CONFIG_INTER_BTM:
  428. seq_puts(s, "\tInterlaced, bottom field");
  429. break;
  430. default:
  431. seq_puts(s, "\t<UNKNOWN>");
  432. break;
  433. }
  434. seq_printf(s, "\n\t %-20s 0x%08X", "MemFormat", c->top.mem_format);
  435. seq_printf(s, "\n\t %-20s 0x%08X", "CurrentY", c->top.current_luma);
  436. seq_printf(s, "\n\t %-20s 0x%08X", "CurrentC", c->top.current_chroma);
  437. seq_printf(s, "\n\t %-20s 0x%08X", "YSrcPitch", c->top.luma_src_pitch);
  438. seq_printf(s, "\n\t %-20s 0x%08X", "CSrcPitch",
  439. c->top.chroma_src_pitch);
  440. seq_printf(s, "\n\t %-20s 0x%08X", "InputFrameSize",
  441. c->top.input_frame_size);
  442. seq_printf(s, "\t%dx%d",
  443. c->top.input_frame_size & 0x0000FFFF,
  444. c->top.input_frame_size >> 16);
  445. seq_printf(s, "\n\t %-20s 0x%08X", "InputViewportSize",
  446. c->top.input_viewport_size);
  447. src_w = c->top.input_viewport_size & 0x0000FFFF;
  448. src_h = c->top.input_viewport_size >> 16;
  449. seq_printf(s, "\t%dx%d", src_w, src_h);
  450. seq_puts(s, "\n\tHVSRC:");
  451. seq_printf(s, "\n\t %-20s 0x%08X", "OutputPictureSize",
  452. c->hvsrc.output_picture_size);
  453. dst_w = c->hvsrc.output_picture_size & 0x0000FFFF;
  454. dst_h = c->hvsrc.output_picture_size >> 16;
  455. seq_printf(s, "\t%dx%d", dst_w, dst_h);
  456. seq_printf(s, "\n\t %-20s 0x%08X", "ParamCtrl", c->hvsrc.param_ctrl);
  457. seq_printf(s, "\n\t %-20s %s", "yh_coef",
  458. hqvdp_dbg_get_lut(c->hvsrc.yh_coef));
  459. seq_printf(s, "\n\t %-20s %s", "ch_coef",
  460. hqvdp_dbg_get_lut(c->hvsrc.ch_coef));
  461. seq_printf(s, "\n\t %-20s %s", "yv_coef",
  462. hqvdp_dbg_get_lut(c->hvsrc.yv_coef));
  463. seq_printf(s, "\n\t %-20s %s", "cv_coef",
  464. hqvdp_dbg_get_lut(c->hvsrc.cv_coef));
  465. seq_printf(s, "\n\t %-20s", "ScaleH");
  466. if (dst_w > src_w)
  467. seq_printf(s, " %d/1", dst_w / src_w);
  468. else
  469. seq_printf(s, " 1/%d", src_w / dst_w);
  470. seq_printf(s, "\n\t %-20s", "tScaleV");
  471. if (dst_h > src_h)
  472. seq_printf(s, " %d/1", dst_h / src_h);
  473. else
  474. seq_printf(s, " 1/%d", src_h / dst_h);
  475. seq_puts(s, "\n\tCSDI:");
  476. seq_printf(s, "\n\t %-20s 0x%08X\t", "Config", c->csdi.config);
  477. switch (c->csdi.config) {
  478. case CSDI_CONFIG_PROG:
  479. seq_puts(s, "Bypass");
  480. break;
  481. case CSDI_CONFIG_INTER_DIR:
  482. seq_puts(s, "Deinterlace, directional");
  483. break;
  484. default:
  485. seq_puts(s, "<UNKNOWN>");
  486. break;
  487. }
  488. seq_printf(s, "\n\t %-20s 0x%08X", "Config2", c->csdi.config2);
  489. seq_printf(s, "\n\t %-20s 0x%08X", "DcdiConfig", c->csdi.dcdi_config);
  490. }
  491. static int hqvdp_dbg_show(struct seq_file *s, void *data)
  492. {
  493. struct drm_info_node *node = s->private;
  494. struct sti_hqvdp *hqvdp = (struct sti_hqvdp *)node->info_ent->data;
  495. int cmd, cmd_offset, infoxp70;
  496. void *virt;
  497. seq_printf(s, "%s: (vaddr = 0x%p)",
  498. sti_plane_to_str(&hqvdp->plane), hqvdp->regs);
  499. DBGFS_DUMP(HQVDP_MBX_IRQ_TO_XP70);
  500. DBGFS_DUMP(HQVDP_MBX_INFO_HOST);
  501. DBGFS_DUMP(HQVDP_MBX_IRQ_TO_HOST);
  502. DBGFS_DUMP(HQVDP_MBX_INFO_XP70);
  503. infoxp70 = readl(hqvdp->regs + HQVDP_MBX_INFO_XP70);
  504. seq_puts(s, "\tFirmware state: ");
  505. if (infoxp70 & INFO_XP70_FW_READY)
  506. seq_puts(s, "idle and ready");
  507. else if (infoxp70 & INFO_XP70_FW_PROCESSING)
  508. seq_puts(s, "processing a picture");
  509. else if (infoxp70 & INFO_XP70_FW_INITQUEUES)
  510. seq_puts(s, "programming queues");
  511. else
  512. seq_puts(s, "NOT READY");
  513. DBGFS_DUMP(HQVDP_MBX_SW_RESET_CTRL);
  514. DBGFS_DUMP(HQVDP_MBX_STARTUP_CTRL1);
  515. if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1)
  516. & STARTUP_CTRL1_RST_DONE)
  517. seq_puts(s, "\tReset is done");
  518. else
  519. seq_puts(s, "\tReset is NOT done");
  520. DBGFS_DUMP(HQVDP_MBX_STARTUP_CTRL2);
  521. if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL2)
  522. & STARTUP_CTRL2_FETCH_EN)
  523. seq_puts(s, "\tFetch is enabled");
  524. else
  525. seq_puts(s, "\tFetch is NOT enabled");
  526. DBGFS_DUMP(HQVDP_MBX_GP_STATUS);
  527. DBGFS_DUMP(HQVDP_MBX_NEXT_CMD);
  528. DBGFS_DUMP(HQVDP_MBX_CURRENT_CMD);
  529. DBGFS_DUMP(HQVDP_MBX_SOFT_VSYNC);
  530. if (!(readl(hqvdp->regs + HQVDP_MBX_SOFT_VSYNC) & 3))
  531. seq_puts(s, "\tHW Vsync");
  532. else
  533. seq_puts(s, "\tSW Vsync ?!?!");
  534. /* Last command */
  535. cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
  536. cmd_offset = sti_hqvdp_get_curr_cmd(hqvdp);
  537. if (cmd_offset == -1) {
  538. seq_puts(s, "\n\n Last command: unknown");
  539. } else {
  540. virt = hqvdp->hqvdp_cmd + cmd_offset;
  541. seq_printf(s, "\n\n Last command: address @ 0x%x (0x%p)",
  542. cmd, virt);
  543. hqvdp_dbg_dump_cmd(s, (struct sti_hqvdp_cmd *)virt);
  544. }
  545. /* Next command */
  546. cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  547. cmd_offset = sti_hqvdp_get_next_cmd(hqvdp);
  548. if (cmd_offset == -1) {
  549. seq_puts(s, "\n\n Next command: unknown");
  550. } else {
  551. virt = hqvdp->hqvdp_cmd + cmd_offset;
  552. seq_printf(s, "\n\n Next command address: @ 0x%x (0x%p)",
  553. cmd, virt);
  554. hqvdp_dbg_dump_cmd(s, (struct sti_hqvdp_cmd *)virt);
  555. }
  556. seq_puts(s, "\n");
  557. return 0;
  558. }
  559. static struct drm_info_list hqvdp_debugfs_files[] = {
  560. { "hqvdp", hqvdp_dbg_show, 0, NULL },
  561. };
  562. static int hqvdp_debugfs_init(struct sti_hqvdp *hqvdp, struct drm_minor *minor)
  563. {
  564. unsigned int i;
  565. for (i = 0; i < ARRAY_SIZE(hqvdp_debugfs_files); i++)
  566. hqvdp_debugfs_files[i].data = hqvdp;
  567. return drm_debugfs_create_files(hqvdp_debugfs_files,
  568. ARRAY_SIZE(hqvdp_debugfs_files),
  569. minor->debugfs_root, minor);
  570. }
  571. /**
  572. * sti_hqvdp_update_hvsrc
  573. * @orient: horizontal or vertical
  574. * @scale: scaling/zoom factor
  575. * @hvsrc: the structure containing the LUT coef
  576. *
  577. * Update the Y and C Lut coef, as well as the shift param
  578. *
  579. * RETURNS:
  580. * None.
  581. */
  582. static void sti_hqvdp_update_hvsrc(enum sti_hvsrc_orient orient, int scale,
  583. struct sti_hqvdp_hvsrc *hvsrc)
  584. {
  585. const int *coef_c, *coef_y;
  586. int shift_c, shift_y;
  587. /* Get the appropriate coef tables */
  588. if (scale < SCALE_MAX_FOR_LEG_LUT_F) {
  589. coef_y = coef_lut_f_y_legacy;
  590. coef_c = coef_lut_f_c_legacy;
  591. shift_y = SHIFT_LUT_F_Y_LEGACY;
  592. shift_c = SHIFT_LUT_F_C_LEGACY;
  593. } else if (scale < SCALE_MAX_FOR_LEG_LUT_E) {
  594. coef_y = coef_lut_e_y_legacy;
  595. coef_c = coef_lut_e_c_legacy;
  596. shift_y = SHIFT_LUT_E_Y_LEGACY;
  597. shift_c = SHIFT_LUT_E_C_LEGACY;
  598. } else if (scale < SCALE_MAX_FOR_LEG_LUT_D) {
  599. coef_y = coef_lut_d_y_legacy;
  600. coef_c = coef_lut_d_c_legacy;
  601. shift_y = SHIFT_LUT_D_Y_LEGACY;
  602. shift_c = SHIFT_LUT_D_C_LEGACY;
  603. } else if (scale < SCALE_MAX_FOR_LEG_LUT_C) {
  604. coef_y = coef_lut_c_y_legacy;
  605. coef_c = coef_lut_c_c_legacy;
  606. shift_y = SHIFT_LUT_C_Y_LEGACY;
  607. shift_c = SHIFT_LUT_C_C_LEGACY;
  608. } else if (scale == SCALE_MAX_FOR_LEG_LUT_C) {
  609. coef_y = coef_c = coef_lut_b;
  610. shift_y = shift_c = SHIFT_LUT_B;
  611. } else {
  612. coef_y = coef_c = coef_lut_a_legacy;
  613. shift_y = shift_c = SHIFT_LUT_A_LEGACY;
  614. }
  615. if (orient == HVSRC_HORI) {
  616. hvsrc->hori_shift = (shift_c << 16) | shift_y;
  617. memcpy(hvsrc->yh_coef, coef_y, sizeof(hvsrc->yh_coef));
  618. memcpy(hvsrc->ch_coef, coef_c, sizeof(hvsrc->ch_coef));
  619. } else {
  620. hvsrc->vert_shift = (shift_c << 16) | shift_y;
  621. memcpy(hvsrc->yv_coef, coef_y, sizeof(hvsrc->yv_coef));
  622. memcpy(hvsrc->cv_coef, coef_c, sizeof(hvsrc->cv_coef));
  623. }
  624. }
  625. /**
  626. * sti_hqvdp_check_hw_scaling
  627. * @hqvdp: hqvdp pointer
  628. * @mode: display mode with timing constraints
  629. * @src_w: source width
  630. * @src_h: source height
  631. * @dst_w: destination width
  632. * @dst_h: destination height
  633. *
  634. * Check if the HW is able to perform the scaling request
  635. * The firmware scaling limitation is "CEIL(1/Zy) <= FLOOR(LFW)" where:
  636. * Zy = OutputHeight / InputHeight
  637. * LFW = (Tx * IPClock) / (MaxNbCycles * Cp)
  638. * Tx : Total video mode horizontal resolution
  639. * IPClock : HQVDP IP clock (Mhz)
  640. * MaxNbCycles: max(InputWidth, OutputWidth)
  641. * Cp: Video mode pixel clock (Mhz)
  642. *
  643. * RETURNS:
  644. * True if the HW can scale.
  645. */
  646. static bool sti_hqvdp_check_hw_scaling(struct sti_hqvdp *hqvdp,
  647. struct drm_display_mode *mode,
  648. int src_w, int src_h,
  649. int dst_w, int dst_h)
  650. {
  651. unsigned long lfw;
  652. unsigned int inv_zy;
  653. lfw = mode->htotal * (clk_get_rate(hqvdp->clk) / 1000000);
  654. lfw /= max(src_w, dst_w) * mode->clock / 1000;
  655. inv_zy = DIV_ROUND_UP(src_h, dst_h);
  656. return (inv_zy <= lfw) ? true : false;
  657. }
  658. /**
  659. * sti_hqvdp_disable
  660. * @hqvdp: hqvdp pointer
  661. *
  662. * Disables the HQVDP plane
  663. */
  664. static void sti_hqvdp_disable(struct sti_hqvdp *hqvdp)
  665. {
  666. int i;
  667. DRM_DEBUG_DRIVER("%s\n", sti_plane_to_str(&hqvdp->plane));
  668. /* Unregister VTG Vsync callback */
  669. if (sti_vtg_unregister_client(hqvdp->vtg, &hqvdp->vtg_nb))
  670. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  671. /* Set next cmd to NULL */
  672. writel(0, hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  673. for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
  674. if (readl(hqvdp->regs + HQVDP_MBX_INFO_XP70)
  675. & INFO_XP70_FW_READY)
  676. break;
  677. msleep(POLL_DELAY_MS);
  678. }
  679. /* VTG can stop now */
  680. clk_disable_unprepare(hqvdp->clk_pix_main);
  681. if (i == POLL_MAX_ATTEMPT)
  682. DRM_ERROR("XP70 could not revert to idle\n");
  683. hqvdp->plane.status = STI_PLANE_DISABLED;
  684. }
  685. /**
  686. * sti_vdp_vtg_cb
  687. * @nb: notifier block
  688. * @evt: event message
  689. * @data: private data
  690. *
  691. * Handle VTG Vsync event, display pending bottom field
  692. *
  693. * RETURNS:
  694. * 0 on success.
  695. */
  696. int sti_hqvdp_vtg_cb(struct notifier_block *nb, unsigned long evt, void *data)
  697. {
  698. struct sti_hqvdp *hqvdp = container_of(nb, struct sti_hqvdp, vtg_nb);
  699. int btm_cmd_offset, top_cmd_offest;
  700. struct sti_hqvdp_cmd *btm_cmd, *top_cmd;
  701. if ((evt != VTG_TOP_FIELD_EVENT) && (evt != VTG_BOTTOM_FIELD_EVENT)) {
  702. DRM_DEBUG_DRIVER("Unknown event\n");
  703. return 0;
  704. }
  705. if (hqvdp->plane.status == STI_PLANE_FLUSHING) {
  706. /* disable need to be synchronize on vsync event */
  707. DRM_DEBUG_DRIVER("Vsync event received => disable %s\n",
  708. sti_plane_to_str(&hqvdp->plane));
  709. sti_hqvdp_disable(hqvdp);
  710. }
  711. if (hqvdp->btm_field_pending) {
  712. /* Create the btm field command from the current one */
  713. btm_cmd_offset = sti_hqvdp_get_free_cmd(hqvdp);
  714. top_cmd_offest = sti_hqvdp_get_curr_cmd(hqvdp);
  715. if ((btm_cmd_offset == -1) || (top_cmd_offest == -1)) {
  716. DRM_DEBUG_DRIVER("Warning: no cmd, will skip field\n");
  717. return -EBUSY;
  718. }
  719. btm_cmd = hqvdp->hqvdp_cmd + btm_cmd_offset;
  720. top_cmd = hqvdp->hqvdp_cmd + top_cmd_offest;
  721. memcpy(btm_cmd, top_cmd, sizeof(*btm_cmd));
  722. btm_cmd->top.config = TOP_CONFIG_INTER_BTM;
  723. btm_cmd->top.current_luma +=
  724. btm_cmd->top.luma_src_pitch / 2;
  725. btm_cmd->top.current_chroma +=
  726. btm_cmd->top.chroma_src_pitch / 2;
  727. /* Post the command to mailbox */
  728. writel(hqvdp->hqvdp_cmd_paddr + btm_cmd_offset,
  729. hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  730. hqvdp->btm_field_pending = false;
  731. dev_dbg(hqvdp->dev, "%s Posted command:0x%x\n",
  732. __func__, hqvdp->hqvdp_cmd_paddr);
  733. sti_plane_update_fps(&hqvdp->plane, false, true);
  734. }
  735. return 0;
  736. }
  737. static void sti_hqvdp_init(struct sti_hqvdp *hqvdp)
  738. {
  739. int size;
  740. dma_addr_t dma_addr;
  741. hqvdp->vtg_nb.notifier_call = sti_hqvdp_vtg_cb;
  742. /* Allocate memory for the VDP commands */
  743. size = NB_VDP_CMD * sizeof(struct sti_hqvdp_cmd);
  744. hqvdp->hqvdp_cmd = dma_alloc_wc(hqvdp->dev, size,
  745. &dma_addr,
  746. GFP_KERNEL | GFP_DMA);
  747. if (!hqvdp->hqvdp_cmd) {
  748. DRM_ERROR("Failed to allocate memory for VDP cmd\n");
  749. return;
  750. }
  751. hqvdp->hqvdp_cmd_paddr = (u32)dma_addr;
  752. memset(hqvdp->hqvdp_cmd, 0, size);
  753. }
  754. static void sti_hqvdp_init_plugs(struct sti_hqvdp *hqvdp)
  755. {
  756. /* Configure Plugs (same for RD & WR) */
  757. writel(PLUG_PAGE_SIZE_256, hqvdp->regs + HQVDP_RD_PLUG_PAGE_SIZE);
  758. writel(PLUG_MIN_OPC_8, hqvdp->regs + HQVDP_RD_PLUG_MIN_OPC);
  759. writel(PLUG_MAX_OPC_64, hqvdp->regs + HQVDP_RD_PLUG_MAX_OPC);
  760. writel(PLUG_MAX_CHK_2X, hqvdp->regs + HQVDP_RD_PLUG_MAX_CHK);
  761. writel(PLUG_MAX_MSG_1X, hqvdp->regs + HQVDP_RD_PLUG_MAX_MSG);
  762. writel(PLUG_MIN_SPACE_1, hqvdp->regs + HQVDP_RD_PLUG_MIN_SPACE);
  763. writel(PLUG_CONTROL_ENABLE, hqvdp->regs + HQVDP_RD_PLUG_CONTROL);
  764. writel(PLUG_PAGE_SIZE_256, hqvdp->regs + HQVDP_WR_PLUG_PAGE_SIZE);
  765. writel(PLUG_MIN_OPC_8, hqvdp->regs + HQVDP_WR_PLUG_MIN_OPC);
  766. writel(PLUG_MAX_OPC_64, hqvdp->regs + HQVDP_WR_PLUG_MAX_OPC);
  767. writel(PLUG_MAX_CHK_2X, hqvdp->regs + HQVDP_WR_PLUG_MAX_CHK);
  768. writel(PLUG_MAX_MSG_1X, hqvdp->regs + HQVDP_WR_PLUG_MAX_MSG);
  769. writel(PLUG_MIN_SPACE_1, hqvdp->regs + HQVDP_WR_PLUG_MIN_SPACE);
  770. writel(PLUG_CONTROL_ENABLE, hqvdp->regs + HQVDP_WR_PLUG_CONTROL);
  771. }
  772. /**
  773. * sti_hqvdp_start_xp70
  774. * @hqvdp: hqvdp pointer
  775. *
  776. * Run the xP70 initialization sequence
  777. */
  778. static void sti_hqvdp_start_xp70(struct sti_hqvdp *hqvdp)
  779. {
  780. const struct firmware *firmware;
  781. u32 *fw_rd_plug, *fw_wr_plug, *fw_pmem, *fw_dmem;
  782. u8 *data;
  783. int i;
  784. struct fw_header {
  785. int rd_size;
  786. int wr_size;
  787. int pmem_size;
  788. int dmem_size;
  789. } *header;
  790. DRM_DEBUG_DRIVER("\n");
  791. if (hqvdp->xp70_initialized) {
  792. DRM_DEBUG_DRIVER("HQVDP XP70 already initialized\n");
  793. return;
  794. }
  795. /* Request firmware */
  796. if (request_firmware(&firmware, HQVDP_FMW_NAME, hqvdp->dev)) {
  797. DRM_ERROR("Can't get HQVDP firmware\n");
  798. return;
  799. }
  800. /* Check firmware parts */
  801. if (!firmware) {
  802. DRM_ERROR("Firmware not available\n");
  803. return;
  804. }
  805. header = (struct fw_header *)firmware->data;
  806. if (firmware->size < sizeof(*header)) {
  807. DRM_ERROR("Invalid firmware size (%d)\n", firmware->size);
  808. goto out;
  809. }
  810. if ((sizeof(*header) + header->rd_size + header->wr_size +
  811. header->pmem_size + header->dmem_size) != firmware->size) {
  812. DRM_ERROR("Invalid fmw structure (%d+%d+%d+%d+%d != %d)\n",
  813. sizeof(*header), header->rd_size, header->wr_size,
  814. header->pmem_size, header->dmem_size,
  815. firmware->size);
  816. goto out;
  817. }
  818. data = (u8 *)firmware->data;
  819. data += sizeof(*header);
  820. fw_rd_plug = (void *)data;
  821. data += header->rd_size;
  822. fw_wr_plug = (void *)data;
  823. data += header->wr_size;
  824. fw_pmem = (void *)data;
  825. data += header->pmem_size;
  826. fw_dmem = (void *)data;
  827. /* Enable clock */
  828. if (clk_prepare_enable(hqvdp->clk))
  829. DRM_ERROR("Failed to prepare/enable HQVDP clk\n");
  830. /* Reset */
  831. writel(SW_RESET_CTRL_FULL, hqvdp->regs + HQVDP_MBX_SW_RESET_CTRL);
  832. for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
  833. if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1)
  834. & STARTUP_CTRL1_RST_DONE)
  835. break;
  836. msleep(POLL_DELAY_MS);
  837. }
  838. if (i == POLL_MAX_ATTEMPT) {
  839. DRM_ERROR("Could not reset\n");
  840. goto out;
  841. }
  842. /* Init Read & Write plugs */
  843. for (i = 0; i < header->rd_size / 4; i++)
  844. writel(fw_rd_plug[i], hqvdp->regs + HQVDP_RD_PLUG + i * 4);
  845. for (i = 0; i < header->wr_size / 4; i++)
  846. writel(fw_wr_plug[i], hqvdp->regs + HQVDP_WR_PLUG + i * 4);
  847. sti_hqvdp_init_plugs(hqvdp);
  848. /* Authorize Idle Mode */
  849. writel(STARTUP_CTRL1_AUTH_IDLE, hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1);
  850. /* Prevent VTG interruption during the boot */
  851. writel(SOFT_VSYNC_SW_CTRL_IRQ, hqvdp->regs + HQVDP_MBX_SOFT_VSYNC);
  852. writel(0, hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  853. /* Download PMEM & DMEM */
  854. for (i = 0; i < header->pmem_size / 4; i++)
  855. writel(fw_pmem[i], hqvdp->regs + HQVDP_PMEM + i * 4);
  856. for (i = 0; i < header->dmem_size / 4; i++)
  857. writel(fw_dmem[i], hqvdp->regs + HQVDP_DMEM + i * 4);
  858. /* Enable fetch */
  859. writel(STARTUP_CTRL2_FETCH_EN, hqvdp->regs + HQVDP_MBX_STARTUP_CTRL2);
  860. /* Wait end of boot */
  861. for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
  862. if (readl(hqvdp->regs + HQVDP_MBX_INFO_XP70)
  863. & INFO_XP70_FW_READY)
  864. break;
  865. msleep(POLL_DELAY_MS);
  866. }
  867. if (i == POLL_MAX_ATTEMPT) {
  868. DRM_ERROR("Could not boot\n");
  869. goto out;
  870. }
  871. /* Launch Vsync */
  872. writel(SOFT_VSYNC_HW, hqvdp->regs + HQVDP_MBX_SOFT_VSYNC);
  873. DRM_INFO("HQVDP XP70 initialized\n");
  874. hqvdp->xp70_initialized = true;
  875. out:
  876. release_firmware(firmware);
  877. }
  878. static int sti_hqvdp_atomic_check(struct drm_plane *drm_plane,
  879. struct drm_plane_state *state)
  880. {
  881. struct sti_plane *plane = to_sti_plane(drm_plane);
  882. struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
  883. struct drm_crtc *crtc = state->crtc;
  884. struct drm_framebuffer *fb = state->fb;
  885. bool first_prepare = plane->status == STI_PLANE_DISABLED ? true : false;
  886. struct drm_crtc_state *crtc_state;
  887. struct drm_display_mode *mode;
  888. int dst_x, dst_y, dst_w, dst_h;
  889. int src_x, src_y, src_w, src_h;
  890. /* no need for further checks if the plane is being disabled */
  891. if (!crtc || !fb)
  892. return 0;
  893. crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
  894. mode = &crtc_state->mode;
  895. dst_x = state->crtc_x;
  896. dst_y = state->crtc_y;
  897. dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
  898. dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
  899. /* src_x are in 16.16 format */
  900. src_x = state->src_x >> 16;
  901. src_y = state->src_y >> 16;
  902. src_w = state->src_w >> 16;
  903. src_h = state->src_h >> 16;
  904. if (!sti_hqvdp_check_hw_scaling(hqvdp, mode,
  905. src_w, src_h,
  906. dst_w, dst_h)) {
  907. DRM_ERROR("Scaling beyond HW capabilities\n");
  908. return -EINVAL;
  909. }
  910. if (!drm_fb_cma_get_gem_obj(fb, 0)) {
  911. DRM_ERROR("Can't get CMA GEM object for fb\n");
  912. return -EINVAL;
  913. }
  914. /*
  915. * Input / output size
  916. * Align to upper even value
  917. */
  918. dst_w = ALIGN(dst_w, 2);
  919. dst_h = ALIGN(dst_h, 2);
  920. if ((src_w > MAX_WIDTH) || (src_w < MIN_WIDTH) ||
  921. (src_h > MAX_HEIGHT) || (src_h < MIN_HEIGHT) ||
  922. (dst_w > MAX_WIDTH) || (dst_w < MIN_WIDTH) ||
  923. (dst_h > MAX_HEIGHT) || (dst_h < MIN_HEIGHT)) {
  924. DRM_ERROR("Invalid in/out size %dx%d -> %dx%d\n",
  925. src_w, src_h,
  926. dst_w, dst_h);
  927. return -EINVAL;
  928. }
  929. if (first_prepare) {
  930. /* Start HQVDP XP70 coprocessor */
  931. sti_hqvdp_start_xp70(hqvdp);
  932. /* Prevent VTG shutdown */
  933. if (clk_prepare_enable(hqvdp->clk_pix_main)) {
  934. DRM_ERROR("Failed to prepare/enable pix main clk\n");
  935. return -EINVAL;
  936. }
  937. /* Register VTG Vsync callback to handle bottom fields */
  938. if (sti_vtg_register_client(hqvdp->vtg,
  939. &hqvdp->vtg_nb,
  940. crtc)) {
  941. DRM_ERROR("Cannot register VTG notifier\n");
  942. return -EINVAL;
  943. }
  944. }
  945. DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
  946. crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)),
  947. drm_plane->base.id, sti_plane_to_str(plane));
  948. DRM_DEBUG_KMS("%s dst=(%dx%d)@(%d,%d) - src=(%dx%d)@(%d,%d)\n",
  949. sti_plane_to_str(plane),
  950. dst_w, dst_h, dst_x, dst_y,
  951. src_w, src_h, src_x, src_y);
  952. return 0;
  953. }
  954. static void sti_hqvdp_atomic_update(struct drm_plane *drm_plane,
  955. struct drm_plane_state *oldstate)
  956. {
  957. struct drm_plane_state *state = drm_plane->state;
  958. struct sti_plane *plane = to_sti_plane(drm_plane);
  959. struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
  960. struct drm_crtc *crtc = state->crtc;
  961. struct drm_framebuffer *fb = state->fb;
  962. struct drm_display_mode *mode;
  963. int dst_x, dst_y, dst_w, dst_h;
  964. int src_x, src_y, src_w, src_h;
  965. struct drm_gem_cma_object *cma_obj;
  966. struct sti_hqvdp_cmd *cmd;
  967. int scale_h, scale_v;
  968. int cmd_offset;
  969. if (!crtc || !fb)
  970. return;
  971. mode = &crtc->mode;
  972. dst_x = state->crtc_x;
  973. dst_y = state->crtc_y;
  974. dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
  975. dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
  976. /* src_x are in 16.16 format */
  977. src_x = state->src_x >> 16;
  978. src_y = state->src_y >> 16;
  979. src_w = state->src_w >> 16;
  980. src_h = state->src_h >> 16;
  981. cmd_offset = sti_hqvdp_get_free_cmd(hqvdp);
  982. if (cmd_offset == -1) {
  983. DRM_DEBUG_DRIVER("Warning: no cmd, will skip frame\n");
  984. return;
  985. }
  986. cmd = hqvdp->hqvdp_cmd + cmd_offset;
  987. /* Static parameters, defaulting to progressive mode */
  988. cmd->top.config = TOP_CONFIG_PROGRESSIVE;
  989. cmd->top.mem_format = TOP_MEM_FORMAT_DFLT;
  990. cmd->hvsrc.param_ctrl = HVSRC_PARAM_CTRL_DFLT;
  991. cmd->csdi.config = CSDI_CONFIG_PROG;
  992. /* VC1RE, FMD bypassed : keep everything set to 0
  993. * IQI/P2I bypassed */
  994. cmd->iqi.config = IQI_CONFIG_DFLT;
  995. cmd->iqi.con_bri = IQI_CON_BRI_DFLT;
  996. cmd->iqi.sat_gain = IQI_SAT_GAIN_DFLT;
  997. cmd->iqi.pxf_conf = IQI_PXF_CONF_DFLT;
  998. cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  999. DRM_DEBUG_DRIVER("drm FB:%d format:%.4s phys@:0x%lx\n", fb->base.id,
  1000. (char *)&fb->pixel_format,
  1001. (unsigned long)cma_obj->paddr);
  1002. /* Buffer planes address */
  1003. cmd->top.current_luma = (u32)cma_obj->paddr + fb->offsets[0];
  1004. cmd->top.current_chroma = (u32)cma_obj->paddr + fb->offsets[1];
  1005. /* Pitches */
  1006. cmd->top.luma_processed_pitch = fb->pitches[0];
  1007. cmd->top.luma_src_pitch = fb->pitches[0];
  1008. cmd->top.chroma_processed_pitch = fb->pitches[1];
  1009. cmd->top.chroma_src_pitch = fb->pitches[1];
  1010. /* Input / output size
  1011. * Align to upper even value */
  1012. dst_w = ALIGN(dst_w, 2);
  1013. dst_h = ALIGN(dst_h, 2);
  1014. cmd->top.input_viewport_size = src_h << 16 | src_w;
  1015. cmd->top.input_frame_size = src_h << 16 | src_w;
  1016. cmd->hvsrc.output_picture_size = dst_h << 16 | dst_w;
  1017. cmd->top.input_viewport_ori = src_y << 16 | src_x;
  1018. /* Handle interlaced */
  1019. if (fb->flags & DRM_MODE_FB_INTERLACED) {
  1020. /* Top field to display */
  1021. cmd->top.config = TOP_CONFIG_INTER_TOP;
  1022. /* Update pitches and vert size */
  1023. cmd->top.input_frame_size = (src_h / 2) << 16 | src_w;
  1024. cmd->top.luma_processed_pitch *= 2;
  1025. cmd->top.luma_src_pitch *= 2;
  1026. cmd->top.chroma_processed_pitch *= 2;
  1027. cmd->top.chroma_src_pitch *= 2;
  1028. /* Enable directional deinterlacing processing */
  1029. cmd->csdi.config = CSDI_CONFIG_INTER_DIR;
  1030. cmd->csdi.config2 = CSDI_CONFIG2_DFLT;
  1031. cmd->csdi.dcdi_config = CSDI_DCDI_CONFIG_DFLT;
  1032. }
  1033. /* Update hvsrc lut coef */
  1034. scale_h = SCALE_FACTOR * dst_w / src_w;
  1035. sti_hqvdp_update_hvsrc(HVSRC_HORI, scale_h, &cmd->hvsrc);
  1036. scale_v = SCALE_FACTOR * dst_h / src_h;
  1037. sti_hqvdp_update_hvsrc(HVSRC_VERT, scale_v, &cmd->hvsrc);
  1038. writel(hqvdp->hqvdp_cmd_paddr + cmd_offset,
  1039. hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  1040. /* Interlaced : get ready to display the bottom field at next Vsync */
  1041. if (fb->flags & DRM_MODE_FB_INTERLACED)
  1042. hqvdp->btm_field_pending = true;
  1043. dev_dbg(hqvdp->dev, "%s Posted command:0x%x\n",
  1044. __func__, hqvdp->hqvdp_cmd_paddr + cmd_offset);
  1045. sti_plane_update_fps(plane, true, true);
  1046. plane->status = STI_PLANE_UPDATED;
  1047. }
  1048. static void sti_hqvdp_atomic_disable(struct drm_plane *drm_plane,
  1049. struct drm_plane_state *oldstate)
  1050. {
  1051. struct sti_plane *plane = to_sti_plane(drm_plane);
  1052. if (!drm_plane->crtc) {
  1053. DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
  1054. drm_plane->base.id);
  1055. return;
  1056. }
  1057. DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
  1058. drm_plane->crtc->base.id,
  1059. sti_mixer_to_str(to_sti_mixer(drm_plane->crtc)),
  1060. drm_plane->base.id, sti_plane_to_str(plane));
  1061. plane->status = STI_PLANE_DISABLING;
  1062. }
  1063. static const struct drm_plane_helper_funcs sti_hqvdp_helpers_funcs = {
  1064. .atomic_check = sti_hqvdp_atomic_check,
  1065. .atomic_update = sti_hqvdp_atomic_update,
  1066. .atomic_disable = sti_hqvdp_atomic_disable,
  1067. };
  1068. static void sti_hqvdp_destroy(struct drm_plane *drm_plane)
  1069. {
  1070. DRM_DEBUG_DRIVER("\n");
  1071. drm_plane_helper_disable(drm_plane);
  1072. drm_plane_cleanup(drm_plane);
  1073. }
  1074. static int sti_hqvdp_late_register(struct drm_plane *drm_plane)
  1075. {
  1076. struct sti_plane *plane = to_sti_plane(drm_plane);
  1077. struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
  1078. return hqvdp_debugfs_init(hqvdp, drm_plane->dev->primary);
  1079. }
  1080. struct drm_plane_funcs sti_hqvdp_plane_helpers_funcs = {
  1081. .update_plane = drm_atomic_helper_update_plane,
  1082. .disable_plane = drm_atomic_helper_disable_plane,
  1083. .destroy = sti_hqvdp_destroy,
  1084. .set_property = drm_atomic_helper_plane_set_property,
  1085. .reset = sti_plane_reset,
  1086. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  1087. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  1088. .late_register = sti_hqvdp_late_register,
  1089. };
  1090. static struct drm_plane *sti_hqvdp_create(struct drm_device *drm_dev,
  1091. struct device *dev, int desc)
  1092. {
  1093. struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
  1094. int res;
  1095. hqvdp->plane.desc = desc;
  1096. hqvdp->plane.status = STI_PLANE_DISABLED;
  1097. sti_hqvdp_init(hqvdp);
  1098. res = drm_universal_plane_init(drm_dev, &hqvdp->plane.drm_plane, 1,
  1099. &sti_hqvdp_plane_helpers_funcs,
  1100. hqvdp_supported_formats,
  1101. ARRAY_SIZE(hqvdp_supported_formats),
  1102. DRM_PLANE_TYPE_OVERLAY, NULL);
  1103. if (res) {
  1104. DRM_ERROR("Failed to initialize universal plane\n");
  1105. return NULL;
  1106. }
  1107. drm_plane_helper_add(&hqvdp->plane.drm_plane, &sti_hqvdp_helpers_funcs);
  1108. sti_plane_init_property(&hqvdp->plane, DRM_PLANE_TYPE_OVERLAY);
  1109. return &hqvdp->plane.drm_plane;
  1110. }
  1111. int sti_hqvdp_bind(struct device *dev, struct device *master, void *data)
  1112. {
  1113. struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
  1114. struct drm_device *drm_dev = data;
  1115. struct drm_plane *plane;
  1116. DRM_DEBUG_DRIVER("\n");
  1117. hqvdp->drm_dev = drm_dev;
  1118. /* Create HQVDP plane once xp70 is initialized */
  1119. plane = sti_hqvdp_create(drm_dev, hqvdp->dev, STI_HQVDP_0);
  1120. if (!plane)
  1121. DRM_ERROR("Can't create HQVDP plane\n");
  1122. return 0;
  1123. }
  1124. static void sti_hqvdp_unbind(struct device *dev,
  1125. struct device *master, void *data)
  1126. {
  1127. /* do nothing */
  1128. }
  1129. static const struct component_ops sti_hqvdp_ops = {
  1130. .bind = sti_hqvdp_bind,
  1131. .unbind = sti_hqvdp_unbind,
  1132. };
  1133. static int sti_hqvdp_probe(struct platform_device *pdev)
  1134. {
  1135. struct device *dev = &pdev->dev;
  1136. struct device_node *vtg_np;
  1137. struct sti_hqvdp *hqvdp;
  1138. struct resource *res;
  1139. DRM_DEBUG_DRIVER("\n");
  1140. hqvdp = devm_kzalloc(dev, sizeof(*hqvdp), GFP_KERNEL);
  1141. if (!hqvdp) {
  1142. DRM_ERROR("Failed to allocate HQVDP context\n");
  1143. return -ENOMEM;
  1144. }
  1145. hqvdp->dev = dev;
  1146. /* Get Memory resources */
  1147. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1148. if (res == NULL) {
  1149. DRM_ERROR("Get memory resource failed\n");
  1150. return -ENXIO;
  1151. }
  1152. hqvdp->regs = devm_ioremap(dev, res->start, resource_size(res));
  1153. if (hqvdp->regs == NULL) {
  1154. DRM_ERROR("Register mapping failed\n");
  1155. return -ENXIO;
  1156. }
  1157. /* Get clock resources */
  1158. hqvdp->clk = devm_clk_get(dev, "hqvdp");
  1159. hqvdp->clk_pix_main = devm_clk_get(dev, "pix_main");
  1160. if (IS_ERR(hqvdp->clk) || IS_ERR(hqvdp->clk_pix_main)) {
  1161. DRM_ERROR("Cannot get clocks\n");
  1162. return -ENXIO;
  1163. }
  1164. /* Get reset resources */
  1165. hqvdp->reset = devm_reset_control_get(dev, "hqvdp");
  1166. if (!IS_ERR(hqvdp->reset))
  1167. reset_control_deassert(hqvdp->reset);
  1168. vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0);
  1169. if (vtg_np)
  1170. hqvdp->vtg = of_vtg_find(vtg_np);
  1171. of_node_put(vtg_np);
  1172. platform_set_drvdata(pdev, hqvdp);
  1173. return component_add(&pdev->dev, &sti_hqvdp_ops);
  1174. }
  1175. static int sti_hqvdp_remove(struct platform_device *pdev)
  1176. {
  1177. component_del(&pdev->dev, &sti_hqvdp_ops);
  1178. return 0;
  1179. }
  1180. static struct of_device_id hqvdp_of_match[] = {
  1181. { .compatible = "st,stih407-hqvdp", },
  1182. { /* end node */ }
  1183. };
  1184. MODULE_DEVICE_TABLE(of, hqvdp_of_match);
  1185. struct platform_driver sti_hqvdp_driver = {
  1186. .driver = {
  1187. .name = "sti-hqvdp",
  1188. .owner = THIS_MODULE,
  1189. .of_match_table = hqvdp_of_match,
  1190. },
  1191. .probe = sti_hqvdp_probe,
  1192. .remove = sti_hqvdp_remove,
  1193. };
  1194. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  1195. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  1196. MODULE_LICENSE("GPL");