tw68-risc.c 6.9 KB

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