RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / blackfin / mm / blackfin_sram.c
bloba63bcda0958eb5ca9960e6459fad8e7644507482
1 /*
2 * File: arch/blackfin/mm/blackfin_sram.c
3 * Based on:
4 * Author:
6 * Created:
7 * Description: SRAM driver for Blackfin ADSP-BF5xx
9 * Modified:
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/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
50 #endif
52 #if CONFIG_L1_MAX_PIECE > 1024
53 #undef CONFIG_L1_MAX_PIECE
54 #define CONFIG_L1_MAX_PIECE 1024
55 #endif
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 {
63 void *paddr;
64 int size;
65 int flag;
66 pid_t pid;
69 static struct l1_sram_piece l1_ssram[CONFIG_L1_MAX_PIECE];
71 #if L1_DATA_A_LENGTH != 0
72 static struct l1_sram_piece l1_data_A_sram[CONFIG_L1_MAX_PIECE];
73 #endif
75 #if L1_DATA_B_LENGTH != 0
76 static struct l1_sram_piece l1_data_B_sram[CONFIG_L1_MAX_PIECE];
77 #endif
79 #if L1_CODE_LENGTH != 0
80 static struct l1_sram_piece l1_inst_sram[CONFIG_L1_MAX_PIECE];
81 #endif
83 /* L1 Scratchpad SRAM initialization function */
84 void __init l1sram_init(void)
86 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
87 L1_SCRATCH_LENGTH >> 10);
89 memset(&l1_ssram, 0x00, sizeof(l1_ssram));
90 l1_ssram[0].paddr = (void*)L1_SCRATCH_START;
91 l1_ssram[0].size = L1_SCRATCH_LENGTH;
92 l1_ssram[0].flag = SRAM_SLT_FREE;
94 /* mutex initialize */
95 spin_lock_init(&l1sram_lock);
98 void __init l1_data_sram_init(void)
100 #if L1_DATA_A_LENGTH != 0
101 memset(&l1_data_A_sram, 0x00, sizeof(l1_data_A_sram));
102 l1_data_A_sram[0].paddr = (void *)L1_DATA_A_START +
103 (_ebss_l1 - _sdata_l1);
104 l1_data_A_sram[0].size = L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
105 l1_data_A_sram[0].flag = SRAM_SLT_FREE;
107 printk(KERN_INFO "Blackfin Data A SRAM: %d KB (%d KB free)\n",
108 L1_DATA_A_LENGTH >> 10, l1_data_A_sram[0].size >> 10);
109 #endif
110 #if L1_DATA_B_LENGTH != 0
111 memset(&l1_data_B_sram, 0x00, sizeof(l1_data_B_sram));
112 l1_data_B_sram[0].paddr = (void *)L1_DATA_B_START +
113 (_ebss_b_l1 - _sdata_b_l1);
114 l1_data_B_sram[0].size = L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
115 l1_data_B_sram[0].flag = SRAM_SLT_FREE;
117 printk(KERN_INFO "Blackfin Data B SRAM: %d KB (%d KB free)\n",
118 L1_DATA_B_LENGTH >> 10, l1_data_B_sram[0].size >> 10);
119 #endif
121 /* mutex initialize */
122 spin_lock_init(&l1_data_sram_lock);
125 void __init l1_inst_sram_init(void)
127 #if L1_CODE_LENGTH != 0
128 memset(&l1_inst_sram, 0x00, sizeof(l1_inst_sram));
129 l1_inst_sram[0].paddr = (void*)L1_CODE_START + (_etext_l1 - _stext_l1);
130 l1_inst_sram[0].size = L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
131 l1_inst_sram[0].flag = SRAM_SLT_FREE;
133 printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
134 L1_CODE_LENGTH >> 10, l1_inst_sram[0].size >> 10);
135 #endif
137 /* mutex initialize */
138 spin_lock_init(&l1_inst_sram_lock);
141 /* L1 memory allocate function */
142 static void *_l1_sram_alloc(size_t size, struct l1_sram_piece *pfree, int count)
144 int i, index = 0;
145 void *addr = NULL;
147 if (size <= 0)
148 return NULL;
150 /* Align the size */
151 size = (size + 3) & ~3;
153 /* not use the good method to match the best slot !!! */
154 /* search an available memory slot */
155 for (i = 0; i < count; i++) {
156 if ((pfree[i].flag == SRAM_SLT_FREE)
157 && (pfree[i].size >= size)) {
158 addr = pfree[i].paddr;
159 pfree[i].flag = SRAM_SLT_ALLOCATED;
160 pfree[i].pid = current->pid;
161 index = i;
162 break;
165 if (i >= count)
166 return NULL;
168 /* updated the NULL memory slot !!! */
169 if (pfree[i].size > size) {
170 for (i = 0; i < count; i++) {
171 if (pfree[i].flag == SRAM_SLT_NULL) {
172 pfree[i].pid = 0;
173 pfree[i].flag = SRAM_SLT_FREE;
174 pfree[i].paddr = addr + size;
175 pfree[i].size = pfree[index].size - size;
176 pfree[index].size = size;
177 break;
182 return addr;
185 /* Allocate the largest available block. */
186 static void *_l1_sram_alloc_max(struct l1_sram_piece *pfree, int count,
187 unsigned long *psize)
189 unsigned long best = 0;
190 int i, index = -1;
191 void *addr = NULL;
193 /* search an available memory slot */
194 for (i = 0; i < count; i++) {
195 if (pfree[i].flag == SRAM_SLT_FREE && pfree[i].size > best) {
196 addr = pfree[i].paddr;
197 index = i;
198 best = pfree[i].size;
201 if (index < 0)
202 return NULL;
203 *psize = best;
205 pfree[index].pid = current->pid;
206 pfree[index].flag = SRAM_SLT_ALLOCATED;
207 return addr;
210 /* L1 memory free function */
211 static int _l1_sram_free(const void *addr,
212 struct l1_sram_piece *pfree,
213 int count)
215 int i, index = 0;
217 /* search the relevant memory slot */
218 for (i = 0; i < count; i++) {
219 if (pfree[i].paddr == addr) {
220 if (pfree[i].flag != SRAM_SLT_ALLOCATED) {
221 /* error log */
222 return -1;
224 index = i;
225 break;
228 if (i >= count)
229 return -1;
231 pfree[index].pid = 0;
232 pfree[index].flag = SRAM_SLT_FREE;
234 /* link the next address slot */
235 for (i = 0; i < count; i++) {
236 if (((pfree[index].paddr + pfree[index].size) == pfree[i].paddr)
237 && (pfree[i].flag == SRAM_SLT_FREE)) {
238 pfree[i].pid = 0;
239 pfree[i].flag = SRAM_SLT_NULL;
240 pfree[index].size += pfree[i].size;
241 pfree[index].flag = SRAM_SLT_FREE;
242 break;
246 /* link the last address slot */
247 for (i = 0; i < count; i++) {
248 if (((pfree[i].paddr + pfree[i].size) == pfree[index].paddr) &&
249 (pfree[i].flag == SRAM_SLT_FREE)) {
250 pfree[index].flag = SRAM_SLT_NULL;
251 pfree[i].size += pfree[index].size;
252 break;
256 return 0;
259 int sram_free(const void *addr)
261 if (0) {}
262 #if L1_CODE_LENGTH != 0
263 else if (addr >= (void *)L1_CODE_START
264 && addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
265 return l1_inst_sram_free(addr);
266 #endif
267 #if L1_DATA_A_LENGTH != 0
268 else if (addr >= (void *)L1_DATA_A_START
269 && addr < (void *)(L1_DATA_A_START + L1_DATA_A_LENGTH))
270 return l1_data_A_sram_free(addr);
271 #endif
272 #if L1_DATA_B_LENGTH != 0
273 else if (addr >= (void *)L1_DATA_B_START
274 && addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
275 return l1_data_B_sram_free(addr);
276 #endif
277 else
278 return -1;
280 EXPORT_SYMBOL(sram_free);
282 void *l1_data_A_sram_alloc(size_t size)
284 unsigned flags;
285 void *addr = NULL;
287 /* add mutex operation */
288 spin_lock_irqsave(&l1_data_sram_lock, flags);
290 #if L1_DATA_A_LENGTH != 0
291 addr = _l1_sram_alloc(size, l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
292 #endif
294 /* add mutex operation */
295 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
297 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
298 (long unsigned int)addr, size);
300 return addr;
302 EXPORT_SYMBOL(l1_data_A_sram_alloc);
304 int l1_data_A_sram_free(const void *addr)
306 unsigned flags;
307 int ret;
309 /* add mutex operation */
310 spin_lock_irqsave(&l1_data_sram_lock, flags);
312 #if L1_DATA_A_LENGTH != 0
313 ret = _l1_sram_free(addr,
314 l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
315 #else
316 ret = -1;
317 #endif
319 /* add mutex operation */
320 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
322 return ret;
324 EXPORT_SYMBOL(l1_data_A_sram_free);
326 void *l1_data_B_sram_alloc(size_t size)
328 #if L1_DATA_B_LENGTH != 0
329 unsigned flags;
330 void *addr;
332 /* add mutex operation */
333 spin_lock_irqsave(&l1_data_sram_lock, flags);
335 addr = _l1_sram_alloc(size, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
337 /* add mutex operation */
338 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
340 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
341 (long unsigned int)addr, size);
343 return addr;
344 #else
345 return NULL;
346 #endif
348 EXPORT_SYMBOL(l1_data_B_sram_alloc);
350 int l1_data_B_sram_free(const void *addr)
352 #if L1_DATA_B_LENGTH != 0
353 unsigned flags;
354 int ret;
356 /* add mutex operation */
357 spin_lock_irqsave(&l1_data_sram_lock, flags);
359 ret = _l1_sram_free(addr, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
361 /* add mutex operation */
362 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
364 return ret;
365 #else
366 return -1;
367 #endif
369 EXPORT_SYMBOL(l1_data_B_sram_free);
371 void *l1_data_sram_alloc(size_t size)
373 void *addr = l1_data_A_sram_alloc(size);
375 if (!addr)
376 addr = l1_data_B_sram_alloc(size);
378 return addr;
380 EXPORT_SYMBOL(l1_data_sram_alloc);
382 void *l1_data_sram_zalloc(size_t size)
384 void *addr = l1_data_sram_alloc(size);
386 if (addr)
387 memset(addr, 0x00, size);
389 return addr;
391 EXPORT_SYMBOL(l1_data_sram_zalloc);
393 int l1_data_sram_free(const void *addr)
395 int ret;
396 ret = l1_data_A_sram_free(addr);
397 if (ret == -1)
398 ret = l1_data_B_sram_free(addr);
399 return ret;
401 EXPORT_SYMBOL(l1_data_sram_free);
403 void *l1_inst_sram_alloc(size_t size)
405 #if L1_DATA_A_LENGTH != 0
406 unsigned flags;
407 void *addr;
409 /* add mutex operation */
410 spin_lock_irqsave(&l1_inst_sram_lock, flags);
412 addr = _l1_sram_alloc(size, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
414 /* add mutex operation */
415 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
417 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
418 (long unsigned int)addr, size);
420 return addr;
421 #else
422 return NULL;
423 #endif
425 EXPORT_SYMBOL(l1_inst_sram_alloc);
427 int l1_inst_sram_free(const void *addr)
429 #if L1_CODE_LENGTH != 0
430 unsigned flags;
431 int ret;
433 /* add mutex operation */
434 spin_lock_irqsave(&l1_inst_sram_lock, flags);
436 ret = _l1_sram_free(addr, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
438 /* add mutex operation */
439 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
441 return ret;
442 #else
443 return -1;
444 #endif
446 EXPORT_SYMBOL(l1_inst_sram_free);
448 /* L1 Scratchpad memory allocate function */
449 void *l1sram_alloc(size_t size)
451 unsigned flags;
452 void *addr;
454 /* add mutex operation */
455 spin_lock_irqsave(&l1sram_lock, flags);
457 addr = _l1_sram_alloc(size, l1_ssram, ARRAY_SIZE(l1_ssram));
459 /* add mutex operation */
460 spin_unlock_irqrestore(&l1sram_lock, flags);
462 return addr;
465 /* L1 Scratchpad memory allocate function */
466 void *l1sram_alloc_max(size_t *psize)
468 unsigned flags;
469 void *addr;
471 /* add mutex operation */
472 spin_lock_irqsave(&l1sram_lock, flags);
474 addr = _l1_sram_alloc_max(l1_ssram, ARRAY_SIZE(l1_ssram), psize);
476 /* add mutex operation */
477 spin_unlock_irqrestore(&l1sram_lock, flags);
479 return addr;
482 /* L1 Scratchpad memory free function */
483 int l1sram_free(const void *addr)
485 unsigned flags;
486 int ret;
488 /* add mutex operation */
489 spin_lock_irqsave(&l1sram_lock, flags);
491 ret = _l1_sram_free(addr, l1_ssram, ARRAY_SIZE(l1_ssram));
493 /* add mutex operation */
494 spin_unlock_irqrestore(&l1sram_lock, flags);
496 return ret;
499 int sram_free_with_lsl(const void *addr)
501 struct sram_list_struct *lsl, **tmp;
502 struct mm_struct *mm = current->mm;
504 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
505 if ((*tmp)->addr == addr)
506 goto found;
507 return -1;
508 found:
509 lsl = *tmp;
510 sram_free(addr);
511 *tmp = lsl->next;
512 kfree(lsl);
514 return 0;
516 EXPORT_SYMBOL(sram_free_with_lsl);
518 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
520 void *addr = NULL;
521 struct sram_list_struct *lsl = NULL;
522 struct mm_struct *mm = current->mm;
524 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
525 if (!lsl)
526 return NULL;
528 if (flags & L1_INST_SRAM)
529 addr = l1_inst_sram_alloc(size);
531 if (addr == NULL && (flags & L1_DATA_A_SRAM))
532 addr = l1_data_A_sram_alloc(size);
534 if (addr == NULL && (flags & L1_DATA_B_SRAM))
535 addr = l1_data_B_sram_alloc(size);
537 if (addr == NULL) {
538 kfree(lsl);
539 return NULL;
541 lsl->addr = addr;
542 lsl->length = size;
543 lsl->next = mm->context.sram_list;
544 mm->context.sram_list = lsl;
545 return addr;
547 EXPORT_SYMBOL(sram_alloc_with_lsl);
549 #ifdef CONFIG_PROC_FS
550 /* Once we get a real allocator, we'll throw all of this away.
551 * Until then, we need some sort of visibility into the L1 alloc.
553 static void _l1sram_proc_read(char *buf, int *len, const char *desc,
554 struct l1_sram_piece *pfree, const int array_size)
556 int i;
558 *len += sprintf(&buf[*len], "--- L1 %-14s Size PID State\n", desc);
559 for (i = 0; i < array_size; ++i) {
560 const char *alloc_type;
561 switch (pfree[i].flag) {
562 case SRAM_SLT_NULL: alloc_type = "NULL"; break;
563 case SRAM_SLT_FREE: alloc_type = "FREE"; break;
564 case SRAM_SLT_ALLOCATED: alloc_type = "ALLOCATED"; break;
565 default: alloc_type = "????"; break;
567 *len += sprintf(&buf[*len], "%p-%p %8i %4i %s\n",
568 pfree[i].paddr, pfree[i].paddr + pfree[i].size,
569 pfree[i].size, pfree[i].pid, alloc_type);
572 static int l1sram_proc_read(char *buf, char **start, off_t offset, int count,
573 int *eof, void *data)
575 int len = 0;
577 _l1sram_proc_read(buf, &len, "Scratchpad",
578 l1_ssram, ARRAY_SIZE(l1_ssram));
579 #if L1_DATA_A_LENGTH != 0
580 _l1sram_proc_read(buf, &len, "Data A",
581 l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
582 #endif
583 #if L1_DATA_B_LENGTH != 0
584 _l1sram_proc_read(buf, &len, "Data B",
585 l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
586 #endif
587 #if L1_CODE_LENGTH != 0
588 _l1sram_proc_read(buf, &len, "Instruction",
589 l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
590 #endif
592 return len;
595 static int __init l1sram_proc_init(void)
597 struct proc_dir_entry *ptr;
598 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
599 if (!ptr) {
600 printk(KERN_WARNING "unable to create /proc/sram\n");
601 return -1;
603 ptr->owner = THIS_MODULE;
604 ptr->read_proc = l1sram_proc_read;
605 return 0;
607 late_initcall(l1sram_proc_init);
608 #endif