2 * Copyright (C) STMicroelectronics SA 2015
3 * Authors: Yannick Fertre <yannick.fertre@st.com>
4 * Hugues Fruchet <hugues.fruchet@st.com>
5 * License terms: GNU General Public License (GPL), version 2
9 #include <linux/interrupt.h>
10 #include <linux/platform_device.h>
11 #include <linux/pm_runtime.h>
16 /* HVA register offsets */
17 #define HVA_HIF_REG_RST 0x0100U
18 #define HVA_HIF_REG_RST_ACK 0x0104U
19 #define HVA_HIF_REG_MIF_CFG 0x0108U
20 #define HVA_HIF_REG_HEC_MIF_CFG 0x010CU
21 #define HVA_HIF_REG_CFL 0x0110U
22 #define HVA_HIF_FIFO_CMD 0x0114U
23 #define HVA_HIF_FIFO_STS 0x0118U
24 #define HVA_HIF_REG_SFL 0x011CU
25 #define HVA_HIF_REG_IT_ACK 0x0120U
26 #define HVA_HIF_REG_ERR_IT_ACK 0x0124U
27 #define HVA_HIF_REG_LMI_ERR 0x0128U
28 #define HVA_HIF_REG_EMI_ERR 0x012CU
29 #define HVA_HIF_REG_HEC_MIF_ERR 0x0130U
30 #define HVA_HIF_REG_HEC_STS 0x0134U
31 #define HVA_HIF_REG_HVC_STS 0x0138U
32 #define HVA_HIF_REG_HJE_STS 0x013CU
33 #define HVA_HIF_REG_CNT 0x0140U
34 #define HVA_HIF_REG_HEC_CHKSYN_DIS 0x0144U
35 #define HVA_HIF_REG_CLK_GATING 0x0148U
36 #define HVA_HIF_REG_VERSION 0x014CU
37 #define HVA_HIF_REG_BSM 0x0150U
39 /* define value for version id register (HVA_HIF_REG_VERSION) */
40 #define VERSION_ID_MASK 0x0000FFFF
42 /* define values for BSM register (HVA_HIF_REG_BSM) */
43 #define BSM_CFG_VAL1 0x0003F000
44 #define BSM_CFG_VAL2 0x003F0000
46 /* define values for memory interface register (HVA_HIF_REG_MIF_CFG) */
47 #define MIF_CFG_VAL1 0x04460446
48 #define MIF_CFG_VAL2 0x04460806
49 #define MIF_CFG_VAL3 0x00000000
51 /* define value for HEC memory interface register (HVA_HIF_REG_MIF_CFG) */
52 #define HEC_MIF_CFG_VAL 0x000000C4
54 /* Bits definition for clock gating register (HVA_HIF_REG_CLK_GATING) */
55 #define CLK_GATING_HVC BIT(0)
56 #define CLK_GATING_HEC BIT(1)
57 #define CLK_GATING_HJE BIT(2)
59 /* fix hva clock rate */
60 #define CLK_RATE 300000000
62 /* fix delay for pmruntime */
63 #define AUTOSUSPEND_DELAY_MS 3
66 * hw encode error values
67 * NO_ERROR: Success, Task OK
68 * H264_BITSTREAM_OVERSIZE: VECH264 Bitstream size > bitstream buffer
69 * H264_FRAME_SKIPPED: VECH264 Frame skipped (refers to CPB Buffer Size)
70 * H264_SLICE_LIMIT_SIZE: VECH264 MB > slice limit size
71 * H264_MAX_SLICE_NUMBER: VECH264 max slice number reached
72 * H264_SLICE_READY: VECH264 Slice ready
73 * TASK_LIST_FULL: HVA/FPC task list full
74 (discard latest transform command)
75 * UNKNOWN_COMMAND: Transform command not known by HVA/FPC
76 * WRONG_CODEC_OR_RESOLUTION: Wrong Codec or Resolution Selection
77 * NO_INT_COMPLETION: Time-out on interrupt completion
78 * LMI_ERR: Local Memory Interface Error
79 * EMI_ERR: External Memory Interface Error
80 * HECMI_ERR: HEC Memory Interface Error
84 H264_BITSTREAM_OVERSIZE
= 0x2,
85 H264_FRAME_SKIPPED
= 0x4,
86 H264_SLICE_LIMIT_SIZE
= 0x5,
87 H264_MAX_SLICE_NUMBER
= 0x7,
88 H264_SLICE_READY
= 0x8,
89 TASK_LIST_FULL
= 0xF0,
90 UNKNOWN_COMMAND
= 0xF1,
91 WRONG_CODEC_OR_RESOLUTION
= 0xF4,
92 NO_INT_COMPLETION
= 0x100,
98 static irqreturn_t
hva_hw_its_interrupt(int irq
, void *data
)
100 struct hva_dev
*hva
= data
;
102 /* read status registers */
103 hva
->sts_reg
= readl_relaxed(hva
->regs
+ HVA_HIF_FIFO_STS
);
104 hva
->sfl_reg
= readl_relaxed(hva
->regs
+ HVA_HIF_REG_SFL
);
106 /* acknowledge interruption */
107 writel_relaxed(0x1, hva
->regs
+ HVA_HIF_REG_IT_ACK
);
109 return IRQ_WAKE_THREAD
;
112 static irqreturn_t
hva_hw_its_irq_thread(int irq
, void *arg
)
114 struct hva_dev
*hva
= arg
;
115 struct device
*dev
= hva_to_dev(hva
);
116 u32 status
= hva
->sts_reg
& 0xFF;
118 struct hva_ctx
*ctx
= NULL
;
120 dev_dbg(dev
, "%s %s: status: 0x%02x fifo level: 0x%02x\n",
121 HVA_PREFIX
, __func__
, hva
->sts_reg
& 0xFF, hva
->sfl_reg
& 0xF);
124 * status: task_id[31:16] client_id[15:8] status[7:0]
125 * the context identifier is retrieved from the client identifier
127 ctx_id
= (hva
->sts_reg
& 0xFF00) >> 8;
128 if (ctx_id
>= HVA_MAX_INSTANCES
) {
129 dev_err(dev
, "%s %s: bad context identifier: %d\n",
130 ctx
->name
, __func__
, ctx_id
);
135 ctx
= hva
->instances
[ctx_id
];
141 dev_dbg(dev
, "%s %s: no error\n",
142 ctx
->name
, __func__
);
145 case H264_SLICE_READY
:
146 dev_dbg(dev
, "%s %s: h264 slice ready\n",
147 ctx
->name
, __func__
);
150 case H264_FRAME_SKIPPED
:
151 dev_dbg(dev
, "%s %s: h264 frame skipped\n",
152 ctx
->name
, __func__
);
155 case H264_BITSTREAM_OVERSIZE
:
156 dev_err(dev
, "%s %s:h264 bitstream oversize\n",
157 ctx
->name
, __func__
);
160 case H264_SLICE_LIMIT_SIZE
:
161 dev_err(dev
, "%s %s: h264 slice limit size is reached\n",
162 ctx
->name
, __func__
);
165 case H264_MAX_SLICE_NUMBER
:
166 dev_err(dev
, "%s %s: h264 max slice number is reached\n",
167 ctx
->name
, __func__
);
171 dev_err(dev
, "%s %s:task list full\n",
172 ctx
->name
, __func__
);
175 case UNKNOWN_COMMAND
:
176 dev_err(dev
, "%s %s: command not known\n",
177 ctx
->name
, __func__
);
180 case WRONG_CODEC_OR_RESOLUTION
:
181 dev_err(dev
, "%s %s: wrong codec or resolution\n",
182 ctx
->name
, __func__
);
186 dev_err(dev
, "%s %s: status not recognized\n",
187 ctx
->name
, __func__
);
192 complete(&hva
->interrupt
);
197 static irqreturn_t
hva_hw_err_interrupt(int irq
, void *data
)
199 struct hva_dev
*hva
= data
;
201 /* read status registers */
202 hva
->sts_reg
= readl_relaxed(hva
->regs
+ HVA_HIF_FIFO_STS
);
203 hva
->sfl_reg
= readl_relaxed(hva
->regs
+ HVA_HIF_REG_SFL
);
205 /* read error registers */
206 hva
->lmi_err_reg
= readl_relaxed(hva
->regs
+ HVA_HIF_REG_LMI_ERR
);
207 hva
->emi_err_reg
= readl_relaxed(hva
->regs
+ HVA_HIF_REG_EMI_ERR
);
208 hva
->hec_mif_err_reg
= readl_relaxed(hva
->regs
+
209 HVA_HIF_REG_HEC_MIF_ERR
);
211 /* acknowledge interruption */
212 writel_relaxed(0x1, hva
->regs
+ HVA_HIF_REG_IT_ACK
);
214 return IRQ_WAKE_THREAD
;
217 static irqreturn_t
hva_hw_err_irq_thread(int irq
, void *arg
)
219 struct hva_dev
*hva
= arg
;
220 struct device
*dev
= hva_to_dev(hva
);
224 dev_dbg(dev
, "%s status: 0x%02x fifo level: 0x%02x\n",
225 HVA_PREFIX
, hva
->sts_reg
& 0xFF, hva
->sfl_reg
& 0xF);
228 * status: task_id[31:16] client_id[15:8] status[7:0]
229 * the context identifier is retrieved from the client identifier
231 ctx_id
= (hva
->sts_reg
& 0xFF00) >> 8;
232 if (ctx_id
>= HVA_MAX_INSTANCES
) {
233 dev_err(dev
, "%s bad context identifier: %d\n", HVA_PREFIX
,
238 ctx
= hva
->instances
[ctx_id
];
242 if (hva
->lmi_err_reg
) {
243 dev_err(dev
, "%s local memory interface error: 0x%08x\n",
244 ctx
->name
, hva
->lmi_err_reg
);
248 if (hva
->emi_err_reg
) {
249 dev_err(dev
, "%s external memory interface error: 0x%08x\n",
250 ctx
->name
, hva
->emi_err_reg
);
254 if (hva
->hec_mif_err_reg
) {
255 dev_err(dev
, "%s hec memory interface error: 0x%08x\n",
256 ctx
->name
, hva
->hec_mif_err_reg
);
260 complete(&hva
->interrupt
);
265 static unsigned long int hva_hw_get_ip_version(struct hva_dev
*hva
)
267 struct device
*dev
= hva_to_dev(hva
);
268 unsigned long int version
;
270 if (pm_runtime_get_sync(dev
) < 0) {
271 dev_err(dev
, "%s failed to get pm_runtime\n", HVA_PREFIX
);
272 mutex_unlock(&hva
->protect_mutex
);
276 version
= readl_relaxed(hva
->regs
+ HVA_HIF_REG_VERSION
) &
279 pm_runtime_put_autosuspend(dev
);
282 case HVA_VERSION_V400
:
283 dev_dbg(dev
, "%s IP hardware version 0x%lx\n",
284 HVA_PREFIX
, version
);
287 dev_err(dev
, "%s unknown IP hardware version 0x%lx\n",
288 HVA_PREFIX
, version
);
289 version
= HVA_VERSION_UNKNOWN
;
296 int hva_hw_probe(struct platform_device
*pdev
, struct hva_dev
*hva
)
298 struct device
*dev
= &pdev
->dev
;
299 struct resource
*regs
;
300 struct resource
*esram
;
305 /* get memory for registers */
306 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
307 hva
->regs
= devm_ioremap_resource(dev
, regs
);
308 if (IS_ERR(hva
->regs
)) {
309 dev_err(dev
, "%s failed to get regs\n", HVA_PREFIX
);
310 return PTR_ERR(hva
->regs
);
313 /* get memory for esram */
314 esram
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
316 dev_err(dev
, "%s failed to get esram\n", HVA_PREFIX
);
319 hva
->esram_addr
= esram
->start
;
320 hva
->esram_size
= resource_size(esram
);
322 dev_info(dev
, "%s esram reserved for address: 0x%x size:%d\n",
323 HVA_PREFIX
, hva
->esram_addr
, hva
->esram_size
);
325 /* get clock resource */
326 hva
->clk
= devm_clk_get(dev
, "clk_hva");
327 if (IS_ERR(hva
->clk
)) {
328 dev_err(dev
, "%s failed to get clock\n", HVA_PREFIX
);
329 return PTR_ERR(hva
->clk
);
332 ret
= clk_prepare(hva
->clk
);
334 dev_err(dev
, "%s failed to prepare clock\n", HVA_PREFIX
);
335 hva
->clk
= ERR_PTR(-EINVAL
);
339 /* get status interruption resource */
340 ret
= platform_get_irq(pdev
, 0);
342 dev_err(dev
, "%s failed to get status IRQ\n", HVA_PREFIX
);
347 ret
= devm_request_threaded_irq(dev
, hva
->irq_its
, hva_hw_its_interrupt
,
348 hva_hw_its_irq_thread
,
352 dev_err(dev
, "%s failed to install status IRQ 0x%x\n",
353 HVA_PREFIX
, hva
->irq_its
);
356 disable_irq(hva
->irq_its
);
358 /* get error interruption resource */
359 ret
= platform_get_irq(pdev
, 1);
361 dev_err(dev
, "%s failed to get error IRQ\n", HVA_PREFIX
);
366 ret
= devm_request_threaded_irq(dev
, hva
->irq_err
, hva_hw_err_interrupt
,
367 hva_hw_err_irq_thread
,
371 dev_err(dev
, "%s failed to install error IRQ 0x%x\n",
372 HVA_PREFIX
, hva
->irq_err
);
375 disable_irq(hva
->irq_err
);
377 /* initialise protection mutex */
378 mutex_init(&hva
->protect_mutex
);
380 /* initialise completion signal */
381 init_completion(&hva
->interrupt
);
383 /* initialise runtime power management */
384 pm_runtime_set_autosuspend_delay(dev
, AUTOSUSPEND_DELAY_MS
);
385 pm_runtime_use_autosuspend(dev
);
386 pm_runtime_set_suspended(dev
);
387 pm_runtime_enable(dev
);
389 ret
= pm_runtime_get_sync(dev
);
391 dev_err(dev
, "%s failed to set PM\n", HVA_PREFIX
);
395 /* check IP hardware version */
396 hva
->ip_version
= hva_hw_get_ip_version(hva
);
398 if (hva
->ip_version
== HVA_VERSION_UNKNOWN
) {
403 dev_info(dev
, "%s found hva device (version 0x%lx)\n", HVA_PREFIX
,
412 clk_unprepare(hva
->clk
);
417 void hva_hw_remove(struct hva_dev
*hva
)
419 struct device
*dev
= hva_to_dev(hva
);
421 disable_irq(hva
->irq_its
);
422 disable_irq(hva
->irq_err
);
424 pm_runtime_put_autosuspend(dev
);
425 pm_runtime_disable(dev
);
428 int hva_hw_runtime_suspend(struct device
*dev
)
430 struct hva_dev
*hva
= dev_get_drvdata(dev
);
432 clk_disable_unprepare(hva
->clk
);
437 int hva_hw_runtime_resume(struct device
*dev
)
439 struct hva_dev
*hva
= dev_get_drvdata(dev
);
441 if (clk_prepare_enable(hva
->clk
)) {
442 dev_err(hva
->dev
, "%s failed to prepare hva clk\n",
447 if (clk_set_rate(hva
->clk
, CLK_RATE
)) {
448 dev_err(dev
, "%s failed to set clock frequency\n",
456 int hva_hw_execute_task(struct hva_ctx
*ctx
, enum hva_hw_cmd_type cmd
,
457 struct hva_buffer
*task
)
459 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
460 struct device
*dev
= hva_to_dev(hva
);
461 u8 client_id
= ctx
->id
;
465 mutex_lock(&hva
->protect_mutex
);
468 enable_irq(hva
->irq_its
);
469 enable_irq(hva
->irq_err
);
471 if (pm_runtime_get_sync(dev
) < 0) {
472 dev_err(dev
, "%s failed to get pm_runtime\n", ctx
->name
);
477 reg
= readl_relaxed(hva
->regs
+ HVA_HIF_REG_CLK_GATING
);
480 reg
|= CLK_GATING_HVC
;
483 dev_dbg(dev
, "%s unknown command 0x%x\n", ctx
->name
, cmd
);
487 writel_relaxed(reg
, hva
->regs
+ HVA_HIF_REG_CLK_GATING
);
489 dev_dbg(dev
, "%s %s: write configuration registers\n", ctx
->name
,
492 /* byte swap config */
493 writel_relaxed(BSM_CFG_VAL1
, hva
->regs
+ HVA_HIF_REG_BSM
);
495 /* define Max Opcode Size and Max Message Size for LMI and EMI */
496 writel_relaxed(MIF_CFG_VAL3
, hva
->regs
+ HVA_HIF_REG_MIF_CFG
);
497 writel_relaxed(HEC_MIF_CFG_VAL
, hva
->regs
+ HVA_HIF_REG_HEC_MIF_CFG
);
500 * command FIFO: task_id[31:16] client_id[15:8] command_type[7:0]
501 * the context identifier is provided as client identifier to the
502 * hardware, and is retrieved in the interrupt functions from the
505 dev_dbg(dev
, "%s %s: send task (cmd: %d, task_desc: %pad)\n",
506 ctx
->name
, __func__
, cmd
+ (client_id
<< 8), &task
->paddr
);
507 writel_relaxed(cmd
+ (client_id
<< 8), hva
->regs
+ HVA_HIF_FIFO_CMD
);
508 writel_relaxed(task
->paddr
, hva
->regs
+ HVA_HIF_FIFO_CMD
);
510 if (!wait_for_completion_timeout(&hva
->interrupt
,
511 msecs_to_jiffies(2000))) {
512 dev_err(dev
, "%s %s: time out on completion\n", ctx
->name
,
518 /* get encoding status */
519 ret
= ctx
->hw_err
? -EFAULT
: 0;
522 disable_irq(hva
->irq_its
);
523 disable_irq(hva
->irq_err
);
527 reg
&= ~CLK_GATING_HVC
;
528 writel_relaxed(reg
, hva
->regs
+ HVA_HIF_REG_CLK_GATING
);
531 dev_dbg(dev
, "%s unknown command 0x%x\n", ctx
->name
, cmd
);
534 pm_runtime_put_autosuspend(dev
);
535 mutex_unlock(&hva
->protect_mutex
);