1
2
3
4
5
6
7
8
9
10#include <linux/init.h>
11#include <linux/ioctl.h>
12#include <linux/list.h>
13#include <linux/module.h>
14#include <linux/of_device.h>
15#include <linux/reset.h>
16#include <linux/interrupt.h>
17#include <media/videobuf2-dma-contig.h>
18#include <media/v4l2-mem2mem.h>
19
20#include "dos_regs.h"
21#include "esparser.h"
22#include "vdec_helpers.h"
23
24
25#define PARSER_CONTROL 0x00
26 #define ES_PACK_SIZE_BIT 8
27 #define ES_WRITE BIT(5)
28 #define ES_SEARCH BIT(1)
29 #define ES_PARSER_START BIT(0)
30#define PARSER_FETCH_ADDR 0x4
31#define PARSER_FETCH_CMD 0x8
32#define PARSER_CONFIG 0x14
33 #define PS_CFG_MAX_FETCH_CYCLE_BIT 0
34 #define PS_CFG_STARTCODE_WID_24_BIT 10
35 #define PS_CFG_MAX_ES_WR_CYCLE_BIT 12
36 #define PS_CFG_PFIFO_EMPTY_CNT_BIT 16
37#define PFIFO_WR_PTR 0x18
38#define PFIFO_RD_PTR 0x1c
39#define PARSER_SEARCH_PATTERN 0x24
40 #define ES_START_CODE_PATTERN 0x00000100
41#define PARSER_SEARCH_MASK 0x28
42 #define ES_START_CODE_MASK 0xffffff00
43 #define FETCH_ENDIAN_BIT 27
44#define PARSER_INT_ENABLE 0x2c
45 #define PARSER_INT_HOST_EN_BIT 8
46#define PARSER_INT_STATUS 0x30
47 #define PARSER_INTSTAT_SC_FOUND 1
48#define PARSER_ES_CONTROL 0x5c
49#define PARSER_VIDEO_START_PTR 0x80
50#define PARSER_VIDEO_END_PTR 0x84
51#define PARSER_VIDEO_WP 0x88
52#define PARSER_VIDEO_HOLE 0x90
53
54#define SEARCH_PATTERN_LEN 512
55
56static DECLARE_WAIT_QUEUE_HEAD(wq);
57static int search_done;
58
59static irqreturn_t esparser_isr(int irq, void *dev)
60{
61 int int_status;
62 struct amvdec_core *core = dev;
63
64 int_status = amvdec_read_parser(core, PARSER_INT_STATUS);
65 amvdec_write_parser(core, PARSER_INT_STATUS, int_status);
66
67 if (int_status & PARSER_INTSTAT_SC_FOUND) {
68 amvdec_write_parser(core, PFIFO_RD_PTR, 0);
69 amvdec_write_parser(core, PFIFO_WR_PTR, 0);
70 search_done = 1;
71 wake_up_interruptible(&wq);
72 }
73
74 return IRQ_HANDLED;
75}
76
77
78
79
80
81
82static u32 esparser_pad_start_code(struct vb2_buffer *vb)
83{
84 u32 payload_size = vb2_get_plane_payload(vb, 0);
85 u32 pad_size = 0;
86 u8 *vaddr = vb2_plane_vaddr(vb, 0) + payload_size;
87
88 if (payload_size < ESPARSER_MIN_PACKET_SIZE) {
89 pad_size = ESPARSER_MIN_PACKET_SIZE - payload_size;
90 memset(vaddr, 0, pad_size);
91 }
92
93 memset(vaddr + pad_size, 0, SEARCH_PATTERN_LEN);
94 vaddr[pad_size] = 0x00;
95 vaddr[pad_size + 1] = 0x00;
96 vaddr[pad_size + 2] = 0x01;
97 vaddr[pad_size + 3] = 0xff;
98
99 return pad_size;
100}
101
102static int
103esparser_write_data(struct amvdec_core *core, dma_addr_t addr, u32 size)
104{
105 amvdec_write_parser(core, PFIFO_RD_PTR, 0);
106 amvdec_write_parser(core, PFIFO_WR_PTR, 0);
107 amvdec_write_parser(core, PARSER_CONTROL,
108 ES_WRITE |
109 ES_PARSER_START |
110 ES_SEARCH |
111 (size << ES_PACK_SIZE_BIT));
112
113 amvdec_write_parser(core, PARSER_FETCH_ADDR, addr);
114 amvdec_write_parser(core, PARSER_FETCH_CMD,
115 (7 << FETCH_ENDIAN_BIT) |
116 (size + SEARCH_PATTERN_LEN));
117
118 search_done = 0;
119 return wait_event_interruptible_timeout(wq, search_done, (HZ / 5));
120}
121
122static u32 esparser_vififo_get_free_space(struct amvdec_session *sess)
123{
124 u32 vififo_usage;
125 struct amvdec_ops *vdec_ops = sess->fmt_out->vdec_ops;
126 struct amvdec_core *core = sess->core;
127
128 vififo_usage = vdec_ops->vififo_level(sess);
129 vififo_usage += amvdec_read_parser(core, PARSER_VIDEO_HOLE);
130 vififo_usage += (6 * SZ_1K);
131
132 if (vififo_usage > sess->vififo_size) {
133 dev_warn(sess->core->dev,
134 "VIFIFO usage (%u) > VIFIFO size (%u)\n",
135 vififo_usage, sess->vififo_size);
136 return 0;
137 }
138
139 return sess->vififo_size - vififo_usage;
140}
141
142int esparser_queue_eos(struct amvdec_core *core, const u8 *data, u32 len)
143{
144 struct device *dev = core->dev;
145 void *eos_vaddr;
146 dma_addr_t eos_paddr;
147 int ret;
148
149 eos_vaddr = dma_alloc_coherent(dev, len + SEARCH_PATTERN_LEN,
150 &eos_paddr, GFP_KERNEL);
151 if (!eos_vaddr)
152 return -ENOMEM;
153
154 memcpy(eos_vaddr, data, len);
155 ret = esparser_write_data(core, eos_paddr, len);
156 dma_free_coherent(dev, len + SEARCH_PATTERN_LEN,
157 eos_vaddr, eos_paddr);
158
159 return ret;
160}
161
162static u32 esparser_get_offset(struct amvdec_session *sess)
163{
164 struct amvdec_core *core = sess->core;
165 u32 offset = amvdec_read_parser(core, PARSER_VIDEO_WP) -
166 sess->vififo_paddr;
167
168 if (offset < sess->last_offset)
169 sess->wrap_count++;
170
171 sess->last_offset = offset;
172 offset += (sess->wrap_count * sess->vififo_size);
173
174 return offset;
175}
176
177static int
178esparser_queue(struct amvdec_session *sess, struct vb2_v4l2_buffer *vbuf)
179{
180 int ret;
181 struct vb2_buffer *vb = &vbuf->vb2_buf;
182 struct amvdec_core *core = sess->core;
183 struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops;
184 u32 num_dst_bufs = 0;
185 u32 payload_size = vb2_get_plane_payload(vb, 0);
186 dma_addr_t phy = vb2_dma_contig_plane_dma_addr(vb, 0);
187 u32 offset;
188 u32 pad_size;
189
190 if (codec_ops->num_pending_bufs)
191 num_dst_bufs = codec_ops->num_pending_bufs(sess);
192
193 num_dst_bufs += v4l2_m2m_num_dst_bufs_ready(sess->m2m_ctx);
194
195 if (esparser_vififo_get_free_space(sess) < payload_size ||
196 atomic_read(&sess->esparser_queued_bufs) >= num_dst_bufs)
197 return -EAGAIN;
198
199 v4l2_m2m_src_buf_remove_by_buf(sess->m2m_ctx, vbuf);
200
201 offset = esparser_get_offset(sess);
202
203 amvdec_add_ts_reorder(sess, vb->timestamp, offset);
204 dev_dbg(core->dev, "esparser: ts = %llu pld_size = %u offset = %08X\n",
205 vb->timestamp, payload_size, offset);
206
207 pad_size = esparser_pad_start_code(vb);
208 ret = esparser_write_data(core, phy, payload_size + pad_size);
209
210 if (ret <= 0) {
211 dev_warn(core->dev, "esparser: input parsing error\n");
212 amvdec_remove_ts(sess, vb->timestamp);
213 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
214 amvdec_write_parser(core, PARSER_FETCH_CMD, 0);
215
216 return 0;
217 }
218
219
220
221
222 if (!sess->keyframe_found)
223 usleep_range(1000, 2000);
224
225 if (sess->keyframe_found)
226 atomic_inc(&sess->esparser_queued_bufs);
227 else
228 amvdec_remove_ts(sess, vb->timestamp);
229
230 vbuf->flags = 0;
231 vbuf->field = V4L2_FIELD_NONE;
232 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
233
234 return 0;
235}
236
237void esparser_queue_all_src(struct work_struct *work)
238{
239 struct v4l2_m2m_buffer *buf, *n;
240 struct amvdec_session *sess =
241 container_of(work, struct amvdec_session, esparser_queue_work);
242
243 mutex_lock(&sess->lock);
244 v4l2_m2m_for_each_src_buf_safe(sess->m2m_ctx, buf, n) {
245 if (sess->should_stop)
246 break;
247
248 if (esparser_queue(sess, &buf->vb) < 0)
249 break;
250 }
251 mutex_unlock(&sess->lock);
252}
253
254int esparser_power_up(struct amvdec_session *sess)
255{
256 struct amvdec_core *core = sess->core;
257 struct amvdec_ops *vdec_ops = sess->fmt_out->vdec_ops;
258
259 reset_control_reset(core->esparser_reset);
260 amvdec_write_parser(core, PARSER_CONFIG,
261 (10 << PS_CFG_PFIFO_EMPTY_CNT_BIT) |
262 (1 << PS_CFG_MAX_ES_WR_CYCLE_BIT) |
263 (16 << PS_CFG_MAX_FETCH_CYCLE_BIT));
264
265 amvdec_write_parser(core, PFIFO_RD_PTR, 0);
266 amvdec_write_parser(core, PFIFO_WR_PTR, 0);
267
268 amvdec_write_parser(core, PARSER_SEARCH_PATTERN,
269 ES_START_CODE_PATTERN);
270 amvdec_write_parser(core, PARSER_SEARCH_MASK, ES_START_CODE_MASK);
271
272 amvdec_write_parser(core, PARSER_CONFIG,
273 (10 << PS_CFG_PFIFO_EMPTY_CNT_BIT) |
274 (1 << PS_CFG_MAX_ES_WR_CYCLE_BIT) |
275 (16 << PS_CFG_MAX_FETCH_CYCLE_BIT) |
276 (2 << PS_CFG_STARTCODE_WID_24_BIT));
277
278 amvdec_write_parser(core, PARSER_CONTROL,
279 (ES_SEARCH | ES_PARSER_START));
280
281 amvdec_write_parser(core, PARSER_VIDEO_START_PTR, sess->vififo_paddr);
282 amvdec_write_parser(core, PARSER_VIDEO_END_PTR,
283 sess->vififo_paddr + sess->vififo_size - 8);
284 amvdec_write_parser(core, PARSER_ES_CONTROL,
285 amvdec_read_parser(core, PARSER_ES_CONTROL) & ~1);
286
287 if (vdec_ops->conf_esparser)
288 vdec_ops->conf_esparser(sess);
289
290 amvdec_write_parser(core, PARSER_INT_STATUS, 0xffff);
291 amvdec_write_parser(core, PARSER_INT_ENABLE,
292 BIT(PARSER_INT_HOST_EN_BIT));
293
294 return 0;
295}
296
297int esparser_init(struct platform_device *pdev, struct amvdec_core *core)
298{
299 struct device *dev = &pdev->dev;
300 int ret;
301 int irq;
302
303 irq = platform_get_irq_byname(pdev, "esparser");
304 if (irq < 0)
305 return irq;
306
307 ret = devm_request_irq(dev, irq, esparser_isr, IRQF_SHARED,
308 "esparserirq", core);
309 if (ret) {
310 dev_err(dev, "Failed requesting ESPARSER IRQ\n");
311 return ret;
312 }
313
314 core->esparser_reset =
315 devm_reset_control_get_exclusive(dev, "esparser");
316 if (IS_ERR(core->esparser_reset)) {
317 dev_err(dev, "Failed to get esparser_reset\n");
318 return PTR_ERR(core->esparser_reset);
319 }
320
321 return 0;
322}
323