2 * linux/arch/arm/mach-omap/dsp/dsp_common.c
4 * OMAP DSP driver static part
6 * Copyright (C) 2002-2005 Nokia Corporation
8 * Written by Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
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.
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.
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
24 * 2005/06/13: DSP Gateway version 3.3
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>
34 #include <asm/tlbflush.h>
36 #include <asm/arch/dsp.h>
37 #include <asm/arch/tc.h>
38 #include <asm/hardware/clock.h>
39 #include "dsp_common.h"
41 struct clk *dsp_ck_handle;
42 struct clk *api_ck_handle;
43 unsigned long dspmem_base, dspmem_size,
44 daram_base, daram_size,
45 saram_base, saram_size;
51 unsigned short icrmask;
57 int (*mem_req_cb)(void);
58 void (*mem_rel_cb)(void);
60 struct cpustat cpustat = {
61 .sem = __SEMAPHORE_INIT(cpustat.sem, 1),
62 .stat = CPUSTAT_RESET,
66 int dsp_set_rstvect(unsigned long adr)
68 unsigned long *dst_adr;
70 if (adr >= DSPSPACE_SIZE)
73 dst_adr = dspbyte_to_virt(DSP_BOOT_ADR_DIRECT);
75 *dst_adr = ((adr & 0xffff) << 16) | (adr >> 16);
79 omap_writew(MPUI_DSP_BOOT_CONFIG_DIRECT, MPUI_DSP_BOOT_CONFIG);
84 static void simple_load_code(unsigned char *src_c, unsigned short *dst, int len)
87 unsigned short *src = (unsigned short *)src_c;
90 /* len must be multiple of 2. */
95 for (i = 0; i < len_w; i++) {
97 *dst = ((*src & 0x00ff) << 8) |
98 ((*src & 0xff00) >> 8);
104 /* program size must be multiple of 2 */
105 #define GBL_IDLE_TEXT_SIZE 52
106 #define GBL_IDLE_TEXT_INIT { \
108 0x3c, 0x4a, /* 0x3c4a: MOV 0x4, AR2 */ \
109 0xf4, 0x41, 0xfc, 0xff, /* 0xf441fcff: AND 0xfcff, *AR2 */ \
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 */ \
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 */ \
133 /* program size must be multiple of 2 */
134 #define CPU_IDLE_TEXT_SIZE 48
135 #define CPU_IDLE_TEXT_INIT(icrh, icrl) { \
137 0x3c, 0x4b, /* 0x3c4b: MOV 0x4, AR3 */ \
138 0xf4, 0x61, 0xfc, 0xff, /* 0xf461fcff: AND 0xfcff, *AR3 */ \
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 */ \
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.
167 static unsigned long idle_boot_base = DSP_BOOT_ADR_MPUI;
169 static void dsp_gbl_idle(void)
171 unsigned char idle_text[GBL_IDLE_TEXT_SIZE] = GBL_IDLE_TEXT_INIT;
174 clk_enable(api_ck_handle);
177 omap_writew(MPUI_DSP_BOOT_CONFIG_IDLE, MPUI_DSP_BOOT_CONFIG);
179 simple_load_code(idle_text, dspbyte_to_virt(idle_boot_base),
181 if (idle_boot_base == DSP_BOOT_ADR_MPUI)
182 omap_writew(MPUI_DSP_BOOT_CONFIG_MPUI, MPUI_DSP_BOOT_CONFIG);
184 dsp_set_rstvect(idle_boot_base);
187 udelay(100); /* to make things stable */
188 clk_disable(api_ck_handle);
191 static void dsp_cpu_idle(void)
193 unsigned short icr_tmp;
194 unsigned char icrh, icrl;
197 clk_enable(api_ck_handle);
201 * DMA should not sleep for DARAM/SARAM access
202 * DPLL should not sleep while any other domain is active
204 icr_tmp = cpustat.icrmask & ~(DSPREG_ICR_DMA_IDLE_DOMAIN |
205 DSPREG_ICR_DPLL_IDLE_DOMAIN);
207 icrl = icr_tmp & 0xff;
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),
213 if (idle_boot_base == DSP_BOOT_ADR_MPUI)
214 omap_writew(MPUI_DSP_BOOT_CONFIG_MPUI, MPUI_DSP_BOOT_CONFIG);
216 dsp_set_rstvect(idle_boot_base);
218 udelay(100); /* to make things stable */
219 clk_disable(api_ck_handle);
222 void dsp_set_idle_boot_base(unsigned long adr, size_t size)
224 if (adr == idle_boot_base)
226 idle_boot_base = adr;
227 if ((size < GBL_IDLE_TEXT_SIZE) ||
228 (size < CPU_IDLE_TEXT_SIZE)) {
230 "omapdsp: size for idle program is not enough!\n");
234 /* restart idle program with new base address */
235 if (cpustat.stat == CPUSTAT_GBL_IDLE)
237 if (cpustat.stat == CPUSTAT_CPU_IDLE)
241 static unsigned short save_dsp_idlect2;
242 static int init_done;
245 * note: if we are in pm_suspend / pm_resume function,
246 * we are out of clk_enable() management.
248 void omap_dsp_pm_suspend(void)
250 unsigned short save_arm_idlect2;
258 /* DSP code may have turned this on, make sure it gets turned off */
259 clk_enable(dsp_ck_handle);
260 clk_disable(dsp_ck_handle);
262 /* Stop any DSP domain clocks */
263 save_arm_idlect2 = omap_readw(ARM_IDLECT2); // api_ck is in ARM_IDLECT2
264 clk_enable(api_ck_handle);
265 save_dsp_idlect2 = __raw_readw(DSP_IDLECT2);
266 __raw_writew(0, DSP_IDLECT2);
267 omap_writew(save_arm_idlect2, ARM_IDLECT2);
268 clk_disable(api_ck_handle);
271 void omap_dsp_pm_resume(void)
273 unsigned short save_arm_idlect2;
278 /* Restore DSP domain clocks */
279 save_arm_idlect2 = omap_readw(ARM_IDLECT2); // api_ck is in ARM_IDLECT2
280 clk_enable(api_ck_handle);
281 __raw_writew(save_dsp_idlect2, DSP_IDLECT2);
282 omap_writew(save_arm_idlect2, ARM_IDLECT2);
283 clk_disable(api_ck_handle);
285 /* Run DSP, if it was running */
286 if (cpustat.stat != CPUSTAT_RESET)
290 static int __init omap_dsp_init(void)
293 #ifdef CONFIG_ARCH_OMAP15XX
294 if (cpu_is_omap1510()) {
295 dspmem_base = OMAP1510_DSP_BASE;
296 dspmem_size = OMAP1510_DSP_SIZE;
297 daram_base = OMAP1510_DARAM_BASE;
298 daram_size = OMAP1510_DARAM_SIZE;
299 saram_base = OMAP1510_SARAM_BASE;
300 saram_size = OMAP1510_SARAM_SIZE;
303 #ifdef CONFIG_ARCH_OMAP16XX
304 if (cpu_is_omap16xx()) {
305 dspmem_base = OMAP16XX_DSP_BASE;
306 dspmem_size = OMAP16XX_DSP_SIZE;
307 daram_base = OMAP16XX_DARAM_BASE;
308 daram_size = OMAP16XX_DARAM_SIZE;
309 saram_base = OMAP16XX_SARAM_BASE;
310 saram_size = OMAP16XX_SARAM_SIZE;
313 if (dspmem_size == 0) {
314 printk(KERN_ERR "omapdsp: unsupported omap architecture.\n");
318 dsp_ck_handle = clk_get(0, "dsp_ck");
319 if (IS_ERR(dsp_ck_handle)) {
320 printk(KERN_ERR "omapdsp: could not acquire dsp_ck handle.\n");
321 return PTR_ERR(dsp_ck_handle);
324 api_ck_handle = clk_get(0, "api_ck");
325 if (IS_ERR(api_ck_handle)) {
326 printk(KERN_ERR "omapdsp: could not acquire api_ck handle.\n");
327 return PTR_ERR(api_ck_handle);
339 static void dsp_cpustat_update(void)
344 if (cpustat.req == CPUSTAT_RUN) {
345 if (cpustat.stat < CPUSTAT_RUN) {
347 clk_enable(api_ck_handle);
350 cpustat.stat = CPUSTAT_RUN;
351 enable_irq(INT_DSP_MMU);
356 /* cpustat.stat < CPUSTAT_RUN */
358 if (cpustat.stat == CPUSTAT_RUN) {
359 disable_irq(INT_DSP_MMU);
360 clk_disable(api_ck_handle);
364 * (1) when ARM wants DARAM access, MPUI should be SAM and
365 * DSP needs to be on.
366 * (2) if any bits of icr is masked, we can not enter global idle.
368 if ((cpustat.req == CPUSTAT_CPU_IDLE) ||
369 (cpustat.usecount.mem > 0) ||
370 (cpustat.usecount.mem_delayed > 0) ||
371 ((cpustat.usecount.mpui > 0) && (cpustat.icrmask != 0xffff))) {
372 if (cpustat.stat != CPUSTAT_CPU_IDLE) {
374 cpustat.stat = CPUSTAT_CPU_IDLE;
380 * when ARM only needs MPUI access, MPUI can be HOM and
383 if ((cpustat.req == CPUSTAT_GBL_IDLE) ||
384 (cpustat.usecount.mpui > 0)) {
385 if (cpustat.stat != CPUSTAT_GBL_IDLE) {
387 cpustat.stat = CPUSTAT_GBL_IDLE;
393 * no user, no request
395 if (cpustat.stat != CPUSTAT_RESET) {
397 cpustat.stat = CPUSTAT_RESET;
401 void dsp_cpustat_request(enum e_cpustat req)
405 dsp_cpustat_update();
409 enum e_cpustat dsp_cpustat_get_stat(void)
414 unsigned short dsp_cpustat_get_icrmask(void)
416 return cpustat.icrmask;
419 void dsp_cpustat_set_icrmask(unsigned short mask)
422 cpustat.icrmask = mask;
423 dsp_cpustat_update();
427 void omap_dsp_request_mpui(void)
430 if (cpustat.usecount.mpui++ == 0)
431 dsp_cpustat_update();
435 void omap_dsp_release_mpui(void)
438 if (cpustat.usecount.mpui-- == 0) {
440 "omapdsp: unbalanced mpui request/release detected.\n"
441 " cpustat.usecount.mpui is going to be "
442 "less than zero! ... fixed to be zero.\n");
443 cpustat.usecount.mpui = 0;
445 if (cpustat.usecount.mpui == 0)
446 dsp_cpustat_update();
450 int omap_dsp_request_mem(void)
455 if ((cpustat.usecount.mem++ == 0) &&
456 (cpustat.usecount.mem_delayed == 0)) {
457 if (cpustat.mem_req_cb) {
458 if ((ret = cpustat.mem_req_cb()) < 0) {
459 cpustat.usecount.mem--;
463 dsp_cpustat_update();
472 * release_mem will be delayed.
474 static void do_release_mem(void) {
476 cpustat.usecount.mem_delayed = 0;
477 if (cpustat.usecount.mem == 0) {
478 dsp_cpustat_update();
479 if (cpustat.mem_rel_cb)
480 cpustat.mem_rel_cb();
485 static DECLARE_WORK(mem_rel_work, (void (*)(void *))do_release_mem, NULL);
487 int omap_dsp_release_mem(void)
491 /* cancel previous release work */
492 cancel_delayed_work(&mem_rel_work);
493 cpustat.usecount.mem_delayed = 0;
495 if (cpustat.usecount.mem-- == 0) {
497 "omapdsp: unbalanced memory request/release detected.\n"
498 " cpustat.usecount.mem is going to be "
499 "less than zero! ... fixed to be zero.\n");
500 cpustat.usecount.mem = 0;
502 if (cpustat.usecount.mem == 0) {
503 cpustat.usecount.mem_delayed = 1;
504 schedule_delayed_work(&mem_rel_work, HZ);
512 void dsp_register_mem_cb(int (*req_cb)(void), void (*rel_cb)(void))
516 cpustat.mem_req_cb = req_cb;
517 cpustat.mem_rel_cb = rel_cb;
520 * This function must be called while mem is enabled!
522 BUG_ON(cpustat.usecount.mem == 0);
527 void dsp_unregister_mem_cb(void)
530 cpustat.mem_req_cb = NULL;
531 cpustat.mem_rel_cb = NULL;
535 arch_initcall(omap_dsp_init);
537 EXPORT_SYMBOL(omap_dsp_pm_suspend);
538 EXPORT_SYMBOL(omap_dsp_pm_resume);
539 EXPORT_SYMBOL(omap_dsp_request_mpui);
540 EXPORT_SYMBOL(omap_dsp_release_mpui);
541 EXPORT_SYMBOL(omap_dsp_request_mem);
542 EXPORT_SYMBOL(omap_dsp_release_mem);
544 #ifdef CONFIG_OMAP_DSP_MODULE
545 EXPORT_SYMBOL(dsp_ck_handle);
546 EXPORT_SYMBOL(api_ck_handle);
547 EXPORT_SYMBOL(dspmem_base);
548 EXPORT_SYMBOL(dspmem_size);
549 EXPORT_SYMBOL(daram_base);
550 EXPORT_SYMBOL(daram_size);
551 EXPORT_SYMBOL(saram_base);
552 EXPORT_SYMBOL(saram_size);
553 EXPORT_SYMBOL(dsp_set_rstvect);
554 EXPORT_SYMBOL(dsp_set_idle_boot_base);
555 EXPORT_SYMBOL(dsp_cpustat_request);
556 EXPORT_SYMBOL(dsp_cpustat_get_stat);
557 EXPORT_SYMBOL(dsp_cpustat_get_icrmask);
558 EXPORT_SYMBOL(dsp_cpustat_set_icrmask);
559 EXPORT_SYMBOL(dsp_register_mem_cb);
560 EXPORT_SYMBOL(dsp_unregister_mem_cb);
562 EXPORT_SYMBOL(__cpu_flush_kern_tlb_range);