buffer_icap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. * @drvdata: a pointer to the drvdata.
  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. u32 buffer_icap_get_status(struct hwicap_drvdata *drvdata)
  81. {
  82. return in_be32(drvdata->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. u32 status = in_be32(base_address + XHI_STATUS_REG_OFFSET);
  108. return (status & 1) == XHI_NOT_FINISHED;
  109. }
  110. /**
  111. * buffer_icap_set_size - Set the size register.
  112. * @base_address: is the base address of the device
  113. * @data: The size in bytes.
  114. *
  115. * The size register holds the number of 8 bit bytes to transfer between
  116. * bram and the icap (or icap to bram).
  117. **/
  118. static inline void buffer_icap_set_size(void __iomem *base_address,
  119. u32 data)
  120. {
  121. out_be32(base_address + XHI_SIZE_REG_OFFSET, data);
  122. }
  123. /**
  124. * buffer_icap_set_offset - Set the bram offset register.
  125. * @base_address: contains the base address of the device.
  126. * @data: is the value to be written to the data register.
  127. *
  128. * The bram offset register holds the starting bram address to transfer
  129. * data from during configuration or write data to during readback.
  130. **/
  131. static inline void buffer_icap_set_offset(void __iomem *base_address,
  132. u32 data)
  133. {
  134. out_be32(base_address + XHI_BRAM_OFFSET_REG_OFFSET, data);
  135. }
  136. /**
  137. * buffer_icap_set_rnc - Set the RNC (Readback not Configure) register.
  138. * @base_address: contains the base address of the device.
  139. * @data: is the value to be written to the data register.
  140. *
  141. * The RNC register determines the direction of the data transfer. It
  142. * controls whether a configuration or readback take place. Writing to
  143. * this register initiates the transfer. A value of 1 initiates a
  144. * readback while writing a value of 0 initiates a configuration.
  145. **/
  146. static inline void buffer_icap_set_rnc(void __iomem *base_address,
  147. u32 data)
  148. {
  149. out_be32(base_address + XHI_RNC_REG_OFFSET, data);
  150. }
  151. /**
  152. * buffer_icap_set_bram - Write data to the storage buffer bram.
  153. * @base_address: contains the base address of the component.
  154. * @offset: The word offset at which the data should be written.
  155. * @data: The value to be written to the bram offset.
  156. *
  157. * A bram is used as a configuration memory cache. One frame of data can
  158. * be stored in this "storage buffer".
  159. **/
  160. static inline void buffer_icap_set_bram(void __iomem *base_address,
  161. u32 offset, u32 data)
  162. {
  163. out_be32(base_address + (offset << 2), data);
  164. }
  165. /**
  166. * buffer_icap_device_read - Transfer bytes from ICAP to the storage buffer.
  167. * @drvdata: a pointer to the drvdata.
  168. * @offset: The storage buffer start address.
  169. * @count: The number of words (32 bit) to read from the
  170. * device (ICAP).
  171. **/
  172. static int buffer_icap_device_read(struct hwicap_drvdata *drvdata,
  173. u32 offset, u32 count)
  174. {
  175. s32 retries = 0;
  176. void __iomem *base_address = drvdata->base_address;
  177. if (buffer_icap_busy(base_address))
  178. return -EBUSY;
  179. if ((offset + count) > XHI_MAX_BUFFER_INTS)
  180. return -EINVAL;
  181. /* setSize count*4 to get bytes. */
  182. buffer_icap_set_size(base_address, (count << 2));
  183. buffer_icap_set_offset(base_address, offset);
  184. buffer_icap_set_rnc(base_address, XHI_READBACK);
  185. while (buffer_icap_busy(base_address)) {
  186. retries++;
  187. if (retries > XHI_MAX_RETRIES)
  188. return -EBUSY;
  189. }
  190. return 0;
  191. };
  192. /**
  193. * buffer_icap_device_write - Transfer bytes from ICAP to the storage buffer.
  194. * @drvdata: a pointer to the drvdata.
  195. * @offset: The storage buffer start address.
  196. * @count: The number of words (32 bit) to read from the
  197. * device (ICAP).
  198. **/
  199. static int buffer_icap_device_write(struct hwicap_drvdata *drvdata,
  200. u32 offset, u32 count)
  201. {
  202. s32 retries = 0;
  203. void __iomem *base_address = drvdata->base_address;
  204. if (buffer_icap_busy(base_address))
  205. return -EBUSY;
  206. if ((offset + count) > XHI_MAX_BUFFER_INTS)
  207. return -EINVAL;
  208. /* setSize count*4 to get bytes. */
  209. buffer_icap_set_size(base_address, count << 2);
  210. buffer_icap_set_offset(base_address, offset);
  211. buffer_icap_set_rnc(base_address, XHI_CONFIGURE);
  212. while (buffer_icap_busy(base_address)) {
  213. retries++;
  214. if (retries > XHI_MAX_RETRIES)
  215. return -EBUSY;
  216. }
  217. return 0;
  218. };
  219. /**
  220. * buffer_icap_reset - Reset the logic of the icap device.
  221. * @drvdata: a pointer to the drvdata.
  222. *
  223. * Writing to the status register resets the ICAP logic in an internal
  224. * version of the core. For the version of the core published in EDK,
  225. * this is a noop.
  226. **/
  227. void buffer_icap_reset(struct hwicap_drvdata *drvdata)
  228. {
  229. out_be32(drvdata->base_address + XHI_STATUS_REG_OFFSET, 0xFEFE);
  230. }
  231. /**
  232. * buffer_icap_set_configuration - Load a partial bitstream from system memory.
  233. * @drvdata: a pointer to the drvdata.
  234. * @data: Kernel address of the partial bitstream.
  235. * @size: the size of the partial bitstream in 32 bit words.
  236. **/
  237. int buffer_icap_set_configuration(struct hwicap_drvdata *drvdata, u32 *data,
  238. u32 size)
  239. {
  240. int status;
  241. s32 buffer_count = 0;
  242. s32 num_writes = 0;
  243. bool dirty = 0;
  244. u32 i;
  245. void __iomem *base_address = drvdata->base_address;
  246. /* Loop through all the data */
  247. for (i = 0, buffer_count = 0; i < size; i++) {
  248. /* Copy data to bram */
  249. buffer_icap_set_bram(base_address, buffer_count, data[i]);
  250. dirty = 1;
  251. if (buffer_count < XHI_MAX_BUFFER_INTS - 1) {
  252. buffer_count++;
  253. continue;
  254. }
  255. /* Write data to ICAP */
  256. status = buffer_icap_device_write(
  257. drvdata,
  258. XHI_BUFFER_START,
  259. XHI_MAX_BUFFER_INTS);
  260. if (status != 0) {
  261. /* abort. */
  262. buffer_icap_reset(drvdata);
  263. return status;
  264. }
  265. buffer_count = 0;
  266. num_writes++;
  267. dirty = 0;
  268. }
  269. /* Write unwritten data to ICAP */
  270. if (dirty) {
  271. /* Write data to ICAP */
  272. status = buffer_icap_device_write(drvdata, XHI_BUFFER_START,
  273. buffer_count);
  274. if (status != 0) {
  275. /* abort. */
  276. buffer_icap_reset(drvdata);
  277. }
  278. return status;
  279. }
  280. return 0;
  281. };
  282. /**
  283. * buffer_icap_get_configuration - Read configuration data from the device.
  284. * @drvdata: a pointer to the drvdata.
  285. * @data: Address of the data representing the partial bitstream
  286. * @size: the size of the partial bitstream in 32 bit words.
  287. **/
  288. int buffer_icap_get_configuration(struct hwicap_drvdata *drvdata, u32 *data,
  289. u32 size)
  290. {
  291. int status;
  292. s32 buffer_count = 0;
  293. s32 read_count = 0;
  294. u32 i;
  295. void __iomem *base_address = drvdata->base_address;
  296. /* Loop through all the data */
  297. for (i = 0, buffer_count = XHI_MAX_BUFFER_INTS; i < size; i++) {
  298. if (buffer_count == XHI_MAX_BUFFER_INTS) {
  299. u32 words_remaining = size - i;
  300. u32 words_to_read =
  301. words_remaining <
  302. XHI_MAX_BUFFER_INTS ? words_remaining :
  303. XHI_MAX_BUFFER_INTS;
  304. /* Read data from ICAP */
  305. status = buffer_icap_device_read(
  306. drvdata,
  307. XHI_BUFFER_START,
  308. words_to_read);
  309. if (status != 0) {
  310. /* abort. */
  311. buffer_icap_reset(drvdata);
  312. return status;
  313. }
  314. buffer_count = 0;
  315. read_count++;
  316. }
  317. /* Copy data from bram */
  318. data[i] = buffer_icap_get_bram(base_address, buffer_count);
  319. buffer_count++;
  320. }
  321. return 0;
  322. };