2 * File: arch/blackfin/mm/blackfin_sram.c
7 * Description: SRAM driver for Blackfin ADSP-BF5xx
10 * Copyright 2004-2007 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
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/miscdevice.h>
34 #include <linux/ioport.h>
35 #include <linux/fcntl.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/proc_fs.h>
39 #include <linux/spinlock.h>
40 #include <linux/rtc.h>
41 #include <asm/blackfin.h>
42 #include "blackfin_sram.h"
44 spinlock_t l1sram_lock
, l1_data_sram_lock
, l1_inst_sram_lock
;
46 #if CONFIG_L1_MAX_PIECE < 16
47 #undef CONFIG_L1_MAX_PIECE
48 #define CONFIG_L1_MAX_PIECE 16
51 #if CONFIG_L1_MAX_PIECE > 1024
52 #undef CONFIG_L1_MAX_PIECE
53 #define CONFIG_L1_MAX_PIECE 1024
56 #define SRAM_SLT_NULL 0
57 #define SRAM_SLT_FREE 1
58 #define SRAM_SLT_ALLOCATED 2
60 /* the data structure for L1 scratchpad and DATA SRAM */
61 struct l1_sram_piece
{
68 static struct l1_sram_piece l1_ssram
[CONFIG_L1_MAX_PIECE
];
70 #if L1_DATA_A_LENGTH != 0
71 static struct l1_sram_piece l1_data_A_sram
[CONFIG_L1_MAX_PIECE
];
74 #if L1_DATA_B_LENGTH != 0
75 static struct l1_sram_piece l1_data_B_sram
[CONFIG_L1_MAX_PIECE
];
78 #if L1_CODE_LENGTH != 0
79 static struct l1_sram_piece l1_inst_sram
[CONFIG_L1_MAX_PIECE
];
82 /* L1 Scratchpad SRAM initialization function */
83 void __init
l1sram_init(void)
85 printk(KERN_INFO
"Blackfin Scratchpad data SRAM: %d KB\n",
86 L1_SCRATCH_LENGTH
>> 10);
88 memset(&l1_ssram
, 0x00, sizeof(l1_ssram
));
89 l1_ssram
[0].paddr
= (void *)L1_SCRATCH_START
;
90 l1_ssram
[0].size
= L1_SCRATCH_LENGTH
;
91 l1_ssram
[0].flag
= SRAM_SLT_FREE
;
93 /* mutex initialize */
94 spin_lock_init(&l1sram_lock
);
97 void __init
l1_data_sram_init(void)
99 #if L1_DATA_A_LENGTH != 0
100 memset(&l1_data_A_sram
, 0x00, sizeof(l1_data_A_sram
));
101 l1_data_A_sram
[0].paddr
= (void *)L1_DATA_A_START
+
102 (_ebss_l1
- _sdata_l1
);
103 l1_data_A_sram
[0].size
= L1_DATA_A_LENGTH
- (_ebss_l1
- _sdata_l1
);
104 l1_data_A_sram
[0].flag
= SRAM_SLT_FREE
;
106 printk(KERN_INFO
"Blackfin Data A SRAM: %d KB (%d KB free)\n",
107 L1_DATA_A_LENGTH
>> 10, l1_data_A_sram
[0].size
>> 10);
109 #if L1_DATA_B_LENGTH != 0
110 memset(&l1_data_B_sram
, 0x00, sizeof(l1_data_B_sram
));
111 l1_data_B_sram
[0].paddr
= (void *)L1_DATA_B_START
+
112 (_ebss_b_l1
- _sdata_b_l1
);
113 l1_data_B_sram
[0].size
= L1_DATA_B_LENGTH
- (_ebss_b_l1
- _sdata_b_l1
);
114 l1_data_B_sram
[0].flag
= SRAM_SLT_FREE
;
116 printk(KERN_INFO
"Blackfin Data B SRAM: %d KB (%d KB free)\n",
117 L1_DATA_B_LENGTH
>> 10, l1_data_B_sram
[0].size
>> 10);
120 /* mutex initialize */
121 spin_lock_init(&l1_data_sram_lock
);
124 void __init
l1_inst_sram_init(void)
126 #if L1_CODE_LENGTH != 0
127 memset(&l1_inst_sram
, 0x00, sizeof(l1_inst_sram
));
128 l1_inst_sram
[0].paddr
= (void *)L1_CODE_START
+ (_etext_l1
- _stext_l1
);
129 l1_inst_sram
[0].size
= L1_CODE_LENGTH
- (_etext_l1
- _stext_l1
);
130 l1_inst_sram
[0].flag
= SRAM_SLT_FREE
;
132 printk(KERN_INFO
"Blackfin Instruction SRAM: %d KB (%d KB free)\n",
133 L1_CODE_LENGTH
>> 10, l1_inst_sram
[0].size
>> 10);
136 /* mutex initialize */
137 spin_lock_init(&l1_inst_sram_lock
);
140 /* L1 memory allocate function */
141 static void *_l1_sram_alloc(size_t size
, struct l1_sram_piece
*pfree
, int count
)
150 size
= (size
+ 3) & ~3;
152 /* not use the good method to match the best slot !!! */
153 /* search an available memory slot */
154 for (i
= 0; i
< count
; i
++) {
155 if ((pfree
[i
].flag
== SRAM_SLT_FREE
)
156 && (pfree
[i
].size
>= size
)) {
157 addr
= pfree
[i
].paddr
;
158 pfree
[i
].flag
= SRAM_SLT_ALLOCATED
;
159 pfree
[i
].pid
= current
->pid
;
167 /* updated the NULL memory slot !!! */
168 if (pfree
[i
].size
> size
) {
169 for (i
= 0; i
< count
; i
++) {
170 if (pfree
[i
].flag
== SRAM_SLT_NULL
) {
172 pfree
[i
].flag
= SRAM_SLT_FREE
;
173 pfree
[i
].paddr
= addr
+ size
;
174 pfree
[i
].size
= pfree
[index
].size
- size
;
175 pfree
[index
].size
= size
;
184 /* Allocate the largest available block. */
185 static void *_l1_sram_alloc_max(struct l1_sram_piece
*pfree
, int count
,
186 unsigned long *psize
)
188 unsigned long best
= 0;
192 /* search an available memory slot */
193 for (i
= 0; i
< count
; i
++) {
194 if (pfree
[i
].flag
== SRAM_SLT_FREE
&& pfree
[i
].size
> best
) {
195 addr
= pfree
[i
].paddr
;
197 best
= pfree
[i
].size
;
204 pfree
[index
].pid
= current
->pid
;
205 pfree
[index
].flag
= SRAM_SLT_ALLOCATED
;
209 /* L1 memory free function */
210 static int _l1_sram_free(const void *addr
,
211 struct l1_sram_piece
*pfree
,
216 /* search the relevant memory slot */
217 for (i
= 0; i
< count
; i
++) {
218 if (pfree
[i
].paddr
== addr
) {
219 if (pfree
[i
].flag
!= SRAM_SLT_ALLOCATED
) {
230 pfree
[index
].pid
= 0;
231 pfree
[index
].flag
= SRAM_SLT_FREE
;
233 /* link the next address slot */
234 for (i
= 0; i
< count
; i
++) {
235 if (((pfree
[index
].paddr
+ pfree
[index
].size
) == pfree
[i
].paddr
)
236 && (pfree
[i
].flag
== SRAM_SLT_FREE
)) {
238 pfree
[i
].flag
= SRAM_SLT_NULL
;
239 pfree
[index
].size
+= pfree
[i
].size
;
240 pfree
[index
].flag
= SRAM_SLT_FREE
;
245 /* link the last address slot */
246 for (i
= 0; i
< count
; i
++) {
247 if (((pfree
[i
].paddr
+ pfree
[i
].size
) == pfree
[index
].paddr
) &&
248 (pfree
[i
].flag
== SRAM_SLT_FREE
)) {
249 pfree
[index
].flag
= SRAM_SLT_NULL
;
250 pfree
[i
].size
+= pfree
[index
].size
;
258 int sram_free(const void *addr
)
261 #if L1_CODE_LENGTH != 0
262 else if (addr
>= (void *)L1_CODE_START
263 && addr
< (void *)(L1_CODE_START
+ L1_CODE_LENGTH
))
264 return l1_inst_sram_free(addr
);
266 #if L1_DATA_A_LENGTH != 0
267 else if (addr
>= (void *)L1_DATA_A_START
268 && addr
< (void *)(L1_DATA_A_START
+ L1_DATA_A_LENGTH
))
269 return l1_data_A_sram_free(addr
);
271 #if L1_DATA_B_LENGTH != 0
272 else if (addr
>= (void *)L1_DATA_B_START
273 && addr
< (void *)(L1_DATA_B_START
+ L1_DATA_B_LENGTH
))
274 return l1_data_B_sram_free(addr
);
279 EXPORT_SYMBOL(sram_free
);
281 void *l1_data_A_sram_alloc(size_t size
)
286 /* add mutex operation */
287 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
289 #if L1_DATA_A_LENGTH != 0
290 addr
= _l1_sram_alloc(size
, l1_data_A_sram
, ARRAY_SIZE(l1_data_A_sram
));
293 /* add mutex operation */
294 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
296 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
297 (long unsigned int)addr
, size
);
301 EXPORT_SYMBOL(l1_data_A_sram_alloc
);
303 int l1_data_A_sram_free(const void *addr
)
308 /* add mutex operation */
309 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
311 #if L1_DATA_A_LENGTH != 0
312 ret
= _l1_sram_free(addr
,
313 l1_data_A_sram
, ARRAY_SIZE(l1_data_A_sram
));
318 /* add mutex operation */
319 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
323 EXPORT_SYMBOL(l1_data_A_sram_free
);
325 void *l1_data_B_sram_alloc(size_t size
)
327 #if L1_DATA_B_LENGTH != 0
331 /* add mutex operation */
332 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
334 addr
= _l1_sram_alloc(size
, l1_data_B_sram
, ARRAY_SIZE(l1_data_B_sram
));
336 /* add mutex operation */
337 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
339 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
340 (long unsigned int)addr
, size
);
347 EXPORT_SYMBOL(l1_data_B_sram_alloc
);
349 int l1_data_B_sram_free(const void *addr
)
351 #if L1_DATA_B_LENGTH != 0
355 /* add mutex operation */
356 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
358 ret
= _l1_sram_free(addr
, l1_data_B_sram
, ARRAY_SIZE(l1_data_B_sram
));
360 /* add mutex operation */
361 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
368 EXPORT_SYMBOL(l1_data_B_sram_free
);
370 void *l1_data_sram_alloc(size_t size
)
372 void *addr
= l1_data_A_sram_alloc(size
);
375 addr
= l1_data_B_sram_alloc(size
);
379 EXPORT_SYMBOL(l1_data_sram_alloc
);
381 void *l1_data_sram_zalloc(size_t size
)
383 void *addr
= l1_data_sram_alloc(size
);
386 memset(addr
, 0x00, size
);
390 EXPORT_SYMBOL(l1_data_sram_zalloc
);
392 int l1_data_sram_free(const void *addr
)
395 ret
= l1_data_A_sram_free(addr
);
397 ret
= l1_data_B_sram_free(addr
);
400 EXPORT_SYMBOL(l1_data_sram_free
);
402 void *l1_inst_sram_alloc(size_t size
)
404 #if L1_CODE_LENGTH != 0
408 /* add mutex operation */
409 spin_lock_irqsave(&l1_inst_sram_lock
, flags
);
411 addr
= _l1_sram_alloc(size
, l1_inst_sram
, ARRAY_SIZE(l1_inst_sram
));
413 /* add mutex operation */
414 spin_unlock_irqrestore(&l1_inst_sram_lock
, flags
);
416 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
417 (long unsigned int)addr
, size
);
424 EXPORT_SYMBOL(l1_inst_sram_alloc
);
426 int l1_inst_sram_free(const void *addr
)
428 #if L1_CODE_LENGTH != 0
432 /* add mutex operation */
433 spin_lock_irqsave(&l1_inst_sram_lock
, flags
);
435 ret
= _l1_sram_free(addr
, l1_inst_sram
, ARRAY_SIZE(l1_inst_sram
));
437 /* add mutex operation */
438 spin_unlock_irqrestore(&l1_inst_sram_lock
, flags
);
445 EXPORT_SYMBOL(l1_inst_sram_free
);
447 /* L1 Scratchpad memory allocate function */
448 void *l1sram_alloc(size_t size
)
453 /* add mutex operation */
454 spin_lock_irqsave(&l1sram_lock
, flags
);
456 addr
= _l1_sram_alloc(size
, l1_ssram
, ARRAY_SIZE(l1_ssram
));
458 /* add mutex operation */
459 spin_unlock_irqrestore(&l1sram_lock
, flags
);
464 /* L1 Scratchpad memory allocate function */
465 void *l1sram_alloc_max(size_t *psize
)
470 /* add mutex operation */
471 spin_lock_irqsave(&l1sram_lock
, flags
);
473 addr
= _l1_sram_alloc_max(l1_ssram
, ARRAY_SIZE(l1_ssram
), psize
);
475 /* add mutex operation */
476 spin_unlock_irqrestore(&l1sram_lock
, flags
);
481 /* L1 Scratchpad memory free function */
482 int l1sram_free(const void *addr
)
487 /* add mutex operation */
488 spin_lock_irqsave(&l1sram_lock
, flags
);
490 ret
= _l1_sram_free(addr
, l1_ssram
, ARRAY_SIZE(l1_ssram
));
492 /* add mutex operation */
493 spin_unlock_irqrestore(&l1sram_lock
, flags
);
498 int sram_free_with_lsl(const void *addr
)
500 struct sram_list_struct
*lsl
, **tmp
;
501 struct mm_struct
*mm
= current
->mm
;
503 for (tmp
= &mm
->context
.sram_list
; *tmp
; tmp
= &(*tmp
)->next
)
504 if ((*tmp
)->addr
== addr
)
515 EXPORT_SYMBOL(sram_free_with_lsl
);
517 void *sram_alloc_with_lsl(size_t size
, unsigned long flags
)
520 struct sram_list_struct
*lsl
= NULL
;
521 struct mm_struct
*mm
= current
->mm
;
523 lsl
= kzalloc(sizeof(struct sram_list_struct
), GFP_KERNEL
);
527 if (flags
& L1_INST_SRAM
)
528 addr
= l1_inst_sram_alloc(size
);
530 if (addr
== NULL
&& (flags
& L1_DATA_A_SRAM
))
531 addr
= l1_data_A_sram_alloc(size
);
533 if (addr
== NULL
&& (flags
& L1_DATA_B_SRAM
))
534 addr
= l1_data_B_sram_alloc(size
);
542 lsl
->next
= mm
->context
.sram_list
;
543 mm
->context
.sram_list
= lsl
;
546 EXPORT_SYMBOL(sram_alloc_with_lsl
);
548 #ifdef CONFIG_PROC_FS
549 /* Once we get a real allocator, we'll throw all of this away.
550 * Until then, we need some sort of visibility into the L1 alloc.
552 static void _l1sram_proc_read(char *buf
, int *len
, const char *desc
,
553 struct l1_sram_piece
*pfree
, const int array_size
)
557 *len
+= sprintf(&buf
[*len
], "--- L1 %-14s Size PID State\n", desc
);
558 for (i
= 0; i
< array_size
; ++i
) {
559 const char *alloc_type
;
560 switch (pfree
[i
].flag
) {
561 case SRAM_SLT_NULL
: alloc_type
= "NULL"; break;
562 case SRAM_SLT_FREE
: alloc_type
= "FREE"; break;
563 case SRAM_SLT_ALLOCATED
: alloc_type
= "ALLOCATED"; break;
564 default: alloc_type
= "????"; break;
566 *len
+= sprintf(&buf
[*len
], "%p-%p %8i %4i %s\n",
567 pfree
[i
].paddr
, pfree
[i
].paddr
+ pfree
[i
].size
,
568 pfree
[i
].size
, pfree
[i
].pid
, alloc_type
);
571 static int l1sram_proc_read(char *buf
, char **start
, off_t offset
, int count
,
572 int *eof
, void *data
)
576 _l1sram_proc_read(buf
, &len
, "Scratchpad",
577 l1_ssram
, ARRAY_SIZE(l1_ssram
));
578 #if L1_DATA_A_LENGTH != 0
579 _l1sram_proc_read(buf
, &len
, "Data A",
580 l1_data_A_sram
, ARRAY_SIZE(l1_data_A_sram
));
582 #if L1_DATA_B_LENGTH != 0
583 _l1sram_proc_read(buf
, &len
, "Data B",
584 l1_data_B_sram
, ARRAY_SIZE(l1_data_B_sram
));
586 #if L1_CODE_LENGTH != 0
587 _l1sram_proc_read(buf
, &len
, "Instruction",
588 l1_inst_sram
, ARRAY_SIZE(l1_inst_sram
));
594 static int __init
l1sram_proc_init(void)
596 struct proc_dir_entry
*ptr
;
597 ptr
= create_proc_entry("sram", S_IFREG
| S_IRUGO
, NULL
);
599 printk(KERN_WARNING
"unable to create /proc/sram\n");
602 ptr
->owner
= THIS_MODULE
;
603 ptr
->read_proc
= l1sram_proc_read
;
606 late_initcall(l1sram_proc_init
);