2 * Xilinx Zynq Device Config driver
4 * Copyright (c) 2011 - 2013 Xilinx Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/cdev.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/dma-mapping.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/sysctl.h>
29 #include <linux/types.h>
30 #include <linux/uaccess.h>
31 #include <../../arch/arm/mach-zynq/common.h>
33 #define DRIVER_NAME "xdevcfg"
34 #define XDEVCFG_DEVICES 1
36 /* An array, which is set to true when the device is registered. */
37 static DEFINE_MUTEX(xdevcfg_mutex);
39 /* Constant Definitions */
40 #define XDCFG_CTRL_OFFSET 0x00 /* Control Register */
41 #define XDCFG_LOCK_OFFSET 0x04 /* Lock Register */
42 #define XDCFG_INT_STS_OFFSET 0x0C /* Interrupt Status Register */
43 #define XDCFG_INT_MASK_OFFSET 0x10 /* Interrupt Mask Register */
44 #define XDCFG_STATUS_OFFSET 0x14 /* Status Register */
45 #define XDCFG_DMA_SRC_ADDR_OFFSET 0x18 /* DMA Source Address Register */
46 #define XDCFG_DMA_DEST_ADDR_OFFSET 0x1C /* DMA Destination Address Reg */
47 #define XDCFG_DMA_SRC_LEN_OFFSET 0x20 /* DMA Source Transfer Length */
48 #define XDCFG_DMA_DEST_LEN_OFFSET 0x24 /* DMA Destination Transfer */
49 #define XDCFG_UNLOCK_OFFSET 0x34 /* Unlock Register */
50 #define XDCFG_MCTRL_OFFSET 0x80 /* Misc. Control Register */
52 /* Control Register Bit definitions */
53 #define XDCFG_CTRL_PCFG_PROG_B_MASK 0x40000000 /* Program signal to
56 #define XDCFG_CTRL_PCAP_PR_MASK 0x08000000 /* Enable PCAP for PR */
57 #define XDCFG_CTRL_PCAP_MODE_MASK 0x04000000 /* Enable PCAP */
58 #define XDCFG_CTRL_PCAP_RATE_EN_MASK 0x02000000 /* Enable PCAP Quad Rate */
59 #define XDCFG_CTRL_PCFG_AES_EN_MASK 0x00000E00 /* AES Enable Mask */
60 #define XDCFG_CTRL_SEU_EN_MASK 0x00000100 /* SEU Enable Mask */
61 #define XDCFG_CTRL_SPNIDEN_MASK 0x00000040 /* Secure Non Invasive
64 #define XDCFG_CTRL_SPIDEN_MASK 0x00000020 /* Secure Invasive
67 #define XDCFG_CTRL_NIDEN_MASK 0x00000010 /* Non-Invasive Debug
70 #define XDCFG_CTRL_DBGEN_MASK 0x00000008 /* Invasive Debug
73 #define XDCFG_CTRL_DAP_EN_MASK 0x00000007 /* DAP Enable Mask */
75 /* Lock register bit definitions */
77 #define XDCFG_LOCK_AES_EN_MASK 0x00000008 /* Lock AES_EN update */
78 #define XDCFG_LOCK_SEU_MASK 0x00000004 /* Lock SEU_En update */
79 #define XDCFG_LOCK_DBG_MASK 0x00000001 /* This bit locks
85 /* Miscellaneous Control Register bit definitions */
86 #define XDCFG_MCTRL_PCAP_LPBK_MASK 0x00000010 /* Internal PCAP loopback */
88 /* Status register bit definitions */
89 #define XDCFG_STATUS_PCFG_INIT_MASK 0x00000010 /* FPGA init status */
91 /* Interrupt Status/Mask Register Bit definitions */
92 #define XDCFG_IXR_DMA_DONE_MASK 0x00002000 /* DMA Command Done */
93 #define XDCFG_IXR_D_P_DONE_MASK 0x00001000 /* DMA and PCAP Cmd Done */
94 #define XDCFG_IXR_PCFG_DONE_MASK 0x00000004 /* FPGA programmed */
95 #define XDCFG_IXR_ERROR_FLAGS_MASK 0x00F0F860
96 #define XDCFG_IXR_ALL_MASK 0xF8F7F87F
97 /* Miscellaneous constant values */
98 #define XDCFG_DMA_INVALID_ADDRESS 0xFFFFFFFF /* Invalid DMA address */
100 static const char * const fclk_name[] = {
106 #define NUMFCLKS ARRAY_SIZE(fclk_name)
109 * struct xdevcfg_drvdata - Device Configuration driver structure
111 * @dev: Pointer to the device structure
112 * @cdev: Instance of the cdev structure
113 * @devt: Pointer to the dev_t structure
114 * @class: Pointer to device class
115 * @fclk_class: Pointer to fclk device class
116 * @dma_done: The dma_done status bit for the DMA command completion
117 * @error_status: The error status captured during the DMA transfer
118 * @irq: Interrupt number
119 * @clk: Peripheral clock for devcfg
120 * @fclk: Array holding references to the FPGA clocks
121 * @fclk_exported: Flag inidcating whether an FPGA clock is exported
122 * @is_open: The status bit to indicate whether the device is opened
123 * @sem: Instance for the mutex
124 * @lock: Instance of spinlock
125 * @base_address: The virtual device base address of the device registers
126 * @ep107: Flags is used to identify the platform
127 * @endian_swap: Flags is used to identify the endianness format
128 * @residue_buf: Array holding stragglers from last time (0 to 3 bytes)
129 * @residue_len: stragglers length in bytes
130 * @is_partial_bitstream: Status bit to indicate partial/full bitstream
132 struct xdevcfg_drvdata {
137 struct class *fclk_class;
140 struct clk *fclk[NUMFCLKS];
141 u8 fclk_exported[NUMFCLKS];
147 void __iomem *base_address;
149 bool is_partial_bitstream;
156 * struct fclk_data - FPGA clock data
157 * @clk: Pointer to clock
158 * @enabled: Flag indicating enable status of the clock
159 * @rate_rnd: Rate to be rounded for round rate operation
164 unsigned long rate_rnd;
167 /* Register read/write access routines */
168 #define xdevcfg_writereg(offset, val) __raw_writel(val, offset)
169 #define xdevcfg_readreg(offset) __raw_readl(offset)
172 * xdevcfg_reset_pl - Reset the programmable logic.
173 * @base_address: The base address of the device.
175 * Must be called with PCAP clock enabled
177 static void xdevcfg_reset_pl(void __iomem *base_address)
180 * Create a rising edge on PCFG_INIT. PCFG_INIT follows PCFG_PROG_B,
181 * so we need to * poll it after setting PCFG_PROG_B to make sure that
182 * the rising edge happens.
184 xdevcfg_writereg(base_address + XDCFG_CTRL_OFFSET,
185 (xdevcfg_readreg(base_address + XDCFG_CTRL_OFFSET) &
186 ~XDCFG_CTRL_PCFG_PROG_B_MASK));
187 while (xdevcfg_readreg(base_address + XDCFG_STATUS_OFFSET) &
188 XDCFG_STATUS_PCFG_INIT_MASK)
191 usleep_range(5000, 5100);
192 xdevcfg_writereg(base_address + XDCFG_CTRL_OFFSET,
193 (xdevcfg_readreg(base_address + XDCFG_CTRL_OFFSET) |
194 XDCFG_CTRL_PCFG_PROG_B_MASK));
195 while (!(xdevcfg_readreg(base_address + XDCFG_STATUS_OFFSET) &
196 XDCFG_STATUS_PCFG_INIT_MASK))
201 * xdevcfg_irq - The main interrupt handler.
202 * @irq: The interrupt number.
203 * @data: Pointer to the driver data structure.
204 * returns: IRQ_HANDLED after the interrupt is handled.
206 static irqreturn_t xdevcfg_irq(int irq, void *data)
209 struct xdevcfg_drvdata *drvdata = data;
211 spin_lock(&drvdata->lock);
213 intr_status = xdevcfg_readreg(drvdata->base_address +
214 XDCFG_INT_STS_OFFSET);
216 /* Clear the interrupts */
217 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
220 if ((intr_status & XDCFG_IXR_D_P_DONE_MASK) ==
221 XDCFG_IXR_D_P_DONE_MASK)
222 drvdata->dma_done = 1;
224 if ((intr_status & XDCFG_IXR_ERROR_FLAGS_MASK) ==
225 XDCFG_IXR_ERROR_FLAGS_MASK)
226 drvdata->error_status = 1;
228 spin_unlock(&drvdata->lock);
234 * xdevcfg_write - The is the driver write function.
236 * @file: Pointer to the file structure.
237 * @buf: Pointer to the bitstream location.
238 * @count: The number of bytes to be written.
239 * @ppos: Pointer to the offset value
240 * returns: Success or error status.
243 xdevcfg_write(struct file *file, const char __user *buf, size_t count,
248 unsigned long timeout;
249 u32 intr_reg, dma_len;
251 u32 transfer_length = 0;
252 struct xdevcfg_drvdata *drvdata = file->private_data;
253 size_t user_count = count;
256 status = clk_enable(drvdata->clk);
260 status = mutex_lock_interruptible(&drvdata->sem);
265 dma_len = count + drvdata->residue_len;
266 kbuf = dma_alloc_coherent(drvdata->dev, dma_len, &dma_addr, GFP_KERNEL);
272 /* Collect stragglers from last time (0 to 3 bytes) */
273 memcpy(kbuf, drvdata->residue_buf, drvdata->residue_len);
275 /* Fetch user data, appending to stragglers */
276 if (copy_from_user(kbuf + drvdata->residue_len, buf, count)) {
281 /* Include stragglers in total bytes to be handled */
282 count += drvdata->residue_len;
284 /* First block contains a header */
285 if (*ppos == 0 && count > 4) {
286 /* Look for sync word */
287 for (i = 0; i < count - 4; i++) {
288 if (memcmp(kbuf + i, "\x66\x55\x99\xAA", 4) == 0) {
289 pr_debug("Found normal sync word\n");
290 drvdata->endian_swap = 0;
293 if (memcmp(kbuf + i, "\xAA\x99\x55\x66", 4) == 0) {
294 pr_debug("Found swapped sync word\n");
295 drvdata->endian_swap = 1;
299 /* Remove the header, aligning the data on word boundary */
300 if (i != count - 4) {
302 memmove(kbuf, kbuf + i, count);
306 /* Save stragglers for next time */
307 drvdata->residue_len = count % 4;
308 count -= drvdata->residue_len;
309 memcpy(drvdata->residue_buf, kbuf + count, drvdata->residue_len);
311 /* Fixup endianess of the data */
312 if (drvdata->endian_swap) {
313 for (i = 0; i < count; i += 4) {
314 u32 *p = (u32 *)&kbuf[i];
319 /* Enable DMA and error interrupts */
320 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
324 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
325 (u32) (~(XDCFG_IXR_D_P_DONE_MASK |
326 XDCFG_IXR_ERROR_FLAGS_MASK)));
328 drvdata->dma_done = 0;
329 drvdata->error_status = 0;
331 /* Initiate DMA write command */
333 xdevcfg_writereg(drvdata->base_address +
334 XDCFG_DMA_SRC_ADDR_OFFSET, (u32)(dma_addr + 1));
336 xdevcfg_writereg(drvdata->base_address +
337 XDCFG_DMA_SRC_ADDR_OFFSET, (u32) dma_addr);
339 xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_ADDR_OFFSET,
340 (u32)XDCFG_DMA_INVALID_ADDRESS);
341 /* Convert number of bytes to number of words. */
343 transfer_length = (count / 4 + 1);
345 transfer_length = count / 4;
346 xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_SRC_LEN_OFFSET,
348 xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_LEN_OFFSET, 0);
350 timeout = jiffies + msecs_to_jiffies(1000);
352 while (!READ_ONCE(drvdata->dma_done)) {
353 if (time_after(jiffies, timeout)) {
359 if (READ_ONCE(drvdata->error_status))
360 status = drvdata->error_status;
362 /* Disable the DMA and error interrupts */
363 intr_reg = xdevcfg_readreg(drvdata->base_address +
364 XDCFG_INT_MASK_OFFSET);
365 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
366 intr_reg | (XDCFG_IXR_D_P_DONE_MASK |
367 XDCFG_IXR_ERROR_FLAGS_MASK));
369 /* If we didn't write correctly, then bail out. */
379 dma_free_coherent(drvdata->dev, dma_len, kbuf, dma_addr);
381 mutex_unlock(&drvdata->sem);
383 clk_disable(drvdata->clk);
389 * xdevcfg_read - The is the driver read function.
390 * @file: Pointer to the file structure.
391 * @buf: Pointer to the bitstream location.
392 * @count: The number of bytes read.
393 * @ppos: Pointer to the offsetvalue
394 * returns: Success or error status.
397 xdevcfg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
401 unsigned long timeout;
403 struct xdevcfg_drvdata *drvdata = file->private_data;
406 status = clk_enable(drvdata->clk);
410 status = mutex_lock_interruptible(&drvdata->sem);
414 /* Get new data from the ICAP, and return was requested. */
415 kbuf = dma_alloc_coherent(drvdata->dev, count, &dma_addr, GFP_KERNEL);
421 drvdata->dma_done = 0;
422 drvdata->error_status = 0;
424 /* Enable DMA and error interrupts */
425 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
428 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
429 (u32) (~(XDCFG_IXR_D_P_DONE_MASK |
430 XDCFG_IXR_ERROR_FLAGS_MASK)));
431 /* Initiate DMA read command */
432 xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_SRC_ADDR_OFFSET,
433 (u32)XDCFG_DMA_INVALID_ADDRESS);
434 xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_ADDR_OFFSET,
436 xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_SRC_LEN_OFFSET, 0);
437 xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_LEN_OFFSET,
440 timeout = jiffies + msecs_to_jiffies(1000);
442 while (!drvdata->dma_done) {
443 if (time_after(jiffies, timeout)) {
449 if (drvdata->error_status)
450 status = drvdata->error_status;
452 /* Disable and clear DMA and error interrupts */
453 intr_reg = xdevcfg_readreg(drvdata->base_address +
454 XDCFG_INT_MASK_OFFSET);
455 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
456 intr_reg | (XDCFG_IXR_D_P_DONE_MASK |
457 XDCFG_IXR_ERROR_FLAGS_MASK));
460 /* If we didn't read correctly, then bail out. */
466 /* If we fail to return the data to the user, then bail out. */
467 if (copy_to_user(buf, kbuf, count)) {
474 dma_free_coherent(drvdata->dev, count, kbuf, dma_addr);
476 mutex_unlock(&drvdata->sem);
478 clk_disable(drvdata->clk);
483 static void xdevcfg_enable_partial(struct xdevcfg_drvdata *drvdata)
485 u32 reg = xdevcfg_readreg(drvdata->base_address + XDCFG_CTRL_OFFSET);
487 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
488 reg | XDCFG_CTRL_PCAP_PR_MASK);
491 static void xdevcfg_disable_partial(struct xdevcfg_drvdata *drvdata)
493 u32 reg = xdevcfg_readreg(drvdata->base_address + XDCFG_CTRL_OFFSET);
495 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
496 reg & ~XDCFG_CTRL_PCAP_PR_MASK);
500 * xdevcfg_open - The is the driver open function.
501 * @inode: Pointer to the inode structure of this device.
502 * @file: Pointer to the file structure.
503 * returns: Success or error status.
505 static int xdevcfg_open(struct inode *inode, struct file *file)
507 struct xdevcfg_drvdata *drvdata;
510 drvdata = container_of(inode->i_cdev, struct xdevcfg_drvdata, cdev);
512 status = clk_enable(drvdata->clk);
516 status = mutex_lock_interruptible(&drvdata->sem);
520 if (drvdata->is_open) {
525 file->private_data = drvdata;
526 drvdata->is_open = 1;
527 drvdata->endian_swap = 0;
528 drvdata->residue_len = 0;
531 * If is_partial_bitstream is set, then PROG_B is not asserted
532 * (xdevcfg_reset_pl function) and also zynq_slcr_init_preload_fpga and
533 * zynq_slcr_init_postload_fpga functions are not invoked.
535 if (drvdata->is_partial_bitstream)
536 xdevcfg_enable_partial(drvdata);
538 zynq_slcr_init_preload_fpga();
541 * Only do the reset of the PL for Zynq as it causes problems on the
542 * EP107 and the issue is not understood, but not worth investigating
543 * as the emulation platform is very different than silicon and not a
544 * complete implementation. Also, do not reset if it is a partial
547 if ((!drvdata->ep107) && (!drvdata->is_partial_bitstream))
548 xdevcfg_reset_pl(drvdata->base_address);
550 xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
551 XDCFG_IXR_PCFG_DONE_MASK);
554 mutex_unlock(&drvdata->sem);
556 clk_disable(drvdata->clk);
561 * xdevcfg_release - The is the driver release function.
562 * @inode: Pointer to the inode structure of this device.
563 * @file: Pointer to the file structure.
566 static int xdevcfg_release(struct inode *inode, struct file *file)
568 struct xdevcfg_drvdata *drvdata = file->private_data;
570 if (drvdata->is_partial_bitstream)
571 xdevcfg_disable_partial(drvdata);
573 zynq_slcr_init_postload_fpga();
575 if (drvdata->residue_len)
576 dev_info(drvdata->dev, "Did not transfer last %d bytes\n",
577 drvdata->residue_len);
579 drvdata->is_open = 0;
584 static const struct file_operations xdevcfg_fops = {
585 .owner = THIS_MODULE,
586 .write = xdevcfg_write,
587 .read = xdevcfg_read,
588 .open = xdevcfg_open,
589 .release = xdevcfg_release,
593 * The following functions are the routines provided to the user to
594 * set/get the status bit value in the control/lock registers.
598 * xdevcfg_set_dap_en - This function sets the DAP bits in the
599 * control register with the given value.
600 * @dev: Pointer to the device structure.
601 * @attr: Pointer to the device attribute structure.
602 * @buf: Pointer to the buffer location for the configuration
604 * @size: The number of bytes used from the buffer
605 * returns: negative error if the string could not be converted
606 * or the size of the buffer.
608 static ssize_t xdevcfg_set_dap_en(struct device *dev,
609 struct device_attribute *attr, const char *buf, size_t size)
613 unsigned long mask_bit;
615 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
617 status = clk_enable(drvdata->clk);
621 ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
623 spin_lock_irqsave(&drvdata->lock, flags);
625 status = kstrtoul(buf, 10, &mask_bit);
635 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
637 (((u32)mask_bit) & XDCFG_CTRL_DAP_EN_MASK)));
639 spin_unlock_irqrestore(&drvdata->lock, flags);
641 clk_disable(drvdata->clk);
646 spin_unlock_irqrestore(&drvdata->lock, flags);
647 clk_disable(drvdata->clk);
653 * xdevcfg_show_dap_en_status - The function returns the DAP_EN bits status in
654 * the control register.
655 * @dev: Pointer to the device structure.
656 * @attr: Pointer to the device attribute structure.
657 * @buf: Pointer to the buffer location for the configuration
659 * returns: Size of the buffer.
661 static ssize_t xdevcfg_show_dap_en_status(struct device *dev,
662 struct device_attribute *attr, char *buf)
666 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
668 status = clk_enable(drvdata->clk);
672 dap_en_status = xdevcfg_readreg(drvdata->base_address +
673 XDCFG_CTRL_OFFSET) & XDCFG_CTRL_DAP_EN_MASK;
675 clk_disable(drvdata->clk);
677 status = sprintf(buf, "%d\n", dap_en_status);
682 static DEVICE_ATTR(enable_dap, 0644, xdevcfg_show_dap_en_status,
686 * xdevcfg_set_dbgen - This function sets the DBGEN bit in the
687 * control register with the given value.
688 * @dev: Pointer to the device structure.
689 * @attr: Pointer to the device attribute structure.
690 * @buf: Pointer to the buffer location for the configuration
692 * @size: The number of bytes used from the buffer
693 * returns: -EINVAL if invalid parameter is sent or size
695 static ssize_t xdevcfg_set_dbgen(struct device *dev,
696 struct device_attribute *attr, const char *buf, size_t size)
700 unsigned long mask_bit;
702 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
704 status = clk_enable(drvdata->clk);
708 ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
711 status = kstrtoul(buf, 10, &mask_bit);
721 spin_lock_irqsave(&drvdata->lock, flags);
724 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
725 (ctrl_reg_status | XDCFG_CTRL_DBGEN_MASK));
727 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
728 (ctrl_reg_status & (~XDCFG_CTRL_DBGEN_MASK)));
730 spin_unlock_irqrestore(&drvdata->lock, flags);
732 clk_disable(drvdata->clk);
737 clk_disable(drvdata->clk);
743 * xdevcfg_show_dbgen_status - The function returns the DBGEN bit status in
744 * the control register.
745 * @dev: Pointer to the device structure.
746 * @attr: Pointer to the device attribute structure.
747 * @buf: Pointer to the buffer location for the configuration
749 * returns: Size of the buffer.
751 static ssize_t xdevcfg_show_dbgen_status(struct device *dev,
752 struct device_attribute *attr, char *buf)
756 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
758 status = clk_enable(drvdata->clk);
762 dbgen_status = xdevcfg_readreg(drvdata->base_address +
763 XDCFG_CTRL_OFFSET) & XDCFG_CTRL_DBGEN_MASK;
765 clk_disable(drvdata->clk);
767 status = sprintf(buf, "%d\n", (dbgen_status >> 3));
772 static DEVICE_ATTR(enable_dbg_in, 0644, xdevcfg_show_dbgen_status,
776 * xdevcfg_set_niden - This function sets the NIDEN bit in the
777 * control register with the given value.
778 * @dev: Pointer to the device structure.
779 * @attr: Pointer to the device attribute structure.
780 * @buf: Pointer to the buffer location for the configuration
782 * @size: The number of bytes used from the buffer
783 * returns: -EINVAL if invalid parameter is sent or size
785 static ssize_t xdevcfg_set_niden(struct device *dev,
786 struct device_attribute *attr, const char *buf, size_t size)
790 unsigned long mask_bit;
792 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
794 status = clk_enable(drvdata->clk);
798 ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
801 status = kstrtoul(buf, 10, &mask_bit);
811 spin_lock_irqsave(&drvdata->lock, flags);
814 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
815 (ctrl_reg_status | XDCFG_CTRL_NIDEN_MASK));
817 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
818 (ctrl_reg_status & (~XDCFG_CTRL_NIDEN_MASK)));
820 spin_unlock_irqrestore(&drvdata->lock, flags);
822 clk_disable(drvdata->clk);
827 clk_disable(drvdata->clk);
833 * xdevcfg_show_niden_status - The function returns the NIDEN bit status in
834 * the control register.
835 * @dev: Pointer to the device structure.
836 * @attr: Pointer to the device attribute structure.
837 * @buf: Pointer to the buffer location for the configuration
839 * returns: Size of the buffer.
841 static ssize_t xdevcfg_show_niden_status(struct device *dev,
842 struct device_attribute *attr, char *buf)
846 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
848 status = clk_enable(drvdata->clk);
852 niden_status = xdevcfg_readreg(drvdata->base_address +
853 XDCFG_CTRL_OFFSET) & XDCFG_CTRL_NIDEN_MASK;
855 clk_disable(drvdata->clk);
857 status = sprintf(buf, "%d\n", (niden_status >> 4));
862 static DEVICE_ATTR(enable_dbg_nonin, 0644, xdevcfg_show_niden_status,
866 * xdevcfg_set_spiden - This function sets the SPIDEN bit in the
867 * control register with the given value.
868 * @dev: Pointer to the device structure.
869 * @attr: Pointer to the device attribute structure.
870 * @buf: Pointer to the buffer location for the configuration
872 * @size: The number of bytes used from the buffer
873 * returns: -EINVAL if invalid parameter is sent or size
875 static ssize_t xdevcfg_set_spiden(struct device *dev,
876 struct device_attribute *attr, const char *buf, size_t size)
880 unsigned long mask_bit;
882 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
884 status = clk_enable(drvdata->clk);
888 ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
891 status = kstrtoul(buf, 10, &mask_bit);
901 spin_lock_irqsave(&drvdata->lock, flags);
904 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
905 (ctrl_reg_status | XDCFG_CTRL_SPIDEN_MASK));
908 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
909 (ctrl_reg_status & (~XDCFG_CTRL_SPIDEN_MASK)));
911 spin_unlock_irqrestore(&drvdata->lock, flags);
913 clk_disable(drvdata->clk);
918 clk_disable(drvdata->clk);
924 * xdevcfg_show_spiden_status - The function returns the SPIDEN bit status in
925 * the control register.
926 * @dev: Pointer to the device structure.
927 * @attr: Pointer to the device attribute structure.
928 * @buf: Pointer to the buffer location for the configuration
930 * returns: Size of the buffer.
932 static ssize_t xdevcfg_show_spiden_status(struct device *dev,
933 struct device_attribute *attr, char *buf)
937 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
939 status = clk_enable(drvdata->clk);
943 spiden_status = xdevcfg_readreg(drvdata->base_address +
944 XDCFG_CTRL_OFFSET) & XDCFG_CTRL_SPIDEN_MASK;
946 clk_disable(drvdata->clk);
948 status = sprintf(buf, "%d\n", (spiden_status >> 5));
953 static DEVICE_ATTR(enable_sec_dbg_in, 0644, xdevcfg_show_spiden_status,
957 * xdevcfg_set_spniden - This function sets the SPNIDEN bit in the
958 * control register with the given value.
959 * @dev: Pointer to the device structure.
960 * @attr: Pointer to the device attribute structure.
961 * @buf: Pointer to the buffer location for the configuration
963 * @size: The number of bytes used from the buffer
964 * returns: -EINVAL if invalid parameter is sent or the size of buffer
966 static ssize_t xdevcfg_set_spniden(struct device *dev,
967 struct device_attribute *attr, const char *buf, size_t size)
971 unsigned long mask_bit;
973 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
975 status = clk_enable(drvdata->clk);
979 ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
981 status = kstrtoul(buf, 10, &mask_bit);
991 spin_lock_irqsave(&drvdata->lock, flags);
994 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
995 (ctrl_reg_status | XDCFG_CTRL_SPNIDEN_MASK));
997 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
998 (ctrl_reg_status & (~XDCFG_CTRL_SPNIDEN_MASK)));
1000 spin_unlock_irqrestore(&drvdata->lock, flags);
1002 clk_disable(drvdata->clk);
1007 clk_disable(drvdata->clk);
1013 * xdevcfg_show_spniden_status - The function returns the SPNIDEN bit status
1014 * in the control register.
1015 * @dev: Pointer to the device structure.
1016 * @attr: Pointer to the device attribute structure.
1017 * @buf: Pointer to the buffer location for the configuration
1019 * returns: Size of the buffer.
1021 static ssize_t xdevcfg_show_spniden_status(struct device *dev,
1022 struct device_attribute *attr, char *buf)
1026 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1028 status = clk_enable(drvdata->clk);
1032 spniden_status = xdevcfg_readreg(drvdata->base_address +
1033 XDCFG_CTRL_OFFSET) & XDCFG_CTRL_SPNIDEN_MASK;
1035 clk_disable(drvdata->clk);
1037 status = sprintf(buf, "%d\n", (spniden_status >> 6));
1042 static DEVICE_ATTR(enable_sec_dbg_nonin, 0644, xdevcfg_show_spniden_status,
1043 xdevcfg_set_spniden);
1046 * xdevcfg_set_seu - This function sets the SEU_EN bit in the
1047 * control register with the given value
1048 * @dev: Pointer to the device structure.
1049 * @attr: Pointer to the device attribute structure.
1050 * @buf: Pointer to the buffer location for the configuration
1052 * @size: The number of bytes used from the buffer
1053 * returns: -EINVAL if invalid parameter is sent or size
1055 static ssize_t xdevcfg_set_seu(struct device *dev,
1056 struct device_attribute *attr, const char *buf, size_t size)
1058 u32 ctrl_reg_status;
1059 unsigned long flags;
1060 unsigned long mask_bit;
1062 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1064 status = clk_enable(drvdata->clk);
1068 ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
1071 status = kstrtoul(buf, 10, &mask_bit);
1081 spin_lock_irqsave(&drvdata->lock, flags);
1084 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
1085 (ctrl_reg_status | XDCFG_CTRL_SEU_EN_MASK));
1087 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
1088 (ctrl_reg_status & (~XDCFG_CTRL_SEU_EN_MASK)));
1090 spin_unlock_irqrestore(&drvdata->lock, flags);
1092 clk_disable(drvdata->clk);
1097 clk_disable(drvdata->clk);
1103 * xdevcfg_show_seu_status - The function returns the SEU_EN bit status
1104 * in the control register.
1105 * @dev: Pointer to the device structure.
1106 * @attr: Pointer to the device attribute structure.
1107 * @buf: Pointer to the buffer location for the configuration
1109 * returns: size of the buffer.
1111 static ssize_t xdevcfg_show_seu_status(struct device *dev,
1112 struct device_attribute *attr, char *buf)
1116 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1118 status = clk_enable(drvdata->clk);
1122 seu_status = xdevcfg_readreg(drvdata->base_address +
1123 XDCFG_CTRL_OFFSET) & XDCFG_CTRL_SEU_EN_MASK;
1125 clk_disable(drvdata->clk);
1127 status = sprintf(buf, "%d\n", (seu_status > 8));
1132 static DEVICE_ATTR(enable_seu, 0644, xdevcfg_show_seu_status, xdevcfg_set_seu);
1135 * xdevcfg_set_aes - This function sets the AES_EN bits in the
1136 * control register with either all 1s or all 0s.
1137 * @dev: Pointer to the device structure.
1138 * @attr: Pointer to the device attribute structure.
1139 * @buf: Pointer to the buffer location for the configuration
1141 * @size: The number of bytes used from the buffer
1142 * returns: -EINVAL if invalid parameter is sent or size
1144 * The user must send only one bit in the buffer to notify whether he wants to
1145 * either set or reset these bits.
1147 static ssize_t xdevcfg_set_aes(struct device *dev,
1148 struct device_attribute *attr, const char *buf, size_t size)
1150 u32 ctrl_reg_status;
1151 unsigned long flags;
1152 unsigned long mask_bit;
1154 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1156 status = clk_enable(drvdata->clk);
1160 ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
1163 status = kstrtoul(buf, 10, &mask_bit);
1174 spin_lock_irqsave(&drvdata->lock, flags);
1177 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
1179 XDCFG_CTRL_PCFG_AES_EN_MASK |
1180 XDCFG_CTRL_PCAP_RATE_EN_MASK));
1182 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
1184 ~(XDCFG_CTRL_PCFG_AES_EN_MASK |
1185 XDCFG_CTRL_PCAP_RATE_EN_MASK)));
1187 spin_unlock_irqrestore(&drvdata->lock, flags);
1189 clk_disable(drvdata->clk);
1194 clk_disable(drvdata->clk);
1200 * xdevcfg_show_aes_status - The function returns the AES_EN bit status
1201 * in the control register.
1202 * @dev: Pointer to the device structure.
1203 * @attr: Pointer to the device attribute structure.
1204 * @buf: Pointer to the buffer location for the configuration
1206 * returns: size of the buffer.
1208 static ssize_t xdevcfg_show_aes_status(struct device *dev,
1209 struct device_attribute *attr, char *buf)
1213 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1215 status = clk_enable(drvdata->clk);
1219 aes_status = xdevcfg_readreg(drvdata->base_address +
1220 XDCFG_CTRL_OFFSET) & XDCFG_CTRL_PCFG_AES_EN_MASK;
1222 clk_disable(drvdata->clk);
1224 status = sprintf(buf, "%d\n", (aes_status >> 9));
1229 static DEVICE_ATTR(enable_aes, 0644, xdevcfg_show_aes_status, xdevcfg_set_aes);
1232 * xdevcfg_set_aes_en_lock - This function sets the LOCK_AES_EN bit in the
1234 * @dev: Pointer to the device structure.
1235 * @attr: Pointer to the device attribute structure.
1236 * @buf: Pointer to the buffer location for the configuration
1238 * @size: The number of bytes used from the buffer
1239 * returns: -EINVAL if invalid parameter is sent or size
1241 static ssize_t xdevcfg_set_aes_en_lock(struct device *dev,
1242 struct device_attribute *attr, const char *buf, size_t size)
1244 u32 aes_en_lock_status;
1245 unsigned long flags;
1246 unsigned long mask_bit;
1248 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1250 status = clk_enable(drvdata->clk);
1254 aes_en_lock_status = xdevcfg_readreg(drvdata->base_address +
1257 status = kstrtoul(buf, 10, &mask_bit);
1267 spin_lock_irqsave(&drvdata->lock, flags);
1270 xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
1271 (aes_en_lock_status | XDCFG_LOCK_AES_EN_MASK));
1273 xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
1274 (aes_en_lock_status &
1275 (~XDCFG_LOCK_AES_EN_MASK)));
1277 spin_unlock_irqrestore(&drvdata->lock, flags);
1279 clk_disable(drvdata->clk);
1284 clk_disable(drvdata->clk);
1290 * xdevcfg_show_aes_en_lock_status - The function returns the LOCK_AES_EN bit
1291 * status in the lock register.
1292 * @dev: Pointer to the device structure.
1293 * @attr: Pointer to the device attribute structure.
1294 * @buf: Pointer to the buffer location for the configuration
1296 * returns: size of the buffer.
1298 static ssize_t xdevcfg_show_aes_en_lock_status(struct device *dev,
1299 struct device_attribute *attr, char *buf)
1301 u32 aes_en_lock_status;
1303 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1305 status = clk_enable(drvdata->clk);
1309 aes_en_lock_status = xdevcfg_readreg(drvdata->base_address +
1310 XDCFG_LOCK_OFFSET) & XDCFG_LOCK_AES_EN_MASK;
1312 clk_disable(drvdata->clk);
1314 status = sprintf(buf, "%d\n", (aes_en_lock_status >> 3));
1319 static DEVICE_ATTR(aes_en_lock, 0644, xdevcfg_show_aes_en_lock_status,
1320 xdevcfg_set_aes_en_lock);
1323 * xdevcfg_set_seu_lock - This function sets the LOCK_SEU bit in the
1325 * @dev: Pointer to the device structure.
1326 * @attr: Pointer to the device attribute structure.
1327 * @buf: Pointer to the buffer location for the configuration
1329 * @size: The number of bytes used from the buffer
1330 * returns: -EINVAL if invalid parameter is sent or size
1332 static ssize_t xdevcfg_set_seu_lock(struct device *dev,
1333 struct device_attribute *attr, const char *buf, size_t size)
1335 u32 seu_lock_status;
1336 unsigned long flags;
1337 unsigned long mask_bit;
1339 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1341 status = clk_enable(drvdata->clk);
1345 seu_lock_status = xdevcfg_readreg(drvdata->base_address +
1348 status = kstrtoul(buf, 10, &mask_bit);
1358 spin_lock_irqsave(&drvdata->lock, flags);
1361 xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
1362 (seu_lock_status | XDCFG_LOCK_SEU_MASK));
1364 xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
1365 (seu_lock_status & (~XDCFG_LOCK_SEU_MASK)));
1367 spin_unlock_irqrestore(&drvdata->lock, flags);
1369 clk_disable(drvdata->clk);
1374 clk_disable(drvdata->clk);
1380 * xdevcfg_show_seu_lock_status - The function returns the LOCK_SEU bit
1381 * status in the lock register.
1382 * @dev: Pointer to the device structure.
1383 * @attr: Pointer to the device attribute structure.
1384 * @buf: Pointer to the buffer location for the configuration
1386 * returns: size of the buffer.
1388 static ssize_t xdevcfg_show_seu_lock_status(struct device *dev,
1389 struct device_attribute *attr, char *buf)
1391 u32 seu_lock_status;
1393 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1395 status = clk_enable(drvdata->clk);
1399 seu_lock_status = xdevcfg_readreg(drvdata->base_address +
1400 XDCFG_LOCK_OFFSET) & XDCFG_LOCK_SEU_MASK;
1402 clk_disable(drvdata->clk);
1404 status = sprintf(buf, "%d\n", (seu_lock_status >> 2));
1409 static DEVICE_ATTR(seu_lock, 0644, xdevcfg_show_seu_lock_status,
1410 xdevcfg_set_seu_lock);
1413 * xdevcfg_set_dbg_lock - This function sets the LOCK_DBG bit in the
1415 * @dev: Pointer to the device structure.
1416 * @attr: Pointer to the device attribute structure.
1417 * @buf: Pointer to the buffer location for the configuration
1419 * @size: The number of bytes used from the buffer
1420 * returns: -EINVAL if invalid parameter is sent or size
1422 static ssize_t xdevcfg_set_dbg_lock(struct device *dev,
1423 struct device_attribute *attr, const char *buf, size_t size)
1425 u32 lock_reg_status;
1426 unsigned long flags;
1427 unsigned long mask_bit;
1429 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1431 status = clk_enable(drvdata->clk);
1435 lock_reg_status = xdevcfg_readreg(drvdata->base_address +
1437 status = kstrtoul(buf, 10, &mask_bit);
1447 spin_lock_irqsave(&drvdata->lock, flags);
1450 xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
1451 (lock_reg_status | XDCFG_LOCK_DBG_MASK));
1453 xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
1454 (lock_reg_status & (~XDCFG_LOCK_DBG_MASK)));
1456 spin_unlock_irqrestore(&drvdata->lock, flags);
1458 clk_disable(drvdata->clk);
1463 clk_disable(drvdata->clk);
1469 * xdevcfg_show_dbg_lock_status - The function returns the LOCK_DBG bit
1470 * status in the lock register.
1471 * @dev: Pointer to the device structure.
1472 * @attr: Pointer to the device attribute structure.
1473 * @buf: Pointer to the buffer location for the configuration
1475 * returns: size of the buffer.
1477 static ssize_t xdevcfg_show_dbg_lock_status(struct device *dev,
1478 struct device_attribute *attr, char *buf)
1480 u32 dbg_lock_status;
1482 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1484 status = clk_enable(drvdata->clk);
1488 dbg_lock_status = xdevcfg_readreg(drvdata->base_address +
1489 XDCFG_LOCK_OFFSET) & XDCFG_LOCK_DBG_MASK;
1491 clk_disable(drvdata->clk);
1493 status = sprintf(buf, "%d\n", dbg_lock_status);
1498 static DEVICE_ATTR(dbg_lock, 0644, xdevcfg_show_dbg_lock_status,
1499 xdevcfg_set_dbg_lock);
1502 * xdevcfg_show_prog_done_status - The function returns the PROG_DONE bit
1503 * status in the interrupt status register.
1504 * @dev: Pointer to the device structure.
1505 * @attr: Pointer to the device attribute structure.
1506 * @buf: Pointer to the buffer location for the configuration
1508 * returns: size of the buffer.
1510 static ssize_t xdevcfg_show_prog_done_status(struct device *dev,
1511 struct device_attribute *attr, char *buf)
1513 u32 prog_done_status;
1515 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1517 status = clk_enable(drvdata->clk);
1521 prog_done_status = xdevcfg_readreg(drvdata->base_address +
1522 XDCFG_INT_STS_OFFSET) & XDCFG_IXR_PCFG_DONE_MASK;
1524 clk_disable(drvdata->clk);
1526 status = sprintf(buf, "%d\n", (prog_done_status >> 2));
1531 static DEVICE_ATTR(prog_done, 0644, xdevcfg_show_prog_done_status,
1535 * xdevcfg_set_is_partial_bitstream - This function sets the
1536 * is_partial_bitstream variable. If is_partial_bitstream is set,
1537 * then PROG_B is not asserted (xdevcfg_reset_pl) and also
1538 * zynq_slcr_init_preload_fpga and zynq_slcr_init_postload_fpga functions
1540 * @dev: Pointer to the device structure.
1541 * @attr: Pointer to the device attribute structure.
1542 * @buf: Pointer to the buffer location for the configuration
1544 * @size: The number of bytes used from the buffer
1545 * returns: -EINVAL if invalid parameter is sent or size
1547 static ssize_t xdevcfg_set_is_partial_bitstream(struct device *dev,
1548 struct device_attribute *attr, const char *buf, size_t size)
1550 unsigned long mask_bit;
1552 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1554 status = kstrtoul(buf, 10, &mask_bit);
1563 drvdata->is_partial_bitstream = 1;
1565 drvdata->is_partial_bitstream = 0;
1571 * xdevcfg_show_is_partial_bitstream_status - The function returns the
1572 * value of is_partial_bitstream variable.
1573 * @dev: Pointer to the device structure.
1574 * @attr: Pointer to the device attribute structure.
1575 * @buf: Pointer to the buffer location for the configuration
1577 * returns: size of the buffer.
1579 static ssize_t xdevcfg_show_is_partial_bitstream_status(struct device *dev,
1580 struct device_attribute *attr, char *buf)
1583 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1585 status = sprintf(buf, "%d\n", drvdata->is_partial_bitstream);
1590 static DEVICE_ATTR(is_partial_bitstream, 0644,
1591 xdevcfg_show_is_partial_bitstream_status,
1592 xdevcfg_set_is_partial_bitstream);
1594 static const struct attribute *xdevcfg_attrs[] = {
1595 &dev_attr_prog_done.attr, /* PCFG_DONE bit in Intr Status register */
1596 &dev_attr_dbg_lock.attr, /* Debug lock bit in Lock register */
1597 &dev_attr_seu_lock.attr, /* SEU lock bit in Lock register */
1598 &dev_attr_aes_en_lock.attr, /* AES EN lock bit in Lock register */
1599 &dev_attr_enable_aes.attr, /* AES EN bit in Control register */
1600 &dev_attr_enable_seu.attr, /* SEU EN bit in Control register */
1601 &dev_attr_enable_sec_dbg_nonin.attr, /*SPNIDEN bit in Control register*/
1602 &dev_attr_enable_sec_dbg_in.attr, /*SPIDEN bit in Control register */
1603 &dev_attr_enable_dbg_nonin.attr, /* NIDEN bit in Control register */
1604 &dev_attr_enable_dbg_in.attr, /* DBGEN bit in Control register */
1605 &dev_attr_enable_dap.attr, /* DAP_EN bits in Control register */
1606 &dev_attr_is_partial_bitstream.attr, /* Flag for partial bitstream */
1611 static const struct attribute_group xdevcfg_attr_group = {
1612 .attrs = (struct attribute **) xdevcfg_attrs,
1615 static ssize_t fclk_enable_show(struct device *dev,
1616 struct device_attribute *attr, char *buf)
1618 struct fclk_data *pdata = dev_get_drvdata(dev);
1620 return scnprintf(buf, PAGE_SIZE, "%u\n", pdata->enabled);
1623 static ssize_t fclk_enable_store(struct device *dev,
1624 struct device_attribute *attr, const char *buf, size_t count)
1626 unsigned long enable;
1628 struct fclk_data *pdata = dev_get_drvdata(dev);
1630 ret = kstrtoul(buf, 0, &enable);
1635 if (enable == pdata->enabled)
1639 ret = clk_enable(pdata->clk);
1641 clk_disable(pdata->clk);
1646 pdata->enabled = enable;
1650 static DEVICE_ATTR(enable, 0644, fclk_enable_show, fclk_enable_store);
1652 static ssize_t fclk_set_rate_show(struct device *dev,
1653 struct device_attribute *attr, char *buf)
1655 struct fclk_data *pdata = dev_get_drvdata(dev);
1657 return scnprintf(buf, PAGE_SIZE, "%lu\n", clk_get_rate(pdata->clk));
1660 static ssize_t fclk_set_rate_store(struct device *dev,
1661 struct device_attribute *attr, const char *buf, size_t count)
1665 struct fclk_data *pdata = dev_get_drvdata(dev);
1667 ret = kstrtoul(buf, 0, &rate);
1671 rate = clk_round_rate(pdata->clk, rate);
1672 ret = clk_set_rate(pdata->clk, rate);
1674 return ret ? ret : count;
1677 static DEVICE_ATTR(set_rate, 0644, fclk_set_rate_show, fclk_set_rate_store);
1679 static ssize_t fclk_round_rate_show(struct device *dev,
1680 struct device_attribute *attr, char *buf)
1682 struct fclk_data *pdata = dev_get_drvdata(dev);
1684 return scnprintf(buf, PAGE_SIZE, "%lu => %lu\n", pdata->rate_rnd,
1685 clk_round_rate(pdata->clk, pdata->rate_rnd));
1688 static ssize_t fclk_round_rate_store(struct device *dev,
1689 struct device_attribute *attr, const char *buf, size_t count)
1693 struct fclk_data *pdata = dev_get_drvdata(dev);
1695 ret = kstrtoul(buf, 0, &rate);
1699 pdata->rate_rnd = rate;
1704 static DEVICE_ATTR(round_rate, 0644, fclk_round_rate_show,
1705 fclk_round_rate_store);
1707 static const struct attribute *fclk_ctrl_attrs[] = {
1708 &dev_attr_enable.attr,
1709 &dev_attr_set_rate.attr,
1710 &dev_attr_round_rate.attr,
1714 static const struct attribute_group fclk_ctrl_attr_grp = {
1715 .attrs = (struct attribute **)fclk_ctrl_attrs,
1718 static ssize_t xdevcfg_fclk_export_store(struct device *dev,
1719 struct device_attribute *attr, const char *buf, size_t size)
1722 struct device *subdev;
1723 struct fclk_data *fdata;
1724 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1726 for (i = 0; i < NUMFCLKS; i++) {
1727 if (!strncmp(buf, fclk_name[i], strlen(fclk_name[i])))
1731 if (i < NUMFCLKS && !drvdata->fclk_exported[i]) {
1732 drvdata->fclk_exported[i] = 1;
1733 subdev = device_create(drvdata->fclk_class, dev, MKDEV(0, 0),
1734 NULL, fclk_name[i]);
1736 return PTR_ERR(subdev);
1737 ret = clk_prepare(drvdata->fclk[i]);
1740 fdata = kzalloc(sizeof(*fdata), GFP_KERNEL);
1745 fdata->clk = drvdata->fclk[i];
1746 dev_set_drvdata(subdev, fdata);
1747 ret = sysfs_create_group(&subdev->kobj, &fclk_ctrl_attr_grp);
1759 clk_unprepare(drvdata->fclk[i]);
1764 static ssize_t xdevcfg_fclk_export_show(struct device *dev,
1765 struct device_attribute *attr, char *buf)
1769 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1771 for (i = 0; i < NUMFCLKS; i++) {
1772 if (!drvdata->fclk_exported[i])
1773 count += scnprintf(buf + count, PAGE_SIZE - count,
1774 "%s\n", fclk_name[i]);
1779 static DEVICE_ATTR(fclk_export, 0644, xdevcfg_fclk_export_show,
1780 xdevcfg_fclk_export_store);
1782 static int match_fclk(struct device *dev, const void *data)
1784 struct fclk_data *fdata = dev_get_drvdata(dev);
1786 return fdata->clk == data;
1789 static ssize_t xdevcfg_fclk_unexport_store(struct device *dev,
1790 struct device_attribute *attr, const char *buf, size_t size)
1793 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1795 for (i = 0; i < NUMFCLKS; i++) {
1796 if (!strncmp(buf, fclk_name[i], strlen(fclk_name[i])))
1800 if (i < NUMFCLKS && drvdata->fclk_exported[i]) {
1801 struct fclk_data *fdata;
1802 struct device *subdev;
1804 drvdata->fclk_exported[i] = 0;
1805 subdev = class_find_device(drvdata->fclk_class, NULL,
1806 drvdata->fclk[i], match_fclk);
1807 fdata = dev_get_drvdata(subdev);
1809 clk_disable(fdata->clk);
1810 clk_unprepare(fdata->clk);
1812 device_unregister(subdev);
1821 static ssize_t xdevcfg_fclk_unexport_show(struct device *dev,
1822 struct device_attribute *attr, char *buf)
1826 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1828 for (i = 0; i < NUMFCLKS; i++) {
1829 if (drvdata->fclk_exported[i])
1830 count += scnprintf(buf + count, PAGE_SIZE - count,
1831 "%s\n", fclk_name[i]);
1836 static DEVICE_ATTR(fclk_unexport, 0644, xdevcfg_fclk_unexport_show,
1837 xdevcfg_fclk_unexport_store);
1839 static const struct attribute *fclk_exp_attrs[] = {
1840 &dev_attr_fclk_export.attr,
1841 &dev_attr_fclk_unexport.attr,
1845 static const struct attribute_group fclk_exp_attr_grp = {
1846 .attrs = (struct attribute **)fclk_exp_attrs,
1849 static void xdevcfg_fclk_init(struct device *dev)
1852 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1854 for (i = 0; i < NUMFCLKS; i++) {
1855 drvdata->fclk[i] = clk_get(dev, fclk_name[i]);
1856 if (IS_ERR(drvdata->fclk[i])) {
1857 dev_warn(dev, "fclk not found\n");
1862 drvdata->fclk_class = class_create(THIS_MODULE, "fclk");
1863 if (IS_ERR(drvdata->fclk_class)) {
1864 dev_warn(dev, "failed to create fclk class\n");
1868 if (sysfs_create_group(&dev->kobj, &fclk_exp_attr_grp))
1869 dev_warn(dev, "failed to create sysfs entries\n");
1872 static void xdevcfg_fclk_remove(struct device *dev)
1875 struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
1877 for (i = 0; i < NUMFCLKS; i++) {
1878 if (drvdata->fclk_exported[i]) {
1879 struct fclk_data *fdata;
1880 struct device *subdev;
1882 drvdata->fclk_exported[i] = 0;
1883 subdev = class_find_device(drvdata->fclk_class, NULL,
1884 drvdata->fclk[i], match_fclk);
1885 fdata = dev_get_drvdata(subdev);
1887 clk_disable(fdata->clk);
1888 clk_unprepare(fdata->clk);
1890 device_unregister(subdev);
1896 class_destroy(drvdata->fclk_class);
1897 sysfs_remove_group(&dev->kobj, &fclk_exp_attr_grp);
1902 * xdevcfg_drv_probe - Probe call for the device.
1904 * @pdev: handle to the platform device structure.
1906 * Returns: 0 on success, negative error otherwise.
1908 * It does all the memory allocation and registration for the device.
1910 static int xdevcfg_drv_probe(struct platform_device *pdev)
1912 struct resource *res;
1913 struct xdevcfg_drvdata *drvdata;
1917 struct device_node *np;
1922 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
1926 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1927 drvdata->base_address = devm_ioremap_resource(&pdev->dev, res);
1928 if (IS_ERR(drvdata->base_address))
1929 return PTR_ERR(drvdata->base_address);
1931 drvdata->irq = platform_get_irq(pdev, 0);
1932 retval = devm_request_irq(&pdev->dev, drvdata->irq, &xdevcfg_irq,
1933 0, dev_name(&pdev->dev), drvdata);
1935 dev_err(&pdev->dev, "No IRQ available");
1939 platform_set_drvdata(pdev, drvdata);
1940 spin_lock_init(&drvdata->lock);
1941 mutex_init(&drvdata->sem);
1942 drvdata->is_open = 0;
1943 drvdata->is_partial_bitstream = 0;
1944 drvdata->dma_done = 0;
1945 drvdata->error_status = 0;
1946 dev_info(&pdev->dev, "ioremap %pa to %p\n",
1947 &res->start, drvdata->base_address);
1949 drvdata->clk = devm_clk_get(&pdev->dev, "ref_clk");
1950 if (IS_ERR(drvdata->clk)) {
1951 dev_err(&pdev->dev, "input clock not found\n");
1952 return PTR_ERR(drvdata->clk);
1955 retval = clk_prepare_enable(drvdata->clk);
1957 dev_err(&pdev->dev, "unable to enable clock\n");
1962 * Figure out from the device tree if this is running on the EP107
1963 * emulation platform as it doesn't match the silicon exactly and the
1964 * driver needs to work accordingly.
1966 np = of_get_next_parent(pdev->dev.of_node);
1967 np = of_get_next_parent(np);
1968 prop = of_get_property(np, "compatible", &size);
1971 if ((strcmp((const char *)prop, "xlnx,zynq-ep107")) == 0)
1977 /* Unlock the device */
1978 xdevcfg_writereg(drvdata->base_address + XDCFG_UNLOCK_OFFSET,
1982 * Set the configuration register with the following options
1984 * - Enable the PCAP interface
1985 * - Set the throughput rate for maximum speed
1986 * - Set the CPU in user mode
1988 ctrlreg = xdevcfg_readreg(drvdata->base_address + XDCFG_CTRL_OFFSET);
1989 ctrlreg &= ~XDCFG_CTRL_PCAP_PR_MASK;
1990 ctrlreg |= XDCFG_CTRL_PCFG_PROG_B_MASK | XDCFG_CTRL_PCAP_MODE_MASK;
1991 xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET, ctrlreg);
1993 /* Ensure internal PCAP loopback is disabled */
1994 ctrlreg = xdevcfg_readreg(drvdata->base_address + XDCFG_MCTRL_OFFSET);
1995 xdevcfg_writereg(drvdata->base_address + XDCFG_MCTRL_OFFSET,
1996 (~XDCFG_MCTRL_PCAP_LPBK_MASK &
2000 retval = alloc_chrdev_region(&devt, 0, XDEVCFG_DEVICES, DRIVER_NAME);
2004 drvdata->devt = devt;
2006 cdev_init(&drvdata->cdev, &xdevcfg_fops);
2007 drvdata->cdev.owner = THIS_MODULE;
2008 retval = cdev_add(&drvdata->cdev, devt, 1);
2010 dev_err(&pdev->dev, "cdev_add() failed\n");
2014 drvdata->class = class_create(THIS_MODULE, DRIVER_NAME);
2015 if (IS_ERR(drvdata->class)) {
2016 dev_err(&pdev->dev, "failed to create class\n");
2020 dev = device_create(drvdata->class, &pdev->dev, devt, drvdata,
2023 dev_err(&pdev->dev, "unable to create device\n");
2027 /* create sysfs files for the device */
2028 retval = sysfs_create_group(&(pdev->dev.kobj), &xdevcfg_attr_group);
2030 dev_err(&pdev->dev, "Failed to create sysfs attr group\n");
2031 cdev_del(&drvdata->cdev);
2035 xdevcfg_fclk_init(&pdev->dev);
2037 clk_disable(drvdata->clk);
2039 return 0; /* Success */
2042 device_destroy(drvdata->class, drvdata->devt);
2044 class_destroy(drvdata->class);
2046 /* Unregister char driver */
2047 unregister_chrdev_region(devt, XDEVCFG_DEVICES);
2049 clk_disable_unprepare(drvdata->clk);
2055 * xdevcfg_drv_remove - Remove call for the device.
2057 * @pdev: handle to the platform device structure.
2059 * Returns: 0 or error status.
2061 * Unregister the device after releasing the resources.
2063 static int xdevcfg_drv_remove(struct platform_device *pdev)
2065 struct xdevcfg_drvdata *drvdata;
2067 drvdata = platform_get_drvdata(pdev);
2072 unregister_chrdev_region(drvdata->devt, XDEVCFG_DEVICES);
2074 sysfs_remove_group(&pdev->dev.kobj, &xdevcfg_attr_group);
2076 xdevcfg_fclk_remove(&pdev->dev);
2077 device_destroy(drvdata->class, drvdata->devt);
2078 class_destroy(drvdata->class);
2079 cdev_del(&drvdata->cdev);
2080 clk_unprepare(drvdata->clk);
2082 return 0; /* Success */
2085 static const struct of_device_id xdevcfg_of_match[] = {
2086 { .compatible = "xlnx,zynq-devcfg-1.0", },
2087 { /* end of table */}
2089 MODULE_DEVICE_TABLE(of, xdevcfg_of_match);
2091 /* Driver Structure */
2092 static struct platform_driver xdevcfg_platform_driver = {
2093 .probe = xdevcfg_drv_probe,
2094 .remove = xdevcfg_drv_remove,
2096 .owner = THIS_MODULE,
2097 .name = DRIVER_NAME,
2098 .of_match_table = xdevcfg_of_match,
2102 module_platform_driver(xdevcfg_platform_driver);
2104 MODULE_AUTHOR("Xilinx, Inc");
2105 MODULE_DESCRIPTION("Xilinx Device Config Driver");
2106 MODULE_LICENSE("GPL");