cobalt-irq.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cobalt interrupt handling
  4. *
  5. * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
  6. * All rights reserved.
  7. */
  8. #include <media/i2c/adv7604.h>
  9. #include "cobalt-driver.h"
  10. #include "cobalt-irq.h"
  11. #include "cobalt-omnitek.h"
  12. static void cobalt_dma_stream_queue_handler(struct cobalt_stream *s)
  13. {
  14. struct cobalt *cobalt = s->cobalt;
  15. int rx = s->video_channel;
  16. struct m00473_freewheel_regmap __iomem *fw =
  17. COBALT_CVI_FREEWHEEL(s->cobalt, rx);
  18. struct m00233_video_measure_regmap __iomem *vmr =
  19. COBALT_CVI_VMR(s->cobalt, rx);
  20. struct m00389_cvi_regmap __iomem *cvi =
  21. COBALT_CVI(s->cobalt, rx);
  22. struct m00479_clk_loss_detector_regmap __iomem *clkloss =
  23. COBALT_CVI_CLK_LOSS(s->cobalt, rx);
  24. struct cobalt_buffer *cb;
  25. bool skip = false;
  26. spin_lock(&s->irqlock);
  27. if (list_empty(&s->bufs)) {
  28. pr_err("no buffers!\n");
  29. spin_unlock(&s->irqlock);
  30. return;
  31. }
  32. /* Give the fresh filled up buffer to the user.
  33. * Note that the interrupt is only sent if the DMA can continue
  34. * with a new buffer, so it is always safe to return this buffer
  35. * to userspace. */
  36. cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
  37. list_del(&cb->list);
  38. spin_unlock(&s->irqlock);
  39. if (s->is_audio || s->is_output)
  40. goto done;
  41. if (s->unstable_frame) {
  42. uint32_t stat = ioread32(&vmr->irq_status);
  43. iowrite32(stat, &vmr->irq_status);
  44. if (!(ioread32(&vmr->status) &
  45. M00233_STATUS_BITMAP_INIT_DONE_MSK)) {
  46. cobalt_dbg(1, "!init_done\n");
  47. if (s->enable_freewheel)
  48. goto restart_fw;
  49. goto done;
  50. }
  51. if (ioread32(&clkloss->status) &
  52. M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) {
  53. iowrite32(0, &clkloss->ctrl);
  54. iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl);
  55. cobalt_dbg(1, "no clock\n");
  56. if (s->enable_freewheel)
  57. goto restart_fw;
  58. goto done;
  59. }
  60. if ((stat & (M00233_IRQ_STATUS_BITMAP_VACTIVE_AREA_MSK |
  61. M00233_IRQ_STATUS_BITMAP_HACTIVE_AREA_MSK)) ||
  62. ioread32(&vmr->vactive_area) != s->timings.bt.height ||
  63. ioread32(&vmr->hactive_area) != s->timings.bt.width) {
  64. cobalt_dbg(1, "unstable\n");
  65. if (s->enable_freewheel)
  66. goto restart_fw;
  67. goto done;
  68. }
  69. if (!s->enable_cvi) {
  70. s->enable_cvi = true;
  71. iowrite32(M00389_CONTROL_BITMAP_ENABLE_MSK, &cvi->control);
  72. goto done;
  73. }
  74. if (!(ioread32(&cvi->status) & M00389_STATUS_BITMAP_LOCK_MSK)) {
  75. cobalt_dbg(1, "cvi no lock\n");
  76. if (s->enable_freewheel)
  77. goto restart_fw;
  78. goto done;
  79. }
  80. if (!s->enable_freewheel) {
  81. cobalt_dbg(1, "stable\n");
  82. s->enable_freewheel = true;
  83. iowrite32(0, &fw->ctrl);
  84. goto done;
  85. }
  86. cobalt_dbg(1, "enabled fw\n");
  87. iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK |
  88. M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK,
  89. &vmr->control);
  90. iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK, &fw->ctrl);
  91. s->enable_freewheel = false;
  92. s->unstable_frame = false;
  93. s->skip_first_frames = 2;
  94. skip = true;
  95. goto done;
  96. }
  97. if (ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) {
  98. restart_fw:
  99. cobalt_dbg(1, "lost lock\n");
  100. iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK,
  101. &vmr->control);
  102. iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK |
  103. M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK,
  104. &fw->ctrl);
  105. iowrite32(0, &cvi->control);
  106. s->unstable_frame = true;
  107. s->enable_freewheel = false;
  108. s->enable_cvi = false;
  109. }
  110. done:
  111. if (s->skip_first_frames) {
  112. skip = true;
  113. s->skip_first_frames--;
  114. }
  115. cb->vb.vb2_buf.timestamp = ktime_get_ns();
  116. /* TODO: the sequence number should be read from the FPGA so we
  117. also know about dropped frames. */
  118. cb->vb.sequence = s->sequence++;
  119. vb2_buffer_done(&cb->vb.vb2_buf,
  120. (skip || s->unstable_frame) ?
  121. VB2_BUF_STATE_REQUEUEING : VB2_BUF_STATE_DONE);
  122. }
  123. irqreturn_t cobalt_irq_handler(int irq, void *dev_id)
  124. {
  125. struct cobalt *cobalt = (struct cobalt *)dev_id;
  126. u32 dma_interrupt =
  127. cobalt_read_bar0(cobalt, DMA_INTERRUPT_STATUS_REG) & 0xffff;
  128. u32 mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
  129. u32 edge = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_EDGE);
  130. int i;
  131. /* Clear DMA interrupt */
  132. cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG, dma_interrupt);
  133. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK, mask & ~edge);
  134. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_EDGE, edge);
  135. for (i = 0; i < COBALT_NUM_STREAMS; i++) {
  136. struct cobalt_stream *s = &cobalt->streams[i];
  137. unsigned dma_fifo_mask = s->dma_fifo_mask;
  138. if (dma_interrupt & (1 << s->dma_channel)) {
  139. cobalt->irq_dma[i]++;
  140. /* Give fresh buffer to user and chain newly
  141. * queued buffers */
  142. cobalt_dma_stream_queue_handler(s);
  143. if (!s->is_audio) {
  144. edge &= ~dma_fifo_mask;
  145. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
  146. mask & ~edge);
  147. }
  148. }
  149. if (s->is_audio)
  150. continue;
  151. if (edge & s->adv_irq_mask)
  152. set_bit(COBALT_STREAM_FL_ADV_IRQ, &s->flags);
  153. if ((edge & mask & dma_fifo_mask) && vb2_is_streaming(&s->q)) {
  154. cobalt_info("full rx FIFO %d\n", i);
  155. cobalt->irq_full_fifo++;
  156. }
  157. }
  158. queue_work(cobalt->irq_work_queues, &cobalt->irq_work_queue);
  159. if (edge & mask & (COBALT_SYSSTAT_VI0_INT1_MSK |
  160. COBALT_SYSSTAT_VI1_INT1_MSK |
  161. COBALT_SYSSTAT_VI2_INT1_MSK |
  162. COBALT_SYSSTAT_VI3_INT1_MSK |
  163. COBALT_SYSSTAT_VIHSMA_INT1_MSK |
  164. COBALT_SYSSTAT_VOHSMA_INT1_MSK))
  165. cobalt->irq_adv1++;
  166. if (edge & mask & (COBALT_SYSSTAT_VI0_INT2_MSK |
  167. COBALT_SYSSTAT_VI1_INT2_MSK |
  168. COBALT_SYSSTAT_VI2_INT2_MSK |
  169. COBALT_SYSSTAT_VI3_INT2_MSK |
  170. COBALT_SYSSTAT_VIHSMA_INT2_MSK))
  171. cobalt->irq_adv2++;
  172. if (edge & mask & COBALT_SYSSTAT_VOHSMA_INT1_MSK)
  173. cobalt->irq_advout++;
  174. if (dma_interrupt)
  175. cobalt->irq_dma_tot++;
  176. if (!(edge & mask) && !dma_interrupt)
  177. cobalt->irq_none++;
  178. dma_interrupt = cobalt_read_bar0(cobalt, DMA_INTERRUPT_STATUS_REG);
  179. return IRQ_HANDLED;
  180. }
  181. void cobalt_irq_work_handler(struct work_struct *work)
  182. {
  183. struct cobalt *cobalt =
  184. container_of(work, struct cobalt, irq_work_queue);
  185. int i;
  186. for (i = 0; i < COBALT_NUM_NODES; i++) {
  187. struct cobalt_stream *s = &cobalt->streams[i];
  188. if (test_and_clear_bit(COBALT_STREAM_FL_ADV_IRQ, &s->flags)) {
  189. u32 mask;
  190. v4l2_subdev_call(cobalt->streams[i].sd, core,
  191. interrupt_service_routine, 0, NULL);
  192. mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
  193. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
  194. mask | s->adv_irq_mask);
  195. }
  196. }
  197. }
  198. void cobalt_irq_log_status(struct cobalt *cobalt)
  199. {
  200. u32 mask;
  201. int i;
  202. cobalt_info("irq: adv1=%u adv2=%u advout=%u none=%u full=%u\n",
  203. cobalt->irq_adv1, cobalt->irq_adv2, cobalt->irq_advout,
  204. cobalt->irq_none, cobalt->irq_full_fifo);
  205. cobalt_info("irq: dma_tot=%u (", cobalt->irq_dma_tot);
  206. for (i = 0; i < COBALT_NUM_STREAMS; i++)
  207. pr_cont("%s%u", i ? "/" : "", cobalt->irq_dma[i]);
  208. pr_cont(")\n");
  209. cobalt->irq_dma_tot = cobalt->irq_adv1 = cobalt->irq_adv2 = 0;
  210. cobalt->irq_advout = cobalt->irq_none = cobalt->irq_full_fifo = 0;
  211. memset(cobalt->irq_dma, 0, sizeof(cobalt->irq_dma));
  212. mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
  213. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
  214. mask |
  215. COBALT_SYSSTAT_VI0_LOST_DATA_MSK |
  216. COBALT_SYSSTAT_VI1_LOST_DATA_MSK |
  217. COBALT_SYSSTAT_VI2_LOST_DATA_MSK |
  218. COBALT_SYSSTAT_VI3_LOST_DATA_MSK |
  219. COBALT_SYSSTAT_VIHSMA_LOST_DATA_MSK |
  220. COBALT_SYSSTAT_VOHSMA_LOST_DATA_MSK |
  221. COBALT_SYSSTAT_AUD_IN_LOST_DATA_MSK |
  222. COBALT_SYSSTAT_AUD_OUT_LOST_DATA_MSK);
  223. }