2 * File: arch/blackfin/mm/blackfin_sram.c
7 * Description: SRAM driver for Blackfin ADSP-BF5xx
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
30 #include <linux/autoconf.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/miscdevice.h>
35 #include <linux/ioport.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/proc_fs.h>
40 #include <linux/spinlock.h>
41 #include <linux/rtc.h>
42 #include <asm/blackfin.h>
43 #include "blackfin_sram.h"
45 spinlock_t l1sram_lock
, l1_data_sram_lock
, l1_inst_sram_lock
;
47 #if CONFIG_L1_MAX_PIECE < 16
48 #undef CONFIG_L1_MAX_PIECE
49 #define CONFIG_L1_MAX_PIECE 16
52 #if CONFIG_L1_MAX_PIECE > 1024
53 #undef CONFIG_L1_MAX_PIECE
54 #define CONFIG_L1_MAX_PIECE 1024
57 #define SRAM_SLT_NULL 0
58 #define SRAM_SLT_FREE 1
59 #define SRAM_SLT_ALLOCATED 2
61 /* the data structure for L1 scratchpad and DATA SRAM */
62 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 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 l1_data_sram_init(void)
99 #if L1_DATA_A_LENGTH != 0
100 printk(KERN_INFO
"Blackfin DATA_A SRAM: %d KB\n",
101 L1_DATA_A_LENGTH
>> 10);
103 memset(&l1_data_A_sram
, 0x00, sizeof(l1_data_A_sram
));
104 l1_data_A_sram
[0].paddr
= (void*)L1_DATA_A_START
+
105 (_ebss_l1
- _sdata_l1
);
106 l1_data_A_sram
[0].size
= L1_DATA_A_LENGTH
- (_ebss_l1
- _sdata_l1
);
107 l1_data_A_sram
[0].flag
= SRAM_SLT_FREE
;
109 #if L1_DATA_B_LENGTH != 0
110 printk(KERN_INFO
"Blackfin DATA_B SRAM: %d KB\n",
111 L1_DATA_B_LENGTH
>> 10);
113 memset(&l1_data_B_sram
, 0x00, sizeof(l1_data_B_sram
));
114 l1_data_B_sram
[0].paddr
= (void*)L1_DATA_B_START
;
115 l1_data_B_sram
[0].size
= L1_DATA_B_LENGTH
;
116 l1_data_B_sram
[0].flag
= SRAM_SLT_FREE
;
119 /* mutex initialize */
120 spin_lock_init(&l1_data_sram_lock
);
123 void l1_inst_sram_init(void)
125 #if L1_CODE_LENGTH != 0
126 printk(KERN_INFO
"Blackfin Instruction SRAM: %d KB\n",
127 L1_CODE_LENGTH
>> 10);
129 memset(&l1_inst_sram
, 0x00, sizeof(l1_inst_sram
));
130 l1_inst_sram
[0].paddr
= (void*)L1_CODE_START
+ (_etext_l1
- _stext_l1
);
131 l1_inst_sram
[0].size
= L1_CODE_LENGTH
- (_etext_l1
- _stext_l1
);
132 l1_inst_sram
[0].flag
= SRAM_SLT_FREE
;
135 /* mutex initialize */
136 spin_lock_init(&l1_inst_sram_lock
);
139 /* L1 memory allocate function */
140 static void *_l1_sram_alloc(size_t size
, struct l1_sram_piece
*pfree
, int count
)
149 size
= (size
+ 3) & ~3;
151 /* not use the good method to match the best slot !!! */
152 /* search an available memeory slot */
153 for (i
= 0; i
< count
; i
++) {
154 if ((pfree
[i
].flag
== SRAM_SLT_FREE
)
155 && (pfree
[i
].size
>= size
)) {
156 addr
= pfree
[i
].paddr
;
157 pfree
[i
].flag
= SRAM_SLT_ALLOCATED
;
165 /* updated the NULL memeory slot !!! */
166 if (pfree
[i
].size
> size
) {
167 for (i
= 0; i
< count
; i
++) {
168 if (pfree
[i
].flag
== SRAM_SLT_NULL
) {
169 pfree
[i
].flag
= SRAM_SLT_FREE
;
170 pfree
[i
].paddr
= addr
+ size
;
171 pfree
[i
].size
= pfree
[index
].size
- size
;
172 pfree
[index
].size
= size
;
181 /* Allocate the largest available block. */
182 static void *_l1_sram_alloc_max(struct l1_sram_piece
*pfree
, int count
,
183 unsigned long *psize
)
185 unsigned long best
= 0;
189 /* search an available memeory slot */
190 for (i
= 0; i
< count
; i
++) {
191 if (pfree
[i
].flag
== SRAM_SLT_FREE
&& pfree
[i
].size
> best
) {
192 addr
= pfree
[i
].paddr
;
194 best
= pfree
[i
].size
;
201 pfree
[index
].flag
= SRAM_SLT_ALLOCATED
;
205 /* L1 memory free function */
206 static int _l1_sram_free(const void *addr
,
207 struct l1_sram_piece
*pfree
, int count
)
211 /* search the relevant memory slot */
212 for (i
= 0; i
< count
; i
++) {
213 if (pfree
[i
].paddr
== addr
) {
214 if (pfree
[i
].flag
!= SRAM_SLT_ALLOCATED
) {
225 pfree
[index
].flag
= SRAM_SLT_FREE
;
227 /* link the next address slot */
228 for (i
= 0; i
< count
; i
++) {
229 if (((pfree
[index
].paddr
+ pfree
[index
].size
) == pfree
[i
].paddr
)
230 && (pfree
[i
].flag
== SRAM_SLT_FREE
)) {
231 pfree
[i
].flag
= SRAM_SLT_NULL
;
232 pfree
[index
].size
+= pfree
[i
].size
;
233 pfree
[index
].flag
= SRAM_SLT_FREE
;
238 /* link the last address slot */
239 for (i
= 0; i
< count
; i
++) {
240 if (((pfree
[i
].paddr
+ pfree
[i
].size
) == pfree
[index
].paddr
) &&
241 (pfree
[i
].flag
== SRAM_SLT_FREE
)) {
242 pfree
[index
].flag
= SRAM_SLT_NULL
;
243 pfree
[i
].size
+= pfree
[index
].size
;
251 int sram_free(const void *addr
)
254 #if L1_CODE_LENGTH != 0
255 else if (addr
>= (void *)L1_CODE_START
256 && addr
< (void *)(L1_CODE_START
+ L1_CODE_LENGTH
))
257 return l1_inst_sram_free(addr
);
259 #if L1_DATA_A_LENGTH != 0
260 else if (addr
>= (void *)L1_DATA_A_START
261 && addr
< (void *)(L1_DATA_A_START
+ L1_DATA_A_LENGTH
))
262 return l1_data_A_sram_free(addr
);
264 #if L1_DATA_B_LENGTH != 0
265 else if (addr
>= (void *)L1_DATA_B_START
266 && addr
< (void *)(L1_DATA_B_START
+ L1_DATA_B_LENGTH
))
267 return l1_data_B_sram_free(addr
);
272 EXPORT_SYMBOL(sram_free
);
274 void *l1_data_A_sram_alloc(size_t size
)
279 /* add mutex operation */
280 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
282 #if L1_DATA_A_LENGTH != 0
283 addr
= _l1_sram_alloc(size
, l1_data_A_sram
, ARRAY_SIZE(l1_data_A_sram
));
286 /* add mutex operation */
287 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
289 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
290 (long unsigned int)addr
, size
);
294 EXPORT_SYMBOL(l1_data_A_sram_alloc
);
296 int l1_data_A_sram_free(const void *addr
)
301 /* add mutex operation */
302 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
304 #if L1_DATA_A_LENGTH != 0
305 ret
= _l1_sram_free(addr
,
306 l1_data_A_sram
, ARRAY_SIZE(l1_data_A_sram
));
311 /* add mutex operation */
312 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
316 EXPORT_SYMBOL(l1_data_A_sram_free
);
318 void *l1_data_B_sram_alloc(size_t size
)
320 #if L1_DATA_B_LENGTH != 0
324 /* add mutex operation */
325 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
327 addr
= _l1_sram_alloc(size
, l1_data_B_sram
, ARRAY_SIZE(l1_data_B_sram
));
329 /* add mutex operation */
330 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
332 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
333 (long unsigned int)addr
, size
);
340 EXPORT_SYMBOL(l1_data_B_sram_alloc
);
342 int l1_data_B_sram_free(const void *addr
)
344 #if L1_DATA_B_LENGTH != 0
348 /* add mutex operation */
349 spin_lock_irqsave(&l1_data_sram_lock
, flags
);
351 ret
= _l1_sram_free(addr
, l1_data_B_sram
, ARRAY_SIZE(l1_data_B_sram
));
353 /* add mutex operation */
354 spin_unlock_irqrestore(&l1_data_sram_lock
, flags
);
361 EXPORT_SYMBOL(l1_data_B_sram_free
);
363 void *l1_data_sram_alloc(size_t size
)
365 void *addr
= l1_data_A_sram_alloc(size
);
368 addr
= l1_data_B_sram_alloc(size
);
372 EXPORT_SYMBOL(l1_data_sram_alloc
);
374 void *l1_data_sram_zalloc(size_t size
)
376 void *addr
= l1_data_sram_alloc(size
);
379 memset(addr
, 0x00, size
);
383 EXPORT_SYMBOL(l1_data_sram_zalloc
);
385 int l1_data_sram_free(const void *addr
)
388 ret
= l1_data_A_sram_free(addr
);
390 ret
= l1_data_B_sram_free(addr
);
393 EXPORT_SYMBOL(l1_data_sram_free
);
395 void *l1_inst_sram_alloc(size_t size
)
397 #if L1_DATA_A_LENGTH != 0
401 /* add mutex operation */
402 spin_lock_irqsave(&l1_inst_sram_lock
, flags
);
404 addr
= _l1_sram_alloc(size
, l1_inst_sram
, ARRAY_SIZE(l1_inst_sram
));
406 /* add mutex operation */
407 spin_unlock_irqrestore(&l1_inst_sram_lock
, flags
);
409 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
410 (long unsigned int)addr
, size
);
417 EXPORT_SYMBOL(l1_inst_sram_alloc
);
419 int l1_inst_sram_free(const void *addr
)
421 #if L1_CODE_LENGTH != 0
425 /* add mutex operation */
426 spin_lock_irqsave(&l1_inst_sram_lock
, flags
);
428 ret
= _l1_sram_free(addr
, l1_inst_sram
, ARRAY_SIZE(l1_inst_sram
));
430 /* add mutex operation */
431 spin_unlock_irqrestore(&l1_inst_sram_lock
, flags
);
438 EXPORT_SYMBOL(l1_inst_sram_free
);
440 /* L1 Scratchpad memory allocate function */
441 void *l1sram_alloc(size_t size
)
446 /* add mutex operation */
447 spin_lock_irqsave(&l1sram_lock
, flags
);
449 addr
= _l1_sram_alloc(size
, l1_ssram
, ARRAY_SIZE(l1_ssram
));
451 /* add mutex operation */
452 spin_unlock_irqrestore(&l1sram_lock
, flags
);
457 /* L1 Scratchpad memory allocate function */
458 void *l1sram_alloc_max(size_t *psize
)
463 /* add mutex operation */
464 spin_lock_irqsave(&l1sram_lock
, flags
);
466 addr
= _l1_sram_alloc_max(l1_ssram
, ARRAY_SIZE(l1_ssram
), psize
);
468 /* add mutex operation */
469 spin_unlock_irqrestore(&l1sram_lock
, flags
);
474 /* L1 Scratchpad memory free function */
475 int l1sram_free(const void *addr
)
480 /* add mutex operation */
481 spin_lock_irqsave(&l1sram_lock
, flags
);
483 ret
= _l1_sram_free(addr
, l1_ssram
, ARRAY_SIZE(l1_ssram
));
485 /* add mutex operation */
486 spin_unlock_irqrestore(&l1sram_lock
, flags
);
491 int sram_free_with_lsl(const void *addr
)
493 struct sram_list_struct
*lsl
, **tmp
;
494 struct mm_struct
*mm
= current
->mm
;
496 for (tmp
= &mm
->context
.sram_list
; *tmp
; tmp
= &(*tmp
)->next
)
497 if ((*tmp
)->addr
== addr
)
508 EXPORT_SYMBOL(sram_free_with_lsl
);
510 void *sram_alloc_with_lsl(size_t size
, unsigned long flags
)
513 struct sram_list_struct
*lsl
= NULL
;
514 struct mm_struct
*mm
= current
->mm
;
516 lsl
= kmalloc(sizeof(struct sram_list_struct
), GFP_KERNEL
);
519 memset(lsl
, 0, sizeof(*lsl
));
521 if (flags
& L1_INST_SRAM
)
522 addr
= l1_inst_sram_alloc(size
);
524 if (addr
== NULL
&& (flags
& L1_DATA_A_SRAM
))
525 addr
= l1_data_A_sram_alloc(size
);
527 if (addr
== NULL
&& (flags
& L1_DATA_B_SRAM
))
528 addr
= l1_data_B_sram_alloc(size
);
536 lsl
->next
= mm
->context
.sram_list
;
537 mm
->context
.sram_list
= lsl
;
540 EXPORT_SYMBOL(sram_alloc_with_lsl
);