isram-driver.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/sched.h>
  23. #include <asm/blackfin.h>
  24. /*
  25. * IMPORTANT WARNING ABOUT THESE FUNCTIONS
  26. *
  27. * The emulator will not function correctly if a write command is left in
  28. * ITEST_COMMAND or DTEST_COMMAND AND access to cache memory is needed by
  29. * the emulator. To avoid such problems, ensure that both ITEST_COMMAND
  30. * and DTEST_COMMAND are zero when exiting these functions.
  31. */
  32. /*
  33. * On the Blackfin, L1 instruction sram (which operates at core speeds) can not
  34. * be accessed by a normal core load, so we need to go through a few hoops to
  35. * read/write it.
  36. * To try to make it easier - we export a memcpy interface, where either src or
  37. * dest can be in this special L1 memory area.
  38. * The low level read/write functions should not be exposed to the rest of the
  39. * kernel, since they operate on 64-bit data, and need specific address alignment
  40. */
  41. static DEFINE_SPINLOCK(dtest_lock);
  42. /* Takes a void pointer */
  43. #define IADDR2DTEST(x) \
  44. ({ unsigned long __addr = (unsigned long)(x); \
  45. (__addr & 0x47F8) | /* address bits 14 & 10:3 */ \
  46. (__addr & 0x0800) << 15 | /* address bit 11 */ \
  47. (__addr & 0x3000) << 4 | /* address bits 13:12 */ \
  48. (__addr & 0x8000) << 8 | /* address bit 15 */ \
  49. (0x1000004); /* isram access */ \
  50. })
  51. /* Takes a pointer, and returns the offset (in bits) which things should be shifted */
  52. #define ADDR2OFFSET(x) ((((unsigned long)(x)) & 0x7) * 8)
  53. /* Takes a pointer, determines if it is the last byte in the isram 64-bit data type */
  54. #define ADDR2LAST(x) ((((unsigned long)x) & 0x7) == 0x7)
  55. static void isram_write(const void *addr, uint64_t data)
  56. {
  57. uint32_t cmd;
  58. unsigned long flags;
  59. if (addr >= (void *)(L1_CODE_START + L1_CODE_LENGTH))
  60. return;
  61. cmd = IADDR2DTEST(addr) | 1; /* write */
  62. /*
  63. * Writes to DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  64. * While in exception context - atomicity is guaranteed or double fault
  65. */
  66. spin_lock_irqsave(&dtest_lock, flags);
  67. bfin_write_DTEST_DATA0(data & 0xFFFFFFFF);
  68. bfin_write_DTEST_DATA1(data >> 32);
  69. /* use the builtin, since interrupts are already turned off */
  70. __builtin_bfin_csync();
  71. bfin_write_DTEST_COMMAND(cmd);
  72. __builtin_bfin_csync();
  73. bfin_write_DTEST_COMMAND(0);
  74. __builtin_bfin_csync();
  75. spin_unlock_irqrestore(&dtest_lock, flags);
  76. }
  77. static uint64_t isram_read(const void *addr)
  78. {
  79. uint32_t cmd;
  80. unsigned long flags;
  81. uint64_t ret;
  82. if (addr > (void *)(L1_CODE_START + L1_CODE_LENGTH))
  83. return 0;
  84. cmd = IADDR2DTEST(addr) | 0; /* read */
  85. /*
  86. * Reads of DTEST_DATA[0:1] need to be atomic with write to DTEST_COMMAND
  87. * While in exception context - atomicity is guaranteed or double fault
  88. */
  89. spin_lock_irqsave(&dtest_lock, flags);
  90. /* use the builtin, since interrupts are already turned off */
  91. __builtin_bfin_csync();
  92. bfin_write_DTEST_COMMAND(cmd);
  93. __builtin_bfin_csync();
  94. ret = bfin_read_DTEST_DATA0() | ((uint64_t)bfin_read_DTEST_DATA1() << 32);
  95. bfin_write_DTEST_COMMAND(0);
  96. __builtin_bfin_csync();
  97. spin_unlock_irqrestore(&dtest_lock, flags);
  98. return ret;
  99. }
  100. static bool isram_check_addr(const void *addr, size_t n)
  101. {
  102. if ((addr >= (void *)L1_CODE_START) &&
  103. (addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))) {
  104. if ((addr + n) >= (void *)(L1_CODE_START + L1_CODE_LENGTH)) {
  105. show_stack(NULL, NULL);
  106. printk(KERN_ERR "isram_memcpy: copy involving %p length "
  107. "(%zu) too long\n", addr, n);
  108. }
  109. return true;
  110. }
  111. return false;
  112. }
  113. /*
  114. * The isram_memcpy() function copies n bytes from memory area src to memory area dest.
  115. * The isram_memcpy() function returns a pointer to dest.
  116. * Either dest or src can be in L1 instruction sram.
  117. */
  118. void *isram_memcpy(void *dest, const void *src, size_t n)
  119. {
  120. uint64_t data_in = 0, data_out = 0;
  121. size_t count;
  122. bool dest_in_l1, src_in_l1, need_data, put_data;
  123. unsigned char byte, *src_byte, *dest_byte;
  124. src_byte = (unsigned char *)src;
  125. dest_byte = (unsigned char *)dest;
  126. dest_in_l1 = isram_check_addr(dest, n);
  127. src_in_l1 = isram_check_addr(src, n);
  128. need_data = true;
  129. put_data = true;
  130. for (count = 0; count < n; count++) {
  131. if (src_in_l1) {
  132. if (need_data) {
  133. data_in = isram_read(src + count);
  134. need_data = false;
  135. }
  136. if (ADDR2LAST(src + count))
  137. need_data = true;
  138. byte = (unsigned char)((data_in >> ADDR2OFFSET(src + count)) & 0xff);
  139. } else {
  140. /* src is in L2 or L3 - so just dereference*/
  141. byte = src_byte[count];
  142. }
  143. if (dest_in_l1) {
  144. if (put_data) {
  145. data_out = isram_read(dest + count);
  146. put_data = false;
  147. }
  148. data_out &= ~((uint64_t)0xff << ADDR2OFFSET(dest + count));
  149. data_out |= ((uint64_t)byte << ADDR2OFFSET(dest + count));
  150. if (ADDR2LAST(dest + count)) {
  151. put_data = true;
  152. isram_write(dest + count, data_out);
  153. }
  154. } else {
  155. /* dest in L2 or L3 - so just dereference */
  156. dest_byte[count] = byte;
  157. }
  158. }
  159. /* make sure we dump the last byte if necessary */
  160. if (dest_in_l1 && !put_data)
  161. isram_write(dest + count, data_out);
  162. return dest;
  163. }
  164. EXPORT_SYMBOL(isram_memcpy);