devices/ccat/sram.c
author Florian Pose <fp@igh.de>
Fri, 08 Sep 2017 13:50:06 +0200
branchstable-1.5
changeset 2684 56587a22d05c
parent 2638 5995331a55fe
permissions -rw-r--r--
Do not build GPIO part of CCAT if GPIO is not configured in kernel.
2636
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     1
/**
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     2
    Network Driver for Beckhoff CCAT communication controller
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     3
    Copyright (C) 2015  Beckhoff Automation GmbH & Co. KG
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     4
    Author: Patrick Bruenn <p.bruenn@beckhoff.com>
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     5
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     6
    This program is free software; you can redistribute it and/or modify
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     7
    it under the terms of the GNU General Public License as published by
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     8
    the Free Software Foundation; either version 2 of the License, or
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
     9
    (at your option) any later version.
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    10
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    11
    This program is distributed in the hope that it will be useful,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    14
    GNU General Public License for more details.
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    15
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    16
    You should have received a copy of the GNU General Public License along
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    17
    with this program; if not, write to the Free Software Foundation, Inc.,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    18
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    19
*/
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    20
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    21
#include "module.h"
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    22
#include <asm/io.h>
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    23
#include <linux/fs.h>
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    24
#include <linux/module.h>
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    25
#include <linux/uaccess.h>
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    26
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    27
#define CCAT_SRAM_DEVICES_MAX 4
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    28
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    29
static ssize_t __sram_read(struct cdev_buffer *buffer, char __user * buf,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    30
			   size_t len, loff_t * off)
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    31
{
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    32
	memcpy_fromio(buffer->data, buffer->ccdev->ioaddr + *off, len);
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    33
	if (copy_to_user(buf, buffer->data, len))
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    34
		return -EFAULT;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    35
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    36
	*off += len;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    37
	return len;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    38
}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    39
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    40
static ssize_t ccat_sram_read(struct file *const f, char __user * buf,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    41
			      size_t len, loff_t * off)
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    42
{
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    43
	struct cdev_buffer *buffer = f->private_data;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    44
	const size_t iosize = buffer->ccdev->iosize;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    45
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    46
	if (*off >= iosize) {
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    47
		return 0;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    48
	}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    49
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    50
	len = min(len, (size_t) (iosize - *off));
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    51
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    52
	return __sram_read(buffer, buf, len, off);
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    53
}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    54
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    55
static ssize_t ccat_sram_write(struct file *const f, const char __user * buf,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    56
			       size_t len, loff_t * off)
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    57
{
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    58
	struct cdev_buffer *const buffer = f->private_data;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    59
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    60
	if (*off + len > buffer->ccdev->iosize) {
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    61
		return 0;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    62
	}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    63
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    64
	if (copy_from_user(buffer->data, buf, len)) {
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    65
		return -EFAULT;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    66
	}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    67
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    68
	memcpy_toio(buffer->ccdev->ioaddr + *off, buffer->data, len);
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    69
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    70
	*off += len;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    71
	return len;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    72
}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    73
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    74
static struct ccat_cdev dev_table[CCAT_SRAM_DEVICES_MAX];
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    75
static struct ccat_class cdev_class = {
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    76
	.instances = {0},
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    77
	.count = CCAT_SRAM_DEVICES_MAX,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    78
	.devices = dev_table,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    79
	.name = "ccat_sram",
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    80
	.fops = {
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    81
		 .owner = THIS_MODULE,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    82
		 .open = ccat_cdev_open,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    83
		 .release = ccat_cdev_release,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    84
		 .read = ccat_sram_read,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    85
		 .write = ccat_sram_write,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    86
		 },
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    87
};
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    88
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    89
static int ccat_sram_probe(struct ccat_function *func)
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    90
{
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    91
	static const u8 NO_SRAM_CONNECTED = 0;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    92
	const u8 type = func->info.sram_width & 0x3;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    93
	const size_t iosize = (1 << func->info.sram_size);
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    94
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    95
	pr_info("%s: 0x%04x rev: 0x%04x\n", __FUNCTION__, func->info.type,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    96
		func->info.rev);
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    97
	if (type == NO_SRAM_CONNECTED) {
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    98
		return -ENODEV;
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
    99
	}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   100
	return ccat_cdev_probe(func, &cdev_class, iosize);
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   101
}
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   102
2638
5995331a55fe update ccat driver to v0.14
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2636
diff changeset
   103
const struct ccat_driver sram_driver = {
2636
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   104
	.type = CCATINFO_SRAM,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   105
	.probe = ccat_sram_probe,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   106
	.remove = ccat_cdev_remove,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   107
	.cdev_class = &cdev_class,
0613017547fe update ccat driver to v0.13
Patrick Bruenn <p.bruenn@beckhoff.com>
parents:
diff changeset
   108
};