devices/ccat/update.c
author Patrick Bruenn <p.bruenn@beckhoff.com>
Thu, 05 Jun 2014 17:08:54 +0200
branchstable-1.5
changeset 2571 a4fdad543554
parent 2570 144e11d93e99
child 2573 ad9a35065387
permissions -rw-r--r--
reorder functions in update.c to omit forward declarations
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     1
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     2
    Network Driver for Beckhoff CCAT communication controller
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     3
    Copyright (C) 2014  Beckhoff Automation GmbH
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     4
    Author: Patrick Bruenn <p.bruenn@beckhoff.com>
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     5
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     6
    This program is free software; you can redistribute it and/or modify
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     7
    it under the terms of the GNU General Public License as published by
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     8
    the Free Software Foundation; either version 2 of the License, or
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     9
    (at your option) any later version.
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    10
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    11
    This program is distributed in the hope that it will be useful,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    14
    GNU General Public License for more details.
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    15
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    16
    You should have received a copy of the GNU General Public License along
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    17
    with this program; if not, write to the Free Software Foundation, Inc.,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    18
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    19
*/
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    20
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    21
#include <linux/fs.h>
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    22
#include <linux/kernel.h>
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    23
#include <linux/module.h>
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    24
#include <linux/sched.h>
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    25
#include <linux/uaccess.h>
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    26
#include "module.h"
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    27
#include "update.h"
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    28
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    29
#define CCAT_DATA_IN_4 0x038
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    30
#define CCAT_DATA_IN_N 0x7F0
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    31
#define CCAT_DATA_OUT_4 0x030
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    32
#define CCAT_DATA_BLOCK_SIZE (size_t)((CCAT_DATA_IN_N - CCAT_DATA_IN_4)/8)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    33
#define CCAT_WRITE_BLOCK_SIZE 128
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    34
#define CCAT_FLASH_SIZE (size_t)0xE0000
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    35
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    36
/**     FUNCTION_NAME            CMD,  CLOCKS          */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    37
#define CCAT_BULK_ERASE          0xE3, 8
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    38
#define CCAT_GET_PROM_ID         0xD5, 40
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    39
#define CCAT_READ_FLASH          0xC0, 32
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    40
#define CCAT_READ_STATUS         0xA0, 16
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    41
#define CCAT_WRITE_ENABLE        0x60, 8
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    42
#define CCAT_WRITE_FLASH         0x40, 32
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    43
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    44
/* from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    45
#define SWAP_BITS(B) \
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    46
	((((B) * 0x0802LU & 0x22110LU) | ((B) * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    47
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    48
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    49
 * struct update_buffer - keep track of a CCAT FPGA update
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    50
 * @update: pointer to a valid ccat_update object
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    51
 * @data: buffer used for write operations
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    52
 * @size: number of bytes written to the data buffer, if 0 on ccat_update_release() no data will be written to FPGA
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    53
 */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    54
struct update_buffer {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    55
	struct ccat_update *update;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    56
	char data[CCAT_FLASH_SIZE];
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    57
	size_t size;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    58
};
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    59
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    60
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    61
 * wait_until_busy_reset() - wait until the busy flag was reset
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    62
 * @ioaddr: address of the CCAT Update function in PCI config space
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    63
 */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    64
