fifo_icap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 2007-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 "fifo_icap.h"
  36. /* Register offsets for the XHwIcap device. */
  37. #define XHI_GIER_OFFSET 0x1C /* Device Global Interrupt Enable Reg */
  38. #define XHI_IPISR_OFFSET 0x20 /* Interrupt Status Register */
  39. #define XHI_IPIER_OFFSET 0x28 /* Interrupt Enable Register */
  40. #define XHI_WF_OFFSET 0x100 /* Write FIFO */
  41. #define XHI_RF_OFFSET 0x104 /* Read FIFO */
  42. #define XHI_SZ_OFFSET 0x108 /* Size Register */
  43. #define XHI_CR_OFFSET 0x10C /* Control Register */
  44. #define XHI_SR_OFFSET 0x110 /* Status Register */
  45. #define XHI_WFV_OFFSET 0x114 /* Write FIFO Vacancy Register */
  46. #define XHI_RFO_OFFSET 0x118 /* Read FIFO Occupancy Register */
  47. /* Device Global Interrupt Enable Register (GIER) bit definitions */
  48. #define XHI_GIER_GIE_MASK 0x80000000 /* Global Interrupt enable Mask */
  49. /**
  50. * HwIcap Device Interrupt Status/Enable Registers
  51. *
  52. * Interrupt Status Register (IPISR) : This register holds the
  53. * interrupt status flags for the device. These bits are toggle on
  54. * write.
  55. *
  56. * Interrupt Enable Register (IPIER) : This register is used to enable
  57. * interrupt sources for the device.
  58. * Writing a '1' to a bit enables the corresponding interrupt.
  59. * Writing a '0' to a bit disables the corresponding interrupt.
  60. *
  61. * IPISR/IPIER registers have the same bit definitions and are only defined
  62. * once.
  63. */
  64. #define XHI_IPIXR_RFULL_MASK 0x00000008 /* Read FIFO Full */
  65. #define XHI_IPIXR_WEMPTY_MASK 0x00000004 /* Write FIFO Empty */
  66. #define XHI_IPIXR_RDP_MASK 0x00000002 /* Read FIFO half full */
  67. #define XHI_IPIXR_WRP_MASK 0x00000001 /* Write FIFO half full */
  68. #define XHI_IPIXR_ALL_MASK 0x0000000F /* Mask of all interrupts */
  69. /* Control Register (CR) */
  70. #define XHI_CR_SW_RESET_MASK 0x00000008 /* SW Reset Mask */
  71. #define XHI_CR_FIFO_CLR_MASK 0x00000004 /* FIFO Clear Mask */
  72. #define XHI_CR_READ_MASK 0x00000002 /* Read from ICAP to FIFO */
  73. #define XHI_CR_WRITE_MASK 0x00000001 /* Write from FIFO to ICAP */
  74. /* Status Register (SR) */
  75. #define XHI_SR_CFGERR_N_MASK 0x00000100 /* Config Error Mask */
  76. #define XHI_SR_DALIGN_MASK 0x00000080 /* Data Alignment Mask */
  77. #define XHI_SR_RIP_MASK 0x00000040 /* Read back Mask */
  78. #define XHI_SR_IN_ABORT_N_MASK 0x00000020 /* Select Map Abort Mask */
  79. #define XHI_SR_DONE_MASK 0x00000001 /* Done bit Mask */
  80. #define XHI_WFO_MAX_VACANCY 1024 /* Max Write FIFO Vacancy, in words */
  81. #define XHI_RFO_MAX_OCCUPANCY 256 /* Max Read FIFO Occupancy, in words */
  82. /* The maximum amount we can request from fifo_icap_get_configuration
  83. at once, in bytes. */
  84. #define XHI_MAX_READ_TRANSACTION_WORDS 0xFFF
  85. /**
  86. * fifo_icap_fifo_write - Write data to the write FIFO.
  87. * @drvdata: a pointer to the drvdata.
  88. * @data: the 32-bit value to be written to the FIFO.
  89. *
  90. * This function will silently fail if the fifo is full.
  91. **/
  92. static inline void fifo_icap_fifo_write(struct hwicap_drvdata *drvdata,
  93. u32 data)
  94. {
  95. dev_dbg(drvdata->dev, "fifo_write: %x\n", data);
  96. out_be32(drvdata->base_address + XHI_WF_OFFSET, data);
  97. }
  98. /**
  99. * fifo_icap_fifo_read - Read data from the Read FIFO.
  100. * @drvdata: a pointer to the drvdata.
  101. *
  102. * This function will silently fail if the fifo is empty.
  103. **/
  104. static inline u32 fifo_icap_fifo_read(struct hwicap_drvdata *drvdata)
  105. {
  106. u32 data = in_be32(drvdata->base_address + XHI_RF_OFFSET);
  107. dev_dbg(drvdata->dev, "fifo_read: %x\n", data);
  108. return data;
  109. }
  110. /**
  111. * fifo_icap_set_read_size - Set the the size register.
  112. * @drvdata: a pointer to the drvdata.
  113. * @data: the size of the following read transaction, in words.
  114. **/
  115. static inline void fifo_icap_set_read_size(struct hwicap_drvdata *drvdata,
  116. u32 data)
  117. {
  118. out_be32(drvdata->base_address + XHI_SZ_OFFSET, data);
  119. }
  120. /**
  121. * fifo_icap_start_config - Initiate a configuration (write) to the device.
  122. * @drvdata: a pointer to the drvdata.
  123. **/
  124. static inline void fifo_icap_start_config(struct hwicap_drvdata *drvdata)
  125. {
  126. out_be32(drvdata->base_address + XHI_CR_OFFSET, XHI_CR_WRITE_MASK);
  127. dev_dbg(drvdata->dev, "configuration started\n");
  128. }
  129. /**
  130. * fifo_icap_start_readback - Initiate a readback from the device.
  131. * @drvdata: a pointer to the drvdata.
  132. **/
  133. static inline void fifo_icap_start_readback(struct hwicap_drvdata *drvdata)
  134. {
  135. out_be32(drvdata->base_address + XHI_CR_OFFSET, XHI_CR_READ_MASK);
  136. dev_dbg(drvdata->dev, "readback started\n");
  137. }
  138. /**
  139. * fifo_icap_busy - Return true if the ICAP is still processing a transaction.
  140. * @drvdata: a pointer to the drvdata.
  141. **/
  142. static inline u32 fifo_icap_busy(struct hwicap_drvdata *drvdata)
  143. {
  144. u32 status = in_be32(drvdata->base_address + XHI_SR_OFFSET);
  145. dev_dbg(drvdata->dev, "Getting status = %x\n", status);
  146. return (status & XHI_SR_DONE_MASK) ? 0 : 1;
  147. }
  148. /**
  149. * fifo_icap_write_fifo_vacancy - Query the write fifo available space.
  150. * @drvdata: a pointer to the drvdata.
  151. *
  152. * Return the number of words that can be safely pushed into the write fifo.
  153. **/
  154. static inline u32 fifo_icap_write_fifo_vacancy(
  155. struct hwicap_drvdata *drvdata)
  156. {
  157. return in_be32(drvdata->base_address + XHI_WFV_OFFSET);
  158. }
  159. /**
  160. * fifo_icap_read_fifo_occupancy - Query the read fifo available data.
  161. * @drvdata: a pointer to the drvdata.
  162. *
  163. * Return the number of words that can be safely read from the read fifo.
  164. **/
  165. static inline u32 fifo_icap_read_fifo_occupancy(
  166. struct hwicap_drvdata *drvdata)
  167. {
  168. return in_be32(drvdata->base_address + XHI_RFO_OFFSET);
  169. }
  170. /**
  171. * fifo_icap_set_configuration - Send configuration data to the ICAP.
  172. * @drvdata: a pointer to the drvdata.
  173. * @frame_buffer: a pointer to the data to be written to the
  174. * ICAP device.
  175. * @num_words: the number of words (32 bit) to write to the ICAP
  176. * device.
  177. * This function writes the given user data to the Write FIFO in
  178. * polled mode and starts the transfer of the data to
  179. * the ICAP device.
  180. **/
  181. int fifo_icap_set_configuration(struct hwicap_drvdata *drvdata,
  182. u32 *frame_buffer, u32 num_words)
  183. {
  184. u32 write_fifo_vacancy = 0;
  185. u32 retries = 0;
  186. u32 remaining_words;
  187. dev_dbg(drvdata->dev, "fifo_set_configuration\n");
  188. /*
  189. * Check if the ICAP device is Busy with the last Read/Write
  190. */
  191. if (fifo_icap_busy(drvdata))
  192. return -EBUSY;
  193. /*
  194. * Set up the buffer pointer and the words to be transferred.
  195. */
  196. remaining_words = num_words;
  197. while (remaining_words > 0) {
  198. /*
  199. * Wait until we have some data in the fifo.
  200. */
  201. while (write_fifo_vacancy == 0) {
  202. write_fifo_vacancy =
  203. fifo_icap_write_fifo_vacancy(drvdata);
  204. retries++;
  205. if (retries > XHI_MAX_RETRIES)
  206. return -EIO;
  207. }
  208. /*
  209. * Write data into the Write FIFO.
  210. */
  211. while ((write_fifo_vacancy != 0) &&
  212. (remaining_words > 0)) {
  213. fifo_icap_fifo_write(drvdata, *frame_buffer);
  214. remaining_words--;
  215. write_fifo_vacancy--;
  216. frame_buffer++;
  217. }
  218. /* Start pushing whatever is in the FIFO into the ICAP. */
  219. fifo_icap_start_config(drvdata);
  220. }
  221. /* Wait until the write has finished. */
  222. while (fifo_icap_busy(drvdata)) {
  223. retries++;
  224. if (retries > XHI_MAX_RETRIES)
  225. break;
  226. }
  227. dev_dbg(drvdata->dev, "done fifo_set_configuration\n");
  228. /*
  229. * If the requested number of words have not been read from
  230. * the device then indicate failure.
  231. */
  232. if (remaining_words != 0)
  233. return -EIO;
  234. return 0;
  235. }
  236. /**
  237. * fifo_icap_get_configuration - Read configuration data from the device.
  238. * @drvdata: a pointer to the drvdata.
  239. * @data: Address of the data representing the partial bitstream
  240. * @size: the size of the partial bitstream in 32 bit words.
  241. *
  242. * This function reads the specified number of words from the ICAP device in
  243. * the polled mode.
  244. */
  245. int fifo_icap_get_configuration(struct hwicap_drvdata *drvdata,
  246. u32 *frame_buffer, u32 num_words)
  247. {
  248. u32 read_fifo_occupancy = 0;
  249. u32 retries = 0;
  250. u32 *data = frame_buffer;
  251. u32 remaining_words;
  252. u32 words_to_read;
  253. dev_dbg(drvdata->dev, "fifo_get_configuration\n");
  254. /*
  255. * Check if the ICAP device is Busy with the last Write/Read
  256. */
  257. if (fifo_icap_busy(drvdata))
  258. return -EBUSY;
  259. remaining_words = num_words;
  260. while (remaining_words > 0) {
  261. words_to_read = remaining_words;
  262. /* The hardware has a limit on the number of words
  263. that can be read at one time. */
  264. if (words_to_read > XHI_MAX_READ_TRANSACTION_WORDS)
  265. words_to_read = XHI_MAX_READ_TRANSACTION_WORDS;
  266. remaining_words -= words_to_read;
  267. fifo_icap_set_read_size(drvdata, words_to_read);
  268. fifo_icap_start_readback(drvdata);
  269. while (words_to_read > 0) {
  270. /* Wait until we have some data in the fifo. */
  271. while (read_fifo_occupancy == 0) {
  272. read_fifo_occupancy =
  273. fifo_icap_read_fifo_occupancy(drvdata);
  274. retries++;
  275. if (retries > XHI_MAX_RETRIES)
  276. return -EIO;
  277. }
  278. if (read_fifo_occupancy > words_to_read)
  279. read_fifo_occupancy = words_to_read;
  280. words_to_read -= read_fifo_occupancy;
  281. /* Read the data from the Read FIFO. */
  282. while (read_fifo_occupancy != 0) {
  283. *data++ = fifo_icap_fifo_read(drvdata);
  284. read_fifo_occupancy--;
  285. }
  286. }
  287. }
  288. dev_dbg(drvdata->dev, "done fifo_get_configuration\n");
  289. return 0;
  290. }
  291. /**
  292. * buffer_icap_reset - Reset the logic of the icap device.
  293. * @drvdata: a pointer to the drvdata.
  294. *
  295. * This function forces the software reset of the complete HWICAP device.
  296. * All the registers will return to the default value and the FIFO is also
  297. * flushed as a part of this software reset.
  298. */
  299. void fifo_icap_reset(struct hwicap_drvdata *drvdata)
  300. {
  301. u32 reg_data;
  302. /*
  303. * Reset the device by setting/clearing the RESET bit in the
  304. * Control Register.
  305. */
  306. reg_data = in_be32(drvdata->base_address + XHI_CR_OFFSET);
  307. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  308. reg_data | XHI_CR_SW_RESET_MASK);
  309. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  310. reg_data & (~XHI_CR_SW_RESET_MASK));
  311. }
  312. /**
  313. * fifo_icap_flush_fifo - This function flushes the FIFOs in the device.
  314. * @drvdata: a pointer to the drvdata.
  315. */
  316. void fifo_icap_flush_fifo(struct hwicap_drvdata *drvdata)
  317. {
  318. u32 reg_data;
  319. /*
  320. * Flush the FIFO by setting/clearing the FIFO Clear bit in the
  321. * Control Register.
  322. */
  323. reg_data = in_be32(drvdata->base_address + XHI_CR_OFFSET);
  324. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  325. reg_data | XHI_CR_FIFO_CLR_MASK);
  326. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  327. reg_data & (~XHI_CR_FIFO_CLR_MASK));
  328. }