sti_hqvdp.c 40 KB

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