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 <linux/interrupt.h>
24 #include <linux/mutex.h>
27 #include <sound/core.h>
29 #include "mixart_hwdep.h"
30 #include "mixart_core.h"
33 #define MSG_TIMEOUT_JIFFIES (400 * HZ) / 1000 /* 400 ms */
35 #define MSG_DESCRIPTOR_SIZE 0x24
36 #define MSG_HEADER_SIZE (MSG_DESCRIPTOR_SIZE + 4)
38 #define MSG_DEFAULT_SIZE 512
40 #define MSG_TYPE_MASK 0x00000003 /* mask for following types */
41 #define MSG_TYPE_NOTIFY 0 /* embedded -> driver (only notification, do not get_msg() !) */
42 #define MSG_TYPE_COMMAND 1 /* driver <-> embedded (a command has no answer) */
43 #define MSG_TYPE_REQUEST 2 /* driver -> embedded (request will get an answer back) */
44 #define MSG_TYPE_ANSWER 3 /* embedded -> driver */
45 #define MSG_CANCEL_NOTIFY_MASK 0x80000000 /* this bit is set for a notification that has been canceled */
48 static int retrieve_msg_frame(struct mixart_mgr *mgr, u32 *msg_frame)
50 /* read the message frame fifo */
53 tailptr = readl_be(MIXART_MEM(mgr, MSG_OUTBOUND_POST_TAIL));
54 headptr = readl_be(MIXART_MEM(mgr, MSG_OUTBOUND_POST_HEAD));
56 if (tailptr == headptr)
57 return 0; /* no message posted */
59 snd_assert( tailptr >= MSG_OUTBOUND_POST_STACK, return 0); /* error */
60 snd_assert( tailptr < (MSG_OUTBOUND_POST_STACK+MSG_BOUND_STACK_SIZE), return 0); /* error */
62 *msg_frame = readl_be(MIXART_MEM(mgr, tailptr));
64 /* increment the tail index */
66 if( tailptr >= (MSG_OUTBOUND_POST_STACK+MSG_BOUND_STACK_SIZE) )
67 tailptr = MSG_OUTBOUND_POST_STACK;
68 writel_be(tailptr, MIXART_MEM(mgr, MSG_OUTBOUND_POST_TAIL));
73 static int get_msg(struct mixart_mgr *mgr, struct mixart_msg *resp,
74 u32 msg_frame_address )
84 spin_lock_irqsave(&mgr->msg_lock, flags);
87 /* copy message descriptor from miXart to driver */
88 size = readl_be(MIXART_MEM(mgr, msg_frame_address)); /* size of descriptor + response */
89 resp->message_id = readl_be(MIXART_MEM(mgr, msg_frame_address + 4)); /* dwMessageID */
90 resp->uid.object_id = readl_be(MIXART_MEM(mgr, msg_frame_address + 8)); /* uidDest */
91 resp->uid.desc = readl_be(MIXART_MEM(mgr, msg_frame_address + 12)); /* */
93 if( (size < MSG_DESCRIPTOR_SIZE) || (resp->size < (size - MSG_DESCRIPTOR_SIZE))) {
95 snd_printk(KERN_ERR "problem with response size = %d\n", size);
98 size -= MSG_DESCRIPTOR_SIZE;
100 memcpy_fromio(resp->data, MIXART_MEM(mgr, msg_frame_address + MSG_HEADER_SIZE ), size);
103 /* swap if necessary */
105 size /= 4; /* u32 size */
106 for(i=0; i < size; i++) {
107 ((u32*)resp->data)[i] = be32_to_cpu(((u32*)resp->data)[i]);
112 * free message frame address
114 headptr = readl_be(MIXART_MEM(mgr, MSG_OUTBOUND_FREE_HEAD));
116 if( (headptr < MSG_OUTBOUND_FREE_STACK) || ( headptr >= (MSG_OUTBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE))) {
121 /* give address back to outbound fifo */
122 writel_be(msg_frame_address, MIXART_MEM(mgr, headptr));
124 /* increment the outbound free head */
126 if( headptr >= (MSG_OUTBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE) )
127 headptr = MSG_OUTBOUND_FREE_STACK;
129 writel_be(headptr, MIXART_MEM(mgr, MSG_OUTBOUND_FREE_HEAD));
132 spin_unlock_irqrestore(&mgr->msg_lock, flags);
139 * send a message to miXart. return: the msg_frame used for this message
141 /* call with mgr->msg_lock held! */
142 static int send_msg( struct mixart_mgr *mgr,
143 struct mixart_msg *msg,
148 u32 headptr, tailptr;
149 u32 msg_frame_address;
152 snd_assert(msg->size % 4 == 0, return -EINVAL);
156 /* get message frame address */
157 tailptr = readl_be(MIXART_MEM(mgr, MSG_INBOUND_FREE_TAIL));
158 headptr = readl_be(MIXART_MEM(mgr, MSG_INBOUND_FREE_HEAD));
160 if (tailptr == headptr) {
161 snd_printk(KERN_ERR "error: no message frame available\n");
165 if( (tailptr < MSG_INBOUND_FREE_STACK) || (tailptr >= (MSG_INBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE))) {
169 msg_frame_address = readl_be(MIXART_MEM(mgr, tailptr));
170 writel(0, MIXART_MEM(mgr, tailptr)); /* set address to zero on this fifo position */
172 /* increment the inbound free tail */
174 if( tailptr >= (MSG_INBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE) )
175 tailptr = MSG_INBOUND_FREE_STACK;
177 writel_be(tailptr, MIXART_MEM(mgr, MSG_INBOUND_FREE_TAIL));
179 /* TODO : use memcpy_toio() with intermediate buffer to copy the message */
181 /* copy message descriptor to card memory */
182 writel_be( msg->size + MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address) ); /* size of descriptor + request */
183 writel_be( msg->message_id , MIXART_MEM(mgr, msg_frame_address + 4) ); /* dwMessageID */
184 writel_be( msg->uid.object_id, MIXART_MEM(mgr, msg_frame_address + 8) ); /* uidDest */
185 writel_be( msg->uid.desc, MIXART_MEM(mgr, msg_frame_address + 12) ); /* */
186 writel_be( MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address + 16) ); /* SizeHeader */
187 writel_be( MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address + 20) ); /* OffsetDLL_T16 */
188 writel_be( msg->size, MIXART_MEM(mgr, msg_frame_address + 24) ); /* SizeDLL_T16 */
189 writel_be( MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address + 28) ); /* OffsetDLL_DRV */
190 writel_be( 0, MIXART_MEM(mgr, msg_frame_address + 32) ); /* SizeDLL_DRV */
191 writel_be( MSG_DESCRIPTOR_SIZE + max_answersize, MIXART_MEM(mgr, msg_frame_address + 36) ); /* dwExpectedAnswerSize */
193 /* copy message data to card memory */
194 for( i=0; i < msg->size; i+=4 ) {
195 writel_be( *(u32*)(msg->data + i), MIXART_MEM(mgr, MSG_HEADER_SIZE + msg_frame_address + i) );
200 /* the pending event is the notification we wait for ! */
201 mgr->pending_event = *msg_event;
204 /* the pending event is the answer we wait for (same address than the request)! */
205 mgr->pending_event = msg_frame_address;
207 /* copy address back to caller */
208 *msg_event = msg_frame_address;
212 /* mark the frame as a request (will have an answer) */
213 msg_frame_address |= MSG_TYPE_REQUEST;
216 headptr = readl_be(MIXART_MEM(mgr, MSG_INBOUND_POST_HEAD));
218 if( (headptr < MSG_INBOUND_POST_STACK) || (headptr >= (MSG_INBOUND_POST_STACK+MSG_BOUND_STACK_SIZE))) {
222 writel_be(msg_frame_address, MIXART_MEM(mgr, headptr));
224 /* increment the inbound post head */
226 if( headptr >= (MSG_INBOUND_POST_STACK+MSG_BOUND_STACK_SIZE) )
227 headptr = MSG_INBOUND_POST_STACK;
229 writel_be(headptr, MIXART_MEM(mgr, MSG_INBOUND_POST_HEAD));
235 int snd_mixart_send_msg(struct mixart_mgr *mgr, struct mixart_msg *request, int max_resp_size, void *resp_data)
237 struct mixart_msg resp;
238 u32 msg_frame = 0; /* set to 0, so it's no notification to wait for, but the answer */
243 mutex_lock(&mgr->msg_mutex);
245 init_waitqueue_entry(&wait, current);
247 spin_lock_irq(&mgr->msg_lock);
248 /* send the message */
249 err = send_msg(mgr, request, max_resp_size, 1, &msg_frame); /* send and mark the answer pending */
251 spin_unlock_irq(&mgr->msg_lock);
252 mutex_unlock(&mgr->msg_mutex);
256 set_current_state(TASK_UNINTERRUPTIBLE);
257 add_wait_queue(&mgr->msg_sleep, &wait);
258 spin_unlock_irq(&mgr->msg_lock);
259 timeout = schedule_timeout(MSG_TIMEOUT_JIFFIES);
260 remove_wait_queue(&mgr->msg_sleep, &wait);
264 mutex_unlock(&mgr->msg_mutex);
265 snd_printk(KERN_ERR "error: no reponse on msg %x\n", msg_frame);
269 /* retrieve the answer into the same struct mixart_msg */
271 resp.uid = (struct mixart_uid){0,0};
272 resp.data = resp_data;
273 resp.size = max_resp_size;
275 err = get_msg(mgr, &resp, msg_frame);
277 if( request->message_id != resp.message_id )
278 snd_printk(KERN_ERR "REPONSE ERROR!\n");
280 mutex_unlock(&mgr->msg_mutex);
285 int snd_mixart_send_msg_wait_notif(struct mixart_mgr *mgr,
286 struct mixart_msg *request, u32 notif_event)
292 snd_assert(notif_event != 0, return -EINVAL);
293 snd_assert((notif_event & MSG_TYPE_MASK) == MSG_TYPE_NOTIFY, return -EINVAL);
294 snd_assert((notif_event & MSG_CANCEL_NOTIFY_MASK) == 0, return -EINVAL);
296 mutex_lock(&mgr->msg_mutex);
298 init_waitqueue_entry(&wait, current);
300 spin_lock_irq(&mgr->msg_lock);
301 /* send the message */
302 err = send_msg(mgr, request, MSG_DEFAULT_SIZE, 1, ¬if_event); /* send and mark the notification event pending */
304 spin_unlock_irq(&mgr->msg_lock);
305 mutex_unlock(&mgr->msg_mutex);
309 set_current_state(TASK_UNINTERRUPTIBLE);
310 add_wait_queue(&mgr->msg_sleep, &wait);
311 spin_unlock_irq(&mgr->msg_lock);
312 timeout = schedule_timeout(MSG_TIMEOUT_JIFFIES);
313 remove_wait_queue(&mgr->msg_sleep, &wait);
317 mutex_unlock(&mgr->msg_mutex);
318 snd_printk(KERN_ERR "error: notification %x not received\n", notif_event);
322 mutex_unlock(&mgr->msg_mutex);
327 int snd_mixart_send_msg_nonblock(struct mixart_mgr *mgr, struct mixart_msg *request)
333 /* just send the message (do not mark it as a pending one) */
334 spin_lock_irqsave(&mgr->msg_lock, flags);
335 err = send_msg(mgr, request, MSG_DEFAULT_SIZE, 0, &message_frame);
336 spin_unlock_irqrestore(&mgr->msg_lock, flags);
338 /* the answer will be handled by snd_struct mixart_msgasklet() */
339 atomic_inc(&mgr->msg_processed);
345 /* common buffer of tasklet and interrupt to send/receive messages */
346 static u32 mixart_msg_data[MSG_DEFAULT_SIZE / 4];
349 void snd_mixart_msg_tasklet(unsigned long arg)
351 struct mixart_mgr *mgr = ( struct mixart_mgr*)(arg);
352 struct mixart_msg resp;
356 spin_lock(&mgr->lock);
358 while (mgr->msg_fifo_readptr != mgr->msg_fifo_writeptr) {
359 msg = mgr->msg_fifo[mgr->msg_fifo_readptr];
360 mgr->msg_fifo_readptr++;
361 mgr->msg_fifo_readptr %= MSG_FIFO_SIZE;
363 /* process the message ... */
364 addr = msg & ~MSG_TYPE_MASK;
365 type = msg & MSG_TYPE_MASK;
368 case MSG_TYPE_ANSWER:
369 /* answer to a message on that we did not wait for (send_msg_nonblock) */
371 resp.data = mixart_msg_data;
372 resp.size = sizeof(mixart_msg_data);
373 err = get_msg(mgr, &resp, addr);
375 snd_printk(KERN_ERR "tasklet: error(%d) reading mf %x\n", err, msg);
379 switch(resp.message_id) {
380 case MSG_STREAM_START_INPUT_STAGE_PACKET:
381 case MSG_STREAM_START_OUTPUT_STAGE_PACKET:
382 case MSG_STREAM_STOP_INPUT_STAGE_PACKET:
383 case MSG_STREAM_STOP_OUTPUT_STAGE_PACKET:
384 if(mixart_msg_data[0])
385 snd_printk(KERN_ERR "tasklet : error MSG_STREAM_ST***_***PUT_STAGE_PACKET status=%x\n", mixart_msg_data[0]);
388 snd_printdd("tasklet received mf(%x) : msg_id(%x) uid(%x, %x) size(%zd)\n",
389 msg, resp.message_id, resp.uid.object_id, resp.uid.desc, resp.size);
393 case MSG_TYPE_NOTIFY:
394 /* msg contains no address ! do not get_msg() ! */
395 case MSG_TYPE_COMMAND:
396 /* get_msg() necessary */
398 snd_printk(KERN_ERR "tasklet doesn't know what to do with message %x\n", msg);
401 /* decrement counter */
402 atomic_dec(&mgr->msg_processed);
404 } /* while there is a msg in fifo */
406 spin_unlock(&mgr->lock);
410 irqreturn_t snd_mixart_interrupt(int irq, void *dev_id)
412 struct mixart_mgr *mgr = dev_id;
414 struct mixart_msg resp;
419 spin_lock(&mgr->lock);
421 it_reg = readl_le(MIXART_REG(mgr, MIXART_PCI_OMISR_OFFSET));
422 if( !(it_reg & MIXART_OIDI) ) {
423 /* this device did not cause the interrupt */
424 spin_unlock(&mgr->lock);
428 /* mask all interrupts */
429 writel_le(MIXART_HOST_ALL_INTERRUPT_MASKED, MIXART_REG(mgr, MIXART_PCI_OMIMR_OFFSET));
431 /* outdoorbell register clear */
432 it_reg = readl(MIXART_REG(mgr, MIXART_PCI_ODBR_OFFSET));
433 writel(it_reg, MIXART_REG(mgr, MIXART_PCI_ODBR_OFFSET));
435 /* clear interrupt */
436 writel_le( MIXART_OIDI, MIXART_REG(mgr, MIXART_PCI_OMISR_OFFSET) );
438 /* process interrupt */
439 while (retrieve_msg_frame(mgr, &msg)) {
441 switch (msg & MSG_TYPE_MASK) {
442 case MSG_TYPE_COMMAND:
444 resp.data = mixart_msg_data;
445 resp.size = sizeof(mixart_msg_data);
446 err = get_msg(mgr, &resp, msg & ~MSG_TYPE_MASK);
448 snd_printk(KERN_ERR "interrupt: error(%d) reading mf %x\n", err, msg);
452 if(resp.message_id == MSG_SERVICES_TIMER_NOTIFY) {
454 struct mixart_timer_notify *notify;
455 notify = (struct mixart_timer_notify *)mixart_msg_data;
457 for(i=0; i<notify->stream_count; i++) {
459 u32 buffer_id = notify->streams[i].buffer_id;
460 unsigned int chip_number = (buffer_id & MIXART_NOTIFY_CARD_MASK) >> MIXART_NOTIFY_CARD_OFFSET; /* card0 to 3 */
461 unsigned int pcm_number = (buffer_id & MIXART_NOTIFY_PCM_MASK ) >> MIXART_NOTIFY_PCM_OFFSET; /* pcm0 to 3 */
462 unsigned int sub_number = buffer_id & MIXART_NOTIFY_SUBS_MASK; /* 0 to MIXART_PLAYBACK_STREAMS */
463 unsigned int is_capture = ((buffer_id & MIXART_NOTIFY_CAPT_MASK) != 0); /* playback == 0 / capture == 1 */
465 struct snd_mixart *chip = mgr->chip[chip_number];
466 struct mixart_stream *stream;
468 if ((chip_number >= mgr->num_cards) || (pcm_number >= MIXART_PCM_TOTAL) || (sub_number >= MIXART_PLAYBACK_STREAMS)) {
469 snd_printk(KERN_ERR "error MSG_SERVICES_TIMER_NOTIFY buffer_id (%x) pos(%d)\n",
470 buffer_id, notify->streams[i].sample_pos_low_part);
475 stream = &chip->capture_stream[pcm_number];
477 stream = &chip->playback_stream[pcm_number][sub_number];
479 if (stream->substream && (stream->status == MIXART_STREAM_STATUS_RUNNING)) {
480 struct snd_pcm_runtime *runtime = stream->substream->runtime;
482 u64 sample_count = ((u64)notify->streams[i].sample_pos_high_part) << 32;
483 sample_count |= notify->streams[i].sample_pos_low_part;
486 u64 new_elapse_pos = stream->abs_period_elapsed + runtime->period_size;
488 if (new_elapse_pos > sample_count) {
493 stream->buf_periods++;
494 if (stream->buf_periods >= runtime->periods)
495 stream->buf_periods = 0;
497 stream->abs_period_elapsed = new_elapse_pos;
500 stream->buf_period_frag = (u32)( sample_count - stream->abs_period_elapsed );
503 spin_unlock(&mgr->lock);
504 snd_pcm_period_elapsed(stream->substream);
505 spin_lock(&mgr->lock);
511 if(resp.message_id == MSG_SERVICES_REPORT_TRACES) {
514 /* Traces are text: the swapped msg_data has to be swapped back ! */
516 for(i=0; i<(resp.size/4); i++) {
517 (mixart_msg_data)[i] = cpu_to_be32((mixart_msg_data)[i]);
520 ((char*)mixart_msg_data)[resp.size - 1] = 0;
521 snd_printdd("MIXART TRACE : %s\n", (char*)mixart_msg_data);
526 snd_printdd("command %x not handled\n", resp.message_id);
529 case MSG_TYPE_NOTIFY:
530 if(msg & MSG_CANCEL_NOTIFY_MASK) {
531 msg &= ~MSG_CANCEL_NOTIFY_MASK;
532 snd_printk(KERN_ERR "canceled notification %x !\n", msg);
534 /* no break, continue ! */
535 case MSG_TYPE_ANSWER:
536 /* answer or notification to a message we are waiting for*/
537 spin_lock(&mgr->msg_lock);
538 if( (msg & ~MSG_TYPE_MASK) == mgr->pending_event ) {
539 wake_up(&mgr->msg_sleep);
540 mgr->pending_event = 0;
542 /* answer to a message we did't want to wait for */
544 mgr->msg_fifo[mgr->msg_fifo_writeptr] = msg;
545 mgr->msg_fifo_writeptr++;
546 mgr->msg_fifo_writeptr %= MSG_FIFO_SIZE;
547 tasklet_hi_schedule(&mgr->msg_taskq);
549 spin_unlock(&mgr->msg_lock);
551 case MSG_TYPE_REQUEST:
553 snd_printdd("interrupt received request %x\n", msg);
554 /* TODO : are there things to do here ? */
556 } /* switch on msg type */
557 } /* while there are msgs */
559 /* allow interrupt again */
560 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL, MIXART_REG( mgr, MIXART_PCI_OMIMR_OFFSET));
562 spin_unlock(&mgr->lock);
568 void snd_mixart_init_mailbox(struct mixart_mgr *mgr)
570 writel( 0, MIXART_MEM( mgr, MSG_HOST_RSC_PROTECTION ) );
571 writel( 0, MIXART_MEM( mgr, MSG_AGENT_RSC_PROTECTION ) );
573 /* allow outbound messagebox to generate interrupts */
575 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL, MIXART_REG( mgr, MIXART_PCI_OMIMR_OFFSET));
580 void snd_mixart_exit_mailbox(struct mixart_mgr *mgr)
582 /* no more interrupts on outbound messagebox */
583 writel_le( MIXART_HOST_ALL_INTERRUPT_MASKED, MIXART_REG( mgr, MIXART_PCI_OMIMR_OFFSET));
587 void snd_mixart_reset_board(struct mixart_mgr *mgr)
590 writel_be( 1, MIXART_REG(mgr, MIXART_BA1_BRUTAL_RESET_OFFSET) );