2 * Intel Baytrail SST IPC Support
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/wait.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/export.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/firmware.h>
28 #include <asm/div64.h>
30 #include "sst-baytrail-ipc.h"
31 #include "../common/sst-dsp.h"
32 #include "../common/sst-dsp-priv.h"
33 #include "../common/sst-ipc.h"
35 /* IPC message timeout */
36 #define IPC_TIMEOUT_MSECS 300
37 #define IPC_BOOT_MSECS 200
39 #define IPC_EMPTY_LIST_SIZE 8
42 #define IPC_HEADER_MSG_ID_MASK 0xff
43 #define IPC_HEADER_MSG_ID(x) ((x) & IPC_HEADER_MSG_ID_MASK)
44 #define IPC_HEADER_STR_ID_SHIFT 8
45 #define IPC_HEADER_STR_ID_MASK 0x1f
46 #define IPC_HEADER_STR_ID(x) (((x) & 0x1f) << IPC_HEADER_STR_ID_SHIFT)
47 #define IPC_HEADER_LARGE_SHIFT 13
48 #define IPC_HEADER_LARGE(x) (((x) & 0x1) << IPC_HEADER_LARGE_SHIFT)
49 #define IPC_HEADER_DATA_SHIFT 16
50 #define IPC_HEADER_DATA_MASK 0x3fff
51 #define IPC_HEADER_DATA(x) (((x) & 0x3fff) << IPC_HEADER_DATA_SHIFT)
53 /* mask for differentiating between notification and reply message */
54 #define IPC_NOTIFICATION (0x1 << 7)
56 /* I2L Stream config/control msgs */
57 #define IPC_IA_ALLOC_STREAM 0x20
58 #define IPC_IA_FREE_STREAM 0x21
59 #define IPC_IA_PAUSE_STREAM 0x24
60 #define IPC_IA_RESUME_STREAM 0x25
61 #define IPC_IA_DROP_STREAM 0x26
62 #define IPC_IA_START_STREAM 0x30
64 /* notification messages */
65 #define IPC_IA_FW_INIT_CMPLT 0x81
66 #define IPC_SST_PERIOD_ELAPSED 0x97
68 /* IPC messages between host and ADSP */
69 struct sst_byt_address_info
{
74 struct sst_byt_str_type
{
84 struct sst_byt_pcm_params
{
93 struct sst_byt_frames_info
{
97 struct sst_byt_address_info ring_buf_info
[8];
100 struct sst_byt_alloc_params
{
101 struct sst_byt_str_type str_type
;
102 struct sst_byt_pcm_params pcm_params
;
103 struct sst_byt_frames_info frame_info
;
106 struct sst_byt_alloc_response
{
107 struct sst_byt_str_type str_type
;
111 struct sst_byt_start_stream_params
{
115 struct sst_byt_tstamp
{
116 u64 ring_buffer_counter
;
117 u64 hardware_counter
;
121 u32 sampling_frequency
;
125 struct sst_byt_fw_version
{
132 struct sst_byt_fw_build_info
{
137 struct sst_byt_fw_init
{
138 struct sst_byt_fw_version fw_version
;
139 struct sst_byt_fw_build_info build_info
;
145 struct sst_byt_stream
;
148 /* stream infomation */
149 struct sst_byt_stream
{
150 struct list_head node
;
153 struct sst_byt_alloc_params request
;
154 struct sst_byt_alloc_response reply
;
162 /* driver callback */
163 u32 (*notify_position
)(struct sst_byt_stream
*stream
, void *data
);
167 /* SST Baytrail IPC data */
173 struct list_head stream_list
;
176 wait_queue_head_t boot_wait
;
181 struct sst_generic_ipc ipc
;
184 static inline u64
sst_byt_header(int msg_id
, int data
, bool large
, int str_id
)
186 return IPC_HEADER_MSG_ID(msg_id
) | IPC_HEADER_STR_ID(str_id
) |
187 IPC_HEADER_LARGE(large
) | IPC_HEADER_DATA(data
) |
191 static inline u16
sst_byt_header_msg_id(u64 header
)
193 return header
& IPC_HEADER_MSG_ID_MASK
;
196 static inline u8
sst_byt_header_str_id(u64 header
)
198 return (header
>> IPC_HEADER_STR_ID_SHIFT
) & IPC_HEADER_STR_ID_MASK
;
201 static inline u16
sst_byt_header_data(u64 header
)
203 return (header
>> IPC_HEADER_DATA_SHIFT
) & IPC_HEADER_DATA_MASK
;
206 static struct sst_byt_stream
*sst_byt_get_stream(struct sst_byt
*byt
,
209 struct sst_byt_stream
*stream
;
211 list_for_each_entry(stream
, &byt
->stream_list
, node
) {
212 if (stream
->str_id
== stream_id
)
219 static void sst_byt_stream_update(struct sst_byt
*byt
, struct ipc_message
*msg
)
221 struct sst_byt_stream
*stream
;
222 u64 header
= msg
->header
;
223 u8 stream_id
= sst_byt_header_str_id(header
);
224 u8 stream_msg
= sst_byt_header_msg_id(header
);
226 stream
= sst_byt_get_stream(byt
, stream_id
);
230 switch (stream_msg
) {
231 case IPC_IA_DROP_STREAM
:
232 case IPC_IA_PAUSE_STREAM
:
233 case IPC_IA_FREE_STREAM
:
234 stream
->running
= false;
236 case IPC_IA_START_STREAM
:
237 case IPC_IA_RESUME_STREAM
:
238 stream
->running
= true;
243 static int sst_byt_process_reply(struct sst_byt
*byt
, u64 header
)
245 struct ipc_message
*msg
;
247 msg
= sst_ipc_reply_find_msg(&byt
->ipc
, header
);
251 if (header
& IPC_HEADER_LARGE(true)) {
252 msg
->rx_size
= sst_byt_header_data(header
);
253 sst_dsp_inbox_read(byt
->dsp
, msg
->rx_data
, msg
->rx_size
);
256 /* update any stream states */
257 sst_byt_stream_update(byt
, msg
);
259 list_del(&msg
->list
);
261 sst_ipc_tx_msg_reply_complete(&byt
->ipc
, msg
);
266 static void sst_byt_fw_ready(struct sst_byt
*byt
, u64 header
)
268 dev_dbg(byt
->dev
, "ipc: DSP is ready 0x%llX\n", header
);
270 byt
->boot_complete
= true;
271 wake_up(&byt
->boot_wait
);
274 static int sst_byt_process_notification(struct sst_byt
*byt
,
275 unsigned long *flags
)
277 struct sst_dsp
*sst
= byt
->dsp
;
278 struct sst_byt_stream
*stream
;
280 u8 msg_id
, stream_id
;
283 header
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
284 msg_id
= sst_byt_header_msg_id(header
);
287 case IPC_SST_PERIOD_ELAPSED
:
288 stream_id
= sst_byt_header_str_id(header
);
289 stream
= sst_byt_get_stream(byt
, stream_id
);
290 if (stream
&& stream
->running
&& stream
->notify_position
) {
291 spin_unlock_irqrestore(&sst
->spinlock
, *flags
);
292 stream
->notify_position(stream
, stream
->pdata
);
293 spin_lock_irqsave(&sst
->spinlock
, *flags
);
296 case IPC_IA_FW_INIT_CMPLT
:
297 sst_byt_fw_ready(byt
, header
);
304 static irqreturn_t
sst_byt_irq_thread(int irq
, void *context
)
306 struct sst_dsp
*sst
= (struct sst_dsp
*) context
;
307 struct sst_byt
*byt
= sst_dsp_get_thread_context(sst
);
308 struct sst_generic_ipc
*ipc
= &byt
->ipc
;
312 spin_lock_irqsave(&sst
->spinlock
, flags
);
314 header
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
315 if (header
& SST_BYT_IPCD_BUSY
) {
316 if (header
& IPC_NOTIFICATION
) {
317 /* message from ADSP */
318 sst_byt_process_notification(byt
, &flags
);
320 /* reply from ADSP */
321 sst_byt_process_reply(byt
, header
);
324 * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
325 * processed the message and can accept new. Clear data part
328 sst_dsp_shim_update_bits64_unlocked(sst
, SST_IPCD
,
329 SST_BYT_IPCD_DONE
| SST_BYT_IPCD_BUSY
|
330 IPC_HEADER_DATA(IPC_HEADER_DATA_MASK
),
332 /* unmask message request interrupts */
333 sst_dsp_shim_update_bits64_unlocked(sst
, SST_IMRX
,
334 SST_BYT_IMRX_REQUEST
, 0);
337 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
339 /* continue to send any remaining messages... */
340 schedule_work(&ipc
->kwork
);
346 struct sst_byt_stream
*sst_byt_stream_new(struct sst_byt
*byt
, int id
,
347 u32 (*notify_position
)(struct sst_byt_stream
*stream
, void *data
),
350 struct sst_byt_stream
*stream
;
351 struct sst_dsp
*sst
= byt
->dsp
;
354 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
358 spin_lock_irqsave(&sst
->spinlock
, flags
);
359 list_add(&stream
->node
, &byt
->stream_list
);
360 stream
->notify_position
= notify_position
;
361 stream
->pdata
= data
;
364 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
369 int sst_byt_stream_set_bits(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
372 stream
->request
.pcm_params
.pcm_wd_sz
= bits
;
376 int sst_byt_stream_set_channels(struct sst_byt
*byt
,
377 struct sst_byt_stream
*stream
, u8 channels
)
379 stream
->request
.pcm_params
.num_chan
= channels
;
383 int sst_byt_stream_set_rate(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
386 stream
->request
.pcm_params
.sfreq
= rate
;
390 /* stream sonfiguration */
391 int sst_byt_stream_type(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
392 int codec_type
, int stream_type
, int operation
)
394 stream
->request
.str_type
.codec_type
= codec_type
;
395 stream
->request
.str_type
.str_type
= stream_type
;
396 stream
->request
.str_type
.operation
= operation
;
397 stream
->request
.str_type
.time_slots
= 0xc;
402 int sst_byt_stream_buffer(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
403 uint32_t buffer_addr
, uint32_t buffer_size
)
405 stream
->request
.frame_info
.num_entries
= 1;
406 stream
->request
.frame_info
.ring_buf_info
[0].addr
= buffer_addr
;
407 stream
->request
.frame_info
.ring_buf_info
[0].size
= buffer_size
;
408 /* calculate bytes per 4 ms fragment */
409 stream
->request
.frame_info
.frag_size
=
410 stream
->request
.pcm_params
.sfreq
*
411 stream
->request
.pcm_params
.num_chan
*
412 stream
->request
.pcm_params
.pcm_wd_sz
/ 8 *
417 int sst_byt_stream_commit(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
419 struct sst_byt_alloc_params
*str_req
= &stream
->request
;
420 struct sst_byt_alloc_response
*reply
= &stream
->reply
;
424 header
= sst_byt_header(IPC_IA_ALLOC_STREAM
,
425 sizeof(*str_req
) + sizeof(u32
),
426 true, stream
->str_id
);
427 ret
= sst_ipc_tx_message_wait(&byt
->ipc
, header
, str_req
,
429 reply
, sizeof(*reply
));
431 dev_err(byt
->dev
, "ipc: error stream commit failed\n");
435 stream
->commited
= true;
440 int sst_byt_stream_free(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
444 struct sst_dsp
*sst
= byt
->dsp
;
447 if (!stream
->commited
)
450 header
= sst_byt_header(IPC_IA_FREE_STREAM
, 0, false, stream
->str_id
);
451 ret
= sst_ipc_tx_message_wait(&byt
->ipc
, header
, NULL
, 0, NULL
, 0);
453 dev_err(byt
->dev
, "ipc: free stream %d failed\n",
458 stream
->commited
= false;
460 spin_lock_irqsave(&sst
->spinlock
, flags
);
461 list_del(&stream
->node
);
463 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
468 static int sst_byt_stream_operations(struct sst_byt
*byt
, int type
,
469 int stream_id
, int wait
)
473 header
= sst_byt_header(type
, 0, false, stream_id
);
475 return sst_ipc_tx_message_wait(&byt
->ipc
, header
, NULL
,
478 return sst_ipc_tx_message_nowait(&byt
->ipc
, header
,
482 /* stream ALSA trigger operations */
483 int sst_byt_stream_start(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
486 struct sst_byt_start_stream_params start_stream
;
492 start_stream
.byte_offset
= start_offset
;
493 header
= sst_byt_header(IPC_IA_START_STREAM
,
494 sizeof(start_stream
) + sizeof(u32
),
495 true, stream
->str_id
);
496 tx_msg
= &start_stream
;
497 size
= sizeof(start_stream
);
499 ret
= sst_ipc_tx_message_nowait(&byt
->ipc
, header
, tx_msg
, size
);
501 dev_err(byt
->dev
, "ipc: error failed to start stream %d\n",
507 int sst_byt_stream_stop(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
511 /* don't stop streams that are not commited */
512 if (!stream
->commited
)
515 ret
= sst_byt_stream_operations(byt
, IPC_IA_DROP_STREAM
,
518 dev_err(byt
->dev
, "ipc: error failed to stop stream %d\n",
523 int sst_byt_stream_pause(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
527 ret
= sst_byt_stream_operations(byt
, IPC_IA_PAUSE_STREAM
,
530 dev_err(byt
->dev
, "ipc: error failed to pause stream %d\n",
536 int sst_byt_stream_resume(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
540 ret
= sst_byt_stream_operations(byt
, IPC_IA_RESUME_STREAM
,
543 dev_err(byt
->dev
, "ipc: error failed to resume stream %d\n",
549 int sst_byt_get_dsp_position(struct sst_byt
*byt
,
550 struct sst_byt_stream
*stream
, int buffer_size
)
552 struct sst_dsp
*sst
= byt
->dsp
;
553 struct sst_byt_tstamp fw_tstamp
;
554 u8 str_id
= stream
->str_id
;
557 tstamp_offset
= SST_BYT_TIMESTAMP_OFFSET
+ str_id
* sizeof(fw_tstamp
);
558 memcpy_fromio(&fw_tstamp
,
559 sst
->addr
.lpe
+ tstamp_offset
, sizeof(fw_tstamp
));
561 return do_div(fw_tstamp
.ring_buffer_counter
, buffer_size
);
564 struct sst_dsp
*sst_byt_get_dsp(struct sst_byt
*byt
)
569 static struct sst_dsp_device byt_dev
= {
570 .thread
= sst_byt_irq_thread
,
574 int sst_byt_dsp_suspend_late(struct device
*dev
, struct sst_pdata
*pdata
)
576 struct sst_byt
*byt
= pdata
->dsp
;
578 dev_dbg(byt
->dev
, "dsp reset\n");
579 sst_dsp_reset(byt
->dsp
);
580 sst_ipc_drop_all(&byt
->ipc
);
581 dev_dbg(byt
->dev
, "dsp in reset\n");
583 dev_dbg(byt
->dev
, "free all blocks and unload fw\n");
584 sst_fw_unload(byt
->fw
);
588 EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late
);
590 int sst_byt_dsp_boot(struct device
*dev
, struct sst_pdata
*pdata
)
592 struct sst_byt
*byt
= pdata
->dsp
;
595 dev_dbg(byt
->dev
, "reload dsp fw\n");
597 sst_dsp_reset(byt
->dsp
);
599 ret
= sst_fw_reload(byt
->fw
);
601 dev_err(dev
, "error: failed to reload firmware\n");
605 /* wait for DSP boot completion */
606 byt
->boot_complete
= false;
607 sst_dsp_boot(byt
->dsp
);
608 dev_dbg(byt
->dev
, "dsp booting...\n");
612 EXPORT_SYMBOL_GPL(sst_byt_dsp_boot
);
614 int sst_byt_dsp_wait_for_ready(struct device
*dev
, struct sst_pdata
*pdata
)
616 struct sst_byt
*byt
= pdata
->dsp
;
619 dev_dbg(byt
->dev
, "wait for dsp reboot\n");
621 err
= wait_event_timeout(byt
->boot_wait
, byt
->boot_complete
,
622 msecs_to_jiffies(IPC_BOOT_MSECS
));
624 dev_err(byt
->dev
, "ipc: error DSP boot timeout\n");
628 dev_dbg(byt
->dev
, "dsp rebooted\n");
631 EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready
);
633 static void byt_tx_msg(struct sst_generic_ipc
*ipc
, struct ipc_message
*msg
)
635 if (msg
->header
& IPC_HEADER_LARGE(true))
636 sst_dsp_outbox_write(ipc
->dsp
, msg
->tx_data
, msg
->tx_size
);
638 sst_dsp_shim_write64_unlocked(ipc
->dsp
, SST_IPCX
, msg
->header
);
641 static void byt_shim_dbg(struct sst_generic_ipc
*ipc
, const char *text
)
643 struct sst_dsp
*sst
= ipc
->dsp
;
644 u64 isr
, ipcd
, imrx
, ipcx
;
646 ipcx
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCX
);
647 isr
= sst_dsp_shim_read64_unlocked(sst
, SST_ISRX
);
648 ipcd
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
649 imrx
= sst_dsp_shim_read64_unlocked(sst
, SST_IMRX
);
652 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
653 text
, ipcx
, isr
, ipcd
, imrx
);
656 static void byt_tx_data_copy(struct ipc_message
*msg
, char *tx_data
,
659 /* msg content = lower 32-bit of the header + data */
660 *(u32
*)msg
->tx_data
= (u32
)(msg
->header
& (u32
)-1);
661 memcpy(msg
->tx_data
+ sizeof(u32
), tx_data
, tx_size
);
662 msg
->tx_size
+= sizeof(u32
);
665 static u64
byt_reply_msg_match(u64 header
, u64
*mask
)
667 /* match reply to message sent based on msg and stream IDs */
668 *mask
= IPC_HEADER_MSG_ID_MASK
|
669 IPC_HEADER_STR_ID_MASK
<< IPC_HEADER_STR_ID_SHIFT
;
675 static bool byt_is_dsp_busy(struct sst_dsp
*dsp
)
679 ipcx
= sst_dsp_shim_read_unlocked(dsp
, SST_IPCX
);
680 return (ipcx
& (SST_IPCX_BUSY
| SST_IPCX_DONE
));
683 int sst_byt_dsp_init(struct device
*dev
, struct sst_pdata
*pdata
)
686 struct sst_generic_ipc
*ipc
;
687 struct sst_fw
*byt_sst_fw
;
688 struct sst_byt_fw_init init
;
691 dev_dbg(dev
, "initialising Byt DSP IPC\n");
693 byt
= devm_kzalloc(dev
, sizeof(*byt
), GFP_KERNEL
);
701 ipc
->ops
.tx_msg
= byt_tx_msg
;
702 ipc
->ops
.shim_dbg
= byt_shim_dbg
;
703 ipc
->ops
.tx_data_copy
= byt_tx_data_copy
;
704 ipc
->ops
.reply_msg_match
= byt_reply_msg_match
;
705 ipc
->ops
.is_dsp_busy
= byt_is_dsp_busy
;
706 ipc
->tx_data_max_size
= IPC_MAX_MAILBOX_BYTES
;
707 ipc
->rx_data_max_size
= IPC_MAX_MAILBOX_BYTES
;
709 err
= sst_ipc_init(ipc
);
713 INIT_LIST_HEAD(&byt
->stream_list
);
714 init_waitqueue_head(&byt
->boot_wait
);
715 byt_dev
.thread_context
= byt
;
718 byt
->dsp
= sst_dsp_new(dev
, &byt_dev
, pdata
);
719 if (byt
->dsp
== NULL
) {
726 /* keep the DSP in reset state for base FW loading */
727 sst_dsp_reset(byt
->dsp
);
729 byt_sst_fw
= sst_fw_new(byt
->dsp
, pdata
->fw
, byt
);
730 if (byt_sst_fw
== NULL
) {
732 dev_err(dev
, "error: failed to load firmware\n");
736 /* wait for DSP boot completion */
737 sst_dsp_boot(byt
->dsp
);
738 err
= wait_event_timeout(byt
->boot_wait
, byt
->boot_complete
,
739 msecs_to_jiffies(IPC_BOOT_MSECS
));
742 dev_err(byt
->dev
, "ipc: error DSP boot timeout\n");
746 /* show firmware information */
747 sst_dsp_inbox_read(byt
->dsp
, &init
, sizeof(init
));
748 dev_info(byt
->dev
, "FW version: %02x.%02x.%02x.%02x\n",
749 init
.fw_version
.major
, init
.fw_version
.minor
,
750 init
.fw_version
.build
, init
.fw_version
.type
);
751 dev_info(byt
->dev
, "Build type: %x\n", init
.fw_version
.type
);
752 dev_info(byt
->dev
, "Build date: %s %s\n",
753 init
.build_info
.date
, init
.build_info
.time
);
756 byt
->fw
= byt_sst_fw
;
761 sst_dsp_reset(byt
->dsp
);
762 sst_fw_free(byt_sst_fw
);
764 sst_dsp_free(byt
->dsp
);
771 EXPORT_SYMBOL_GPL(sst_byt_dsp_init
);
773 void sst_byt_dsp_free(struct device
*dev
, struct sst_pdata
*pdata
)
775 struct sst_byt
*byt
= pdata
->dsp
;
777 sst_dsp_reset(byt
->dsp
);
778 sst_fw_free_all(byt
->dsp
);
779 sst_dsp_free(byt
->dsp
);
780 sst_ipc_fini(&byt
->ipc
);
782 EXPORT_SYMBOL_GPL(sst_byt_dsp_free
);