]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/dsp/dsp_common.c
[PATCH] ARM: OMAP: rework mutex_init() for RT compatibility
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / dsp / dsp_common.c
1 /*
2  * linux/arch/arm/mach-omap/dsp/dsp_common.c
3  *
4  * OMAP DSP driver static part
5  *
6  * Copyright (C) 2002-2005 Nokia Corporation
7  *
8  * Written by Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * 2005/06/13:  DSP Gateway version 3.3
25  */
26
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/mm.h>
33 #include <linux/clk.h>
34 #include <linux/mutex.h>
35 #include <asm/io.h>
36 #include <asm/tlbflush.h>
37 #include <asm/irq.h>
38 #include <asm/arch/dsp.h>
39 #include <asm/arch/tc.h>
40 #include "dsp_common.h"
41
42 struct clk *dsp_ck_handle;
43 struct clk *api_ck_handle;
44 unsigned long dspmem_base, dspmem_size,
45               daram_base, daram_size,
46               saram_base, saram_size;
47
48 struct cpustat {
49         struct mutex lock;
50         enum e_cpustat stat;
51         enum e_cpustat req;
52         unsigned short icrmask;
53         struct {
54                 int mpui;
55                 int mem;
56                 int mem_delayed;
57         } usecount;
58         int (*mem_req_cb)(void);
59         void (*mem_rel_cb)(void);
60 };
61 struct cpustat cpustat = {
62         .stat = CPUSTAT_RESET,
63         .icrmask = 0xffff,
64 };
65
66 int dsp_set_rstvect(unsigned long adr)
67 {
68         unsigned long *dst_adr;
69
70         if (adr >= DSPSPACE_SIZE)
71                 return -EINVAL;
72
73         dst_adr = dspbyte_to_virt(DSP_BOOT_ADR_DIRECT);
74         /* word swap */
75         *dst_adr = ((adr & 0xffff) << 16) | (adr >> 16);
76         /* fill 8 bytes! */
77         *(dst_adr+1) = 0;
78         /* direct boot */
79         omap_writew(MPUI_DSP_BOOT_CONFIG_DIRECT, MPUI_DSP_BOOT_CONFIG);
80
81         return 0;
82 }
83
84 static void simple_load_code(unsigned char *src_c, unsigned short *dst, int len)
85 {
86         int i;
87         unsigned short *src = (unsigned short *)src_c;
88         int len_w;
89
90         /* len must be multiple of 2. */
91         if (len & 1)
92                 BUG();
93
94         len_w = len / 2;
95         for (i = 0; i < len_w; i++) {
96                 /* byte swap copy */
97                 *dst = ((*src & 0x00ff) << 8) |
98                        ((*src & 0xff00) >> 8);
99                 src++;
100                 dst++;
101         }
102 }
103
104 /* program size must be multiple of 2 */
105 #define GBL_IDLE_TEXT_SIZE      52
106 #define GBL_IDLE_TEXT_INIT { \
107         /* SAM */ \
108         0x3c, 0x4a,                     /* 0x3c4a:     MOV 0x4, AR2 */ \
109         0xf4, 0x41, 0xfc, 0xff,         /* 0xf441fcff: AND 0xfcff, *AR2 */ \
110         /* disable WDT */ \
111         0x76, 0x34, 0x04, 0xb8,         /* 0x763404b8: MOV 0x3404, AR3 */ \
112         0xfb, 0x61, 0x00, 0xf5,         /* 0xfb6100f5: MOV 0x00f5, *AR3 */ \
113         0x9a,                           /* 0x9a:       PORT */ \
114         0xfb, 0x61, 0x00, 0xa0,         /* 0xfb6100a0: MOV 0x00a0, *AR3 */ \
115         0x9a,                           /* 0x9a:       PORT */ \
116         /* *IER0 = 0, *IER1 = 0 */ \
117         0x3c, 0x0b,                     /* 0x3c0b:     MOV 0x0, AR3 */ \
118         0xe6, 0x61, 0x00,               /* 0xe66100:   MOV 0, *AR3 */ \
119         0x76, 0x00, 0x45, 0xb8,         /* 0x76004508: MOV 0x45, AR3 */ \
120         0xe6, 0x61, 0x00,               /* 0xe66100:   MOV 0, *AR3 */ \
121         /* *ICR = 0xffff */ \
122         0x3c, 0x1b,                     /* 0x3c1b:     MOV 0x1, AR3 */ \
123         0xfb, 0x61, 0xff, 0xff,         /* 0xfb61ffff: MOV 0xffff, *AR3 */ \
124         0x9a,                           /* 0x9a:       PORT */ \
125         /* HOM */ \
126         0xf5, 0x41, 0x03, 0x00,         /* 0xf5410300: OR 0x0300, *AR2 */ \
127         /* idle and loop forever */ \
128         0x7a, 0x00, 0x00, 0x0c,         /* 0x7a00000c: IDLE */ \
129         0x4a, 0x7a,                     /* 0x4a7a:     B -6 (infinite loop) */ \
130         0x20, 0x20, 0x20,               /* 0x20:       NOP */ \
131 }
132
133 /* program size must be multiple of 2 */
134 #define CPU_IDLE_TEXT_SIZE      48
135 #define CPU_IDLE_TEXT_INIT(icrh, icrl) { \
136         /* SAM */ \
137         0x3c, 0x4b,                     /* 0x3c4b:     MOV 0x4, AR3 */ \
138         0xf4, 0x61, 0xfc, 0xff,         /* 0xf461fcff: AND 0xfcff, *AR3 */ \
139         /* disable WDT */ \
140         0x76, 0x34, 0x04, 0xb8,         /* 0x763404b8: MOV 0x3404, AR3 */ \
141         0xfb, 0x61, 0x00, 0xf5,         /* 0xfb6100f5: MOV 0x00f5, *AR3 */ \
142         0x9a,                           /* 0x9a:       PORT */ \
143         0xfb, 0x61, 0x00, 0xa0,         /* 0xfb6100a0: MOV 0x00a0, *AR3 */ \
144         0x9a,                           /* 0x9a:       PORT */ \
145         /* *IER0 = 0, *IER1 = 0 */ \
146         0x3c, 0x0b,                     /* 0x3c0b:     MOV 0x0, AR3 */ \
147         0xe6, 0x61, 0x00,               /* 0xe66100:   MOV 0, *AR3 */ \
148         0x76, 0x00, 0x45, 0xb8,         /* 0x76004508: MOV 0x45, AR3 */ \
149         0xe6, 0x61, 0x00,               /* 0xe66100:   MOV 0, *AR3 */ \
150         /* set ICR = icr */ \
151         0x3c, 0x1b,                     /* 0x3c1b:     MOV AR3 0x1 */ \
152         0xfb, 0x61, (icrh), (icrl),     /* 0xfb61****: MOV *AR3, icr */ \
153         0x9a,                           /* 0x9a:       PORT */ \
154         /* idle and loop forever */ \
155         0x7a, 0x00, 0x00, 0x0c,         /* 0x7a00000c: IDLE */ \
156         0x4a, 0x7a,                     /* 0x4a7a:     B -6 (infinite loop) */ \
157         0x20, 0x20, 0x20                /* 0x20: nop */ \
158 }
159
160 /*
161  * idle_boot base:
162  * Initialized with DSP_BOOT_ADR_MPUI (=0x010000).
163  * This value is used before DSP Gateway driver is initialized.
164  * DSP Gateway driver will overwrite this value with other value,
165  * to avoid confliction with the user program.
166  */
167 static unsigned long idle_boot_base = DSP_BOOT_ADR_MPUI;
168
169 static void dsp_gbl_idle(void)
170 {
171         unsigned char idle_text[GBL_IDLE_TEXT_SIZE] = GBL_IDLE_TEXT_INIT;
172
173         __dsp_reset();
174         clk_enable(api_ck_handle);
175
176 #if 0
177         omap_writew(MPUI_DSP_BOOT_CONFIG_IDLE, MPUI_DSP_BOOT_CONFIG);
178 #endif
179         simple_load_code(idle_text, dspbyte_to_virt(idle_boot_base),
180                          GBL_IDLE_TEXT_SIZE);
181         if (idle_boot_base == DSP_BOOT_ADR_MPUI)
182                 omap_writew(MPUI_DSP_BOOT_CONFIG_MPUI, MPUI_DSP_BOOT_CONFIG);
183         else
184                 dsp_set_rstvect(idle_boot_base);
185
186         __dsp_run();
187         udelay(100);    /* to make things stable */
188         clk_disable(api_ck_handle);
189 }
190
191 static void dsp_cpu_idle(void)
192 {
193         unsigned short icr_tmp;
194         unsigned char icrh, icrl;
195
196         __dsp_reset();
197         clk_enable(api_ck_handle);
198
199         /*
200          * icr settings:
201          * DMA should not sleep for DARAM/SARAM access
202          * DPLL should not sleep while any other domain is active
203          */
204         icr_tmp = cpustat.icrmask & ~(DSPREG_ICR_DMA_IDLE_DOMAIN |
205                                       DSPREG_ICR_DPLL_IDLE_DOMAIN);
206         icrh = icr_tmp >> 8;
207         icrl = icr_tmp & 0xff;
208         {
209                 unsigned char idle_text[CPU_IDLE_TEXT_SIZE] = CPU_IDLE_TEXT_INIT(icrh, icrl);
210                 simple_load_code(idle_text, dspbyte_to_virt(idle_boot_base),
211                                  CPU_IDLE_TEXT_SIZE);
212         }
213         if (idle_boot_base == DSP_BOOT_ADR_MPUI)
214                 omap_writew(MPUI_DSP_BOOT_CONFIG_MPUI, MPUI_DSP_BOOT_CONFIG);
215         else
216                 dsp_set_rstvect(idle_boot_base);
217         __dsp_run();
218         udelay(100);    /* to make things stable */
219         clk_disable(api_ck_handle);
220 }
221
222 void dsp_set_idle_boot_base(unsigned long adr, size_t size)
223 {
224         if (adr == idle_boot_base)
225                 return;
226         idle_boot_base = adr;
227         if ((size < GBL_IDLE_TEXT_SIZE) ||
228             (size < CPU_IDLE_TEXT_SIZE)) {
229                 printk(KERN_ERR
230                        "omapdsp: size for idle program is not enough!\n");
231                 BUG();
232         }
233
234         /* restart idle program with new base address */
235         if (cpustat.stat == CPUSTAT_GBL_IDLE)
236                 dsp_gbl_idle();
237         if (cpustat.stat == CPUSTAT_CPU_IDLE)
238                 dsp_cpu_idle();
239 }
240
241 static int init_done;
242
243 static int __init omap_dsp_init(void)
244 {
245         mutex_init(&cpustat.lock);
246
247         dspmem_size = 0;
248 #ifdef CONFIG_ARCH_OMAP15XX
249         if (cpu_is_omap1510()) {
250                 dspmem_base = OMAP1510_DSP_BASE;
251                 dspmem_size = OMAP1510_DSP_SIZE;
252                 daram_base = OMAP1510_DARAM_BASE;
253                 daram_size = OMAP1510_DARAM_SIZE;
254                 saram_base = OMAP1510_SARAM_BASE;
255                 saram_size = OMAP1510_SARAM_SIZE;
256         }
257 #endif
258 #ifdef CONFIG_ARCH_OMAP16XX
259         if (cpu_is_omap16xx()) {
260                 dspmem_base = OMAP16XX_DSP_BASE;
261                 dspmem_size = OMAP16XX_DSP_SIZE;
262                 daram_base = OMAP16XX_DARAM_BASE;
263                 daram_size = OMAP16XX_DARAM_SIZE;
264                 saram_base = OMAP16XX_SARAM_BASE;
265                 saram_size = OMAP16XX_SARAM_SIZE;
266         }
267 #endif
268         if (dspmem_size == 0) {
269                 printk(KERN_ERR "omapdsp: unsupported omap architecture.\n");
270                 return -ENODEV;
271         }
272
273         dsp_ck_handle = clk_get(0, "dsp_ck");
274         if (IS_ERR(dsp_ck_handle)) {
275                 printk(KERN_ERR "omapdsp: could not acquire dsp_ck handle.\n");
276                 return PTR_ERR(dsp_ck_handle);
277         }
278
279         api_ck_handle = clk_get(0, "api_ck");
280         if (IS_ERR(api_ck_handle)) {
281                 printk(KERN_ERR "omapdsp: could not acquire api_ck handle.\n");
282                 return PTR_ERR(api_ck_handle);
283         }
284
285         /* This is needed for McBSP init, released in late_initcall */
286         clk_enable(api_ck_handle);
287
288         __dsp_enable();
289         mpui_byteswap_off();
290         mpui_wordswap_on();
291         tc_wordswap();
292
293         init_done = 1;
294         printk(KERN_INFO "omap_dsp_init() done\n");
295         return 0;
296 }
297
298 static int dsp_late_init(void)
299 {
300         clk_disable(api_ck_handle);
301         return 0;
302 }
303 late_initcall(dsp_late_init);
304
305 static void dsp_cpustat_update(void)
306 {
307         if (!init_done)
308                 omap_dsp_init();
309
310         if (cpustat.req == CPUSTAT_RUN) {
311                 if (cpustat.stat < CPUSTAT_RUN) {
312                         __dsp_reset();
313                         clk_enable(api_ck_handle);
314                         udelay(10);
315                         __dsp_run();
316                         cpustat.stat = CPUSTAT_RUN;
317                         enable_irq(INT_DSP_MMU);
318                 }
319                 return;
320         }
321
322         /* cpustat.stat < CPUSTAT_RUN */
323
324         if (cpustat.stat == CPUSTAT_RUN) {
325                 disable_irq(INT_DSP_MMU);
326                 clk_disable(api_ck_handle);
327         }
328
329         /*
330          * (1) when ARM wants DARAM access, MPUI should be SAM and
331          *     DSP needs to be on.
332          * (2) if any bits of icr is masked, we can not enter global idle.
333          */
334         if ((cpustat.req == CPUSTAT_CPU_IDLE) ||
335             (cpustat.usecount.mem > 0) ||
336             (cpustat.usecount.mem_delayed > 0) ||
337             ((cpustat.usecount.mpui > 0) && (cpustat.icrmask != 0xffff))) {
338                 if (cpustat.stat != CPUSTAT_CPU_IDLE) {
339                         dsp_cpu_idle();
340                         cpustat.stat = CPUSTAT_CPU_IDLE;
341                 }
342                 return;
343         }
344
345         /*
346          * when ARM only needs MPUI access, MPUI can be HOM and
347          * DSP can be idling.
348          */
349         if ((cpustat.req == CPUSTAT_GBL_IDLE) ||
350             (cpustat.usecount.mpui > 0)) {
351                 if (cpustat.stat != CPUSTAT_GBL_IDLE) {
352                         dsp_gbl_idle();
353                         cpustat.stat = CPUSTAT_GBL_IDLE;
354                 }
355                 return;
356         }
357
358         /*
359          * no user, no request
360          */
361         if (cpustat.stat != CPUSTAT_RESET) {
362                 __dsp_reset();
363                 cpustat.stat = CPUSTAT_RESET;
364         }
365 }
366
367 void dsp_cpustat_request(enum e_cpustat req)
368 {
369         mutex_lock(&cpustat.lock);
370         cpustat.req = req;
371         dsp_cpustat_update();
372         mutex_unlock(&cpustat.lock);
373 }
374
375 enum e_cpustat dsp_cpustat_get_stat(void)
376 {
377         return cpustat.stat;
378 }
379
380 unsigned short dsp_cpustat_get_icrmask(void)
381 {
382         return cpustat.icrmask;
383 }
384
385 void dsp_cpustat_set_icrmask(unsigned short mask)
386 {
387         mutex_lock(&cpustat.lock);
388         cpustat.icrmask = mask;
389         dsp_cpustat_update();
390         mutex_unlock(&cpustat.lock);
391 }
392
393 void omap_dsp_request_mpui(void)
394 {
395         mutex_lock(&cpustat.lock);
396         if (cpustat.usecount.mpui++ == 0)
397                 dsp_cpustat_update();
398         mutex_unlock(&cpustat.lock);
399 }
400
401 void omap_dsp_release_mpui(void)
402 {
403         mutex_lock(&cpustat.lock);
404         if (cpustat.usecount.mpui-- == 0) {
405                 printk(KERN_ERR
406                        "omapdsp: unbalanced mpui request/release detected.\n"
407                        "         cpustat.usecount.mpui is going to be "
408                        "less than zero! ... fixed to be zero.\n");
409                 cpustat.usecount.mpui = 0;
410         }
411         if (cpustat.usecount.mpui == 0)
412                 dsp_cpustat_update();
413         mutex_unlock(&cpustat.lock);
414 }
415
416 int omap_dsp_request_mem(void)
417 {
418         int ret = 0;
419
420         mutex_lock(&cpustat.lock);
421         if ((cpustat.usecount.mem++ == 0) &&
422             (cpustat.usecount.mem_delayed == 0)) {
423                 if (cpustat.mem_req_cb) {
424                         if ((ret = cpustat.mem_req_cb()) < 0) {
425                                 cpustat.usecount.mem--;
426                                 goto out;
427                         }
428                 }
429                 dsp_cpustat_update();
430         }
431 out:
432         mutex_unlock(&cpustat.lock);
433
434         return ret;
435 }
436
437 /*
438  * release_mem will be delayed.
439  */
440 static void do_release_mem(void)
441 {
442         mutex_lock(&cpustat.lock);
443         cpustat.usecount.mem_delayed = 0;
444         if (cpustat.usecount.mem == 0) {
445                 dsp_cpustat_update();
446                 if (cpustat.mem_rel_cb)
447                         cpustat.mem_rel_cb();
448         }
449         mutex_unlock(&cpustat.lock);
450 }
451
452 static DECLARE_WORK(mem_rel_work, (void (*)(void *))do_release_mem, NULL);
453
454 int omap_dsp_release_mem(void)
455 {
456         mutex_lock(&cpustat.lock);
457
458         /* cancel previous release work */
459         cancel_delayed_work(&mem_rel_work);
460         cpustat.usecount.mem_delayed = 0;
461
462         if (cpustat.usecount.mem-- == 0) {
463                 printk(KERN_ERR
464                        "omapdsp: unbalanced memory request/release detected.\n"
465                        "         cpustat.usecount.mem is going to be "
466                        "less than zero! ... fixed to be zero.\n");
467                 cpustat.usecount.mem = 0;
468         }
469         if (cpustat.usecount.mem == 0) {
470                 cpustat.usecount.mem_delayed = 1;
471                 schedule_delayed_work(&mem_rel_work, HZ);
472         }
473
474         mutex_unlock(&cpustat.lock);
475
476         return 0;
477 }
478
479 void dsp_register_mem_cb(int (*req_cb)(void), void (*rel_cb)(void))
480 {
481         mutex_lock(&cpustat.lock);
482
483         cpustat.mem_req_cb = req_cb;
484         cpustat.mem_rel_cb = rel_cb;
485
486         /*
487          * This function must be called while mem is enabled!
488          */
489         BUG_ON(cpustat.usecount.mem == 0);
490
491         mutex_unlock(&cpustat.lock);
492 }
493
494 void dsp_unregister_mem_cb(void)
495 {
496         mutex_lock(&cpustat.lock);
497         cpustat.mem_req_cb = NULL;
498         cpustat.mem_rel_cb = NULL;
499         mutex_unlock(&cpustat.lock);
500 }
501
502 /*
503  * Audio power control function prototypes and defaults
504  * (To be overridden with board specific functions)
505  */
506 static void generic_audio_pwr_up_request(int stage)
507 {
508         printk(KERN_ERR "audio power-up request function is not defined.\n");
509 }
510
511 void (*omap_dsp_audio_pwr_up_request)(int stage) = generic_audio_pwr_up_request;
512 EXPORT_SYMBOL(omap_dsp_audio_pwr_up_request);
513
514 static void generic_audio_pwr_down_request(int stage)
515 {
516         printk(KERN_ERR "audio power-down request function is not defined.\n");
517 }
518
519 void (*omap_dsp_audio_pwr_down_request)(int stage) = generic_audio_pwr_down_request;
520 EXPORT_SYMBOL(omap_dsp_audio_pwr_down_request);
521
522 arch_initcall(omap_dsp_init);
523
524 EXPORT_SYMBOL(omap_dsp_request_mpui);
525 EXPORT_SYMBOL(omap_dsp_release_mpui);
526 EXPORT_SYMBOL(omap_dsp_request_mem);
527 EXPORT_SYMBOL(omap_dsp_release_mem);
528
529 #ifdef CONFIG_OMAP_DSP_MODULE
530 EXPORT_SYMBOL(dsp_ck_handle);
531 EXPORT_SYMBOL(api_ck_handle);
532 EXPORT_SYMBOL(dspmem_base);
533 EXPORT_SYMBOL(dspmem_size);
534 EXPORT_SYMBOL(daram_base);
535 EXPORT_SYMBOL(daram_size);
536 EXPORT_SYMBOL(saram_base);
537 EXPORT_SYMBOL(saram_size);
538 EXPORT_SYMBOL(dsp_set_rstvect);
539 EXPORT_SYMBOL(dsp_set_idle_boot_base);
540 EXPORT_SYMBOL(dsp_cpustat_request);
541 EXPORT_SYMBOL(dsp_cpustat_get_stat);
542 EXPORT_SYMBOL(dsp_cpustat_get_icrmask);
543 EXPORT_SYMBOL(dsp_cpustat_set_icrmask);
544 EXPORT_SYMBOL(dsp_register_mem_cb);
545 EXPORT_SYMBOL(dsp_unregister_mem_cb);
546
547 EXPORT_SYMBOL(__cpu_flush_kern_tlb_range);
548 EXPORT_SYMBOL(cpu_architecture);
549 EXPORT_SYMBOL(pmd_clear_bad);
550 #endif