[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / media / video / saa7164 / saa7164-buffer.c
blob9ca5c83d165b636d410323e21a37bcb808f3a03f
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "saa7164.h"
24 /* The PCI address space for buffer handling looks like this:
26 +-u32 wide-------------+
27 | +
28 +-u64 wide------------------------------------+
29 + +
30 +----------------------+
31 | CurrentBufferPtr + Pointer to current PCI buffer >-+
32 +----------------------+ |
33 | Unused + |
34 +----------------------+ |
35 | Pitch + = 188 (bytes) |
36 +----------------------+ |
37 | PCI buffer size + = pitch * number of lines (312) |
38 +----------------------+ |
39 |0| Buf0 Write Offset + |
40 +----------------------+ v
41 |1| Buf1 Write Offset + |
42 +----------------------+ |
43 |2| Buf2 Write Offset + |
44 +----------------------+ |
45 |3| Buf3 Write Offset + |
46 +----------------------+ |
47 ... More write offsets |
48 +---------------------------------------------+ |
49 +0| set of ptrs to PCI pagetables + |
50 +---------------------------------------------+ |
51 +1| set of ptrs to PCI pagetables + <--------+
52 +---------------------------------------------+
53 +2| set of ptrs to PCI pagetables +
54 +---------------------------------------------+
55 +3| set of ptrs to PCI pagetables + >--+
56 +---------------------------------------------+ |
57 ... More buffer pointers | +----------------+
58 +->| pt[0] TS data |
59 | +----------------+
61 | +----------------+
62 +->| pt[1] TS data |
63 | +----------------+
64 | etc
67 /* Allocate a new buffer structure and associated PCI space in bytes.
68 * len must be a multiple of sizeof(u64)
70 struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_tsport *port,
71 u32 len)
73 struct saa7164_buffer *buf = 0;
74 struct saa7164_dev *dev = port->dev;
75 int i;
77 if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
78 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
79 goto ret;
82 buf = kzalloc(sizeof(struct saa7164_buffer), GFP_KERNEL);
83 if (buf == NULL) {
84 log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__);
85 goto ret;
88 buf->port = port;
89 buf->flags = SAA7164_BUFFER_FREE;
90 /* TODO: arg len is being ignored */
91 buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
92 buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
94 /* Allocate contiguous memory */
95 buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
96 &buf->dma);
97 if (!buf->cpu)
98 goto fail1;
100 buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
101 &buf->pt_dma);
102 if (!buf->pt_cpu)
103 goto fail2;
105 /* init the buffers to a known pattern, easier during debugging */
106 memset(buf->cpu, 0xff, buf->pci_size);
107 memset(buf->pt_cpu, 0xff, buf->pt_size);
109 dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p\n", __func__, buf);
110 dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
111 buf->cpu, (long)buf->dma, buf->pci_size);
112 dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
113 buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
115 /* Format the Page Table Entries to point into the data buffer */
116 for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
118 *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
122 goto ret;
124 fail2:
125 pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
126 fail1:
127 kfree(buf);
129 buf = 0;
130 ret:
131 return buf;
134 int saa7164_buffer_dealloc(struct saa7164_tsport *port,
135 struct saa7164_buffer *buf)
137 struct saa7164_dev *dev = port->dev;
139 if ((buf == 0) || (port == 0))
140 return SAA_ERR_BAD_PARAMETER;
142 dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n", __func__, buf);
144 if (buf->flags != SAA7164_BUFFER_FREE)
145 log_warn(" freeing a non-free buffer\n");
147 pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
148 pci_free_consistent(port->dev->pci, buf->pt_size, buf->pt_cpu,
149 buf->pt_dma);
151 kfree(buf);
153 return SAA_OK;