2 * This file is part of OMAP DSP driver (DSP Gateway version 3.3.1)
4 * Copyright (C) 2002-2006 Nokia Corporation. All rights reserved.
6 * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/sched.h>
28 #include <linux/mutex.h>
29 #include <linux/err.h>
30 #include <linux/clk.h>
31 #include <asm/delay.h>
32 #include <asm/arch/mailbox.h>
33 #include <asm/arch/dsp_common.h>
34 #include "dsp_mbcmd.h"
37 #include "dsp_common.h"
39 MODULE_AUTHOR("Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>");
40 MODULE_DESCRIPTION("OMAP DSP driver module");
41 MODULE_LICENSE("GPL");
43 struct omap_dsp *omap_dsp;
44 static struct sync_seq *mbseq;
45 static u16 mbseq_expect_tmp;
46 static u16 *mbseq_expect = &mbseq_expect_tmp;
51 extern void mbox_wdsnd(struct mbcmd *mb);
52 extern void mbox_wdreq(struct mbcmd *mb);
53 extern void mbox_bksnd(struct mbcmd *mb);
54 extern void mbox_bkreq(struct mbcmd *mb);
55 extern void mbox_bkyld(struct mbcmd *mb);
56 extern void mbox_bksndp(struct mbcmd *mb);
57 extern void mbox_bkreqp(struct mbcmd *mb);
58 extern void mbox_tctl(struct mbcmd *mb);
59 extern void mbox_poll(struct mbcmd *mb);
60 #ifdef OLD_BINARY_SUPPORT
62 extern void mbox_wdt(struct mbcmd *mb);
64 extern void mbox_suspend(struct mbcmd *mb);
65 static void mbox_kfunc(struct mbcmd *mb);
66 extern void mbox_tcfg(struct mbcmd *mb);
67 extern void mbox_tadd(struct mbcmd *mb);
68 extern void mbox_tdel(struct mbcmd *mb);
69 extern void mbox_dspcfg(struct mbcmd *mb);
70 extern void mbox_regrw(struct mbcmd *mb);
71 extern void mbox_getvar(struct mbcmd *mb);
72 extern void mbox_err(struct mbcmd *mb);
73 extern void mbox_dbg(struct mbcmd *mb);
75 static const struct cmdinfo
76 cif_wdsnd = { "WDSND", CMD_L_TYPE_TID, mbox_wdsnd },
77 cif_wdreq = { "WDREQ", CMD_L_TYPE_TID, mbox_wdreq },
78 cif_bksnd = { "BKSND", CMD_L_TYPE_TID, mbox_bksnd },
79 cif_bkreq = { "BKREQ", CMD_L_TYPE_TID, mbox_bkreq },
80 cif_bkyld = { "BKYLD", CMD_L_TYPE_NULL, mbox_bkyld },
81 cif_bksndp = { "BKSNDP", CMD_L_TYPE_TID, mbox_bksndp },
82 cif_bkreqp = { "BKREQP", CMD_L_TYPE_TID, mbox_bkreqp },
83 cif_tctl = { "TCTL", CMD_L_TYPE_TID, mbox_tctl },
84 cif_poll = { "POLL", CMD_L_TYPE_NULL, mbox_poll },
85 #ifdef OLD_BINARY_SUPPORT
87 cif_wdt = { "WDT", CMD_L_TYPE_NULL, mbox_wdt },
89 cif_runlevel = { "RUNLEVEL", CMD_L_TYPE_SUBCMD, NULL },
90 cif_pm = { "PM", CMD_L_TYPE_SUBCMD, NULL },
91 cif_suspend = { "SUSPEND", CMD_L_TYPE_NULL, mbox_suspend },
92 cif_kfunc = { "KFUNC", CMD_L_TYPE_SUBCMD, mbox_kfunc },
93 cif_tcfg = { "TCFG", CMD_L_TYPE_TID, mbox_tcfg },
94 cif_tadd = { "TADD", CMD_L_TYPE_TID, mbox_tadd },
95 cif_tdel = { "TDEL", CMD_L_TYPE_TID, mbox_tdel },
96 cif_tstop = { "TSTOP", CMD_L_TYPE_TID, NULL },
97 cif_dspcfg = { "DSPCFG", CMD_L_TYPE_SUBCMD, mbox_dspcfg },
98 cif_regrw = { "REGRW", CMD_L_TYPE_SUBCMD, mbox_regrw },
99 cif_getvar = { "GETVAR", CMD_L_TYPE_SUBCMD, mbox_getvar },
100 cif_setvar = { "SETVAR", CMD_L_TYPE_SUBCMD, NULL },
101 cif_err = { "ERR", CMD_L_TYPE_SUBCMD, mbox_err },
102 cif_dbg = { "DBG", CMD_L_TYPE_NULL, mbox_dbg };
104 #define MBOX_CMD_MAX 0x80
105 const struct cmdinfo *cmdinfo[MBOX_CMD_MAX] = {
106 [MBOX_CMD_DSP_WDSND] = &cif_wdsnd,
107 [MBOX_CMD_DSP_WDREQ] = &cif_wdreq,
108 [MBOX_CMD_DSP_BKSND] = &cif_bksnd,
109 [MBOX_CMD_DSP_BKREQ] = &cif_bkreq,
110 [MBOX_CMD_DSP_BKYLD] = &cif_bkyld,
111 [MBOX_CMD_DSP_BKSNDP] = &cif_bksndp,
112 [MBOX_CMD_DSP_BKREQP] = &cif_bkreqp,
113 [MBOX_CMD_DSP_TCTL] = &cif_tctl,
114 [MBOX_CMD_DSP_POLL] = &cif_poll,
115 #ifdef OLD_BINARY_SUPPORT
116 [MBOX_CMD_DSP_WDT] = &cif_wdt, /* v3.3 obsolete */
118 [MBOX_CMD_DSP_RUNLEVEL] = &cif_runlevel,
119 [MBOX_CMD_DSP_PM] = &cif_pm,
120 [MBOX_CMD_DSP_SUSPEND] = &cif_suspend,
121 [MBOX_CMD_DSP_KFUNC] = &cif_kfunc,
122 [MBOX_CMD_DSP_TCFG] = &cif_tcfg,
123 [MBOX_CMD_DSP_TADD] = &cif_tadd,
124 [MBOX_CMD_DSP_TDEL] = &cif_tdel,
125 [MBOX_CMD_DSP_TSTOP] = &cif_tstop,
126 [MBOX_CMD_DSP_DSPCFG] = &cif_dspcfg,
127 [MBOX_CMD_DSP_REGRW] = &cif_regrw,
128 [MBOX_CMD_DSP_GETVAR] = &cif_getvar,
129 [MBOX_CMD_DSP_SETVAR] = &cif_setvar,
130 [MBOX_CMD_DSP_ERR] = &cif_err,
131 [MBOX_CMD_DSP_DBG] = &cif_dbg,
134 static int dsp_kfunc_probe_devices(struct omap_dsp *dsp)
136 struct dsp_kfunc_device *p;
139 mutex_lock(&dsp->lock);
140 list_for_each_entry(p, dsp->kdev_list, entry) {
141 if (p->probe == NULL)
146 "probing %s failed\n", p->name);
150 mutex_unlock(&dsp->lock);
152 pr_debug("%s() fail:%d\n", __FUNCTION__, fail);
157 static int dsp_kfunc_remove_devices(struct omap_dsp *dsp)
159 struct dsp_kfunc_device *p;
162 mutex_lock(&dsp->lock);
163 list_for_each_entry_reverse(p, dsp->kdev_list, entry) {
164 if (p->remove == NULL)
169 "removing %s failed\n", p->name);
173 mutex_unlock(&dsp->lock);
175 pr_debug("%s() fail:%d\n", __FUNCTION__, fail);
180 static int dsp_kfunc_enable_devices(struct omap_dsp *dsp, int type, int stage)
182 struct dsp_kfunc_device *p;
185 mutex_lock(&dsp->lock);
186 list_for_each_entry(p, dsp->kdev_list, entry) {
187 if ((p->type != type) || (p->enable == NULL))
189 ret = p->enable(p, stage);
192 "enabling %s failed\n", p->name);
196 mutex_unlock(&dsp->lock);
198 pr_debug("%s(%d) fail:%d\n", __FUNCTION__, type, fail);
203 static int dsp_kfunc_disable_devices(struct omap_dsp *dsp, int type, int stage)
205 struct dsp_kfunc_device *p;
208 mutex_lock(&dsp->lock);
209 list_for_each_entry_reverse(p, omap_dsp->kdev_list, entry) {
210 if ((p->type != type) || (p->disable == NULL))
212 ret = p->disable(p, stage);
215 "disabling %s failed\n", p->name);
219 mutex_unlock(&dsp->lock);
221 pr_debug("%s(%d) fail:%d\n", __FUNCTION__, type, fail);
226 int sync_with_dsp(u16 *adr, u16 val, int try_cnt)
230 if (*(volatile u16 *)adr == val)
233 for (try = 0; try < try_cnt; try++) {
235 if (*(volatile u16 *)adr == val) {
237 pr_info("omapdsp: sync_with_dsp(): try = %d\n", try);
246 static int mbcmd_sender_prepare(void *data)
248 struct mb_exarg *arg = data;
251 * even if ipbuf_sys_ad is in DSP internal memory,
252 * dsp_mem_enable() never cause to call PM mailbox command
253 * because in that case DSP memory should be always enabled.
254 * (see ipbuf_sys_hold_mem_active in ipbuf.c)
256 * Therefore, we can call this function here safely.
258 if (sync_with_dsp(&ipbuf_sys_ad->s, TID_FREE, 10) < 0) {
259 printk(KERN_ERR "omapdsp: ipbuf_sys_ad is busy.\n");
264 for (i = 0; i < arg->argc; i++) {
265 ipbuf_sys_ad->d[i] = arg->argv[i];
267 ipbuf_sys_ad->s = arg->tid;
273 * __dsp_mbcmd_send_exarg(): mailbox dispatcher
275 int __dsp_mbcmd_send_exarg(struct mbcmd *mb, struct mb_exarg *arg,
280 if (unlikely(omap_dsp->enabled == 0)) {
281 ret = dsp_kfunc_enable_devices(omap_dsp,
282 DSP_KFUNC_DEV_TYPE_COMMON, 0);
284 omap_dsp->enabled = 1;
288 * while MMU fault is set,
289 * only recovery command can be executed
291 if (dsp_err_isset(ERRCODE_MMU) && !recovery_flag) {
293 "mbox: mmu interrupt is set. %s is aborting.\n",
299 dsp_mem_enable(ipbuf_sys_ad);
301 ret = omap_mbox_msg_send(omap_dsp->mbox,
302 *(mbox_msg_t *)mb, (void*)arg);
309 mblog_add(mb, DIR_A2D);
312 dsp_mem_disable(ipbuf_sys_ad);
317 int dsp_mbcmd_send_and_wait_exarg(struct mbcmd *mb, struct mb_exarg *arg,
318 wait_queue_head_t *q)
321 DECLARE_WAITQUEUE(wait, current);
323 add_wait_queue(q, &wait);
324 current_state = current->state;
325 set_current_state(TASK_INTERRUPTIBLE);
326 if (dsp_mbcmd_send_exarg(mb, arg) < 0) {
327 set_current_state(current_state);
328 remove_wait_queue(q, &wait);
331 schedule_timeout(DSP_TIMEOUT);
332 set_current_state(current_state);
333 remove_wait_queue(q, &wait);
341 static void mbcmd_receiver(mbox_msg_t msg)
343 struct mbcmd *mb = (struct mbcmd *)&msg;
345 if (cmdinfo[mb->cmd_h] == NULL) {
347 "invalid message (%08x) for mbcmd_receiver().\n", msg);
353 mblog_add(mb, DIR_D2A);
355 /* call handler for the command */
356 if (cmdinfo[mb->cmd_h]->handler)
357 cmdinfo[mb->cmd_h]->handler(mb);
359 printk(KERN_ERR "mbox: %s is not allowed from DSP.\n",
363 static int mbsync_hold_mem_active;
365 void dsp_mbox_start(void)
367 omap_mbox_init_seq(omap_dsp->mbox);
368 mbseq_expect_tmp = 0;
371 void dsp_mbox_stop(void)
374 mbseq_expect = &mbseq_expect_tmp;
377 int dsp_mbox_config(void *p)
381 if (dsp_address_validate(p, sizeof(struct sync_seq), "mbseq") < 0)
383 if (dsp_mem_type(p, sizeof(struct sync_seq)) != MEM_TYPE_EXTERN) {
385 "omapdsp: mbseq is placed in DSP internal memory.\n"
386 " It will prevent DSP from idling.\n");
387 mbsync_hold_mem_active = 1;
389 * dsp_mem_enable() never fails because
390 * it has been already enabled in dspcfg process and
391 * this will just increment the usecount.
393 dsp_mem_enable((void *)daram_base);
396 local_irq_save(flags);
398 mbseq->da_arm = mbseq_expect_tmp;
399 mbseq_expect = &mbseq->da_arm;
400 local_irq_restore(flags);
405 static int __init dsp_mbox_init(void)
407 omap_dsp->mbox = omap_mbox_get("dsp");
408 if (IS_ERR(omap_dsp->mbox)) {
409 printk(KERN_ERR "failed to get mailbox handler for DSP.\n");
413 omap_dsp->mbox->msg_receive_cb = mbcmd_receiver;
414 omap_dsp->mbox->msg_sender_cb = mbcmd_sender_prepare;
419 static void dsp_mbox_exit(void)
421 omap_dsp->mbox->msg_sender_cb = NULL;
422 omap_dsp->mbox->msg_receive_cb = NULL;
424 if (mbsync_hold_mem_active) {
425 dsp_mem_disable((void *)daram_base);
426 mbsync_hold_mem_active = 0;
431 * kernel function dispatcher
433 extern void mbox_fbctl_upd(void);
434 extern void mbox_fbctl_disable(struct mbcmd *mb);
436 static void mbox_kfunc_fbctl(struct mbcmd *mb)
443 mbox_fbctl_disable(mb);
447 "mbox: Unknown FBCTL from DSP: 0x%04x\n", mb->data);
452 * dspgw: KFUNC message handler
454 static void mbox_kfunc_power(unsigned short data)
459 case DVFS_START: /* ACK from DSP */
463 ret = dsp_kfunc_enable_devices(omap_dsp,
464 DSP_KFUNC_DEV_TYPE_AUDIO, 0);
468 case AUDIO_PWR_DOWN: /* == AUDIO_PWR_DOWN1 */
469 ret = dsp_kfunc_disable_devices(omap_dsp,
470 DSP_KFUNC_DEV_TYPE_AUDIO, 1);
472 case AUDIO_PWR_DOWN2:
473 ret = dsp_kfunc_disable_devices(omap_dsp,
474 DSP_KFUNC_DEV_TYPE_AUDIO, 2);
477 ret = dsp_kfunc_disable_devices(omap_dsp,
478 DSP_KFUNC_DEV_TYPE_COMMON, 0);
480 omap_dsp->enabled = 0;
484 "mailbox: Unknown PWR from DSP: 0x%04x\n", data);
488 if (unlikely(ret < 0)) {
489 printk(KERN_ERR "mailbox: PWR(0x%04x) failed\n", data);
493 if (likely(ret == 0))
496 mbcompose_send(KFUNC, KFUNC_POWER, data);
499 static void mbox_kfunc(struct mbcmd *mb)
503 mbox_kfunc_fbctl(mb);
506 mbox_kfunc_power(mb->data);
510 "mbox: Unknown KFUNC from DSP: 0x%02x\n", mb->cmd_l);
514 extern int dsp_ctl_core_init(void);
515 extern void dsp_ctl_core_exit(void);
516 extern void dsp_ctl_init(void);
517 extern void dsp_ctl_exit(void);
518 extern int dsp_mem_init(void);
519 extern void dsp_mem_exit(void);
520 extern void mblog_init(void);
521 extern void mblog_exit(void);
522 extern int dsp_taskmod_init(void);
523 extern void dsp_taskmod_exit(void);
528 static int __init dsp_drv_probe(struct platform_device *pdev)
531 struct omap_dsp *info;
532 struct dsp_platform_data *pdata = pdev->dev.platform_data;
534 dev_info(&pdev->dev, "OMAP DSP driver initialization\n");
536 info = kzalloc(sizeof(struct omap_dsp), GFP_KERNEL);
537 if (unlikely(info == NULL)) {
538 dev_dbg(&pdev->dev, "no memory for info\n");
541 platform_set_drvdata(pdev, info);
544 mutex_init(&info->lock);
545 info->dev = &pdev->dev;
546 info->kdev_list = &pdata->kdev_list;
548 ret = dsp_kfunc_probe_devices(info);
554 info->mmu_irq = platform_get_irq_byname(pdev, "dsp_mmu");
555 if (unlikely(info->mmu_irq) < 0) {
560 #ifdef CONFIG_ARCH_OMAP2
561 clk_enable(dsp_fck_handle);
562 clk_enable(dsp_ick_handle);
566 if ((ret = dsp_ctl_core_init()) < 0)
568 if ((ret = dsp_mem_init()) < 0)
572 if ((ret = dsp_taskmod_init()) < 0)
574 if ((ret = dsp_mbox_init()) < 0)
588 #ifdef CONFIG_ARCH_OMAP2
590 clk_disable(dsp_ick_handle);
591 clk_disable(dsp_fck_handle);
594 dsp_kfunc_remove_devices(info);
601 static int dsp_drv_remove(struct platform_device *pdev)
603 struct omap_dsp *info = platform_get_drvdata(pdev);
605 dsp_cpustat_request(CPUSTAT_RESET);
607 dsp_cfgstat_request(CFGSTAT_CLEAN);
616 #ifdef CONFIG_ARCH_OMAP2
618 clk_disable(dsp_ick_handle);
619 clk_disable(dsp_fck_handle);
621 dsp_kfunc_remove_devices(info);
628 static int dsp_drv_suspend(struct platform_device *pdev, pm_message_t state)
630 dsp_cfgstat_request(CFGSTAT_SUSPEND);
635 static int dsp_drv_resume(struct platform_device *pdev)
637 dsp_cfgstat_request(CFGSTAT_RESUME);
642 #define dsp_drv_suspend NULL
643 #define dsp_drv_resume NULL
644 #endif /* CONFIG_PM */
646 static struct platform_driver dsp_driver = {
647 .probe = dsp_drv_probe,
648 .remove = dsp_drv_remove,
649 .suspend = dsp_drv_suspend,
650 .resume = dsp_drv_resume,
656 static int __init omap_dsp_mod_init(void)
658 return platform_driver_register(&dsp_driver);
661 static void __exit omap_dsp_mod_exit(void)
663 platform_driver_unregister(&dsp_driver);
666 module_init(omap_dsp_mod_init);
667 module_exit(omap_dsp_mod_exit);