buffer_icap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*****************************************************************************
  2. *
  3. * Author: Xilinx, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
  11. * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
  12. * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
  13. * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
  14. * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
  15. * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
  16. * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
  17. * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
  18. * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
  19. * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
  20. * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
  21. * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE.
  23. *
  24. * Xilinx products are not intended for use in life support appliances,
  25. * devices, or systems. Use in such applications is expressly prohibited.
  26. *
  27. * (c) Copyright 2003-2008 Xilinx Inc.
  28. * All rights reserved.
  29. *
  30. * You should have received a copy of the GNU General Public License along
  31. * with this program; if not, write to the Free Software Foundation, Inc.,
  32. * 675 Mass Ave, Cambridge, MA 02139, USA.
  33. *
  34. *****************************************************************************/
  35. #include "buffer_icap.h"
  36. /* Indicates how many bytes will fit in a buffer. (1 BRAM) */
  37. #define XHI_MAX_BUFFER_BYTES 2048
  38. #define XHI_MAX_BUFFER_INTS (XHI_MAX_BUFFER_BYTES >> 2)
  39. /* File access and error constants */
  40. #define XHI_DEVICE_READ_ERROR -1
  41. #define XHI_DEVICE_WRITE_ERROR -2
  42. #define XHI_BUFFER_OVERFLOW_ERROR -3
  43. #define XHI_DEVICE_READ 0x1
  44. #define XHI_DEVICE_WRITE 0x0
  45. /* Constants for checking transfer status */
  46. #define XHI_CYCLE_DONE 0
  47. #define XHI_CYCLE_EXECUTING 1
  48. /* buffer_icap register offsets */
  49. /* Size of transfer, read & write */
  50. #define XHI_SIZE_REG_OFFSET 0x800L
  51. /* offset into bram, read & write */
  52. #define XHI_BRAM_OFFSET_REG_OFFSET 0x804L
  53. /* Read not Configure, direction of transfer. Write only */
  54. #define XHI_RNC_REG_OFFSET 0x808L
  55. /* Indicates transfer complete. Read only */
  56. #define XHI_STATUS_REG_OFFSET 0x80CL
  57. /* Constants for setting the RNC register */
  58. #define XHI_CONFIGURE 0x0UL
  59. #define XHI_READBACK 0x1UL
  60. /* Constants for the Done register */
  61. #define XHI_NOT_FINISHED 0x0UL
  62. #define XHI_FINISHED 0x1UL
  63. #define XHI_BUFFER_START 0
  64. /**
  65. * buffer_icap_get_status - Get the contents of the status register.
  66. * @base_address: is the base address of the device
  67. *
  68. * The status register contains the ICAP status and the done bit.
  69. *
  70. * D8 - cfgerr
  71. * D7 - dalign
  72. * D6 - rip
  73. * D5 - in_abort_l
  74. * D4 - Always 1
  75. * D3 - Always 1
  76. * D2 - Always 1
  77. * D1 - Always 1
  78. * D0 - Done bit
  79. **/
  80. static inline u32 buffer_icap_get_status(void __iomem *base_address)
  81. {
  82. return in_be32(base_address + XHI_STATUS_REG_OFFSET);
  83. }
  84. /**
  85. * buffer_icap_get_bram - Reads data from the storage buffer bram.
  86. * @base_address: contains the base address of the component.
  87. * @offset: The word offset from which the data should be read.
  88. *
  89. * A bram is used as a configuration memory cache. One frame of data can
  90. * be stored in this "storage buffer".
  91. **/
  92. static inline u32 buffer_icap_get_bram(void __iomem *base_address,
  93. u32 offset)
  94. {
  95. return in_be32(base_address + (offset << 2));
  96. }
  97. /**
  98. * buffer_icap_busy - Return true if the icap device is busy
  99. * @base_address: is the base address of the device
  100. *
  101. * The queries the low order bit of the status register, which
  102. * indicates whether the current configuration or readback operation
  103. * has completed.
  104. **/
  105. static inline bool buffer_icap_busy(void __iomem *base_address)
  106. {
  107. return (buffer_icap_get_status(base_address) & 1) == XHI_NOT_FINISHED;
  108. }
  109. /**
  110. * buffer_icap_busy - Return true if the icap device is not busy
  111. * @base_address: is the base address of the device
  112. *
  113. * The queries the low order bit of the status register, which
  114. * indicates whether the current configuration or readback operation
  115. * has completed.
  116. **/
  117. static inline bool buffer_icap_done(void __iomem *base_address)
  118. {
  119. return (buffer_icap_get_status(base_address) & 1) == XHI_FINISHED;
  120. }
  121. /**
  122. * buffer_icap_set_size - Set the size register.
  123. * @base_address: is the base address of the device
  124. * @data: The size in bytes.
  125. *
  126. * The size register holds the number of 8 bit bytes to transfer between
  127. * bram and the icap (or icap to bram).
  128. **/
  129. static inline void buffer_icap_set_size(void __iomem *base_address,
  130. u32 data)
  131. {
  132. out_be32(base_address + XHI_SIZE_REG_OFFSET, data);
  133. }
  134. /**
  135. * buffer_icap_set_offset - Set the bram offset register.
  136. * @base_address: contains the base address of the device.
  137. * @data: is the value to be written to the data register.
  138. *
  139. * The bram offset register holds the starting bram address to transfer
  140. * data from during configuration or write data to during readback.
  141. **/
  142. static inline void buffer_icap_set_offset(void __iomem *base_address,
  143. u32 data)
  144. {
  145. out_be32(base_address + XHI_BRAM_OFFSET_REG_OFFSET, data);
  146. }
  147. /**
  148. * buffer_icap_set_rnc - Set the RNC (Readback not Configure) register.
  149. * @base_address: contains the base address of the device.
  150. * @data: is the value to be written to the data register.
  151. *
  152. * The RNC register determines the direction of the data transfer. It
  153. * controls whether a configuration or readback take place. Writing to
  154. * this register initiates the transfer. A value of 1 initiates a
  155. * readback while writing a value of 0 initiates a configuration.
  156. **/
  157. static inline void buffer_icap_set_rnc(void __iomem *base_address,
  158. u32 data)
  159. {
  160. out_be32(base_address + XHI_RNC_REG_OFFSET, data);
  161. }
  162. /**
  163. * buffer_icap_set_bram - Write data to the storage buffer bram.
  164. * @base_address: contains the base address of the component.
  165. * @offset: The word offset at which the data should be written.
  166. * @data: The value to be written to the bram offset.
  167. *
  168. * A bram is used as a configuration memory cache. One frame of data can
  169. * be stored in this "storage buffer".
  170. **/
  171. static inline void buffer_icap_set_bram(void __iomem *base_address,
  172. u32 offset, u32 data)
  173. {
  174. out_be32(base_address + (offset << 2), data);
  175. }
  176. /**
  177. * buffer_icap_device_read - Transfer bytes from ICAP to the storage buffer.
  178. * @drvdata: a pointer to the drvdata.
  179. * @offset: The storage buffer start address.
  180. * @count: The number of words (32 bit) to read from the
  181. * device (ICAP).
  182. **/
  183. static int buffer_icap_device_read(struct hwicap_drvdata *drvdata,
  184. u32 offset, u32 count)
  185. {
  186. s32 retries = 0;
  187. void __iomem *base_address = drvdata->base_address;
  188. if (buffer_icap_busy(base_address))
  189. return -EBUSY;
  190. if ((offset + count) > XHI_MAX_BUFFER_INTS)
  191. return -EINVAL;
  192. /* setSize count*4 to get bytes. */
  193. buffer_icap_set_size(base_address, (count << 2));
  194. buffer_icap_set_offset(base_address, offset);
  195. buffer_icap_set_rnc(base_address, XHI_READBACK);
  196. while (buffer_icap_busy(base_address)) {
  197. retries++;
  198. if (retries > XHI_MAX_RETRIES)
  199. return -EBUSY;
  200. }
  201. return 0;
  202. };
  203. /**
  204. * buffer_icap_device_write - Transfer bytes from ICAP to the storage buffer.
  205. * @drvdata: a pointer to the drvdata.
  206. * @offset: The storage buffer start address.
  207. * @count: The number of words (32 bit) to read from the
  208. * device (ICAP).
  209. **/
  210. static int buffer_icap_device_write(struct hwicap_drvdata *drvdata,
  211. u32 offset, u32 count)
  212. {
  213. s32 retries = 0;
  214. void __iomem *base_address = drvdata->base_address;
  215. if (buffer_icap_busy(base_address))
  216. return -EBUSY;
  217. if ((offset + count) > XHI_MAX_BUFFER_INTS)
  218. return -EINVAL;
  219. /* setSize count*4 to get bytes. */
  220. buffer_icap_set_size(base_address, count << 2);
  221. buffer_icap_set_offset(base_address, offset);
  222. buffer_icap_set_rnc(base_address, XHI_CONFIGURE);
  223. while (buffer_icap_busy(base_address)) {
  224. retries++;
  225. if (retries > XHI_MAX_RETRIES)
  226. return -EBUSY;
  227. }
  228. return 0;
  229. };
  230. /**
  231. * buffer_icap_reset - Reset the logic of the icap device.
  232. * @drvdata: a pointer to the drvdata.
  233. *
  234. * Writing to the status register resets the ICAP logic in an internal
  235. * version of the core. For the version of the core published in EDK,
  236. * this is a noop.
  237. **/
  238. void buffer_icap_reset(struct hwicap_drvdata *drvdata)
  239. {
  240. out_be32(drvdata->base_address + XHI_STATUS_REG_OFFSET, 0xFEFE);
  241. }
  242. /**
  243. * buffer_icap_set_configuration - Load a partial bitstream from system memory.
  244. * @drvdata: a pointer to the drvdata.
  245. * @data: Kernel address of the partial bitstream.
  246. * @size: the size of the partial bitstream in 32 bit words.
  247. **/
  248. int buffer_icap_set_configuration(struct hwicap_drvdata *drvdata, u32 *data,
  249. u32 size)
  250. {
  251. int status;
  252. s32 buffer_count = 0;
  253. s32 num_writes = 0;
  254. bool dirty = 0;
  255. u32 i;
  256. void __iomem *base_address = drvdata->base_address;
  257. /* Loop through all the data */
  258. for (i = 0, buffer_count = 0; i < size; i++) {
  259. /* Copy data to bram */
  260. buffer_icap_set_bram(base_address, buffer_count, data[i]);
  261. dirty = 1;
  262. if (buffer_count < XHI_MAX_BUFFER_INTS - 1) {
  263. buffer_count++;
  264. continue;
  265. }
  266. /* Write data to ICAP */
  267. status = buffer_icap_device_write(
  268. drvdata,
  269. XHI_BUFFER_START,
  270. XHI_MAX_BUFFER_INTS);
  271. if (status != 0) {
  272. /* abort. */
  273. buffer_icap_reset(drvdata);
  274. return status;
  275. }
  276. buffer_count = 0;
  277. num_writes++;
  278. dirty = 0;
  279. }
  280. /* Write unwritten data to ICAP */
  281. if (dirty) {
  282. /* Write data to ICAP */
  283. status = buffer_icap_device_write(drvdata, XHI_BUFFER_START,
  284. buffer_count);
  285. if (status != 0) {
  286. /* abort. */
  287. buffer_icap_reset(drvdata);
  288. }
  289. return status;
  290. }
  291. return 0;
  292. };
  293. /**
  294. * buffer_icap_get_configuration - Read configuration data from the device.
  295. * @drvdata: a pointer to the drvdata.
  296. * @data: Address of the data representing the partial bitstream
  297. * @size: the size of the partial bitstream in 32 bit words.
  298. **/
  299. int buffer_icap_get_configuration(struct hwicap_drvdata *drvdata, u32 *data,
  300. u32 size)
  301. {
  302. int status;
  303. s32 buffer_count = 0;
  304. s32 read_count = 0;
  305. u32 i;
  306. void __iomem *base_address = drvdata->base_address;
  307. /* Loop through all the data */
  308. for (i = 0, buffer_count = XHI_MAX_BUFFER_INTS; i < size; i++) {
  309. if (buffer_count == XHI_MAX_BUFFER_INTS) {
  310. u32 words_remaining = size - i;
  311. u32 words_to_read =
  312. words_remaining <
  313. XHI_MAX_BUFFER_INTS ? words_remaining :
  314. XHI_MAX_BUFFER_INTS;
  315. /* Read data from ICAP */
  316. status = buffer_icap_device_read(
  317. drvdata,
  318. XHI_BUFFER_START,
  319. words_to_read);
  320. if (status != 0) {
  321. /* abort. */
  322. buffer_icap_reset(drvdata);
  323. return status;
  324. }
  325. buffer_count = 0;
  326. read_count++;
  327. }
  328. /* Copy data from bram */
  329. data[i] = buffer_icap_get_bram(base_address, buffer_count);
  330. buffer_count++;
  331. }
  332. return 0;
  333. };