mirror of
https://gitlab.com/etherlab.org/ethercat.git
synced 2026-08-18 17:17:23 +08:00
update ccat driver to v0.13
- add driver for the SRAM function block - add driver for the GPIO function block - add support for multiple CCATs - prepare support for devices without pci
This commit is contained in:
@@ -35,8 +35,10 @@ TOPDIR := $(src)/../..
|
||||
|
||||
ifeq (@ENABLE_CCAT@,1)
|
||||
EC_CCAT_OBJ := \
|
||||
gpio.o \
|
||||
module.o \
|
||||
netdev.o \
|
||||
sram.o \
|
||||
update.o
|
||||
obj-m += ec_ccat.o
|
||||
ec_ccat-objs := $(EC_CCAT_OBJ)
|
||||
|
||||
@@ -29,8 +29,10 @@
|
||||
|
||||
EXTRA_DIST = \
|
||||
Kbuild.in \
|
||||
gpio.h \
|
||||
module.h \
|
||||
netdev.h \
|
||||
sram.h \
|
||||
update.h
|
||||
|
||||
BUILT_SOURCES = \
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
Network Driver for Beckhoff CCAT communication controller
|
||||
Copyright (C) 2014 Beckhoff Automation GmbH
|
||||
Author: Patrick Bruenn <p.bruenn@beckhoff.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/gpio.h>
|
||||
#include <linux/version.h>
|
||||
#include "module.h"
|
||||
|
||||
/**
|
||||
* struct ccat_gpio - CCAT GPIO function
|
||||
* @ioaddr: PCI base address of the CCAT Update function
|
||||
* @info: holds a copy of the CCAT Update function information block (read from PCI config space)
|
||||
*/
|
||||
struct ccat_gpio {
|
||||
struct gpio_chip chip;
|
||||
void __iomem *ioaddr;
|
||||
struct mutex lock;
|
||||
};
|
||||
|
||||
/** TODO implement in LED driver
|
||||
#define TC_RED 0x01
|
||||
#define TC_GREEN 0x02
|
||||
#define TC_BLUE 0x04
|
||||
#define FB1_RED 0x08
|
||||
#define FB1_GREEN 0x10
|
||||
#define FB1_BLUE 0x20
|
||||
#define FB2_RED 0x40
|
||||
#define FB2_GREEN 0x80
|
||||
#define FB2_BLUE 0x100
|
||||
*/
|
||||
|
||||
static int set_bit_in_register(struct mutex *lock, void __iomem * ioaddr,
|
||||
unsigned nr, int val)
|
||||
{
|
||||
volatile unsigned long old;
|
||||
|
||||
mutex_lock(lock);
|
||||
old = ioread32(ioaddr);
|
||||
val ? set_bit(nr, &old) : clear_bit(nr, &old);
|
||||
if (val)
|
||||
set_bit(nr, &old);
|
||||
else
|
||||
clear_bit(nr, &old);
|
||||
iowrite32(old, ioaddr);
|
||||
mutex_unlock(lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ccat_gpio_get_direction(struct gpio_chip *chip, unsigned nr)
|
||||
{
|
||||
struct ccat_gpio *gdev = container_of(chip, struct ccat_gpio, chip);
|
||||
const size_t byte_offset = 4 * (nr / 32) + 0x8;
|
||||
const u32 mask = 1 << (nr % 32);
|
||||
|
||||
return !(mask & ioread32(gdev->ioaddr + byte_offset));
|
||||
}
|
||||
|
||||
static int ccat_gpio_direction_input(struct gpio_chip *chip, unsigned nr)
|
||||
{
|
||||
struct ccat_gpio *gdev = container_of(chip, struct ccat_gpio, chip);
|
||||
|
||||
return set_bit_in_register(&gdev->lock, gdev->ioaddr + 0x8, nr, 0);
|
||||
}
|
||||
|
||||
static int ccat_gpio_direction_output(struct gpio_chip *chip, unsigned nr,
|
||||
int val)
|
||||
{
|
||||
struct ccat_gpio *gdev = container_of(chip, struct ccat_gpio, chip);
|
||||
|
||||
return set_bit_in_register(&gdev->lock, gdev->ioaddr + 0x8, nr, 1);
|
||||
}
|
||||
|
||||
static int ccat_gpio_get(struct gpio_chip *chip, unsigned nr)
|
||||
{
|
||||
struct ccat_gpio *gdev = container_of(chip, struct ccat_gpio, chip);
|
||||
const size_t byte_off = 4 * (nr / 32);
|
||||
const int mask = 1 << (nr % 32);
|
||||
int dir_off;
|
||||
int value;
|
||||
|
||||
/** omit direction changes before value was read */
|
||||
mutex_lock(&gdev->lock);
|
||||
dir_off = 0x10 * ccat_gpio_get_direction(chip, nr);
|
||||
value = !(mask & ioread32(gdev->ioaddr + byte_off + dir_off));
|
||||
mutex_unlock(&gdev->lock);
|
||||
return value;
|
||||
}
|
||||
|
||||
static void ccat_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
|
||||
{
|
||||
struct ccat_gpio *gdev = container_of(chip, struct ccat_gpio, chip);
|
||||
|
||||
set_bit_in_register(&gdev->lock, gdev->ioaddr, nr, val);
|
||||
}
|
||||
|
||||
static const struct gpio_chip ccat_gpio_chip = {
|
||||
.label = KBUILD_MODNAME,
|
||||
.owner = THIS_MODULE,
|
||||
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0))
|
||||
.get_direction = ccat_gpio_get_direction,
|
||||
#endif
|
||||
.direction_input = ccat_gpio_direction_input,
|
||||
.get = ccat_gpio_get,
|
||||
.direction_output = ccat_gpio_direction_output,
|
||||
.set = ccat_gpio_set,
|
||||
.dbg_show = NULL,
|
||||
.base = -1,
|
||||
.can_sleep = false
|
||||
};
|
||||
|
||||
static int ccat_gpio_probe(struct ccat_function *func)
|
||||
{
|
||||
struct ccat_gpio *const gpio = kzalloc(sizeof(*gpio), GFP_KERNEL);
|
||||
int ret;
|
||||
|
||||
if (!gpio)
|
||||
return -ENOMEM;
|
||||
|
||||
gpio->ioaddr = func->ccat->bar_0 + func->info.addr;
|
||||
memcpy(&gpio->chip, &ccat_gpio_chip, sizeof(gpio->chip));
|
||||
gpio->chip.ngpio = func->info.num_gpios;
|
||||
mutex_init(&gpio->lock);
|
||||
|
||||
ret = gpiochip_add(&gpio->chip);
|
||||
if (ret) {
|
||||
kfree(gpio);
|
||||
return ret;
|
||||
}
|
||||
pr_info("registered %s as gpio chip with #%d GPIOs.\n",
|
||||
gpio->chip.label, gpio->chip.ngpio);
|
||||
func->private_data = gpio;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ccat_gpio_remove(struct ccat_function *func)
|
||||
{
|
||||
struct ccat_gpio *const gpio = func->private_data;
|
||||
|
||||
gpiochip_remove(&gpio->chip);
|
||||
};
|
||||
|
||||
struct ccat_driver gpio_driver = {
|
||||
.type = CCATINFO_GPIO,
|
||||
.probe = ccat_gpio_probe,
|
||||
.remove = ccat_gpio_remove,
|
||||
};
|
||||
+266
-166
File diff suppressed because it is too large
Load Diff
+74
-185
@@ -22,140 +22,76 @@
|
||||
#define _CCAT_H_
|
||||
|
||||
#include <linux/cdev.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/hrtimer.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/pci.h>
|
||||
#include "../ecdev.h"
|
||||
|
||||
#define DRV_EXTRAVERSION "-ec"
|
||||
#define DRV_VERSION "0.10" DRV_EXTRAVERSION
|
||||
#define DRV_VERSION "0.13" DRV_EXTRAVERSION
|
||||
#define DRV_DESCRIPTION "Beckhoff CCAT Ethernet/EtherCAT Network Driver"
|
||||
|
||||
#undef pr_fmt
|
||||
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
||||
|
||||
extern struct ccat_driver eth_eim_driver;
|
||||
extern struct ccat_driver eth_dma_driver;
|
||||
extern struct ccat_driver gpio_driver;
|
||||
extern struct ccat_driver sram_driver;
|
||||
extern struct ccat_driver update_driver;
|
||||
|
||||
/**
|
||||
* CCAT function type identifiers (u16)
|
||||
*/
|
||||
enum ccat_info_t {
|
||||
CCATINFO_NOTUSED = 0,
|
||||
CCATINFO_ETHERCAT_NODMA = 0x3,
|
||||
CCATINFO_GPIO = 0xd,
|
||||
CCATINFO_EPCS_PROM = 0xf,
|
||||
CCATINFO_ETHERCAT_MASTER_DMA = 0x14,
|
||||
CCATINFO_COPY_BLOCK = 0x17,
|
||||
CCATINFO_MAX
|
||||
CCATINFO_SRAM = 0x16,
|
||||
};
|
||||
|
||||
/**
|
||||
* struct ccat_bar - CCAT PCI Base Address Register(BAR) configuration
|
||||
* @start: start address of this BAR
|
||||
* @end: end address of this BAR
|
||||
* @len: length of this BAR
|
||||
* @flags: flags set on this BAR
|
||||
* @ioaddr: ioremapped address of this bar
|
||||
*/
|
||||
struct ccat_bar {
|
||||
unsigned long start;
|
||||
unsigned long end;
|
||||
unsigned long len;
|
||||
unsigned long flags;
|
||||
struct ccat_cdev {
|
||||
atomic_t in_use;
|
||||
void __iomem *ioaddr;
|
||||
size_t iosize;
|
||||
dev_t dev;
|
||||
struct cdev cdev;
|
||||
struct ccat_class *class;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct ccat_dma - CCAT DMA channel configuration
|
||||
* @phys: device-viewed address(physical) of the associated DMA memory
|
||||
* @virt: CPU-viewed address(virtual) of the associated DMA memory
|
||||
* @size: number of bytes in the associated DMA memory
|
||||
* @channel: CCAT DMA channel number
|
||||
* @dev: valid struct device pointer
|
||||
* struct cdev_buffer
|
||||
* @ccdev: referenced character device
|
||||
* @data: buffer used for write operations
|
||||
* @size: number of bytes written to the data buffer
|
||||
*/
|
||||
struct ccat_dma {
|
||||
dma_addr_t phys;
|
||||
void *virt;
|
||||
struct cdev_buffer {
|
||||
struct ccat_cdev *ccdev;
|
||||
size_t size;
|
||||
size_t channel;
|
||||
struct device *dev;
|
||||
char data[];
|
||||
};
|
||||
|
||||
extern void ccat_dma_free(struct ccat_dma *const dma);
|
||||
extern int ccat_dma_init(struct ccat_dma *const dma, size_t channel,
|
||||
void __iomem * const ioaddr, struct device *const dev);
|
||||
|
||||
/**
|
||||
* struct ccat_eth_frame - Ethernet frame with DMA descriptor header in front
|
||||
* @reservedn: is not used and should always be set to 0
|
||||
* @received: used for reception, is set to 1 by the CCAT when data was written
|
||||
* @length: number of bytes in the frame including the DMA header
|
||||
* @sent: is set to 1 by the CCAT when data was transmitted
|
||||
* @timestamp: a 64 bit EtherCAT timestamp
|
||||
* @data: the bytes of the ethernet frame
|
||||
*/
|
||||
struct ccat_eth_frame {
|
||||
__le32 reserved1;
|
||||
__le32 rx_flags;
|
||||
#define CCAT_FRAME_RECEIVED 0x1
|
||||
__le16 length;
|
||||
__le16 reserved3;
|
||||
__le32 tx_flags;
|
||||
#define CCAT_FRAME_SENT 0x1
|
||||
__le64 timestamp;
|
||||
u8 data[0x800 - 3 * sizeof(u64)];
|
||||
#define CCAT_ETH_FRAME_HEAD_LEN offsetof(struct ccat_eth_frame, data)
|
||||
};
|
||||
|
||||
/**
|
||||
* struct ccat_eth_register - CCAT register addresses in the PCI BAR
|
||||
* @mii: address of the CCAT management interface register
|
||||
* @tx_fifo: address of the CCAT TX DMA fifo register
|
||||
* @rx_fifo: address of the CCAT RX DMA fifo register
|
||||
* @mac: address of the CCAT media access control register
|
||||
* @rx_mem: address of the CCAT register holding the RX DMA address
|
||||
* @tx_mem: address of the CCAT register holding the TX DMA address
|
||||
* @misc: address of a CCAT register holding miscellaneous information
|
||||
*/
|
||||
struct ccat_eth_register {
|
||||
void __iomem *mii;
|
||||
void __iomem *tx_fifo;
|
||||
void __iomem *rx_fifo;
|
||||
void __iomem *mac;
|
||||
void __iomem *rx_mem;
|
||||
void __iomem *tx_mem;
|
||||
void __iomem *misc;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct ccat_eth_dma_fifo - CCAT RX or TX DMA fifo
|
||||
* @add: callback used to add a frame to this fifo
|
||||
* @reg: PCI register address of this DMA fifo
|
||||
* @dma: information about the associated DMA memory
|
||||
*/
|
||||
struct ccat_eth_dma_fifo {
|
||||
void (*add) (struct ccat_eth_dma_fifo *, struct ccat_eth_frame *);
|
||||
void __iomem *reg;
|
||||
const struct ccat_eth_frame *end;
|
||||
struct ccat_eth_frame *next;
|
||||
struct ccat_dma dma;
|
||||
};
|
||||
extern int ccat_cdev_open(struct inode *const i, struct file *const f);
|
||||
extern int ccat_cdev_release(struct inode *const i, struct file *const f);
|
||||
|
||||
/**
|
||||
* struct ccat_device - CCAT device representation
|
||||
* @pdev: pointer to the pci object allocated by the kernel
|
||||
* @ethdev: CCAT Ethernet/EtherCAT Master (with DMA) function, NULL if function is not available or failed to initialize
|
||||
* @update: CCAT Update function, NULL if function is not available or failed to initialize
|
||||
* @bar [0] and [2] holding information about PCI BARs 0 and 2.
|
||||
* @bar_0: holding information about PCI BAR 0
|
||||
* @bar_2: holding information about PCI BAR 2 (optional)
|
||||
* @functions: list of available (driver loaded) FPGA functions
|
||||
*
|
||||
* One instance of a ccat_device should represent a physical CCAT. Since
|
||||
* a CCAT is implemented as FPGA the available functions can vary so
|
||||
* the function object pointers can be NULL.
|
||||
* Extra note: you will recognize that PCI BAR1 is not used and is a
|
||||
* waste of memory, thats true but right now, its very easy to use it
|
||||
* this way. So we might optimize it later.
|
||||
* a CCAT is implemented as FPGA the available functions can vary.
|
||||
*/
|
||||
struct ccat_device {
|
||||
struct pci_dev *pdev;
|
||||
struct ccat_eth_priv *ethdev;
|
||||
struct ccat_update *update;
|
||||
struct ccat_bar bar[3]; //TODO optimize this
|
||||
void *pdev;
|
||||
void __iomem *bar_0;
|
||||
void __iomem *bar_2;
|
||||
struct list_head functions;
|
||||
};
|
||||
|
||||
struct ccat_info_block {
|
||||
@@ -163,106 +99,59 @@ struct ccat_info_block {
|
||||
u16 rev;
|
||||
union {
|
||||
u32 config;
|
||||
u8 num_gpios;
|
||||
struct {
|
||||
u16 tx_size;
|
||||
u16 rx_size;
|
||||
};
|
||||
struct {
|
||||
u8 tx_dma_chan;
|
||||
u8 rx_dma_chan;
|
||||
};
|
||||
struct {
|
||||
u8 sram_width;
|
||||
u8 sram_size;
|
||||
u16 reserved;
|
||||
};
|
||||
};
|
||||
u32 addr;
|
||||
u32 size;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct ccat_eth_priv - CCAT Ethernet/EtherCAT Master function (netdev)
|
||||
* @ccatdev: pointer to the parent struct ccat_device
|
||||
* @netdev: the net_device structure used by the kernel networking stack
|
||||
* @info: holds a copy of the CCAT Ethernet/EtherCAT Master function information block (read from PCI config space)
|
||||
* @reg: register addresses in PCI config space of the Ethernet/EtherCAT Master function
|
||||
* @rx_fifo: DMA fifo used for RX DMA descriptors
|
||||
* @tx_fifo: DMA fifo used for TX DMA descriptors
|
||||
* @poll_timer: interval timer used to poll CCAT for events like link changed, rx done, tx done
|
||||
* @rx_bytes: number of bytes received -> reported with ndo_get_stats64()
|
||||
* @rx_dropped: number of received frames, which were dropped -> reported with ndo_get_stats64()
|
||||
* @tx_bytes: number of bytes send -> reported with ndo_get_stats64()
|
||||
* @tx_dropped: number of frames requested to send, which were dropped -> reported with ndo_get_stats64()
|
||||
*/
|
||||
struct ccat_eth_priv {
|
||||
const struct ccat_device *ccatdev;
|
||||
struct net_device *netdev;
|
||||
struct ccat_function {
|
||||
const struct ccat_driver *drv;
|
||||
struct ccat_device *ccat;
|
||||
struct ccat_info_block info;
|
||||
struct ccat_eth_register reg;
|
||||
struct ccat_eth_dma_fifo rx_fifo;
|
||||
struct ccat_eth_dma_fifo tx_fifo;
|
||||
struct hrtimer poll_timer;
|
||||
atomic64_t rx_bytes;
|
||||
atomic64_t rx_dropped;
|
||||
atomic64_t tx_bytes;
|
||||
atomic64_t tx_dropped;
|
||||
ec_device_t *ecdev;
|
||||
void (*carrier_off) (struct net_device * netdev);
|
||||
bool (*carrier_ok) (const struct net_device * netdev);
|
||||
void (*carrier_on) (struct net_device * netdev);
|
||||
void (*kfree_skb_any) (struct sk_buff * skb);
|
||||
void (*start_queue) (struct net_device * netdev);
|
||||
void (*stop_queue) (struct net_device * netdev);
|
||||
void (*unregister) (struct net_device * netdev);
|
||||
struct list_head list;
|
||||
void *private_data;
|
||||
};
|
||||
|
||||
/**
|
||||
* same as: typedef struct _CCatInfoBlockOffs from CCatDefinitions.h
|
||||
* TODO add some checking facility outside of the linux tree
|
||||
*/
|
||||
struct ccat_mac_infoblock {
|
||||
u32 reserved;
|
||||
u32 mii;
|
||||
u32 tx_fifo;
|
||||
u32 mac;
|
||||
u32 rx_mem;
|
||||
u32 tx_mem;
|
||||
u32 misc;
|
||||
};
|
||||
|
||||
struct ccat_mac_register {
|
||||
/** MAC error register @+0x0 */
|
||||
u8 frame_len_err;
|
||||
u8 rx_err;
|
||||
u8 crc_err;
|
||||
u8 link_lost_err;
|
||||
u32 reserved1;
|
||||
/** Buffer overflow errors @+0x8 */
|
||||
u8 rx_mem_full;
|
||||
u8 reserved2[7];
|
||||
/** MAC frame counter @+0x10 */
|
||||
u32 tx_frames;
|
||||
u32 rx_frames;
|
||||
u64 reserved3;
|
||||
/** MAC fifo level @+0x20 */
|
||||
u8 tx_fifo_level:7;
|
||||
u8 reserved4:1;
|
||||
u8 reserved5[7];
|
||||
/** TX memory full error @+0x28 */
|
||||
u8 tx_mem_full;
|
||||
u8 reserved6[7];
|
||||
u64 reserved8[9];
|
||||
/** Connection @+0x78 */
|
||||
u8 mii_connected;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct ccat_update - CCAT Update function (update)
|
||||
* @ccatdev: pointer to the parent struct ccat_device
|
||||
* @ioaddr: PCI base address of the CCAT Update function
|
||||
* dev: device number for this update function
|
||||
* cdev: character device used for the CCAT Update function
|
||||
* class: pointer to a device class used when registering the CCAT Update device
|
||||
* @info: holds a copy of the CCAT Update function information block (read from PCI config space)
|
||||
*/
|
||||
struct ccat_update {
|
||||
struct kref refcount;
|
||||
void __iomem *ioaddr;
|
||||
struct ccat_class {
|
||||
dev_t dev;
|
||||
struct cdev cdev;
|
||||
struct class *class;
|
||||
struct ccat_info_block info;
|
||||
atomic_t instances;
|
||||
const unsigned count;
|
||||
struct ccat_cdev *devices;
|
||||
const char *name;
|
||||
struct file_operations fops;
|
||||
};
|
||||
|
||||
extern void ccat_cdev_remove(struct ccat_function *func);
|
||||
extern int ccat_cdev_probe(struct ccat_function *func,
|
||||
struct ccat_class *cdev_class, size_t iosize);
|
||||
|
||||
/**
|
||||
* struct ccat_driver - CCAT FPGA function
|
||||
* @probe: add device instance
|
||||
* @remove: remove device instance
|
||||
* @type: type of the FPGA function supported by this driver
|
||||
* @cdev_class: if not NULL that driver supports ccat_class_init()/_exit()
|
||||
*/
|
||||
struct ccat_driver {
|
||||
int (*probe) (struct ccat_function * func);
|
||||
void (*remove) (struct ccat_function * drv);
|
||||
enum ccat_info_t type;
|
||||
struct ccat_class *cdev_class;
|
||||
};
|
||||
|
||||
#endif /* #ifndef _CCAT_H_ */
|
||||
|
||||
+595
-144
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
Network Driver for Beckhoff CCAT communication controller
|
||||
Copyright (C) 2015 Beckhoff Automation GmbH & Co. KG
|
||||
Author: Patrick Bruenn <p.bruenn@beckhoff.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include "module.h"
|
||||
#include <asm/io.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/uaccess.h>
|
||||
|
||||
#define CCAT_SRAM_DEVICES_MAX 4
|
||||
|
||||
static ssize_t __sram_read(struct cdev_buffer *buffer, char __user * buf,
|
||||
size_t len, loff_t * off)
|
||||
{
|
||||
memcpy_fromio(buffer->data, buffer->ccdev->ioaddr + *off, len);
|
||||
if (copy_to_user(buf, buffer->data, len))
|
||||
return -EFAULT;
|
||||
|
||||
*off += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
static ssize_t ccat_sram_read(struct file *const f, char __user * buf,
|
||||
size_t len, loff_t * off)
|
||||
{
|
||||
struct cdev_buffer *buffer = f->private_data;
|
||||
const size_t iosize = buffer->ccdev->iosize;
|
||||
|
||||
if (*off >= iosize) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
len = min(len, (size_t) (iosize - *off));
|
||||
|
||||
return __sram_read(buffer, buf, len, off);
|
||||
}
|
||||
|
||||
static ssize_t ccat_sram_write(struct file *const f, const char __user * buf,
|
||||
size_t len, loff_t * off)
|
||||
{
|
||||
struct cdev_buffer *const buffer = f->private_data;
|
||||
|
||||
if (*off + len > buffer->ccdev->iosize) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (copy_from_user(buffer->data, buf, len)) {
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
memcpy_toio(buffer->ccdev->ioaddr + *off, buffer->data, len);
|
||||
|
||||
*off += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
static struct ccat_cdev dev_table[CCAT_SRAM_DEVICES_MAX];
|
||||
static struct ccat_class cdev_class = {
|
||||
.instances = {0},
|
||||
.count = CCAT_SRAM_DEVICES_MAX,
|
||||
.devices = dev_table,
|
||||
.name = "ccat_sram",
|
||||
.fops = {
|
||||
.owner = THIS_MODULE,
|
||||
.open = ccat_cdev_open,
|
||||
.release = ccat_cdev_release,
|
||||
.read = ccat_sram_read,
|
||||
.write = ccat_sram_write,
|
||||
},
|
||||
};
|
||||
|
||||
static int ccat_sram_probe(struct ccat_function *func)
|
||||
{
|
||||
static const u8 NO_SRAM_CONNECTED = 0;
|
||||
const u8 type = func->info.sram_width & 0x3;
|
||||
const size_t iosize = (1 << func->info.sram_size);
|
||||
|
||||
pr_info("%s: 0x%04x rev: 0x%04x\n", __FUNCTION__, func->info.type,
|
||||
func->info.rev);
|
||||
if (type == NO_SRAM_CONNECTED) {
|
||||
return -ENODEV;
|
||||
}
|
||||
return ccat_cdev_probe(func, &cdev_class, iosize);
|
||||
}
|
||||
|
||||
struct ccat_driver sram_driver = {
|
||||
.type = CCATINFO_SRAM,
|
||||
.probe = ccat_sram_probe,
|
||||
.remove = ccat_cdev_remove,
|
||||
.cdev_class = &cdev_class,
|
||||
};
|
||||
+51
-159
@@ -18,14 +18,13 @@
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include "module.h"
|
||||
#include "update.h"
|
||||
|
||||
#define CCAT_DEVICES_MAX 5
|
||||
#define CCAT_DATA_IN_4 0x038
|
||||
#define CCAT_DATA_IN_N 0x7F0
|
||||
#define CCAT_DATA_OUT_4 0x030
|
||||
@@ -45,18 +44,6 @@
|
||||
#define SWAP_BITS(B) \
|
||||
((((B) * 0x0802LU & 0x22110LU) | ((B) * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16)
|
||||
|
||||
/**
|
||||
* struct update_buffer - keep track of a CCAT FPGA update
|
||||
* @update: pointer to a valid ccat_update object
|
||||
* @data: buffer used for write operations
|
||||
* @size: number of bytes written to the data buffer, if 0 on ccat_update_release() no data will be written to FPGA
|
||||
*/
|
||||
struct update_buffer {
|
||||
struct ccat_update *update;
|
||||
char data[CCAT_FLASH_SIZE];
|
||||
size_t size;
|
||||
};
|
||||
|
||||
/**
|
||||
* wait_until_busy_reset() - wait until the busy flag was reset
|
||||
* @ioaddr: address of the CCAT Update function in PCI config space
|
||||
@@ -243,78 +230,34 @@ static int ccat_write_flash_block(void __iomem * const ioaddr,
|
||||
* ccat_write_flash() - Write a new CCAT configuration to FPGA's flash
|
||||
* @update: a CCAT Update buffer containing the new FPGA configuration
|
||||
*/
|
||||
static void ccat_write_flash(const struct update_buffer *const update)
|
||||
static void ccat_write_flash(const struct cdev_buffer *const buffer)
|
||||
{
|
||||
const char *buf = update->data;
|
||||
const char *buf = buffer->data;
|
||||
u32 off = 0;
|
||||
size_t len = update->size;
|
||||
size_t len = buffer->size;
|
||||
|
||||
while (len > CCAT_WRITE_BLOCK_SIZE) {
|
||||
ccat_write_flash_block(update->update->ioaddr, off,
|
||||
ccat_write_flash_block(buffer->ccdev->ioaddr, off,
|
||||
(u16) CCAT_WRITE_BLOCK_SIZE, buf);
|
||||
off += CCAT_WRITE_BLOCK_SIZE;
|
||||
buf += CCAT_WRITE_BLOCK_SIZE;
|
||||
len -= CCAT_WRITE_BLOCK_SIZE;
|
||||
}
|
||||
ccat_write_flash_block(update->update->ioaddr, off, (u16) len, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* ccat_update_destroy() - Cleanup the CCAT Update function
|
||||
* @ref: pointer to a struct kref embedded into a struct ccat_update, which we intend to destroy
|
||||
*
|
||||
* Retrieves the parent struct ccat_update and destroys it.
|
||||
*/
|
||||
static void ccat_update_destroy(struct kref *ref)
|
||||
{
|
||||
struct ccat_update *update =
|
||||
container_of(ref, struct ccat_update, refcount);
|
||||
|
||||
cdev_del(&update->cdev);
|
||||
device_destroy(update->class, update->dev);
|
||||
class_destroy(update->class);
|
||||
unregister_chrdev_region(update->dev, 1);
|
||||
kfree(update);
|
||||
pr_debug("%s(): done\n", __FUNCTION__);
|
||||
}
|
||||
|
||||
static int ccat_update_open(struct inode *const i, struct file *const f)
|
||||
{
|
||||
struct ccat_update *update =
|
||||
container_of(i->i_cdev, struct ccat_update, cdev);
|
||||
struct update_buffer *buf;
|
||||
|
||||
kref_get(&update->refcount);
|
||||
if (atomic_read(&update->refcount.refcount) > 2) {
|
||||
kref_put(&update->refcount, ccat_update_destroy);
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
|
||||
if (!buf) {
|
||||
kref_put(&update->refcount, ccat_update_destroy);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
buf->update = update;
|
||||
f->private_data = buf;
|
||||
return 0;
|
||||
ccat_write_flash_block(buffer->ccdev->ioaddr, off, (u16) len, buf);
|
||||
}
|
||||
|
||||
static int ccat_update_release(struct inode *const i, struct file *const f)
|
||||
{
|
||||
const struct update_buffer *const buf = f->private_data;
|
||||
struct ccat_update *const update = buf->update;
|
||||
const struct cdev_buffer *const buf = f->private_data;
|
||||
void __iomem *ioaddr = buf->ccdev->ioaddr;
|
||||
|
||||
if (buf->size > 0) {
|
||||
ccat_update_cmd(update->ioaddr, CCAT_WRITE_ENABLE);
|
||||
ccat_update_cmd(update->ioaddr, CCAT_BULK_ERASE);
|
||||
ccat_wait_status_cleared(update->ioaddr);
|
||||
ccat_update_cmd(ioaddr, CCAT_WRITE_ENABLE);
|
||||
ccat_update_cmd(ioaddr, CCAT_BULK_ERASE);
|
||||
ccat_wait_status_cleared(ioaddr);
|
||||
ccat_write_flash(buf);
|
||||
}
|
||||
kfree(f->private_data);
|
||||
kref_put(&update->refcount, ccat_update_destroy);
|
||||
return 0;
|
||||
return ccat_cdev_release(i, f);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -333,18 +276,16 @@ static int ccat_update_release(struct inode *const i, struct file *const f)
|
||||
static ssize_t ccat_update_read(struct file *const f, char __user * buf,
|
||||
size_t len, loff_t * off)
|
||||
{
|
||||
struct update_buffer *update = f->private_data;
|
||||
struct cdev_buffer *buffer = f->private_data;
|
||||
const size_t iosize = buffer->ccdev->iosize;
|
||||
|
||||
if (!buf || !off) {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (*off >= CCAT_FLASH_SIZE) {
|
||||
if (*off >= iosize) {
|
||||
return 0;
|
||||
}
|
||||
if (*off + len >= CCAT_FLASH_SIZE) {
|
||||
len = CCAT_FLASH_SIZE - *off;
|
||||
}
|
||||
return ccat_read_flash(update->update->ioaddr, buf, len, off);
|
||||
|
||||
len = min(len, (size_t) (iosize - *off));
|
||||
|
||||
return ccat_read_flash(buffer->ccdev->ioaddr, buf, len, off);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -355,105 +296,56 @@ static ssize_t ccat_update_read(struct file *const f, char __user * buf,
|
||||
* @off: current offset in the configuration data
|
||||
*
|
||||
* Copies data from user space (possibly a *.rbf) to the CCAT FPGA's
|
||||
* configuration flash to user space.
|
||||
* configuration flash.
|
||||
*
|
||||
* Return: the number of bytes written, or 0 if flash end is reached
|
||||
*/
|
||||
|
||||
static ssize_t ccat_update_write(struct file *const f, const char __user * buf,
|
||||
size_t len, loff_t * off)
|
||||
{
|
||||
struct update_buffer *const update = f->private_data;
|
||||
struct cdev_buffer *const buffer = f->private_data;
|
||||
|
||||
if (*off + len > sizeof(update->data))
|
||||
if (*off + len > buffer->ccdev->iosize) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (copy_from_user(update->data + *off, buf, len)) {
|
||||
if (copy_from_user(buffer->data + *off, buf, len)) {
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
*off += len;
|
||||
update->size = *off;
|
||||
buffer->size = *off;
|
||||
return len;
|
||||
}
|
||||
|
||||
static struct file_operations update_ops = {
|
||||
.owner = THIS_MODULE,
|
||||
.open = ccat_update_open,
|
||||
.release = ccat_update_release,
|
||||
.read = ccat_update_read,
|
||||
.write = ccat_update_write,
|
||||
static struct ccat_cdev dev_table[CCAT_DEVICES_MAX];
|
||||
static struct ccat_class cdev_class = {
|
||||
.count = CCAT_DEVICES_MAX,
|
||||
.devices = dev_table,
|
||||
.name = "ccat_update",
|
||||
.fops = {
|
||||
.owner = THIS_MODULE,
|
||||
.open = ccat_cdev_open,
|
||||
.release = ccat_update_release,
|
||||
.read = ccat_update_read,
|
||||
.write = ccat_update_write,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* ccat_get_prom_id() - Read CCAT PROM ID
|
||||
* @ioaddr: address of the CCAT Update function in PCI config space
|
||||
*
|
||||
* Return: the CCAT FPGA's PROM identifier
|
||||
*/
|
||||
u8 ccat_get_prom_id(void __iomem * const ioaddr)
|
||||
static int ccat_update_probe(struct ccat_function *func)
|
||||
{
|
||||
ccat_update_cmd(ioaddr, CCAT_GET_PROM_ID);
|
||||
return ioread8(ioaddr + 0x38);
|
||||
static const u16 SUPPORTED_REVISION = 0x00;
|
||||
|
||||
if (SUPPORTED_REVISION != func->info.rev) {
|
||||
pr_warn("CCAT Update rev. %d not supported\n", func->info.rev);
|
||||
return -ENODEV;
|
||||
}
|
||||
return ccat_cdev_probe(func, &cdev_class, CCAT_FLASH_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* ccat_update_init() - Initialize the CCAT Update function
|
||||
*/
|
||||
struct ccat_update *ccat_update_init(const struct ccat_device *const ccatdev,
|
||||
void __iomem * const addr)
|
||||
{
|
||||
struct ccat_update *const update = kzalloc(sizeof(*update), GFP_KERNEL);
|
||||
|
||||
if (!update) {
|
||||
return NULL;
|
||||
}
|
||||
kref_init(&update->refcount);
|
||||
update->ioaddr = ccatdev->bar[0].ioaddr + ioread32(addr + 0x8);
|
||||
memcpy_fromio(&update->info, addr, sizeof(update->info));
|
||||
|
||||
if (0x00 != update->info.rev) {
|
||||
pr_warn("CCAT Update rev. %d not supported\n",
|
||||
update->info.rev);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (alloc_chrdev_region(&update->dev, 0, 1, KBUILD_MODNAME)) {
|
||||
pr_warn("alloc_chrdev_region() failed\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
update->class = class_create(THIS_MODULE, "ccat_update");
|
||||
if (NULL == update->class) {
|
||||
pr_warn("Create device class failed\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (NULL ==
|
||||
device_create(update->class, NULL, update->dev, NULL,
|
||||
"ccat_update")) {
|
||||
pr_warn("device_create() failed\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
cdev_init(&update->cdev, &update_ops);
|
||||
update->cdev.owner = THIS_MODULE;
|
||||
update->cdev.ops = &update_ops;
|
||||
if (cdev_add(&update->cdev, update->dev, 1)) {
|
||||
pr_warn("add update device failed\n");
|
||||
goto cleanup;
|
||||
}
|
||||
return update;
|
||||
cleanup:
|
||||
kref_put(&update->refcount, ccat_update_destroy);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* ccat_update_remove() - Prepare the CCAT Update function for removal
|
||||
*/
|
||||
void ccat_update_remove(struct ccat_update *update)
|
||||
{
|
||||
kref_put(&update->refcount, ccat_update_destroy);
|
||||
pr_debug("%s(): done\n", __FUNCTION__);
|
||||
}
|
||||
struct ccat_driver update_driver = {
|
||||
.type = CCATINFO_EPCS_PROM,
|
||||
.probe = ccat_update_probe,
|
||||
.remove = ccat_cdev_remove,
|
||||
.cdev_class = &cdev_class,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user