blob: b75157d411dd6cfff47c3701e3bb310f91d3830f [file] [log] [blame]
Steven Toth443c12282009-05-09 21:17:28 -03001/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
Steven Toth9b8b0192010-07-31 14:39:44 -03004 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
Steven Toth443c12282009-05-09 21:17:28 -03005 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
16 *
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.
20 */
21
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
23
Steven Toth443c12282009-05-09 21:17:28 -030024#include "saa7164.h"
25
26/* The PCI address space for buffer handling looks like this:
27
28 +-u32 wide-------------+
29 | +
30 +-u64 wide------------------------------------+
31 + +
32 +----------------------+
33 | CurrentBufferPtr + Pointer to current PCI buffer >-+
34 +----------------------+ |
35 | Unused + |
36 +----------------------+ |
37 | Pitch + = 188 (bytes) |
38 +----------------------+ |
39 | PCI buffer size + = pitch * number of lines (312) |
40 +----------------------+ |
41 |0| Buf0 Write Offset + |
42 +----------------------+ v
43 |1| Buf1 Write Offset + |
44 +----------------------+ |
45 |2| Buf2 Write Offset + |
46 +----------------------+ |
47 |3| Buf3 Write Offset + |
48 +----------------------+ |
49 ... More write offsets |
50 +---------------------------------------------+ |
51 +0| set of ptrs to PCI pagetables + |
52 +---------------------------------------------+ |
53 +1| set of ptrs to PCI pagetables + <--------+
54 +---------------------------------------------+
55 +2| set of ptrs to PCI pagetables +
56 +---------------------------------------------+
57 +3| set of ptrs to PCI pagetables + >--+
58 +---------------------------------------------+ |
59 ... More buffer pointers | +----------------+
60 +->| pt[0] TS data |
61 | +----------------+
62 |
63 | +----------------+
64 +->| pt[1] TS data |
65 | +----------------+
66 | etc
67 */
68
Steven Tothadd3f582010-07-31 14:43:07 -030069void saa7164_buffer_display(struct saa7164_buffer *buf)
70{
71 struct saa7164_dev *dev = buf->port->dev;
72 int i;
73
74 dprintk(DBGLVL_BUF, "%s() buffer @ 0x%p nr=%d\n",
75 __func__, buf, buf->idx);
76 dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%p len = 0x%x\n",
77 buf->cpu, (void *)buf->dma, buf->pci_size);
78 dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%p len = 0x%x\n",
79 buf->pt_cpu, (void *)buf->pt_dma, buf->pt_size);
80
81 /* Format the Page Table Entries to point into the data buffer */
82 for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
83
84 dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
85 i, buf->pt_cpu, (u64)*(buf->pt_cpu));
86
87 }
88}
Steven Toth443c12282009-05-09 21:17:28 -030089/* Allocate a new buffer structure and associated PCI space in bytes.
90 * len must be a multiple of sizeof(u64)
91 */
Steven Tothadd3f582010-07-31 14:43:07 -030092struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_port *port,
Steven Toth443c12282009-05-09 21:17:28 -030093 u32 len)
94{
Steven Tothadd3f582010-07-31 14:43:07 -030095 tmHWStreamParameters_t *params = &port->hw_streamingparams;
Steven Toth443c12282009-05-09 21:17:28 -030096 struct saa7164_buffer *buf = 0;
97 struct saa7164_dev *dev = port->dev;
98 int i;
99
100 if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
101 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
102 goto ret;
103 }
104
105 buf = kzalloc(sizeof(struct saa7164_buffer), GFP_KERNEL);
106 if (buf == NULL) {
107 log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__);
108 goto ret;
109 }
110
Steven Tothadd3f582010-07-31 14:43:07 -0300111 buf->idx = -1;
Steven Toth443c12282009-05-09 21:17:28 -0300112 buf->port = port;
113 buf->flags = SAA7164_BUFFER_FREE;
Steven Tothadd3f582010-07-31 14:43:07 -0300114 buf->pos = 0;
115 buf->actual_size = params->pitch * params->numberoflines;
Steven Toth12d32032010-07-31 15:13:45 -0300116 buf->crc = 0;
Steven Toth443c12282009-05-09 21:17:28 -0300117 /* TODO: arg len is being ignored */
118 buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
119 buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
120
121 /* Allocate contiguous memory */
122 buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
123 &buf->dma);
124 if (!buf->cpu)
125 goto fail1;
126
127 buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
128 &buf->pt_dma);
129 if (!buf->pt_cpu)
130 goto fail2;
131
132 /* init the buffers to a known pattern, easier during debugging */
Steven Toth12d32032010-07-31 15:13:45 -0300133 memset_io(buf->cpu, 0xff, buf->pci_size);
134 buf->crc = crc32(0, buf->cpu, buf->actual_size);
135 memset_io(buf->pt_cpu, 0xff, buf->pt_size);
Steven Toth443c12282009-05-09 21:17:28 -0300136
Steven Tothadd3f582010-07-31 14:43:07 -0300137 dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p\n",
138 __func__, buf);
Mauro Carvalho Chehabab205852009-09-19 00:41:18 -0300139 dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
140 buf->cpu, (long)buf->dma, buf->pci_size);
141 dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
142 buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
Steven Toth443c12282009-05-09 21:17:28 -0300143
144 /* Format the Page Table Entries to point into the data buffer */
145 for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
146
147 *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
148
Steven Toth443c12282009-05-09 21:17:28 -0300149 }
150
151 goto ret;
152
153fail2:
154 pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
155fail1:
156 kfree(buf);
157
158 buf = 0;
159ret:
160 return buf;
161}
162
Steven Tothadd3f582010-07-31 14:43:07 -0300163int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
Steven Toth443c12282009-05-09 21:17:28 -0300164{
Dan Carpenter23e64d52010-08-19 07:00:22 -0300165 struct saa7164_dev *dev;
Steven Toth443c12282009-05-09 21:17:28 -0300166
Steven Tothadd3f582010-07-31 14:43:07 -0300167 if (!buf || !buf->port)
Steven Toth443c12282009-05-09 21:17:28 -0300168 return SAA_ERR_BAD_PARAMETER;
Steven Tothadd3f582010-07-31 14:43:07 -0300169 dev = buf->port->dev;
Steven Toth443c12282009-05-09 21:17:28 -0300170
Steven Tothadd3f582010-07-31 14:43:07 -0300171 dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
172 __func__, buf);
Steven Toth443c12282009-05-09 21:17:28 -0300173
174 if (buf->flags != SAA7164_BUFFER_FREE)
175 log_warn(" freeing a non-free buffer\n");
176
Steven Tothadd3f582010-07-31 14:43:07 -0300177 pci_free_consistent(dev->pci, buf->pci_size, buf->cpu, buf->dma);
178 pci_free_consistent(dev->pci, buf->pt_size, buf->pt_cpu, buf->pt_dma);
Steven Toth443c12282009-05-09 21:17:28 -0300179
180 kfree(buf);
181
182 return SAA_OK;
183}
184
Steven Toth9230aca2010-07-31 15:06:49 -0300185int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i)
186{
187 struct saa7164_dev *dev = port->dev;
188
189 if ((i < 0) || (i >= port->hwcfg.buffercount))
190 return -EINVAL;
191
192 dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
193
194 saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
195
196 return 0;
197}
198
Steven Tothadd3f582010-07-31 14:43:07 -0300199/* Write a buffer into the hardware */
200int saa7164_buffer_activate(struct saa7164_buffer *buf, int i)
201{
202 struct saa7164_port *port = buf->port;
203 struct saa7164_dev *dev = port->dev;
204
205 if ((i < 0) || (i >= port->hwcfg.buffercount))
206 return -EINVAL;
207
208 dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
209
210 buf->idx = i; /* Note of which buffer list index position we occupy */
211 buf->flags = SAA7164_BUFFER_BUSY;
212 buf->pos = 0;
213
214 /* TODO: Review this in light of 32v64 assignments */
215 saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
216 saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i), buf->pt_dma);
217 saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
218
219 dprintk(DBGLVL_BUF, " buf[%d] offset 0x%llx (0x%x) "
220 "buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
221 buf->idx,
222 (u64)port->bufoffset + (i * sizeof(u32)),
223 saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
224 (u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
225 (u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
226 saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
227 saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
228 buf->idx);
229
230 return 0;
231}
232
233int saa7164_buffer_cfg_port(struct saa7164_port *port)
234{
235 tmHWStreamParameters_t *params = &port->hw_streamingparams;
236 struct saa7164_dev *dev = port->dev;
237 struct saa7164_buffer *buf;
238 struct list_head *c, *n;
239 int i = 0;
240
241 dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
242
243 saa7164_writel(port->bufcounter, 0);
244 saa7164_writel(port->pitch, params->pitch);
245 saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
246
247 dprintk(DBGLVL_BUF, " configured:\n");
248 dprintk(DBGLVL_BUF, " lmmio 0x%p\n", dev->lmmio);
249 dprintk(DBGLVL_BUF, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
250 saa7164_readl(port->bufcounter));
251
252 dprintk(DBGLVL_BUF, " pitch 0x%x = %d\n", port->pitch,
253 saa7164_readl(port->pitch));
254
255 dprintk(DBGLVL_BUF, " bufsize 0x%x = %d\n", port->bufsize,
256 saa7164_readl(port->bufsize));
257
258 dprintk(DBGLVL_BUF, " buffercount = %d\n", port->hwcfg.buffercount);
259 dprintk(DBGLVL_BUF, " bufoffset = 0x%x\n", port->bufoffset);
260 dprintk(DBGLVL_BUF, " bufptr32h = 0x%x\n", port->bufptr32h);
261 dprintk(DBGLVL_BUF, " bufptr32l = 0x%x\n", port->bufptr32l);
262
263 /* Poke the buffers and offsets into PCI space */
264 mutex_lock(&port->dmaqueue_lock);
265 list_for_each_safe(c, n, &port->dmaqueue.list) {
266 buf = list_entry(c, struct saa7164_buffer, list);
267
268 if (buf->flags != SAA7164_BUFFER_FREE)
269 BUG();
270
271 /* Place the buffer in the h/w queue */
272 saa7164_buffer_activate(buf, i);
273
274 /* Don't exceed the device maximum # bufs */
275 if (i++ > port->hwcfg.buffercount)
276 BUG();
277
278 }
279 mutex_unlock(&port->dmaqueue_lock);
280
281 return 0;
282}
283
Steven Toth7615e432010-07-31 14:44:53 -0300284struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev, u32 len)
285{
286 struct saa7164_user_buffer *buf;
287
288 buf = kzalloc(sizeof(struct saa7164_user_buffer), GFP_KERNEL);
289 if (buf == 0)
290 return 0;
291
292 buf->data = kzalloc(len, GFP_KERNEL);
293
294 if (buf->data == 0) {
295 kfree(buf);
296 return 0;
297 }
298
299 buf->actual_size = len;
300 buf->pos = 0;
Steven Toth12d32032010-07-31 15:13:45 -0300301 buf->crc = 0;
Steven Toth7615e432010-07-31 14:44:53 -0300302
303 dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n",
304 __func__, buf);
305
306 return buf;
307}
308
309void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
310{
311 if (!buf)
312 return;
313
314 if (buf->data) {
315 kfree(buf->data);
316 buf->data = 0;
317 }
318
319 if (buf)
320 kfree(buf);
321}
322