static inline void wait_until_busy_reset(void __iomem * const ioaddr)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    65
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    66
	wmb();
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    67
	while (ioread8(ioaddr + 1)) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    68
		schedule();
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    69
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    70
}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    71
2571
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    72
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    73
 * __ccat_update_cmd() - Helper to issue a FPGA flash command
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    74
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    75
 * @cmd: the command identifier
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    76
 * @clocks: the number of clocks associated with the specified command
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    77
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    78
 * no write memory barrier is called and the busy flag is not evaluated
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    79
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    80
static inline void __ccat_update_cmd(void __iomem * const ioaddr, u8 cmd,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    81
				     u16 clocks)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    82
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    83
	iowrite8((0xff00 & clocks) >> 8, ioaddr);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    84
	iowrite8(0x00ff & clocks, ioaddr + 0x8);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    85
	iowrite8(cmd, ioaddr + 0x10);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    86
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    87
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    88
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    89
 * ccat_update_cmd() - Helper to issue a FPGA flash command
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    90
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    91
 * @cmd: the command identifier
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    92
 * @clocks: the number of clocks associated with the specified command
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    93
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    94
 * Triggers a full flash command cycle with write memory barrier and
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    95
 * command activate. This call blocks until the busy flag is reset.
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    96
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    97
static inline void ccat_update_cmd(void __iomem * const ioaddr, u8 cmd,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    98
				   u16 clocks)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
    99
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   100
	__ccat_update_cmd(ioaddr, cmd, clocks);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   101
	wmb();
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   102
	iowrite8(0xff, ioaddr + 0x7f8);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   103
	wait_until_busy_reset(ioaddr);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   104
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   105
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   106
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   107
 * ccat_update_cmd_addr() - Helper to issue a FPGA flash command with address parameter
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   108
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   109
 * @cmd: the command identifier
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   110
 * @clocks: the number of clocks associated with the specified command
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   111
 * @addr: 24 bit address associated with the specified command
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   112
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   113
 * Triggers a full flash command cycle with write memory barrier and
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   114
 * command activate. This call blocks until the busy flag is reset.
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   115
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   116
static inline void ccat_update_cmd_addr(void __iomem * const ioaddr,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   117
					u8 cmd, u16 clocks, u32 addr)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   118
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   119
	const u8 addr_0 = SWAP_BITS(addr & 0xff);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   120
	const u8 addr_1 = SWAP_BITS((addr & 0xff00) >> 8);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   121
	const u8 addr_2 = SWAP_BITS((addr & 0xff0000) >> 16);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   122
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   123
	__ccat_update_cmd(ioaddr, cmd, clocks);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   124
	iowrite8(addr_2, ioaddr + 0x18);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   125
	iowrite8(addr_1, ioaddr + 0x20);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   126
	iowrite8(addr_0, ioaddr + 0x28);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   127
	wmb();
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   128
	iowrite8(0xff, ioaddr + 0x7f8);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   129
	wait_until_busy_reset(ioaddr);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   130
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   131
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   132
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   133
 * ccat_get_status() - Read CCAT Update status
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   134
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   135
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   136
 * Return: the current status of the CCAT Update function
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   137
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   138
static u8 ccat_get_status(void __iomem * const ioaddr)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   139
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   140
	ccat_update_cmd(ioaddr, CCAT_READ_STATUS);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   141
	return ioread8(ioaddr + 0x20);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   142
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   143
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   144
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   145
 * ccat_read_flash_block() - Read a block of CCAT configuration data from flash
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   146
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   147
 * @addr: 24 bit address of the block to read
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   148
 * @len: number of bytes to read from this block, len <= CCAT_DATA_BLOCK_SIZE
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   149
 * @buf: output buffer in user space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   150
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   151
 * Copies one block of configuration data from the CCAT FPGA's flash to
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   152
 * the user space buffer.
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   153
 * Note that the size of the FPGA's firmware is not known exactly so it
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   154
 * is very possible that the overall buffer ends with a lot of 0xff.
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   155
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   156
 * Return: the number of bytes copied
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   157
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   158
static int ccat_read_flash_block(void __iomem * const ioaddr,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   159
				 const u32 addr, const u16 len,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   160
				 char __user * const buf)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   161
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   162
	u16 i;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   163
	const u16 clocks = 8 * len;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   164
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   165
	ccat_update_cmd_addr(ioaddr, CCAT_READ_FLASH + clocks, addr);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   166
	for (i = 0; i < len; i++) {
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   167
		put_user(ioread8(ioaddr + CCAT_DATA_IN_4 + 8 * i), buf + i);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   168
	}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   169
	return len;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   170
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   171
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   172
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   173
 * ccat_read_flash() - Read a chunk of CCAT configuration data from flash
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   174
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   175
 * @buf: output buffer in user space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   176
 * @len: number of bytes to read
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   177
 * @off: offset in the configuration data
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   178
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   179
 * Copies multiple blocks of configuration data from the CCAT FPGA's
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   180
 * flash to the user space buffer.
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   181
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   182
 * Return: the number of bytes copied
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   183
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   184
static int ccat_read_flash(void __iomem * const ioaddr, char __user * buf,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   185
			   u32 len, loff_t * off)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   186
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   187
	const loff_t start = *off;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   188
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   189
	while (len > CCAT_DATA_BLOCK_SIZE) {
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   190
		*off +=
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   191
		    ccat_read_flash_block(ioaddr, *off, CCAT_DATA_BLOCK_SIZE,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   192
					  buf);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   193
		buf += CCAT_DATA_BLOCK_SIZE;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   194
		len -= CCAT_DATA_BLOCK_SIZE;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   195
	}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   196
	*off += ccat_read_flash_block(ioaddr, *off, len, buf);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   197
	return *off - start;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   198
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   199
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   200
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   201
 * ccat_wait_status_cleared() - wait until CCAT status is cleared
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   202
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   203
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   204
 * Blocks until bit 7 of the CCAT Update status is reset
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   205
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   206
static void ccat_wait_status_cleared(void __iomem * const ioaddr)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   207
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   208
	u8 status;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   209
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   210
	do {
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   211
		status = ccat_get_status(ioaddr);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   212
	} while (status & (1 << 7));
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   213
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   214
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   215
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   216
 * ccat_write_flash_block() - Write a block of CCAT configuration data to flash
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   217
 * @ioaddr: address of the CCAT Update function in PCI config space
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   218
 * @addr: 24 bit start address in the CCAT FPGA's flash
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   219
 * @len: number of bytes to write in this block, len <= CCAT_WRITE_BLOCK_SIZE
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   220
 * @buf: input buffer
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   221
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   222
 * Copies one block of configuration data to the CCAT FPGA's flash
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   223
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   224
 * Return: the number of bytes copied
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   225
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   226
static int ccat_write_flash_block(void __iomem * const ioaddr,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   227
				  const u32 addr, const u16 len,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   228
				  const char *const buf)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   229
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   230
	const u16 clocks = 8 * len;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   231
	u16 i;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   232
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   233
	ccat_update_cmd(ioaddr, CCAT_WRITE_ENABLE);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   234
	for (i = 0; i < len; i++) {
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   235
		iowrite8(buf[i], ioaddr + CCAT_DATA_OUT_4 + 8 * i);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   236
	}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   237
	ccat_update_cmd_addr(ioaddr, CCAT_WRITE_FLASH + clocks, addr);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   238
	ccat_wait_status_cleared(ioaddr);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   239
	return len;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   240
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   241
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   242
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   243
 * ccat_write_flash() - Write a new CCAT configuration to FPGA's flash
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   244
 * @update: a CCAT Update buffer containing the new FPGA configuration
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   245
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   246
static void ccat_write_flash(const struct update_buffer *const update)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   247
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   248
	const char *buf = update->data;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   249
	u32 off = 0;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   250
	size_t len = update->size;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   251
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   252
	while (len > CCAT_WRITE_BLOCK_SIZE) {
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   253
		ccat_write_flash_block(update->update->ioaddr, off,
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   254
				       (u16) CCAT_WRITE_BLOCK_SIZE, buf);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   255
		off += CCAT_WRITE_BLOCK_SIZE;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   256
		buf += CCAT_WRITE_BLOCK_SIZE;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   257
		len -= CCAT_WRITE_BLOCK_SIZE;
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   258
	}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   259
	ccat_write_flash_block(update->update->ioaddr, off, (u16) len, buf);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   260
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   261
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   262
/**
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   263
 * ccat_update_destroy() - Cleanup the CCAT Update function
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   264
 * @ref: pointer to a struct kref embedded into a struct ccat_update, which we intend to destroy
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   265
 *
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   266
 * Retrieves the parent struct ccat_update and destroys it.
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   267
 */
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   268
static void ccat_update_destroy(struct kref *ref)
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   269
{
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   270
	struct ccat_update *update =
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   271
	    container_of(ref, struct ccat_update, refcount);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   272
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   273
	cdev_del(&update->cdev);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   274
	device_destroy(update->class, update->dev);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   275
	class_destroy(update->class);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   276
	unregister_chrdev_region(update->dev, 1);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   277
	kfree(update);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   278
	pr_debug("%s(): done\n", __FUNCTION__);
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   279
}
a4fdad543554 reorder functions in update.c to omit forward declarations
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2570
diff changeset
   280
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   281
static int ccat_update_open(struct inode *const i, struct file *const f)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   282
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   283
	struct ccat_update *update =
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   284
	    container_of(i->i_cdev, struct ccat_update, cdev);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   285
	struct update_buffer *buf;
