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>
36 #include <asm/uaccess.h>
38 #define MODULE_VER "v0.1"
40 static spinlock_t coreb_lock
;
41 static wait_queue_head_t coreb_dma_wait
;
43 #define COREB_IS_OPEN 0x00000001
44 #define COREB_IS_RUNNING 0x00000010
46 #define CMD_COREB_INDEX 1
47 #define CMD_COREB_START 2
48 #define CMD_COREB_STOP 3
49 #define CMD_COREB_RESET 4
51 #define COREB_MINOR 229
53 static unsigned long coreb_status
= 0;
54 static unsigned long coreb_base
= 0xff600000;
55 static unsigned long coreb_size
= 0x4000;
58 static loff_t
coreb_lseek(struct file
*file
, loff_t offset
, int origin
);
59 static ssize_t
coreb_read(struct file
*file
, char *buf
, size_t count
,
61 static ssize_t
coreb_write(struct file
*file
, const char *buf
, size_t count
,
63 static int coreb_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
65 static int coreb_open(struct inode
*inode
, struct file
*file
);
66 static int coreb_release(struct inode
*inode
, struct file
*file
);
68 static irqreturn_t
coreb_dma_interrupt(int irq
, void *dev_id
)
70 clear_dma_irqstat(CH_MEM_STREAM2_DEST
);
72 wake_up_interruptible(&coreb_dma_wait
);
76 static ssize_t
coreb_write(struct file
*file
, const char *buf
, size_t count
,
79 unsigned long p
= *ppos
;
82 if (p
+ count
> coreb_size
)
94 set_dma_start_addr(CH_MEM_STREAM2_SRC
, (unsigned long)buf
);
95 set_dma_x_count(CH_MEM_STREAM2_SRC
, len
);
96 set_dma_x_modify(CH_MEM_STREAM2_SRC
, sizeof(char));
97 set_dma_config(CH_MEM_STREAM2_SRC
, RESTART
);
98 /* Destination Channel */
99 set_dma_start_addr(CH_MEM_STREAM2_DEST
, coreb_base
+ p
);
100 set_dma_x_count(CH_MEM_STREAM2_DEST
, len
);
101 set_dma_x_modify(CH_MEM_STREAM2_DEST
, sizeof(char));
102 set_dma_config(CH_MEM_STREAM2_DEST
, WNR
| RESTART
| DI_EN
);
104 enable_dma(CH_MEM_STREAM2_SRC
);
105 enable_dma(CH_MEM_STREAM2_DEST
);
107 wait_event_interruptible(coreb_dma_wait
, coreb_dma_done
);
109 disable_dma(CH_MEM_STREAM2_SRC
);
110 disable_dma(CH_MEM_STREAM2_DEST
);
121 static ssize_t
coreb_read(struct file
*file
, char *buf
, size_t count
,
124 unsigned long p
= *ppos
;
127 if ((p
+ count
) > coreb_size
)
139 set_dma_start_addr(CH_MEM_STREAM2_SRC
, coreb_base
+ p
);
140 set_dma_x_count(CH_MEM_STREAM2_SRC
, len
);
141 set_dma_x_modify(CH_MEM_STREAM2_SRC
, sizeof(char));
142 set_dma_config(CH_MEM_STREAM2_SRC
, RESTART
);
143 /* Destination Channel */
144 set_dma_start_addr(CH_MEM_STREAM2_DEST
, (unsigned long)buf
);
145 set_dma_x_count(CH_MEM_STREAM2_DEST
, len
);
146 set_dma_x_modify(CH_MEM_STREAM2_DEST
, sizeof(char));
147 set_dma_config(CH_MEM_STREAM2_DEST
, WNR
| RESTART
| DI_EN
);
149 enable_dma(CH_MEM_STREAM2_SRC
);
150 enable_dma(CH_MEM_STREAM2_DEST
);
152 wait_event_interruptible(coreb_dma_wait
, coreb_dma_done
);
154 disable_dma(CH_MEM_STREAM2_SRC
);
155 disable_dma(CH_MEM_STREAM2_DEST
);
166 static loff_t
coreb_lseek(struct file
*file
, loff_t offset
, int origin
)
170 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
173 case 0 /* SEEK_SET */ :
174 if (offset
< coreb_size
) {
175 file
->f_pos
= offset
;
180 case 1 /* SEEK_CUR */ :
181 if ((offset
+ file
->f_pos
) < coreb_size
) {
182 file
->f_pos
+= offset
;
189 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
193 static int coreb_open(struct inode
*inode
, struct file
*file
)
195 spin_lock_irq(&coreb_lock
);
197 if (coreb_status
& COREB_IS_OPEN
)
200 coreb_status
|= COREB_IS_OPEN
;
202 spin_unlock_irq(&coreb_lock
);
206 spin_unlock_irq(&coreb_lock
);
210 static int coreb_release(struct inode
*inode
, struct file
*file
)
212 spin_lock_irq(&coreb_lock
);
213 coreb_status
&= ~COREB_IS_OPEN
;
214 spin_unlock_irq(&coreb_lock
);
218 static int coreb_ioctl(struct inode
*inode
, struct file
*file
,
219 unsigned int cmd
, unsigned long arg
)
225 case CMD_COREB_INDEX
:
226 if (copy_from_user(&coreb_index
, (int *)arg
, sizeof(int))) {
231 spin_lock_irq(&coreb_lock
);
232 switch (coreb_index
) {
234 coreb_base
= 0xff600000;
238 coreb_base
= 0xff610000;
242 coreb_base
= 0xff500000;
246 coreb_base
= 0xff400000;
253 spin_unlock_irq(&coreb_lock
);
255 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
257 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
259 case CMD_COREB_START
:
260 spin_lock_irq(&coreb_lock
);
261 if (coreb_status
& COREB_IS_RUNNING
) {
265 printk(KERN_INFO
"Starting Core B\n");
266 coreb_status
|= COREB_IS_RUNNING
;
267 bfin_write_SICA_SYSCR(bfin_read_SICA_SYSCR() & ~0x0020);
269 spin_lock_irq(&coreb_lock
);
271 #if defined(CONFIG_BF561_COREB_RESET)
273 spin_lock_irq(&coreb_lock
);
274 printk(KERN_INFO
"Stopping Core B\n");
275 bfin_write_SICA_SYSCR(bfin_read_SICA_SYSCR() | 0x0020);
276 bfin_write_SICB_SYSCR(bfin_read_SICB_SYSCR() | 0x0080);
277 coreb_status
&= ~COREB_IS_RUNNING
;
278 spin_lock_irq(&coreb_lock
);
280 case CMD_COREB_RESET
:
281 printk(KERN_INFO
"Resetting Core B\n");
282 bfin_write_SICB_SYSCR(bfin_read_SICB_SYSCR() | 0x0080);
290 static struct file_operations coreb_fops
= {
291 .owner
= THIS_MODULE
,
292 .llseek
= coreb_lseek
,
294 .write
= coreb_write
,
295 .ioctl
= coreb_ioctl
,
297 .release
= coreb_release
300 static struct miscdevice coreb_dev
= {
306 static ssize_t
coreb_show_status(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
309 "Base Address:\t0x%08lx\n"
311 "SICA_SYSCR:\t%04x\n"
312 "SICB_SYSCR:\t%04x\n"
314 "IRQ Status:\tCore A\t\tCore B\n"
315 "ISR0:\t\t%08x\t\t%08x\n"
316 "ISR1:\t\t%08x\t\t%08x\n"
317 "IMASK0:\t\t%08x\t\t%08x\n"
318 "IMASK1:\t\t%08x\t\t%08x\n",
320 coreb_status
& COREB_IS_RUNNING
? "running" : "stalled",
321 bfin_read_SICA_SYSCR(), bfin_read_SICB_SYSCR(),
322 bfin_read_SICA_ISR0(), bfin_read_SICB_ISR0(),
323 bfin_read_SICA_ISR1(), bfin_read_SICB_ISR0(),
324 bfin_read_SICA_IMASK0(), bfin_read_SICB_IMASK0(),
325 bfin_read_SICA_IMASK1(), bfin_read_SICB_IMASK1());
328 static DEVICE_ATTR(coreb_status
, S_IRUGO
, coreb_show_status
, NULL
);
330 int __init
bf561_coreb_init(void)
332 init_waitqueue_head(&coreb_dma_wait
);
334 spin_lock_init(&coreb_lock
);
335 /* Request the core memory regions for Core B */
336 if (request_mem_region(0xff600000, 0x4000,
337 "Core B - Instruction SRAM") == NULL
)
340 if (request_mem_region(0xFF610000, 0x4000,
341 "Core B - Instruction SRAM") == NULL
)
342 goto release_instruction_a_sram
;
344 if (request_mem_region(0xFF500000, 0x8000,
345 "Core B - Data Bank B SRAM") == NULL
)
346 goto release_instruction_b_sram
;
348 if (request_mem_region(0xff400000, 0x8000,
349 "Core B - Data Bank A SRAM") == NULL
)
350 goto release_data_b_sram
;
352 if (request_dma(CH_MEM_STREAM2_DEST
, "Core B - DMA Destination") < 0)
353 goto release_data_a_sram
;
355 if (request_dma(CH_MEM_STREAM2_SRC
, "Core B - DMA Source") < 0)
356 goto release_dma_dest
;
358 set_dma_callback(CH_MEM_STREAM2_DEST
, coreb_dma_interrupt
, NULL
);
360 misc_register(&coreb_dev
);
362 if (device_create_file(coreb_dev
.this_device
, &dev_attr_coreb_status
))
363 goto release_dma_src
;
365 printk(KERN_INFO
"BF561 Core B driver %s initialized.\n", MODULE_VER
);
369 free_dma(CH_MEM_STREAM2_SRC
);
371 free_dma(CH_MEM_STREAM2_DEST
);
373 release_mem_region(0xff400000, 0x8000);
375 release_mem_region(0xff500000, 0x8000);
376 release_instruction_b_sram
:
377 release_mem_region(0xff610000, 0x4000);
378 release_instruction_a_sram
:
379 release_mem_region(0xff600000, 0x4000);
384 void __exit
bf561_coreb_exit(void)
386 device_remove_file(coreb_dev
.this_device
, &dev_attr_coreb_status
);
387 misc_deregister(&coreb_dev
);
389 release_mem_region(0xff610000, 0x4000);
390 release_mem_region(0xff600000, 0x4000);
391 release_mem_region(0xff500000, 0x8000);
392 release_mem_region(0xff400000, 0x8000);
394 free_dma(CH_MEM_STREAM2_DEST
);
395 free_dma(CH_MEM_STREAM2_SRC
);
398 module_init(bf561_coreb_init
);
399 module_exit(bf561_coreb_exit
);
401 MODULE_AUTHOR("Bas Vermeulen <bvermeul@blackstar.xs4all.nl>");
402 MODULE_DESCRIPTION("BF561 Core B Support");