1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Digigram miXart soundcards
5 * low level interface with interrupt handling and mail box implementation
7 * Copyright (c) 2003 by Digigram <alsa@digigram.com>
10 #include <linux/interrupt.h>
11 #include <linux/mutex.h>
12 #include <linux/pci.h>
15 #include <sound/core.h>
17 #include "mixart_hwdep.h"
18 #include "mixart_core.h"
21 #define MSG_TIMEOUT_JIFFIES (400 * HZ) / 1000 /* 400 ms */
23 #define MSG_DESCRIPTOR_SIZE 0x24
24 #define MSG_HEADER_SIZE (MSG_DESCRIPTOR_SIZE + 4)
26 #define MSG_TYPE_MASK 0x00000003 /* mask for following types */
27 #define MSG_TYPE_NOTIFY 0 /* embedded -> driver (only notification, do not get_msg() !) */
28 #define MSG_TYPE_COMMAND 1 /* driver <-> embedded (a command has no answer) */
29 #define MSG_TYPE_REQUEST 2 /* driver -> embedded (request will get an answer back) */
30 #define MSG_TYPE_ANSWER 3 /* embedded -> driver */
31 #define MSG_CANCEL_NOTIFY_MASK 0x80000000 /* this bit is set for a notification that has been canceled */
34 static int retrieve_msg_frame(struct mixart_mgr
*mgr
, u32
*msg_frame
)
36 /* read the message frame fifo */
39 tailptr
= readl_be(MIXART_MEM(mgr
, MSG_OUTBOUND_POST_TAIL
));
40 headptr
= readl_be(MIXART_MEM(mgr
, MSG_OUTBOUND_POST_HEAD
));
42 if (tailptr
== headptr
)
43 return 0; /* no message posted */
45 if (tailptr
< MSG_OUTBOUND_POST_STACK
)
47 if (tailptr
>= MSG_OUTBOUND_POST_STACK
+ MSG_BOUND_STACK_SIZE
)
50 *msg_frame
= readl_be(MIXART_MEM(mgr
, tailptr
));
52 /* increment the tail index */
54 if( tailptr
>= (MSG_OUTBOUND_POST_STACK
+MSG_BOUND_STACK_SIZE
) )
55 tailptr
= MSG_OUTBOUND_POST_STACK
;
56 writel_be(tailptr
, MIXART_MEM(mgr
, MSG_OUTBOUND_POST_TAIL
));
61 static int get_msg(struct mixart_mgr
*mgr
, struct mixart_msg
*resp
,
62 u32 msg_frame_address
)
73 /* copy message descriptor from miXart to driver */
74 size
= readl_be(MIXART_MEM(mgr
, msg_frame_address
)); /* size of descriptor + response */
75 resp
->message_id
= readl_be(MIXART_MEM(mgr
, msg_frame_address
+ 4)); /* dwMessageID */
76 resp
->uid
.object_id
= readl_be(MIXART_MEM(mgr
, msg_frame_address
+ 8)); /* uidDest */
77 resp
->uid
.desc
= readl_be(MIXART_MEM(mgr
, msg_frame_address
+ 12)); /* */
79 if( (size
< MSG_DESCRIPTOR_SIZE
) || (resp
->size
< (size
- MSG_DESCRIPTOR_SIZE
))) {
81 dev_err(&mgr
->pci
->dev
,
82 "problem with response size = %d\n", size
);
85 size
-= MSG_DESCRIPTOR_SIZE
;
87 memcpy_fromio(resp
->data
, MIXART_MEM(mgr
, msg_frame_address
+ MSG_HEADER_SIZE
), size
);
90 /* swap if necessary */
92 size
/= 4; /* u32 size */
93 for(i
=0; i
< size
; i
++) {
94 ((u32
*)resp
->data
)[i
] = be32_to_cpu(((__be32
*)resp
->data
)[i
]);
99 * free message frame address
101 headptr
= readl_be(MIXART_MEM(mgr
, MSG_OUTBOUND_FREE_HEAD
));
103 if( (headptr
< MSG_OUTBOUND_FREE_STACK
) || ( headptr
>= (MSG_OUTBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
))) {
108 /* give address back to outbound fifo */
109 writel_be(msg_frame_address
, MIXART_MEM(mgr
, headptr
));
111 /* increment the outbound free head */
113 if( headptr
>= (MSG_OUTBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
) )
114 headptr
= MSG_OUTBOUND_FREE_STACK
;
116 writel_be(headptr
, MIXART_MEM(mgr
, MSG_OUTBOUND_FREE_HEAD
));
124 * send a message to miXart. return: the msg_frame used for this message
126 /* call with mgr->msg_lock held! */
127 static int send_msg( struct mixart_mgr
*mgr
,
128 struct mixart_msg
*msg
,
133 u32 headptr
, tailptr
;
134 u32 msg_frame_address
;
137 if (snd_BUG_ON(msg
->size
% 4))
140 /* get message frame address */
141 tailptr
= readl_be(MIXART_MEM(mgr
, MSG_INBOUND_FREE_TAIL
));
142 headptr
= readl_be(MIXART_MEM(mgr
, MSG_INBOUND_FREE_HEAD
));
144 if (tailptr
== headptr
) {
145 dev_err(&mgr
->pci
->dev
, "error: no message frame available\n");
149 if( (tailptr
< MSG_INBOUND_FREE_STACK
) || (tailptr
>= (MSG_INBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
))) {
153 msg_frame_address
= readl_be(MIXART_MEM(mgr
, tailptr
));
154 writel(0, MIXART_MEM(mgr
, tailptr
)); /* set address to zero on this fifo position */
156 /* increment the inbound free tail */
158 if( tailptr
>= (MSG_INBOUND_FREE_STACK
+MSG_BOUND_STACK_SIZE
) )
159 tailptr
= MSG_INBOUND_FREE_STACK
;
161 writel_be(tailptr
, MIXART_MEM(mgr
, MSG_INBOUND_FREE_TAIL
));
163 /* TODO : use memcpy_toio() with intermediate buffer to copy the message */
165 /* copy message descriptor to card memory */
166 writel_be( msg
->size
+ MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
) ); /* size of descriptor + request */
167 writel_be( msg
->message_id
, MIXART_MEM(mgr
, msg_frame_address
+ 4) ); /* dwMessageID */
168 writel_be( msg
->uid
.object_id
, MIXART_MEM(mgr
, msg_frame_address
+ 8) ); /* uidDest */
169 writel_be( msg
->uid
.desc
, MIXART_MEM(mgr
, msg_frame_address
+ 12) ); /* */
170 writel_be( MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
+ 16) ); /* SizeHeader */
171 writel_be( MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
+ 20) ); /* OffsetDLL_T16 */
172 writel_be( msg
->size
, MIXART_MEM(mgr
, msg_frame_address
+ 24) ); /* SizeDLL_T16 */
173 writel_be( MSG_DESCRIPTOR_SIZE
, MIXART_MEM(mgr
, msg_frame_address
+ 28) ); /* OffsetDLL_DRV */
174 writel_be( 0, MIXART_MEM(mgr
, msg_frame_address
+ 32) ); /* SizeDLL_DRV */
175 writel_be( MSG_DESCRIPTOR_SIZE
+ max_answersize
, MIXART_MEM(mgr
, msg_frame_address
+ 36) ); /* dwExpectedAnswerSize */
177 /* copy message data to card memory */
178 for( i
=0; i
< msg
->size
; i
+=4 ) {
179 writel_be( *(u32
*)(msg
->data
+ i
), MIXART_MEM(mgr
, MSG_HEADER_SIZE
+ msg_frame_address
+ i
) );
184 /* the pending event is the notification we wait for ! */
185 mgr
->pending_event
= *msg_event
;
188 /* the pending event is the answer we wait for (same address than the request)! */
189 mgr
->pending_event
= msg_frame_address
;
191 /* copy address back to caller */
192 *msg_event
= msg_frame_address
;
196 /* mark the frame as a request (will have an answer) */
197 msg_frame_address
|= MSG_TYPE_REQUEST
;
200 headptr
= readl_be(MIXART_MEM(mgr
, MSG_INBOUND_POST_HEAD
));
202 if( (headptr
< MSG_INBOUND_POST_STACK
) || (headptr
>= (MSG_INBOUND_POST_STACK
+MSG_BOUND_STACK_SIZE
))) {
206 writel_be(msg_frame_address
, MIXART_MEM(mgr
, headptr
));
208 /* increment the inbound post head */
210 if( headptr
>= (MSG_INBOUND_POST_STACK
+MSG_BOUND_STACK_SIZE
) )
211 headptr
= MSG_INBOUND_POST_STACK
;
213 writel_be(headptr
, MIXART_MEM(mgr
, MSG_INBOUND_POST_HEAD
));
219 int snd_mixart_send_msg(struct mixart_mgr
*mgr
, struct mixart_msg
*request
, int max_resp_size
, void *resp_data
)
221 struct mixart_msg resp
;
222 u32 msg_frame
= 0; /* set to 0, so it's no notification to wait for, but the answer */
224 wait_queue_entry_t wait
;
227 init_waitqueue_entry(&wait
, current
);
229 mutex_lock(&mgr
->msg_lock
);
230 /* send the message */
231 err
= send_msg(mgr
, request
, max_resp_size
, 1, &msg_frame
); /* send and mark the answer pending */
233 mutex_unlock(&mgr
->msg_lock
);
237 set_current_state(TASK_UNINTERRUPTIBLE
);
238 add_wait_queue(&mgr
->msg_sleep
, &wait
);
239 mutex_unlock(&mgr
->msg_lock
);
240 timeout
= schedule_timeout(MSG_TIMEOUT_JIFFIES
);
241 remove_wait_queue(&mgr
->msg_sleep
, &wait
);
245 dev_err(&mgr
->pci
->dev
,
246 "error: no response on msg %x\n", msg_frame
);
250 /* retrieve the answer into the same struct mixart_msg */
252 resp
.uid
= (struct mixart_uid
){0,0};
253 resp
.data
= resp_data
;
254 resp
.size
= max_resp_size
;
256 mutex_lock(&mgr
->msg_lock
);
257 err
= get_msg(mgr
, &resp
, msg_frame
);
258 mutex_unlock(&mgr
->msg_lock
);
260 if( request
->message_id
!= resp
.message_id
)
261 dev_err(&mgr
->pci
->dev
, "RESPONSE ERROR!\n");
267 int snd_mixart_send_msg_wait_notif(struct mixart_mgr
*mgr
,
268 struct mixart_msg
*request
, u32 notif_event
)
271 wait_queue_entry_t wait
;
274 if (snd_BUG_ON(!notif_event
))
276 if (snd_BUG_ON((notif_event
& MSG_TYPE_MASK
) != MSG_TYPE_NOTIFY
))
278 if (snd_BUG_ON(notif_event
& MSG_CANCEL_NOTIFY_MASK
))
281 init_waitqueue_entry(&wait
, current
);
283 mutex_lock(&mgr
->msg_lock
);
284 /* send the message */
285 err
= send_msg(mgr
, request
, MSG_DEFAULT_SIZE
, 1, ¬if_event
); /* send and mark the notification event pending */
287 mutex_unlock(&mgr
->msg_lock
);
291 set_current_state(TASK_UNINTERRUPTIBLE
);
292 add_wait_queue(&mgr
->msg_sleep
, &wait
);
293 mutex_unlock(&mgr
->msg_lock
);
294 timeout
= schedule_timeout(MSG_TIMEOUT_JIFFIES
);
295 remove_wait_queue(&mgr
->msg_sleep
, &wait
);
299 dev_err(&mgr
->pci
->dev
,
300 "error: notification %x not received\n", notif_event
);
308 int snd_mixart_send_msg_nonblock(struct mixart_mgr
*mgr
, struct mixart_msg
*request
)
313 /* just send the message (do not mark it as a pending one) */
314 mutex_lock(&mgr
->msg_lock
);
315 err
= send_msg(mgr
, request
, MSG_DEFAULT_SIZE
, 0, &message_frame
);
316 mutex_unlock(&mgr
->msg_lock
);
318 /* the answer will be handled by snd_struct mixart_msgasklet() */
319 atomic_inc(&mgr
->msg_processed
);
325 /* common buffer of interrupt to send/receive messages */
326 static u32 mixart_msg_data
[MSG_DEFAULT_SIZE
/ 4];
329 static void snd_mixart_process_msg(struct mixart_mgr
*mgr
)
331 struct mixart_msg resp
;
335 while (mgr
->msg_fifo_readptr
!= mgr
->msg_fifo_writeptr
) {
336 msg
= mgr
->msg_fifo
[mgr
->msg_fifo_readptr
];
337 mgr
->msg_fifo_readptr
++;
338 mgr
->msg_fifo_readptr
%= MSG_FIFO_SIZE
;
340 /* process the message ... */
341 addr
= msg
& ~MSG_TYPE_MASK
;
342 type
= msg
& MSG_TYPE_MASK
;
345 case MSG_TYPE_ANSWER
:
346 /* answer to a message on that we did not wait for (send_msg_nonblock) */
348 resp
.data
= mixart_msg_data
;
349 resp
.size
= sizeof(mixart_msg_data
);
350 err
= get_msg(mgr
, &resp
, addr
);
352 dev_err(&mgr
->pci
->dev
,
353 "error(%d) reading mf %x\n",
358 switch(resp
.message_id
) {
359 case MSG_STREAM_START_INPUT_STAGE_PACKET
:
360 case MSG_STREAM_START_OUTPUT_STAGE_PACKET
:
361 case MSG_STREAM_STOP_INPUT_STAGE_PACKET
:
362 case MSG_STREAM_STOP_OUTPUT_STAGE_PACKET
:
363 if(mixart_msg_data
[0])
364 dev_err(&mgr
->pci
->dev
,
365 "error MSG_STREAM_ST***_***PUT_STAGE_PACKET status=%x\n",
369 dev_dbg(&mgr
->pci
->dev
,
370 "received mf(%x) : msg_id(%x) uid(%x, %x) size(%zd)\n",
371 msg
, resp
.message_id
, resp
.uid
.object_id
, resp
.uid
.desc
, resp
.size
);
375 case MSG_TYPE_NOTIFY
:
376 /* msg contains no address ! do not get_msg() ! */
377 case MSG_TYPE_COMMAND
:
378 /* get_msg() necessary */
380 dev_err(&mgr
->pci
->dev
,
381 "doesn't know what to do with message %x\n",
385 /* decrement counter */
386 atomic_dec(&mgr
->msg_processed
);
388 } /* while there is a msg in fifo */
392 irqreturn_t
snd_mixart_interrupt(int irq
, void *dev_id
)
394 struct mixart_mgr
*mgr
= dev_id
;
397 it_reg
= readl_le(MIXART_REG(mgr
, MIXART_PCI_OMISR_OFFSET
));
398 if( !(it_reg
& MIXART_OIDI
) ) {
399 /* this device did not cause the interrupt */
403 /* mask all interrupts */
404 writel_le(MIXART_HOST_ALL_INTERRUPT_MASKED
, MIXART_REG(mgr
, MIXART_PCI_OMIMR_OFFSET
));
406 /* outdoorbell register clear */
407 it_reg
= readl(MIXART_REG(mgr
, MIXART_PCI_ODBR_OFFSET
));
408 writel(it_reg
, MIXART_REG(mgr
, MIXART_PCI_ODBR_OFFSET
));
410 /* clear interrupt */
411 writel_le( MIXART_OIDI
, MIXART_REG(mgr
, MIXART_PCI_OMISR_OFFSET
) );
413 return IRQ_WAKE_THREAD
;
416 irqreturn_t
snd_mixart_threaded_irq(int irq
, void *dev_id
)
418 struct mixart_mgr
*mgr
= dev_id
;
420 struct mixart_msg resp
;
423 mutex_lock(&mgr
->lock
);
424 /* process interrupt */
425 while (retrieve_msg_frame(mgr
, &msg
)) {
427 switch (msg
& MSG_TYPE_MASK
) {
428 case MSG_TYPE_COMMAND
:
430 resp
.data
= mixart_msg_data
;
431 resp
.size
= sizeof(mixart_msg_data
);
432 err
= get_msg(mgr
, &resp
, msg
& ~MSG_TYPE_MASK
);
434 dev_err(&mgr
->pci
->dev
,
435 "interrupt: error(%d) reading mf %x\n",
440 if(resp
.message_id
== MSG_SERVICES_TIMER_NOTIFY
) {
442 struct mixart_timer_notify
*notify
;
443 notify
= (struct mixart_timer_notify
*)mixart_msg_data
;
445 BUILD_BUG_ON(sizeof(notify
) > sizeof(mixart_msg_data
));
446 if (snd_BUG_ON(notify
->stream_count
> ARRAY_SIZE(notify
->streams
)))
448 for(i
=0; i
<notify
->stream_count
; i
++) {
450 u32 buffer_id
= notify
->streams
[i
].buffer_id
;
451 unsigned int chip_number
= (buffer_id
& MIXART_NOTIFY_CARD_MASK
) >> MIXART_NOTIFY_CARD_OFFSET
; /* card0 to 3 */
452 unsigned int pcm_number
= (buffer_id
& MIXART_NOTIFY_PCM_MASK
) >> MIXART_NOTIFY_PCM_OFFSET
; /* pcm0 to 3 */
453 unsigned int sub_number
= buffer_id
& MIXART_NOTIFY_SUBS_MASK
; /* 0 to MIXART_PLAYBACK_STREAMS */
454 unsigned int is_capture
= ((buffer_id
& MIXART_NOTIFY_CAPT_MASK
) != 0); /* playback == 0 / capture == 1 */
456 struct snd_mixart
*chip
= mgr
->chip
[chip_number
];
457 struct mixart_stream
*stream
;
459 if ((chip_number
>= mgr
->num_cards
) || (pcm_number
>= MIXART_PCM_TOTAL
) || (sub_number
>= MIXART_PLAYBACK_STREAMS
)) {
460 dev_err(&mgr
->pci
->dev
,
461 "error MSG_SERVICES_TIMER_NOTIFY buffer_id (%x) pos(%d)\n",
462 buffer_id
, notify
->streams
[i
].sample_pos_low_part
);
467 stream
= &chip
->capture_stream
[pcm_number
];
469 stream
= &chip
->playback_stream
[pcm_number
][sub_number
];
471 if (stream
->substream
&& (stream
->status
== MIXART_STREAM_STATUS_RUNNING
)) {
472 struct snd_pcm_runtime
*runtime
= stream
->substream
->runtime
;
474 u64 sample_count
= ((u64
)notify
->streams
[i
].sample_pos_high_part
) << 32;
475 sample_count
|= notify
->streams
[i
].sample_pos_low_part
;
478 u64 new_elapse_pos
= stream
->abs_period_elapsed
+ runtime
->period_size
;
480 if (new_elapse_pos
> sample_count
) {
485 stream
->buf_periods
++;
486 if (stream
->buf_periods
>= runtime
->periods
)
487 stream
->buf_periods
= 0;
489 stream
->abs_period_elapsed
= new_elapse_pos
;
492 stream
->buf_period_frag
= (u32
)( sample_count
- stream
->abs_period_elapsed
);
495 mutex_unlock(&mgr
->lock
);
496 snd_pcm_period_elapsed(stream
->substream
);
497 mutex_lock(&mgr
->lock
);
503 if(resp
.message_id
== MSG_SERVICES_REPORT_TRACES
) {
506 /* Traces are text: the swapped msg_data has to be swapped back ! */
508 for(i
=0; i
<(resp
.size
/4); i
++) {
509 ((__be32
*)mixart_msg_data
)[i
] = cpu_to_be32((mixart_msg_data
)[i
]);
512 ((char*)mixart_msg_data
)[resp
.size
- 1] = 0;
513 dev_dbg(&mgr
->pci
->dev
,
514 "MIXART TRACE : %s\n",
515 (char *)mixart_msg_data
);
520 dev_dbg(&mgr
->pci
->dev
, "command %x not handled\n",
524 case MSG_TYPE_NOTIFY
:
525 if(msg
& MSG_CANCEL_NOTIFY_MASK
) {
526 msg
&= ~MSG_CANCEL_NOTIFY_MASK
;
527 dev_err(&mgr
->pci
->dev
,
528 "canceled notification %x !\n", msg
);
531 case MSG_TYPE_ANSWER
:
532 /* answer or notification to a message we are waiting for*/
533 mutex_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 snd_mixart_process_msg(mgr
);
545 mutex_unlock(&mgr
->msg_lock
);
547 case MSG_TYPE_REQUEST
:
549 dev_dbg(&mgr
->pci
->dev
,
550 "interrupt received request %x\n", msg
);
551 /* TODO : are there things to do here ? */
553 } /* switch on msg type */
554 } /* while there are msgs */
556 /* allow interrupt again */
557 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL
, MIXART_REG( mgr
, MIXART_PCI_OMIMR_OFFSET
));
559 mutex_unlock(&mgr
->lock
);
565 void snd_mixart_init_mailbox(struct mixart_mgr
*mgr
)
567 writel( 0, MIXART_MEM( mgr
, MSG_HOST_RSC_PROTECTION
) );
568 writel( 0, MIXART_MEM( mgr
, MSG_AGENT_RSC_PROTECTION
) );
570 /* allow outbound messagebox to generate interrupts */
572 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL
, MIXART_REG( mgr
, MIXART_PCI_OMIMR_OFFSET
));
577 void snd_mixart_exit_mailbox(struct mixart_mgr
*mgr
)
579 /* no more interrupts on outbound messagebox */
580 writel_le( MIXART_HOST_ALL_INTERRUPT_MASKED
, MIXART_REG( mgr
, MIXART_PCI_OMIMR_OFFSET
));
584 void snd_mixart_reset_board(struct mixart_mgr
*mgr
)
587 writel_be( 1, MIXART_REG(mgr
, MIXART_BA1_BRUTAL_RESET_OFFSET
) );