1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * tw68_risc.c
4*4882a593Smuzhiyun * Part of the device driver for Techwell 68xx based cards
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Much of this code is derived from the cx88 and sa7134 drivers, which
7*4882a593Smuzhiyun * were in turn derived from the bt87x driver. The original work was by
8*4882a593Smuzhiyun * Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
9*4882a593Smuzhiyun * Hans Verkuil, Andy Walls and many others. Their work is gratefully
10*4882a593Smuzhiyun * acknowledged. Full credit goes to them - any problems within this code
11*4882a593Smuzhiyun * are mine.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Copyright (C) 2009 William M. Brack
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Refactored and updated to the latest v4l core frameworks:
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "tw68.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /**
23*4882a593Smuzhiyun * tw68_risc_field
24*4882a593Smuzhiyun * @rp: pointer to current risc program position
25*4882a593Smuzhiyun * @sglist: pointer to "scatter-gather list" of buffer pointers
26*4882a593Smuzhiyun * @offset: offset to target memory buffer
27*4882a593Smuzhiyun * @sync_line: 0 -> no sync, 1 -> odd sync, 2 -> even sync
28*4882a593Smuzhiyun * @bpl: number of bytes per scan line
29*4882a593Smuzhiyun * @padding: number of bytes of padding to add
30*4882a593Smuzhiyun * @lines: number of lines in field
31*4882a593Smuzhiyun * @jump: insert a jump at the start
32*4882a593Smuzhiyun */
tw68_risc_field(__le32 * rp,struct scatterlist * sglist,unsigned int offset,u32 sync_line,unsigned int bpl,unsigned int padding,unsigned int lines,bool jump)33*4882a593Smuzhiyun static __le32 *tw68_risc_field(__le32 *rp, struct scatterlist *sglist,
34*4882a593Smuzhiyun unsigned int offset, u32 sync_line,
35*4882a593Smuzhiyun unsigned int bpl, unsigned int padding,
36*4882a593Smuzhiyun unsigned int lines, bool jump)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct scatterlist *sg;
39*4882a593Smuzhiyun unsigned int line, todo, done;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun if (jump) {
42*4882a593Smuzhiyun *(rp++) = cpu_to_le32(RISC_JUMP);
43*4882a593Smuzhiyun *(rp++) = 0;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* sync instruction */
47*4882a593Smuzhiyun if (sync_line == 1)
48*4882a593Smuzhiyun *(rp++) = cpu_to_le32(RISC_SYNCO);
49*4882a593Smuzhiyun else
50*4882a593Smuzhiyun *(rp++) = cpu_to_le32(RISC_SYNCE);
51*4882a593Smuzhiyun *(rp++) = 0;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* scan lines */
54*4882a593Smuzhiyun sg = sglist;
55*4882a593Smuzhiyun for (line = 0; line < lines; line++) {
56*4882a593Smuzhiyun /* calculate next starting position */
57*4882a593Smuzhiyun while (offset && offset >= sg_dma_len(sg)) {
58*4882a593Smuzhiyun offset -= sg_dma_len(sg);
59*4882a593Smuzhiyun sg = sg_next(sg);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun if (bpl <= sg_dma_len(sg) - offset) {
62*4882a593Smuzhiyun /* fits into current chunk */
63*4882a593Smuzhiyun *(rp++) = cpu_to_le32(RISC_LINESTART |
64*4882a593Smuzhiyun /* (offset<<12) |*/ bpl);
65*4882a593Smuzhiyun *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
66*4882a593Smuzhiyun offset += bpl;
67*4882a593Smuzhiyun } else {
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * scanline needs to be split. Put the start in
70*4882a593Smuzhiyun * whatever memory remains using RISC_LINESTART,
71*4882a593Smuzhiyun * then the remainder into following addresses
72*4882a593Smuzhiyun * given by the scatter-gather list.
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun todo = bpl; /* one full line to be done */
75*4882a593Smuzhiyun /* first fragment */
76*4882a593Smuzhiyun done = (sg_dma_len(sg) - offset);
77*4882a593Smuzhiyun *(rp++) = cpu_to_le32(RISC_LINESTART |
78*4882a593Smuzhiyun (7 << 24) |
79*4882a593Smuzhiyun done);
80*4882a593Smuzhiyun *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
81*4882a593Smuzhiyun todo -= done;
82*4882a593Smuzhiyun sg = sg_next(sg);
83*4882a593Smuzhiyun /* succeeding fragments have no offset */
84*4882a593Smuzhiyun while (todo > sg_dma_len(sg)) {
85*4882a593Smuzhiyun *(rp++) = cpu_to_le32(RISC_INLINE |
86*4882a593Smuzhiyun (done << 12) |
87*4882a593Smuzhiyun sg_dma_len(sg));
88*4882a593Smuzhiyun *(rp++) = cpu_to_le32(sg_dma_address(sg));
89*4882a593Smuzhiyun todo -= sg_dma_len(sg);
90*4882a593Smuzhiyun sg = sg_next(sg);
91*4882a593Smuzhiyun done += sg_dma_len(sg);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun if (todo) {
94*4882a593Smuzhiyun /* final chunk - offset 0, count 'todo' */
95*4882a593Smuzhiyun *(rp++) = cpu_to_le32(RISC_INLINE |
96*4882a593Smuzhiyun (done << 12) |
97*4882a593Smuzhiyun todo);
98*4882a593Smuzhiyun *(rp++) = cpu_to_le32(sg_dma_address(sg));
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun offset = todo;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun offset += padding;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun return rp;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun * tw68_risc_buffer
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * This routine is called by tw68-video. It allocates
112*4882a593Smuzhiyun * memory for the dma controller "program" and then fills in that
113*4882a593Smuzhiyun * memory with the appropriate "instructions".
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun * @pci: structure with info about the pci
116*4882a593Smuzhiyun * slot which our device is in.
117*4882a593Smuzhiyun * @buf: structure with info about the memory
118*4882a593Smuzhiyun * used for our controller program.
119*4882a593Smuzhiyun * @sglist: scatter-gather list entry
120*4882a593Smuzhiyun * @top_offset: offset within the risc program area for the
121*4882a593Smuzhiyun * first odd frame line
122*4882a593Smuzhiyun * @bottom_offset: offset within the risc program area for the
123*4882a593Smuzhiyun * first even frame line
124*4882a593Smuzhiyun * @bpl: number of data bytes per scan line
125*4882a593Smuzhiyun * @padding: number of extra bytes to add at end of line
126*4882a593Smuzhiyun * @lines: number of scan lines
127*4882a593Smuzhiyun */
tw68_risc_buffer(struct pci_dev * pci,struct tw68_buf * buf,struct scatterlist * sglist,unsigned int top_offset,unsigned int bottom_offset,unsigned int bpl,unsigned int padding,unsigned int lines)128*4882a593Smuzhiyun int tw68_risc_buffer(struct pci_dev *pci,
129*4882a593Smuzhiyun struct tw68_buf *buf,
130*4882a593Smuzhiyun struct scatterlist *sglist,
131*4882a593Smuzhiyun unsigned int top_offset,
132*4882a593Smuzhiyun unsigned int bottom_offset,
133*4882a593Smuzhiyun unsigned int bpl,
134*4882a593Smuzhiyun unsigned int padding,
135*4882a593Smuzhiyun unsigned int lines)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun u32 instructions, fields;
138*4882a593Smuzhiyun __le32 *rp;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun fields = 0;
141*4882a593Smuzhiyun if (UNSET != top_offset)
142*4882a593Smuzhiyun fields++;
143*4882a593Smuzhiyun if (UNSET != bottom_offset)
144*4882a593Smuzhiyun fields++;
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * estimate risc mem: worst case is one write per page border +
147*4882a593Smuzhiyun * one write per scan line + syncs + 2 jumps (all 2 dwords).
148*4882a593Smuzhiyun * Padding can cause next bpl to start close to a page border.
149*4882a593Smuzhiyun * First DMA region may be smaller than PAGE_SIZE
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun instructions = fields * (1 + (((bpl + padding) * lines) /
152*4882a593Smuzhiyun PAGE_SIZE) + lines) + 4;
153*4882a593Smuzhiyun buf->size = instructions * 8;
154*4882a593Smuzhiyun buf->cpu = pci_alloc_consistent(pci, buf->size, &buf->dma);
155*4882a593Smuzhiyun if (buf->cpu == NULL)
156*4882a593Smuzhiyun return -ENOMEM;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /* write risc instructions */
159*4882a593Smuzhiyun rp = buf->cpu;
160*4882a593Smuzhiyun if (UNSET != top_offset) /* generates SYNCO */
161*4882a593Smuzhiyun rp = tw68_risc_field(rp, sglist, top_offset, 1,
162*4882a593Smuzhiyun bpl, padding, lines, true);
163*4882a593Smuzhiyun if (UNSET != bottom_offset) /* generates SYNCE */
164*4882a593Smuzhiyun rp = tw68_risc_field(rp, sglist, bottom_offset, 2,
165*4882a593Smuzhiyun bpl, padding, lines, top_offset == UNSET);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /* save pointer to jmp instruction address */
168*4882a593Smuzhiyun buf->jmp = rp;
169*4882a593Smuzhiyun buf->cpu[1] = cpu_to_le32(buf->dma + 8);
170*4882a593Smuzhiyun /* assure risc buffer hasn't overflowed */
171*4882a593Smuzhiyun BUG_ON((buf->jmp - buf->cpu + 2) * sizeof(buf->cpu[0]) > buf->size);
172*4882a593Smuzhiyun return 0;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun #if 0
176*4882a593Smuzhiyun /* ------------------------------------------------------------------ */
177*4882a593Smuzhiyun /* debug helper code */
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun static void tw68_risc_decode(u32 risc, u32 addr)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun #define RISC_OP(reg) (((reg) >> 28) & 7)
182*4882a593Smuzhiyun static struct instr_details {
183*4882a593Smuzhiyun char *name;
184*4882a593Smuzhiyun u8 has_data_type;
185*4882a593Smuzhiyun u8 has_byte_info;
186*4882a593Smuzhiyun u8 has_addr;
187*4882a593Smuzhiyun } instr[8] = {
188*4882a593Smuzhiyun [RISC_OP(RISC_SYNCO)] = {"syncOdd", 0, 0, 0},
189*4882a593Smuzhiyun [RISC_OP(RISC_SYNCE)] = {"syncEven", 0, 0, 0},
190*4882a593Smuzhiyun [RISC_OP(RISC_JUMP)] = {"jump", 0, 0, 1},
191*4882a593Smuzhiyun [RISC_OP(RISC_LINESTART)] = {"lineStart", 1, 1, 1},
192*4882a593Smuzhiyun [RISC_OP(RISC_INLINE)] = {"inline", 1, 1, 1},
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun u32 p;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun p = RISC_OP(risc);
197*4882a593Smuzhiyun if (!(risc & 0x80000000) || !instr[p].name) {
198*4882a593Smuzhiyun pr_debug("0x%08x [ INVALID ]\n", risc);
199*4882a593Smuzhiyun return;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun pr_debug("0x%08x %-9s IRQ=%d",
202*4882a593Smuzhiyun risc, instr[p].name, (risc >> 27) & 1);
203*4882a593Smuzhiyun if (instr[p].has_data_type)
204*4882a593Smuzhiyun pr_debug(" Type=%d", (risc >> 24) & 7);
205*4882a593Smuzhiyun if (instr[p].has_byte_info)
206*4882a593Smuzhiyun pr_debug(" Start=0x%03x Count=%03u",
207*4882a593Smuzhiyun (risc >> 12) & 0xfff, risc & 0xfff);
208*4882a593Smuzhiyun if (instr[p].has_addr)
209*4882a593Smuzhiyun pr_debug(" StartAddr=0x%08x", addr);
210*4882a593Smuzhiyun pr_debug("\n");
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun void tw68_risc_program_dump(struct tw68_core *core, struct tw68_buf *buf)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun const __le32 *addr;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun pr_debug("%s: risc_program_dump: risc=%p, buf->cpu=0x%p, buf->jmp=0x%p\n",
218*4882a593Smuzhiyun core->name, buf, buf->cpu, buf->jmp);
219*4882a593Smuzhiyun for (addr = buf->cpu; addr <= buf->jmp; addr += 2)
220*4882a593Smuzhiyun tw68_risc_decode(*addr, *(addr+1));
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun #endif
223