blob: 7e2e674ed4440a6a0e333f0ddca5f29837394e13 [file] [log] [blame]
Robin Getz9df10282008-10-08 18:03:33 +08001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * Instruction SRAM accessor functions for the Blackfin
Robin Getz9df10282008-10-08 18:03:33 +08003 *
4 * Copyright 2008 Analog Devices Inc.
5 *
Robin Getz96f10502009-09-24 14:11:24 +00006 * Licensed under the GPL-2 or later
Robin Getz9df10282008-10-08 18:03:33 +08007 */
8
Mike Frysingerc40cdb22009-08-27 01:46:48 +00009#define pr_fmt(fmt) "isram: " fmt
10
Robin Getz9df10282008-10-08 18:03:33 +080011#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Robin Getz9df10282008-10-08 18:03:33 +080015#include <linux/spinlock.h>
16#include <linux/sched.h>
17
18#include <asm/blackfin.h>
Mike Frysingerc40cdb22009-08-27 01:46:48 +000019#include <asm/dma.h>
Robin Getz9df10282008-10-08 18:03:33 +080020
21/*
22 * IMPORTANT WARNING ABOUT THESE FUNCTIONS
23 *
24 * The emulator will not function correctly if a write command is left in
25 * ITEST_COMMAND or DTEST_COMMAND AND access to cache memory is needed by
26 * the emulator. To avoid such problems, ensure that both ITEST_COMMAND
27 * and DTEST_COMMAND are zero when exiting these functions.
28 */
29
30
31/*
32 * On the Blackfin, L1 instruction sram (which operates at core speeds) can not
33 * be accessed by a normal core load, so we need to go through a few hoops to
34 * read/write it.
35 * To try to make it easier - we export a memcpy interface, where either src or
36 * dest can be in this special L1 memory area.
37 * The low level read/write functions should not be exposed to the rest of the
38 * kernel, since they operate on 64-bit data, and need specific address alignment
39 */
40
41static DEFINE_SPINLOCK(dtest_lock);
42
43/* Takes a void pointer */
44#define IADDR2DTEST(x) \
45 ({ unsigned long __addr = (unsigned long)(x); \
Mike Frysingerc70dcab2010-05-04 04:14:08 +000046 ((__addr & (1 << 11)) << (26 - 11)) | /* addr bit 11 (Way0/Way1) */ \
47 (1 << 24) | /* instruction access = 1 */ \
48 ((__addr & (1 << 15)) << (23 - 15)) | /* addr bit 15 (Data Bank) */ \
49 ((__addr & (3 << 12)) << (16 - 12)) | /* addr bits 13:12 (Subbank) */ \
50 (__addr & 0x47F8) | /* addr bits 14 & 10:3 */ \
51 (1 << 2); /* data array = 1 */ \
Robin Getz9df10282008-10-08 18:03:33 +080052 })
53
54/* Takes a pointer, and returns the offset (in bits) which things should be shifted */
55#define ADDR2OFFSET(x) ((((unsigned long)(x)) & 0x7) * 8)
56
57/* Takes a pointer, determines if it is the last byte in the isram 64-bit data type */
58#define ADDR2LAST(x) ((((unsigned long)x) & 0x7) == 0x7)
59
60static void isram_write(const void *addr, uint64_t data)
61{
62 uint32_t cmd;
63 unsigned long flags;
64
Mike Frysingerf05ede32009-12-25 15:34:08 +000065 if (unlikely(addr >= (void *)(L1_CODE_START + L1_CODE_LENGTH)))
Robin Getz9df10282008-10-08 18:03:33 +080066 return;
67
Robin Getz774b8022009-08-27 14:39:31 +000068 cmd = IADDR2DTEST(addr) | 2; /* write */
Robin Getz9df10282008-10-08 18:03:33 +080069
70 /*
71 * Writes to DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
72 * While in exception context - atomicity is guaranteed or double fault
73 */
74 spin_lock_irqsave(&dtest_lock, flags);
75
76 bfin_write_DTEST_DATA0(data & 0xFFFFFFFF);
77 bfin_write_DTEST_DATA1(data >> 32);
78
79 /* use the builtin, since interrupts are already turned off */
80 __builtin_bfin_csync();
81 bfin_write_DTEST_COMMAND(cmd);
82 __builtin_bfin_csync();
83
84 bfin_write_DTEST_COMMAND(0);
85 __builtin_bfin_csync();
86
87 spin_unlock_irqrestore(&dtest_lock, flags);
88}
89
90static uint64_t isram_read(const void *addr)
91{
92 uint32_t cmd;
93 unsigned long flags;
94 uint64_t ret;
95
Mike Frysingerf05ede32009-12-25 15:34:08 +000096 if (unlikely(addr > (void *)(L1_CODE_START + L1_CODE_LENGTH)))
Robin Getz9df10282008-10-08 18:03:33 +080097 return 0;
98
99 cmd = IADDR2DTEST(addr) | 0; /* read */
100
101 /*
102 * Reads of DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
103 * While in exception context - atomicity is guaranteed or double fault
104 */
105 spin_lock_irqsave(&dtest_lock, flags);
106 /* use the builtin, since interrupts are already turned off */
107 __builtin_bfin_csync();
108 bfin_write_DTEST_COMMAND(cmd);
109 __builtin_bfin_csync();
110 ret = bfin_read_DTEST_DATA0() | ((uint64_t)bfin_read_DTEST_DATA1() << 32);
111
112 bfin_write_DTEST_COMMAND(0);
113 __builtin_bfin_csync();
114 spin_unlock_irqrestore(&dtest_lock, flags);
115
116 return ret;
117}
118
119static bool isram_check_addr(const void *addr, size_t n)
120{
121 if ((addr >= (void *)L1_CODE_START) &&
122 (addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
Mike Frysingerf05ede32009-12-25 15:34:08 +0000123 if (unlikely((addr + n) > (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
Robin Getz9df10282008-10-08 18:03:33 +0800124 show_stack(NULL, NULL);
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000125 pr_err("copy involving %p length (%zu) too long\n", addr, n);
Robin Getz9df10282008-10-08 18:03:33 +0800126 }
127 return true;
128 }
129 return false;
130}
131
132/*
133 * The isram_memcpy() function copies n bytes from memory area src to memory area dest.
134 * The isram_memcpy() function returns a pointer to dest.
135 * Either dest or src can be in L1 instruction sram.
136 */
137void *isram_memcpy(void *dest, const void *src, size_t n)
138{
139 uint64_t data_in = 0, data_out = 0;
140 size_t count;
141 bool dest_in_l1, src_in_l1, need_data, put_data;
142 unsigned char byte, *src_byte, *dest_byte;
143
144 src_byte = (unsigned char *)src;
145 dest_byte = (unsigned char *)dest;
146
147 dest_in_l1 = isram_check_addr(dest, n);
148 src_in_l1 = isram_check_addr(src, n);
149
150 need_data = true;
151 put_data = true;
152 for (count = 0; count < n; count++) {
153 if (src_in_l1) {
154 if (need_data) {
155 data_in = isram_read(src + count);
156 need_data = false;
157 }
158
159 if (ADDR2LAST(src + count))
160 need_data = true;
161
162 byte = (unsigned char)((data_in >> ADDR2OFFSET(src + count)) & 0xff);
163
164 } else {
165 /* src is in L2 or L3 - so just dereference*/
166 byte = src_byte[count];
167 }
168
169 if (dest_in_l1) {
170 if (put_data) {
171 data_out = isram_read(dest + count);
172 put_data = false;
173 }
174
175 data_out &= ~((uint64_t)0xff << ADDR2OFFSET(dest + count));
176 data_out |= ((uint64_t)byte << ADDR2OFFSET(dest + count));
177
178 if (ADDR2LAST(dest + count)) {
179 put_data = true;
180 isram_write(dest + count, data_out);
181 }
182 } else {
183 /* dest in L2 or L3 - so just dereference */
184 dest_byte[count] = byte;
185 }
186 }
187
188 /* make sure we dump the last byte if necessary */
189 if (dest_in_l1 && !put_data)
190 isram_write(dest + count, data_out);
191
192 return dest;
193}
194EXPORT_SYMBOL(isram_memcpy);
195
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000196#ifdef CONFIG_BFIN_ISRAM_SELF_TEST
197
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000198static int test_len = 0x20000;
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000199
200static __init void hex_dump(unsigned char *buf, int len)
201{
202 while (len--)
203 pr_cont("%02x", *buf++);
204}
205
206static __init int isram_read_test(char *sdram, void *l1inst)
207{
208 int i, ret = 0;
209 uint64_t data1, data2;
210
211 pr_info("INFO: running isram_read tests\n");
212
213 /* setup some different data to play with */
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000214 for (i = 0; i < test_len; ++i)
215 sdram[i] = i % 255;
216 dma_memcpy(l1inst, sdram, test_len);
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000217
218 /* make sure we can read the L1 inst */
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000219 for (i = 0; i < test_len; i += sizeof(uint64_t)) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000220 data1 = isram_read(l1inst + i);
221 memcpy(&data2, sdram + i, sizeof(data2));
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000222 if (data1 != data2) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000223 pr_err("FAIL: isram_read(%p) returned %#llx but wanted %#llx\n",
224 l1inst + i, data1, data2);
225 ++ret;
226 }
227 }
228
229 return ret;
230}
231
232static __init int isram_write_test(char *sdram, void *l1inst)
233{
234 int i, ret = 0;
235 uint64_t data1, data2;
236
237 pr_info("INFO: running isram_write tests\n");
238
239 /* setup some different data to play with */
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000240 memset(sdram, 0, test_len * 2);
241 dma_memcpy(l1inst, sdram, test_len);
242 for (i = 0; i < test_len; ++i)
243 sdram[i] = i % 255;
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000244
245 /* make sure we can write the L1 inst */
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000246 for (i = 0; i < test_len; i += sizeof(uint64_t)) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000247 memcpy(&data1, sdram + i, sizeof(data1));
248 isram_write(l1inst + i, data1);
249 data2 = isram_read(l1inst + i);
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000250 if (data1 != data2) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000251 pr_err("FAIL: isram_write(%p, %#llx) != %#llx\n",
252 l1inst + i, data1, data2);
253 ++ret;
254 }
255 }
256
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000257 dma_memcpy(sdram + test_len, l1inst, test_len);
258 if (memcmp(sdram, sdram + test_len, test_len)) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000259 pr_err("FAIL: isram_write() did not work properly\n");
260 ++ret;
261 }
262
263 return ret;
264}
265
266static __init int
267_isram_memcpy_test(char pattern, void *sdram, void *l1inst, const char *smemcpy,
268 void *(*fmemcpy)(void *, const void *, size_t))
269{
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000270 memset(sdram, pattern, test_len);
271 fmemcpy(l1inst, sdram, test_len);
272 fmemcpy(sdram + test_len, l1inst, test_len);
273 if (memcmp(sdram, sdram + test_len, test_len)) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000274 pr_err("FAIL: %s(%p <=> %p, %#x) failed (data is %#x)\n",
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000275 smemcpy, l1inst, sdram, test_len, pattern);
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000276 return 1;
277 }
278 return 0;
279}
280#define _isram_memcpy_test(a, b, c, d) _isram_memcpy_test(a, b, c, #d, d)
281
282static __init int isram_memcpy_test(char *sdram, void *l1inst)
283{
284 int i, j, thisret, ret = 0;
285
286 /* check broad isram_memcpy() */
287 pr_info("INFO: running broad isram_memcpy tests\n");
288 for (i = 0xf; i >= 0; --i)
289 ret += _isram_memcpy_test(i, sdram, l1inst, isram_memcpy);
290
291 /* check read of small, unaligned, and hardware 64bit limits */
292 pr_info("INFO: running isram_memcpy (read) tests\n");
293
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000294 /* setup some different data to play with */
295 for (i = 0; i < test_len; ++i)
296 sdram[i] = i % 255;
297 dma_memcpy(l1inst, sdram, test_len);
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000298
299 thisret = 0;
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000300 for (i = 0; i < test_len - 32; ++i) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000301 unsigned char cmp[32];
302 for (j = 1; j <= 32; ++j) {
303 memset(cmp, 0, sizeof(cmp));
304 isram_memcpy(cmp, l1inst + i, j);
305 if (memcmp(cmp, sdram + i, j)) {
306 pr_err("FAIL: %p:", l1inst + 1);
307 hex_dump(cmp, j);
308 pr_cont(" SDRAM:");
309 hex_dump(sdram + i, j);
310 pr_cont("\n");
311 if (++thisret > 20) {
312 pr_err("FAIL: skipping remaining series\n");
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000313 i = test_len;
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000314 break;
315 }
316 }
317 }
318 }
319 ret += thisret;
320
321 /* check write of small, unaligned, and hardware 64bit limits */
322 pr_info("INFO: running isram_memcpy (write) tests\n");
323
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000324 memset(sdram + test_len, 0, test_len);
325 dma_memcpy(l1inst, sdram + test_len, test_len);
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000326
327 thisret = 0;
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000328 for (i = 0; i < test_len - 32; ++i) {
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000329 unsigned char cmp[32];
330 for (j = 1; j <= 32; ++j) {
331 isram_memcpy(l1inst + i, sdram + i, j);
332 dma_memcpy(cmp, l1inst + i, j);
333 if (memcmp(cmp, sdram + i, j)) {
334 pr_err("FAIL: %p:", l1inst + i);
335 hex_dump(cmp, j);
336 pr_cont(" SDRAM:");
337 hex_dump(sdram + i, j);
338 pr_cont("\n");
339 if (++thisret > 20) {
340 pr_err("FAIL: skipping remaining series\n");
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000341 i = test_len;
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000342 break;
343 }
344 }
345 }
346 }
347 ret += thisret;
348
349 return ret;
350}
351
352static __init int isram_test_init(void)
353{
354 int ret;
355 char *sdram;
356 void *l1inst;
357
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000358 /* Try to test as much of L1SRAM as possible */
359 while (test_len) {
360 test_len >>= 1;
361 l1inst = l1_inst_sram_alloc(test_len);
362 if (l1inst)
363 break;
364 }
365 if (!l1inst) {
366 pr_warning("SKIP: could not allocate L1 inst\n");
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000367 return 0;
368 }
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000369 pr_info("INFO: testing %#x bytes (%p - %p)\n",
370 test_len, l1inst, l1inst + test_len);
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000371
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000372 sdram = kmalloc(test_len * 2, GFP_KERNEL);
373 if (!sdram) {
374 sram_free(l1inst);
375 pr_warning("SKIP: could not allocate sdram\n");
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000376 return 0;
377 }
378
379 /* sanity check initial L1 inst state */
380 ret = 1;
Mike Frysingerc70dcab2010-05-04 04:14:08 +0000381 pr_info("INFO: running initial dma_memcpy checks %p\n", sdram);
Mike Frysingerc40cdb22009-08-27 01:46:48 +0000382 if (_isram_memcpy_test(0xa, sdram, l1inst, dma_memcpy))
383 goto abort;
384 if (_isram_memcpy_test(0x5, sdram, l1inst, dma_memcpy))
385 goto abort;
386
387 ret = 0;
388 ret += isram_read_test(sdram, l1inst);
389 ret += isram_write_test(sdram, l1inst);
390 ret += isram_memcpy_test(sdram, l1inst);
391
392 abort:
393 sram_free(l1inst);
394 kfree(sdram);
395
396 if (ret)
397 return -EIO;
398
399 pr_info("PASS: all tests worked !\n");
400 return 0;
401}
402late_initcall(isram_test_init);
403
404static __exit void isram_test_exit(void)
405{
406 /* stub to allow unloading */
407}
408module_exit(isram_test_exit);
409
410#endif