Commit | Line | Data |
---|---|---|
ef141a0b SN |
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 | * | |
ef141a0b SN |
24 | * (c) Copyright 2002 Xilinx Inc., Systems Engineering Group |
25 | * (c) Copyright 2004 Xilinx Inc., Systems Engineering Group | |
26 | * (c) Copyright 2007-2008 Xilinx Inc. | |
27 | * All rights reserved. | |
28 | * | |
29 | * You should have received a copy of the GNU General Public License along | |
30 | * with this program; if not, write to the Free Software Foundation, Inc., | |
31 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
32 | * | |
33 | *****************************************************************************/ | |
34 | ||
35 | /* | |
ac646734 | 36 | * This is the code behind /dev/icap* -- it allows a user-space |
ef141a0b SN |
37 | * application to use the Xilinx ICAP subsystem. |
38 | * | |
39 | * The following operations are possible: | |
40 | * | |
41 | * open open the port and initialize for access. | |
42 | * release release port | |
43 | * write Write a bitstream to the configuration processor. | |
44 | * read Read a data stream from the configuration processor. | |
45 | * | |
46 | * After being opened, the port is initialized and accessed to avoid a | |
47 | * corrupted first read which may occur with some hardware. The port | |
48 | * is left in a desynched state, requiring that a synch sequence be | |
49 | * transmitted before any valid configuration data. A user will have | |
50 | * exclusive access to the device while it remains open, and the state | |
51 | * of the ICAP cannot be guaranteed after the device is closed. Note | |
52 | * that a complete reset of the core and the state of the ICAP cannot | |
53 | * be performed on many versions of the cores, hence users of this | |
54 | * device should avoid making inconsistent accesses to the device. In | |
55 | * particular, accessing the read interface, without first generating | |
56 | * a write containing a readback packet can leave the ICAP in an | |
57 | * inaccessible state. | |
58 | * | |
59 | * Note that in order to use the read interface, it is first necessary | |
60 | * to write a request packet to the write interface. i.e., it is not | |
61 | * possible to simply readback the bitstream (or any configuration | |
62 | * bits) from a device without specifically requesting them first. | |
63 | * The code to craft such packets is intended to be part of the | |
64 | * user-space application code that uses this device. The simplest | |
65 | * way to use this interface is simply: | |
66 | * | |
ac646734 | 67 | * cp foo.bit /dev/icap0 |
ef141a0b SN |
68 | * |
69 | * Note that unless foo.bit is an appropriately constructed partial | |
70 | * bitstream, this has a high likelyhood of overwriting the design | |
71 | * currently programmed in the FPGA. | |
72 | */ | |
73 | ||
ef141a0b SN |
74 | #include <linux/module.h> |
75 | #include <linux/kernel.h> | |
76 | #include <linux/types.h> | |
77 | #include <linux/ioport.h> | |
78 | #include <linux/interrupt.h> | |
79 | #include <linux/fcntl.h> | |
80 | #include <linux/init.h> | |
81 | #include <linux/poll.h> | |
82 | #include <linux/proc_fs.h> | |
f62f2fdd | 83 | #include <linux/mutex.h> |
f4943db1 | 84 | #include <linux/smp_lock.h> |
ef141a0b | 85 | #include <linux/sysctl.h> |
ef141a0b SN |
86 | #include <linux/fs.h> |
87 | #include <linux/cdev.h> | |
88 | #include <linux/platform_device.h> | |
5a0e3ad6 | 89 | #include <linux/slab.h> |
ef141a0b SN |
90 | |
91 | #include <asm/io.h> | |
92 | #include <asm/uaccess.h> | |
93 | #include <asm/system.h> | |
94 | ||
95 | #ifdef CONFIG_OF | |
96 | /* For open firmware. */ | |
97 | #include <linux/of_device.h> | |
98 | #include <linux/of_platform.h> | |
99 | #endif | |
100 | ||
101 | #include "xilinx_hwicap.h" | |
102 | #include "buffer_icap.h" | |
103 | #include "fifo_icap.h" | |
104 | ||
ac646734 | 105 | #define DRIVER_NAME "icap" |
ef141a0b SN |
106 | |
107 | #define HWICAP_REGS (0x10000) | |
108 | ||
ac646734 SN |
109 | #define XHWICAP_MAJOR 259 |
110 | #define XHWICAP_MINOR 0 | |
ef141a0b SN |
111 | #define HWICAP_DEVICES 1 |
112 | ||
ef141a0b SN |
113 | /* An array, which is set to true when the device is registered. */ |
114 | static bool probed_devices[HWICAP_DEVICES]; | |
f62f2fdd | 115 | static struct mutex icap_sem; |
ef141a0b SN |
116 | |
117 | static struct class *icap_class; | |
118 | ||
119 | #define UNIMPLEMENTED 0xFFFF | |
120 | ||
121 | static const struct config_registers v2_config_registers = { | |
122 | .CRC = 0, | |
123 | .FAR = 1, | |
124 | .FDRI = 2, | |
125 | .FDRO = 3, | |
126 | .CMD = 4, | |
127 | .CTL = 5, | |
128 | .MASK = 6, | |
129 | .STAT = 7, | |
130 | .LOUT = 8, | |
131 | .COR = 9, | |
132 | .MFWR = 10, | |
133 | .FLR = 11, | |
134 | .KEY = 12, | |
135 | .CBC = 13, | |
136 | .IDCODE = 14, | |
137 | .AXSS = UNIMPLEMENTED, | |
138 | .C0R_1 = UNIMPLEMENTED, | |
139 | .CSOB = UNIMPLEMENTED, | |
140 | .WBSTAR = UNIMPLEMENTED, | |
141 | .TIMER = UNIMPLEMENTED, | |
142 | .BOOTSTS = UNIMPLEMENTED, | |
143 | .CTL_1 = UNIMPLEMENTED, | |
144 | }; | |
145 | ||
146 | static const struct config_registers v4_config_registers = { | |
147 | .CRC = 0, | |
148 | .FAR = 1, | |
149 | .FDRI = 2, | |
150 | .FDRO = 3, | |
151 | .CMD = 4, | |
152 | .CTL = 5, | |
153 | .MASK = 6, | |
154 | .STAT = 7, | |
155 | .LOUT = 8, | |
156 | .COR = 9, | |
157 | .MFWR = 10, | |
158 | .FLR = UNIMPLEMENTED, | |
159 | .KEY = UNIMPLEMENTED, | |
160 | .CBC = 11, | |
161 | .IDCODE = 12, | |
162 | .AXSS = 13, | |
163 | .C0R_1 = UNIMPLEMENTED, | |
164 | .CSOB = UNIMPLEMENTED, | |
165 | .WBSTAR = UNIMPLEMENTED, | |
166 | .TIMER = UNIMPLEMENTED, | |
167 | .BOOTSTS = UNIMPLEMENTED, | |
168 | .CTL_1 = UNIMPLEMENTED, | |
169 | }; | |
170 | static const struct config_registers v5_config_registers = { | |
171 | .CRC = 0, | |
172 | .FAR = 1, | |
173 | .FDRI = 2, | |
174 | .FDRO = 3, | |
175 | .CMD = 4, | |
176 | .CTL = 5, | |
177 | .MASK = 6, | |
178 | .STAT = 7, | |
179 | .LOUT = 8, | |
180 | .COR = 9, | |
181 | .MFWR = 10, | |
182 | .FLR = UNIMPLEMENTED, | |
183 | .KEY = UNIMPLEMENTED, | |
184 | .CBC = 11, | |
185 | .IDCODE = 12, | |
186 | .AXSS = 13, | |
187 | .C0R_1 = 14, | |
188 | .CSOB = 15, | |
189 | .WBSTAR = 16, | |
190 | .TIMER = 17, | |
191 | .BOOTSTS = 18, | |
192 | .CTL_1 = 19, | |
193 | }; | |
194 | ||
195 | /** | |
f62f2fdd SN |
196 | * hwicap_command_desync - Send a DESYNC command to the ICAP port. |
197 | * @drvdata: a pointer to the drvdata. | |
ef141a0b SN |
198 | * |
199 | * This command desynchronizes the ICAP After this command, a | |
200 | * bitstream containing a NULL packet, followed by a SYNCH packet is | |
201 | * required before the ICAP will recognize commands. | |
202 | */ | |
f62f2fdd | 203 | static int hwicap_command_desync(struct hwicap_drvdata *drvdata) |
ef141a0b SN |
204 | { |
205 | u32 buffer[4]; | |
206 | u32 index = 0; | |
207 | ||
208 | /* | |
209 | * Create the data to be written to the ICAP. | |
210 | */ | |
211 | buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1; | |
212 | buffer[index++] = XHI_CMD_DESYNCH; | |
213 | buffer[index++] = XHI_NOOP_PACKET; | |
214 | buffer[index++] = XHI_NOOP_PACKET; | |
215 | ||
216 | /* | |
217 | * Write the data to the FIFO and intiate the transfer of data present | |
218 | * in the FIFO to the ICAP device. | |
219 | */ | |
220 | return drvdata->config->set_configuration(drvdata, | |
221 | &buffer[0], index); | |
222 | } | |
223 | ||
224 | /** | |
f62f2fdd SN |
225 | * hwicap_get_configuration_register - Query a configuration register. |
226 | * @drvdata: a pointer to the drvdata. | |
227 | * @reg: a constant which represents the configuration | |
ef141a0b SN |
228 | * register value to be returned. |
229 | * Examples: XHI_IDCODE, XHI_FLR. | |
f62f2fdd | 230 | * @reg_data: returns the value of the register. |
ef141a0b SN |
231 | * |
232 | * Sends a query packet to the ICAP and then receives the response. | |
233 | * The icap is left in Synched state. | |
234 | */ | |
f62f2fdd SN |
235 | static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata, |
236 | u32 reg, u32 *reg_data) | |
ef141a0b SN |
237 | { |
238 | int status; | |
239 | u32 buffer[6]; | |
240 | u32 index = 0; | |
241 | ||
242 | /* | |
243 | * Create the data to be written to the ICAP. | |
244 | */ | |
245 | buffer[index++] = XHI_DUMMY_PACKET; | |
4c58f8fe | 246 | buffer[index++] = XHI_NOOP_PACKET; |
ef141a0b SN |
247 | buffer[index++] = XHI_SYNC_PACKET; |
248 | buffer[index++] = XHI_NOOP_PACKET; | |
4c58f8fe SN |
249 | buffer[index++] = XHI_NOOP_PACKET; |
250 | ||
251 | /* | |
252 | * Write the data to the FIFO and initiate the transfer of data present | |
253 | * in the FIFO to the ICAP device. | |
254 | */ | |
255 | status = drvdata->config->set_configuration(drvdata, | |
256 | &buffer[0], index); | |
257 | if (status) | |
258 | return status; | |
259 | ||
260 | /* If the syncword was not found, then we need to start over. */ | |
261 | status = drvdata->config->get_status(drvdata); | |
262 | if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK) | |
263 | return -EIO; | |
264 | ||
265 | index = 0; | |
ef141a0b SN |
266 | buffer[index++] = hwicap_type_1_read(reg) | 1; |
267 | buffer[index++] = XHI_NOOP_PACKET; | |
268 | buffer[index++] = XHI_NOOP_PACKET; | |
269 | ||
270 | /* | |
271 | * Write the data to the FIFO and intiate the transfer of data present | |
272 | * in the FIFO to the ICAP device. | |
273 | */ | |
274 | status = drvdata->config->set_configuration(drvdata, | |
275 | &buffer[0], index); | |
276 | if (status) | |
277 | return status; | |
278 | ||
279 | /* | |
280 | * Read the configuration register | |
281 | */ | |
f62f2fdd | 282 | status = drvdata->config->get_configuration(drvdata, reg_data, 1); |
ef141a0b SN |
283 | if (status) |
284 | return status; | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
f62f2fdd | 289 | static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata) |
ef141a0b SN |
290 | { |
291 | int status; | |
292 | u32 idcode; | |
293 | ||
294 | dev_dbg(drvdata->dev, "initializing\n"); | |
295 | ||
296 | /* Abort any current transaction, to make sure we have the | |
297 | * ICAP in a good state. */ | |
298 | dev_dbg(drvdata->dev, "Reset...\n"); | |
299 | drvdata->config->reset(drvdata); | |
300 | ||
301 | dev_dbg(drvdata->dev, "Desync...\n"); | |
302 | status = hwicap_command_desync(drvdata); | |
303 | if (status) | |
304 | return status; | |
305 | ||
306 | /* Attempt to read the IDCODE from ICAP. This | |
307 | * may not be returned correctly, due to the design of the | |
308 | * hardware. | |
309 | */ | |
310 | dev_dbg(drvdata->dev, "Reading IDCODE...\n"); | |
311 | status = hwicap_get_configuration_register( | |
312 | drvdata, drvdata->config_regs->IDCODE, &idcode); | |
313 | dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode); | |
314 | if (status) | |
315 | return status; | |
316 | ||
317 | dev_dbg(drvdata->dev, "Desync...\n"); | |
318 | status = hwicap_command_desync(drvdata); | |
319 | if (status) | |
320 | return status; | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
325 | static ssize_t | |
f62f2fdd | 326 | hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
ef141a0b SN |
327 | { |
328 | struct hwicap_drvdata *drvdata = file->private_data; | |
329 | ssize_t bytes_to_read = 0; | |
330 | u32 *kbuf; | |
331 | u32 words; | |
332 | u32 bytes_remaining; | |
333 | int status; | |
334 | ||
f62f2fdd SN |
335 | status = mutex_lock_interruptible(&drvdata->sem); |
336 | if (status) | |
337 | return status; | |
ef141a0b SN |
338 | |
339 | if (drvdata->read_buffer_in_use) { | |
340 | /* If there are leftover bytes in the buffer, just */ | |
341 | /* return them and don't try to read more from the */ | |
342 | /* ICAP device. */ | |
343 | bytes_to_read = | |
344 | (count < drvdata->read_buffer_in_use) ? count : | |
345 | drvdata->read_buffer_in_use; | |
346 | ||
347 | /* Return the data currently in the read buffer. */ | |
348 | if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) { | |
349 | status = -EFAULT; | |
350 | goto error; | |
351 | } | |
352 | drvdata->read_buffer_in_use -= bytes_to_read; | |
f62f2fdd SN |
353 | memmove(drvdata->read_buffer, |
354 | drvdata->read_buffer + bytes_to_read, | |
355 | 4 - bytes_to_read); | |
ef141a0b SN |
356 | } else { |
357 | /* Get new data from the ICAP, and return was was requested. */ | |
358 | kbuf = (u32 *) get_zeroed_page(GFP_KERNEL); | |
359 | if (!kbuf) { | |
360 | status = -ENOMEM; | |
361 | goto error; | |
362 | } | |
363 | ||
364 | /* The ICAP device is only able to read complete */ | |
365 | /* words. If a number of bytes that do not correspond */ | |
366 | /* to complete words is requested, then we read enough */ | |
367 | /* words to get the required number of bytes, and then */ | |
368 | /* save the remaining bytes for the next read. */ | |
369 | ||
370 | /* Determine the number of words to read, rounding up */ | |
371 | /* if necessary. */ | |
372 | words = ((count + 3) >> 2); | |
373 | bytes_to_read = words << 2; | |
374 | ||
375 | if (bytes_to_read > PAGE_SIZE) | |
376 | bytes_to_read = PAGE_SIZE; | |
377 | ||
378 | /* Ensure we only read a complete number of words. */ | |
379 | bytes_remaining = bytes_to_read & 3; | |
380 | bytes_to_read &= ~3; | |
381 | words = bytes_to_read >> 2; | |
382 | ||
383 | status = drvdata->config->get_configuration(drvdata, | |
384 | kbuf, words); | |
385 | ||
386 | /* If we didn't read correctly, then bail out. */ | |
387 | if (status) { | |
388 | free_page((unsigned long)kbuf); | |
389 | goto error; | |
390 | } | |
391 | ||
392 | /* If we fail to return the data to the user, then bail out. */ | |
393 | if (copy_to_user(buf, kbuf, bytes_to_read)) { | |
394 | free_page((unsigned long)kbuf); | |
395 | status = -EFAULT; | |
396 | goto error; | |
397 | } | |
f62f2fdd SN |
398 | memcpy(drvdata->read_buffer, |
399 | kbuf, | |
400 | bytes_remaining); | |
ef141a0b SN |
401 | drvdata->read_buffer_in_use = bytes_remaining; |
402 | free_page((unsigned long)kbuf); | |
403 | } | |
404 | status = bytes_to_read; | |
405 | error: | |
f62f2fdd | 406 | mutex_unlock(&drvdata->sem); |
ef141a0b SN |
407 | return status; |
408 | } | |
409 | ||
410 | static ssize_t | |
f62f2fdd | 411 | hwicap_write(struct file *file, const char __user *buf, |
ef141a0b SN |
412 | size_t count, loff_t *ppos) |
413 | { | |
414 | struct hwicap_drvdata *drvdata = file->private_data; | |
415 | ssize_t written = 0; | |
416 | ssize_t left = count; | |
417 | u32 *kbuf; | |
418 | ssize_t len; | |
419 | ssize_t status; | |
420 | ||
f62f2fdd SN |
421 | status = mutex_lock_interruptible(&drvdata->sem); |
422 | if (status) | |
423 | return status; | |
ef141a0b SN |
424 | |
425 | left += drvdata->write_buffer_in_use; | |
426 | ||
427 | /* Only write multiples of 4 bytes. */ | |
428 | if (left < 4) { | |
429 | status = 0; | |
430 | goto error; | |
431 | } | |
432 | ||
433 | kbuf = (u32 *) __get_free_page(GFP_KERNEL); | |
434 | if (!kbuf) { | |
435 | status = -ENOMEM; | |
436 | goto error; | |
437 | } | |
438 | ||
439 | while (left > 3) { | |
440 | /* only write multiples of 4 bytes, so there might */ | |
441 | /* be as many as 3 bytes left (at the end). */ | |
442 | len = left; | |
443 | ||
444 | if (len > PAGE_SIZE) | |
445 | len = PAGE_SIZE; | |
446 | len &= ~3; | |
447 | ||
448 | if (drvdata->write_buffer_in_use) { | |
449 | memcpy(kbuf, drvdata->write_buffer, | |
450 | drvdata->write_buffer_in_use); | |
451 | if (copy_from_user( | |
f62f2fdd | 452 | (((char *)kbuf) + drvdata->write_buffer_in_use), |
ef141a0b SN |
453 | buf + written, |
454 | len - (drvdata->write_buffer_in_use))) { | |
455 | free_page((unsigned long)kbuf); | |
456 | status = -EFAULT; | |
457 | goto error; | |
458 | } | |
459 | } else { | |
460 | if (copy_from_user(kbuf, buf + written, len)) { | |
461 | free_page((unsigned long)kbuf); | |
462 | status = -EFAULT; | |
463 | goto error; | |
464 | } | |
465 | } | |
466 | ||
467 | status = drvdata->config->set_configuration(drvdata, | |
468 | kbuf, len >> 2); | |
469 | ||
470 | if (status) { | |
471 | free_page((unsigned long)kbuf); | |
472 | status = -EFAULT; | |
473 | goto error; | |
474 | } | |
475 | if (drvdata->write_buffer_in_use) { | |
476 | len -= drvdata->write_buffer_in_use; | |
477 | left -= drvdata->write_buffer_in_use; | |
478 | drvdata->write_buffer_in_use = 0; | |
479 | } | |
480 | written += len; | |
481 | left -= len; | |
482 | } | |
483 | if ((left > 0) && (left < 4)) { | |
484 | if (!copy_from_user(drvdata->write_buffer, | |
485 | buf + written, left)) { | |
486 | drvdata->write_buffer_in_use = left; | |
487 | written += left; | |
488 | left = 0; | |
489 | } | |
490 | } | |
491 | ||
492 | free_page((unsigned long)kbuf); | |
493 | status = written; | |
494 | error: | |
f62f2fdd | 495 | mutex_unlock(&drvdata->sem); |
ef141a0b SN |
496 | return status; |
497 | } | |
498 | ||
499 | static int hwicap_open(struct inode *inode, struct file *file) | |
500 | { | |
501 | struct hwicap_drvdata *drvdata; | |
502 | int status; | |
503 | ||
f4943db1 | 504 | lock_kernel(); |
ef141a0b SN |
505 | drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev); |
506 | ||
f62f2fdd SN |
507 | status = mutex_lock_interruptible(&drvdata->sem); |
508 | if (status) | |
f4943db1 | 509 | goto out; |
ef141a0b SN |
510 | |
511 | if (drvdata->is_open) { | |
512 | status = -EBUSY; | |
513 | goto error; | |
514 | } | |
515 | ||
516 | status = hwicap_initialize_hwicap(drvdata); | |
517 | if (status) { | |
518 | dev_err(drvdata->dev, "Failed to open file"); | |
519 | goto error; | |
520 | } | |
521 | ||
522 | file->private_data = drvdata; | |
523 | drvdata->write_buffer_in_use = 0; | |
524 | drvdata->read_buffer_in_use = 0; | |
525 | drvdata->is_open = 1; | |
526 | ||
527 | error: | |
f62f2fdd | 528 | mutex_unlock(&drvdata->sem); |
f4943db1 JC |
529 | out: |
530 | unlock_kernel(); | |
ef141a0b SN |
531 | return status; |
532 | } | |
533 | ||
534 | static int hwicap_release(struct inode *inode, struct file *file) | |
535 | { | |
536 | struct hwicap_drvdata *drvdata = file->private_data; | |
537 | int i; | |
538 | int status = 0; | |
539 | ||
f62f2fdd | 540 | mutex_lock(&drvdata->sem); |
ef141a0b SN |
541 | |
542 | if (drvdata->write_buffer_in_use) { | |
543 | /* Flush write buffer. */ | |
544 | for (i = drvdata->write_buffer_in_use; i < 4; i++) | |
545 | drvdata->write_buffer[i] = 0; | |
546 | ||
547 | status = drvdata->config->set_configuration(drvdata, | |
548 | (u32 *) drvdata->write_buffer, 1); | |
549 | if (status) | |
550 | goto error; | |
551 | } | |
552 | ||
553 | status = hwicap_command_desync(drvdata); | |
554 | if (status) | |
555 | goto error; | |
556 | ||
557 | error: | |
558 | drvdata->is_open = 0; | |
f62f2fdd | 559 | mutex_unlock(&drvdata->sem); |
ef141a0b SN |
560 | return status; |
561 | } | |
562 | ||
828c0950 | 563 | static const struct file_operations hwicap_fops = { |
ef141a0b SN |
564 | .owner = THIS_MODULE, |
565 | .write = hwicap_write, | |
566 | .read = hwicap_read, | |
567 | .open = hwicap_open, | |
568 | .release = hwicap_release, | |
569 | }; | |
570 | ||
571 | static int __devinit hwicap_setup(struct device *dev, int id, | |
572 | const struct resource *regs_res, | |
573 | const struct hwicap_driver_config *config, | |
574 | const struct config_registers *config_regs) | |
575 | { | |
576 | dev_t devt; | |
577 | struct hwicap_drvdata *drvdata = NULL; | |
578 | int retval = 0; | |
579 | ||
580 | dev_info(dev, "Xilinx icap port driver\n"); | |
581 | ||
f62f2fdd SN |
582 | mutex_lock(&icap_sem); |
583 | ||
ef141a0b SN |
584 | if (id < 0) { |
585 | for (id = 0; id < HWICAP_DEVICES; id++) | |
586 | if (!probed_devices[id]) | |
587 | break; | |
588 | } | |
589 | if (id < 0 || id >= HWICAP_DEVICES) { | |
f62f2fdd | 590 | mutex_unlock(&icap_sem); |
ef141a0b SN |
591 | dev_err(dev, "%s%i too large\n", DRIVER_NAME, id); |
592 | return -EINVAL; | |
593 | } | |
594 | if (probed_devices[id]) { | |
f62f2fdd | 595 | mutex_unlock(&icap_sem); |
ef141a0b SN |
596 | dev_err(dev, "cannot assign to %s%i; it is already in use\n", |
597 | DRIVER_NAME, id); | |
598 | return -EBUSY; | |
599 | } | |
600 | ||
601 | probed_devices[id] = 1; | |
f62f2fdd | 602 | mutex_unlock(&icap_sem); |
ef141a0b | 603 | |
ac646734 | 604 | devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id); |
ef141a0b | 605 | |
f62f2fdd | 606 | drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL); |
ef141a0b SN |
607 | if (!drvdata) { |
608 | dev_err(dev, "Couldn't allocate device private record\n"); | |
f62f2fdd SN |
609 | retval = -ENOMEM; |
610 | goto failed0; | |
ef141a0b | 611 | } |
ef141a0b SN |
612 | dev_set_drvdata(dev, (void *)drvdata); |
613 | ||
614 | if (!regs_res) { | |
615 | dev_err(dev, "Couldn't get registers resource\n"); | |
616 | retval = -EFAULT; | |
617 | goto failed1; | |
618 | } | |
619 | ||
620 | drvdata->mem_start = regs_res->start; | |
621 | drvdata->mem_end = regs_res->end; | |
622 | drvdata->mem_size = regs_res->end - regs_res->start + 1; | |
623 | ||
624 | if (!request_mem_region(drvdata->mem_start, | |
625 | drvdata->mem_size, DRIVER_NAME)) { | |
b17b8181 | 626 | dev_err(dev, "Couldn't lock memory region at %Lx\n", |
a1080968 | 627 | (unsigned long long) regs_res->start); |
ef141a0b SN |
628 | retval = -EBUSY; |
629 | goto failed1; | |
630 | } | |
631 | ||
632 | drvdata->devt = devt; | |
633 | drvdata->dev = dev; | |
634 | drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size); | |
635 | if (!drvdata->base_address) { | |
636 | dev_err(dev, "ioremap() failed\n"); | |
637 | goto failed2; | |
638 | } | |
639 | ||
640 | drvdata->config = config; | |
641 | drvdata->config_regs = config_regs; | |
642 | ||
f62f2fdd | 643 | mutex_init(&drvdata->sem); |
ef141a0b SN |
644 | drvdata->is_open = 0; |
645 | ||
a1080968 GL |
646 | dev_info(dev, "ioremap %llx to %p with size %llx\n", |
647 | (unsigned long long) drvdata->mem_start, | |
648 | drvdata->base_address, | |
649 | (unsigned long long) drvdata->mem_size); | |
ef141a0b SN |
650 | |
651 | cdev_init(&drvdata->cdev, &hwicap_fops); | |
652 | drvdata->cdev.owner = THIS_MODULE; | |
653 | retval = cdev_add(&drvdata->cdev, devt, 1); | |
654 | if (retval) { | |
655 | dev_err(dev, "cdev_add() failed\n"); | |
656 | goto failed3; | |
657 | } | |
47aa5793 | 658 | |
03457cd4 | 659 | device_create(icap_class, dev, devt, NULL, "%s%d", DRIVER_NAME, id); |
ef141a0b SN |
660 | return 0; /* success */ |
661 | ||
662 | failed3: | |
663 | iounmap(drvdata->base_address); | |
664 | ||
665 | failed2: | |
666 | release_mem_region(regs_res->start, drvdata->mem_size); | |
667 | ||
668 | failed1: | |
669 | kfree(drvdata); | |
670 | ||
f62f2fdd SN |
671 | failed0: |
672 | mutex_lock(&icap_sem); | |
673 | probed_devices[id] = 0; | |
674 | mutex_unlock(&icap_sem); | |
675 | ||
ef141a0b SN |
676 | return retval; |
677 | } | |
678 | ||
679 | static struct hwicap_driver_config buffer_icap_config = { | |
680 | .get_configuration = buffer_icap_get_configuration, | |
681 | .set_configuration = buffer_icap_set_configuration, | |
6b06fdba | 682 | .get_status = buffer_icap_get_status, |
ef141a0b SN |
683 | .reset = buffer_icap_reset, |
684 | }; | |
685 | ||
686 | static struct hwicap_driver_config fifo_icap_config = { | |
687 | .get_configuration = fifo_icap_get_configuration, | |
688 | .set_configuration = fifo_icap_set_configuration, | |
6b06fdba | 689 | .get_status = fifo_icap_get_status, |
ef141a0b SN |
690 | .reset = fifo_icap_reset, |
691 | }; | |
692 | ||
693 | static int __devexit hwicap_remove(struct device *dev) | |
694 | { | |
695 | struct hwicap_drvdata *drvdata; | |
696 | ||
697 | drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev); | |
698 | ||
699 | if (!drvdata) | |
700 | return 0; | |
701 | ||
f62f2fdd | 702 | device_destroy(icap_class, drvdata->devt); |
ef141a0b SN |
703 | cdev_del(&drvdata->cdev); |
704 | iounmap(drvdata->base_address); | |
705 | release_mem_region(drvdata->mem_start, drvdata->mem_size); | |
706 | kfree(drvdata); | |
707 | dev_set_drvdata(dev, NULL); | |
ef141a0b | 708 | |
f62f2fdd | 709 | mutex_lock(&icap_sem); |
ac646734 | 710 | probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0; |
f62f2fdd | 711 | mutex_unlock(&icap_sem); |
ef141a0b SN |
712 | return 0; /* success */ |
713 | } | |
714 | ||
715 | static int __devinit hwicap_drv_probe(struct platform_device *pdev) | |
716 | { | |
717 | struct resource *res; | |
718 | const struct config_registers *regs; | |
719 | const char *family; | |
720 | ||
721 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
722 | if (!res) | |
723 | return -ENODEV; | |
724 | ||
725 | /* It's most likely that we're using V4, if the family is not | |
726 | specified */ | |
727 | regs = &v4_config_registers; | |
728 | family = pdev->dev.platform_data; | |
729 | ||
730 | if (family) { | |
731 | if (!strcmp(family, "virtex2p")) { | |
732 | regs = &v2_config_registers; | |
733 | } else if (!strcmp(family, "virtex4")) { | |
734 | regs = &v4_config_registers; | |
735 | } else if (!strcmp(family, "virtex5")) { | |
736 | regs = &v5_config_registers; | |
737 | } | |
738 | } | |
739 | ||
740 | return hwicap_setup(&pdev->dev, pdev->id, res, | |
741 | &buffer_icap_config, regs); | |
742 | } | |
743 | ||
744 | static int __devexit hwicap_drv_remove(struct platform_device *pdev) | |
745 | { | |
746 | return hwicap_remove(&pdev->dev); | |
747 | } | |
748 | ||
749 | static struct platform_driver hwicap_platform_driver = { | |
750 | .probe = hwicap_drv_probe, | |
751 | .remove = hwicap_drv_remove, | |
752 | .driver = { | |
753 | .owner = THIS_MODULE, | |
754 | .name = DRIVER_NAME, | |
755 | }, | |
756 | }; | |
757 | ||
758 | /* --------------------------------------------------------------------- | |
759 | * OF bus binding | |
760 | */ | |
761 | ||
762 | #if defined(CONFIG_OF) | |
763 | static int __devinit | |
2dc11581 | 764 | hwicap_of_probe(struct platform_device *op, const struct of_device_id *match) |
ef141a0b SN |
765 | { |
766 | struct resource res; | |
767 | const unsigned int *id; | |
768 | const char *family; | |
769 | int rc; | |
770 | const struct hwicap_driver_config *config = match->data; | |
771 | const struct config_registers *regs; | |
772 | ||
773 | dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match); | |
774 | ||
61c7a080 | 775 | rc = of_address_to_resource(op->dev.of_node, 0, &res); |
ef141a0b SN |
776 | if (rc) { |
777 | dev_err(&op->dev, "invalid address\n"); | |
778 | return rc; | |
779 | } | |
780 | ||
61c7a080 | 781 | id = of_get_property(op->dev.of_node, "port-number", NULL); |
ef141a0b SN |
782 | |
783 | /* It's most likely that we're using V4, if the family is not | |
784 | specified */ | |
785 | regs = &v4_config_registers; | |
61c7a080 | 786 | family = of_get_property(op->dev.of_node, "xlnx,family", NULL); |
ef141a0b SN |
787 | |
788 | if (family) { | |
789 | if (!strcmp(family, "virtex2p")) { | |
790 | regs = &v2_config_registers; | |
791 | } else if (!strcmp(family, "virtex4")) { | |
792 | regs = &v4_config_registers; | |
793 | } else if (!strcmp(family, "virtex5")) { | |
794 | regs = &v5_config_registers; | |
795 | } | |
796 | } | |
797 | return hwicap_setup(&op->dev, id ? *id : -1, &res, config, | |
798 | regs); | |
799 | } | |
800 | ||
2dc11581 | 801 | static int __devexit hwicap_of_remove(struct platform_device *op) |
ef141a0b SN |
802 | { |
803 | return hwicap_remove(&op->dev); | |
804 | } | |
805 | ||
806 | /* Match table for of_platform binding */ | |
2fd53e02 | 807 | static const struct of_device_id __devinitconst hwicap_of_match[] = { |
ef141a0b SN |
808 | { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config}, |
809 | { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config}, | |
810 | {}, | |
811 | }; | |
812 | MODULE_DEVICE_TABLE(of, hwicap_of_match); | |
813 | ||
814 | static struct of_platform_driver hwicap_of_driver = { | |
ef141a0b SN |
815 | .probe = hwicap_of_probe, |
816 | .remove = __devexit_p(hwicap_of_remove), | |
817 | .driver = { | |
818 | .name = DRIVER_NAME, | |
4018294b GL |
819 | .owner = THIS_MODULE, |
820 | .of_match_table = hwicap_of_match, | |
ef141a0b SN |
821 | }, |
822 | }; | |
823 | ||
824 | /* Registration helpers to keep the number of #ifdefs to a minimum */ | |
f62f2fdd | 825 | static inline int __init hwicap_of_register(void) |
ef141a0b SN |
826 | { |
827 | pr_debug("hwicap: calling of_register_platform_driver()\n"); | |
828 | return of_register_platform_driver(&hwicap_of_driver); | |
829 | } | |
830 | ||
f62f2fdd | 831 | static inline void __exit hwicap_of_unregister(void) |
ef141a0b SN |
832 | { |
833 | of_unregister_platform_driver(&hwicap_of_driver); | |
834 | } | |
835 | #else /* CONFIG_OF */ | |
836 | /* CONFIG_OF not enabled; do nothing helpers */ | |
f62f2fdd SN |
837 | static inline int __init hwicap_of_register(void) { return 0; } |
838 | static inline void __exit hwicap_of_unregister(void) { } | |
ef141a0b SN |
839 | #endif /* CONFIG_OF */ |
840 | ||
f62f2fdd | 841 | static int __init hwicap_module_init(void) |
ef141a0b SN |
842 | { |
843 | dev_t devt; | |
844 | int retval; | |
845 | ||
846 | icap_class = class_create(THIS_MODULE, "xilinx_config"); | |
f62f2fdd | 847 | mutex_init(&icap_sem); |
ef141a0b | 848 | |
ac646734 SN |
849 | devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR); |
850 | retval = register_chrdev_region(devt, | |
851 | HWICAP_DEVICES, | |
852 | DRIVER_NAME); | |
853 | if (retval < 0) | |
854 | return retval; | |
ef141a0b SN |
855 | |
856 | retval = platform_driver_register(&hwicap_platform_driver); | |
857 | ||
858 | if (retval) | |
859 | goto failed1; | |
860 | ||
861 | retval = hwicap_of_register(); | |
862 | ||
863 | if (retval) | |
864 | goto failed2; | |
865 | ||
866 | return retval; | |
867 | ||
868 | failed2: | |
869 | platform_driver_unregister(&hwicap_platform_driver); | |
870 | ||
871 | failed1: | |
872 | unregister_chrdev_region(devt, HWICAP_DEVICES); | |
873 | ||
874 | return retval; | |
875 | } | |
876 | ||
f62f2fdd | 877 | static void __exit hwicap_module_cleanup(void) |
ef141a0b | 878 | { |
ac646734 | 879 | dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR); |
ef141a0b SN |
880 | |
881 | class_destroy(icap_class); | |
882 | ||
883 | platform_driver_unregister(&hwicap_platform_driver); | |
884 | ||
885 | hwicap_of_unregister(); | |
886 | ||
887 | unregister_chrdev_region(devt, HWICAP_DEVICES); | |
888 | } | |
889 | ||
890 | module_init(hwicap_module_init); | |
891 | module_exit(hwicap_module_cleanup); | |
892 | ||
893 | MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group"); | |
894 | MODULE_DESCRIPTION("Xilinx ICAP Port Driver"); | |
895 | MODULE_LICENSE("GPL"); |