2 * File: arch/blackfin/mach-bf561/coreb.c
7 * Description: Handle CoreB on a BF561
10 * Copyright 2004-2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <linux/miscdevice.h>
32 #include <linux/device.h>
33 #include <linux/ioport.h>
34 #include <linux/module.h>
35 #include <linux/uaccess.h>
38 #include <asm/cacheflush.h>
40 #define MODULE_VER "v0.1"
42 static spinlock_t coreb_lock
;
43 static wait_queue_head_t coreb_dma_wait
;
45 #define COREB_IS_OPEN 0x00000001
46 #define COREB_IS_RUNNING 0x00000010
48 #define CMD_COREB_INDEX 1
49 #define CMD_COREB_START 2
50 #define CMD_COREB_STOP 3
51 #define CMD_COREB_RESET 4
53 #define COREB_MINOR 229
55 static unsigned long coreb_status
= 0;
56 static unsigned long coreb_base
= 0xff600000;
57 static unsigned long coreb_size
= 0x4000;
60 static loff_t
coreb_lseek(struct file
*file
, loff_t offset
, int origin
);
61 static ssize_t
coreb_read(struct file
*file
, char *buf
, size_t count
,
63 static ssize_t
coreb_write(struct file
*file
, const char *buf
, size_t count
,
65 static int coreb_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
67 static int coreb_open(struct inode
*inode
, struct file
*file
);
68 static int coreb_release(struct inode
*inode
, struct file
*file
);
70 static irqreturn_t
coreb_dma_interrupt(int irq
, void *dev_id
)
72 clear_dma_irqstat(CH_MEM_STREAM2_DEST
);
74 wake_up_interruptible(&coreb_dma_wait
);
78 static ssize_t
coreb_write(struct file
*file
, const char *buf
, size_t count
,
81 unsigned long p
= *ppos
;
84 if (p
+ count
> coreb_size
)
95 flush_dcache_range((unsigned long)buf
, (unsigned long)(buf
+len
));
97 set_dma_start_addr(CH_MEM_STREAM2_SRC
, (unsigned long)buf
);
98 set_dma_x_count(CH_MEM_STREAM2_SRC
, len
);
99 set_dma_x_modify(CH_MEM_STREAM2_SRC
, sizeof(char));
100 set_dma_config(CH_MEM_STREAM2_SRC
, 0);
101 /* Destination Channel */
102 set_dma_start_addr(CH_MEM_STREAM2_DEST
, coreb_base
+ p
);
103 set_dma_x_count(CH_MEM_STREAM2_DEST
, len
);
104 set_dma_x_modify(CH_MEM_STREAM2_DEST
, sizeof(char));
105 set_dma_config(CH_MEM_STREAM2_DEST
, WNR
| RESTART
| DI_EN
);
107 enable_dma(CH_MEM_STREAM2_SRC
);
108 enable_dma(CH_MEM_STREAM2_DEST
);
110 wait_event_interruptible(coreb_dma_wait
, coreb_dma_done
);
112 disable_dma(CH_MEM_STREAM2_SRC
);
113 disable_dma(CH_MEM_STREAM2_DEST
);
124 static ssize_t
coreb_read(struct file
*file
, char *buf
, size_t count
,
127 unsigned long p
= *ppos
;
130 if ((p
+ count
) > coreb_size
)
141 invalidate_dcache_range((unsigned long)buf
, (unsigned long)(buf
+len
));
143 set_dma_start_addr(CH_MEM_STREAM2_SRC
, coreb_base
+ p
);
144 set_dma_x_count(CH_MEM_STREAM2_SRC
, len
);
145 set_dma_x_modify(CH_MEM_STREAM2_SRC
, sizeof(char));
146 set_dma_config(CH_MEM_STREAM2_SRC
, 0);
147 /* Destination Channel */
148 set_dma_start_addr(CH_MEM_STREAM2_DEST
, (unsigned long)buf
);
149 set_dma_x_count(CH_MEM_STREAM2_DEST
, len
);
150 set_dma_x_modify(CH_MEM_STREAM2_DEST
, sizeof(char));
151 set_dma_config(CH_MEM_STREAM2_DEST
, WNR
| RESTART
| DI_EN
);
153 enable_dma(CH_MEM_STREAM2_SRC
);
154 enable_dma(CH_MEM_STREAM2_DEST
);
156 wait_event_interruptible(coreb_dma_wait
, coreb_dma_done
);
158 disable_dma(CH_MEM_STREAM2_SRC
);
159 disable_dma(CH_MEM_STREAM2_DEST
);
170 static loff_t
coreb_lseek(struct file
*file
, loff_t offset
, int origin
)
174 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
177 case 0 /* SEEK_SET */ :
178 if (offset
< coreb_size
) {
179 file
->f_pos
= offset
;
184 case 1 /* SEEK_CUR */ :
185 if ((offset
+ file
->f_pos
) < coreb_size
) {
186 file
->f_pos
+= offset
;
193 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
197 /* No BKL needed here */
198 static int coreb_open(struct inode
*inode
, struct file
*file
)
200 spin_lock_irq(&coreb_lock
);
202 if (coreb_status
& COREB_IS_OPEN
)
205 coreb_status
|= COREB_IS_OPEN
;
207 spin_unlock_irq(&coreb_lock
);
211 spin_unlock_irq(&coreb_lock
);
215 static int coreb_release(struct inode
*inode
, struct file
*file
)
217 spin_lock_irq(&coreb_lock
);
218 coreb_status
&= ~COREB_IS_OPEN
;
219 spin_unlock_irq(&coreb_lock
);
223 static int coreb_ioctl(struct inode
*inode
, struct file
*file
,
224 unsigned int cmd
, unsigned long arg
)
230 case CMD_COREB_INDEX
:
231 if (copy_from_user(&coreb_index
, (int *)arg
, sizeof(int))) {
236 spin_lock_irq(&coreb_lock
);
237 switch (coreb_index
) {
239 coreb_base
= 0xff600000;
243 coreb_base
= 0xff610000;
247 coreb_base
= 0xff500000;
251 coreb_base
= 0xff400000;
258 spin_unlock_irq(&coreb_lock
);
260 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
262 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
264 case CMD_COREB_START
:
265 spin_lock_irq(&coreb_lock
);
266 if (coreb_status
& COREB_IS_RUNNING
) {
270 printk(KERN_INFO
"Starting Core B\n");
271 coreb_status
|= COREB_IS_RUNNING
;
272 bfin_write_SICA_SYSCR(bfin_read_SICA_SYSCR() & ~0x0020);
274 spin_unlock_irq(&coreb_lock
);
276 #if defined(CONFIG_BF561_COREB_RESET)
278 spin_lock_irq(&coreb_lock
);
279 printk(KERN_INFO
"Stopping Core B\n");
280 bfin_write_SICA_SYSCR(bfin_read_SICA_SYSCR() | 0x0020);
281 bfin_write_SICB_SYSCR(bfin_read_SICB_SYSCR() | 0x0080);
282 coreb_status
&= ~COREB_IS_RUNNING
;
283 spin_unlock_irq(&coreb_lock
);
285 case CMD_COREB_RESET
:
286 printk(KERN_INFO
"Resetting Core B\n");
287 bfin_write_SICB_SYSCR(bfin_read_SICB_SYSCR() | 0x0080);
295 static struct file_operations coreb_fops
= {
296 .owner
= THIS_MODULE
,
297 .llseek
= coreb_lseek
,
299 .write
= coreb_write
,
300 .ioctl
= coreb_ioctl
,
302 .release
= coreb_release
305 static struct miscdevice coreb_dev
= {
311 static ssize_t
coreb_show_status(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
314 "Base Address:\t0x%08lx\n"
316 "SICA_SYSCR:\t%04x\n"
317 "SICB_SYSCR:\t%04x\n"
319 "IRQ Status:\tCore A\t\tCore B\n"
320 "ISR0:\t\t%08x\t\t%08x\n"
321 "ISR1:\t\t%08x\t\t%08x\n"
322 "IMASK0:\t\t%08x\t\t%08x\n"
323 "IMASK1:\t\t%08x\t\t%08x\n",
325 coreb_status
& COREB_IS_RUNNING
? "running" : "stalled",
326 bfin_read_SICA_SYSCR(), bfin_read_SICB_SYSCR(),
327 bfin_read_SICA_ISR0(), bfin_read_SICB_ISR0(),
328 bfin_read_SICA_ISR1(), bfin_read_SICB_ISR0(),
329 bfin_read_SICA_IMASK0(), bfin_read_SICB_IMASK0(),
330 bfin_read_SICA_IMASK1(), bfin_read_SICB_IMASK1());
333 static DEVICE_ATTR(coreb_status
, S_IRUGO
, coreb_show_status
, NULL
);
335 int __init
bf561_coreb_init(void)
337 init_waitqueue_head(&coreb_dma_wait
);
339 spin_lock_init(&coreb_lock
);
340 /* Request the core memory regions for Core B */
341 if (request_mem_region(0xff600000, 0x4000,
342 "Core B - Instruction SRAM") == NULL
)
345 if (request_mem_region(0xFF610000, 0x4000,
346 "Core B - Instruction SRAM") == NULL
)
347 goto release_instruction_a_sram
;
349 if (request_mem_region(0xFF500000, 0x8000,
350 "Core B - Data Bank B SRAM") == NULL
)
351 goto release_instruction_b_sram
;
353 if (request_mem_region(0xff400000, 0x8000,
354 "Core B - Data Bank A SRAM") == NULL
)
355 goto release_data_b_sram
;
357 if (request_dma(CH_MEM_STREAM2_DEST
, "Core B - DMA Destination") < 0)
358 goto release_data_a_sram
;
360 if (request_dma(CH_MEM_STREAM2_SRC
, "Core B - DMA Source") < 0)
361 goto release_dma_dest
;
363 set_dma_callback(CH_MEM_STREAM2_DEST
, coreb_dma_interrupt
, NULL
);
365 misc_register(&coreb_dev
);
367 if (device_create_file(coreb_dev
.this_device
, &dev_attr_coreb_status
))
368 goto release_dma_src
;
370 printk(KERN_INFO
"BF561 Core B driver %s initialized.\n", MODULE_VER
);
374 free_dma(CH_MEM_STREAM2_SRC
);
376 free_dma(CH_MEM_STREAM2_DEST
);
378 release_mem_region(0xff400000, 0x8000);
380 release_mem_region(0xff500000, 0x8000);
381 release_instruction_b_sram
:
382 release_mem_region(0xff610000, 0x4000);
383 release_instruction_a_sram
:
384 release_mem_region(0xff600000, 0x4000);
389 void __exit
bf561_coreb_exit(void)
391 device_remove_file(coreb_dev
.this_device
, &dev_attr_coreb_status
);
392 misc_deregister(&coreb_dev
);
394 release_mem_region(0xff610000, 0x4000);
395 release_mem_region(0xff600000, 0x4000);
396 release_mem_region(0xff500000, 0x8000);
397 release_mem_region(0xff400000, 0x8000);
399 free_dma(CH_MEM_STREAM2_DEST
);
400 free_dma(CH_MEM_STREAM2_SRC
);
403 module_init(bf561_coreb_init
);
404 module_exit(bf561_coreb_exit
);
406 MODULE_AUTHOR("Bas Vermeulen <bvermeul@blackstar.xs4all.nl>");
407 MODULE_DESCRIPTION("BF561 Core B Support");