2567
d70aad2f131f - use short kernel types, add empty lines after variable declaration to conform better with linux coding style
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2565
diff changeset
   286
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   287
	kref_get(&update->refcount);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   288
	if (atomic_read(&update->refcount.refcount) > 2) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   289
		kref_put(&update->refcount, ccat_update_destroy);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   290
		return -EBUSY;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   291
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   292
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   293
	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   294
	if (!buf) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   295
		kref_put(&update->refcount, ccat_update_destroy);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   296
		return -ENOMEM;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   297
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   298
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   299
	buf->update = update;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   300
	f->private_data = buf;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   301
	return 0;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   302
}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   303
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   304
static int ccat_update_release(struct inode *const i, struct file *const f)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   305
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   306
	const struct update_buffer *const buf = f->private_data;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   307
	struct ccat_update *const update = buf->update;
2567
d70aad2f131f - use short kernel types, add empty lines after variable declaration to conform better with linux coding style
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2565
diff changeset
   308
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   309
	if (buf->size > 0) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   310
		ccat_update_cmd(update->ioaddr, CCAT_WRITE_ENABLE);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   311
		ccat_update_cmd(update->ioaddr, CCAT_BULK_ERASE);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   312
		ccat_wait_status_cleared(update->ioaddr);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   313
		ccat_write_flash(buf);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   314
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   315
	kfree(f->private_data);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   316
	kref_put(&update->refcount, ccat_update_destroy);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   317
	return 0;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   318
}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   319
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   320
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   321
 * ccat_update_read() - Read CCAT configuration data from flash
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   322
 * @f: file handle previously initialized with ccat_update_open()
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   323
 * @buf: buffer in user space provided for our data
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   324
 * @len: length of the user space buffer
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   325
 * @off: current offset of our file operation
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   326
 *
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   327
 * Copies data from the CCAT FPGA's configuration flash to user space.
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   328
 * Note that the size of the FPGA's firmware is not known exactly so it
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   329
 * is very possible that the overall buffer ends with a lot of 0xff.
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   330
 *
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   331
 * Return: the number of bytes written, or 0 if EOF reached
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   332
 */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   333
