fifo_icap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #define XHI_WFO_MAX_VACANCY 1024 /* Max Write FIFO Vacancy, in words */
  75. #define XHI_RFO_MAX_OCCUPANCY 256 /* Max Read FIFO Occupancy, in words */
  76. /* The maximum amount we can request from fifo_icap_get_configuration
  77. at once, in bytes. */
  78. #define XHI_MAX_READ_TRANSACTION_WORDS 0xFFF
  79. /**
  80. * fifo_icap_fifo_write - Write data to the write FIFO.
  81. * @drvdata: a pointer to the drvdata.
  82. * @data: the 32-bit value to be written to the FIFO.
  83. *
  84. * This function will silently fail if the fifo is full.
  85. **/
  86. static inline void fifo_icap_fifo_write(struct hwicap_drvdata *drvdata,
  87. u32 data)
  88. {
  89. dev_dbg(drvdata->dev, "fifo_write: %x\n", data);
  90. out_be32(drvdata->base_address + XHI_WF_OFFSET, data);
  91. }
  92. /**
  93. * fifo_icap_fifo_read - Read data from the Read FIFO.
  94. * @drvdata: a pointer to the drvdata.
  95. *
  96. * This function will silently fail if the fifo is empty.
  97. **/
  98. static inline u32 fifo_icap_fifo_read(struct hwicap_drvdata *drvdata)
  99. {
  100. u32 data = in_be32(drvdata->base_address + XHI_RF_OFFSET);
  101. dev_dbg(drvdata->dev, "fifo_read: %x\n", data);
  102. return data;
  103. }
  104. /**
  105. * fifo_icap_set_read_size - Set the the size register.
  106. * @drvdata: a pointer to the drvdata.
  107. * @data: the size of the following read transaction, in words.
  108. **/
  109. static inline void fifo_icap_set_read_size(struct hwicap_drvdata *drvdata,
  110. u32 data)
  111. {
  112. out_be32(drvdata->base_address + XHI_SZ_OFFSET, data);
  113. }
  114. /**
  115. * fifo_icap_start_config - Initiate a configuration (write) to the device.
  116. * @drvdata: a pointer to the drvdata.
  117. **/
  118. static inline void fifo_icap_start_config(struct hwicap_drvdata *drvdata)
  119. {
  120. out_be32(drvdata->base_address + XHI_CR_OFFSET, XHI_CR_WRITE_MASK);
  121. dev_dbg(drvdata->dev, "configuration started\n");
  122. }
  123. /**
  124. * fifo_icap_start_readback - Initiate a readback from the device.
  125. * @drvdata: a pointer to the drvdata.
  126. **/
  127. static inline void fifo_icap_start_readback(struct hwicap_drvdata *drvdata)
  128. {
  129. out_be32(drvdata->base_address + XHI_CR_OFFSET, XHI_CR_READ_MASK);
  130. dev_dbg(drvdata->dev, "readback started\n");
  131. }
  132. /**
  133. * fifo_icap_get_status - Get the contents of the status register.
  134. * @drvdata: a pointer to the drvdata.
  135. *
  136. * The status register contains the ICAP status and the done bit.
  137. *
  138. * D8 - cfgerr
  139. * D7 - dalign
  140. * D6 - rip
  141. * D5 - in_abort_l
  142. * D4 - Always 1
  143. * D3 - Always 1
  144. * D2 - Always 1
  145. * D1 - Always 1
  146. * D0 - Done bit
  147. **/
  148. u32 fifo_icap_get_status(struct hwicap_drvdata *drvdata)
  149. {
  150. u32 status = in_be32(drvdata->base_address + XHI_SR_OFFSET);
  151. dev_dbg(drvdata->dev, "Getting status = %x\n", status);
  152. return status;
  153. }
  154. /**
  155. * fifo_icap_busy - Return true if the ICAP is still processing a transaction.
  156. * @drvdata: a pointer to the drvdata.
  157. **/
  158. static inline u32 fifo_icap_busy(struct hwicap_drvdata *drvdata)
  159. {
  160. u32 status = in_be32(drvdata->base_address + XHI_SR_OFFSET);
  161. return (status & XHI_SR_DONE_MASK) ? 0 : 1;
  162. }
  163. /**
  164. * fifo_icap_write_fifo_vacancy - Query the write fifo available space.
  165. * @drvdata: a pointer to the drvdata.
  166. *
  167. * Return the number of words that can be safely pushed into the write fifo.
  168. **/
  169. static inline u32 fifo_icap_write_fifo_vacancy(
  170. struct hwicap_drvdata *drvdata)
  171. {
  172. return in_be32(drvdata->base_address + XHI_WFV_OFFSET);
  173. }
  174. /**
  175. * fifo_icap_read_fifo_occupancy - Query the read fifo available data.
  176. * @drvdata: a pointer to the drvdata.
  177. *
  178. * Return the number of words that can be safely read from the read fifo.
  179. **/
  180. static inline u32 fifo_icap_read_fifo_occupancy(
  181. struct hwicap_drvdata *drvdata)
  182. {
  183. return in_be32(drvdata->base_address + XHI_RFO_OFFSET);
  184. }
  185. /**
  186. * fifo_icap_set_configuration - Send configuration data to the ICAP.
  187. * @drvdata: a pointer to the drvdata.
  188. * @frame_buffer: a pointer to the data to be written to the
  189. * ICAP device.
  190. * @num_words: the number of words (32 bit) to write to the ICAP
  191. * device.
  192. * This function writes the given user data to the Write FIFO in
  193. * polled mode and starts the transfer of the data to
  194. * the ICAP device.
  195. **/
  196. int fifo_icap_set_configuration(struct hwicap_drvdata *drvdata,
  197. u32 *frame_buffer, u32 num_words)
  198. {
  199. u32 write_fifo_vacancy = 0;
  200. u32 retries = 0;
  201. u32 remaining_words;
  202. dev_dbg(drvdata->dev, "fifo_set_configuration\n");
  203. /*
  204. * Check if the ICAP device is Busy with the last Read/Write
  205. */
  206. if (fifo_icap_busy(drvdata))
  207. return -EBUSY;
  208. /*
  209. * Set up the buffer pointer and the words to be transferred.
  210. */
  211. remaining_words = num_words;
  212. while (remaining_words > 0) {
  213. /*
  214. * Wait until we have some data in the fifo.
  215. */
  216. while (write_fifo_vacancy == 0) {
  217. write_fifo_vacancy =
  218. fifo_icap_write_fifo_vacancy(drvdata);
  219. retries++;
  220. if (retries > XHI_MAX_RETRIES)
  221. return -EIO;
  222. }
  223. /*
  224. * Write data into the Write FIFO.
  225. */
  226. while ((write_fifo_vacancy != 0) &&
  227. (remaining_words > 0)) {
  228. fifo_icap_fifo_write(drvdata, *frame_buffer);
  229. remaining_words--;
  230. write_fifo_vacancy--;
  231. frame_buffer++;
  232. }
  233. /* Start pushing whatever is in the FIFO into the ICAP. */
  234. fifo_icap_start_config(drvdata);
  235. }
  236. /* Wait until the write has finished. */
  237. while (fifo_icap_busy(drvdata)) {
  238. retries++;
  239. if (retries > XHI_MAX_RETRIES)
  240. break;
  241. }
  242. dev_dbg(drvdata->dev, "done fifo_set_configuration\n");
  243. /*
  244. * If the requested number of words have not been read from
  245. * the device then indicate failure.
  246. */
  247. if (remaining_words != 0)
  248. return -EIO;
  249. return 0;
  250. }
  251. /**
  252. * fifo_icap_get_configuration - Read configuration data from the device.
  253. * @drvdata: a pointer to the drvdata.
  254. * @data: Address of the data representing the partial bitstream
  255. * @size: the size of the partial bitstream in 32 bit words.
  256. *
  257. * This function reads the specified number of words from the ICAP device in
  258. * the polled mode.
  259. */
  260. int fifo_icap_get_configuration(struct hwicap_drvdata *drvdata,
  261. u32 *frame_buffer, u32 num_words)
  262. {
  263. u32 read_fifo_occupancy = 0;
  264. u32 retries = 0;
  265. u32 *data = frame_buffer;
  266. u32 remaining_words;
  267. u32 words_to_read;
  268. dev_dbg(drvdata->dev, "fifo_get_configuration\n");
  269. /*
  270. * Check if the ICAP device is Busy with the last Write/Read
  271. */
  272. if (fifo_icap_busy(drvdata))
  273. return -EBUSY;
  274. remaining_words = num_words;
  275. while (remaining_words > 0) {
  276. words_to_read = remaining_words;
  277. /* The hardware has a limit on the number of words
  278. that can be read at one time. */
  279. if (words_to_read > XHI_MAX_READ_TRANSACTION_WORDS)
  280. words_to_read = XHI_MAX_READ_TRANSACTION_WORDS;
  281. remaining_words -= words_to_read;
  282. fifo_icap_set_read_size(drvdata, words_to_read);
  283. fifo_icap_start_readback(drvdata);
  284. while (words_to_read > 0) {
  285. /* Wait until we have some data in the fifo. */
  286. while (read_fifo_occupancy == 0) {
  287. read_fifo_occupancy =
  288. fifo_icap_read_fifo_occupancy(drvdata);
  289. retries++;
  290. if (retries > XHI_MAX_RETRIES)
  291. return -EIO;
  292. }
  293. if (read_fifo_occupancy > words_to_read)
  294. read_fifo_occupancy = words_to_read;
  295. words_to_read -= read_fifo_occupancy;
  296. /* Read the data from the Read FIFO. */
  297. while (read_fifo_occupancy != 0) {
  298. *data++ = fifo_icap_fifo_read(drvdata);
  299. read_fifo_occupancy--;
  300. }
  301. }
  302. }
  303. dev_dbg(drvdata->dev, "done fifo_get_configuration\n");
  304. return 0;
  305. }
  306. /**
  307. * buffer_icap_reset - Reset the logic of the icap device.
  308. * @drvdata: a pointer to the drvdata.
  309. *
  310. * This function forces the software reset of the complete HWICAP device.
  311. * All the registers will return to the default value and the FIFO is also
  312. * flushed as a part of this software reset.
  313. */
  314. void fifo_icap_reset(struct hwicap_drvdata *drvdata)
  315. {
  316. u32 reg_data;
  317. /*
  318. * Reset the device by setting/clearing the RESET bit in the
  319. * Control Register.
  320. */
  321. reg_data = in_be32(drvdata->base_address + XHI_CR_OFFSET);
  322. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  323. reg_data | XHI_CR_SW_RESET_MASK);
  324. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  325. reg_data & (~XHI_CR_SW_RESET_MASK));
  326. }
  327. /**
  328. * fifo_icap_flush_fifo - This function flushes the FIFOs in the device.
  329. * @drvdata: a pointer to the drvdata.
  330. */
  331. void fifo_icap_flush_fifo(struct hwicap_drvdata *drvdata)
  332. {
  333. u32 reg_data;
  334. /*
  335. * Flush the FIFO by setting/clearing the FIFO Clear bit in the
  336. * Control Register.
  337. */
  338. reg_data = in_be32(drvdata->base_address + XHI_CR_OFFSET);
  339. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  340. reg_data | XHI_CR_FIFO_CLR_MASK);
  341. out_be32(drvdata->base_address + XHI_CR_OFFSET,
  342. reg_data & (~XHI_CR_FIFO_CLR_MASK));
  343. }