tw68-risc.c 6.5 KB

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