2 * Driver for Digigram miXart soundcards
4 * low level interface with interrupt handling and mail box implementation
6 * Copyright (c) 2003 by Digigram <alsa@digigram.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <linux/interrupt.h>
26 #include <sound/core.h>
28 #include "mixart_hwdep.h"
29 #include "mixart_core.h"
32 #define MSG_TIMEOUT_JIFFIES (400 * HZ) / 1000 /* 400 ms */
34 #define MSG_DESCRIPTOR_SIZE 0x24
35 #define MSG_HEADER_SIZE (MSG_DESCRIPTOR_SIZE + 4)
37 #define MSG_DEFAULT_SIZE 512
39 #define MSG_TYPE_MASK 0x00000003 /* mask for following types */
40 #define MSG_TYPE_NOTIFY 0 /* embedded -> driver (only notification, do not get_msg() !) */
41 #define MSG_TYPE_COMMAND 1 /* driver <-> embedded (a command has no answer) */
42 #define MSG_TYPE_REQUEST 2 /* driver -> embedded (request will get an answer back) */
43 #define MSG_TYPE_ANSWER 3 /* embedded -> driver */
44 #define MSG_CANCEL_NOTIFY_MASK 0x80000000 /* this bit is set for a notification that has been canceled */
47 static int retrieve_msg_frame(mixart_mgr_t
*mgr
, u32
*msg_frame
)
49 /* read the message frame fifo */
52 tailptr
= readl_be(MIXART_MEM(mgr
, MSG_OUTBOUND_POST_TAIL
));
53 headptr
= readl_be(MIXART_MEM(mgr
, MSG_OUTBOUND_POST_HEAD
));
55 if (tailptr
== headptr
)
56 return 0; /* no message posted */
58 snd_assert( tailptr
>= MSG_OUTBOUND_POST_STACK
, return 0); /* error */
59 snd_assert( tailptr
< (MSG_OUTBOUND_POST_STACK
+MSG_BOUND_STACK_SIZE
), return 0); /* error */
61 *msg_frame
= readl_be(MIXART_MEM(mgr
, tailptr
));
63 /* increment the tail index */
65 if( tailptr
>= (MSG_OUTBOUND_POST_STACK
+MSG_BOUND_STACK_SIZE
) )
66 tailptr
= MSG_OUTBOUND_POST_STACK
;
67 writel_be(tailptr
, MIXART_MEM(mgr
, MSG_OUTBOUND_POST_TAIL
));
72 static int get_msg(mixart_mgr_t
*mgr
, mixart_msg_t
*resp
, u32 msg_frame_address
)
82 spin_lock_irqsave(&mgr
->msg_lock
, flags
);
85 /* copy message descriptor from miXart to driver */
86 size
= readl_be(MIXART_MEM(mgr
, msg_frame_address
)); /* size of descriptor + response */
87 resp
->message_id
= readl_be(MIXART_MEM(mgr
, msg_frame_address
+ 4)); /* dwMessageID */
88 resp
->uid
.object_id
= readl_be(MIXART_MEM(mgr
, msg_frame_address
+ 8)); /* uidDest */
89 resp
->uid
.desc
= readl_be(MIXART_MEM(mgr
, msg_frame_address
+ 12)); /* */
91 if( (size
< MSG_DESCRIPTOR_SIZE
) || (resp
->size
< (size
- MSG_DESCRIPTOR_SIZE
))) {
93 snd_printk(KERN_ERR
"problem with response size = %d\n", size
);
96 size
-= MSG_DESCRIPTOR_SIZE
;
98 memcpy_fromio(resp
->data
, MIXART_MEM(mgr
, msg_frame_address
+ MSG_HEADER_SIZE
), size
);
101 /* swap if necessary */
103 size
/= 4; /* u32 size */
104 for(i
=0; i
< size
; i
++) {
105 ((u32
*)resp
->data
)[i
] = be32_to_cpu(((u32
*)resp
->data
)[i
]);
110 * free message frame address
112 headptr
= readl_be(MIXART_MEM(mgr
, MSG_OUTBOUND_FREE_HEAD
));
114 if( (headptr
< MSG_OUTBOUND_FREE_STACK
) || ( headptr
>= (MSG_OUTBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
))) {
119 /* give address back to outbound fifo */
120 writel_be(msg_frame_address
, MIXART_MEM(mgr
, headptr
));
122 /* increment the outbound free head */
124 if( headptr
>= (MSG_OUTBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
) )
125 headptr
= MSG_OUTBOUND_FREE_STACK
;
127 writel_be(headptr
, MIXART_MEM(mgr
, MSG_OUTBOUND_FREE_HEAD
));
130 spin_unlock_irqrestore(&mgr
->msg_lock
, flags
);
137 * send a message to miXart. return: the msg_frame used for this message
139 /* call with mgr->msg_lock held! */
140 static int send_msg( mixart_mgr_t
*mgr
,
146 u32 headptr
, tailptr
;
147 u32 msg_frame_address
;
150 snd_assert(msg
->size
% 4 == 0, return -EINVAL
);
154 /* get message frame address */
155 tailptr
= readl_be(MIXART_MEM(mgr
, MSG_INBOUND_FREE_TAIL
));
156 headptr
= readl_be(MIXART_MEM(mgr
, MSG_INBOUND_FREE_HEAD
));
158 if (tailptr
== headptr
) {
159 snd_printk(KERN_ERR
"error: no message frame available\n");
163 if( (tailptr
< MSG_INBOUND_FREE_STACK
) || (tailptr
>= (MSG_INBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
))) {
167 msg_frame_address
= readl_be(MIXART_MEM(mgr
, tailptr
));
168 writel(0, MIXART_MEM(mgr
, tailptr
)); /* set address to zero on this fifo position */
170 /* increment the inbound free tail */
172 if( tailptr
>= (MSG_INBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
) )
173 tailptr
= MSG_INBOUND_FREE_STACK
;
175 writel_be(tailptr
, MIXART_MEM(mgr
, MSG_INBOUND_FREE_TAIL
));
177 /* TODO : use memcpy_toio() with intermediate buffer to copy the message */
179 /* copy message descriptor to card memory */
180 writel_be( msg
->size
+ MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
) ); /* size of descriptor + request */
181 writel_be( msg
->message_id
, MIXART_MEM(mgr
, msg_frame_address
+ 4) ); /* dwMessageID */
182 writel_be( msg
->uid
.object_id
, MIXART_MEM(mgr
, msg_frame_address
+ 8) ); /* uidDest */
183 writel_be( msg
->uid
.desc
, MIXART_MEM(mgr
, msg_frame_address
+ 12) ); /* */
184 writel_be( MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
+ 16) ); /* SizeHeader */
185 writel_be( MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
+ 20) ); /* OffsetDLL_T16 */
186 writel_be( msg
->size
, MIXART_MEM(mgr
, msg_frame_address
+ 24) ); /* SizeDLL_T16 */
187 writel_be( MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
+ 28) ); /* OffsetDLL_DRV */
188 writel_be( 0, MIXART_MEM(mgr
, msg_frame_address
+ 32) ); /* SizeDLL_DRV */
189 writel_be( MSG_DESCRIPTOR_SIZE
+ max_answersize
, MIXART_MEM(mgr
, msg_frame_address
+ 36) ); /* dwExpectedAnswerSize */
191 /* copy message data to card memory */
192 for( i
=0; i
< msg
->size
; i
+=4 ) {
193 writel_be( *(u32
*)(msg
->data
+ i
), MIXART_MEM(mgr
, MSG_HEADER_SIZE
+ msg_frame_address
+ i
) );
198 /* the pending event is the notification we wait for ! */
199 mgr
->pending_event
= *msg_event
;
202 /* the pending event is the answer we wait for (same address than the request)! */
203 mgr
->pending_event
= msg_frame_address
;
205 /* copy address back to caller */
206 *msg_event
= msg_frame_address
;
210 /* mark the frame as a request (will have an answer) */
211 msg_frame_address
|= MSG_TYPE_REQUEST
;
214 headptr
= readl_be(MIXART_MEM(mgr
, MSG_INBOUND_POST_HEAD
));
216 if( (headptr
< MSG_INBOUND_POST_STACK
) || (headptr
>= (MSG_INBOUND_POST_STACK
+MSG_BOUND_STACK_SIZE
))) {
220 writel_be(msg_frame_address
, MIXART_MEM(mgr
, headptr
));
222 /* increment the inbound post head */
224 if( headptr
>= (MSG_INBOUND_POST_STACK
+MSG_BOUND_STACK_SIZE
) )
225 headptr
= MSG_INBOUND_POST_STACK
;
227 writel_be(headptr
, MIXART_MEM(mgr
, MSG_INBOUND_POST_HEAD
));
233 int snd_mixart_send_msg(mixart_mgr_t
*mgr
, mixart_msg_t
*request
, int max_resp_size
, void *resp_data
)
236 u32 msg_frame
= 0; /* set to 0, so it's no notification to wait for, but the answer */
241 down(&mgr
->msg_mutex
);
243 init_waitqueue_entry(&wait
, current
);
245 spin_lock_irq(&mgr
->msg_lock
);
246 /* send the message */
247 err
= send_msg(mgr
, request
, max_resp_size
, 1, &msg_frame
); /* send and mark the answer pending */
249 spin_unlock_irq(&mgr
->msg_lock
);
254 set_current_state(TASK_UNINTERRUPTIBLE
);
255 add_wait_queue(&mgr
->msg_sleep
, &wait
);
256 spin_unlock_irq(&mgr
->msg_lock
);
257 timeout
= schedule_timeout(MSG_TIMEOUT_JIFFIES
);
258 remove_wait_queue(&mgr
->msg_sleep
, &wait
);
263 snd_printk(KERN_ERR
"error: no reponse on msg %x\n", msg_frame
);
267 /* retrieve the answer into the same mixart_msg_t */
269 resp
.uid
= (mixart_uid_t
){0,0};
270 resp
.data
= resp_data
;
271 resp
.size
= max_resp_size
;
273 err
= get_msg(mgr
, &resp
, msg_frame
);
275 if( request
->message_id
!= resp
.message_id
)
276 snd_printk(KERN_ERR
"REPONSE ERROR!\n");
283 int snd_mixart_send_msg_wait_notif(mixart_mgr_t
*mgr
, mixart_msg_t
*request
, u32 notif_event
)
289 snd_assert(notif_event
!= 0, return -EINVAL
);
290 snd_assert((notif_event
& MSG_TYPE_MASK
) == MSG_TYPE_NOTIFY
, return -EINVAL
);
291 snd_assert((notif_event
& MSG_CANCEL_NOTIFY_MASK
) == 0, return -EINVAL
);
293 down(&mgr
->msg_mutex
);
295 init_waitqueue_entry(&wait
, current
);
297 spin_lock_irq(&mgr
->msg_lock
);
298 /* send the message */
299 err
= send_msg(mgr
, request
, MSG_DEFAULT_SIZE
, 1, ¬if_event
); /* send and mark the notification event pending */
301 spin_unlock_irq(&mgr
->msg_lock
);
306 set_current_state(TASK_UNINTERRUPTIBLE
);
307 add_wait_queue(&mgr
->msg_sleep
, &wait
);
308 spin_unlock_irq(&mgr
->msg_lock
);
309 timeout
= schedule_timeout(MSG_TIMEOUT_JIFFIES
);
310 remove_wait_queue(&mgr
->msg_sleep
, &wait
);
315 snd_printk(KERN_ERR
"error: notification %x not received\n", notif_event
);
324 int snd_mixart_send_msg_nonblock(mixart_mgr_t
*mgr
, mixart_msg_t
*request
)
330 /* just send the message (do not mark it as a pending one) */
331 spin_lock_irqsave(&mgr
->msg_lock
, flags
);
332 err
= send_msg(mgr
, request
, MSG_DEFAULT_SIZE
, 0, &message_frame
);
333 spin_unlock_irqrestore(&mgr
->msg_lock
, flags
);
335 /* the answer will be handled by snd_mixart_msg_tasklet() */
336 atomic_inc(&mgr
->msg_processed
);
342 /* common buffer of tasklet and interrupt to send/receive messages */
343 static u32 mixart_msg_data
[MSG_DEFAULT_SIZE
/ 4];
346 void snd_mixart_msg_tasklet( unsigned long arg
)
348 mixart_mgr_t
*mgr
= ( mixart_mgr_t
*)(arg
);
353 spin_lock(&mgr
->lock
);
355 while (mgr
->msg_fifo_readptr
!= mgr
->msg_fifo_writeptr
) {
356 msg
= mgr
->msg_fifo
[mgr
->msg_fifo_readptr
];
357 mgr
->msg_fifo_readptr
++;
358 mgr
->msg_fifo_readptr
%= MSG_FIFO_SIZE
;
360 /* process the message ... */
361 addr
= msg
& ~MSG_TYPE_MASK
;
362 type
= msg
& MSG_TYPE_MASK
;
365 case MSG_TYPE_ANSWER
:
366 /* answer to a message on that we did not wait for (send_msg_nonblock) */
368 resp
.data
= mixart_msg_data
;
369 resp
.size
= sizeof(mixart_msg_data
);
370 err
= get_msg(mgr
, &resp
, addr
);
372 snd_printk(KERN_ERR
"tasklet: error(%d) reading mf %x\n", err
, msg
);
376 switch(resp
.message_id
) {
377 case MSG_STREAM_START_INPUT_STAGE_PACKET
:
378 case MSG_STREAM_START_OUTPUT_STAGE_PACKET
:
379 case MSG_STREAM_STOP_INPUT_STAGE_PACKET
:
380 case MSG_STREAM_STOP_OUTPUT_STAGE_PACKET
:
381 if(mixart_msg_data
[0])
382 snd_printk(KERN_ERR
"tasklet : error MSG_STREAM_ST***_***PUT_STAGE_PACKET status=%x\n", mixart_msg_data
[0]);
385 snd_printdd("tasklet received mf(%x) : msg_id(%x) uid(%x, %x) size(%zd)\n",
386 msg
, resp
.message_id
, resp
.uid
.object_id
, resp
.uid
.desc
, resp
.size
);
390 case MSG_TYPE_NOTIFY
:
391 /* msg contains no address ! do not get_msg() ! */
392 case MSG_TYPE_COMMAND
:
393 /* get_msg() necessary */
395 snd_printk(KERN_ERR
"tasklet doesn't know what to do with message %x\n", msg
);
398 /* decrement counter */
399 atomic_dec(&mgr
->msg_processed
);
401 } /* while there is a msg in fifo */
403 spin_unlock(&mgr
->lock
);
407 irqreturn_t
snd_mixart_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
409 mixart_mgr_t
*mgr
= dev_id
;
416 spin_lock(&mgr
->lock
);
418 it_reg
= readl_le(MIXART_REG(mgr
, MIXART_PCI_OMISR_OFFSET
));
419 if( !(it_reg
& MIXART_OIDI
) ) {
420 /* this device did not cause the interrupt */
421 spin_unlock(&mgr
->lock
);
425 /* mask all interrupts */
426 writel_le(MIXART_HOST_ALL_INTERRUPT_MASKED
, MIXART_REG(mgr
, MIXART_PCI_OMIMR_OFFSET
));
428 /* outdoorbell register clear */
429 it_reg
= readl(MIXART_REG(mgr
, MIXART_PCI_ODBR_OFFSET
));
430 writel(it_reg
, MIXART_REG(mgr
, MIXART_PCI_ODBR_OFFSET
));
432 /* clear interrupt */
433 writel_le( MIXART_OIDI
, MIXART_REG(mgr
, MIXART_PCI_OMISR_OFFSET
) );
435 /* process interrupt */
436 while (retrieve_msg_frame(mgr
, &msg
)) {
438 switch (msg
& MSG_TYPE_MASK
) {
439 case MSG_TYPE_COMMAND
:
441 resp
.data
= mixart_msg_data
;
442 resp
.size
= sizeof(mixart_msg_data
);
443 err
= get_msg(mgr
, &resp
, msg
& ~MSG_TYPE_MASK
);
445 snd_printk(KERN_ERR
"interrupt: error(%d) reading mf %x\n", err
, msg
);
449 if(resp
.message_id
== MSG_SERVICES_TIMER_NOTIFY
) {
451 mixart_timer_notify_t
*notify
= (mixart_timer_notify_t
*)mixart_msg_data
;
453 for(i
=0; i
<notify
->stream_count
; i
++) {
455 u32 buffer_id
= notify
->streams
[i
].buffer_id
;
456 unsigned int chip_number
= (buffer_id
& MIXART_NOTIFY_CARD_MASK
) >> MIXART_NOTIFY_CARD_OFFSET
; /* card0 to 3 */
457 unsigned int pcm_number
= (buffer_id
& MIXART_NOTIFY_PCM_MASK
) >> MIXART_NOTIFY_PCM_OFFSET
; /* pcm0 to 3 */
458 unsigned int sub_number
= buffer_id
& MIXART_NOTIFY_SUBS_MASK
; /* 0 to MIXART_PLAYBACK_STREAMS */
459 unsigned int is_capture
= ((buffer_id
& MIXART_NOTIFY_CAPT_MASK
) != 0); /* playback == 0 / capture == 1 */
461 mixart_t
*chip
= mgr
->chip
[chip_number
];
462 mixart_stream_t
*stream
;
464 if ((chip_number
>= mgr
->num_cards
) || (pcm_number
>= MIXART_PCM_TOTAL
) || (sub_number
>= MIXART_PLAYBACK_STREAMS
)) {
465 snd_printk(KERN_ERR
"error MSG_SERVICES_TIMER_NOTIFY buffer_id (%x) pos(%d)\n",
466 buffer_id
, notify
->streams
[i
].sample_pos_low_part
);
471 stream
= &chip
->capture_stream
[pcm_number
];
473 stream
= &chip
->playback_stream
[pcm_number
][sub_number
];
475 if (stream
->substream
&& (stream
->status
== MIXART_STREAM_STATUS_RUNNING
)) {
476 snd_pcm_runtime_t
*runtime
= stream
->substream
->runtime
;
478 u64 sample_count
= ((u64
)notify
->streams
[i
].sample_pos_high_part
) << 32;
479 sample_count
|= notify
->streams
[i
].sample_pos_low_part
;
482 u64 new_elapse_pos
= stream
->abs_period_elapsed
+ runtime
->period_size
;
484 if (new_elapse_pos
> sample_count
) {
489 stream
->buf_periods
++;
490 if (stream
->buf_periods
>= runtime
->periods
)
491 stream
->buf_periods
= 0;
493 stream
->abs_period_elapsed
= new_elapse_pos
;
496 stream
->buf_period_frag
= (u32
)( sample_count
- stream
->abs_period_elapsed
);
499 spin_unlock(&mgr
->lock
);
500 snd_pcm_period_elapsed(stream
->substream
);
501 spin_lock(&mgr
->lock
);
507 if(resp
.message_id
== MSG_SERVICES_REPORT_TRACES
) {
510 /* Traces are text: the swapped msg_data has to be swapped back ! */
512 for(i
=0; i
<(resp
.size
/4); i
++) {
513 (mixart_msg_data
)[i
] = cpu_to_be32((mixart_msg_data
)[i
]);
516 ((char*)mixart_msg_data
)[resp
.size
- 1] = 0;
517 snd_printdd("MIXART TRACE : %s\n", (char*)mixart_msg_data
);
522 snd_printdd("command %x not handled\n", resp
.message_id
);
525 case MSG_TYPE_NOTIFY
:
526 if(msg
& MSG_CANCEL_NOTIFY_MASK
) {
527 msg
&= ~MSG_CANCEL_NOTIFY_MASK
;
528 snd_printk(KERN_ERR
"canceled notification %x !\n", msg
);
530 /* no break, continue ! */
531 case MSG_TYPE_ANSWER
:
532 /* answer or notification to a message we are waiting for*/
533 spin_lock(&mgr
->msg_lock
);
534 if( (msg
& ~MSG_TYPE_MASK
) == mgr
->pending_event
) {
535 wake_up(&mgr
->msg_sleep
);
536 mgr
->pending_event
= 0;
538 /* answer to a message we did't want to wait for */
540 mgr
->msg_fifo
[mgr
->msg_fifo_writeptr
] = msg
;
541 mgr
->msg_fifo_writeptr
++;
542 mgr
->msg_fifo_writeptr
%= MSG_FIFO_SIZE
;
543 tasklet_hi_schedule(&mgr
->msg_taskq
);
545 spin_unlock(&mgr
->msg_lock
);
547 case MSG_TYPE_REQUEST
:
549 snd_printdd("interrupt received request %x\n", msg
);
550 /* TODO : are there things to do here ? */
552 } /* switch on msg type */
553 } /* while there are msgs */
555 /* allow interrupt again */
556 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL
, MIXART_REG( mgr
, MIXART_PCI_OMIMR_OFFSET
));
558 spin_unlock(&mgr
->lock
);
564 void snd_mixart_init_mailbox(mixart_mgr_t
*mgr
)
566 writel( 0, MIXART_MEM( mgr
, MSG_HOST_RSC_PROTECTION
) );
567 writel( 0, MIXART_MEM( mgr
, MSG_AGENT_RSC_PROTECTION
) );
569 /* allow outbound messagebox to generate interrupts */
571 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL
, MIXART_REG( mgr
, MIXART_PCI_OMIMR_OFFSET
));
576 void snd_mixart_exit_mailbox(mixart_mgr_t
*mgr
)
578 /* no more interrupts on outbound messagebox */
579 writel_le( MIXART_HOST_ALL_INTERRUPT_MASKED
, MIXART_REG( mgr
, MIXART_PCI_OMIMR_OFFSET
));
583 void snd_mixart_reset_board(mixart_mgr_t
*mgr
)
586 writel_be( 1, MIXART_REG(mgr
, MIXART_BA1_BRUTAL_RESET_OFFSET
) );