cx25821-video-upstream.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. *
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include "cx25821-video.h"
  24. #include "cx25821-video-upstream.h"
  25. #include <linux/fs.h>
  26. #include <linux/errno.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/file.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/slab.h>
  34. #include <linux/uaccess.h>
  35. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  36. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  37. MODULE_LICENSE("GPL");
  38. static int _intr_msk = FLD_VID_SRC_RISC1 | FLD_VID_SRC_UF | FLD_VID_SRC_SYNC |
  39. FLD_VID_SRC_OPC_ERR;
  40. int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
  41. struct sram_channel *ch,
  42. unsigned int bpl, u32 risc)
  43. {
  44. unsigned int i, lines;
  45. u32 cdt;
  46. if (ch->cmds_start == 0) {
  47. cx_write(ch->ptr1_reg, 0);
  48. cx_write(ch->ptr2_reg, 0);
  49. cx_write(ch->cnt2_reg, 0);
  50. cx_write(ch->cnt1_reg, 0);
  51. return 0;
  52. }
  53. bpl = (bpl + 7) & ~7; /* alignment */
  54. cdt = ch->cdt;
  55. lines = ch->fifo_size / bpl;
  56. if (lines > 4)
  57. lines = 4;
  58. BUG_ON(lines < 2);
  59. /* write CDT */
  60. for (i = 0; i < lines; i++) {
  61. cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
  62. cx_write(cdt + 16 * i + 4, 0);
  63. cx_write(cdt + 16 * i + 8, 0);
  64. cx_write(cdt + 16 * i + 12, 0);
  65. }
  66. /* write CMDS */
  67. cx_write(ch->cmds_start + 0, risc);
  68. cx_write(ch->cmds_start + 4, 0);
  69. cx_write(ch->cmds_start + 8, cdt);
  70. cx_write(ch->cmds_start + 12, (lines * 16) >> 3);
  71. cx_write(ch->cmds_start + 16, ch->ctrl_start);
  72. cx_write(ch->cmds_start + 20, VID_IQ_SIZE_DW);
  73. for (i = 24; i < 80; i += 4)
  74. cx_write(ch->cmds_start + i, 0);
  75. /* fill registers */
  76. cx_write(ch->ptr1_reg, ch->fifo_start);
  77. cx_write(ch->ptr2_reg, cdt);
  78. cx_write(ch->cnt2_reg, (lines * 16) >> 3);
  79. cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
  80. return 0;
  81. }
  82. static __le32 *cx25821_update_riscprogram(struct cx25821_dev *dev,
  83. __le32 *rp, unsigned int offset,
  84. unsigned int bpl, u32 sync_line,
  85. unsigned int lines, int fifo_enable,
  86. int field_type)
  87. {
  88. unsigned int line, i;
  89. int dist_betwn_starts = bpl * 2;
  90. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  91. if (USE_RISC_NOOP_VIDEO) {
  92. for (i = 0; i < NUM_NO_OPS; i++)
  93. *(rp++) = cpu_to_le32(RISC_NOOP);
  94. }
  95. /* scan lines */
  96. for (line = 0; line < lines; line++) {
  97. *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
  98. *(rp++) = cpu_to_le32(dev->_data_buf_phys_addr + offset);
  99. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  100. if ((lines <= NTSC_FIELD_HEIGHT)
  101. || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC)) {
  102. offset += dist_betwn_starts;
  103. }
  104. }
  105. return rp;
  106. }
  107. static __le32 *cx25821_risc_field_upstream(struct cx25821_dev *dev, __le32 * rp,
  108. dma_addr_t databuf_phys_addr,
  109. unsigned int offset, u32 sync_line,
  110. unsigned int bpl, unsigned int lines,
  111. int fifo_enable, int field_type)
  112. {
  113. unsigned int line, i;
  114. struct sram_channel *sram_ch =
  115. dev->channels[dev->_channel_upstream_select].sram_channels;
  116. int dist_betwn_starts = bpl * 2;
  117. /* sync instruction */
  118. if (sync_line != NO_SYNC_LINE)
  119. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  120. if (USE_RISC_NOOP_VIDEO) {
  121. for (i = 0; i < NUM_NO_OPS; i++)
  122. *(rp++) = cpu_to_le32(RISC_NOOP);
  123. }
  124. /* scan lines */
  125. for (line = 0; line < lines; line++) {
  126. *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
  127. *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
  128. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  129. if ((lines <= NTSC_FIELD_HEIGHT)
  130. || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC))
  131. /* to skip the other field line */
  132. offset += dist_betwn_starts;
  133. /* check if we need to enable the FIFO after the first 4 lines
  134. * For the upstream video channel, the risc engine will enable
  135. * the FIFO. */
  136. if (fifo_enable && line == 3) {
  137. *(rp++) = RISC_WRITECR;
  138. *(rp++) = sram_ch->dma_ctl;
  139. *(rp++) = FLD_VID_FIFO_EN;
  140. *(rp++) = 0x00000001;
  141. }
  142. }
  143. return rp;
  144. }
  145. static int cx25821_risc_buffer_upstream(struct cx25821_dev *dev,
  146. struct pci_dev *pci,
  147. unsigned int top_offset,
  148. unsigned int bpl, unsigned int lines)
  149. {
  150. __le32 *rp;
  151. int fifo_enable = 0;
  152. /* get line count for single field */
  153. int singlefield_lines = lines >> 1;
  154. int odd_num_lines = singlefield_lines;
  155. int frame = 0;
  156. int frame_size = 0;
  157. int databuf_offset = 0;
  158. int risc_program_size = 0;
  159. int risc_flag = RISC_CNT_RESET;
  160. unsigned int bottom_offset = bpl;
  161. dma_addr_t risc_phys_jump_addr;
  162. if (dev->_isNTSC) {
  163. odd_num_lines = singlefield_lines + 1;
  164. risc_program_size = FRAME1_VID_PROG_SIZE;
  165. frame_size = (bpl == Y411_LINE_SZ) ?
  166. FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
  167. } else {
  168. risc_program_size = PAL_VID_PROG_SIZE;
  169. frame_size = (bpl == Y411_LINE_SZ) ?
  170. FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
  171. }
  172. /* Virtual address of Risc buffer program */
  173. rp = dev->_dma_virt_addr;
  174. for (frame = 0; frame < NUM_FRAMES; frame++) {
  175. databuf_offset = frame_size * frame;
  176. if (UNSET != top_offset) {
  177. fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
  178. rp = cx25821_risc_field_upstream(dev, rp,
  179. dev->_data_buf_phys_addr +
  180. databuf_offset, top_offset, 0, bpl,
  181. odd_num_lines, fifo_enable, ODD_FIELD);
  182. }
  183. fifo_enable = FIFO_DISABLE;
  184. /* Even Field */
  185. rp = cx25821_risc_field_upstream(dev, rp,
  186. dev->_data_buf_phys_addr +
  187. databuf_offset, bottom_offset,
  188. 0x200, bpl, singlefield_lines,
  189. fifo_enable, EVEN_FIELD);
  190. if (frame == 0) {
  191. risc_flag = RISC_CNT_RESET;
  192. risc_phys_jump_addr = dev->_dma_phys_start_addr +
  193. risc_program_size;
  194. } else {
  195. risc_phys_jump_addr = dev->_dma_phys_start_addr;
  196. risc_flag = RISC_CNT_INC;
  197. }
  198. /* Loop to 2ndFrameRISC or to Start of Risc
  199. * program & generate IRQ
  200. */
  201. *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
  202. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  203. *(rp++) = cpu_to_le32(0);
  204. }
  205. return 0;
  206. }
  207. void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev)
  208. {
  209. struct sram_channel *sram_ch =
  210. dev->channels[VID_UPSTREAM_SRAM_CHANNEL_I].sram_channels;
  211. u32 tmp = 0;
  212. if (!dev->_is_running) {
  213. pr_info("No video file is currently running so return!\n");
  214. return;
  215. }
  216. /* Disable RISC interrupts */
  217. tmp = cx_read(sram_ch->int_msk);
  218. cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
  219. /* Turn OFF risc and fifo enable */
  220. tmp = cx_read(sram_ch->dma_ctl);
  221. cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
  222. /* Clear data buffer memory */
  223. if (dev->_data_buf_virt_addr)
  224. memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
  225. dev->_is_running = 0;
  226. dev->_is_first_frame = 0;
  227. dev->_frame_count = 0;
  228. dev->_file_status = END_OF_FILE;
  229. kfree(dev->_irq_queues);
  230. dev->_irq_queues = NULL;
  231. kfree(dev->_filename);
  232. tmp = cx_read(VID_CH_MODE_SEL);
  233. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  234. }
  235. void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev)
  236. {
  237. if (dev->_is_running)
  238. cx25821_stop_upstream_video_ch1(dev);
  239. if (dev->_dma_virt_addr) {
  240. pci_free_consistent(dev->pci, dev->_risc_size,
  241. dev->_dma_virt_addr, dev->_dma_phys_addr);
  242. dev->_dma_virt_addr = NULL;
  243. }
  244. if (dev->_data_buf_virt_addr) {
  245. pci_free_consistent(dev->pci, dev->_data_buf_size,
  246. dev->_data_buf_virt_addr,
  247. dev->_data_buf_phys_addr);
  248. dev->_data_buf_virt_addr = NULL;
  249. }
  250. }
  251. static int cx25821_get_frame(struct cx25821_dev *dev,
  252. struct sram_channel *sram_ch)
  253. {
  254. struct file *myfile;
  255. int frame_index_temp = dev->_frame_index;
  256. int i = 0;
  257. int line_size = (dev->_pixel_format == PIXEL_FRMT_411) ?
  258. Y411_LINE_SZ : Y422_LINE_SZ;
  259. int frame_size = 0;
  260. int frame_offset = 0;
  261. ssize_t vfs_read_retval = 0;
  262. char mybuf[line_size];
  263. loff_t file_offset;
  264. loff_t pos;
  265. mm_segment_t old_fs;
  266. if (dev->_file_status == END_OF_FILE)
  267. return 0;
  268. if (dev->_isNTSC)
  269. frame_size = (line_size == Y411_LINE_SZ) ?
  270. FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
  271. else
  272. frame_size = (line_size == Y411_LINE_SZ) ?
  273. FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
  274. frame_offset = (frame_index_temp > 0) ? frame_size : 0;
  275. file_offset = dev->_frame_count * frame_size;
  276. myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
  277. if (IS_ERR(myfile)) {
  278. const int open_errno = -PTR_ERR(myfile);
  279. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  280. __func__, dev->_filename, open_errno);
  281. return PTR_ERR(myfile);
  282. } else {
  283. if (!(myfile->f_op)) {
  284. pr_err("%s(): File has no file operations registered!\n",
  285. __func__);
  286. filp_close(myfile, NULL);
  287. return -EIO;
  288. }
  289. if (!myfile->f_op->read) {
  290. pr_err("%s(): File has no READ operations registered!\n",
  291. __func__);
  292. filp_close(myfile, NULL);
  293. return -EIO;
  294. }
  295. pos = myfile->f_pos;
  296. old_fs = get_fs();
  297. set_fs(KERNEL_DS);
  298. for (i = 0; i < dev->_lines_count; i++) {
  299. pos = file_offset;
  300. vfs_read_retval = vfs_read(myfile, mybuf, line_size,
  301. &pos);
  302. if (vfs_read_retval > 0 && vfs_read_retval == line_size
  303. && dev->_data_buf_virt_addr != NULL) {
  304. memcpy((void *)(dev->_data_buf_virt_addr +
  305. frame_offset / 4), mybuf,
  306. vfs_read_retval);
  307. }
  308. file_offset += vfs_read_retval;
  309. frame_offset += vfs_read_retval;
  310. if (vfs_read_retval < line_size) {
  311. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  312. __func__);
  313. break;
  314. }
  315. }
  316. if (i > 0)
  317. dev->_frame_count++;
  318. dev->_file_status = (vfs_read_retval == line_size) ?
  319. IN_PROGRESS : END_OF_FILE;
  320. set_fs(old_fs);
  321. filp_close(myfile, NULL);
  322. }
  323. return 0;
  324. }
  325. static void cx25821_vidups_handler(struct work_struct *work)
  326. {
  327. struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
  328. _irq_work_entry);
  329. if (!dev) {
  330. pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
  331. __func__);
  332. return;
  333. }
  334. cx25821_get_frame(dev, dev->channels[dev->_channel_upstream_select].
  335. sram_channels);
  336. }
  337. static int cx25821_openfile(struct cx25821_dev *dev,
  338. struct sram_channel *sram_ch)
  339. {
  340. struct file *myfile;
  341. int i = 0, j = 0;
  342. int line_size = (dev->_pixel_format == PIXEL_FRMT_411) ?
  343. Y411_LINE_SZ : Y422_LINE_SZ;
  344. ssize_t vfs_read_retval = 0;
  345. char mybuf[line_size];
  346. loff_t pos;
  347. loff_t offset = (unsigned long)0;
  348. mm_segment_t old_fs;
  349. myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
  350. if (IS_ERR(myfile)) {
  351. const int open_errno = -PTR_ERR(myfile);
  352. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  353. __func__, dev->_filename, open_errno);
  354. return PTR_ERR(myfile);
  355. } else {
  356. if (!(myfile->f_op)) {
  357. pr_err("%s(): File has no file operations registered!\n",
  358. __func__);
  359. filp_close(myfile, NULL);
  360. return -EIO;
  361. }
  362. if (!myfile->f_op->read) {
  363. pr_err("%s(): File has no READ operations registered! Returning\n",
  364. __func__);
  365. filp_close(myfile, NULL);
  366. return -EIO;
  367. }
  368. pos = myfile->f_pos;
  369. old_fs = get_fs();
  370. set_fs(KERNEL_DS);
  371. for (j = 0; j < NUM_FRAMES; j++) {
  372. for (i = 0; i < dev->_lines_count; i++) {
  373. pos = offset;
  374. vfs_read_retval = vfs_read(myfile, mybuf,
  375. line_size, &pos);
  376. if (vfs_read_retval > 0
  377. && vfs_read_retval == line_size
  378. && dev->_data_buf_virt_addr != NULL) {
  379. memcpy((void *)(dev->
  380. _data_buf_virt_addr +
  381. offset / 4), mybuf,
  382. vfs_read_retval);
  383. }
  384. offset += vfs_read_retval;
  385. if (vfs_read_retval < line_size) {
  386. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  387. __func__);
  388. break;
  389. }
  390. }
  391. if (i > 0)
  392. dev->_frame_count++;
  393. if (vfs_read_retval < line_size)
  394. break;
  395. }
  396. dev->_file_status = (vfs_read_retval == line_size) ?
  397. IN_PROGRESS : END_OF_FILE;
  398. set_fs(old_fs);
  399. myfile->f_pos = 0;
  400. filp_close(myfile, NULL);
  401. }
  402. return 0;
  403. }
  404. static int cx25821_upstream_buffer_prepare(struct cx25821_dev *dev,
  405. struct sram_channel *sram_ch,
  406. int bpl)
  407. {
  408. int ret = 0;
  409. dma_addr_t dma_addr;
  410. dma_addr_t data_dma_addr;
  411. if (dev->_dma_virt_addr != NULL)
  412. pci_free_consistent(dev->pci, dev->upstream_riscbuf_size,
  413. dev->_dma_virt_addr, dev->_dma_phys_addr);
  414. dev->_dma_virt_addr = pci_alloc_consistent(dev->pci,
  415. dev->upstream_riscbuf_size, &dma_addr);
  416. dev->_dma_virt_start_addr = dev->_dma_virt_addr;
  417. dev->_dma_phys_start_addr = dma_addr;
  418. dev->_dma_phys_addr = dma_addr;
  419. dev->_risc_size = dev->upstream_riscbuf_size;
  420. if (!dev->_dma_virt_addr) {
  421. pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
  422. return -ENOMEM;
  423. }
  424. /* Clear memory at address */
  425. memset(dev->_dma_virt_addr, 0, dev->_risc_size);
  426. if (dev->_data_buf_virt_addr != NULL)
  427. pci_free_consistent(dev->pci, dev->upstream_databuf_size,
  428. dev->_data_buf_virt_addr,
  429. dev->_data_buf_phys_addr);
  430. /* For Video Data buffer allocation */
  431. dev->_data_buf_virt_addr = pci_alloc_consistent(dev->pci,
  432. dev->upstream_databuf_size, &data_dma_addr);
  433. dev->_data_buf_phys_addr = data_dma_addr;
  434. dev->_data_buf_size = dev->upstream_databuf_size;
  435. if (!dev->_data_buf_virt_addr) {
  436. pr_err("FAILED to allocate memory for data buffer! Returning\n");
  437. return -ENOMEM;
  438. }
  439. /* Clear memory at address */
  440. memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
  441. ret = cx25821_openfile(dev, sram_ch);
  442. if (ret < 0)
  443. return ret;
  444. /* Create RISC programs */
  445. ret = cx25821_risc_buffer_upstream(dev, dev->pci, 0, bpl,
  446. dev->_lines_count);
  447. if (ret < 0) {
  448. pr_info("Failed creating Video Upstream Risc programs!\n");
  449. goto error;
  450. }
  451. return 0;
  452. error:
  453. return ret;
  454. }
  455. static int cx25821_video_upstream_irq(struct cx25821_dev *dev, int chan_num,
  456. u32 status)
  457. {
  458. u32 int_msk_tmp;
  459. struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  460. int singlefield_lines = NTSC_FIELD_HEIGHT;
  461. int line_size_in_bytes = Y422_LINE_SZ;
  462. int odd_risc_prog_size = 0;
  463. dma_addr_t risc_phys_jump_addr;
  464. __le32 *rp;
  465. if (status & FLD_VID_SRC_RISC1) {
  466. /* We should only process one program per call */
  467. u32 prog_cnt = cx_read(channel->gpcnt);
  468. /* Since we've identified our IRQ, clear our bits from the
  469. * interrupt mask and interrupt status registers */
  470. int_msk_tmp = cx_read(channel->int_msk);
  471. cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
  472. cx_write(channel->int_stat, _intr_msk);
  473. spin_lock(&dev->slock);
  474. dev->_frame_index = prog_cnt;
  475. queue_work(dev->_irq_queues, &dev->_irq_work_entry);
  476. if (dev->_is_first_frame) {
  477. dev->_is_first_frame = 0;
  478. if (dev->_isNTSC) {
  479. singlefield_lines += 1;
  480. odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
  481. } else {
  482. singlefield_lines = PAL_FIELD_HEIGHT;
  483. odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
  484. }
  485. if (dev->_dma_virt_start_addr != NULL) {
  486. line_size_in_bytes =
  487. (dev->_pixel_format ==
  488. PIXEL_FRMT_411) ? Y411_LINE_SZ :
  489. Y422_LINE_SZ;
  490. risc_phys_jump_addr =
  491. dev->_dma_phys_start_addr +
  492. odd_risc_prog_size;
  493. rp = cx25821_update_riscprogram(dev,
  494. dev->_dma_virt_start_addr, TOP_OFFSET,
  495. line_size_in_bytes, 0x0,
  496. singlefield_lines, FIFO_DISABLE,
  497. ODD_FIELD);
  498. /* Jump to Even Risc program of 1st Frame */
  499. *(rp++) = cpu_to_le32(RISC_JUMP);
  500. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  501. *(rp++) = cpu_to_le32(0);
  502. }
  503. }
  504. spin_unlock(&dev->slock);
  505. } else {
  506. if (status & FLD_VID_SRC_UF)
  507. pr_err("%s(): Video Received Underflow Error Interrupt!\n",
  508. __func__);
  509. if (status & FLD_VID_SRC_SYNC)
  510. pr_err("%s(): Video Received Sync Error Interrupt!\n",
  511. __func__);
  512. if (status & FLD_VID_SRC_OPC_ERR)
  513. pr_err("%s(): Video Received OpCode Error Interrupt!\n",
  514. __func__);
  515. }
  516. if (dev->_file_status == END_OF_FILE) {
  517. pr_err("EOF Channel 1 Framecount = %d\n", dev->_frame_count);
  518. return -1;
  519. }
  520. /* ElSE, set the interrupt mask register, re-enable irq. */
  521. int_msk_tmp = cx_read(channel->int_msk);
  522. cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
  523. return 0;
  524. }
  525. static irqreturn_t cx25821_upstream_irq(int irq, void *dev_id)
  526. {
  527. struct cx25821_dev *dev = dev_id;
  528. u32 vid_status;
  529. int handled = 0;
  530. int channel_num = 0;
  531. struct sram_channel *sram_ch;
  532. if (!dev)
  533. return -1;
  534. channel_num = VID_UPSTREAM_SRAM_CHANNEL_I;
  535. sram_ch = dev->channels[channel_num].sram_channels;
  536. vid_status = cx_read(sram_ch->int_stat);
  537. /* Only deal with our interrupt */
  538. if (vid_status)
  539. handled = cx25821_video_upstream_irq(dev, channel_num,
  540. vid_status);
  541. if (handled < 0)
  542. cx25821_stop_upstream_video_ch1(dev);
  543. else
  544. handled += handled;
  545. return IRQ_RETVAL(handled);
  546. }
  547. static void cx25821_set_pixelengine(struct cx25821_dev *dev,
  548. struct sram_channel *ch,
  549. int pix_format)
  550. {
  551. int width = WIDTH_D1;
  552. int height = dev->_lines_count;
  553. int num_lines, odd_num_lines;
  554. u32 value;
  555. int vip_mode = OUTPUT_FRMT_656;
  556. value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
  557. value &= 0xFFFFFFEF;
  558. value |= dev->_isNTSC ? 0 : 0x10;
  559. cx_write(ch->vid_fmt_ctl, value);
  560. /* set number of active pixels in each line.
  561. * Default is 720 pixels in both NTSC and PAL format */
  562. cx_write(ch->vid_active_ctl1, width);
  563. num_lines = (height / 2) & 0x3FF;
  564. odd_num_lines = num_lines;
  565. if (dev->_isNTSC)
  566. odd_num_lines += 1;
  567. value = (num_lines << 16) | odd_num_lines;
  568. /* set number of active lines in field 0 (top) and field 1 (bottom) */
  569. cx_write(ch->vid_active_ctl2, value);
  570. cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
  571. }
  572. static int cx25821_start_video_dma_upstream(struct cx25821_dev *dev,
  573. struct sram_channel *sram_ch)
  574. {
  575. u32 tmp = 0;
  576. int err = 0;
  577. /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
  578. * channel A-C
  579. */
  580. tmp = cx_read(VID_CH_MODE_SEL);
  581. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  582. /* Set the physical start address of the RISC program in the initial
  583. * program counter(IPC) member of the cmds.
  584. */
  585. cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr);
  586. /* Risc IPC High 64 bits 63-32 */
  587. cx_write(sram_ch->cmds_start + 4, 0);
  588. /* reset counter */
  589. cx_write(sram_ch->gpcnt_ctl, 3);
  590. /* Clear our bits from the interrupt status register. */
  591. cx_write(sram_ch->int_stat, _intr_msk);
  592. /* Set the interrupt mask register, enable irq. */
  593. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
  594. tmp = cx_read(sram_ch->int_msk);
  595. cx_write(sram_ch->int_msk, tmp |= _intr_msk);
  596. err = request_irq(dev->pci->irq, cx25821_upstream_irq,
  597. IRQF_SHARED, dev->name, dev);
  598. if (err < 0) {
  599. pr_err("%s: can't get upstream IRQ %d\n",
  600. dev->name, dev->pci->irq);
  601. goto fail_irq;
  602. }
  603. /* Start the DMA engine */
  604. tmp = cx_read(sram_ch->dma_ctl);
  605. cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
  606. dev->_is_running = 1;
  607. dev->_is_first_frame = 1;
  608. return 0;
  609. fail_irq:
  610. cx25821_dev_unregister(dev);
  611. return err;
  612. }
  613. int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev, int channel_select,
  614. int pixel_format)
  615. {
  616. struct sram_channel *sram_ch;
  617. u32 tmp;
  618. int err = 0;
  619. int data_frame_size = 0;
  620. int risc_buffer_size = 0;
  621. int str_length = 0;
  622. if (dev->_is_running) {
  623. pr_info("Video Channel is still running so return!\n");
  624. return 0;
  625. }
  626. dev->_channel_upstream_select = channel_select;
  627. sram_ch = dev->channels[channel_select].sram_channels;
  628. INIT_WORK(&dev->_irq_work_entry, cx25821_vidups_handler);
  629. dev->_irq_queues = create_singlethread_workqueue("cx25821_workqueue");
  630. if (!dev->_irq_queues) {
  631. pr_err("create_singlethread_workqueue() for Video FAILED!\n");
  632. return -ENOMEM;
  633. }
  634. /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
  635. * channel A-C
  636. */
  637. tmp = cx_read(VID_CH_MODE_SEL);
  638. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  639. dev->_is_running = 0;
  640. dev->_frame_count = 0;
  641. dev->_file_status = RESET_STATUS;
  642. dev->_lines_count = dev->_isNTSC ? 480 : 576;
  643. dev->_pixel_format = pixel_format;
  644. dev->_line_size = (dev->_pixel_format == PIXEL_FRMT_422) ?
  645. (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
  646. data_frame_size = dev->_isNTSC ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
  647. risc_buffer_size = dev->_isNTSC ?
  648. NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
  649. if (dev->input_filename) {
  650. str_length = strlen(dev->input_filename);
  651. dev->_filename = kmemdup(dev->input_filename, str_length + 1,
  652. GFP_KERNEL);
  653. if (!dev->_filename) {
  654. err = -ENOENT;
  655. goto error;
  656. }
  657. } else {
  658. str_length = strlen(dev->_defaultname);
  659. dev->_filename = kmemdup(dev->_defaultname, str_length + 1,
  660. GFP_KERNEL);
  661. if (!dev->_filename) {
  662. err = -ENOENT;
  663. goto error;
  664. }
  665. }
  666. /* Default if filename is empty string */
  667. if (strcmp(dev->_filename, "") == 0) {
  668. if (dev->_isNTSC) {
  669. dev->_filename =
  670. (dev->_pixel_format == PIXEL_FRMT_411) ?
  671. "/root/vid411.yuv" : "/root/vidtest.yuv";
  672. } else {
  673. dev->_filename =
  674. (dev->_pixel_format == PIXEL_FRMT_411) ?
  675. "/root/pal411.yuv" : "/root/pal422.yuv";
  676. }
  677. }
  678. dev->_is_running = 0;
  679. dev->_frame_count = 0;
  680. dev->_file_status = RESET_STATUS;
  681. dev->_lines_count = dev->_isNTSC ? 480 : 576;
  682. dev->_pixel_format = pixel_format;
  683. dev->_line_size = (dev->_pixel_format == PIXEL_FRMT_422) ?
  684. (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
  685. err = cx25821_sram_channel_setup_upstream(dev, sram_ch,
  686. dev->_line_size, 0);
  687. /* setup fifo + format */
  688. cx25821_set_pixelengine(dev, sram_ch, dev->_pixel_format);
  689. dev->upstream_riscbuf_size = risc_buffer_size * 2;
  690. dev->upstream_databuf_size = data_frame_size * 2;
  691. /* Allocating buffers and prepare RISC program */
  692. err = cx25821_upstream_buffer_prepare(dev, sram_ch, dev->_line_size);
  693. if (err < 0) {
  694. pr_err("%s: Failed to set up Video upstream buffers!\n",
  695. dev->name);
  696. goto error;
  697. }
  698. cx25821_start_video_dma_upstream(dev, sram_ch);
  699. return 0;
  700. error:
  701. cx25821_dev_unregister(dev);
  702. return err;
  703. }