aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/fpga
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/fpga')
-rw-r--r--drivers/fpga/fpga-mgr.c236
-rw-r--r--drivers/fpga/zynq-fpga.c233
2 files changed, 386 insertions, 83 deletions
diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
index f0a69d3e60a5..86d2cb203533 100644
--- a/drivers/fpga/fpga-mgr.c
+++ b/drivers/fpga/fpga-mgr.c
@@ -25,16 +25,106 @@
25#include <linux/of.h> 25#include <linux/of.h>
26#include <linux/mutex.h> 26#include <linux/mutex.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/scatterlist.h>
29#include <linux/highmem.h>
28 30
29static DEFINE_IDA(fpga_mgr_ida); 31static DEFINE_IDA(fpga_mgr_ida);
30static struct class *fpga_mgr_class; 32static struct class *fpga_mgr_class;
31 33
34/*
35 * Call the low level driver's write_init function. This will do the
36 * device-specific things to get the FPGA into the state where it is ready to
37 * receive an FPGA image. The low level driver only gets to see the first
38 * initial_header_size bytes in the buffer.
39 */
40static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
41 struct fpga_image_info *info,
42 const char *buf, size_t count)
43{
44 int ret;
45
46 mgr->state = FPGA_MGR_STATE_WRITE_INIT;
47 if (!mgr->mops->initial_header_size)
48 ret = mgr->mops->write_init(mgr, info, NULL, 0);
49 else
50 ret = mgr->mops->write_init(
51 mgr, info, buf, min(mgr->mops->initial_header_size, count));
52
53 if (ret) {
54 dev_err(&mgr->dev, "Error preparing FPGA for writing\n");
55 mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
56 return ret;
57 }
58
59 return 0;
60}
61
62static int fpga_mgr_write_init_sg(struct fpga_manager *mgr,
63 struct fpga_image_info *info,
64 struct sg_table *sgt)
65{
66 struct sg_mapping_iter miter;
67 size_t len;
68 char *buf;
69 int ret;
70
71 if (!mgr->mops->initial_header_size)
72 return fpga_mgr_write_init_buf(mgr, info, NULL, 0);
73
74 /*
75 * First try to use miter to map the first fragment to access the
76 * header, this is the typical path.
77 */
78 sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
79 if (sg_miter_next(&miter) &&
80 miter.length >= mgr->mops->initial_header_size) {
81 ret = fpga_mgr_write_init_buf(mgr, info, miter.addr,
82 miter.length);
83 sg_miter_stop(&miter);
84 return ret;
85 }
86 sg_miter_stop(&miter);
87
88 /* Otherwise copy the fragments into temporary memory. */
89 buf = kmalloc(mgr->mops->initial_header_size, GFP_KERNEL);
90 if (!buf)
91 return -ENOMEM;
92
93 len = sg_copy_to_buffer(sgt->sgl, sgt->nents, buf,
94 mgr->mops->initial_header_size);
95 ret = fpga_mgr_write_init_buf(mgr, info, buf, len);
96
97 kfree(buf);
98
99 return ret;
100}
101
102/*
103 * After all the FPGA image has been written, do the device specific steps to
104 * finish and set the FPGA into operating mode.
105 */
106static int fpga_mgr_write_complete(struct fpga_manager *mgr,
107 struct fpga_image_info *info)
108{
109 int ret;
110
111 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
112 ret = mgr->mops->write_complete(mgr, info);
113 if (ret) {
114 dev_err(&mgr->dev, "Error after writing image data to FPGA\n");
115 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
116 return ret;
117 }
118 mgr->state = FPGA_MGR_STATE_OPERATING;
119
120 return 0;
121}
122
32/** 123/**
33 * fpga_mgr_buf_load - load fpga from image in buffer 124 * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
34 * @mgr: fpga manager 125 * @mgr: fpga manager
35 * @info: fpga image specific information 126 * @info: fpga image specific information
36 * @buf: buffer contain fpga image 127 * @sgt: scatterlist table
37 * @count: byte count of buf
38 * 128 *
39 * Step the low level fpga manager through the device-specific steps of getting 129 * Step the low level fpga manager through the device-specific steps of getting
40 * an FPGA ready to be configured, writing the image to it, then doing whatever 130 * an FPGA ready to be configured, writing the image to it, then doing whatever
@@ -42,54 +132,139 @@ static struct class *fpga_mgr_class;
42 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is 132 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
43 * not an error code. 133 * not an error code.
44 * 134 *
135 * This is the preferred entry point for FPGA programming, it does not require
136 * any contiguous kernel memory.
137 *
45 * Return: 0 on success, negative error code otherwise. 138 * Return: 0 on success, negative error code otherwise.
46 */ 139 */
47int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info, 140int fpga_mgr_buf_load_sg(struct fpga_manager *mgr, struct fpga_image_info *info,
48 const char *buf, size_t count) 141 struct sg_table *sgt)
49{ 142{
50 struct device *dev = &mgr->dev;
51 int ret; 143 int ret;
52 144
53 /* 145 ret = fpga_mgr_write_init_sg(mgr, info, sgt);
54 * Call the low level driver's write_init function. This will do the 146 if (ret)
55 * device-specific things to get the FPGA into the state where it is 147 return ret;
56 * ready to receive an FPGA image. The low level driver only gets to 148
57 * see the first initial_header_size bytes in the buffer. 149 /* Write the FPGA image to the FPGA. */
58 */ 150 mgr->state = FPGA_MGR_STATE_WRITE;
59 mgr->state = FPGA_MGR_STATE_WRITE_INIT; 151 if (mgr->mops->write_sg) {
60 ret = mgr->mops->write_init(mgr, info, buf, 152 ret = mgr->mops->write_sg(mgr, sgt);
61 min(mgr->mops->initial_header_size, count)); 153 } else {
154 struct sg_mapping_iter miter;
155
156 sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
157 while (sg_miter_next(&miter)) {
158 ret = mgr->mops->write(mgr, miter.addr, miter.length);
159 if (ret)
160 break;
161 }
162 sg_miter_stop(&miter);
163 }
164
62 if (ret) { 165 if (ret) {
63 dev_err(dev, "Error preparing FPGA for writing\n"); 166 dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
64 mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR; 167 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
65 return ret; 168 return ret;
66 } 169 }
67 170
171 return fpga_mgr_write_complete(mgr, info);
172}
173EXPORT_SYMBOL_GPL(fpga_mgr_buf_load_sg);
174
175static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
176 struct fpga_image_info *info,
177 const char *buf, size_t count)
178{
179 int ret;
180
181 ret = fpga_mgr_write_init_buf(mgr, info, buf, count);
182 if (ret)
183 return ret;
184
68 /* 185 /*
69 * Write the FPGA image to the FPGA. 186 * Write the FPGA image to the FPGA.
70 */ 187 */
71 mgr->state = FPGA_MGR_STATE_WRITE; 188 mgr->state = FPGA_MGR_STATE_WRITE;
72 ret = mgr->mops->write(mgr, buf, count); 189 ret = mgr->mops->write(mgr, buf, count);
73 if (ret) { 190 if (ret) {
74 dev_err(dev, "Error while writing image data to FPGA\n"); 191 dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
75 mgr->state = FPGA_MGR_STATE_WRITE_ERR; 192 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
76 return ret; 193 return ret;
77 } 194 }
78 195
196 return fpga_mgr_write_complete(mgr, info);
197}
198
199/**
200 * fpga_mgr_buf_load - load fpga from image in buffer
201 * @mgr: fpga manager
202 * @flags: flags setting fpga confuration modes
203 * @buf: buffer contain fpga image
204 * @count: byte count of buf
205 *
206 * Step the low level fpga manager through the device-specific steps of getting
207 * an FPGA ready to be configured, writing the image to it, then doing whatever
208 * post-configuration steps necessary. This code assumes the caller got the
209 * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
210 *
211 * Return: 0 on success, negative error code otherwise.
212 */
213int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info,
214 const char *buf, size_t count)
215{
216 struct page **pages;
217 struct sg_table sgt;
218 const void *p;
219 int nr_pages;
220 int index;
221 int rc;
222
79 /* 223 /*
80 * After all the FPGA image has been written, do the device specific 224 * This is just a fast path if the caller has already created a
81 * steps to finish and set the FPGA into operating mode. 225 * contiguous kernel buffer and the driver doesn't require SG, non-SG
226 * drivers will still work on the slow path.
82 */ 227 */
83 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE; 228 if (mgr->mops->write)
84 ret = mgr->mops->write_complete(mgr, info); 229 return fpga_mgr_buf_load_mapped(mgr, info, buf, count);
85 if (ret) { 230
86 dev_err(dev, "Error after writing image data to FPGA\n"); 231 /*
87 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR; 232 * Convert the linear kernel pointer into a sg_table of pages for use
88 return ret; 233 * by the driver.
234 */
235 nr_pages = DIV_ROUND_UP((unsigned long)buf + count, PAGE_SIZE) -
236 (unsigned long)buf / PAGE_SIZE;
237 pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
238 if (!pages)
239 return -ENOMEM;
240
241 p = buf - offset_in_page(buf);
242 for (index = 0; index < nr_pages; index++) {
243 if (is_vmalloc_addr(p))
244 pages[index] = vmalloc_to_page(p);
245 else
246 pages[index] = kmap_to_page((void *)p);
247 if (!pages[index]) {
248 kfree(pages);
249 return -EFAULT;
250 }
251 p += PAGE_SIZE;
89 } 252 }
90 mgr->state = FPGA_MGR_STATE_OPERATING;
91 253
92 return 0; 254 /*
255 * The temporary pages list is used to code share the merging algorithm
256 * in sg_alloc_table_from_pages
257 */
258 rc = sg_alloc_table_from_pages(&sgt, pages, index, offset_in_page(buf),
259 count, GFP_KERNEL);
260 kfree(pages);
261 if (rc)
262 return rc;
263
264 rc = fpga_mgr_buf_load_sg(mgr, info, &sgt);
265 sg_free_table(&sgt);
266
267 return rc;
93} 268}
94EXPORT_SYMBOL_GPL(fpga_mgr_buf_load); 269EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
95 270
@@ -291,8 +466,9 @@ int fpga_mgr_register(struct device *dev, const char *name,
291 struct fpga_manager *mgr; 466 struct fpga_manager *mgr;
292 int id, ret; 467 int id, ret;
293 468
294 if (!mops || !mops->write_init || !mops->write || 469 if (!mops || !mops->write_complete || !mops->state ||
295 !mops->write_complete || !mops->state) { 470 !mops->write_init || (!mops->write && !mops->write_sg) ||
471 (mops->write && mops->write_sg)) {
296 dev_err(dev, "Attempt to register without fpga_manager_ops\n"); 472 dev_err(dev, "Attempt to register without fpga_manager_ops\n");
297 return -EINVAL; 473 return -EINVAL;
298 } 474 }
diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
index 1812bf7614e1..34cb98139442 100644
--- a/drivers/fpga/zynq-fpga.c
+++ b/drivers/fpga/zynq-fpga.c
@@ -30,6 +30,7 @@
30#include <linux/pm.h> 30#include <linux/pm.h>
31#include <linux/regmap.h> 31#include <linux/regmap.h>
32#include <linux/string.h> 32#include <linux/string.h>
33#include <linux/scatterlist.h>
33 34
34/* Offsets into SLCR regmap */ 35/* Offsets into SLCR regmap */
35 36
@@ -80,6 +81,7 @@
80 81
81/* FPGA init status */ 82/* FPGA init status */
82#define STATUS_DMA_Q_F BIT(31) 83#define STATUS_DMA_Q_F BIT(31)
84#define STATUS_DMA_Q_E BIT(30)
83#define STATUS_PCFG_INIT_MASK BIT(4) 85#define STATUS_PCFG_INIT_MASK BIT(4)
84 86
85/* Interrupt Status/Mask Register Bit definitions */ 87/* Interrupt Status/Mask Register Bit definitions */
@@ -89,7 +91,7 @@
89#define IXR_D_P_DONE_MASK BIT(12) 91#define IXR_D_P_DONE_MASK BIT(12)
90 /* FPGA programmed */ 92 /* FPGA programmed */
91#define IXR_PCFG_DONE_MASK BIT(2) 93#define IXR_PCFG_DONE_MASK BIT(2)
92#define IXR_ERROR_FLAGS_MASK 0x00F0F860 94#define IXR_ERROR_FLAGS_MASK 0x00F0C860
93#define IXR_ALL_MASK 0xF8F7F87F 95#define IXR_ALL_MASK 0xF8F7F87F
94 96
95/* Miscellaneous constant values */ 97/* Miscellaneous constant values */
@@ -98,12 +100,16 @@
98#define DMA_INVALID_ADDRESS GENMASK(31, 0) 100#define DMA_INVALID_ADDRESS GENMASK(31, 0)
99/* Used to unlock the dev */ 101/* Used to unlock the dev */
100#define UNLOCK_MASK 0x757bdf0d 102#define UNLOCK_MASK 0x757bdf0d
101/* Timeout for DMA to complete */
102#define DMA_DONE_TIMEOUT msecs_to_jiffies(1000)
103/* Timeout for polling reset bits */ 103/* Timeout for polling reset bits */
104#define INIT_POLL_TIMEOUT 2500000 104#define INIT_POLL_TIMEOUT 2500000
105/* Delay for polling reset bits */ 105/* Delay for polling reset bits */
106#define INIT_POLL_DELAY 20 106#define INIT_POLL_DELAY 20
107/* Signal this is the last DMA transfer, wait for the AXI and PCAP before
108 * interrupting
109 */
110#define DMA_SRC_LAST_TRANSFER 1
111/* Timeout for DMA completion */
112#define DMA_TIMEOUT_MS 5000
107 113
108/* Masks for controlling stuff in SLCR */ 114/* Masks for controlling stuff in SLCR */
109/* Disable all Level shifters */ 115/* Disable all Level shifters */
@@ -124,6 +130,11 @@ struct zynq_fpga_priv {
124 void __iomem *io_base; 130 void __iomem *io_base;
125 struct regmap *slcr; 131 struct regmap *slcr;
126 132
133 spinlock_t dma_lock;
134 unsigned int dma_elm;
135 unsigned int dma_nelms;
136 struct scatterlist *cur_sg;
137
127 struct completion dma_done; 138 struct completion dma_done;
128}; 139};
129 140
@@ -143,37 +154,104 @@ static inline u32 zynq_fpga_read(const struct zynq_fpga_priv *priv,
143 readl_poll_timeout(priv->io_base + addr, val, cond, sleep_us, \ 154 readl_poll_timeout(priv->io_base + addr, val, cond, sleep_us, \
144 timeout_us) 155 timeout_us)
145 156
146static void zynq_fpga_mask_irqs(struct zynq_fpga_priv *priv) 157/* Cause the specified irq mask bits to generate IRQs */
158static inline void zynq_fpga_set_irq(struct zynq_fpga_priv *priv, u32 enable)
147{ 159{
148 u32 intr_mask; 160 zynq_fpga_write(priv, INT_MASK_OFFSET, ~enable);
149
150 intr_mask = zynq_fpga_read(priv, INT_MASK_OFFSET);
151 zynq_fpga_write(priv, INT_MASK_OFFSET,
152 intr_mask | IXR_DMA_DONE_MASK | IXR_ERROR_FLAGS_MASK);
153} 161}
154 162
155static void zynq_fpga_unmask_irqs(struct zynq_fpga_priv *priv) 163/* Must be called with dma_lock held */
164static void zynq_step_dma(struct zynq_fpga_priv *priv)
156{ 165{
157 u32 intr_mask; 166 u32 addr;
167 u32 len;
168 bool first;
169
170 first = priv->dma_elm == 0;
171 while (priv->cur_sg) {
172 /* Feed the DMA queue until it is full. */
173 if (zynq_fpga_read(priv, STATUS_OFFSET) & STATUS_DMA_Q_F)
174 break;
175
176 addr = sg_dma_address(priv->cur_sg);
177 len = sg_dma_len(priv->cur_sg);
178 if (priv->dma_elm + 1 == priv->dma_nelms) {
179 /* The last transfer waits for the PCAP to finish too,
180 * notice this also changes the irq_mask to ignore
181 * IXR_DMA_DONE_MASK which ensures we do not trigger
182 * the completion too early.
183 */
184 addr |= DMA_SRC_LAST_TRANSFER;
185 priv->cur_sg = NULL;
186 } else {
187 priv->cur_sg = sg_next(priv->cur_sg);
188 priv->dma_elm++;
189 }
158 190
159 intr_mask = zynq_fpga_read(priv, INT_MASK_OFFSET); 191 zynq_fpga_write(priv, DMA_SRC_ADDR_OFFSET, addr);
160 zynq_fpga_write(priv, INT_MASK_OFFSET, 192 zynq_fpga_write(priv, DMA_DST_ADDR_OFFSET, DMA_INVALID_ADDRESS);
161 intr_mask 193 zynq_fpga_write(priv, DMA_SRC_LEN_OFFSET, len / 4);
162 & ~(IXR_D_P_DONE_MASK | IXR_ERROR_FLAGS_MASK)); 194 zynq_fpga_write(priv, DMA_DEST_LEN_OFFSET, 0);
195 }
196
197 /* Once the first transfer is queued we can turn on the ISR, future
198 * calls to zynq_step_dma will happen from the ISR context. The
199 * dma_lock spinlock guarentees this handover is done coherently, the
200 * ISR enable is put at the end to avoid another CPU spinning in the
201 * ISR on this lock.
202 */
203 if (first && priv->cur_sg) {
204 zynq_fpga_set_irq(priv,
205 IXR_DMA_DONE_MASK | IXR_ERROR_FLAGS_MASK);
206 } else if (!priv->cur_sg) {
207 /* The last transfer changes to DMA & PCAP mode since we do
208 * not want to continue until everything has been flushed into
209 * the PCAP.
210 */
211 zynq_fpga_set_irq(priv,
212 IXR_D_P_DONE_MASK | IXR_ERROR_FLAGS_MASK);
213 }
163} 214}
164 215
165static irqreturn_t zynq_fpga_isr(int irq, void *data) 216static irqreturn_t zynq_fpga_isr(int irq, void *data)
166{ 217{
167 struct zynq_fpga_priv *priv = data; 218 struct zynq_fpga_priv *priv = data;
219 u32 intr_status;
168 220
169 /* disable DMA and error IRQs */ 221 /* If anything other than DMA completion is reported stop and hand
170 zynq_fpga_mask_irqs(priv); 222 * control back to zynq_fpga_ops_write, something went wrong,
223 * otherwise progress the DMA.
224 */
225 spin_lock(&priv->dma_lock);
226 intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
227 if (!(intr_status & IXR_ERROR_FLAGS_MASK) &&
228 (intr_status & IXR_DMA_DONE_MASK) && priv->cur_sg) {
229 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_DMA_DONE_MASK);
230 zynq_step_dma(priv);
231 spin_unlock(&priv->dma_lock);
232 return IRQ_HANDLED;
233 }
234 spin_unlock(&priv->dma_lock);
171 235
236 zynq_fpga_set_irq(priv, 0);
172 complete(&priv->dma_done); 237 complete(&priv->dma_done);
173 238
174 return IRQ_HANDLED; 239 return IRQ_HANDLED;
175} 240}
176 241
242/* Sanity check the proposed bitstream. It must start with the sync word in
243 * the correct byte order, and be dword aligned. The input is a Xilinx .bin
244 * file with every 32 bit quantity swapped.
245 */
246static bool zynq_fpga_has_sync(const u8 *buf, size_t count)
247{
248 for (; count >= 4; buf += 4, count -= 4)
249 if (buf[0] == 0x66 && buf[1] == 0x55 && buf[2] == 0x99 &&
250 buf[3] == 0xaa)
251 return true;
252 return false;
253}
254
177static int zynq_fpga_ops_write_init(struct fpga_manager *mgr, 255static int zynq_fpga_ops_write_init(struct fpga_manager *mgr,
178 struct fpga_image_info *info, 256 struct fpga_image_info *info,
179 const char *buf, size_t count) 257 const char *buf, size_t count)
@@ -190,6 +268,13 @@ static int zynq_fpga_ops_write_init(struct fpga_manager *mgr,
190 268
191 /* don't globally reset PL if we're doing partial reconfig */ 269 /* don't globally reset PL if we're doing partial reconfig */
192 if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) { 270 if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
271 if (!zynq_fpga_has_sync(buf, count)) {
272 dev_err(&mgr->dev,
273 "Invalid bitstream, could not find a sync word. Bitstream must be a byte swapped .bin file\n");
274 err = -EINVAL;
275 goto out_err;
276 }
277
193 /* assert AXI interface resets */ 278 /* assert AXI interface resets */
194 regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET, 279 regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
195 FPGA_RST_ALL_MASK); 280 FPGA_RST_ALL_MASK);
@@ -259,10 +344,11 @@ static int zynq_fpga_ops_write_init(struct fpga_manager *mgr,
259 zynq_fpga_write(priv, CTRL_OFFSET, 344 zynq_fpga_write(priv, CTRL_OFFSET,
260 (CTRL_PCAP_PR_MASK | CTRL_PCAP_MODE_MASK | ctrl)); 345 (CTRL_PCAP_PR_MASK | CTRL_PCAP_MODE_MASK | ctrl));
261 346
262 /* check that we have room in the command queue */ 347 /* We expect that the command queue is empty right now. */
263 status = zynq_fpga_read(priv, STATUS_OFFSET); 348 status = zynq_fpga_read(priv, STATUS_OFFSET);
264 if (status & STATUS_DMA_Q_F) { 349 if ((status & STATUS_DMA_Q_F) ||
265 dev_err(&mgr->dev, "DMA command queue full\n"); 350 (status & STATUS_DMA_Q_E) != STATUS_DMA_Q_E) {
351 dev_err(&mgr->dev, "DMA command queue not right\n");
266 err = -EBUSY; 352 err = -EBUSY;
267 goto out_err; 353 goto out_err;
268 } 354 }
@@ -281,26 +367,36 @@ out_err:
281 return err; 367 return err;
282} 368}
283 369
284static int zynq_fpga_ops_write(struct fpga_manager *mgr, 370static int zynq_fpga_ops_write(struct fpga_manager *mgr, struct sg_table *sgt)
285 const char *buf, size_t count)
286{ 371{
287 struct zynq_fpga_priv *priv; 372 struct zynq_fpga_priv *priv;
373 const char *why;
288 int err; 374 int err;
289 char *kbuf;
290 size_t in_count;
291 dma_addr_t dma_addr;
292 u32 transfer_length;
293 u32 intr_status; 375 u32 intr_status;
376 unsigned long timeout;
377 unsigned long flags;
378 struct scatterlist *sg;
379 int i;
294 380
295 in_count = count;
296 priv = mgr->priv; 381 priv = mgr->priv;
297 382
298 kbuf = 383 /* The hardware can only DMA multiples of 4 bytes, and it requires the
299 dma_alloc_coherent(mgr->dev.parent, count, &dma_addr, GFP_KERNEL); 384 * starting addresses to be aligned to 64 bits (UG585 pg 212).
300 if (!kbuf) 385 */
301 return -ENOMEM; 386 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
387 if ((sg->offset % 8) || (sg->length % 4)) {
388 dev_err(&mgr->dev,
389 "Invalid bitstream, chunks must be aligned\n");
390 return -EINVAL;
391 }
392 }
302 393
303 memcpy(kbuf, buf, count); 394 priv->dma_nelms =
395 dma_map_sg(mgr->dev.parent, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
396 if (priv->dma_nelms == 0) {
397 dev_err(&mgr->dev, "Unable to DMA map (TO_DEVICE)\n");
398 return -ENOMEM;
399 }
304 400
305 /* enable clock */ 401 /* enable clock */
306 err = clk_enable(priv->clk); 402 err = clk_enable(priv->clk);
@@ -308,38 +404,67 @@ static int zynq_fpga_ops_write(struct fpga_manager *mgr,
308 goto out_free; 404 goto out_free;
309 405
310 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK); 406 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
311
312 reinit_completion(&priv->dma_done); 407 reinit_completion(&priv->dma_done);
313 408
314 /* enable DMA and error IRQs */ 409 /* zynq_step_dma will turn on interrupts */
315 zynq_fpga_unmask_irqs(priv); 410 spin_lock_irqsave(&priv->dma_lock, flags);
411 priv->dma_elm = 0;
412 priv->cur_sg = sgt->sgl;
413 zynq_step_dma(priv);
414 spin_unlock_irqrestore(&priv->dma_lock, flags);
316 415
317 /* the +1 in the src addr is used to hold off on DMA_DONE IRQ 416 timeout = wait_for_completion_timeout(&priv->dma_done,
318 * until both AXI and PCAP are done ... 417 msecs_to_jiffies(DMA_TIMEOUT_MS));
319 */
320 zynq_fpga_write(priv, DMA_SRC_ADDR_OFFSET, (u32)(dma_addr) + 1);
321 zynq_fpga_write(priv, DMA_DST_ADDR_OFFSET, (u32)DMA_INVALID_ADDRESS);
322 418
323 /* convert #bytes to #words */ 419 spin_lock_irqsave(&priv->dma_lock, flags);
324 transfer_length = (count + 3) / 4; 420 zynq_fpga_set_irq(priv, 0);
421 priv->cur_sg = NULL;
422 spin_unlock_irqrestore(&priv->dma_lock, flags);
325 423
326 zynq_fpga_write(priv, DMA_SRC_LEN_OFFSET, transfer_length); 424 intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
327 zynq_fpga_write(priv, DMA_DEST_LEN_OFFSET, 0); 425 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
328 426
329 wait_for_completion(&priv->dma_done); 427 /* There doesn't seem to be a way to force cancel any DMA, so if
428 * something went wrong we are relying on the hardware to have halted
429 * the DMA before we get here, if there was we could use
430 * wait_for_completion_interruptible too.
431 */
330 432
331 intr_status = zynq_fpga_read(priv, INT_STS_OFFSET); 433 if (intr_status & IXR_ERROR_FLAGS_MASK) {
332 zynq_fpga_write(priv, INT_STS_OFFSET, intr_status); 434 why = "DMA reported error";
435 err = -EIO;
436 goto out_report;
437 }
333 438
334 if (!((intr_status & IXR_D_P_DONE_MASK) == IXR_D_P_DONE_MASK)) { 439 if (priv->cur_sg ||
335 dev_err(&mgr->dev, "Error configuring FPGA\n"); 440 !((intr_status & IXR_D_P_DONE_MASK) == IXR_D_P_DONE_MASK)) {
336 err = -EFAULT; 441 if (timeout == 0)
442 why = "DMA timed out";
443 else
444 why = "DMA did not complete";
445 err = -EIO;
446 goto out_report;
337 } 447 }
338 448
449 err = 0;
450 goto out_clk;
451
452out_report:
453 dev_err(&mgr->dev,
454 "%s: INT_STS:0x%x CTRL:0x%x LOCK:0x%x INT_MASK:0x%x STATUS:0x%x MCTRL:0x%x\n",
455 why,
456 intr_status,
457 zynq_fpga_read(priv, CTRL_OFFSET),
458 zynq_fpga_read(priv, LOCK_OFFSET),
459 zynq_fpga_read(priv, INT_MASK_OFFSET),
460 zynq_fpga_read(priv, STATUS_OFFSET),
461 zynq_fpga_read(priv, MCTRL_OFFSET));
462
463out_clk:
339 clk_disable(priv->clk); 464 clk_disable(priv->clk);
340 465
341out_free: 466out_free:
342 dma_free_coherent(mgr->dev.parent, count, kbuf, dma_addr); 467 dma_unmap_sg(mgr->dev.parent, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
343 return err; 468 return err;
344} 469}
345 470
@@ -400,9 +525,10 @@ static enum fpga_mgr_states zynq_fpga_ops_state(struct fpga_manager *mgr)
400} 525}
401 526
402static const struct fpga_manager_ops zynq_fpga_ops = { 527static const struct fpga_manager_ops zynq_fpga_ops = {
528 .initial_header_size = 128,
403 .state = zynq_fpga_ops_state, 529 .state = zynq_fpga_ops_state,
404 .write_init = zynq_fpga_ops_write_init, 530 .write_init = zynq_fpga_ops_write_init,
405 .write = zynq_fpga_ops_write, 531 .write_sg = zynq_fpga_ops_write,
406 .write_complete = zynq_fpga_ops_write_complete, 532 .write_complete = zynq_fpga_ops_write_complete,
407}; 533};
408 534
@@ -416,6 +542,7 @@ static int zynq_fpga_probe(struct platform_device *pdev)
416 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 542 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
417 if (!priv) 543 if (!priv)
418 return -ENOMEM; 544 return -ENOMEM;
545 spin_lock_init(&priv->dma_lock);
419 546
420 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 547 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
421 priv->io_base = devm_ioremap_resource(dev, res); 548 priv->io_base = devm_ioremap_resource(dev, res);
@@ -452,7 +579,7 @@ static int zynq_fpga_probe(struct platform_device *pdev)
452 /* unlock the device */ 579 /* unlock the device */
453 zynq_fpga_write(priv, UNLOCK_OFFSET, UNLOCK_MASK); 580 zynq_fpga_write(priv, UNLOCK_OFFSET, UNLOCK_MASK);
454 581
455 zynq_fpga_write(priv, INT_MASK_OFFSET, 0xFFFFFFFF); 582 zynq_fpga_set_irq(priv, 0);
456 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK); 583 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
457 err = devm_request_irq(dev, priv->irq, zynq_fpga_isr, 0, dev_name(dev), 584 err = devm_request_irq(dev, priv->irq, zynq_fpga_isr, 0, dev_name(dev),
458 priv); 585 priv);