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