static ssize_t ccat_update_read(struct file *const f, char __user * buf,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   334
				size_t len, loff_t * off)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   335
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   336
	struct update_buffer *update = f->private_data;
2567
d70aad2f131f - use short kernel types, add empty lines after variable declaration to conform better with linux coding style
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2565
diff changeset
   337
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   338
	if (!buf || !off) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   339
		return -EINVAL;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   340
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   341
	if (*off >= CCAT_FLASH_SIZE) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   342
		return 0;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   343
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   344
	if (*off + len >= CCAT_FLASH_SIZE) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   345
		len = CCAT_FLASH_SIZE - *off;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   346
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   347
	return ccat_read_flash(update->update->ioaddr, buf, len, off);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   348
}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   349
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   350
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   351
 * ccat_update_write() - Write data to the CCAT FPGA's configuration flash
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   352
 * @f: file handle previously initialized with ccat_update_open()
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   353
 * @buf: buffer in user space providing the new configuration data (from *.rbf)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   354
 * @len: length of the user space buffer
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   355
 * @off: current offset in the configuration data
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   356
 *
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   357
 * Copies data from user space (possibly a *.rbf) to the CCAT FPGA's
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   358
 * configuration flash to user space.
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   359
 *
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   360
 * Return: the number of bytes written, or 0 if flash end is reached
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   361
 */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   362
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   363
static ssize_t ccat_update_write(struct file *const f, const char __user * buf,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   364
				 size_t len, loff_t * off)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   365
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   366
	struct update_buffer *const update = f->private_data;
2567
d70aad2f131f - use short kernel types, add empty lines after variable declaration to conform better with linux coding style
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2565
diff changeset
   367
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   368
	if (*off + len > sizeof(update->data))
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   369
		return 0;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   370
2559
a24ff468cb25 Avoided unused return value warning.
Florian Pose <fp@igh-essen.com>
parents: 2553
diff changeset
   371
	if (copy_from_user(update->data + *off, buf, len)) {
a24ff468cb25 Avoided unused return value warning.
Florian Pose <fp@igh-essen.com>
parents: 2553
diff changeset
   372
		return -EFAULT;
a24ff468cb25 Avoided unused return value warning.
Florian Pose <fp@igh-essen.com>
parents: 2553
diff changeset
   373
	}
