sti_hqvdp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 struct drm_plane *sti_hqvdp_create(struct drm_device *drm_dev,
  1069. struct device *dev, int desc)
  1070. {
  1071. struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
  1072. int res;
  1073. hqvdp->plane.desc = desc;
  1074. hqvdp->plane.status = STI_PLANE_DISABLED;
  1075. sti_hqvdp_init(hqvdp);
  1076. res = drm_universal_plane_init(drm_dev, &hqvdp->plane.drm_plane, 1,
  1077. &sti_plane_helpers_funcs,
  1078. hqvdp_supported_formats,
  1079. ARRAY_SIZE(hqvdp_supported_formats),
  1080. DRM_PLANE_TYPE_OVERLAY, NULL);
  1081. if (res) {
  1082. DRM_ERROR("Failed to initialize universal plane\n");
  1083. return NULL;
  1084. }
  1085. drm_plane_helper_add(&hqvdp->plane.drm_plane, &sti_hqvdp_helpers_funcs);
  1086. sti_plane_init_property(&hqvdp->plane, DRM_PLANE_TYPE_OVERLAY);
  1087. if (hqvdp_debugfs_init(hqvdp, drm_dev->primary))
  1088. DRM_ERROR("HQVDP debugfs setup failed\n");
  1089. return &hqvdp->plane.drm_plane;
  1090. }
  1091. int sti_hqvdp_bind(struct device *dev, struct device *master, void *data)
  1092. {
  1093. struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
  1094. struct drm_device *drm_dev = data;
  1095. struct drm_plane *plane;
  1096. DRM_DEBUG_DRIVER("\n");
  1097. hqvdp->drm_dev = drm_dev;
  1098. /* Create HQVDP plane once xp70 is initialized */
  1099. plane = sti_hqvdp_create(drm_dev, hqvdp->dev, STI_HQVDP_0);
  1100. if (!plane)
  1101. DRM_ERROR("Can't create HQVDP plane\n");
  1102. return 0;
  1103. }
  1104. static void sti_hqvdp_unbind(struct device *dev,
  1105. struct device *master, void *data)
  1106. {
  1107. /* do nothing */
  1108. }
  1109. static const struct component_ops sti_hqvdp_ops = {
  1110. .bind = sti_hqvdp_bind,
  1111. .unbind = sti_hqvdp_unbind,
  1112. };
  1113. static int sti_hqvdp_probe(struct platform_device *pdev)
  1114. {
  1115. struct device *dev = &pdev->dev;
  1116. struct device_node *vtg_np;
  1117. struct sti_hqvdp *hqvdp;
  1118. struct resource *res;
  1119. DRM_DEBUG_DRIVER("\n");
  1120. hqvdp = devm_kzalloc(dev, sizeof(*hqvdp), GFP_KERNEL);
  1121. if (!hqvdp) {
  1122. DRM_ERROR("Failed to allocate HQVDP context\n");
  1123. return -ENOMEM;
  1124. }
  1125. hqvdp->dev = dev;
  1126. /* Get Memory resources */
  1127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1128. if (res == NULL) {
  1129. DRM_ERROR("Get memory resource failed\n");
  1130. return -ENXIO;
  1131. }
  1132. hqvdp->regs = devm_ioremap(dev, res->start, resource_size(res));
  1133. if (hqvdp->regs == NULL) {
  1134. DRM_ERROR("Register mapping failed\n");
  1135. return -ENXIO;
  1136. }
  1137. /* Get clock resources */
  1138. hqvdp->clk = devm_clk_get(dev, "hqvdp");
  1139. hqvdp->clk_pix_main = devm_clk_get(dev, "pix_main");
  1140. if (IS_ERR(hqvdp->clk) || IS_ERR(hqvdp->clk_pix_main)) {
  1141. DRM_ERROR("Cannot get clocks\n");
  1142. return -ENXIO;
  1143. }
  1144. /* Get reset resources */
  1145. hqvdp->reset = devm_reset_control_get(dev, "hqvdp");
  1146. if (!IS_ERR(hqvdp->reset))
  1147. reset_control_deassert(hqvdp->reset);
  1148. vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0);
  1149. if (vtg_np)
  1150. hqvdp->vtg = of_vtg_find(vtg_np);
  1151. platform_set_drvdata(pdev, hqvdp);
  1152. return component_add(&pdev->dev, &sti_hqvdp_ops);
  1153. }
  1154. static int sti_hqvdp_remove(struct platform_device *pdev)
  1155. {
  1156. component_del(&pdev->dev, &sti_hqvdp_ops);
  1157. return 0;
  1158. }
  1159. static struct of_device_id hqvdp_of_match[] = {
  1160. { .compatible = "st,stih407-hqvdp", },
  1161. { /* end node */ }
  1162. };
  1163. MODULE_DEVICE_TABLE(of, hqvdp_of_match);
  1164. struct platform_driver sti_hqvdp_driver = {
  1165. .driver = {
  1166. .name = "sti-hqvdp",
  1167. .owner = THIS_MODULE,
  1168. .of_match_table = hqvdp_of_match,
  1169. },
  1170. .probe = sti_hqvdp_probe,
  1171. .remove = sti_hqvdp_remove,
  1172. };
  1173. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  1174. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  1175. MODULE_LICENSE("GPL");