dsp_mailbox.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include <linux/version.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/io.h>
  6. #include <linux/mm.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/mutex.h>
  9. #include <linux/device.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/timer.h>
  12. #include <asm/cacheflush.h>
  13. //#include <asm/arch/am_regs.h>
  14. #include <mach/am_regs.h>
  15. #include <linux/amports/tsync.h>
  16. #include <linux/amports/timestamp.h>
  17. #include "dsp_mailbox.h"
  18. #include "dsp_codec.h"
  19. static void audiodsp_mailbox_work_queue(struct work_struct*);
  20. static struct audiodsp_work_t{
  21. char* buf;
  22. struct work_struct audiodsp_workqueue;
  23. }audiodsp_work;
  24. int dsp_mailbox_send(struct audiodsp_priv *priv,int overwrite,int num,int cmd,const char *data,int len)
  25. {
  26. unsigned long flags;
  27. int res=-1;
  28. struct mail_msg *m;
  29. dma_addr_t buf_map;
  30. m=&priv->mailbox_reg2[num];
  31. local_irq_save(flags);
  32. if(overwrite || m->status==0)
  33. {
  34. m->cmd=cmd;
  35. m->data=(char *)ARM_2_ARC_ADDR_SWAP((unsigned)data);
  36. m->len=len;
  37. m->status=1;
  38. after_change_mailbox(m);
  39. if(data!=NULL && len >0)
  40. {
  41. buf_map = dma_map_single(NULL, (void *)data, len, DMA_FROM_DEVICE);
  42. dma_unmap_single(NULL, buf_map, len, DMA_FROM_DEVICE);
  43. }
  44. MAIBOX2_IRQ_ENABLE(num);
  45. DSP_TRIGGER_IRQ(num);
  46. res=0;
  47. }
  48. local_irq_restore(flags);
  49. return res;
  50. }
  51. int get_mailbox_data(struct audiodsp_priv *priv,int num,struct mail_msg *msg)
  52. {
  53. unsigned long flags;
  54. struct mail_msg *m;
  55. if(num>31 || num <0)
  56. return -1;
  57. local_irq_save(flags);
  58. m=&priv->mailbox_reg[num];
  59. pre_read_mailbox(m);
  60. //dsp_addr_map = dma_map_single(priv->dev,(void*)m,sizeof(*m),DMA_FROM_DEVICE);
  61. //dma_unmap_single(priv->dev,dsp_addr_map,sizeof(*m),DMA_FROM_DEVICE);
  62. msg->cmd=m->cmd;
  63. msg->data=m->data;
  64. msg->data = (char *)((unsigned)msg->data+AUDIO_DSP_START_ADDR);
  65. msg->status=m->status;
  66. msg->len=m->len;
  67. m->status=0;
  68. after_change_mailbox(m);
  69. local_irq_restore(flags);
  70. return 0;
  71. }
  72. static irqreturn_t audiodsp_mailbox_irq(int irq, void *data)
  73. {
  74. struct audiodsp_priv *priv=(struct audiodsp_priv *)data;
  75. unsigned long status,fiq_mask;
  76. struct mail_msg msg;
  77. int i = 0;
  78. status=READ_VREG(MB1_REG);
  79. fiq_mask=READ_VREG(MB1_SEL);
  80. status=status&fiq_mask;
  81. if(status&(1<<M1B_IRQ0_PRINT))
  82. {
  83. get_mailbox_data(priv,M1B_IRQ0_PRINT,&msg);
  84. SYS_CLEAR_IRQ(M1B_IRQ0_PRINT);
  85. // inv_dcache_range((unsigned long )msg.data,(unsigned long)msg.data+msg.len);
  86. DSP_PRNT("%s", msg.data);
  87. //audiodsp_work.buf = msg.data;
  88. //schedule_work(&audiodsp_work.audiodsp_workqueue);
  89. }
  90. if(status&(1<<M1B_IRQ1_BUF_OVERFLOW))
  91. {
  92. SYS_CLEAR_IRQ(M1B_IRQ1_BUF_OVERFLOW);
  93. DSP_PRNT("DSP BUF over flow\n");
  94. }
  95. if(status&(1<<M1B_IRQ2_BUF_UNDERFLOW))
  96. {
  97. SYS_CLEAR_IRQ(M1B_IRQ2_BUF_UNDERFLOW);
  98. DSP_PRNT("DSP BUF over flow\n");
  99. }
  100. if(status&(1<<M1B_IRQ3_DECODE_ERROR))
  101. {
  102. SYS_CLEAR_IRQ(M1B_IRQ3_DECODE_ERROR);
  103. priv->decode_error_count++;
  104. }
  105. if(status&(1<<M1B_IRQ4_DECODE_FINISH_FRAME))
  106. {
  107. struct frame_info *info;
  108. SYS_CLEAR_IRQ(M1B_IRQ4_DECODE_FINISH_FRAME);
  109. get_mailbox_data(priv,M1B_IRQ4_DECODE_FINISH_FRAME,&msg);
  110. info=(struct frame_info *)msg.data;
  111. if(info!=NULL)
  112. {
  113. priv->cur_frame_info.offset=info->offset;
  114. priv->cur_frame_info.buffered_len=info->buffered_len;
  115. }
  116. priv->decoded_nb_frames ++;
  117. complete(&priv->decode_completion);
  118. }
  119. if(status& (1<<M1B_IRQ5_STREAM_FMT_CHANGED))
  120. {
  121. struct frame_fmt *fmt;
  122. SYS_CLEAR_IRQ(M1B_IRQ5_STREAM_FMT_CHANGED);
  123. get_mailbox_data(priv,M1B_IRQ5_STREAM_FMT_CHANGED,&msg);
  124. fmt=(void *)msg.data;
  125. //DSP_PRNT("frame format changed");
  126. if(fmt==NULL || (sizeof(struct frame_fmt )<msg.len))
  127. {
  128. DSP_PRNT("frame format message error\n");
  129. }
  130. else
  131. {
  132. DSP_PRNT("frame format changed,fmt->valid 0x%x\n",fmt->valid);
  133. if(fmt->valid&SUB_FMT_VALID)
  134. {
  135. priv->frame_format.sub_fmt=fmt->sub_fmt;
  136. priv->frame_format.valid|=SUB_FMT_VALID;
  137. }
  138. if(fmt->valid&CHANNEL_VALID)
  139. {
  140. priv->frame_format.channel_num=((fmt->channel_num > 2) ? 2 : (fmt->channel_num));
  141. priv->frame_format.valid|=CHANNEL_VALID;
  142. }
  143. if(fmt->valid&SAMPLE_RATE_VALID)
  144. {
  145. priv->frame_format.sample_rate=fmt->sample_rate;
  146. priv->frame_format.valid|=SAMPLE_RATE_VALID;
  147. }
  148. if(fmt->valid&DATA_WIDTH_VALID)
  149. {
  150. priv->frame_format.data_width=fmt->data_width;
  151. priv->frame_format.valid|=DATA_WIDTH_VALID;
  152. }
  153. }
  154. }
  155. if(status& (1<<M1B_IRQ5_STREAM_RD_WD_TEST)){
  156. DSP_WD((0x84100000-4096+20*20),0);
  157. SYS_CLEAR_IRQ(M1B_IRQ5_STREAM_RD_WD_TEST);
  158. get_mailbox_data(priv,M1B_IRQ5_STREAM_RD_WD_TEST,&msg);
  159. for(i = 0;i<12;i++){
  160. if((DSP_RD((0x84100000-512*1024+i*20)))!= (0xff00|i)){
  161. DSP_PRNT("a9 read dsp reg error ,now 0x%lx, should be 0x%x \n",(DSP_RD((0x84100000-512*1024+i*20))),12-i);
  162. }
  163. // DSP_PRNT("A9 audio dsp reg%d value 0x%x\n",i,DSP_RD((0x84100000-4096+i*20)));
  164. }
  165. for(i = 0;i<12;i++){
  166. DSP_WD((0x84100000-512*1024+i*20),(i%2)?i:(0xf0|i));
  167. }
  168. DSP_WD((0x84100000-512*1024+20*20),DSP_STATUS_HALT);
  169. // DSP_PRNT("A9 mail box handle finished\n");
  170. // dsp_mailbox_send(priv, 1, M1B_IRQ5_STREAM_RD_WD_TEST, 0, NULL,0);
  171. }
  172. if(status & (1<<M1B_IRQ7_DECODE_FATAL_ERR)){
  173. int err_code;
  174. SYS_CLEAR_IRQ(M1B_IRQ7_DECODE_FATAL_ERR);
  175. get_mailbox_data(priv,M1B_IRQ7_DECODE_FATAL_ERR,&msg);
  176. err_code = msg.cmd;
  177. priv->decode_fatal_err = err_code;
  178. if(err_code & 0x01){
  179. timestamp_pcrscr_set(timestamp_vpts_get());
  180. timestamp_pcrscr_enable(1);
  181. }
  182. else if(err_code & 0x02){
  183. printk("Set decode_fatal_err flag, Reset audiodsp!\n");
  184. }
  185. }
  186. return 0;
  187. }
  188. static void audiodsp_mailbox_work_queue(struct work_struct*work)
  189. {
  190. struct audiodsp_work_t* pwork = container_of(work,struct audiodsp_work_t, audiodsp_workqueue);
  191. char* message;
  192. if(pwork){
  193. message = pwork->buf;
  194. if(message)
  195. printk(KERN_INFO "%s",message);
  196. else
  197. printk(KERN_INFO "the message from mailbox is NULL\n");
  198. }else{
  199. printk(KERN_INFO "the work queue for audiodsp mailbox is empty\n");
  200. }
  201. }
  202. int audiodsp_init_mailbox(struct audiodsp_priv *priv)
  203. {
  204. int err;
  205. err = request_irq(INT_MAILBOX_1B, audiodsp_mailbox_irq,
  206. IRQF_SHARED, "audiodsp_mailbox", (void *)priv);
  207. if(err != 0){
  208. printk("request IRQ for dsp mailbox failed: errno = %x\n", err);
  209. return -1;
  210. }
  211. //WRITE_MPEG_REG(ASSIST_MBOX0_MASK, 0xffffffff);
  212. priv->mailbox_reg=(struct mail_msg *)MAILBOX1_REG(0);
  213. priv->mailbox_reg2=(struct mail_msg *)MAILBOX2_REG(0);
  214. INIT_WORK(&audiodsp_work.audiodsp_workqueue, audiodsp_mailbox_work_queue);
  215. return 0;
  216. }
  217. int audiodsp_release_mailbox(struct audiodsp_priv *priv)
  218. {
  219. free_irq(INT_MAILBOX_1B,(void *)priv);
  220. return 0;
  221. }
  222. int mailbox_send_audiodsp(int overwrite,int num,int cmd,const char *data,int len)
  223. {
  224. int res = -1;
  225. res = dsp_mailbox_send(audiodsp_privdata(),overwrite,num,cmd,data,len);
  226. return res;
  227. }
  228. EXPORT_SYMBOL(mailbox_send_audiodsp);