Path: blob/master/drivers/char/xilinx_hwicap/xilinx_hwicap.c
26285 views
/*****************************************************************************1*2* Author: Xilinx, Inc.3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License as published by the6* Free Software Foundation; either version 2 of the License, or (at your7* option) any later version.8*9* XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"10* AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND11* SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,12* OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,13* APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION14* THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,15* AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE16* FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY17* WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE18* IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR19* REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF20* INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE.22*23* (c) Copyright 2002 Xilinx Inc., Systems Engineering Group24* (c) Copyright 2004 Xilinx Inc., Systems Engineering Group25* (c) Copyright 2007-2008 Xilinx Inc.26* All rights reserved.27*28* You should have received a copy of the GNU General Public License along29* with this program; if not, write to the Free Software Foundation, Inc.,30* 675 Mass Ave, Cambridge, MA 02139, USA.31*32*****************************************************************************/3334/*35* This is the code behind /dev/icap* -- it allows a user-space36* application to use the Xilinx ICAP subsystem.37*38* The following operations are possible:39*40* open open the port and initialize for access.41* release release port42* write Write a bitstream to the configuration processor.43* read Read a data stream from the configuration processor.44*45* After being opened, the port is initialized and accessed to avoid a46* corrupted first read which may occur with some hardware. The port47* is left in a desynched state, requiring that a synch sequence be48* transmitted before any valid configuration data. A user will have49* exclusive access to the device while it remains open, and the state50* of the ICAP cannot be guaranteed after the device is closed. Note51* that a complete reset of the core and the state of the ICAP cannot52* be performed on many versions of the cores, hence users of this53* device should avoid making inconsistent accesses to the device. In54* particular, accessing the read interface, without first generating55* a write containing a readback packet can leave the ICAP in an56* inaccessible state.57*58* Note that in order to use the read interface, it is first necessary59* to write a request packet to the write interface. i.e., it is not60* possible to simply readback the bitstream (or any configuration61* bits) from a device without specifically requesting them first.62* The code to craft such packets is intended to be part of the63* user-space application code that uses this device. The simplest64* way to use this interface is simply:65*66* cp foo.bit /dev/icap067*68* Note that unless foo.bit is an appropriately constructed partial69* bitstream, this has a high likelihood of overwriting the design70* currently programmed in the FPGA.71*/7273#include <linux/module.h>74#include <linux/kernel.h>75#include <linux/types.h>76#include <linux/ioport.h>77#include <linux/interrupt.h>78#include <linux/fcntl.h>79#include <linux/init.h>80#include <linux/poll.h>81#include <linux/proc_fs.h>82#include <linux/mutex.h>83#include <linux/sysctl.h>84#include <linux/fs.h>85#include <linux/cdev.h>86#include <linux/of.h>87#include <linux/platform_device.h>88#include <linux/property.h>89#include <linux/slab.h>90#include <linux/io.h>91#include <linux/uaccess.h>9293#include "xilinx_hwicap.h"94#include "buffer_icap.h"95#include "fifo_icap.h"9697#define DRIVER_NAME "icap"9899#define HWICAP_REGS (0x10000)100101#define XHWICAP_MAJOR 259102#define XHWICAP_MINOR 0103#define HWICAP_DEVICES 1104105/* An array, which is set to true when the device is registered. */106static DEFINE_MUTEX(hwicap_mutex);107static bool probed_devices[HWICAP_DEVICES];108static struct mutex icap_sem;109110static const struct class icap_class = {111.name = "xilinx_config",112};113114#define UNIMPLEMENTED 0xFFFF115116static const struct config_registers v2_config_registers = {117.CRC = 0,118.FAR = 1,119.FDRI = 2,120.FDRO = 3,121.CMD = 4,122.CTL = 5,123.MASK = 6,124.STAT = 7,125.LOUT = 8,126.COR = 9,127.MFWR = 10,128.FLR = 11,129.KEY = 12,130.CBC = 13,131.IDCODE = 14,132.AXSS = UNIMPLEMENTED,133.C0R_1 = UNIMPLEMENTED,134.CSOB = UNIMPLEMENTED,135.WBSTAR = UNIMPLEMENTED,136.TIMER = UNIMPLEMENTED,137.BOOTSTS = UNIMPLEMENTED,138.CTL_1 = UNIMPLEMENTED,139};140141static const struct config_registers v4_config_registers = {142.CRC = 0,143.FAR = 1,144.FDRI = 2,145.FDRO = 3,146.CMD = 4,147.CTL = 5,148.MASK = 6,149.STAT = 7,150.LOUT = 8,151.COR = 9,152.MFWR = 10,153.FLR = UNIMPLEMENTED,154.KEY = UNIMPLEMENTED,155.CBC = 11,156.IDCODE = 12,157.AXSS = 13,158.C0R_1 = UNIMPLEMENTED,159.CSOB = UNIMPLEMENTED,160.WBSTAR = UNIMPLEMENTED,161.TIMER = UNIMPLEMENTED,162.BOOTSTS = UNIMPLEMENTED,163.CTL_1 = UNIMPLEMENTED,164};165166static const struct config_registers v5_config_registers = {167.CRC = 0,168.FAR = 1,169.FDRI = 2,170.FDRO = 3,171.CMD = 4,172.CTL = 5,173.MASK = 6,174.STAT = 7,175.LOUT = 8,176.COR = 9,177.MFWR = 10,178.FLR = UNIMPLEMENTED,179.KEY = UNIMPLEMENTED,180.CBC = 11,181.IDCODE = 12,182.AXSS = 13,183.C0R_1 = 14,184.CSOB = 15,185.WBSTAR = 16,186.TIMER = 17,187.BOOTSTS = 18,188.CTL_1 = 19,189};190191static const struct config_registers v6_config_registers = {192.CRC = 0,193.FAR = 1,194.FDRI = 2,195.FDRO = 3,196.CMD = 4,197.CTL = 5,198.MASK = 6,199.STAT = 7,200.LOUT = 8,201.COR = 9,202.MFWR = 10,203.FLR = UNIMPLEMENTED,204.KEY = UNIMPLEMENTED,205.CBC = 11,206.IDCODE = 12,207.AXSS = 13,208.C0R_1 = 14,209.CSOB = 15,210.WBSTAR = 16,211.TIMER = 17,212.BOOTSTS = 22,213.CTL_1 = 24,214};215216/**217* hwicap_command_desync - Send a DESYNC command to the ICAP port.218* @drvdata: a pointer to the drvdata.219*220* Returns: '0' on success and failure value on error221*222* This command desynchronizes the ICAP After this command, a223* bitstream containing a NULL packet, followed by a SYNCH packet is224* required before the ICAP will recognize commands.225*/226static int hwicap_command_desync(struct hwicap_drvdata *drvdata)227{228u32 buffer[4];229u32 index = 0;230231/*232* Create the data to be written to the ICAP.233*/234buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;235buffer[index++] = XHI_CMD_DESYNCH;236buffer[index++] = XHI_NOOP_PACKET;237buffer[index++] = XHI_NOOP_PACKET;238239/*240* Write the data to the FIFO and initiate the transfer of data present241* in the FIFO to the ICAP device.242*/243return drvdata->config->set_configuration(drvdata,244&buffer[0], index);245}246247/**248* hwicap_get_configuration_register - Query a configuration register.249* @drvdata: a pointer to the drvdata.250* @reg: a constant which represents the configuration251* register value to be returned.252* Examples: XHI_IDCODE, XHI_FLR.253* @reg_data: returns the value of the register.254*255* Returns: '0' on success and failure value on error256*257* Sends a query packet to the ICAP and then receives the response.258* The icap is left in Synched state.259*/260static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,261u32 reg, u32 *reg_data)262{263int status;264u32 buffer[6];265u32 index = 0;266267/*268* Create the data to be written to the ICAP.269*/270buffer[index++] = XHI_DUMMY_PACKET;271buffer[index++] = XHI_NOOP_PACKET;272buffer[index++] = XHI_SYNC_PACKET;273buffer[index++] = XHI_NOOP_PACKET;274buffer[index++] = XHI_NOOP_PACKET;275276/*277* Write the data to the FIFO and initiate the transfer of data present278* in the FIFO to the ICAP device.279*/280status = drvdata->config->set_configuration(drvdata,281&buffer[0], index);282if (status)283return status;284285/* If the syncword was not found, then we need to start over. */286status = drvdata->config->get_status(drvdata);287if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK)288return -EIO;289290index = 0;291buffer[index++] = hwicap_type_1_read(reg) | 1;292buffer[index++] = XHI_NOOP_PACKET;293buffer[index++] = XHI_NOOP_PACKET;294295/*296* Write the data to the FIFO and initiate the transfer of data present297* in the FIFO to the ICAP device.298*/299status = drvdata->config->set_configuration(drvdata,300&buffer[0], index);301if (status)302return status;303304/*305* Read the configuration register306*/307status = drvdata->config->get_configuration(drvdata, reg_data, 1);308if (status)309return status;310311return 0;312}313314static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)315{316int status;317u32 idcode;318319dev_dbg(drvdata->dev, "initializing\n");320321/* Abort any current transaction, to make sure we have the322* ICAP in a good state.323*/324dev_dbg(drvdata->dev, "Reset...\n");325drvdata->config->reset(drvdata);326327dev_dbg(drvdata->dev, "Desync...\n");328status = hwicap_command_desync(drvdata);329if (status)330return status;331332/* Attempt to read the IDCODE from ICAP. This333* may not be returned correctly, due to the design of the334* hardware.335*/336dev_dbg(drvdata->dev, "Reading IDCODE...\n");337status = hwicap_get_configuration_register(338drvdata, drvdata->config_regs->IDCODE, &idcode);339dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);340if (status)341return status;342343dev_dbg(drvdata->dev, "Desync...\n");344status = hwicap_command_desync(drvdata);345if (status)346return status;347348return 0;349}350351static ssize_t352hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)353{354struct hwicap_drvdata *drvdata = file->private_data;355ssize_t bytes_to_read = 0;356u32 *kbuf;357u32 words;358u32 bytes_remaining;359int status;360361status = mutex_lock_interruptible(&drvdata->sem);362if (status)363return status;364365if (drvdata->read_buffer_in_use) {366/* If there are leftover bytes in the buffer, just */367/* return them and don't try to read more from the */368/* ICAP device. */369bytes_to_read =370(count < drvdata->read_buffer_in_use) ? count :371drvdata->read_buffer_in_use;372373/* Return the data currently in the read buffer. */374if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {375status = -EFAULT;376goto error;377}378drvdata->read_buffer_in_use -= bytes_to_read;379memmove(drvdata->read_buffer,380drvdata->read_buffer + bytes_to_read,3814 - bytes_to_read);382} else {383/* Get new data from the ICAP, and return what was requested. */384kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);385if (!kbuf) {386status = -ENOMEM;387goto error;388}389390/* The ICAP device is only able to read complete */391/* words. If a number of bytes that do not correspond */392/* to complete words is requested, then we read enough */393/* words to get the required number of bytes, and then */394/* save the remaining bytes for the next read. */395396/* Determine the number of words to read, rounding up */397/* if necessary. */398words = ((count + 3) >> 2);399bytes_to_read = words << 2;400401if (bytes_to_read > PAGE_SIZE)402bytes_to_read = PAGE_SIZE;403404/* Ensure we only read a complete number of words. */405bytes_remaining = bytes_to_read & 3;406bytes_to_read &= ~3;407words = bytes_to_read >> 2;408409status = drvdata->config->get_configuration(drvdata,410kbuf, words);411412/* If we didn't read correctly, then bail out. */413if (status) {414free_page((unsigned long)kbuf);415goto error;416}417418/* If we fail to return the data to the user, then bail out. */419if (copy_to_user(buf, kbuf, bytes_to_read)) {420free_page((unsigned long)kbuf);421status = -EFAULT;422goto error;423}424memcpy(drvdata->read_buffer,425kbuf,426bytes_remaining);427drvdata->read_buffer_in_use = bytes_remaining;428free_page((unsigned long)kbuf);429}430status = bytes_to_read;431error:432mutex_unlock(&drvdata->sem);433return status;434}435436static ssize_t437hwicap_write(struct file *file, const char __user *buf,438size_t count, loff_t *ppos)439{440struct hwicap_drvdata *drvdata = file->private_data;441ssize_t written = 0;442ssize_t left = count;443u32 *kbuf;444ssize_t len;445ssize_t status;446447status = mutex_lock_interruptible(&drvdata->sem);448if (status)449return status;450451left += drvdata->write_buffer_in_use;452453/* Only write multiples of 4 bytes. */454if (left < 4) {455status = 0;456goto error;457}458459kbuf = (u32 *) __get_free_page(GFP_KERNEL);460if (!kbuf) {461status = -ENOMEM;462goto error;463}464465while (left > 3) {466/* only write multiples of 4 bytes, so there might */467/* be as many as 3 bytes left (at the end). */468len = left;469470if (len > PAGE_SIZE)471len = PAGE_SIZE;472len &= ~3;473474if (drvdata->write_buffer_in_use) {475memcpy(kbuf, drvdata->write_buffer,476drvdata->write_buffer_in_use);477if (copy_from_user(478(((char *)kbuf) + drvdata->write_buffer_in_use),479buf + written,480len - (drvdata->write_buffer_in_use))) {481free_page((unsigned long)kbuf);482status = -EFAULT;483goto error;484}485} else {486if (copy_from_user(kbuf, buf + written, len)) {487free_page((unsigned long)kbuf);488status = -EFAULT;489goto error;490}491}492493status = drvdata->config->set_configuration(drvdata,494kbuf, len >> 2);495496if (status) {497free_page((unsigned long)kbuf);498status = -EFAULT;499goto error;500}501if (drvdata->write_buffer_in_use) {502len -= drvdata->write_buffer_in_use;503left -= drvdata->write_buffer_in_use;504drvdata->write_buffer_in_use = 0;505}506written += len;507left -= len;508}509if ((left > 0) && (left < 4)) {510if (!copy_from_user(drvdata->write_buffer,511buf + written, left)) {512drvdata->write_buffer_in_use = left;513written += left;514left = 0;515}516}517518free_page((unsigned long)kbuf);519status = written;520error:521mutex_unlock(&drvdata->sem);522return status;523}524525static int hwicap_open(struct inode *inode, struct file *file)526{527struct hwicap_drvdata *drvdata;528int status;529530mutex_lock(&hwicap_mutex);531drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);532533status = mutex_lock_interruptible(&drvdata->sem);534if (status)535goto out;536537if (drvdata->is_open) {538status = -EBUSY;539goto error;540}541542status = hwicap_initialize_hwicap(drvdata);543if (status) {544dev_err(drvdata->dev, "Failed to open file");545goto error;546}547548file->private_data = drvdata;549drvdata->write_buffer_in_use = 0;550drvdata->read_buffer_in_use = 0;551drvdata->is_open = 1;552553error:554mutex_unlock(&drvdata->sem);555out:556mutex_unlock(&hwicap_mutex);557return status;558}559560static int hwicap_release(struct inode *inode, struct file *file)561{562struct hwicap_drvdata *drvdata = file->private_data;563int i;564int status = 0;565566mutex_lock(&drvdata->sem);567568if (drvdata->write_buffer_in_use) {569/* Flush write buffer. */570for (i = drvdata->write_buffer_in_use; i < 4; i++)571drvdata->write_buffer[i] = 0;572573status = drvdata->config->set_configuration(drvdata,574(u32 *) drvdata->write_buffer, 1);575if (status)576goto error;577}578579status = hwicap_command_desync(drvdata);580if (status)581goto error;582583error:584drvdata->is_open = 0;585mutex_unlock(&drvdata->sem);586return status;587}588589static const struct file_operations hwicap_fops = {590.owner = THIS_MODULE,591.write = hwicap_write,592.read = hwicap_read,593.open = hwicap_open,594.release = hwicap_release,595.llseek = noop_llseek,596};597598static int hwicap_setup(struct platform_device *pdev, int id,599const struct hwicap_driver_config *config,600const struct config_registers *config_regs)601{602dev_t devt;603struct hwicap_drvdata *drvdata = NULL;604struct device *dev = &pdev->dev;605int retval;606607dev_info(dev, "Xilinx icap port driver\n");608609mutex_lock(&icap_sem);610611if (id < 0) {612for (id = 0; id < HWICAP_DEVICES; id++)613if (!probed_devices[id])614break;615}616if (id < 0 || id >= HWICAP_DEVICES) {617mutex_unlock(&icap_sem);618dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);619return -EINVAL;620}621if (probed_devices[id]) {622mutex_unlock(&icap_sem);623dev_err(dev, "cannot assign to %s%i; it is already in use\n",624DRIVER_NAME, id);625return -EBUSY;626}627628probed_devices[id] = 1;629mutex_unlock(&icap_sem);630631devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);632633drvdata = devm_kzalloc(dev, sizeof(struct hwicap_drvdata), GFP_KERNEL);634if (!drvdata) {635retval = -ENOMEM;636goto failed;637}638dev_set_drvdata(dev, drvdata);639640drvdata->base_address = devm_platform_ioremap_resource(pdev, 0);641if (IS_ERR(drvdata->base_address)) {642retval = PTR_ERR(drvdata->base_address);643goto failed;644}645646drvdata->devt = devt;647drvdata->dev = dev;648drvdata->config = config;649drvdata->config_regs = config_regs;650651mutex_init(&drvdata->sem);652drvdata->is_open = 0;653654cdev_init(&drvdata->cdev, &hwicap_fops);655drvdata->cdev.owner = THIS_MODULE;656retval = cdev_add(&drvdata->cdev, devt, 1);657if (retval) {658dev_err(dev, "cdev_add() failed\n");659goto failed;660}661662device_create(&icap_class, dev, devt, NULL, "%s%d", DRIVER_NAME, id);663return 0; /* success */664665failed:666mutex_lock(&icap_sem);667probed_devices[id] = 0;668mutex_unlock(&icap_sem);669670return retval;671}672673static struct hwicap_driver_config buffer_icap_config = {674.get_configuration = buffer_icap_get_configuration,675.set_configuration = buffer_icap_set_configuration,676.get_status = buffer_icap_get_status,677.reset = buffer_icap_reset,678};679680static struct hwicap_driver_config fifo_icap_config = {681.get_configuration = fifo_icap_get_configuration,682.set_configuration = fifo_icap_set_configuration,683.get_status = fifo_icap_get_status,684.reset = fifo_icap_reset,685};686687static int hwicap_drv_probe(struct platform_device *pdev)688{689const struct config_registers *regs;690const struct hwicap_driver_config *config;691const char *family;692int id = -1;693694config = device_get_match_data(&pdev->dev);695696of_property_read_u32(pdev->dev.of_node, "port-number", &id);697698/* It's most likely that we're using V4, if the family is not699* specified700*/701regs = &v4_config_registers;702if (!of_property_read_string(pdev->dev.of_node, "xlnx,family", &family)) {703if (!strcmp(family, "virtex2p"))704regs = &v2_config_registers;705else if (!strcmp(family, "virtex4"))706regs = &v4_config_registers;707else if (!strcmp(family, "virtex5"))708regs = &v5_config_registers;709else if (!strcmp(family, "virtex6"))710regs = &v6_config_registers;711}712return hwicap_setup(pdev, id, config, regs);713}714715static void hwicap_drv_remove(struct platform_device *pdev)716{717struct device *dev = &pdev->dev;718struct hwicap_drvdata *drvdata;719720drvdata = dev_get_drvdata(dev);721722device_destroy(&icap_class, drvdata->devt);723cdev_del(&drvdata->cdev);724725mutex_lock(&icap_sem);726probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;727mutex_unlock(&icap_sem);728}729730/* Match table for device tree binding */731static const struct of_device_id hwicap_of_match[] = {732{ .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},733{ .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},734{},735};736MODULE_DEVICE_TABLE(of, hwicap_of_match);737738static struct platform_driver hwicap_platform_driver = {739.probe = hwicap_drv_probe,740.remove = hwicap_drv_remove,741.driver = {742.name = DRIVER_NAME,743.of_match_table = hwicap_of_match,744},745};746747static int __init hwicap_module_init(void)748{749dev_t devt;750int retval;751752retval = class_register(&icap_class);753if (retval)754return retval;755mutex_init(&icap_sem);756757devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);758retval = register_chrdev_region(devt,759HWICAP_DEVICES,760DRIVER_NAME);761if (retval < 0)762return retval;763764retval = platform_driver_register(&hwicap_platform_driver);765if (retval)766goto failed;767768return retval;769770failed:771unregister_chrdev_region(devt, HWICAP_DEVICES);772773return retval;774}775776static void __exit hwicap_module_cleanup(void)777{778dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);779780class_unregister(&icap_class);781782platform_driver_unregister(&hwicap_platform_driver);783784unregister_chrdev_region(devt, HWICAP_DEVICES);785}786787module_init(hwicap_module_init);788module_exit(hwicap_module_cleanup);789790MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");791MODULE_DESCRIPTION("Xilinx ICAP Port Driver");792MODULE_LICENSE("GPL");793794795