sti_hqvdp.c 39 KB

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