a24ff468cb25 Avoided unused return value warning.
Florian Pose <fp@igh-essen.com>
parents: 2553
diff changeset
   374
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   375
	*off += len;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   376
	update->size = *off;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   377
	return len;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   378
}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   379
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   380
static struct file_operations update_ops = {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   381
	.owner = THIS_MODULE,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   382
	.open = ccat_update_open,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   383
	.release = ccat_update_release,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   384
	.read = ccat_update_read,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   385
	.write = ccat_update_write,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   386
};
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   387
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   388
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   389
 * ccat_get_prom_id() - Read CCAT PROM ID
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   390
 * @ioaddr: address of the CCAT Update function in PCI config space
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   391
 *
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   392
 * Return: the CCAT FPGA's PROM identifier
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   393
 */
2567
d70aad2f131f - use short kernel types, add empty lines after variable declaration to conform better with linux coding style
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2565
diff changeset
   394
u8 ccat_get_prom_id(void __iomem * const ioaddr)
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   395
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   396
	ccat_update_cmd(ioaddr, CCAT_GET_PROM_ID);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   397
	return ioread8(ioaddr + 0x38);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   398
}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   399
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   400
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   401
 * ccat_update_init() - Initialize the CCAT Update function
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   402
 */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   403
struct ccat_update *ccat_update_init(const struct ccat_device *const ccatdev,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   404
				     void __iomem * const addr)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   405
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   406
	struct ccat_update *const update = kzalloc(sizeof(*update), GFP_KERNEL);
2567
d70aad2f131f - use short kernel types, add empty lines after variable declaration to conform better with linux coding style
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2565
diff changeset
   407
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   408
	if (!update) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   409
		return NULL;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   410
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   411
	kref_init(&update->refcount);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   412
	update->ioaddr = ccatdev->bar[0].ioaddr + ioread32(addr + 0x8);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   413
	memcpy_fromio(&update->info, addr, sizeof(update->info));
2569
720172a7563f make ccat driver more linux compliant
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2567
diff changeset
   414
720172a7563f make ccat driver more linux compliant
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2567
diff changeset
   415
	if (0x00 != update->info.rev) {
720172a7563f make ccat driver more linux compliant
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2567
diff changeset
   416
		pr_warn("CCAT Update rev. %d not supported\n", update->info.rev);
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   417
		goto cleanup;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   418
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   419
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2559
diff changeset
   420
	if (alloc_chrdev_region(&update->dev, 0, 1, KBUILD_MODNAME)) {
2550
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   421
		pr_warn("alloc_chrdev_region() failed\n");
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   422
		goto cleanup;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   423
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   424
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   425
	update->class = class_create(THIS_MODULE, "ccat_update");
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   426
	if (NULL == update->class) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   427
		pr_warn("Create device class failed\n");
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   428
		goto cleanup;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   429
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   430
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   431
	if (NULL ==
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   432
	    device_create(update->class, NULL, update->dev, NULL,
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   433
			  "ccat_update")) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   434
		pr_warn("device_create() failed\n");
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   435
		goto cleanup;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   436
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   437
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   438
	cdev_init(&update->cdev, &update_ops);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   439
	update->cdev.owner = THIS_MODULE;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   440
	update->cdev.ops = &update_ops;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   441
	if (cdev_add(&update->cdev, update->dev, 1)) {
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   442
		pr_warn("add update device failed\n");
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   443
		goto cleanup;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   444
	}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   445
	return update;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   446
cleanup:
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   447
	kref_put(&update->refcount, ccat_update_destroy);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   448
	return NULL;
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   449
}
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   450
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   451
/**
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   452
 * ccat_update_remove() - Prepare the CCAT Update function for removal
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   453
 */
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   454
void ccat_update_remove(struct ccat_update *update)
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   455
{
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   456
	kref_put(&update->refcount, ccat_update_destroy);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   457
	pr_debug("%s(): done\n", __FUNCTION__);
7e25950ea941 replace not working symlinks with real copies from the ccat main repository(git)
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   458
}