3 * ALSA driver for the digigram lx6464es interface
6 * Copyright (c) 2009 Tim Blechmann <tim@klingt.org>
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; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 /* #define RMH_DEBUG 1 */
27 #include <linux/bitops.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/delay.h>
35 /* low-level register access */
37 static const unsigned long dsp_port_offsets[] = {
70 static void __iomem *lx_dsp_register(struct lx6464es *chip, int port)
72 void __iomem *base_address = chip->port_dsp_bar;
73 return base_address + dsp_port_offsets[port]*4;
76 unsigned long lx_dsp_reg_read(struct lx6464es *chip, int port)
78 void __iomem *address = lx_dsp_register(chip, port);
79 return ioread32(address);
82 static void lx_dsp_reg_readbuf(struct lx6464es *chip, int port, u32 *data,
85 u32 __iomem *address = lx_dsp_register(chip, port);
88 /* we cannot use memcpy_fromio */
89 for (i = 0; i != len; ++i)
90 data[i] = ioread32(address + i);
94 void lx_dsp_reg_write(struct lx6464es *chip, int port, unsigned data)
96 void __iomem *address = lx_dsp_register(chip, port);
97 iowrite32(data, address);
100 static void lx_dsp_reg_writebuf(struct lx6464es *chip, int port,
101 const u32 *data, u32 len)
103 u32 __iomem *address = lx_dsp_register(chip, port);
106 /* we cannot use memcpy_to */
107 for (i = 0; i != len; ++i)
108 iowrite32(data[i], address + i);
112 static const unsigned long plx_port_offsets[] = {
127 static void __iomem *lx_plx_register(struct lx6464es *chip, int port)
129 void __iomem *base_address = chip->port_plx_remapped;
130 return base_address + plx_port_offsets[port];
133 unsigned long lx_plx_reg_read(struct lx6464es *chip, int port)
135 void __iomem *address = lx_plx_register(chip, port);
136 return ioread32(address);
139 void lx_plx_reg_write(struct lx6464es *chip, int port, u32 data)
141 void __iomem *address = lx_plx_register(chip, port);
142 iowrite32(data, address);
147 #ifdef CONFIG_SND_DEBUG
148 #define CMD_NAME(a) a
150 #define CMD_NAME(a) NULL
153 #define Reg_CSM_MR 0x00000002
154 #define Reg_CSM_MC 0x00000001
156 struct dsp_cmd_info {
157 u32 dcCodeOp; /* Op Code of the command (usually 1st 24-bits
159 u16 dcCmdLength; /* Command length in words of 24 bits.*/
160 u16 dcStatusType; /* Status type: 0 for fixed length, 1 for
162 u16 dcStatusLength; /* Status length (if fixed).*/
167 Initialization and control data for the Microblaze interface
169 the opcode field of the command set at the proper offset
171 the number of command words
173 offset in the status registers: 0 means that the return value may be
174 different from 0, and must be read
176 the number of status words (in addition to the return value)
179 static struct dsp_cmd_info dsp_commands[] =
181 { (CMD_00_INFO_DEBUG << OPCODE_OFFSET) , 1 /*custom*/
182 , 1 , 0 /**/ , CMD_NAME("INFO_DEBUG") },
183 { (CMD_01_GET_SYS_CFG << OPCODE_OFFSET) , 1 /**/
184 , 1 , 2 /**/ , CMD_NAME("GET_SYS_CFG") },
185 { (CMD_02_SET_GRANULARITY << OPCODE_OFFSET) , 1 /**/
186 , 1 , 0 /**/ , CMD_NAME("SET_GRANULARITY") },
187 { (CMD_03_SET_TIMER_IRQ << OPCODE_OFFSET) , 1 /**/
188 , 1 , 0 /**/ , CMD_NAME("SET_TIMER_IRQ") },
189 { (CMD_04_GET_EVENT << OPCODE_OFFSET) , 1 /**/
190 , 1 , 0 /*up to 10*/ , CMD_NAME("GET_EVENT") },
191 { (CMD_05_GET_PIPES << OPCODE_OFFSET) , 1 /**/
192 , 1 , 2 /*up to 4*/ , CMD_NAME("GET_PIPES") },
193 { (CMD_06_ALLOCATE_PIPE << OPCODE_OFFSET) , 1 /**/
194 , 0 , 0 /**/ , CMD_NAME("ALLOCATE_PIPE") },
195 { (CMD_07_RELEASE_PIPE << OPCODE_OFFSET) , 1 /**/
196 , 0 , 0 /**/ , CMD_NAME("RELEASE_PIPE") },
197 { (CMD_08_ASK_BUFFERS << OPCODE_OFFSET) , 1 /**/
198 , 1 , MAX_STREAM_BUFFER , CMD_NAME("ASK_BUFFERS") },
199 { (CMD_09_STOP_PIPE << OPCODE_OFFSET) , 1 /**/
200 , 0 , 0 /*up to 2*/ , CMD_NAME("STOP_PIPE") },
201 { (CMD_0A_GET_PIPE_SPL_COUNT << OPCODE_OFFSET) , 1 /**/
202 , 1 , 1 /*up to 2*/ , CMD_NAME("GET_PIPE_SPL_COUNT") },
203 { (CMD_0B_TOGGLE_PIPE_STATE << OPCODE_OFFSET) , 1 /*up to 5*/
204 , 1 , 0 /**/ , CMD_NAME("TOGGLE_PIPE_STATE") },
205 { (CMD_0C_DEF_STREAM << OPCODE_OFFSET) , 1 /*up to 4*/
206 , 1 , 0 /**/ , CMD_NAME("DEF_STREAM") },
207 { (CMD_0D_SET_MUTE << OPCODE_OFFSET) , 3 /**/
208 , 1 , 0 /**/ , CMD_NAME("SET_MUTE") },
209 { (CMD_0E_GET_STREAM_SPL_COUNT << OPCODE_OFFSET) , 1/**/
210 , 1 , 2 /**/ , CMD_NAME("GET_STREAM_SPL_COUNT") },
211 { (CMD_0F_UPDATE_BUFFER << OPCODE_OFFSET) , 3 /*up to 4*/
212 , 0 , 1 /**/ , CMD_NAME("UPDATE_BUFFER") },
213 { (CMD_10_GET_BUFFER << OPCODE_OFFSET) , 1 /**/
214 , 1 , 4 /**/ , CMD_NAME("GET_BUFFER") },
215 { (CMD_11_CANCEL_BUFFER << OPCODE_OFFSET) , 1 /**/
216 , 1 , 1 /*up to 4*/ , CMD_NAME("CANCEL_BUFFER") },
217 { (CMD_12_GET_PEAK << OPCODE_OFFSET) , 1 /**/
218 , 1 , 1 /**/ , CMD_NAME("GET_PEAK") },
219 { (CMD_13_SET_STREAM_STATE << OPCODE_OFFSET) , 1 /**/
220 , 1 , 0 /**/ , CMD_NAME("SET_STREAM_STATE") },
223 static void lx_message_init(struct lx_rmh *rmh, enum cmd_mb_opcodes cmd)
225 snd_BUG_ON(cmd >= CMD_14_INVALID);
227 rmh->cmd[0] = dsp_commands[cmd].dcCodeOp;
228 rmh->cmd_len = dsp_commands[cmd].dcCmdLength;
229 rmh->stat_len = dsp_commands[cmd].dcStatusLength;
230 rmh->dsp_stat = dsp_commands[cmd].dcStatusType;
232 memset(&rmh->cmd[1], 0, (REG_CRM_NUMBER - 1) * sizeof(u32));
234 #ifdef CONFIG_SND_DEBUG
235 memset(rmh->stat, 0, REG_CRM_NUMBER * sizeof(u32));
243 #define LXRMH "lx6464es rmh: "
244 static void lx_message_dump(struct lx_rmh *rmh)
246 u8 idx = rmh->cmd_idx;
249 snd_printk(LXRMH "command %s\n", dsp_commands[idx].dcOpName);
251 for (i = 0; i != rmh->cmd_len; ++i)
252 snd_printk(LXRMH "\tcmd[%d] %08x\n", i, rmh->cmd[i]);
254 for (i = 0; i != rmh->stat_len; ++i)
255 snd_printk(LXRMH "\tstat[%d]: %08x\n", i, rmh->stat[i]);
259 static inline void lx_message_dump(struct lx_rmh *rmh)
265 /* sleep 500 - 100 = 400 times 100us -> the timeout is >= 40 ms */
266 #define XILINX_TIMEOUT_MS 40
267 #define XILINX_POLL_NO_SLEEP 100
268 #define XILINX_POLL_ITERATIONS 150
271 static int lx_message_send_atomic(struct lx6464es *chip, struct lx_rmh *rmh)
273 u32 reg = ED_DSP_TIMED_OUT;
276 if (lx_dsp_reg_read(chip, eReg_CSM) & (Reg_CSM_MC | Reg_CSM_MR)) {
277 dev_err(chip->card->dev, "PIOSendMessage eReg_CSM %x\n", reg);
282 lx_dsp_reg_writebuf(chip, eReg_CRM1, rmh->cmd, rmh->cmd_len);
284 /* MicoBlaze gogogo */
285 lx_dsp_reg_write(chip, eReg_CSM, Reg_CSM_MC);
287 /* wait for device to answer */
288 for (dwloop = 0; dwloop != XILINX_TIMEOUT_MS * 1000; ++dwloop) {
289 if (lx_dsp_reg_read(chip, eReg_CSM) & Reg_CSM_MR) {
290 if (rmh->dsp_stat == 0)
291 reg = lx_dsp_reg_read(chip, eReg_CRM1);
294 goto polling_successful;
298 dev_warn(chip->card->dev, "TIMEOUT lx_message_send_atomic! "
302 if ((reg & ERROR_VALUE) == 0) {
305 snd_BUG_ON(rmh->stat_len >= (REG_CRM_NUMBER-1));
306 lx_dsp_reg_readbuf(chip, eReg_CRM2, rmh->stat,
310 dev_err(chip->card->dev, "rmh error: %08x\n", reg);
312 /* clear Reg_CSM_MR */
313 lx_dsp_reg_write(chip, eReg_CSM, 0);
316 case ED_DSP_TIMED_OUT:
317 dev_warn(chip->card->dev, "lx_message_send: dsp timeout\n");
321 dev_warn(chip->card->dev, "lx_message_send: dsp crashed\n");
325 lx_message_dump(rmh);
331 /* low-level dsp access */
332 int lx_dsp_get_version(struct lx6464es *chip, u32 *rdsp_version)
336 mutex_lock(&chip->msg_lock);
338 lx_message_init(&chip->rmh, CMD_01_GET_SYS_CFG);
339 ret = lx_message_send_atomic(chip, &chip->rmh);
341 *rdsp_version = chip->rmh.stat[1];
342 mutex_unlock(&chip->msg_lock);
346 int lx_dsp_get_clock_frequency(struct lx6464es *chip, u32 *rfreq)
353 mutex_lock(&chip->msg_lock);
355 lx_message_init(&chip->rmh, CMD_01_GET_SYS_CFG);
356 ret = lx_message_send_atomic(chip, &chip->rmh);
359 freq_raw = chip->rmh.stat[0] >> FREQ_FIELD_OFFSET;
360 freq = freq_raw & XES_FREQ_COUNT8_MASK;
362 if ((freq < XES_FREQ_COUNT8_48_MAX) ||
363 (freq > XES_FREQ_COUNT8_44_MIN))
364 frequency = 0; /* unknown */
365 else if (freq >= XES_FREQ_COUNT8_44_MAX)
371 mutex_unlock(&chip->msg_lock);
373 *rfreq = frequency * chip->freq_ratio;
378 int lx_dsp_get_mac(struct lx6464es *chip)
382 macmsb = lx_dsp_reg_read(chip, eReg_ADMACESMSB) & 0x00FFFFFF;
383 maclsb = lx_dsp_reg_read(chip, eReg_ADMACESLSB) & 0x00FFFFFF;
385 /* todo: endianess handling */
386 chip->mac_address[5] = ((u8 *)(&maclsb))[0];
387 chip->mac_address[4] = ((u8 *)(&maclsb))[1];
388 chip->mac_address[3] = ((u8 *)(&maclsb))[2];
389 chip->mac_address[2] = ((u8 *)(&macmsb))[0];
390 chip->mac_address[1] = ((u8 *)(&macmsb))[1];
391 chip->mac_address[0] = ((u8 *)(&macmsb))[2];
397 int lx_dsp_set_granularity(struct lx6464es *chip, u32 gran)
401 mutex_lock(&chip->msg_lock);
403 lx_message_init(&chip->rmh, CMD_02_SET_GRANULARITY);
404 chip->rmh.cmd[0] |= gran;
406 ret = lx_message_send_atomic(chip, &chip->rmh);
407 mutex_unlock(&chip->msg_lock);
411 int lx_dsp_read_async_events(struct lx6464es *chip, u32 *data)
415 mutex_lock(&chip->msg_lock);
417 lx_message_init(&chip->rmh, CMD_04_GET_EVENT);
418 chip->rmh.stat_len = 9; /* we don't necessarily need the full length */
420 ret = lx_message_send_atomic(chip, &chip->rmh);
423 memcpy(data, chip->rmh.stat, chip->rmh.stat_len * sizeof(u32));
425 mutex_unlock(&chip->msg_lock);
429 #define PIPE_INFO_TO_CMD(capture, pipe) \
430 ((u32)((u32)(pipe) | ((capture) ? ID_IS_CAPTURE : 0L)) << ID_OFFSET)
434 /* low-level pipe handling */
435 int lx_pipe_allocate(struct lx6464es *chip, u32 pipe, int is_capture,
439 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
441 mutex_lock(&chip->msg_lock);
442 lx_message_init(&chip->rmh, CMD_06_ALLOCATE_PIPE);
444 chip->rmh.cmd[0] |= pipe_cmd;
445 chip->rmh.cmd[0] |= channels;
447 err = lx_message_send_atomic(chip, &chip->rmh);
448 mutex_unlock(&chip->msg_lock);
451 dev_err(chip->card->dev, "could not allocate pipe\n");
456 int lx_pipe_release(struct lx6464es *chip, u32 pipe, int is_capture)
459 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
461 mutex_lock(&chip->msg_lock);
462 lx_message_init(&chip->rmh, CMD_07_RELEASE_PIPE);
464 chip->rmh.cmd[0] |= pipe_cmd;
466 err = lx_message_send_atomic(chip, &chip->rmh);
467 mutex_unlock(&chip->msg_lock);
472 int lx_buffer_ask(struct lx6464es *chip, u32 pipe, int is_capture,
473 u32 *r_needed, u32 *r_freed, u32 *size_array)
476 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
478 #ifdef CONFIG_SND_DEBUG
480 memset(size_array, 0, sizeof(u32)*MAX_STREAM_BUFFER);
486 mutex_lock(&chip->msg_lock);
487 lx_message_init(&chip->rmh, CMD_08_ASK_BUFFERS);
489 chip->rmh.cmd[0] |= pipe_cmd;
491 err = lx_message_send_atomic(chip, &chip->rmh);
495 for (i = 0; i < MAX_STREAM_BUFFER; ++i) {
496 u32 stat = chip->rmh.stat[i];
497 if (stat & (BF_EOB << BUFF_FLAGS_OFFSET)) {
501 size_array[i] = stat & MASK_DATA_SIZE;
502 } else if ((stat & (BF_VALID << BUFF_FLAGS_OFFSET))
508 dev_dbg(chip->card->dev,
509 "CMD_08_ASK_BUFFERS: needed %d, freed %d\n",
510 *r_needed, *r_freed);
511 for (i = 0; i < MAX_STREAM_BUFFER; ++i) {
512 for (i = 0; i != chip->rmh.stat_len; ++i)
513 dev_dbg(chip->card->dev,
514 " stat[%d]: %x, %x\n", i,
516 chip->rmh.stat[i] & MASK_DATA_SIZE);
520 mutex_unlock(&chip->msg_lock);
525 int lx_pipe_stop(struct lx6464es *chip, u32 pipe, int is_capture)
528 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
530 mutex_lock(&chip->msg_lock);
531 lx_message_init(&chip->rmh, CMD_09_STOP_PIPE);
533 chip->rmh.cmd[0] |= pipe_cmd;
535 err = lx_message_send_atomic(chip, &chip->rmh);
537 mutex_unlock(&chip->msg_lock);
541 static int lx_pipe_toggle_state(struct lx6464es *chip, u32 pipe, int is_capture)
544 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
546 mutex_lock(&chip->msg_lock);
547 lx_message_init(&chip->rmh, CMD_0B_TOGGLE_PIPE_STATE);
549 chip->rmh.cmd[0] |= pipe_cmd;
551 err = lx_message_send_atomic(chip, &chip->rmh);
553 mutex_unlock(&chip->msg_lock);
558 int lx_pipe_start(struct lx6464es *chip, u32 pipe, int is_capture)
562 err = lx_pipe_wait_for_idle(chip, pipe, is_capture);
566 err = lx_pipe_toggle_state(chip, pipe, is_capture);
571 int lx_pipe_pause(struct lx6464es *chip, u32 pipe, int is_capture)
575 err = lx_pipe_wait_for_start(chip, pipe, is_capture);
579 err = lx_pipe_toggle_state(chip, pipe, is_capture);
585 int lx_pipe_sample_count(struct lx6464es *chip, u32 pipe, int is_capture,
589 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
591 mutex_lock(&chip->msg_lock);
592 lx_message_init(&chip->rmh, CMD_0A_GET_PIPE_SPL_COUNT);
594 chip->rmh.cmd[0] |= pipe_cmd;
595 chip->rmh.stat_len = 2; /* need all words here! */
597 err = lx_message_send_atomic(chip, &chip->rmh); /* don't sleep! */
600 dev_err(chip->card->dev,
601 "could not query pipe's sample count\n");
603 *rsample_count = ((u64)(chip->rmh.stat[0] & MASK_SPL_COUNT_HI)
605 + chip->rmh.stat[1]; /* lo part */
608 mutex_unlock(&chip->msg_lock);
612 int lx_pipe_state(struct lx6464es *chip, u32 pipe, int is_capture, u16 *rstate)
615 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
617 mutex_lock(&chip->msg_lock);
618 lx_message_init(&chip->rmh, CMD_0A_GET_PIPE_SPL_COUNT);
620 chip->rmh.cmd[0] |= pipe_cmd;
622 err = lx_message_send_atomic(chip, &chip->rmh);
625 dev_err(chip->card->dev, "could not query pipe's state\n");
627 *rstate = (chip->rmh.stat[0] >> PSTATE_OFFSET) & 0x0F;
629 mutex_unlock(&chip->msg_lock);
633 static int lx_pipe_wait_for_state(struct lx6464es *chip, u32 pipe,
634 int is_capture, u16 state)
638 /* max 2*PCMOnlyGranularity = 2*1024 at 44100 = < 50 ms:
640 for (i = 0; i != 50; ++i) {
642 int err = lx_pipe_state(chip, pipe, is_capture, ¤t_state);
647 if (!err && current_state == state)
656 int lx_pipe_wait_for_start(struct lx6464es *chip, u32 pipe, int is_capture)
658 return lx_pipe_wait_for_state(chip, pipe, is_capture, PSTATE_RUN);
661 int lx_pipe_wait_for_idle(struct lx6464es *chip, u32 pipe, int is_capture)
663 return lx_pipe_wait_for_state(chip, pipe, is_capture, PSTATE_IDLE);
666 /* low-level stream handling */
667 int lx_stream_set_state(struct lx6464es *chip, u32 pipe,
668 int is_capture, enum stream_state_t state)
671 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
673 mutex_lock(&chip->msg_lock);
674 lx_message_init(&chip->rmh, CMD_13_SET_STREAM_STATE);
676 chip->rmh.cmd[0] |= pipe_cmd;
677 chip->rmh.cmd[0] |= state;
679 err = lx_message_send_atomic(chip, &chip->rmh);
680 mutex_unlock(&chip->msg_lock);
685 int lx_stream_set_format(struct lx6464es *chip, struct snd_pcm_runtime *runtime,
686 u32 pipe, int is_capture)
689 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
690 u32 channels = runtime->channels;
692 if (runtime->channels != channels)
693 dev_err(chip->card->dev, "channel count mismatch: %d vs %d",
694 runtime->channels, channels);
696 mutex_lock(&chip->msg_lock);
697 lx_message_init(&chip->rmh, CMD_0C_DEF_STREAM);
699 chip->rmh.cmd[0] |= pipe_cmd;
701 if (runtime->sample_bits == 16)
703 chip->rmh.cmd[0] |= (STREAM_FMT_16b << STREAM_FMT_OFFSET);
705 if (snd_pcm_format_little_endian(runtime->format))
706 /* little endian/intel format */
707 chip->rmh.cmd[0] |= (STREAM_FMT_intel << STREAM_FMT_OFFSET);
709 chip->rmh.cmd[0] |= channels-1;
711 err = lx_message_send_atomic(chip, &chip->rmh);
712 mutex_unlock(&chip->msg_lock);
717 int lx_stream_state(struct lx6464es *chip, u32 pipe, int is_capture,
721 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
723 mutex_lock(&chip->msg_lock);
724 lx_message_init(&chip->rmh, CMD_0E_GET_STREAM_SPL_COUNT);
726 chip->rmh.cmd[0] |= pipe_cmd;
728 err = lx_message_send_atomic(chip, &chip->rmh);
730 *rstate = (chip->rmh.stat[0] & SF_START) ? START_STATE : PAUSE_STATE;
732 mutex_unlock(&chip->msg_lock);
736 int lx_stream_sample_position(struct lx6464es *chip, u32 pipe, int is_capture,
740 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
742 mutex_lock(&chip->msg_lock);
743 lx_message_init(&chip->rmh, CMD_0E_GET_STREAM_SPL_COUNT);
745 chip->rmh.cmd[0] |= pipe_cmd;
747 err = lx_message_send_atomic(chip, &chip->rmh);
749 *r_bytepos = ((u64) (chip->rmh.stat[0] & MASK_SPL_COUNT_HI)
751 + chip->rmh.stat[1]; /* lo part */
753 mutex_unlock(&chip->msg_lock);
757 /* low-level buffer handling */
758 int lx_buffer_give(struct lx6464es *chip, u32 pipe, int is_capture,
759 u32 buffer_size, u32 buf_address_lo, u32 buf_address_hi,
763 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
765 mutex_lock(&chip->msg_lock);
766 lx_message_init(&chip->rmh, CMD_0F_UPDATE_BUFFER);
768 chip->rmh.cmd[0] |= pipe_cmd;
769 chip->rmh.cmd[0] |= BF_NOTIFY_EOB; /* request interrupt notification */
771 /* todo: pause request, circular buffer */
773 chip->rmh.cmd[1] = buffer_size & MASK_DATA_SIZE;
774 chip->rmh.cmd[2] = buf_address_lo;
776 if (buf_address_hi) {
777 chip->rmh.cmd_len = 4;
778 chip->rmh.cmd[3] = buf_address_hi;
779 chip->rmh.cmd[0] |= BF_64BITS_ADR;
782 err = lx_message_send_atomic(chip, &chip->rmh);
785 *r_buffer_index = chip->rmh.stat[0];
789 if (err == EB_RBUFFERS_TABLE_OVERFLOW)
790 dev_err(chip->card->dev,
791 "lx_buffer_give EB_RBUFFERS_TABLE_OVERFLOW\n");
793 if (err == EB_INVALID_STREAM)
794 dev_err(chip->card->dev,
795 "lx_buffer_give EB_INVALID_STREAM\n");
797 if (err == EB_CMD_REFUSED)
798 dev_err(chip->card->dev,
799 "lx_buffer_give EB_CMD_REFUSED\n");
802 mutex_unlock(&chip->msg_lock);
806 int lx_buffer_free(struct lx6464es *chip, u32 pipe, int is_capture,
810 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
812 mutex_lock(&chip->msg_lock);
813 lx_message_init(&chip->rmh, CMD_11_CANCEL_BUFFER);
815 chip->rmh.cmd[0] |= pipe_cmd;
816 chip->rmh.cmd[0] |= MASK_BUFFER_ID; /* ask for the current buffer: the
817 * microblaze will seek for it */
819 err = lx_message_send_atomic(chip, &chip->rmh);
822 *r_buffer_size = chip->rmh.stat[0] & MASK_DATA_SIZE;
824 mutex_unlock(&chip->msg_lock);
828 int lx_buffer_cancel(struct lx6464es *chip, u32 pipe, int is_capture,
832 u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
834 mutex_lock(&chip->msg_lock);
835 lx_message_init(&chip->rmh, CMD_11_CANCEL_BUFFER);
837 chip->rmh.cmd[0] |= pipe_cmd;
838 chip->rmh.cmd[0] |= buffer_index;
840 err = lx_message_send_atomic(chip, &chip->rmh);
842 mutex_unlock(&chip->msg_lock);
847 /* low-level gain/peak handling
849 * \todo: can we unmute capture/playback channels independently?
852 int lx_level_unmute(struct lx6464es *chip, int is_capture, int unmute)
855 /* bit set to 1: channel muted */
856 u64 mute_mask = unmute ? 0 : 0xFFFFFFFFFFFFFFFFLLU;
858 mutex_lock(&chip->msg_lock);
859 lx_message_init(&chip->rmh, CMD_0D_SET_MUTE);
861 chip->rmh.cmd[0] |= PIPE_INFO_TO_CMD(is_capture, 0);
863 chip->rmh.cmd[1] = (u32)(mute_mask >> (u64)32); /* hi part */
864 chip->rmh.cmd[2] = (u32)(mute_mask & (u64)0xFFFFFFFF); /* lo part */
866 dev_dbg(chip->card->dev,
867 "mute %x %x %x\n", chip->rmh.cmd[0], chip->rmh.cmd[1],
870 err = lx_message_send_atomic(chip, &chip->rmh);
872 mutex_unlock(&chip->msg_lock);
876 static u32 peak_map[] = {
877 0x00000109, /* -90.308dB */
878 0x0000083B, /* -72.247dB */
879 0x000020C4, /* -60.205dB */
880 0x00008273, /* -48.030dB */
881 0x00020756, /* -36.005dB */
882 0x00040C37, /* -30.001dB */
883 0x00081385, /* -24.002dB */
884 0x00101D3F, /* -18.000dB */
885 0x0016C310, /* -15.000dB */
886 0x002026F2, /* -12.001dB */
887 0x002D6A86, /* -9.000dB */
888 0x004026E6, /* -6.004dB */
889 0x005A9DF6, /* -3.000dB */
890 0x0065AC8B, /* -2.000dB */
891 0x00721481, /* -1.000dB */
895 int lx_level_peaks(struct lx6464es *chip, int is_capture, int channels,
901 mutex_lock(&chip->msg_lock);
902 for (i = 0; i < channels; i += 4) {
905 lx_message_init(&chip->rmh, CMD_12_GET_PEAK);
906 chip->rmh.cmd[0] |= PIPE_INFO_TO_CMD(is_capture, i);
908 err = lx_message_send_atomic(chip, &chip->rmh);
911 s0 = peak_map[chip->rmh.stat[0] & 0x0F];
912 s1 = peak_map[(chip->rmh.stat[0] >> 4) & 0xf];
913 s2 = peak_map[(chip->rmh.stat[0] >> 8) & 0xf];
914 s3 = peak_map[(chip->rmh.stat[0] >> 12) & 0xf];
916 s0 = s1 = s2 = s3 = 0;
926 mutex_unlock(&chip->msg_lock);
930 /* interrupt handling */
931 #define PCX_IRQ_NONE 0
932 #define IRQCS_ACTIVE_PCIDB BIT(13)
933 #define IRQCS_ENABLE_PCIIRQ BIT(8)
934 #define IRQCS_ENABLE_PCIDB BIT(9)
936 static u32 lx_interrupt_test_ack(struct lx6464es *chip)
938 u32 irqcs = lx_plx_reg_read(chip, ePLX_IRQCS);
940 /* Test if PCI Doorbell interrupt is active */
941 if (irqcs & IRQCS_ACTIVE_PCIDB) {
943 irqcs = PCX_IRQ_NONE;
945 while ((temp = lx_plx_reg_read(chip, ePLX_L2PCIDB))) {
948 lx_plx_reg_write(chip, ePLX_L2PCIDB, temp);
956 static int lx_interrupt_ack(struct lx6464es *chip, u32 *r_irqsrc,
957 int *r_async_pending, int *r_async_escmd)
960 u32 irqsrc = lx_interrupt_test_ack(chip);
962 if (irqsrc == PCX_IRQ_NONE)
967 irq_async = irqsrc & MASK_SYS_ASYNC_EVENTS; /* + EtherSound response
968 * (set by xilinx) + EOB */
970 if (irq_async & MASK_SYS_STATUS_ESA) {
971 irq_async &= ~MASK_SYS_STATUS_ESA;
976 /* dev_dbg(chip->card->dev, "interrupt: async event pending\n"); */
977 *r_async_pending = 1;
983 static int lx_interrupt_handle_async_events(struct lx6464es *chip, u32 irqsrc,
985 u64 *r_notified_in_pipe_mask,
986 u64 *r_notified_out_pipe_mask)
989 u32 stat[9]; /* answer from CMD_04_GET_EVENT */
991 /* We can optimize this to not read dumb events.
992 * Answer words are in the following order:
993 * Stat[0] general status
994 * Stat[1] end of buffer OUT pF
995 * Stat[2] end of buffer OUT pf
996 * Stat[3] end of buffer IN pF
997 * Stat[4] end of buffer IN pf
998 * Stat[5] MSB underrun
999 * Stat[6] LSB underrun
1000 * Stat[7] MSB overrun
1001 * Stat[8] LSB overrun
1006 int eb_pending_out = (irqsrc & MASK_SYS_STATUS_EOBO) ? 1 : 0;
1007 int eb_pending_in = (irqsrc & MASK_SYS_STATUS_EOBI) ? 1 : 0;
1009 *r_freq_changed = (irqsrc & MASK_SYS_STATUS_FREQ) ? 1 : 0;
1011 err = lx_dsp_read_async_events(chip, stat);
1015 if (eb_pending_in) {
1016 *r_notified_in_pipe_mask = ((u64)stat[3] << 32)
1018 dev_dbg(chip->card->dev, "interrupt: EOBI pending %llx\n",
1019 *r_notified_in_pipe_mask);
1021 if (eb_pending_out) {
1022 *r_notified_out_pipe_mask = ((u64)stat[1] << 32)
1024 dev_dbg(chip->card->dev, "interrupt: EOBO pending %llx\n",
1025 *r_notified_out_pipe_mask);
1028 orun_mask = ((u64)stat[7] << 32) + stat[8];
1029 urun_mask = ((u64)stat[5] << 32) + stat[6];
1031 /* todo: handle xrun notification */
1036 static int lx_interrupt_request_new_buffer(struct lx6464es *chip,
1037 struct lx_stream *lx_stream)
1039 struct snd_pcm_substream *substream = lx_stream->stream;
1040 const unsigned int is_capture = lx_stream->is_capture;
1043 const u32 channels = substream->runtime->channels;
1044 const u32 bytes_per_frame = channels * 3;
1045 const u32 period_size = substream->runtime->period_size;
1046 const u32 period_bytes = period_size * bytes_per_frame;
1047 const u32 pos = lx_stream->frame_pos;
1048 const u32 next_pos = ((pos+1) == substream->runtime->periods) ?
1051 dma_addr_t buf = substream->dma_buffer.addr + pos * period_bytes;
1054 u32 buffer_index = 0;
1057 u32 size_array[MAX_STREAM_BUFFER];
1059 dev_dbg(chip->card->dev, "->lx_interrupt_request_new_buffer\n");
1061 mutex_lock(&chip->lock);
1063 err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
1064 dev_dbg(chip->card->dev,
1065 "interrupt: needed %d, freed %d\n", needed, freed);
1067 unpack_pointer(buf, &buf_lo, &buf_hi);
1068 err = lx_buffer_give(chip, 0, is_capture, period_bytes, buf_lo, buf_hi,
1070 dev_dbg(chip->card->dev,
1071 "interrupt: gave buffer index %x on 0x%lx (%d bytes)\n",
1072 buffer_index, (unsigned long)buf, period_bytes);
1074 lx_stream->frame_pos = next_pos;
1075 mutex_unlock(&chip->lock);
1080 irqreturn_t lx_interrupt(int irq, void *dev_id)
1082 struct lx6464es *chip = dev_id;
1083 int async_pending, async_escmd;
1085 bool wake_thread = false;
1087 dev_dbg(chip->card->dev,
1088 "**************************************************\n");
1090 if (!lx_interrupt_ack(chip, &irqsrc, &async_pending, &async_escmd)) {
1091 dev_dbg(chip->card->dev, "IRQ_NONE\n");
1092 return IRQ_NONE; /* this device did not cause the interrupt */
1095 if (irqsrc & MASK_SYS_STATUS_CMD_DONE)
1098 if (irqsrc & MASK_SYS_STATUS_EOBI)
1099 dev_dbg(chip->card->dev, "interrupt: EOBI\n");
1101 if (irqsrc & MASK_SYS_STATUS_EOBO)
1102 dev_dbg(chip->card->dev, "interrupt: EOBO\n");
1104 if (irqsrc & MASK_SYS_STATUS_URUN)
1105 dev_dbg(chip->card->dev, "interrupt: URUN\n");
1107 if (irqsrc & MASK_SYS_STATUS_ORUN)
1108 dev_dbg(chip->card->dev, "interrupt: ORUN\n");
1110 if (async_pending) {
1112 chip->irqsrc = irqsrc;
1116 /* backdoor for ethersound commands
1118 * for now, we do not need this
1122 dev_dbg(chip->card->dev, "interrupt requests escmd handling\n");
1125 return wake_thread ? IRQ_WAKE_THREAD : IRQ_HANDLED;
1128 irqreturn_t lx_threaded_irq(int irq, void *dev_id)
1130 struct lx6464es *chip = dev_id;
1131 u64 notified_in_pipe_mask = 0;
1132 u64 notified_out_pipe_mask = 0;
1136 /* handle async events */
1137 err = lx_interrupt_handle_async_events(chip, chip->irqsrc,
1139 ¬ified_in_pipe_mask,
1140 ¬ified_out_pipe_mask);
1142 dev_err(chip->card->dev, "error handling async events\n");
1144 if (notified_in_pipe_mask) {
1145 struct lx_stream *lx_stream = &chip->capture_stream;
1147 dev_dbg(chip->card->dev,
1148 "requesting audio transfer for capture\n");
1149 err = lx_interrupt_request_new_buffer(chip, lx_stream);
1151 dev_err(chip->card->dev,
1152 "cannot request new buffer for capture\n");
1153 snd_pcm_period_elapsed(lx_stream->stream);
1156 if (notified_out_pipe_mask) {
1157 struct lx_stream *lx_stream = &chip->playback_stream;
1159 dev_dbg(chip->card->dev,
1160 "requesting audio transfer for playback\n");
1161 err = lx_interrupt_request_new_buffer(chip, lx_stream);
1163 dev_err(chip->card->dev,
1164 "cannot request new buffer for playback\n");
1165 snd_pcm_period_elapsed(lx_stream->stream);
1172 static void lx_irq_set(struct lx6464es *chip, int enable)
1174 u32 reg = lx_plx_reg_read(chip, ePLX_IRQCS);
1176 /* enable/disable interrupts
1178 * Set the Doorbell and PCI interrupt enable bits
1182 reg |= (IRQCS_ENABLE_PCIIRQ | IRQCS_ENABLE_PCIDB);
1184 reg &= ~(IRQCS_ENABLE_PCIIRQ | IRQCS_ENABLE_PCIDB);
1185 lx_plx_reg_write(chip, ePLX_IRQCS, reg);
1188 void lx_irq_enable(struct lx6464es *chip)
1190 dev_dbg(chip->card->dev, "->lx_irq_enable\n");
1191 lx_irq_set(chip, 1);
1194 void lx_irq_disable(struct lx6464es *chip)
1196 dev_dbg(chip->card->dev, "->lx_irq_disable\n");
1197 lx_irq_set(chip, 0);