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"
38 MODULE_AUTHOR("Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>");
39 MODULE_DESCRIPTION("OMAP DSP driver module");
40 MODULE_LICENSE("GPL");
42 struct mbox *mbox_dsp;
43 static struct sync_seq *mbseq;
44 static u16 mbseq_expect_tmp;
45 static u16 *mbseq_expect = &mbseq_expect_tmp;
50 extern void mbox_wdsnd(struct mbcmd *mb);
51 extern void mbox_wdreq(struct mbcmd *mb);
52 extern void mbox_bksnd(struct mbcmd *mb);
53 extern void mbox_bkreq(struct mbcmd *mb);
54 extern void mbox_bkyld(struct mbcmd *mb);
55 extern void mbox_bksndp(struct mbcmd *mb);
56 extern void mbox_bkreqp(struct mbcmd *mb);
57 extern void mbox_tctl(struct mbcmd *mb);
58 extern void mbox_poll(struct mbcmd *mb);
59 #ifdef OLD_BINARY_SUPPORT
61 extern void mbox_wdt(struct mbcmd *mb);
63 extern void mbox_suspend(struct mbcmd *mb);
64 static void mbox_kfunc(struct mbcmd *mb);
65 extern void mbox_tcfg(struct mbcmd *mb);
66 extern void mbox_tadd(struct mbcmd *mb);
67 extern void mbox_tdel(struct mbcmd *mb);
68 extern void mbox_dspcfg(struct mbcmd *mb);
69 extern void mbox_regrw(struct mbcmd *mb);
70 extern void mbox_getvar(struct mbcmd *mb);
71 extern void mbox_err(struct mbcmd *mb);
72 extern void mbox_dbg(struct mbcmd *mb);
74 static const struct cmdinfo
75 cif_wdsnd = { "WDSND", CMD_L_TYPE_TID, mbox_wdsnd },
76 cif_wdreq = { "WDREQ", CMD_L_TYPE_TID, mbox_wdreq },
77 cif_bksnd = { "BKSND", CMD_L_TYPE_TID, mbox_bksnd },
78 cif_bkreq = { "BKREQ", CMD_L_TYPE_TID, mbox_bkreq },
79 cif_bkyld = { "BKYLD", CMD_L_TYPE_NULL, mbox_bkyld },
80 cif_bksndp = { "BKSNDP", CMD_L_TYPE_TID, mbox_bksndp },
81 cif_bkreqp = { "BKREQP", CMD_L_TYPE_TID, mbox_bkreqp },
82 cif_tctl = { "TCTL", CMD_L_TYPE_TID, mbox_tctl },
83 cif_poll = { "POLL", CMD_L_TYPE_NULL, mbox_poll },
84 #ifdef OLD_BINARY_SUPPORT
86 cif_wdt = { "WDT", CMD_L_TYPE_NULL, mbox_wdt },
88 cif_runlevel = { "RUNLEVEL", CMD_L_TYPE_SUBCMD, NULL },
89 cif_pm = { "PM", CMD_L_TYPE_SUBCMD, NULL },
90 cif_suspend = { "SUSPEND", CMD_L_TYPE_NULL, mbox_suspend },
91 cif_kfunc = { "KFUNC", CMD_L_TYPE_SUBCMD, mbox_kfunc },
92 cif_tcfg = { "TCFG", CMD_L_TYPE_TID, mbox_tcfg },
93 cif_tadd = { "TADD", CMD_L_TYPE_TID, mbox_tadd },
94 cif_tdel = { "TDEL", CMD_L_TYPE_TID, mbox_tdel },
95 cif_tstop = { "TSTOP", CMD_L_TYPE_TID, NULL },
96 cif_dspcfg = { "DSPCFG", CMD_L_TYPE_SUBCMD, mbox_dspcfg },
97 cif_regrw = { "REGRW", CMD_L_TYPE_SUBCMD, mbox_regrw },
98 cif_getvar = { "GETVAR", CMD_L_TYPE_SUBCMD, mbox_getvar },
99 cif_setvar = { "SETVAR", CMD_L_TYPE_SUBCMD, NULL },
100 cif_err = { "ERR", CMD_L_TYPE_SUBCMD, mbox_err },
101 cif_dbg = { "DBG", CMD_L_TYPE_NULL, mbox_dbg };
103 #define MBOX_CMD_MAX 0x80
104 const struct cmdinfo *cmdinfo[MBOX_CMD_MAX] = {
105 [MBOX_CMD_DSP_WDSND] = &cif_wdsnd,
106 [MBOX_CMD_DSP_WDREQ] = &cif_wdreq,
107 [MBOX_CMD_DSP_BKSND] = &cif_bksnd,
108 [MBOX_CMD_DSP_BKREQ] = &cif_bkreq,
109 [MBOX_CMD_DSP_BKYLD] = &cif_bkyld,
110 [MBOX_CMD_DSP_BKSNDP] = &cif_bksndp,
111 [MBOX_CMD_DSP_BKREQP] = &cif_bkreqp,
112 [MBOX_CMD_DSP_TCTL] = &cif_tctl,
113 [MBOX_CMD_DSP_POLL] = &cif_poll,
114 #ifdef OLD_BINARY_SUPPORT
115 [MBOX_CMD_DSP_WDT] = &cif_wdt, /* v3.3 obsolete */
117 [MBOX_CMD_DSP_RUNLEVEL] = &cif_runlevel,
118 [MBOX_CMD_DSP_PM] = &cif_pm,
119 [MBOX_CMD_DSP_SUSPEND] = &cif_suspend,
120 [MBOX_CMD_DSP_KFUNC] = &cif_kfunc,
121 [MBOX_CMD_DSP_TCFG] = &cif_tcfg,
122 [MBOX_CMD_DSP_TADD] = &cif_tadd,
123 [MBOX_CMD_DSP_TDEL] = &cif_tdel,
124 [MBOX_CMD_DSP_TSTOP] = &cif_tstop,
125 [MBOX_CMD_DSP_DSPCFG] = &cif_dspcfg,
126 [MBOX_CMD_DSP_REGRW] = &cif_regrw,
127 [MBOX_CMD_DSP_GETVAR] = &cif_getvar,
128 [MBOX_CMD_DSP_SETVAR] = &cif_setvar,
129 [MBOX_CMD_DSP_ERR] = &cif_err,
130 [MBOX_CMD_DSP_DBG] = &cif_dbg,
133 int sync_with_dsp(u16 *adr, u16 val, int try_cnt)
137 if (*(volatile u16 *)adr == val)
140 for (try = 0; try < try_cnt; try++) {
142 if (*(volatile u16 *)adr == val) {
145 "omapdsp: sync_with_dsp(): try = %d\n", try);
155 * __dsp_mbcmd_send_exarg(): mailbox dispatcher
157 int __dsp_mbcmd_send_exarg(struct mbcmd *mb, struct mb_exarg *arg,
160 static DEFINE_MUTEX(mbsend_lock);
164 * while MMU fault is set,
165 * only recovery command can be executed
167 if (dsp_err_isset(ERRCODE_MMU) && !recovery_flag) {
169 "mbox: mmu interrupt is set. %s is aborting.\n",
174 if (mutex_lock_interruptible(&mbsend_lock) < 0)
177 if (arg) { /* we have extra argument */
181 * even if ipbuf_sys_ad is in DSP internal memory,
182 * dsp_mem_enable() never cause to call PM mailbox command
183 * because in that case DSP memory should be always enabled.
184 * (see ipbuf_sys_hold_mem_active in ipbuf.c)
186 * Therefore, we can call this function here safely.
188 dsp_mem_enable(ipbuf_sys_ad);
189 if (sync_with_dsp(&ipbuf_sys_ad->s, TID_FREE, 10) < 0) {
190 printk(KERN_ERR "omapdsp: ipbuf_sys_ad is busy.\n");
191 dsp_mem_disable(ipbuf_sys_ad);
195 for (i = 0; i < arg->argc; i++) {
196 ipbuf_sys_ad->d[i] = arg->argv[i];
198 ipbuf_sys_ad->s = arg->tid;
199 dsp_mem_disable(ipbuf_sys_ad);
205 mblog_add(mb, DIR_A2D);
207 ret = mbox_send(mbox_dsp, *(mbox_msg_t *)mb);
210 mutex_unlock(&mbsend_lock);
214 int dsp_mbcmd_send_and_wait_exarg(struct mbcmd *mb, struct mb_exarg *arg,
215 wait_queue_head_t *q)
218 DECLARE_WAITQUEUE(wait, current);
220 add_wait_queue(q, &wait);
221 current_state = current->state;
222 set_current_state(TASK_INTERRUPTIBLE);
223 if (dsp_mbcmd_send_exarg(mb, arg) < 0) {
224 set_current_state(current_state);
225 remove_wait_queue(q, &wait);
228 schedule_timeout(DSP_TIMEOUT);
229 set_current_state(current_state);
230 remove_wait_queue(q, &wait);
238 static void mbcmd_receiver(mbox_msg_t msg)
240 struct mbcmd *mb = (struct mbcmd *)&msg;
242 if (cmdinfo[mb->cmd_h] == NULL) {
244 "invalid message (%08x) for mbcmd_receiver().\n", msg);
250 mblog_add(mb, DIR_D2A);
252 /* call handler for the command */
253 if (cmdinfo[mb->cmd_h]->handler)
254 cmdinfo[mb->cmd_h]->handler(mb);
256 printk(KERN_ERR "mbox: %s is not allowed from DSP.\n",
260 static int mbsync_hold_mem_active;
262 void dsp_mbox_start(void)
264 mbox_init_seq(mbox_dsp);
265 mbseq_expect_tmp = 0;
268 void dsp_mbox_stop(void)
271 mbseq_expect = &mbseq_expect_tmp;
274 int dsp_mbox_config(void *p)
278 if (dsp_address_validate(p, sizeof(struct sync_seq), "mbseq") < 0)
280 if (dsp_mem_type(p, sizeof(struct sync_seq)) != MEM_TYPE_EXTERN) {
282 "omapdsp: mbseq is placed in DSP internal memory.\n"
283 " It will prevent DSP from idling.\n");
284 mbsync_hold_mem_active = 1;
286 * dsp_mem_enable() never fails because
287 * it has been already enabled in dspcfg process and
288 * this will just increment the usecount.
290 dsp_mem_enable((void *)daram_base);
293 local_irq_save(flags);
295 mbseq->da_arm = mbseq_expect_tmp;
296 mbseq_expect = &mbseq->da_arm;
297 local_irq_restore(flags);
302 static int __init dsp_mbox_init(void)
307 for (i = 0; i < MBOX_CMD_MAX; i++) {
308 if (cmdinfo[i] != NULL) {
309 ret = register_mbox_receiver(mbox_dsp, i, mbcmd_receiver);
319 unregister_mbox_receiver(mbox_dsp, i, mbcmd_receiver);
324 static void dsp_mbox_exit(void)
328 for (i = 0; i < MBOX_CMD_MAX; i++) {
329 if (cmdinfo[i] != NULL)
330 unregister_mbox_receiver(mbox_dsp, i, mbcmd_receiver);
333 if (mbsync_hold_mem_active) {
334 dsp_mem_disable((void *)daram_base);
335 mbsync_hold_mem_active = 0;
340 * kernel function dispatcher
342 extern void mbox_fbctl_upd(void);
343 extern void mbox_fbctl_disable(struct mbcmd *mb);
345 static void mbox_kfunc_fbctl(struct mbcmd *mb)
352 mbox_fbctl_disable(mb);
356 "mbox: Unknown FBCTL from DSP: 0x%04x\n", mb->data);
360 static void mbox_kfunc_audio_pwr(unsigned short data)
364 omap_dsp_audio_pwr_up_request(0);
366 mbcompose_send(KFUNC, KFUNC_AUDIO_PWR, AUDIO_PWR_UP);
368 case AUDIO_PWR_DOWN1:
369 omap_dsp_audio_pwr_down_request(1);
371 case AUDIO_PWR_DOWN2:
372 omap_dsp_audio_pwr_down_request(2);
376 "mailbox: Unknown AUDIO_PWR from DSP: 0x%04x\n", data);
380 static void mbox_kfunc(struct mbcmd *mb)
384 mbox_kfunc_fbctl(mb);
386 case KFUNC_AUDIO_PWR:
387 mbox_kfunc_audio_pwr(mb->data);
391 "mbox: Unknown KFUNC from DSP: 0x%02x\n", mb->cmd_l);
395 extern int dsp_ctl_core_init(void);
396 extern void dsp_ctl_core_exit(void);
397 extern void dsp_ctl_init(void);
398 extern void dsp_ctl_exit(void);
399 extern int dsp_mem_init(void);
400 extern void dsp_mem_exit(void);
401 extern void mblog_init(void);
402 extern void mblog_exit(void);
403 extern int dsp_taskmod_init(void);
404 extern void dsp_taskmod_exit(void);
409 static void dsp_dev_release(struct device *dev)
416 static int __init dsp_drv_probe(struct platform_device *pdev)
420 printk(KERN_INFO "OMAP DSP driver initialization\n");
421 #ifdef CONFIG_ARCH_OMAP2
422 clk_enable(dsp_fck_handle);
423 clk_enable(dsp_ick_handle);
427 if ((ret = dsp_ctl_core_init()) < 0)
429 if ((ret = dsp_mem_init()) < 0)
433 if ((ret = dsp_taskmod_init()) < 0)
435 if ((ret = dsp_mbox_init()) < 0)
449 #ifdef CONFIG_ARCH_OMAP2
451 clk_disable(dsp_ick_handle);
452 clk_disable(dsp_fck_handle);
457 static int dsp_drv_remove(struct platform_device *pdev)
459 dsp_cpustat_request(CPUSTAT_RESET);
461 dsp_cfgstat_request(CFGSTAT_CLEAN);
470 #ifdef CONFIG_ARCH_OMAP2
472 clk_disable(dsp_ick_handle);
473 clk_disable(dsp_fck_handle);
479 static int dsp_drv_suspend(struct platform_device *pdev, pm_message_t state)
481 dsp_cfgstat_request(CFGSTAT_SUSPEND);
486 static int dsp_drv_resume(struct platform_device *pdev)
488 dsp_cfgstat_request(CFGSTAT_RESUME);
493 #define dsp_drv_suspend NULL
494 #define dsp_drv_resume NULL
495 #endif /* CONFIG_PM */
497 static struct resource dsp_resources[] = {
499 .start = INT_DSP_MMU,
500 .flags = IORESOURCE_IRQ,
504 struct platform_device dsp_device = {
508 .release = dsp_dev_release,
510 .num_resources = ARRAY_SIZE(&dsp_resources),
511 .resource = dsp_resources,
514 static struct platform_driver dsp_driver = {
515 .probe = dsp_drv_probe,
516 .remove = dsp_drv_remove,
517 .suspend = dsp_drv_suspend,
518 .resume = dsp_drv_resume,
524 static int __init omap_dsp_mod_init(void)
528 mbox_dsp = mbox_get("DSP");
529 if (IS_ERR(mbox_dsp)) {
530 printk(KERN_ERR "failed to get mailbox handler for DSP.\n");
534 ret = platform_device_register(&dsp_device);
536 printk(KERN_ERR "failed to register the DSP device: %d\n", ret);
540 ret = platform_driver_register(&dsp_driver);
542 printk(KERN_ERR "failed to register the DSP driver: %d\n", ret);
549 platform_device_unregister(&dsp_device);
554 static void __exit omap_dsp_mod_exit(void)
556 platform_driver_unregister(&dsp_driver);
557 platform_device_unregister(&dsp_device);
560 module_init(omap_dsp_mod_init);
561 module_exit(omap_dsp_mod_exit);