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