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