saa7164-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010 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. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/kmod.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <asm/div64.h>
  31. #include "saa7164.h"
  32. MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
  33. MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>");
  34. MODULE_LICENSE("GPL");
  35. /*
  36. 1 Basic
  37. 2
  38. 4 i2c
  39. 8 api
  40. 16 cmd
  41. 32 bus
  42. */
  43. unsigned int saa_debug;
  44. module_param_named(debug, saa_debug, int, 0644);
  45. MODULE_PARM_DESC(debug, "enable debug messages");
  46. unsigned int waitsecs = 10;
  47. module_param(waitsecs, int, 0644);
  48. MODULE_PARM_DESC(debug, "timeout on firmware messages");
  49. static unsigned int card[] = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
  50. module_param_array(card, int, NULL, 0444);
  51. MODULE_PARM_DESC(card, "card type");
  52. static unsigned int saa7164_devcount;
  53. static DEFINE_MUTEX(devlist);
  54. LIST_HEAD(saa7164_devlist);
  55. #define INT_SIZE 16
  56. static void saa7164_work_cmdhandler(struct work_struct *w)
  57. {
  58. struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
  59. /* Wake up any complete commands */
  60. saa7164_irq_dequeue(dev);
  61. }
  62. static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
  63. {
  64. struct saa7164_port *port = buf->port;
  65. /* Feed the transport payload into the kernel demux */
  66. dvb_dmx_swfilter_packets(&port->dvb.demux, (u8 *)buf->cpu,
  67. SAA7164_TS_NUMBER_OF_LINES);
  68. }
  69. static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
  70. {
  71. struct saa7164_dev *dev = port->dev;
  72. struct saa7164_buffer *buf;
  73. struct list_head *c, *n;
  74. int wp, i = 0, rp;
  75. /* Find the current write point from the hardware */
  76. wp = saa7164_readl(port->bufcounter);
  77. if (wp > (port->hwcfg.buffercount - 1))
  78. BUG();
  79. /* Find the previous buffer to the current write point */
  80. if (wp == 0)
  81. rp = 7;
  82. else
  83. rp = wp - 1;
  84. /* Lookup the WP in the buffer list */
  85. /* TODO: turn this into a worker thread */
  86. list_for_each_safe(c, n, &port->dmaqueue.list) {
  87. buf = list_entry(c, struct saa7164_buffer, list);
  88. if (i++ > port->hwcfg.buffercount)
  89. BUG();
  90. if (buf->idx == rp) {
  91. /* Found the buffer, deal with it */
  92. dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
  93. __func__, wp, rp);
  94. saa7164_buffer_deliver(buf);
  95. break;
  96. }
  97. }
  98. return 0;
  99. }
  100. /* Primary IRQ handler and dispatch mechanism */
  101. static irqreturn_t saa7164_irq(int irq, void *dev_id)
  102. {
  103. struct saa7164_dev *dev = dev_id;
  104. u32 intid, intstat[INT_SIZE/4];
  105. int i, handled = 0, bit;
  106. if (dev == 0) {
  107. printk(KERN_ERR "%s() No device specified\n", __func__);
  108. handled = 0;
  109. goto out;
  110. }
  111. /* Check that the hardware is accessable. If the status bytes are
  112. * 0xFF then the device is not accessable, the the IRQ belongs
  113. * to another driver.
  114. * 4 x u32 interrupt registers.
  115. */
  116. for (i = 0; i < INT_SIZE/4; i++) {
  117. /* TODO: Convert into saa7164_readl() */
  118. /* Read the 4 hardware interrupt registers */
  119. intstat[i] = saa7164_readl(dev->int_status + (i * 4));
  120. if (intstat[i])
  121. handled = 1;
  122. }
  123. if (handled == 0)
  124. goto out;
  125. /* For each of the HW interrupt registers */
  126. for (i = 0; i < INT_SIZE/4; i++) {
  127. if (intstat[i]) {
  128. /* Each function of the board has it's own interruptid.
  129. * Find the function that triggered then call
  130. * it's handler.
  131. */
  132. for (bit = 0; bit < 32; bit++) {
  133. if (((intstat[i] >> bit) & 0x00000001) == 0)
  134. continue;
  135. /* Calculate the interrupt id (0x00 to 0x7f) */
  136. intid = (i * 32) + bit;
  137. if (intid == dev->intfdesc.bInterruptId) {
  138. /* A response to an cmd/api call */
  139. schedule_work(&dev->workcmd);
  140. } else if (intid ==
  141. dev->ts1.hwcfg.interruptid) {
  142. /* Transport path 1 */
  143. saa7164_irq_ts(&dev->ts1);
  144. } else if (intid ==
  145. dev->ts2.hwcfg.interruptid) {
  146. /* Transport path 2 */
  147. saa7164_irq_ts(&dev->ts2);
  148. } else {
  149. /* Find the function */
  150. dprintk(DBGLVL_IRQ,
  151. "%s() unhandled interrupt "
  152. "reg 0x%x bit 0x%x "
  153. "intid = 0x%x\n",
  154. __func__, i, bit, intid);
  155. }
  156. }
  157. /* Ack it */
  158. saa7164_writel(dev->int_ack + (i * 4), intstat[i]);
  159. }
  160. }
  161. out:
  162. return IRQ_RETVAL(handled);
  163. }
  164. void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
  165. {
  166. struct saa7164_fw_status *s = &dev->fw_status;
  167. dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS);
  168. dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE);
  169. dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC);
  170. dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST);
  171. dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD);
  172. dev->fw_status.remainheap =
  173. saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP);
  174. dprintk(1, "Firmware status:\n");
  175. dprintk(1, " .status = 0x%08x\n", s->status);
  176. dprintk(1, " .mode = 0x%08x\n", s->mode);
  177. dprintk(1, " .spec = 0x%08x\n", s->spec);
  178. dprintk(1, " .inst = 0x%08x\n", s->inst);
  179. dprintk(1, " .cpuload = 0x%08x\n", s->cpuload);
  180. dprintk(1, " .remainheap = 0x%08x\n", s->remainheap);
  181. }
  182. u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
  183. {
  184. u32 reg;
  185. reg = saa7164_readl(SAA_DEVICE_VERSION);
  186. dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
  187. (reg & 0x0000fc00) >> 10,
  188. (reg & 0x000003e0) >> 5,
  189. (reg & 0x0000001f),
  190. (reg & 0xffff0000) >> 16,
  191. reg);
  192. return reg;
  193. }
  194. /* TODO: Debugging func, remove */
  195. void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len)
  196. {
  197. int i;
  198. printk(KERN_INFO "--------------------> "
  199. "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
  200. for (i = 0; i < len; i += 16)
  201. printk(KERN_INFO " [0x%08x] "
  202. "%02x %02x %02x %02x %02x %02x %02x %02x "
  203. "%02x %02x %02x %02x %02x %02x %02x %02x\n", i,
  204. *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3),
  205. *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7),
  206. *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11),
  207. *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15));
  208. }
  209. /* TODO: Debugging func, remove */
  210. void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
  211. {
  212. int i;
  213. dprintk(1, "--------------------> "
  214. "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
  215. for (i = 0; i < 0x100; i += 16)
  216. dprintk(1, "region0[0x%08x] = "
  217. "%02x %02x %02x %02x %02x %02x %02x %02x"
  218. " %02x %02x %02x %02x %02x %02x %02x %02x\n", i,
  219. (u8)saa7164_readb(addr + i + 0),
  220. (u8)saa7164_readb(addr + i + 1),
  221. (u8)saa7164_readb(addr + i + 2),
  222. (u8)saa7164_readb(addr + i + 3),
  223. (u8)saa7164_readb(addr + i + 4),
  224. (u8)saa7164_readb(addr + i + 5),
  225. (u8)saa7164_readb(addr + i + 6),
  226. (u8)saa7164_readb(addr + i + 7),
  227. (u8)saa7164_readb(addr + i + 8),
  228. (u8)saa7164_readb(addr + i + 9),
  229. (u8)saa7164_readb(addr + i + 10),
  230. (u8)saa7164_readb(addr + i + 11),
  231. (u8)saa7164_readb(addr + i + 12),
  232. (u8)saa7164_readb(addr + i + 13),
  233. (u8)saa7164_readb(addr + i + 14),
  234. (u8)saa7164_readb(addr + i + 15)
  235. );
  236. }
  237. static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
  238. {
  239. dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n",
  240. &dev->hwdesc, (u32)sizeof(tmComResHWDescr_t));
  241. dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
  242. dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
  243. dprintk(1, " .bDescriptorSubtype = 0x%x\n",
  244. dev->hwdesc.bDescriptorSubtype);
  245. dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion);
  246. dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency);
  247. dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes);
  248. dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities);
  249. dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
  250. dev->hwdesc.dwDeviceRegistersLocation);
  251. dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
  252. dev->hwdesc.dwHostMemoryRegion);
  253. dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
  254. dev->hwdesc.dwHostMemoryRegionSize);
  255. dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
  256. dev->hwdesc.dwHostHibernatMemRegion);
  257. dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
  258. dev->hwdesc.dwHostHibernatMemRegionSize);
  259. }
  260. static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
  261. {
  262. dprintk(1, "@0x%p intfdesc "
  263. "sizeof(tmComResInterfaceDescr_t) = %d bytes\n",
  264. &dev->intfdesc, (u32)sizeof(tmComResInterfaceDescr_t));
  265. dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
  266. dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
  267. dprintk(1, " .bDescriptorSubtype = 0x%x\n",
  268. dev->intfdesc.bDescriptorSubtype);
  269. dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags);
  270. dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType);
  271. dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId);
  272. dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface);
  273. dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId);
  274. dprintk(1, " .bDebugInterruptId = 0x%x\n",
  275. dev->intfdesc.bDebugInterruptId);
  276. dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
  277. }
  278. static void saa7164_dump_busdesc(struct saa7164_dev *dev)
  279. {
  280. dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n",
  281. &dev->busdesc, (u32)sizeof(tmComResBusDescr_t));
  282. dprintk(1, " .CommandRing = 0x%016Lx\n", dev->busdesc.CommandRing);
  283. dprintk(1, " .ResponseRing = 0x%016Lx\n", dev->busdesc.ResponseRing);
  284. dprintk(1, " .CommandWrite = 0x%x\n", dev->busdesc.CommandWrite);
  285. dprintk(1, " .CommandRead = 0x%x\n", dev->busdesc.CommandRead);
  286. dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite);
  287. dprintk(1, " .ResponseRead = 0x%x\n", dev->busdesc.ResponseRead);
  288. }
  289. /* Much of the hardware configuration and PCI registers are configured
  290. * dynamically depending on firmware. We have to cache some initial
  291. * structures then use these to locate other important structures
  292. * from PCI space.
  293. */
  294. static void saa7164_get_descriptors(struct saa7164_dev *dev)
  295. {
  296. memcpy(&dev->hwdesc, dev->bmmio, sizeof(tmComResHWDescr_t));
  297. memcpy(&dev->intfdesc, dev->bmmio + sizeof(tmComResHWDescr_t),
  298. sizeof(tmComResInterfaceDescr_t));
  299. memcpy(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
  300. sizeof(tmComResBusDescr_t));
  301. if (dev->hwdesc.bLength != sizeof(tmComResHWDescr_t)) {
  302. printk(KERN_ERR "Structure tmComResHWDescr_t is mangled\n");
  303. printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength,
  304. (u32)sizeof(tmComResHWDescr_t));
  305. } else
  306. saa7164_dump_hwdesc(dev);
  307. if (dev->intfdesc.bLength != sizeof(tmComResInterfaceDescr_t)) {
  308. printk(KERN_ERR "struct tmComResInterfaceDescr_t is mangled\n");
  309. printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength,
  310. (u32)sizeof(tmComResInterfaceDescr_t));
  311. } else
  312. saa7164_dump_intfdesc(dev);
  313. saa7164_dump_busdesc(dev);
  314. }
  315. static int saa7164_pci_quirks(struct saa7164_dev *dev)
  316. {
  317. return 0;
  318. }
  319. static int get_resources(struct saa7164_dev *dev)
  320. {
  321. if (request_mem_region(pci_resource_start(dev->pci, 0),
  322. pci_resource_len(dev->pci, 0), dev->name)) {
  323. if (request_mem_region(pci_resource_start(dev->pci, 2),
  324. pci_resource_len(dev->pci, 2), dev->name))
  325. return 0;
  326. }
  327. printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
  328. dev->name,
  329. (u64)pci_resource_start(dev->pci, 0),
  330. (u64)pci_resource_start(dev->pci, 2));
  331. return -EBUSY;
  332. }
  333. static int saa7164_dev_setup(struct saa7164_dev *dev)
  334. {
  335. int i;
  336. mutex_init(&dev->lock);
  337. atomic_inc(&dev->refcount);
  338. dev->nr = saa7164_devcount++;
  339. sprintf(dev->name, "saa7164[%d]", dev->nr);
  340. mutex_lock(&devlist);
  341. list_add_tail(&dev->devlist, &saa7164_devlist);
  342. mutex_unlock(&devlist);
  343. /* board config */
  344. dev->board = UNSET;
  345. if (card[dev->nr] < saa7164_bcount)
  346. dev->board = card[dev->nr];
  347. for (i = 0; UNSET == dev->board && i < saa7164_idcount; i++)
  348. if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor &&
  349. dev->pci->subsystem_device ==
  350. saa7164_subids[i].subdevice)
  351. dev->board = saa7164_subids[i].card;
  352. if (UNSET == dev->board) {
  353. dev->board = SAA7164_BOARD_UNKNOWN;
  354. saa7164_card_list(dev);
  355. }
  356. dev->pci_bus = dev->pci->bus->number;
  357. dev->pci_slot = PCI_SLOT(dev->pci->devfn);
  358. /* I2C Defaults / setup */
  359. dev->i2c_bus[0].dev = dev;
  360. dev->i2c_bus[0].nr = 0;
  361. dev->i2c_bus[1].dev = dev;
  362. dev->i2c_bus[1].nr = 1;
  363. dev->i2c_bus[2].dev = dev;
  364. dev->i2c_bus[2].nr = 2;
  365. /* Transport port A Defaults / setup */
  366. dev->ts1.dev = dev;
  367. dev->ts1.nr = 0;
  368. dev->ts1.type = SAA7164_MPEG_UNDEFINED;
  369. mutex_init(&dev->ts1.dvb.lock);
  370. INIT_LIST_HEAD(&dev->ts1.dmaqueue.list);
  371. mutex_init(&dev->ts1.dmaqueue_lock);
  372. /* Transport port B Defaults / setup */
  373. dev->ts2.dev = dev;
  374. dev->ts2.nr = 1;
  375. dev->ts2.type = SAA7164_MPEG_UNDEFINED;
  376. mutex_init(&dev->ts2.dvb.lock);
  377. INIT_LIST_HEAD(&dev->ts2.dmaqueue.list);
  378. mutex_init(&dev->ts2.dmaqueue_lock);
  379. if (get_resources(dev) < 0) {
  380. printk(KERN_ERR "CORE %s No more PCIe resources for "
  381. "subsystem: %04x:%04x\n",
  382. dev->name, dev->pci->subsystem_vendor,
  383. dev->pci->subsystem_device);
  384. saa7164_devcount--;
  385. return -ENODEV;
  386. }
  387. /* PCI/e allocations */
  388. dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
  389. pci_resource_len(dev->pci, 0));
  390. dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
  391. pci_resource_len(dev->pci, 2));
  392. dev->bmmio = (u8 __iomem *)dev->lmmio;
  393. dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
  394. /* Inerrupt and ack register locations offset of bmmio */
  395. dev->int_status = 0x183000 + 0xf80;
  396. dev->int_ack = 0x183000 + 0xf90;
  397. printk(KERN_INFO
  398. "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
  399. dev->name, dev->pci->subsystem_vendor,
  400. dev->pci->subsystem_device, saa7164_boards[dev->board].name,
  401. dev->board, card[dev->nr] == dev->board ?
  402. "insmod option" : "autodetected");
  403. saa7164_pci_quirks(dev);
  404. return 0;
  405. }
  406. static void saa7164_dev_unregister(struct saa7164_dev *dev)
  407. {
  408. dprintk(1, "%s()\n", __func__);
  409. release_mem_region(pci_resource_start(dev->pci, 0),
  410. pci_resource_len(dev->pci, 0));
  411. release_mem_region(pci_resource_start(dev->pci, 2),
  412. pci_resource_len(dev->pci, 2));
  413. if (!atomic_dec_and_test(&dev->refcount))
  414. return;
  415. iounmap(dev->lmmio);
  416. iounmap(dev->lmmio2);
  417. return;
  418. }
  419. static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
  420. const struct pci_device_id *pci_id)
  421. {
  422. struct saa7164_dev *dev;
  423. int err, i;
  424. u32 version;
  425. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  426. if (NULL == dev)
  427. return -ENOMEM;
  428. /* pci init */
  429. dev->pci = pci_dev;
  430. if (pci_enable_device(pci_dev)) {
  431. err = -EIO;
  432. goto fail_free;
  433. }
  434. if (saa7164_dev_setup(dev) < 0) {
  435. err = -EINVAL;
  436. goto fail_free;
  437. }
  438. /* print pci info */
  439. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  440. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  441. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  442. "latency: %d, mmio: 0x%llx\n", dev->name,
  443. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  444. dev->pci_lat,
  445. (unsigned long long)pci_resource_start(pci_dev, 0));
  446. pci_set_master(pci_dev);
  447. /* TODO */
  448. if (!pci_dma_supported(pci_dev, 0xffffffff)) {
  449. printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
  450. err = -EIO;
  451. goto fail_irq;
  452. }
  453. err = request_irq(pci_dev->irq, saa7164_irq,
  454. IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
  455. if (err < 0) {
  456. printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
  457. pci_dev->irq);
  458. err = -EIO;
  459. goto fail_irq;
  460. }
  461. pci_set_drvdata(pci_dev, dev);
  462. /* Init the internal command list */
  463. for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
  464. dev->cmds[i].seqno = i;
  465. dev->cmds[i].inuse = 0;
  466. mutex_init(&dev->cmds[i].lock);
  467. init_waitqueue_head(&dev->cmds[i].wait);
  468. }
  469. /* We need a deferred interrupt handler for cmd handling */
  470. INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
  471. /* Only load the firmware if we know the board */
  472. if (dev->board != SAA7164_BOARD_UNKNOWN) {
  473. err = saa7164_downloadfirmware(dev);
  474. if (err < 0) {
  475. printk(KERN_ERR
  476. "Failed to boot firmware, no features "
  477. "registered\n");
  478. goto fail_fw;
  479. }
  480. saa7164_get_descriptors(dev);
  481. saa7164_dumpregs(dev, 0);
  482. saa7164_getcurrentfirmwareversion(dev);
  483. saa7164_getfirmwarestatus(dev);
  484. err = saa7164_bus_setup(dev);
  485. if (err < 0)
  486. printk(KERN_ERR
  487. "Failed to setup the bus, will continue\n");
  488. saa7164_bus_dump(dev);
  489. /* Ping the running firmware via the command bus and get the
  490. * firmware version, this checks the bus is running OK.
  491. */
  492. version = 0;
  493. if (saa7164_api_get_fw_version(dev, &version) == SAA_OK)
  494. dprintk(1, "Bus is operating correctly using "
  495. "version %d.%d.%d.%d (0x%x)\n",
  496. (version & 0x0000fc00) >> 10,
  497. (version & 0x000003e0) >> 5,
  498. (version & 0x0000001f),
  499. (version & 0xffff0000) >> 16,
  500. version);
  501. else
  502. printk(KERN_ERR
  503. "Failed to communicate with the firmware\n");
  504. /* Bring up the I2C buses */
  505. saa7164_i2c_register(&dev->i2c_bus[0]);
  506. saa7164_i2c_register(&dev->i2c_bus[1]);
  507. saa7164_i2c_register(&dev->i2c_bus[2]);
  508. saa7164_gpio_setup(dev);
  509. saa7164_card_setup(dev);
  510. /* Parse the dynamic device configuration, find various
  511. * media endpoints (MPEG, WMV, PS, TS) and cache their
  512. * configuration details into the driver, so we can
  513. * reference them later during simething_register() func,
  514. * interrupt handlers, deferred work handlers etc.
  515. */
  516. saa7164_api_enum_subdevs(dev);
  517. /* Begin to create the video sub-systems and register funcs */
  518. if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
  519. if (saa7164_dvb_register(&dev->ts1) < 0) {
  520. printk(KERN_ERR "%s() Failed to register "
  521. "dvb adapters on porta\n",
  522. __func__);
  523. }
  524. }
  525. if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
  526. if (saa7164_dvb_register(&dev->ts2) < 0) {
  527. printk(KERN_ERR"%s() Failed to register "
  528. "dvb adapters on portb\n",
  529. __func__);
  530. }
  531. }
  532. } /* != BOARD_UNKNOWN */
  533. else
  534. printk(KERN_ERR "%s() Unsupported board detected, "
  535. "registering without firmware\n", __func__);
  536. dprintk(1, "%s() parameter debug = %d\n", __func__, saa_debug);
  537. dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs);
  538. fail_fw:
  539. return 0;
  540. fail_irq:
  541. saa7164_dev_unregister(dev);
  542. fail_free:
  543. kfree(dev);
  544. return err;
  545. }
  546. static void saa7164_shutdown(struct saa7164_dev *dev)
  547. {
  548. dprintk(1, "%s()\n", __func__);
  549. }
  550. static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
  551. {
  552. struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
  553. saa7164_shutdown(dev);
  554. if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
  555. saa7164_dvb_unregister(&dev->ts1);
  556. if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
  557. saa7164_dvb_unregister(&dev->ts2);
  558. saa7164_i2c_unregister(&dev->i2c_bus[0]);
  559. saa7164_i2c_unregister(&dev->i2c_bus[1]);
  560. saa7164_i2c_unregister(&dev->i2c_bus[2]);
  561. pci_disable_device(pci_dev);
  562. /* unregister stuff */
  563. free_irq(pci_dev->irq, dev);
  564. pci_set_drvdata(pci_dev, NULL);
  565. mutex_lock(&devlist);
  566. list_del(&dev->devlist);
  567. mutex_unlock(&devlist);
  568. saa7164_dev_unregister(dev);
  569. kfree(dev);
  570. }
  571. static struct pci_device_id saa7164_pci_tbl[] = {
  572. {
  573. /* SAA7164 */
  574. .vendor = 0x1131,
  575. .device = 0x7164,
  576. .subvendor = PCI_ANY_ID,
  577. .subdevice = PCI_ANY_ID,
  578. }, {
  579. /* --- end of list --- */
  580. }
  581. };
  582. MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
  583. static struct pci_driver saa7164_pci_driver = {
  584. .name = "saa7164",
  585. .id_table = saa7164_pci_tbl,
  586. .probe = saa7164_initdev,
  587. .remove = __devexit_p(saa7164_finidev),
  588. /* TODO */
  589. .suspend = NULL,
  590. .resume = NULL,
  591. };
  592. static int __init saa7164_init(void)
  593. {
  594. printk(KERN_INFO "saa7164 driver loaded\n");
  595. return pci_register_driver(&saa7164_pci_driver);
  596. }
  597. static void __exit saa7164_fini(void)
  598. {
  599. pci_unregister_driver(&saa7164_pci_driver);
  600. }
  601. module_init(saa7164_init);
  602. module_exit(saa7164_fini);