fifo_icap.c 12 KB

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