Commit 48bae050 authored by Eli Billauer's avatar Eli Billauer Committed by Greg Kroah-Hartman

staging: New driver: Xillybus generic interface for FPGA

This is the driver for Xillybus, which is a general-purpose interface for
data communication with FPGAs (programmable logic). Please refer to the
README included in this patch for a detailed explanation.

It was previously submitted for misc-devices, but it appears like noone's
willing to review the code (which I can understand, given its magnitude).
Hence submitted as a staging driver.
Signed-off-by: default avatarEli Billauer <eli.billauer@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 8079154a
......@@ -144,4 +144,6 @@ source "drivers/staging/lustre/Kconfig"
source "drivers/staging/btmtk_usb/Kconfig"
source "drivers/staging/xillybus/Kconfig"
endif # STAGING
......@@ -64,3 +64,4 @@ obj-$(CONFIG_GOLDFISH) += goldfish/
obj-$(CONFIG_USB_DWC2) += dwc2/
obj-$(CONFIG_LUSTRE_FS) += lustre/
obj-$(CONFIG_USB_BTMTK) += btmtk_usb/
obj-$(CONFIG_XILLYBUS) += xillybus/
#
# Xillybus devices
#
config XILLYBUS
tristate "Xillybus generic FPGA interface"
depends on PCI || (OF_ADDRESS && OF_DEVICE && OF_IRQ)
default n
help
Xillybus is a generic interface for peripherals designed on
programmable logic (FPGA). The driver probes the hardware for
its capabilities, and creates device files accordingly.
If unsure, say N.
if XILLYBUS
config XILLYBUS_PCIE
tristate "Xillybus over PCIe"
depends on XILLYBUS && PCI
default n
help
Set to M if you want Xillybus to use PCI Express for communicating
with the FPGA.
config XILLYBUS_OF
tristate "Xillybus over Device Tree"
depends on XILLYBUS && OF_ADDRESS && OF_DEVICE && OF_IRQ
default n
help
Set to M if you want Xillybus to find its resources from the
Open Firmware Flattened Device Tree. If the target is an embedded
system, say M.
endif # if XILLYBUS
#
# Makefile for Xillybus driver
#
obj-$(CONFIG_XILLYBUS) += xillybus_core.o
obj-$(CONFIG_XILLYBUS_PCIE) += xillybus_pcie.o
obj-$(CONFIG_XILLYBUS_OF) += xillybus_of.o
This diff is collapsed.
TODO:
- have the driver reviewed
Please send any patches and/or comments to Eli Billauer,
<eli.billauer@gmail.com>.
/*
* linux/drivers/misc/xillybus.h
*
* Copyright 2011 Xillybus Ltd, http://xillybus.com
*
* Header file for the Xillybus FPGA/host framework.
*
* This program is free software; you can redistribute it and/or modify
* it under the smems of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
#ifndef __XILLYBUS_H
#define __XILLYBUS_H
#include <linux/list.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/workqueue.h>
char xillyname[] = "xillybus";
struct xilly_endpoint_hardware;
struct xilly_page {
struct list_head node;
unsigned long addr;
unsigned int order;
};
struct xilly_dma {
struct list_head node;
struct pci_dev *pdev;
struct device *dev;
dma_addr_t dma_addr;
size_t size;
int direction;
};
struct xilly_buffer {
void *addr;
dma_addr_t dma_addr;
int end_offset; /* Counting elements, not bytes */
};
struct xilly_cleanup {
struct list_head to_kfree;
struct list_head to_pagefree;
struct list_head to_unmap;
};
struct xilly_idt_handle {
unsigned char *chandesc;
unsigned char *idt;
int entries;
};
/*
* Read-write confusion: wr_* and rd_* notation sticks to FPGA view, so
* wr_* buffers are those consumed by read(), since the FPGA writes to them
* and vice versa.
*/
struct xilly_channel {
struct xilly_endpoint *endpoint;
int chan_num;
int log2_element_size;
int seekable;
struct xilly_buffer **wr_buffers; /* FPGA writes, driver reads! */
int num_wr_buffers;
unsigned int wr_buf_size; /* In bytes */
int wr_fpga_buf_idx;
int wr_host_buf_idx;
int wr_host_buf_pos;
int wr_empty;
int wr_ready; /* Significant only when wr_empty == 1 */
int wr_sleepy;
int wr_eof;
int wr_hangup;
spinlock_t wr_spinlock;
struct mutex wr_mutex;
wait_queue_head_t wr_wait;
wait_queue_head_t wr_ready_wait;
int wr_ref_count;
int wr_synchronous;
int wr_allow_partial;
int wr_exclusive_open;
int wr_supports_nonempty;
struct xilly_buffer **rd_buffers; /* FPGA reads, driver writes! */
int num_rd_buffers;
unsigned int rd_buf_size; /* In bytes */
int rd_fpga_buf_idx;
int rd_host_buf_pos;
int rd_host_buf_idx;
int rd_full;
spinlock_t rd_spinlock;
struct mutex rd_mutex;
wait_queue_head_t rd_wait;
int rd_ref_count;
int rd_allow_partial;
int rd_synchronous;
int rd_exclusive_open;
struct delayed_work rd_workitem;
unsigned char rd_leftovers[4];
};
struct xilly_endpoint {
/*
* One of pdev and dev is always NULL, and the other is a valid
* pointer, depending on the type of device
*/
struct pci_dev *pdev;
struct device *dev;
struct resource res; /* OF devices only */
struct xilly_endpoint_hardware *ephw;
struct list_head ep_list;
int dma_using_dac; /* =1 if 64-bit DMA is used, =0 otherwise. */
u32 *registers;
int fatal_error;
struct mutex register_mutex;
wait_queue_head_t ep_wait;
/* List of memory allocations, to make release easy */
struct xilly_cleanup cleanup;
/* Channels and message handling */
struct cdev cdev;
int major;
int lowest_minor; /* Highest minor = lowest_minor + num_channels - 1 */
int num_channels; /* EXCLUDING message buffer */
struct xilly_channel **channels;
int msg_counter;
int failed_messages;
int idtlen;
u32 *msgbuf_addr;
dma_addr_t msgbuf_dma_addr;
unsigned int msg_buf_size;
};
struct xilly_endpoint_hardware {
struct module *owner;
void (*sync_single_for_cpu)(struct xilly_endpoint *,
dma_addr_t,
size_t,
int);
void (*sync_single_for_device)(struct xilly_endpoint *,
dma_addr_t,
size_t,
int);
dma_addr_t (*map_single)(struct xilly_cleanup *,
struct xilly_endpoint *,
void *,
size_t,
int);
void (*unmap_single)(struct xilly_dma *entry);
};
irqreturn_t xillybus_isr(int irq, void *data);
void xillybus_do_cleanup(struct xilly_cleanup *mem,
struct xilly_endpoint *endpoint);
struct xilly_endpoint *xillybus_init_endpoint(struct pci_dev *pdev,
struct device *dev,
struct xilly_endpoint_hardware
*ephw);
int xillybus_endpoint_discovery(struct xilly_endpoint *endpoint);
void xillybus_endpoint_remove(struct xilly_endpoint *endpoint);
#endif /* __XILLYBUS_H */
This diff is collapsed.
/*
* linux/drivers/misc/xillybus_of.c
*
* Copyright 2011 Xillybus Ltd, http://xillybus.com
*
* Driver for the Xillybus FPGA/host framework using Open Firmware.
*
* This program is free software; you can redistribute it and/or modify
* it under the smems of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include "xillybus.h"
MODULE_DESCRIPTION("Xillybus driver for Open Firmware");
MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
MODULE_VERSION("1.06");
MODULE_ALIAS("xillybus_of");
MODULE_LICENSE("GPL v2");
/* Match table for of_platform binding */
static struct of_device_id xillybus_of_match[] = {
{ .compatible = "xlnx,xillybus-1.00.a", },
{}
};
MODULE_DEVICE_TABLE(of, xillybus_of_match);
static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep,
dma_addr_t dma_handle,
size_t size,
int direction)
{
dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction);
}
static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep,
dma_addr_t dma_handle,
size_t size,
int direction)
{
dma_sync_single_for_device(ep->dev, dma_handle, size, direction);
}
static dma_addr_t xilly_map_single_of(struct xilly_cleanup *mem,
struct xilly_endpoint *ep,
void *ptr,
size_t size,
int direction
)
{
dma_addr_t addr = 0;
struct xilly_dma *this;
this = kmalloc(sizeof(struct xilly_dma), GFP_KERNEL);
if (!this)
return 0;
addr = dma_map_single(ep->dev, ptr, size, direction);
this->direction = direction;
if (dma_mapping_error(ep->dev, addr)) {
kfree(this);
return 0;
}
this->dma_addr = addr;
this->dev = ep->dev;
this->size = size;
list_add_tail(&this->node, &mem->to_unmap);
return addr;
}
void xilly_unmap_single_of(struct xilly_dma *entry)
{
dma_unmap_single(entry->dev,
entry->dma_addr,
entry->size,
entry->direction);
}
static struct xilly_endpoint_hardware of_hw = {
.owner = THIS_MODULE,
.sync_single_for_cpu = xilly_dma_sync_single_for_cpu_of,
.sync_single_for_device = xilly_dma_sync_single_for_device_of,
.map_single = xilly_map_single_of,
.unmap_single = xilly_unmap_single_of
};
static int xilly_drv_probe(struct platform_device *op)
{
struct device *dev = &op->dev;
struct xilly_endpoint *endpoint;
int rc = 0;
int irq;
endpoint = xillybus_init_endpoint(NULL, dev, &of_hw);
if (!endpoint)
return -ENOMEM;
dev_set_drvdata(dev, endpoint);
rc = of_address_to_resource(dev->of_node, 0, &endpoint->res);
if (rc) {
pr_warn("xillybus: Failed to obtain device tree "
"resource\n");
goto failed_request_regions;
}
if (!request_mem_region(endpoint->res.start,
resource_size(&endpoint->res), xillyname)) {
pr_err("xillybus: request_mem_region failed. Aborting.\n");
rc = -EBUSY;
goto failed_request_regions;
}
endpoint->registers = of_iomap(dev->of_node, 0);
if (!endpoint->registers) {
pr_err("xillybus: Failed to map I/O memory. Aborting.\n");
goto failed_iomap0;
}
irq = irq_of_parse_and_map(dev->of_node, 0);
rc = request_irq(irq, xillybus_isr, 0, xillyname, endpoint);
if (rc) {
pr_err("xillybus: Failed to register IRQ handler. "
"Aborting.\n");
rc = -ENODEV;
goto failed_register_irq;
}
rc = xillybus_endpoint_discovery(endpoint);
if (!rc)
return 0;
free_irq(irq, endpoint);
failed_register_irq:
iounmap(endpoint->registers);
failed_iomap0:
release_mem_region(endpoint->res.start,
resource_size(&endpoint->res));
failed_request_regions:
xillybus_do_cleanup(&endpoint->cleanup, endpoint);
kfree(endpoint);
return rc;
}
static int xilly_drv_remove(struct platform_device *op)
{
struct device *dev = &op->dev;
struct xilly_endpoint *endpoint = dev_get_drvdata(dev);
int irq = irq_of_parse_and_map(dev->of_node, 0);
xillybus_endpoint_remove(endpoint);
free_irq(irq, endpoint);
iounmap(endpoint->registers);
release_mem_region(endpoint->res.start,
resource_size(&endpoint->res));
xillybus_do_cleanup(&endpoint->cleanup, endpoint);
kfree(endpoint);
return 0;
}
static struct platform_driver xillybus_platform_driver = {
.probe = xilly_drv_probe,
.remove = xilly_drv_remove,
.driver = {
.name = xillyname,
.owner = THIS_MODULE,
.of_match_table = xillybus_of_match,
},
};
static int __init xillybus_of_init(void)
{
return platform_driver_register(&xillybus_platform_driver);
}
static void __exit xillybus_of_exit(void)
{
platform_driver_unregister(&xillybus_platform_driver);
}
module_init(xillybus_of_init);
module_exit(xillybus_of_exit);
/*
* linux/drivers/misc/xillybus_pcie.c
*
* Copyright 2011 Xillybus Ltd, http://xillybus.com
*
* Driver for the Xillybus FPGA/host framework using PCI Express.
*
* This program is free software; you can redistribute it and/or modify
* it under the smems of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/pci-aspm.h>
#include <linux/slab.h>
#include "xillybus.h"
MODULE_DESCRIPTION("Xillybus driver for PCIe");
MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
MODULE_VERSION("1.06");
MODULE_ALIAS("xillybus_pcie");
MODULE_LICENSE("GPL v2");
#define PCI_DEVICE_ID_XILLYBUS 0xebeb
#define PCI_VENDOR_ID_ALTERA 0x1172
#define PCI_VENDOR_ID_ACTEL 0x11aa
#define PCI_VENDOR_ID_LATTICE 0x1204
static DEFINE_PCI_DEVICE_TABLE(xillyids) = {
{PCI_DEVICE(PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_XILLYBUS)},
{PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_XILLYBUS)},
{PCI_DEVICE(PCI_VENDOR_ID_ACTEL, PCI_DEVICE_ID_XILLYBUS)},
{PCI_DEVICE(PCI_VENDOR_ID_LATTICE, PCI_DEVICE_ID_XILLYBUS)},
{ /* End: all zeroes */ }
};
static int xilly_pci_direction(int direction)
{
switch (direction) {
case DMA_TO_DEVICE:
return PCI_DMA_TODEVICE;
case DMA_FROM_DEVICE:
return PCI_DMA_FROMDEVICE;
default:
return PCI_DMA_BIDIRECTIONAL;
}
}
static void xilly_dma_sync_single_for_cpu_pci(struct xilly_endpoint *ep,
dma_addr_t dma_handle,
size_t size,
int direction)
{
pci_dma_sync_single_for_cpu(ep->pdev,
dma_handle,
size,
xilly_pci_direction(direction));
}
static void xilly_dma_sync_single_for_device_pci(struct xilly_endpoint *ep,
dma_addr_t dma_handle,
size_t size,
int direction)
{
pci_dma_sync_single_for_device(ep->pdev,
dma_handle,
size,
xilly_pci_direction(direction));
}
/*
* Map either through the PCI DMA mapper or the non_PCI one. Behind the
* scenes exactly the same functions are called with the same parameters,
* but that can change.
*/
static dma_addr_t xilly_map_single_pci(struct xilly_cleanup *mem,
struct xilly_endpoint *ep,
void *ptr,
size_t size,
int direction
)
{
dma_addr_t addr = 0;
struct xilly_dma *this;
int pci_direction;
this = kmalloc(sizeof(struct xilly_dma), GFP_KERNEL);
if (!this)
return 0;
pci_direction = xilly_pci_direction(direction);
addr = pci_map_single(ep->pdev, ptr, size, pci_direction);
this->direction = pci_direction;
if (pci_dma_mapping_error(ep->pdev, addr)) {
kfree(this);
return 0;
}
this->dma_addr = addr;
this->pdev = ep->pdev;
this->size = size;
list_add_tail(&this->node, &mem->to_unmap);
return addr;
}
void xilly_unmap_single_pci(struct xilly_dma *entry)
{
pci_unmap_single(entry->pdev,
entry->dma_addr,
entry->size,
entry->direction);
}
static struct xilly_endpoint_hardware pci_hw = {
.owner = THIS_MODULE,
.sync_single_for_cpu = xilly_dma_sync_single_for_cpu_pci,
.sync_single_for_device = xilly_dma_sync_single_for_device_pci,
.map_single = xilly_map_single_pci,
.unmap_single = xilly_unmap_single_pci
};
static int xilly_probe(struct pci_dev *pdev,
const struct pci_device_id *ent)
{
struct xilly_endpoint *endpoint;
int rc = 0;
endpoint = xillybus_init_endpoint(pdev, NULL, &pci_hw);
if (!endpoint)
return -ENOMEM;
pci_set_drvdata(pdev, endpoint);
rc = pci_enable_device(pdev);
/* L0s has caused packet drops. No power saving, thank you. */
pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
if (rc) {
pr_err("xillybus: pci_enable_device() failed. "
"Aborting.\n");
goto no_enable;
}
if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
pr_err("xillybus: Incorrect BAR configuration. "
"Aborting.\n");
rc = -ENODEV;
goto bad_bar;
}
rc = pci_request_regions(pdev, xillyname);
if (rc) {
pr_err("xillybus: pci_request_regions() failed. "
"Aborting.\n");
goto failed_request_regions;
}
endpoint->registers = pci_iomap(pdev, 0, 128);
if (!endpoint->registers) {
pr_err("xillybus: Failed to map BAR 0. Aborting.\n");
goto failed_iomap0;
}
pci_set_master(pdev);
/* Set up a single MSI interrupt */
if (pci_enable_msi(pdev)) {
pr_err("xillybus: Failed to enable MSI interrupts. "
"Aborting.\n");
rc = -ENODEV;
goto failed_enable_msi;
}
rc = request_irq(pdev->irq, xillybus_isr, 0, xillyname, endpoint);
if (rc) {
pr_err("xillybus: Failed to register MSI handler. "
"Aborting.\n");
rc = -ENODEV;
goto failed_register_msi;
}
/*
* In theory, an attempt to set the DMA mask to 64 and dma_using_dac=1
* is the right thing. But some unclever PCIe drivers report it's OK
* when the hardware drops those 64-bit PCIe packets. So trust
* nobody and use 32 bits DMA addressing in any case.
*/
if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
endpoint->dma_using_dac = 0;
else {
pr_err("xillybus: Failed to set DMA mask. "
"Aborting.\n");
rc = -ENODEV;
goto failed_dmamask;
}
rc = xillybus_endpoint_discovery(endpoint);
if (!rc)
return 0;
failed_dmamask:
free_irq(pdev->irq, endpoint);
failed_register_msi:
pci_disable_msi(pdev);
failed_enable_msi:
/* pci_clear_master(pdev); Nobody else seems to do this */
pci_iounmap(pdev, endpoint->registers);
failed_iomap0:
pci_release_regions(pdev);
failed_request_regions:
bad_bar:
pci_disable_device(pdev);
no_enable:
xillybus_do_cleanup(&endpoint->cleanup, endpoint);
kfree(endpoint);
return rc;
}
static void xilly_remove(struct pci_dev *pdev)
{
struct xilly_endpoint *endpoint = pci_get_drvdata(pdev);
xillybus_endpoint_remove(endpoint);
free_irq(pdev->irq, endpoint);
pci_disable_msi(pdev);
pci_iounmap(pdev, endpoint->registers);
pci_release_regions(pdev);
pci_disable_device(pdev);
xillybus_do_cleanup(&endpoint->cleanup, endpoint);
kfree(endpoint);
}
MODULE_DEVICE_TABLE(pci, xillyids);
static struct pci_driver xillybus_driver = {
.name = xillyname,
.id_table = xillyids,
.probe = xilly_probe,
.remove = xilly_remove,
};
module_pci_driver(xillybus_driver);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment