Commit | Line | Data |
---|---|---|
443c1228 ST |
1 | /* |
2 | * Driver for the NXP SAA7164 PCIe bridge | |
3 | * | |
4 | * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com> | |
5 | * | |
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 | ||
22 | /* | |
23 | Driver architecture | |
24 | ******************* | |
25 | ||
26 | saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c | |
27 | | : Standard Linux driver framework for creating | |
28 | | : exposing and managing interfaces to the rest | |
29 | | : of the kernel or userland. Also uses _fw.c to load | |
30 | | : firmware direct into the PCIe bus, bypassing layers. | |
31 | V | |
32 | saa7164_api..() : Translate kernel specific functions/features | |
33 | | : into command buffers. | |
34 | V | |
35 | saa7164_cmd..() : Manages the flow of command packets on/off, | |
36 | | : the bus. Deal with bus errors, timeouts etc. | |
37 | V | |
38 | saa7164_bus..() : Manage a read/write memory ring buffer in the | |
39 | | : PCIe Address space. | |
40 | | | |
41 | | saa7164_fw...() : Load any frimware | |
42 | | | : direct into the device | |
43 | V V | |
44 | <- ----------------- PCIe address space -------------------- -> | |
45 | */ | |
46 | ||
47 | #include <linux/pci.h> | |
48 | #include <linux/i2c.h> | |
49 | #include <linux/i2c-algo-bit.h> | |
50 | #include <linux/kdev_t.h> | |
51 | ||
52 | #include <media/tuner.h> | |
53 | #include <media/tveeprom.h> | |
54 | #include <media/videobuf-dma-sg.h> | |
55 | #include <media/videobuf-dvb.h> | |
56 | ||
57 | #include "saa7164-reg.h" | |
58 | #include "saa7164-types.h" | |
59 | ||
60 | #include <linux/version.h> | |
61 | #include <linux/mutex.h> | |
62 | ||
63 | #define SAA7164_MAXBOARDS 8 | |
64 | ||
65 | #define UNSET (-1U) | |
66 | #define SAA7164_BOARD_NOAUTO UNSET | |
67 | #define SAA7164_BOARD_UNKNOWN 0 | |
68 | #define SAA7164_BOARD_UNKNOWN_REV2 1 | |
69 | #define SAA7164_BOARD_UNKNOWN_REV3 2 | |
70 | #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3 | |
71 | #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4 | |
72 | #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5 | |
73 | #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6 | |
74 | #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7 | |
75 | ||
76 | #define SAA7164_MAX_UNITS 8 | |
77 | #define SAA7164_TS_NUMBER_OF_LINES 312 | |
78 | #define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */ | |
79 | ||
80 | #define DBGLVL_FW 4 | |
81 | #define DBGLVL_DVB 8 | |
82 | #define DBGLVL_I2C 16 | |
83 | #define DBGLVL_API 32 | |
84 | #define DBGLVL_CMD 64 | |
85 | #define DBGLVL_BUS 128 | |
86 | #define DBGLVL_IRQ 256 | |
87 | #define DBGLVL_BUF 512 | |
88 | ||
89 | enum port_t { | |
90 | SAA7164_MPEG_UNDEFINED = 0, | |
91 | SAA7164_MPEG_DVB, | |
92 | }; | |
93 | ||
94 | enum saa7164_i2c_bus_nr { | |
95 | SAA7164_I2C_BUS_0 = 0, | |
96 | SAA7164_I2C_BUS_1, | |
97 | SAA7164_I2C_BUS_2, | |
98 | }; | |
99 | ||
100 | enum saa7164_buffer_flags { | |
101 | SAA7164_BUFFER_UNDEFINED = 0, | |
102 | SAA7164_BUFFER_FREE, | |
103 | SAA7164_BUFFER_BUSY, | |
104 | SAA7164_BUFFER_FULL | |
105 | }; | |
106 | ||
107 | enum saa7164_unit_type { | |
108 | SAA7164_UNIT_UNDEFINED = 0, | |
109 | SAA7164_UNIT_DIGITAL_DEMODULATOR, | |
110 | SAA7164_UNIT_ANALOG_DEMODULATOR, | |
111 | SAA7164_UNIT_TUNER, | |
112 | SAA7164_UNIT_EEPROM, | |
113 | SAA7164_UNIT_ZILOG_IRBLASTER, | |
114 | SAA7164_UNIT_ENCODER, | |
115 | }; | |
116 | ||
117 | /* The PCIe bridge doesn't grant direct access to i2c. | |
118 | * Instead, you address i2c devices using a uniqely | |
119 | * allocated 'unitid' value via a messaging API. This | |
120 | * is a problem. The kernel and existing demod/tuner | |
121 | * drivers expect to talk 'i2c', so we have to maintain | |
122 | * a translation layer, and a series of functions to | |
123 | * convert i2c bus + device address into a unit id. | |
124 | */ | |
125 | struct saa7164_unit { | |
126 | enum saa7164_unit_type type; | |
127 | u8 id; | |
128 | char *name; | |
129 | enum saa7164_i2c_bus_nr i2c_bus_nr; | |
130 | u8 i2c_bus_addr; | |
131 | u8 i2c_reg_len; | |
132 | }; | |
133 | ||
134 | struct saa7164_board { | |
135 | char *name; | |
136 | enum port_t porta, portb; | |
137 | enum { | |
138 | SAA7164_CHIP_UNDEFINED = 0, | |
139 | SAA7164_CHIP_REV2, | |
140 | SAA7164_CHIP_REV3, | |
141 | } chiprev; | |
142 | struct saa7164_unit unit[SAA7164_MAX_UNITS]; | |
143 | }; | |
144 | ||
145 | struct saa7164_subid { | |
146 | u16 subvendor; | |
147 | u16 subdevice; | |
148 | u32 card; | |
149 | }; | |
150 | ||
151 | struct saa7164_fw_status { | |
152 | ||
153 | /* RISC Core details */ | |
154 | u32 status; | |
155 | u32 mode; | |
156 | u32 spec; | |
157 | u32 inst; | |
158 | u32 cpuload; | |
159 | u32 remainheap; | |
160 | ||
161 | /* Firmware version */ | |
162 | u32 version; | |
163 | u32 major; | |
164 | u32 sub; | |
165 | u32 rel; | |
166 | u32 buildnr; | |
167 | }; | |
168 | ||
169 | struct saa7164_dvb { | |
170 | struct mutex lock; | |
171 | struct dvb_adapter adapter; | |
172 | struct dvb_frontend *frontend; | |
173 | struct dvb_demux demux; | |
174 | struct dmxdev dmxdev; | |
175 | struct dmx_frontend fe_hw; | |
176 | struct dmx_frontend fe_mem; | |
177 | struct dvb_net net; | |
178 | int feeding; | |
179 | }; | |
180 | ||
181 | struct saa7164_i2c { | |
182 | struct saa7164_dev *dev; | |
183 | ||
184 | enum saa7164_i2c_bus_nr nr; | |
185 | ||
186 | /* I2C I/O */ | |
187 | struct i2c_adapter i2c_adap; | |
188 | struct i2c_algo_bit_data i2c_algo; | |
189 | struct i2c_client i2c_client; | |
190 | u32 i2c_rc; | |
191 | }; | |
192 | ||
193 | struct saa7164_tsport; | |
194 | ||
195 | struct saa7164_buffer { | |
196 | struct list_head list; | |
197 | ||
198 | u32 nr; | |
199 | ||
200 | struct saa7164_tsport *port; | |
201 | ||
202 | /* Hardware Specific */ | |
203 | /* PCI Memory allocations */ | |
204 | enum saa7164_buffer_flags flags; /* Free, Busy, Full */ | |
205 | ||
206 | /* A block of page align PCI memory */ | |
207 | u32 pci_size; /* PCI allocation size in bytes */ | |
208 | u64 *cpu; /* Virtual address */ | |
209 | dma_addr_t dma; /* Physical address */ | |
210 | ||
211 | /* A page table that splits the block into a number of entries */ | |
212 | u32 pt_size; /* PCI allocation size in bytes */ | |
213 | u64 *pt_cpu; /* Virtual address */ | |
214 | dma_addr_t pt_dma; /* Physical address */ | |
215 | }; | |
216 | ||
217 | struct saa7164_tsport { | |
218 | ||
219 | struct saa7164_dev *dev; | |
220 | int nr; | |
221 | enum port_t type; | |
222 | ||
223 | struct saa7164_dvb dvb; | |
224 | ||
225 | /* HW related stream parameters */ | |
226 | tmHWStreamParameters_t hw_streamingparams; | |
227 | ||
228 | /* DMA configuration values, is seeded during initialization */ | |
229 | tmComResDMATermDescrHeader_t hwcfg; | |
230 | ||
231 | /* hardware specific registers */ | |
232 | u32 bufcounter; | |
233 | u32 pitch; | |
234 | u32 bufsize; | |
235 | u32 bufoffset; | |
236 | u32 bufptr32l; | |
237 | u32 bufptr32h; | |
238 | u64 bufptr64; | |
239 | ||
240 | u32 numpte; /* Number of entries in array, only valid in head */ | |
241 | struct mutex dmaqueue_lock; | |
242 | struct mutex dummy_dmaqueue_lock; | |
243 | struct saa7164_buffer dmaqueue; | |
244 | struct saa7164_buffer dummy_dmaqueue; | |
245 | ||
246 | }; | |
247 | ||
248 | struct saa7164_dev { | |
249 | struct list_head devlist; | |
250 | atomic_t refcount; | |
251 | ||
252 | /* pci stuff */ | |
253 | struct pci_dev *pci; | |
254 | unsigned char pci_rev, pci_lat; | |
255 | int pci_bus, pci_slot; | |
256 | u32 __iomem *lmmio; | |
257 | u8 __iomem *bmmio; | |
258 | u32 __iomem *lmmio2; | |
259 | u8 __iomem *bmmio2; | |
260 | int pci_irqmask; | |
261 | ||
262 | /* board details */ | |
263 | int nr; | |
264 | int hwrevision; | |
265 | u32 board; | |
266 | char name[32]; | |
267 | ||
268 | /* firmware status */ | |
269 | struct saa7164_fw_status fw_status; | |
270 | ||
271 | tmComResHWDescr_t hwdesc; | |
272 | tmComResInterfaceDescr_t intfdesc; | |
273 | tmComResBusDescr_t busdesc; | |
274 | ||
275 | tmComResBusInfo_t bus; | |
276 | ||
277 | /* TODO: Urgh, remove volatiles */ | |
278 | volatile u32 *InterruptStatus; | |
279 | volatile u32 *InterruptAck; | |
280 | ||
281 | struct cmd cmds[SAA_CMD_MAX_MSG_UNITS]; | |
282 | struct mutex lock; | |
283 | ||
284 | /* I2c related */ | |
285 | struct saa7164_i2c i2c_bus[3]; | |
286 | ||
287 | /* Transport related */ | |
288 | struct saa7164_tsport ts1, ts2; | |
289 | ||
290 | /* Deferred command/api interrupts handling */ | |
291 | struct work_struct workcmd; | |
292 | ||
293 | }; | |
294 | ||
295 | extern struct list_head saa7164_devlist; | |
296 | ||
297 | /* ----------------------------------------------------------- */ | |
298 | /* saa7164-core.c */ | |
299 | void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr); | |
300 | void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len); | |
301 | void saa7164_getfirmwarestatus(struct saa7164_dev *dev); | |
302 | u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev); | |
303 | ||
304 | /* ----------------------------------------------------------- */ | |
305 | /* saa7164-fw.c */ | |
306 | int saa7164_downloadfirmware(struct saa7164_dev *dev); | |
307 | ||
308 | /* ----------------------------------------------------------- */ | |
309 | /* saa7164-i2c.c */ | |
310 | extern int saa7164_i2c_register(struct saa7164_i2c *bus); | |
311 | extern int saa7164_i2c_unregister(struct saa7164_i2c *bus); | |
312 | extern void saa7164_call_i2c_clients(struct saa7164_i2c *bus, | |
313 | unsigned int cmd, void *arg); | |
314 | ||
315 | /* ----------------------------------------------------------- */ | |
316 | /* saa7164-bus.c */ | |
317 | int saa7164_bus_setup(struct saa7164_dev *dev); | |
318 | void saa7164_bus_dump(struct saa7164_dev *dev); | |
319 | int saa7164_bus_set(struct saa7164_dev *dev, tmComResInfo_t* msg, void *buf); | |
320 | int saa7164_bus_get(struct saa7164_dev *dev, tmComResInfo_t* msg, | |
321 | void *buf, int peekonly); | |
322 | ||
323 | /* ----------------------------------------------------------- */ | |
324 | /* saa7164-cmd.c */ | |
325 | int saa7164_cmd_send(struct saa7164_dev *dev, | |
326 | u8 id, tmComResCmd_t command, u16 controlselector, | |
327 | u16 size, void *buf); | |
328 | void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno); | |
329 | ||
330 | /* ----------------------------------------------------------- */ | |
331 | /* saa7164-api.c */ | |
332 | int saa7164_api_test(struct saa7164_dev *dev); | |
333 | int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version); | |
334 | int saa7164_api_enum_subdevs(struct saa7164_dev *dev); | |
335 | int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg, | |
336 | u32 datalen, u8 *data); | |
337 | int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, | |
338 | u32 datalen, u8 *data); | |
339 | int saa7164_api_dif_write(struct saa7164_i2c *bus, u8 addr, | |
340 | u32 datalen, u8 *data); | |
341 | int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen); | |
342 | int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin); | |
343 | int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin); | |
344 | int saa7164_api_transition_port(struct saa7164_tsport *port, u8 mode); | |
345 | ||
346 | /* ----------------------------------------------------------- */ | |
347 | /* saa7164-cards.c */ | |
348 | extern struct saa7164_board saa7164_boards[]; | |
349 | extern const unsigned int saa7164_bcount; | |
350 | ||
351 | extern struct saa7164_subid saa7164_subids[]; | |
352 | extern const unsigned int saa7164_idcount; | |
353 | ||
354 | extern void saa7164_card_list(struct saa7164_dev *dev); | |
355 | extern void saa7164_gpio_setup(struct saa7164_dev *dev); | |
356 | extern void saa7164_card_setup(struct saa7164_dev *dev); | |
357 | ||
358 | extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr); | |
359 | extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr); | |
360 | extern char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid); | |
361 | ||
362 | /* ----------------------------------------------------------- */ | |
363 | /* saa7164-dvb.c */ | |
364 | extern int saa7164_dvb_register(struct saa7164_tsport *port); | |
365 | extern int saa7164_dvb_unregister(struct saa7164_tsport *port); | |
366 | ||
367 | /* ----------------------------------------------------------- */ | |
368 | /* saa7164-buffer.c */ | |
369 | extern struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_tsport *port, | |
370 | u32 len); | |
371 | extern int saa7164_buffer_dealloc(struct saa7164_tsport *port, | |
372 | struct saa7164_buffer *buf); | |
373 | ||
374 | /* ----------------------------------------------------------- */ | |
375 | ||
376 | extern unsigned int debug; | |
377 | #define dprintk(level, fmt, arg...)\ | |
378 | do { if (debug & level)\ | |
379 | printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\ | |
380 | } while (0) | |
381 | ||
382 | #define log_warn(fmt, arg...)\ | |
383 | do { \ | |
384 | printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\ | |
385 | } while (0) | |
386 | ||
387 | #define log_err(fmt, arg...)\ | |
388 | do { \ | |
389 | printk(KERN_ERROR "%s: " fmt, dev->name, ## arg);\ | |
390 | } while (0) | |
391 | ||
392 | #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2)) | |
207b42c4 ST |
393 | #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2)) |
394 | ||
443c1228 ST |
395 | |
396 | #define saa7164_readb(reg) readl(dev->bmmio + (reg)) | |
397 | #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg)) | |
398 |