]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/dsp/dsp_core.c
39765890e135d5ab7f052008f08d5eaff51fe248
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / dsp / dsp_core.c
1 /*
2  * This file is part of OMAP DSP driver (DSP Gateway version 3.3.1)
3  *
4  * Copyright (C) 2002-2006 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
7  *
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. 
11  *
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.
16  *
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
20  * 02110-1301 USA
21  *
22  */
23
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"
35 #include "dsp.h"
36 #include "ipbuf.h"
37
38 MODULE_AUTHOR("Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>");
39 MODULE_DESCRIPTION("OMAP DSP driver module");
40 MODULE_LICENSE("GPL");
41
42 struct mbx *mbx_dsp;
43 static struct sync_seq *mbseq;
44 static u16 mbseq_expect_tmp;
45 static u16 *mbseq_expect = &mbseq_expect_tmp;
46
47 /*
48  * mailbox commands
49  */
50 extern void mbx_wdsnd(struct mbcmd *mb);
51 extern void mbx_wdreq(struct mbcmd *mb);
52 extern void mbx_bksnd(struct mbcmd *mb);
53 extern void mbx_bkreq(struct mbcmd *mb);
54 extern void mbx_bkyld(struct mbcmd *mb);
55 extern void mbx_bksndp(struct mbcmd *mb);
56 extern void mbx_bkreqp(struct mbcmd *mb);
57 extern void mbx_tctl(struct mbcmd *mb);
58 extern void mbx_poll(struct mbcmd *mb);
59 #ifdef OLD_BINARY_SUPPORT
60 /* v3.3 obsolete */
61 extern void mbx_wdt(struct mbcmd *mb);
62 #endif
63 extern void mbx_suspend(struct mbcmd *mb);
64 static void mbx_kfunc(struct mbcmd *mb);
65 extern void mbx_tcfg(struct mbcmd *mb);
66 extern void mbx_tadd(struct mbcmd *mb);
67 extern void mbx_tdel(struct mbcmd *mb);
68 extern void mbx_dspcfg(struct mbcmd *mb);
69 extern void mbx_regrw(struct mbcmd *mb);
70 extern void mbx_getvar(struct mbcmd *mb);
71 extern void mbx_err(struct mbcmd *mb);
72 extern void mbx_dbg(struct mbcmd *mb);
73
74 static const struct cmdinfo
75         cif_wdsnd    = { "WDSND",    CMD_L_TYPE_TID,    mbx_wdsnd   },
76         cif_wdreq    = { "WDREQ",    CMD_L_TYPE_TID,    mbx_wdreq   },
77         cif_bksnd    = { "BKSND",    CMD_L_TYPE_TID,    mbx_bksnd   },
78         cif_bkreq    = { "BKREQ",    CMD_L_TYPE_TID,    mbx_bkreq   },
79         cif_bkyld    = { "BKYLD",    CMD_L_TYPE_NULL,   mbx_bkyld   },
80         cif_bksndp   = { "BKSNDP",   CMD_L_TYPE_TID,    mbx_bksndp  },
81         cif_bkreqp   = { "BKREQP",   CMD_L_TYPE_TID,    mbx_bkreqp  },
82         cif_tctl     = { "TCTL",     CMD_L_TYPE_TID,    mbx_tctl    },
83         cif_poll     = { "POLL",     CMD_L_TYPE_NULL,   mbx_poll    },
84 #ifdef OLD_BINARY_SUPPORT
85         /* v3.3 obsolete */
86         cif_wdt      = { "WDT",      CMD_L_TYPE_NULL,   mbx_wdt     },
87 #endif
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,   mbx_suspend },
91         cif_kfunc    = { "KFUNC",    CMD_L_TYPE_SUBCMD, mbx_kfunc   },
92         cif_tcfg     = { "TCFG",     CMD_L_TYPE_TID,    mbx_tcfg    },
93         cif_tadd     = { "TADD",     CMD_L_TYPE_TID,    mbx_tadd    },
94         cif_tdel     = { "TDEL",     CMD_L_TYPE_TID,    mbx_tdel    },
95         cif_tstop    = { "TSTOP",    CMD_L_TYPE_TID,    NULL        },
96         cif_dspcfg   = { "DSPCFG",   CMD_L_TYPE_SUBCMD, mbx_dspcfg  },
97         cif_regrw    = { "REGRW",    CMD_L_TYPE_SUBCMD, mbx_regrw   },
98         cif_getvar   = { "GETVAR",   CMD_L_TYPE_SUBCMD, mbx_getvar  },
99         cif_setvar   = { "SETVAR",   CMD_L_TYPE_SUBCMD, NULL        },
100         cif_err      = { "ERR",      CMD_L_TYPE_SUBCMD, mbx_err     },
101         cif_dbg      = { "DBG",      CMD_L_TYPE_NULL,   mbx_dbg     };
102
103 const struct cmdinfo *cmdinfo[MBX_CMD_MAX] = {
104         [MBX_CMD_DSP_WDSND]    = &cif_wdsnd,
105         [MBX_CMD_DSP_WDREQ]    = &cif_wdreq,
106         [MBX_CMD_DSP_BKSND]    = &cif_bksnd,
107         [MBX_CMD_DSP_BKREQ]    = &cif_bkreq,
108         [MBX_CMD_DSP_BKYLD]    = &cif_bkyld,
109         [MBX_CMD_DSP_BKSNDP]   = &cif_bksndp,
110         [MBX_CMD_DSP_BKREQP]   = &cif_bkreqp,
111         [MBX_CMD_DSP_TCTL]     = &cif_tctl,
112         [MBX_CMD_DSP_POLL]     = &cif_poll,
113 #ifdef OLD_BINARY_SUPPORT
114         [MBX_CMD_DSP_WDT]      = &cif_wdt, /* v3.3 obsolete */
115 #endif
116         [MBX_CMD_DSP_RUNLEVEL] = &cif_runlevel,
117         [MBX_CMD_DSP_PM]       = &cif_pm,
118         [MBX_CMD_DSP_SUSPEND]  = &cif_suspend,
119         [MBX_CMD_DSP_KFUNC]    = &cif_kfunc,
120         [MBX_CMD_DSP_TCFG]     = &cif_tcfg,
121         [MBX_CMD_DSP_TADD]     = &cif_tadd,
122         [MBX_CMD_DSP_TDEL]     = &cif_tdel,
123         [MBX_CMD_DSP_TSTOP]    = &cif_tstop,
124         [MBX_CMD_DSP_DSPCFG]   = &cif_dspcfg,
125         [MBX_CMD_DSP_REGRW]    = &cif_regrw,
126         [MBX_CMD_DSP_GETVAR]   = &cif_getvar,
127         [MBX_CMD_DSP_SETVAR]   = &cif_setvar,
128         [MBX_CMD_DSP_ERR]      = &cif_err,
129         [MBX_CMD_DSP_DBG]      = &cif_dbg,
130 };
131
132 int sync_with_dsp(u16 *adr, u16 val, int try_cnt)
133 {
134         int try;
135
136         if (*(volatile u16 *)adr == val)
137                 return 0;
138
139         for (try = 0; try < try_cnt; try++) {
140                 udelay(1);
141                 if (*(volatile u16 *)adr == val) {
142                         /* success! */
143                         printk(KERN_INFO
144                                "omapdsp: sync_with_dsp(): try = %d\n", try);
145                         return 0;
146                 }
147         }
148
149         /* fail! */
150         return -1;
151 }
152
153 /*
154  * __dsp_mbcmd_send_exarg(): mailbox dispatcher
155  */
156 int __dsp_mbcmd_send_exarg(struct mbcmd *mb, struct mb_exarg *arg,
157                            int recovery_flag)
158 {
159         static DEFINE_MUTEX(mbsend_lock);
160         int ret = 0;
161
162         /*
163          * while MMU fault is set,
164          * only recovery command can be executed
165          */
166         if (dsp_err_isset(ERRCODE_MMU) && !recovery_flag) {
167                 printk(KERN_ERR
168                        "mbx: mmu interrupt is set. %s is aborting.\n",
169                        cmd_name(*mb));
170                 return -1;
171         }
172
173         if (mutex_lock_interruptible(&mbsend_lock) < 0)
174                 return -1;
175
176         if (arg) {      /* we have extra argument */
177                 int i;
178
179                 /*
180                  * even if ipbuf_sys_ad is in DSP internal memory,
181                  * dsp_mem_enable() never cause to call PM mailbox command
182                  * because in that case DSP memory should be always enabled.
183                  * (see ipbuf_sys_hold_mem_active in ipbuf.c)
184                  *
185                  * Therefore, we can call this function here safely.
186                  */
187                 dsp_mem_enable(ipbuf_sys_ad);
188                 if (sync_with_dsp(&ipbuf_sys_ad->s, TID_FREE, 10) < 0) {
189                         printk(KERN_ERR "omapdsp: ipbuf_sys_ad is busy.\n");
190                         dsp_mem_disable(ipbuf_sys_ad);
191                         ret = -EBUSY;
192                         goto out;
193                 }
194                 for (i = 0; i < arg->argc; i++) {
195                         ipbuf_sys_ad->d[i] = arg->argv[i];
196                 }
197                 ipbuf_sys_ad->s = arg->tid;
198                 dsp_mem_disable(ipbuf_sys_ad);
199         }
200
201         if (mbseq)
202                 mbseq->ad_arm++;
203
204         mblog_add(mb, DIR_A2D);
205         mblog_printcmd(mb, DIR_A2D);
206
207         ret = mbx_send(mbx_dsp, *(mbx_msg_t *)mb);
208
209 out:
210         mutex_unlock(&mbsend_lock);
211         return ret;
212 }
213
214 int dsp_mbcmd_send_and_wait_exarg(struct mbcmd *mb, struct mb_exarg *arg,
215                                   wait_queue_head_t *q)
216 {
217         long current_state;
218         DECLARE_WAITQUEUE(wait, current);
219
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);
226                 return -1;
227         }
228         schedule_timeout(DSP_TIMEOUT);
229         set_current_state(current_state);
230         remove_wait_queue(q, &wait);
231
232         return 0;
233 }
234
235 /*
236  * mbcmd receiver
237  */
238 static void mbcmd_receiver(mbx_msg_t msg)
239 {
240         struct mbcmd *mb = (struct mbcmd *)&msg;
241
242         if (cmdinfo[mb->cmd_h] == NULL) {
243                 printk(KERN_ERR
244                        "invalid message (%08x) for mbcmd_receiver().\n", msg);
245                 return;
246         }
247
248         (*mbseq_expect)++;
249
250         mblog_add(mb, DIR_D2A);
251         mblog_printcmd(mb, DIR_D2A);
252
253         /* call handler for the command */
254         if (cmdinfo[mb->cmd_h]->handler)
255                 cmdinfo[mb->cmd_h]->handler(mb);
256         else
257                 printk(KERN_ERR "mbx: %s is not allowed from DSP.\n",
258                        cmd_name(*mb));
259 }
260
261 static int mbsync_hold_mem_active;
262
263 void dsp_mbx_start(void)
264 {
265         mbx_init_seq(mbx_dsp);
266         mbseq_expect_tmp = 0;
267 }
268
269 void dsp_mbx_stop(void)
270 {
271         mbseq = NULL;
272         mbseq_expect = &mbseq_expect_tmp;
273 }
274
275 int dsp_mbx_config(void *p)
276 {
277         unsigned long flags;
278
279         if (dsp_address_validate(p, sizeof(struct sync_seq), "mbseq") < 0)
280                 return -1;
281         if (dsp_mem_type(p, sizeof(struct sync_seq)) != MEM_TYPE_EXTERN) {
282                 printk(KERN_WARNING
283                        "omapdsp: mbseq is placed in DSP internal memory.\n"
284                        "         It will prevent DSP from idling.\n");
285                 mbsync_hold_mem_active = 1;
286                 /*
287                  * dsp_mem_enable() never fails because
288                  * it has been already enabled in dspcfg process and
289                  * this will just increment the usecount.
290                  */
291                 dsp_mem_enable((void *)daram_base);
292         }
293
294         local_irq_save(flags);
295         mbseq = p;
296         mbseq->da_arm = mbseq_expect_tmp;
297         mbseq_expect = &mbseq->da_arm;
298         local_irq_restore(flags);
299
300         return 0;
301 }
302
303 static int __init dsp_mbx_init(void)
304 {
305         int i;
306         int ret;
307
308         for (i = 0; i < MBX_CMD_MAX; i++) {
309                 if (cmdinfo[i] != NULL) {
310                         ret = register_mbx_receiver(mbx_dsp, i, mbcmd_receiver);
311                         if (ret)
312                                 goto fail;
313                 }
314         }
315
316         return 0;
317
318 fail:
319         for (i--; i; i--)
320                 unregister_mbx_receiver(mbx_dsp, i, mbcmd_receiver);
321
322         return ret;
323 }
324
325 static void dsp_mbx_exit(void)
326 {
327         int i;
328
329         for (i = 0; i < MBX_CMD_MAX; i++) {
330                 if (cmdinfo[i] != NULL)
331                         unregister_mbx_receiver(mbx_dsp, i, mbcmd_receiver);
332         }
333
334         if (mbsync_hold_mem_active) {
335                 dsp_mem_disable((void *)daram_base);
336                 mbsync_hold_mem_active = 0;
337         }
338 }
339
340 /*
341  * kernel function dispatcher
342  */
343 extern void mbx_fbctl_upd(void);
344 extern void mbx_fbctl_disable(struct mbcmd *mb);
345
346 static void mbx_kfunc_fbctl(struct mbcmd *mb)
347 {
348         switch (mb->data) {
349         case FBCTL_UPD:
350                 mbx_fbctl_upd();
351                 break;
352         case FBCTL_DISABLE:
353                 mbx_fbctl_disable(mb);
354                 break;
355         default:
356                 printk(KERN_ERR
357                        "mbx: Unknown FBCTL from DSP: 0x%04x\n", mb->data);
358         }
359 }
360
361 static void mbx_kfunc_audio_pwr(unsigned short data)
362 {
363         switch (data) {
364         case AUDIO_PWR_UP:
365                 omap_dsp_audio_pwr_up_request(0);
366                 /* send back ack */
367                 mbcompose_send(KFUNC, KFUNC_AUDIO_PWR, AUDIO_PWR_UP);
368                 break;
369         case AUDIO_PWR_DOWN1:
370                 omap_dsp_audio_pwr_down_request(1);
371                 break;
372         case AUDIO_PWR_DOWN2:
373                 omap_dsp_audio_pwr_down_request(2);
374                 break;
375         default:
376                 printk(KERN_ERR
377                        "mailbox: Unknown AUDIO_PWR from DSP: 0x%04x\n", data);
378         }
379 }
380
381 static void mbx_kfunc(struct mbcmd *mb)
382 {
383         switch (mb->cmd_l) {
384         case KFUNC_FBCTL:
385                 mbx_kfunc_fbctl(mb);
386                 break;
387         case KFUNC_AUDIO_PWR:
388                 mbx_kfunc_audio_pwr(mb->data);
389                 break;
390         default:
391                 printk(KERN_ERR
392                        "mbx: Unknown KFUNC from DSP: 0x%02x\n", mb->cmd_l);
393         }
394 }
395
396 extern int  dsp_ctl_core_init(void);
397 extern void dsp_ctl_core_exit(void);
398 extern void dsp_ctl_init(void);
399 extern void dsp_ctl_exit(void);
400 extern int  dsp_mem_init(void);
401 extern void dsp_mem_exit(void);
402 extern void mblog_init(void);
403 extern void mblog_exit(void);
404 extern int  dsp_taskmod_init(void);
405 extern void dsp_taskmod_exit(void);
406
407 /*
408  * device functions
409  */
410 static void dsp_dev_release(struct device *dev)
411 {
412 }
413
414 /*
415  * driver functions
416  */
417 static int __init dsp_drv_probe(struct platform_device *pdev)
418 {
419         int ret;
420
421         printk(KERN_INFO "OMAP DSP driver initialization\n");
422 #ifdef CONFIG_ARCH_OMAP2
423         clk_enable(dsp_fck_handle);
424         clk_enable(dsp_ick_handle);
425         __dsp_per_enable();
426 #endif
427
428         if ((ret = dsp_ctl_core_init()) < 0)
429                 goto fail1;
430         if ((ret = dsp_mem_init()) < 0)
431                 goto fail2;
432         dsp_ctl_init();
433         mblog_init();
434         if ((ret = dsp_taskmod_init()) < 0)
435                 goto fail3;
436         if ((ret = dsp_mbx_init()) < 0)
437                 goto fail4;
438
439         return 0;
440
441 fail4:
442         dsp_taskmod_exit();
443 fail3:
444         mblog_exit();
445         dsp_ctl_exit();
446         dsp_mem_exit();
447 fail2:
448         dsp_ctl_core_exit();
449 fail1:
450 #ifdef CONFIG_ARCH_OMAP2
451         __dsp_per_disable();
452         clk_disable(dsp_ick_handle);
453         clk_disable(dsp_fck_handle);
454 #endif
455         return ret;
456 }
457
458 static int dsp_drv_remove(struct platform_device *pdev)
459 {
460         dsp_cpustat_request(CPUSTAT_RESET);
461
462         dsp_cfgstat_request(CFGSTAT_CLEAN);
463         dsp_mbx_exit();
464         dsp_taskmod_exit();
465         mblog_exit();
466         dsp_ctl_exit();
467         dsp_mem_exit();
468
469         dsp_ctl_core_exit();
470
471 #ifdef CONFIG_ARCH_OMAP2
472         __dsp_per_disable();
473         clk_disable(dsp_ick_handle);
474         clk_disable(dsp_fck_handle);
475 #endif
476         return 0;
477 }
478
479 #ifdef CONFIG_PM
480 static int dsp_drv_suspend(struct platform_device *pdev, pm_message_t state)
481 {
482         dsp_cfgstat_request(CFGSTAT_SUSPEND);
483
484         return 0;
485 }
486
487 static int dsp_drv_resume(struct platform_device *pdev)
488 {
489         dsp_cfgstat_request(CFGSTAT_RESUME);
490
491         return 0;
492 }
493 #else
494 #define dsp_drv_suspend         NULL
495 #define dsp_drv_resume          NULL
496 #endif /* CONFIG_PM */
497
498 static struct resource dsp_resources[] = {
499         {
500                 .start = INT_DSP_MMU,
501                 .flags = IORESOURCE_IRQ,
502         },
503 };
504
505 struct platform_device dsp_device = {
506         .name           = "dsp",
507         .id             = -1,
508         .dev = {
509                 .release        = dsp_dev_release,
510         },
511         .num_resources  = ARRAY_SIZE(&dsp_resources),
512         .resource       = dsp_resources,
513 };
514
515 static struct platform_driver dsp_driver = {
516         .probe          = dsp_drv_probe,
517         .remove         = dsp_drv_remove,
518         .suspend        = dsp_drv_suspend,
519         .resume         = dsp_drv_resume,
520         .driver         = {
521                 .name   = "dsp",
522         },
523 };
524
525 static int __init omap_dsp_mod_init(void)
526 {
527         int ret;
528
529         mbx_dsp = mbx_get("DSP");
530         if (IS_ERR(mbx_dsp)) {
531                 printk(KERN_ERR "failed to get mailbox handler for DSP.\n");
532                 goto fail1;
533         }
534
535         ret = platform_device_register(&dsp_device);
536         if (ret) {
537                 printk(KERN_ERR "failed to register the DSP device: %d\n", ret);
538                 goto fail1;
539         }
540
541         ret = platform_driver_register(&dsp_driver);
542         if (ret) {
543                 printk(KERN_ERR "failed to register the DSP driver: %d\n", ret);
544                 goto fail2;
545         }
546
547         return 0;
548
549 fail2:
550         platform_device_unregister(&dsp_device);
551 fail1:
552         return -ENODEV;
553 }
554
555 static void __exit omap_dsp_mod_exit(void)
556 {
557         platform_driver_unregister(&dsp_driver);
558         platform_device_unregister(&dsp_device);
559 }
560
561 module_init(omap_dsp_mod_init);
562 module_exit(omap_dsp_mod_exit);