isram-driver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Description: Instruction SRAM accessor functions for the Blackfin
  3. *
  4. * Copyright 2008 Analog Devices Inc.
  5. *
  6. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, see the file COPYING, or write
  15. * to the Free Software Foundation, Inc.,
  16. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #define pr_fmt(fmt) "isram: " fmt
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/types.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/sched.h>
  24. #include <asm/blackfin.h>
  25. #include <asm/dma.h>
  26. /*
  27. * IMPORTANT WARNING ABOUT THESE FUNCTIONS
  28. *
  29. * The emulator will not function correctly if a write command is left in
  30. * ITEST_COMMAND or DTEST_COMMAND AND access to cache memory is needed by
  31. * the emulator. To avoid such problems, ensure that both ITEST_COMMAND
  32. * and DTEST_COMMAND are zero when exiting these functions.
  33. */
  34. /*
  35. * On the Blackfin, L1 instruction sram (which operates at core speeds) can not
  36. * be accessed by a normal core load, so we need to go through a few hoops to
  37. * read/write it.
  38. * To try to make it easier - we export a memcpy interface, where either src or
  39. * dest can be in this special L1 memory area.
  40. * The low level read/write functions should not be exposed to the rest of the
  41. * kernel, since they operate on 64-bit data, and need specific address alignment
  42. */
  43. static DEFINE_SPINLOCK(dtest_lock);
  44. /* Takes a void pointer */
  45. #define IADDR2DTEST(x) \
  46. ({ unsigned long __addr = (unsigned long)(x); \
  47. (__addr & 0x47F8) | /* address bits 14 & 10:3 */ \
  48. (__addr & 0x8000) << 23 | /* Bank A/B */ \
  49. (__addr & 0x0800) << 15 | /* address bit 11 */ \
  50. (__addr & 0x3000) << 4 | /* address bits 13:12 */ \
  51. (__addr & 0x8000) << 8 | /* address bit 15 */ \
  52. (0x1000000) | /* instruction access = 1 */ \
  53. (0x4); /* data array = 1 */ \
  54. })
  55. /* Takes a pointer, and returns the offset (in bits) which things should be shifted */
  56. #define ADDR2OFFSET(x) ((((unsigned long)(x)) & 0x7) * 8)
  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. static void isram_write(const void *addr, uint64_t data)
  60. {
  61. uint32_t cmd;
  62. unsigned long flags;
  63. if (addr >= (void *)(L1_CODE_START + L1_CODE_LENGTH))
  64. return;
  65. cmd = IADDR2DTEST(addr) | 2; /* write */
  66. /*
  67. * Writes to DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  68. * While in exception context - atomicity is guaranteed or double fault
  69. */
  70. spin_lock_irqsave(&dtest_lock, flags);
  71. bfin_write_DTEST_DATA0(data & 0xFFFFFFFF);
  72. bfin_write_DTEST_DATA1(data >> 32);
  73. /* use the builtin, since interrupts are already turned off */
  74. __builtin_bfin_csync();
  75. bfin_write_DTEST_COMMAND(cmd);
  76. __builtin_bfin_csync();
  77. bfin_write_DTEST_COMMAND(0);
  78. __builtin_bfin_csync();
  79. spin_unlock_irqrestore(&dtest_lock, flags);
  80. }
  81. static uint64_t isram_read(const void *addr)
  82. {
  83. uint32_t cmd;
  84. unsigned long flags;
  85. uint64_t ret;
  86. if (addr > (void *)(L1_CODE_START + L1_CODE_LENGTH))
  87. return 0;
  88. cmd = IADDR2DTEST(addr) | 0; /* read */
  89. /*
  90. * Reads of DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  91. * While in exception context - atomicity is guaranteed or double fault
  92. */
  93. spin_lock_irqsave(&dtest_lock, flags);
  94. /* use the builtin, since interrupts are already turned off */
  95. __builtin_bfin_csync();
  96. bfin_write_DTEST_COMMAND(cmd);
  97. __builtin_bfin_csync();
  98. ret = bfin_read_DTEST_DATA0() | ((uint64_t)bfin_read_DTEST_DATA1() << 32);
  99. bfin_write_DTEST_COMMAND(0);
  100. __builtin_bfin_csync();
  101. spin_unlock_irqrestore(&dtest_lock, flags);
  102. return ret;
  103. }
  104. static bool isram_check_addr(const void *addr, size_t n)
  105. {
  106. if ((addr >= (void *)L1_CODE_START) &&
  107. (addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
  108. if ((addr + n) > (void *)(L1_CODE_START + L1_CODE_LENGTH)) {
  109. show_stack(NULL, NULL);
  110. pr_err("copy involving %p length (%zu) too long\n", addr, n);
  111. }
  112. return true;
  113. }
  114. return false;
  115. }
  116. /*
  117. * The isram_memcpy() function copies n bytes from memory area src to memory area dest.
  118. * The isram_memcpy() function returns a pointer to dest.
  119. * Either dest or src can be in L1 instruction sram.
  120. */
  121. void *isram_memcpy(void *dest, const void *src, size_t n)
  122. {
  123. uint64_t data_in = 0, data_out = 0;
  124. size_t count;
  125. bool dest_in_l1, src_in_l1, need_data, put_data;
  126. unsigned char byte, *src_byte, *dest_byte;
  127. src_byte = (unsigned char *)src;
  128. dest_byte = (unsigned char *)dest;
  129. dest_in_l1 = isram_check_addr(dest, n);
  130. src_in_l1 = isram_check_addr(src, n);
  131. need_data = true;
  132. put_data = true;
  133. for (count = 0; count < n; count++) {
  134. if (src_in_l1) {
  135. if (need_data) {
  136. data_in = isram_read(src + count);
  137. need_data = false;
  138. }
  139. if (ADDR2LAST(src + count))
  140. need_data = true;
  141. byte = (unsigned char)((data_in >> ADDR2OFFSET(src + count)) & 0xff);
  142. } else {
  143. /* src is in L2 or L3 - so just dereference*/
  144. byte = src_byte[count];
  145. }
  146. if (dest_in_l1) {
  147. if (put_data) {
  148. data_out = isram_read(dest + count);
  149. put_data = false;
  150. }
  151. data_out &= ~((uint64_t)0xff << ADDR2OFFSET(dest + count));
  152. data_out |= ((uint64_t)byte << ADDR2OFFSET(dest + count));
  153. if (ADDR2LAST(dest + count)) {
  154. put_data = true;
  155. isram_write(dest + count, data_out);
  156. }
  157. } else {
  158. /* dest in L2 or L3 - so just dereference */
  159. dest_byte[count] = byte;
  160. }
  161. }
  162. /* make sure we dump the last byte if necessary */
  163. if (dest_in_l1 && !put_data)
  164. isram_write(dest + count, data_out);
  165. return dest;
  166. }
  167. EXPORT_SYMBOL(isram_memcpy);
  168. #ifdef CONFIG_BFIN_ISRAM_SELF_TEST
  169. #define TEST_LEN 0x100
  170. static __init void hex_dump(unsigned char *buf, int len)
  171. {
  172. while (len--)
  173. pr_cont("%02x", *buf++);
  174. }
  175. static __init int isram_read_test(char *sdram, void *l1inst)
  176. {
  177. int i, ret = 0;
  178. uint64_t data1, data2;
  179. pr_info("INFO: running isram_read tests\n");
  180. /* setup some different data to play with */
  181. for (i = 0; i < TEST_LEN; ++i)
  182. sdram[i] = i;
  183. dma_memcpy(l1inst, sdram, TEST_LEN);
  184. /* make sure we can read the L1 inst */
  185. for (i = 0; i < TEST_LEN; i += sizeof(uint64_t)) {
  186. data1 = isram_read(l1inst + i);
  187. memcpy(&data2, sdram + i, sizeof(data2));
  188. if (memcmp(&data1, &data2, sizeof(uint64_t))) {
  189. pr_err("FAIL: isram_read(%p) returned %#llx but wanted %#llx\n",
  190. l1inst + i, data1, data2);
  191. ++ret;
  192. }
  193. }
  194. return ret;
  195. }
  196. static __init int isram_write_test(char *sdram, void *l1inst)
  197. {
  198. int i, ret = 0;
  199. uint64_t data1, data2;
  200. pr_info("INFO: running isram_write tests\n");
  201. /* setup some different data to play with */
  202. memset(sdram, 0, TEST_LEN * 2);
  203. dma_memcpy(l1inst, sdram, TEST_LEN);
  204. for (i = 0; i < TEST_LEN; ++i)
  205. sdram[i] = i;
  206. /* make sure we can write the L1 inst */
  207. for (i = 0; i < TEST_LEN; i += sizeof(uint64_t)) {
  208. memcpy(&data1, sdram + i, sizeof(data1));
  209. isram_write(l1inst + i, data1);
  210. data2 = isram_read(l1inst + i);
  211. if (memcmp(&data1, &data2, sizeof(uint64_t))) {
  212. pr_err("FAIL: isram_write(%p, %#llx) != %#llx\n",
  213. l1inst + i, data1, data2);
  214. ++ret;
  215. }
  216. }
  217. dma_memcpy(sdram + TEST_LEN, l1inst, TEST_LEN);
  218. if (memcmp(sdram, sdram + TEST_LEN, TEST_LEN)) {
  219. pr_err("FAIL: isram_write() did not work properly\n");
  220. ++ret;
  221. }
  222. return ret;
  223. }
  224. static __init int
  225. _isram_memcpy_test(char pattern, void *sdram, void *l1inst, const char *smemcpy,
  226. void *(*fmemcpy)(void *, const void *, size_t))
  227. {
  228. memset(sdram, pattern, TEST_LEN);
  229. fmemcpy(l1inst, sdram, TEST_LEN);
  230. fmemcpy(sdram + TEST_LEN, l1inst, TEST_LEN);
  231. if (memcmp(sdram, sdram + TEST_LEN, TEST_LEN)) {
  232. pr_err("FAIL: %s(%p <=> %p, %#x) failed (data is %#x)\n",
  233. smemcpy, l1inst, sdram, TEST_LEN, pattern);
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. #define _isram_memcpy_test(a, b, c, d) _isram_memcpy_test(a, b, c, #d, d)
  239. static __init int isram_memcpy_test(char *sdram, void *l1inst)
  240. {
  241. int i, j, thisret, ret = 0;
  242. /* check broad isram_memcpy() */
  243. pr_info("INFO: running broad isram_memcpy tests\n");
  244. for (i = 0xf; i >= 0; --i)
  245. ret += _isram_memcpy_test(i, sdram, l1inst, isram_memcpy);
  246. /* check read of small, unaligned, and hardware 64bit limits */
  247. pr_info("INFO: running isram_memcpy (read) tests\n");
  248. for (i = 0; i < TEST_LEN; ++i)
  249. sdram[i] = i;
  250. dma_memcpy(l1inst, sdram, TEST_LEN);
  251. thisret = 0;
  252. for (i = 0; i < TEST_LEN - 32; ++i) {
  253. unsigned char cmp[32];
  254. for (j = 1; j <= 32; ++j) {
  255. memset(cmp, 0, sizeof(cmp));
  256. isram_memcpy(cmp, l1inst + i, j);
  257. if (memcmp(cmp, sdram + i, j)) {
  258. pr_err("FAIL: %p:", l1inst + 1);
  259. hex_dump(cmp, j);
  260. pr_cont(" SDRAM:");
  261. hex_dump(sdram + i, j);
  262. pr_cont("\n");
  263. if (++thisret > 20) {
  264. pr_err("FAIL: skipping remaining series\n");
  265. i = TEST_LEN;
  266. break;
  267. }
  268. }
  269. }
  270. }
  271. ret += thisret;
  272. /* check write of small, unaligned, and hardware 64bit limits */
  273. pr_info("INFO: running isram_memcpy (write) tests\n");
  274. memset(sdram + TEST_LEN, 0, TEST_LEN);
  275. dma_memcpy(l1inst, sdram + TEST_LEN, TEST_LEN);
  276. thisret = 0;
  277. for (i = 0; i < TEST_LEN - 32; ++i) {
  278. unsigned char cmp[32];
  279. for (j = 1; j <= 32; ++j) {
  280. isram_memcpy(l1inst + i, sdram + i, j);
  281. dma_memcpy(cmp, l1inst + i, j);
  282. if (memcmp(cmp, sdram + i, j)) {
  283. pr_err("FAIL: %p:", l1inst + i);
  284. hex_dump(cmp, j);
  285. pr_cont(" SDRAM:");
  286. hex_dump(sdram + i, j);
  287. pr_cont("\n");
  288. if (++thisret > 20) {
  289. pr_err("FAIL: skipping remaining series\n");
  290. i = TEST_LEN;
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. ret += thisret;
  297. return ret;
  298. }
  299. static __init int isram_test_init(void)
  300. {
  301. int ret;
  302. char *sdram;
  303. void *l1inst;
  304. sdram = kmalloc(TEST_LEN * 2, GFP_KERNEL);
  305. if (!sdram) {
  306. pr_warning("SKIP: could not allocate sdram\n");
  307. return 0;
  308. }
  309. l1inst = l1_inst_sram_alloc(TEST_LEN);
  310. if (!l1inst) {
  311. kfree(sdram);
  312. pr_warning("SKIP: could not allocate L1 inst\n");
  313. return 0;
  314. }
  315. /* sanity check initial L1 inst state */
  316. ret = 1;
  317. pr_info("INFO: running initial dma_memcpy checks\n");
  318. if (_isram_memcpy_test(0xa, sdram, l1inst, dma_memcpy))
  319. goto abort;
  320. if (_isram_memcpy_test(0x5, sdram, l1inst, dma_memcpy))
  321. goto abort;
  322. ret = 0;
  323. ret += isram_read_test(sdram, l1inst);
  324. ret += isram_write_test(sdram, l1inst);
  325. ret += isram_memcpy_test(sdram, l1inst);
  326. abort:
  327. sram_free(l1inst);
  328. kfree(sdram);
  329. if (ret)
  330. return -EIO;
  331. pr_info("PASS: all tests worked !\n");
  332. return 0;
  333. }
  334. late_initcall(isram_test_init);
  335. static __exit void isram_test_exit(void)
  336. {
  337. /* stub to allow unloading */
  338. }
  339. module_exit(isram_test_exit);
  340. #endif