15740f4e7SHans Verkuil /* 25740f4e7SHans Verkuil * tw68_risc.c 35740f4e7SHans Verkuil * Part of the device driver for Techwell 68xx based cards 45740f4e7SHans Verkuil * 55740f4e7SHans Verkuil * Much of this code is derived from the cx88 and sa7134 drivers, which 65740f4e7SHans Verkuil * were in turn derived from the bt87x driver. The original work was by 75740f4e7SHans Verkuil * Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab, 85740f4e7SHans Verkuil * Hans Verkuil, Andy Walls and many others. Their work is gratefully 95740f4e7SHans Verkuil * acknowledged. Full credit goes to them - any problems within this code 105740f4e7SHans Verkuil * are mine. 115740f4e7SHans Verkuil * 12e15d1c12SHans Verkuil * Copyright (C) 2009 William M. Brack 13e15d1c12SHans Verkuil * 14e15d1c12SHans Verkuil * Refactored and updated to the latest v4l core frameworks: 15e15d1c12SHans Verkuil * 16e15d1c12SHans Verkuil * Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl> 175740f4e7SHans Verkuil * 185740f4e7SHans Verkuil * This program is free software; you can redistribute it and/or modify 195740f4e7SHans Verkuil * it under the terms of the GNU General Public License as published by 205740f4e7SHans Verkuil * the Free Software Foundation; either version 2 of the License, or 215740f4e7SHans Verkuil * (at your option) any later version. 225740f4e7SHans Verkuil * 235740f4e7SHans Verkuil * This program is distributed in the hope that it will be useful, 245740f4e7SHans Verkuil * but WITHOUT ANY WARRANTY; without even the implied warranty of 255740f4e7SHans Verkuil * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 265740f4e7SHans Verkuil * GNU General Public License for more details. 275740f4e7SHans Verkuil */ 285740f4e7SHans Verkuil 295740f4e7SHans Verkuil #include "tw68.h" 305740f4e7SHans Verkuil 315740f4e7SHans Verkuil /** 32*ef69f8d2SMauro Carvalho Chehab * tw68_risc_field 33*ef69f8d2SMauro Carvalho Chehab * @rp: pointer to current risc program position 34*ef69f8d2SMauro Carvalho Chehab * @sglist: pointer to "scatter-gather list" of buffer pointers 35*ef69f8d2SMauro Carvalho Chehab * @offset: offset to target memory buffer 36*ef69f8d2SMauro Carvalho Chehab * @sync_line: 0 -> no sync, 1 -> odd sync, 2 -> even sync 37*ef69f8d2SMauro Carvalho Chehab * @bpl: number of bytes per scan line 38*ef69f8d2SMauro Carvalho Chehab * @padding: number of bytes of padding to add 39*ef69f8d2SMauro Carvalho Chehab * @lines: number of lines in field 40*ef69f8d2SMauro Carvalho Chehab * @jump: insert a jump at the start 415740f4e7SHans Verkuil */ 425740f4e7SHans Verkuil static __le32 *tw68_risc_field(__le32 *rp, struct scatterlist *sglist, 435740f4e7SHans Verkuil unsigned int offset, u32 sync_line, 445740f4e7SHans Verkuil unsigned int bpl, unsigned int padding, 45e15d1c12SHans Verkuil unsigned int lines, bool jump) 465740f4e7SHans Verkuil { 475740f4e7SHans Verkuil struct scatterlist *sg; 485740f4e7SHans Verkuil unsigned int line, todo, done; 495740f4e7SHans Verkuil 50e15d1c12SHans Verkuil if (jump) { 51e15d1c12SHans Verkuil *(rp++) = cpu_to_le32(RISC_JUMP); 52e15d1c12SHans Verkuil *(rp++) = 0; 53e15d1c12SHans Verkuil } 54e15d1c12SHans Verkuil 555740f4e7SHans Verkuil /* sync instruction */ 565740f4e7SHans Verkuil if (sync_line == 1) 575740f4e7SHans Verkuil *(rp++) = cpu_to_le32(RISC_SYNCO); 585740f4e7SHans Verkuil else 595740f4e7SHans Verkuil *(rp++) = cpu_to_le32(RISC_SYNCE); 605740f4e7SHans Verkuil *(rp++) = 0; 61e15d1c12SHans Verkuil 625740f4e7SHans Verkuil /* scan lines */ 635740f4e7SHans Verkuil sg = sglist; 645740f4e7SHans Verkuil for (line = 0; line < lines; line++) { 655740f4e7SHans Verkuil /* calculate next starting position */ 665740f4e7SHans Verkuil while (offset && offset >= sg_dma_len(sg)) { 675740f4e7SHans Verkuil offset -= sg_dma_len(sg); 68e15d1c12SHans Verkuil sg = sg_next(sg); 695740f4e7SHans Verkuil } 705740f4e7SHans Verkuil if (bpl <= sg_dma_len(sg) - offset) { 715740f4e7SHans Verkuil /* fits into current chunk */ 725740f4e7SHans Verkuil *(rp++) = cpu_to_le32(RISC_LINESTART | 735740f4e7SHans Verkuil /* (offset<<12) |*/ bpl); 745740f4e7SHans Verkuil *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset); 755740f4e7SHans Verkuil offset += bpl; 765740f4e7SHans Verkuil } else { 775740f4e7SHans Verkuil /* 785740f4e7SHans Verkuil * scanline needs to be split. Put the start in 795740f4e7SHans Verkuil * whatever memory remains using RISC_LINESTART, 805740f4e7SHans Verkuil * then the remainder into following addresses 815740f4e7SHans Verkuil * given by the scatter-gather list. 825740f4e7SHans Verkuil */ 835740f4e7SHans Verkuil todo = bpl; /* one full line to be done */ 845740f4e7SHans Verkuil /* first fragment */ 855740f4e7SHans Verkuil done = (sg_dma_len(sg) - offset); 865740f4e7SHans Verkuil *(rp++) = cpu_to_le32(RISC_LINESTART | 875740f4e7SHans Verkuil (7 << 24) | 885740f4e7SHans Verkuil done); 895740f4e7SHans Verkuil *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset); 905740f4e7SHans Verkuil todo -= done; 91e15d1c12SHans Verkuil sg = sg_next(sg); 925740f4e7SHans Verkuil /* succeeding fragments have no offset */ 935740f4e7SHans Verkuil while (todo > sg_dma_len(sg)) { 945740f4e7SHans Verkuil *(rp++) = cpu_to_le32(RISC_INLINE | 955740f4e7SHans Verkuil (done << 12) | 965740f4e7SHans Verkuil sg_dma_len(sg)); 975740f4e7SHans Verkuil *(rp++) = cpu_to_le32(sg_dma_address(sg)); 985740f4e7SHans Verkuil todo -= sg_dma_len(sg); 99e15d1c12SHans Verkuil sg = sg_next(sg); 1005740f4e7SHans Verkuil done += sg_dma_len(sg); 1015740f4e7SHans Verkuil } 1025740f4e7SHans Verkuil if (todo) { 1035740f4e7SHans Verkuil /* final chunk - offset 0, count 'todo' */ 1045740f4e7SHans Verkuil *(rp++) = cpu_to_le32(RISC_INLINE | 1055740f4e7SHans Verkuil (done << 12) | 1065740f4e7SHans Verkuil todo); 1075740f4e7SHans Verkuil *(rp++) = cpu_to_le32(sg_dma_address(sg)); 1085740f4e7SHans Verkuil } 1095740f4e7SHans Verkuil offset = todo; 1105740f4e7SHans Verkuil } 1115740f4e7SHans Verkuil offset += padding; 1125740f4e7SHans Verkuil } 1135740f4e7SHans Verkuil 1145740f4e7SHans Verkuil return rp; 1155740f4e7SHans Verkuil } 1165740f4e7SHans Verkuil 1175740f4e7SHans Verkuil /** 1185740f4e7SHans Verkuil * tw68_risc_buffer 1195740f4e7SHans Verkuil * 1205740f4e7SHans Verkuil * This routine is called by tw68-video. It allocates 1215740f4e7SHans Verkuil * memory for the dma controller "program" and then fills in that 1225740f4e7SHans Verkuil * memory with the appropriate "instructions". 1235740f4e7SHans Verkuil * 124*ef69f8d2SMauro Carvalho Chehab * @pci: structure with info about the pci 1255740f4e7SHans Verkuil * slot which our device is in. 126*ef69f8d2SMauro Carvalho Chehab * @buf: structure with info about the memory 1275740f4e7SHans Verkuil * used for our controller program. 128*ef69f8d2SMauro Carvalho Chehab * @sglist: scatter-gather list entry 129*ef69f8d2SMauro Carvalho Chehab * @top_offset: offset within the risc program area for the 1305740f4e7SHans Verkuil * first odd frame line 131*ef69f8d2SMauro Carvalho Chehab * @bottom_offset: offset within the risc program area for the 1325740f4e7SHans Verkuil * first even frame line 133*ef69f8d2SMauro Carvalho Chehab * @bpl: number of data bytes per scan line 134*ef69f8d2SMauro Carvalho Chehab * @padding: number of extra bytes to add at end of line 135*ef69f8d2SMauro Carvalho Chehab * @lines: number of scan lines 1365740f4e7SHans Verkuil */ 1375740f4e7SHans Verkuil int tw68_risc_buffer(struct pci_dev *pci, 138e15d1c12SHans Verkuil struct tw68_buf *buf, 1395740f4e7SHans Verkuil struct scatterlist *sglist, 1405740f4e7SHans Verkuil unsigned int top_offset, 1415740f4e7SHans Verkuil unsigned int bottom_offset, 1425740f4e7SHans Verkuil unsigned int bpl, 1435740f4e7SHans Verkuil unsigned int padding, 1445740f4e7SHans Verkuil unsigned int lines) 1455740f4e7SHans Verkuil { 1465740f4e7SHans Verkuil u32 instructions, fields; 1475740f4e7SHans Verkuil __le32 *rp; 1485740f4e7SHans Verkuil 1495740f4e7SHans Verkuil fields = 0; 1505740f4e7SHans Verkuil if (UNSET != top_offset) 1515740f4e7SHans Verkuil fields++; 1525740f4e7SHans Verkuil if (UNSET != bottom_offset) 1535740f4e7SHans Verkuil fields++; 1545740f4e7SHans Verkuil /* 1555740f4e7SHans Verkuil * estimate risc mem: worst case is one write per page border + 156e15d1c12SHans Verkuil * one write per scan line + syncs + 2 jumps (all 2 dwords). 1575740f4e7SHans Verkuil * Padding can cause next bpl to start close to a page border. 1585740f4e7SHans Verkuil * First DMA region may be smaller than PAGE_SIZE 1595740f4e7SHans Verkuil */ 1605740f4e7SHans Verkuil instructions = fields * (1 + (((bpl + padding) * lines) / 161e15d1c12SHans Verkuil PAGE_SIZE) + lines) + 4; 162e15d1c12SHans Verkuil buf->size = instructions * 8; 163e15d1c12SHans Verkuil buf->cpu = pci_alloc_consistent(pci, buf->size, &buf->dma); 164e15d1c12SHans Verkuil if (buf->cpu == NULL) 165e15d1c12SHans Verkuil return -ENOMEM; 1665740f4e7SHans Verkuil 1675740f4e7SHans Verkuil /* write risc instructions */ 168e15d1c12SHans Verkuil rp = buf->cpu; 1695740f4e7SHans Verkuil if (UNSET != top_offset) /* generates SYNCO */ 1705740f4e7SHans Verkuil rp = tw68_risc_field(rp, sglist, top_offset, 1, 171e15d1c12SHans Verkuil bpl, padding, lines, true); 1725740f4e7SHans Verkuil if (UNSET != bottom_offset) /* generates SYNCE */ 1735740f4e7SHans Verkuil rp = tw68_risc_field(rp, sglist, bottom_offset, 2, 174e15d1c12SHans Verkuil bpl, padding, lines, top_offset == UNSET); 1755740f4e7SHans Verkuil 1765740f4e7SHans Verkuil /* save pointer to jmp instruction address */ 177e15d1c12SHans Verkuil buf->jmp = rp; 178e15d1c12SHans Verkuil buf->cpu[1] = cpu_to_le32(buf->dma + 8); 1795740f4e7SHans Verkuil /* assure risc buffer hasn't overflowed */ 180e15d1c12SHans Verkuil BUG_ON((buf->jmp - buf->cpu + 2) * sizeof(buf->cpu[0]) > buf->size); 1815740f4e7SHans Verkuil return 0; 1825740f4e7SHans Verkuil } 1835740f4e7SHans Verkuil 1845740f4e7SHans Verkuil #if 0 1855740f4e7SHans Verkuil /* ------------------------------------------------------------------ */ 1865740f4e7SHans Verkuil /* debug helper code */ 1875740f4e7SHans Verkuil 1885740f4e7SHans Verkuil static void tw68_risc_decode(u32 risc, u32 addr) 1895740f4e7SHans Verkuil { 1905740f4e7SHans Verkuil #define RISC_OP(reg) (((reg) >> 28) & 7) 1915740f4e7SHans Verkuil static struct instr_details { 1925740f4e7SHans Verkuil char *name; 1935740f4e7SHans Verkuil u8 has_data_type; 1945740f4e7SHans Verkuil u8 has_byte_info; 1955740f4e7SHans Verkuil u8 has_addr; 1965740f4e7SHans Verkuil } instr[8] = { 1975740f4e7SHans Verkuil [RISC_OP(RISC_SYNCO)] = {"syncOdd", 0, 0, 0}, 1985740f4e7SHans Verkuil [RISC_OP(RISC_SYNCE)] = {"syncEven", 0, 0, 0}, 1995740f4e7SHans Verkuil [RISC_OP(RISC_JUMP)] = {"jump", 0, 0, 1}, 2005740f4e7SHans Verkuil [RISC_OP(RISC_LINESTART)] = {"lineStart", 1, 1, 1}, 2015740f4e7SHans Verkuil [RISC_OP(RISC_INLINE)] = {"inline", 1, 1, 1}, 2025740f4e7SHans Verkuil }; 2035740f4e7SHans Verkuil u32 p; 2045740f4e7SHans Verkuil 2055740f4e7SHans Verkuil p = RISC_OP(risc); 2065740f4e7SHans Verkuil if (!(risc & 0x80000000) || !instr[p].name) { 207e15d1c12SHans Verkuil pr_debug("0x%08x [ INVALID ]\n", risc); 2085740f4e7SHans Verkuil return; 2095740f4e7SHans Verkuil } 210e15d1c12SHans Verkuil pr_debug("0x%08x %-9s IRQ=%d", 2115740f4e7SHans Verkuil risc, instr[p].name, (risc >> 27) & 1); 2125740f4e7SHans Verkuil if (instr[p].has_data_type) 213e15d1c12SHans Verkuil pr_debug(" Type=%d", (risc >> 24) & 7); 2145740f4e7SHans Verkuil if (instr[p].has_byte_info) 215e15d1c12SHans Verkuil pr_debug(" Start=0x%03x Count=%03u", 2165740f4e7SHans Verkuil (risc >> 12) & 0xfff, risc & 0xfff); 2175740f4e7SHans Verkuil if (instr[p].has_addr) 218e15d1c12SHans Verkuil pr_debug(" StartAddr=0x%08x", addr); 219e15d1c12SHans Verkuil pr_debug("\n"); 2205740f4e7SHans Verkuil } 2215740f4e7SHans Verkuil 222e15d1c12SHans Verkuil void tw68_risc_program_dump(struct tw68_core *core, struct tw68_buf *buf) 2235740f4e7SHans Verkuil { 224e15d1c12SHans Verkuil const __le32 *addr; 2255740f4e7SHans Verkuil 226e15d1c12SHans Verkuil pr_debug("%s: risc_program_dump: risc=%p, buf->cpu=0x%p, buf->jmp=0x%p\n", 227e15d1c12SHans Verkuil core->name, buf, buf->cpu, buf->jmp); 228e15d1c12SHans Verkuil for (addr = buf->cpu; addr <= buf->jmp; addr += 2) 2295740f4e7SHans Verkuil tw68_risc_decode(*addr, *(addr+1)); 2305740f4e7SHans Verkuil } 2315740f4e7SHans Verkuil #endif 232