linux/drivers/misc/xilinx_trafgen.c
<<
>>
Prefs
   1/*
   2 * Xilinx AXI Traffic Generator
   3 *
   4 * Copyright (C) 2013 - 2014 Xilinx, Inc.
   5 *
   6 * Description:
   7 * This driver is developed for AXI Traffic Generator IP, which is
   8 * designed to generate AXI4 traffic which can be used to stress
   9 * different modules/interconnect connected in the system. Different
  10 * configurable options which are provided through sysfs entries
  11 * allow the user to generate a wide variety of traffic based on
  12 * their requirements.
  13 *
  14 * This program is free software: you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License as published by
  16 * the Free Software Foundation, either version 2 of the License, or
  17 * (at your option) any later version.
  18 *
  19 * This program is distributed in the hope that it will be useful,
  20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22 * GNU General Public License for more details.
  23 *
  24 * You should have received a copy of the GNU General Public License
  25 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26 */
  27
  28#include <linux/dma-mapping.h>
  29#include <linux/init.h>
  30#include <linux/interrupt.h>
  31#include <linux/io.h>
  32#include <linux/module.h>
  33#include <linux/of_platform.h>
  34#include <linux/of_address.h>
  35#include <linux/of_irq.h>
  36#include <linux/platform_device.h>
  37#include <linux/slab.h>
  38
  39/* Hw specific definitions */
  40
  41/* Internal RAM Offsets */
  42#define XTG_PARAM_RAM_OFFSET       0x1000  /* Parameter RAM offset */
  43#define XTG_COMMAND_RAM_OFFSET     0x8000  /* Command RAM offset */
  44#define XTG_COMMAND_RAM_MSB_OFFSET 0xa000       /**< Command RAM MSB Offset */
  45#define XTG_MASTER_RAM_INIT_OFFSET 0x10000 /* Master RAM initial offset(v1.0) */
  46#define XTG_MASTER_RAM_OFFSET      0xc000  /* Master RAM offset */
  47
  48/* Register Offsets */
  49#define XTG_MCNTL_OFFSET        0x00    /* Master control */
  50#define XTG_SCNTL_OFFSET        0x04    /* Slave control */
  51#define XTG_ERR_STS_OFFSET      0x08    /* Error status  */
  52#define XTG_ERR_EN_OFFSET       0x0C    /* Error enable */
  53#define XTG_MSTERR_INTR_OFFSET  0x10    /* Master error interrupt enable */
  54#define XTG_CFG_STS_OFFSET      0x14    /* Config status */
  55#define XTG_STREAM_CNTL_OFFSET  0x30    /* Streaming Control */
  56#define XTG_STREAM_TL_OFFSET    0x38    /* Streaming Transfer Length */
  57#define XTG_STATIC_CNTL_OFFSET  0x60    /* Static Control */
  58#define XTG_STATIC_LEN_OFFSET   0x64    /* Static Length */
  59
  60/* Register Bitmasks/shifts */
  61
  62/* Master logic enable */
  63#define XTG_MCNTL_MSTEN_MASK            0x00100000
  64/* Slave error interrupt enable */
  65#define XTG_SCNTL_ERREN_MASK            0x00008000
  66/* Master complete interrupt enable */
  67#define XTG_ERR_EN_MSTIRQEN_MASK        0x80000000
  68/* Master error interrupt enable */
  69#define XTG_MSTERR_INTR_MINTREN_MASK    0x00008000
  70/* Master complete done status */
  71#define XTG_ERR_STS_MSTDONE_MASK        0x80000000
  72/* Error mask for error status/enable registers */
  73#define XTG_ERR_ALL_ERRS_MASK           0x001F0003
  74/* Core Revision shift */
  75#define XTG_MCNTL_REV_SHIFT             24
  76
  77/* Axi Traffic Generator Command RAM Entry field mask/shifts */
  78
  79/* Command RAM entry masks */
  80#define XTG_LEN_MASK            0xFF            /* Driven to a*_len line  */
  81#define XTG_LOCK_MASK           0x1             /* Driven to a*_lock line */
  82#define XTG_BURST_MASK          0x3             /* Driven to a*_burst line */
  83#define XTG_SIZE_MASK           0x7             /* Driven to a*_size line */
  84#define XTG_ID_MASK             0x1F            /* Driven to a*_id line */
  85#define XTG_PROT_MASK           0x7             /* Driven to a*_prot line */
  86#define XTG_LAST_ADDR_MASK      0x7             /* Last address */
  87#define XTG_VALID_CMD_MASK      0x1             /* Valid Command */
  88#define XTG_MSTRAM_INDEX_MASK   0x1FFF          /* Master RAM Index */
  89#define XTG_OTHER_DEPEND_MASK   0x1FF           /* Other depend Command no */
  90#define XTG_MY_DEPEND_MASK      0x1FF           /* My depend command no */
  91#define XTG_QOS_MASK            0xF             /* Driven to a*_qos line */
  92#define XTG_USER_MASK           0xFF            /* Driven to a*_user line */
  93#define XTG_CACHE_MASK          0xF             /* Driven to a*_cache line */
  94#define XTG_EXPECTED_RESP_MASK  0x7             /* Expected response */
  95
  96/* Command RAM entry shift values */
  97#define XTG_LEN_SHIFT           0               /* Driven to a*_len line  */
  98#define XTG_LOCK_SHIFT          8               /* Driven to a*_lock line */
  99#define XTG_BURST_SHIFT         10              /* Driven to a*_burst line */
 100#define XTG_SIZE_SHIFT          12              /* Driven to a*_size line */
 101#define XTG_ID_SHIFT            15              /* Driven to a*_id line */
 102#define XTG_PROT_SHIFT          21              /* Driven to a*_prot line */
 103#define XTG_LAST_ADDR_SHIFT     28              /* Last address */
 104#define XTG_VALID_CMD_SHIFT     31              /* Valid Command */
 105#define XTG_MSTRAM_INDEX_SHIFT  0               /* Master RAM Index */
 106#define XTG_OTHER_DEPEND_SHIFT  13              /* Other depend cmd num */
 107#define XTG_MY_DEPEND_SHIFT     22              /* My depend cmd num */
 108#define XTG_QOS_SHIFT           16              /* Driven to a*_qos line */
 109#define XTG_USER_SHIFT          5               /* Driven to a*_user line */
 110#define XTG_CACHE_SHIFT         4               /* Driven to a*_cache line */
 111#define XTG_EXPECTED_RESP_SHIFT 0               /* Expected response */
 112
 113/* Axi Traffic Generator Parameter RAM Entry field mask/shifts */
 114
 115/* Parameter RAM Entry field shift values */
 116#define XTG_PARAM_ADDRMODE_SHIFT        24      /* Address mode */
 117#define XTG_PARAM_INTERVALMODE_SHIFT    26      /* Interval mode */
 118#define XTG_PARAM_IDMODE_SHIFT          28      /* Id mode */
 119#define XTG_PARAM_OP_SHIFT              29      /* Opcode */
 120
 121/* PARAM RAM Opcode shift values */
 122#define XTG_PARAM_COUNT_SHIFT           0       /* Repeat/Delay count */
 123#define XTG_PARAM_DELAYRANGE_SHIFT      0       /* Delay range */
 124#define XTG_PARAM_DELAY_SHIFT           8       /* FIXED RPT delay count */
 125#define XTG_PARAM_ADDRRANGE_SHIFT       20      /* Address range */
 126
 127/* Parameter RAM Entry field mask values */
 128#define XTG_PARAM_ADDRMODE_MASK         0x3     /* Address mode */
 129#define XTG_PARAM_INTERVALMODE_MASK     0x3     /* Interval mode */
 130#define XTG_PARAM_IDMODE_MASK           0x1     /* Id mode */
 131#define XTG_PARAM_OP_MASK               0x7     /* Opcode */
 132
 133/* PARAM RAM Opcode mask values */
 134#define XTG_PARAM_COUNT_MASK            0xFFFFFF/* Repeat/Delay count */
 135#define XTG_PARAM_DELAYRANGE_MASK       0xFF    /* Delay range */
 136#define XTG_PARAM_DELAY_MASK            0xFFF   /* FIXED RPT delay count */
 137#define XTG_PARAM_ADDRRANGE_MASK        0xF     /* Address range */
 138
 139/* PARAM RAM Opcode values */
 140#define XTG_PARAM_OP_NOP                0x0     /* NOP mode */
 141#define XTG_PARAM_OP_RPT                0x1     /* Repeat mode */
 142#define XTG_PARAM_OP_DELAY              0x2     /* Delay mode */
 143#define XTG_PARAM_OP_FIXEDRPT           0x3     /* Fixed repeat delay */
 144
 145/* Axi Traffic Generator Static Mode masks */
 146#define XTG_STATIC_CNTL_TD_MASK         0x00000002      /* Transfer Done Mask */
 147#define XTG_STATIC_CNTL_STEN_MASK       0x00000001      /* Static Enable Mask */
 148#define XTG_STATIC_CNTL_RESET_MASK      0x00000000      /* Static Reset Mask */
 149
 150/* Axi Traffic Generator Stream Mode mask/shifts */
 151#define XTG_STREAM_CNTL_STEN_MASK   0x00000001  /* Stream Enable Mask */
 152#define XTG_STREAM_TL_TCNT_MASK     0xFFFF0000  /* Transfer Count Mask */
 153#define XTG_STREAM_TL_TLEN_MASK     0x0000FFFF  /* Transfer Length Mask */
 154#define XTG_STREAM_TL_TCNT_SHIFT    16          /* Transfer Count Shift */
 155
 156/* Driver Specific Definitions */
 157
 158#define MAX_NUM_ENTRIES 256     /* Number of command entries per region */
 159
 160#define VALID_SIG       0xa5a5a5a5      /* Valid unique identifier */
 161
 162/* Internal RAM Sizes */
 163#define XTG_PRM_RAM_BLOCK_SIZE  0x400   /* PRAM Block size (1KB) */
 164#define XTG_CMD_RAM_BLOCK_SIZE  0x1000  /* CRAM Block size (4KB) */
 165#define XTG_EXTCMD_RAM_BLOCK_SIZE 0x400 /**< Extended CMDRAM Block Size (1KB) */
 166#define XTG_PARAM_RAM_SIZE      0x800   /* Parameter RAM (2KB) */
 167#define XTG_COMMAND_RAM_SIZE    0x2000  /* Command RAM (8KB) */
 168#define XTG_EXTCMD_RAM_SIZE     0x800   /* Command RAM (2KB) */
 169#define XTG_MASTER_RAM_SIZE     0x2000  /* Master RAM (8KB) */
 170
 171/* RAM Access Flags */
 172#define XTG_READ_RAM            0x0     /* Read RAM flag */
 173#define XTG_WRITE_RAM           0x1     /* Write RAM flag */
 174#define XTG_WRITE_RAM_ZERO      0x2     /* Write Zero flag */
 175
 176/* Bytes per entry */
 177#define XTG_CRAM_BYTES_PER_ENTRY        16 /* CRAM bytes per entry */
 178#define XTG_PRAM_BYTES_PER_ENTRY        4  /* PRAM bytes per entry */
 179
 180/* Interrupt Definitions */
 181#define XTG_MASTER_CMP_INTR     0x1     /* Master complete intr flag */
 182#define XTG_MASTER_ERR_INTR     0x2     /* Master error intr flag */
 183#define XTG_SLAVE_ERR_INTR      0x4     /* Slave error intr flag */
 184
 185/*
 186 * Version value of the trafgen core.
 187 * For the initial IP release the version(v1.0) value is 0x47
 188 * From the v2.0 IP and onwards the value starts from  0x20.
 189 * For eg:
 190 * v2.1 -> 0x21
 191 * v2.2 -> 0x22 ... so on.
 192 *
 193 */
 194#define XTG_INIT_VERSION        0x47    /* Trafgen initial version(v1.0) */
 195
 196/* Macro */
 197#define to_xtg_dev_info(n)      ((struct xtg_dev_info *)dev_get_drvdata(n))
 198
 199#define CMD_WDS 0x4     /* No of words in command ram per command */
 200#define EXT_WDS 0x1     /* No of words in extended ram per command */
 201#define MSB_INDEX       0x4
 202/**
 203 * struct xtg_cram - Command RAM structure
 204 * @addr: Address Driven to a*_addr line
 205 * @valid_cmd: Valid Command
 206 * @last_addr: Last address
 207 * @prot: Driven to a*_prot line
 208 * @id: Driven to a*_id line
 209 * @size: Driven to a*_size line
 210 * @burst: Driven to a*_burst line
 211 * @lock: Driven to a*_lock line
 212 * @length: Driven to a*_len line
 213 * @my_dpnd: My Depend command number
 214 * @other_dpnd: Other depend command number
 215 * @mram_idx: Master RAM index
 216 * @qos: Driven to a*_qos line
 217 * @user: Driven to a*_user line
 218 * @cache: Driven to a*_cache line
 219 * @expected_resp: Expected response
 220 * @index: Command Index
 221 * @is_write_block: Write/Read block
 222 * @is_valid_req: Unique signature
 223 *
 224 * FIXME: This structure is shared with the user application and
 225 * hence need to be synchronized. We know these kind of structures
 226 * should not be defined in the driver and this need to be fixed
 227 * if found a proper placeholder (in uapi/).
 228 */
 229struct xtg_cram {
 230        phys_addr_t addr;
 231        u32 valid_cmd;
 232        u32 last_addr;
 233        u32 prot;
 234        u32 id;
 235        u32 size;
 236        u32 burst;
 237        u32 lock;
 238        u32 length;
 239        u32 my_dpnd;
 240        u32 other_dpnd;
 241        u32 mram_idx;
 242        u32 qos;
 243        u32 user;
 244        u32 cache;
 245        u32 expected_resp;
 246        u16 index;
 247        bool is_write_block;
 248        u32 is_valid_req;
 249};
 250
 251/**
 252 * struct xtg_pram - Parameter RAM structure
 253 * @op_cntl0: Control field 0
 254 * @op_cntl1: Control field 1
 255 * @op_cntl2: Control field 2
 256 * @addr_mode: Address mode
 257 * @interval_mode: Interval mode
 258 * @id_mode: Id mode
 259 * @opcode: Opcode
 260 * @index: Command Index
 261 * @is_write_block: Write/Read block
 262 * @is_valid_req: Unique signature
 263 *
 264 * FIXME: This structure is shared with the user application and
 265 * hence need to be synchronized. We know these kind of structures
 266 * should not be defined in the driver and this need to be fixed
 267 * if found a proper placeholder (in uapi/).
 268 */
 269struct xtg_pram {
 270        u32 op_cntl0;
 271        u32 op_cntl1;
 272        u32 op_cntl2;
 273        u32 addr_mode;
 274        u32 interval_mode;
 275        u32 id_mode;
 276        u32 opcode;
 277        u16 index;
 278        bool is_write_block;
 279        u32 is_valid_req;
 280};
 281
 282/**
 283 * struct xtg_dev_info - Global Driver structure
 284 * @regs: Iomapped base address
 285 * @dev: Device structure
 286 * @phys_base_addr: Physical base address
 287 * @last_rd_valid_idx: Last Read Valid Command Index
 288 * @last_wr_valid_idx: Last Write Valid Command Index
 289 * @id: Device instance id
 290 * @xtg_mram_offset: MasterRam offset
 291 */
 292struct xtg_dev_info {
 293        void __iomem *regs;
 294        struct device *dev;
 295        phys_addr_t phys_base_addr;
 296        s16 last_rd_valid_idx;
 297        s16 last_wr_valid_idx;
 298        u32 id;
 299        u32 xtg_mram_offset;
 300};
 301
 302/**
 303 * enum xtg_sysfs_ioctl - Ioctl opcodes
 304 * @XTG_GET_MASTER_CMP_STS: get master complete status
 305 * @XTG_GET_SLV_CTRL_REG: get slave control reg status
 306 * @XTG_GET_ERR_STS: get error status
 307 * @XTG_GET_CFG_STS: get config status
 308 * @XTG_GET_LAST_VALID_INDEX: get last valid index
 309 * @XTG_GET_DEVICE_ID: get device id
 310 * @XTG_GET_RESOURCE: get resource
 311 * @XTG_GET_STATIC_ENABLE: get staic mode traffic genration state
 312 * @XTG_GET_STATIC_BURSTLEN: get static mode burst length
 313 * @XTG_GET_STATIC_TRANSFERDONE: get static transfer done
 314 * @XTG_GET_STREAM_ENABLE : get strean mode traffic genration state
 315 * @XTG_GET_STREAM_TRANSFERLEN: get streaming mode transfer length
 316 * @XTG_GET_STREAM_TRANSFERCNT: get streaming mode transfer count
 317 * @XTG_START_MASTER_LOGIC: start master logic
 318 * @XTG_SET_SLV_CTRL_REG: set slave control
 319 * @XTG_CLEAR_ERRORS: clear errors
 320 * @XTG_ENABLE_ERRORS: enable errors
 321 * @XTG_ENABLE_INTRS: enable interrupts
 322 * @XTG_CLEAR_MRAM: clear master ram
 323 * @XTG_CLEAR_CRAM: clear command ram
 324 * @XTG_CLEAR_PRAM: clear parameter ram
 325 * @XTG_SET_STATIC_ENABLE: enable static mode traffic genration
 326 * @XTG_SET_STATIC_DISABLE: disable static mode traffic genration
 327 * @XTG_SET_STATIC_BURSTLEN: set static mode burst length
 328 * @XTG_SET_STATIC_TRANSFERDONE: set static transfer done
 329 * @XTG_SET_STREAM_ENABLE: enable streaming mode traffic genration
 330 * @XTG_SET_STREAM_DISABLE: disable streaming mode traffic genration
 331 * @XTG_SET_STREAM_TRANSFERLEN: set streaming mode transfer length
 332 * @XTG_SET_STREAM_TRANSFERCNT: set streaming mode transfer count
 333 */
 334enum xtg_sysfs_ioctl_opcode {
 335        XTG_GET_MASTER_CMP_STS,
 336        XTG_GET_SLV_CTRL_REG,
 337        XTG_GET_ERR_STS,
 338        XTG_GET_CFG_STS,
 339        XTG_GET_LAST_VALID_INDEX,
 340        XTG_GET_DEVICE_ID,
 341        XTG_GET_RESOURCE,
 342        XTG_GET_STATIC_ENABLE,
 343        XTG_GET_STATIC_BURSTLEN,
 344        XTG_GET_STATIC_TRANSFERDONE,
 345        XTG_GET_STREAM_ENABLE,
 346        XTG_GET_STREAM_TRANSFERLEN,
 347        XTG_GET_STREAM_TRANSFERCNT,
 348        XTG_START_MASTER_LOGIC,
 349        XTG_SET_SLV_CTRL_REG,
 350        XTG_CLEAR_ERRORS,
 351        XTG_ENABLE_ERRORS,
 352        XTG_ENABLE_INTRS,
 353        XTG_CLEAR_MRAM,
 354        XTG_CLEAR_CRAM,
 355        XTG_CLEAR_PRAM,
 356        XTG_SET_STATIC_ENABLE,
 357        XTG_SET_STATIC_DISABLE,
 358        XTG_SET_STATIC_BURSTLEN,
 359        XTG_SET_STATIC_TRANSFERDONE,
 360        XTG_SET_STREAM_ENABLE,
 361        XTG_SET_STREAM_DISABLE,
 362        XTG_SET_STREAM_TRANSFERLEN,
 363        XTG_SET_STREAM_TRANSFERCNT
 364};
 365
 366/**
 367 * xtg_access_rams - Write/Read Master/Command/Parameter RAM
 368 * @tg: Pointer to xtg_dev_info structure
 369 * @where: Offset from base
 370 * @count: Number of bytes to write/read
 371 * @flags: Read/Write/Write Zero
 372 * @data: Data pointer
 373 */
 374static void xtg_access_rams(struct xtg_dev_info *tg, int where,
 375                                int count, int flags, u32 *data)
 376{
 377        u32 index;
 378
 379        switch (flags) {
 380        case XTG_WRITE_RAM_ZERO:
 381                memset_io(tg->regs + where, 0, count);
 382#ifdef CONFIG_PHYS_ADDR_T_64BIT
 383                writel(0x0, tg->regs + where +
 384                        (XTG_COMMAND_RAM_MSB_OFFSET - XTG_COMMAND_RAM_OFFSET) +
 385                        XTG_EXTCMD_RAM_BLOCK_SIZE - XTG_CMD_RAM_BLOCK_SIZE);
 386#endif
 387                break;
 388        case XTG_WRITE_RAM:
 389                for (index = 0; count > 0; index++, count -= 4)
 390                        writel(data[index], tg->regs + where + index * 4);
 391#ifdef CONFIG_PHYS_ADDR_T_64BIT
 392                writel(data[MSB_INDEX], tg->regs + where +
 393                        (XTG_COMMAND_RAM_MSB_OFFSET - XTG_COMMAND_RAM_OFFSET) +
 394                        XTG_EXTCMD_RAM_BLOCK_SIZE - XTG_CMD_RAM_BLOCK_SIZE);
 395#endif
 396                break;
 397        case XTG_READ_RAM:
 398                for (index = 0; count > 0; index++, count -= 4)
 399                        data[index] = readl(tg->regs + where + index * 4);
 400#ifdef CONFIG_PHYS_ADDR_T_64BIT
 401                data[MSB_INDEX] = readl(tg->regs + where +
 402                        (XTG_COMMAND_RAM_MSB_OFFSET - XTG_COMMAND_RAM_OFFSET));
 403#endif
 404                break;
 405        }
 406}
 407
 408/**
 409 * xtg_prepare_cmd_words - Prepares all four Command RAM words
 410 * @tg: Pointer to xtg_dev_info structure
 411 * @cmdp: Pointer to xtg_cram structure
 412 * @cmd_words: Pointer to Command Words that needs to be prepared
 413 */
 414static void xtg_prepare_cmd_words(struct xtg_dev_info *tg,
 415                                const struct xtg_cram *cmdp, u32 *cmd_words)
 416{
 417        /* Command Word 0 */
 418        cmd_words[0] = lower_32_bits(cmdp->addr);
 419
 420        /* Command Word 4 */
 421#ifdef CONFIG_PHYS_ADDR_T_64BIT
 422        cmd_words[MSB_INDEX] = upper_32_bits(cmdp->addr);
 423#endif
 424
 425        /* Command Word 1 */
 426        cmd_words[1] = 0;
 427        cmd_words[1] |= (cmdp->length & XTG_LEN_MASK) << XTG_LEN_SHIFT;
 428        cmd_words[1] |= (cmdp->lock & XTG_LOCK_MASK) << XTG_LOCK_SHIFT;
 429        cmd_words[1] |= (cmdp->burst & XTG_BURST_MASK) << XTG_BURST_SHIFT;
 430        cmd_words[1] |= (cmdp->size & XTG_SIZE_MASK) << XTG_SIZE_SHIFT;
 431        cmd_words[1] |= (cmdp->id & XTG_ID_MASK) << XTG_ID_SHIFT;
 432        cmd_words[1] |= (cmdp->prot & XTG_PROT_MASK) << XTG_PROT_SHIFT;
 433        cmd_words[1] |= (cmdp->last_addr & XTG_LAST_ADDR_MASK) <<
 434                                        XTG_LAST_ADDR_SHIFT;
 435        cmd_words[1] |= (cmdp->valid_cmd & XTG_VALID_CMD_MASK) <<
 436                                        XTG_VALID_CMD_SHIFT;
 437
 438        /* Command Word 2 */
 439        cmd_words[2] = 0;
 440        cmd_words[2] |= (cmdp->mram_idx & XTG_MSTRAM_INDEX_MASK) <<
 441                                        XTG_MSTRAM_INDEX_SHIFT;
 442        cmd_words[2] |= (cmdp->other_dpnd & XTG_OTHER_DEPEND_MASK) <<
 443                                        XTG_OTHER_DEPEND_SHIFT;
 444        cmd_words[2] |= (cmdp->my_dpnd & XTG_MY_DEPEND_MASK) <<
 445                                        XTG_MY_DEPEND_SHIFT;
 446
 447        /* Command Word 3 */
 448        cmd_words[3] = 0;
 449        cmd_words[3] |= (cmdp->qos & XTG_QOS_MASK) << XTG_QOS_SHIFT;
 450        cmd_words[3] |= (cmdp->user & XTG_USER_MASK) << XTG_USER_SHIFT;
 451        cmd_words[3] |= (cmdp->cache & XTG_CACHE_MASK) << XTG_CACHE_SHIFT;
 452        cmd_words[3] |= (cmdp->expected_resp & XTG_EXPECTED_RESP_MASK) <<
 453                                        XTG_EXPECTED_RESP_SHIFT;
 454}
 455
 456/**
 457 * xtg_prepare_param_words - Prepares Parameter RAM word
 458 * @tg: Pointer to xtg_dev_info structure
 459 * @cmdp: Pointer to xtg_pram structure
 460 * @param_word: Pointer to Param Word that needs to be prepared
 461 */
 462static void xtg_prepare_param_word(struct xtg_dev_info *tg,
 463                        const struct xtg_pram *cmdp, u32 *param_word)
 464{
 465        *param_word = 0;
 466        *param_word |= (cmdp->opcode & XTG_PARAM_OP_MASK) << XTG_PARAM_OP_SHIFT;
 467        *param_word |= (cmdp->addr_mode & XTG_PARAM_ADDRMODE_MASK) <<
 468                                        XTG_PARAM_ADDRMODE_SHIFT;
 469        *param_word |= (cmdp->id_mode & XTG_PARAM_IDMODE_MASK) <<
 470                                        XTG_PARAM_IDMODE_SHIFT;
 471        *param_word |= (cmdp->interval_mode & XTG_PARAM_INTERVALMODE_MASK) <<
 472                                        XTG_PARAM_INTERVALMODE_SHIFT;
 473
 474        switch (cmdp->opcode) {
 475        case XTG_PARAM_OP_RPT:
 476        case XTG_PARAM_OP_DELAY:
 477                *param_word |= (cmdp->op_cntl0 & XTG_PARAM_COUNT_MASK) <<
 478                                        XTG_PARAM_COUNT_SHIFT;
 479                break;
 480
 481        case XTG_PARAM_OP_FIXEDRPT:
 482                *param_word |= (cmdp->op_cntl0 & XTG_PARAM_ADDRRANGE_MASK) <<
 483                                        XTG_PARAM_ADDRRANGE_SHIFT;
 484                *param_word |= (cmdp->op_cntl1 & XTG_PARAM_DELAY_MASK) <<
 485                                        XTG_PARAM_DELAY_SHIFT;
 486                *param_word |= (cmdp->op_cntl2 & XTG_PARAM_DELAYRANGE_MASK) <<
 487                                        XTG_PARAM_DELAYRANGE_SHIFT;
 488                break;
 489
 490        case XTG_PARAM_OP_NOP:
 491                *param_word = 0;
 492                break;
 493        }
 494}
 495
 496/**
 497 * xtg_sysfs_ioctl - Implements sysfs operations
 498 * @dev: Device structure
 499 * @buf: Value to write
 500 * @opcode: Ioctl opcode
 501 *
 502 * Return: value read from the sysfs opcode.
 503 */
 504static ssize_t xtg_sysfs_ioctl(struct device *dev, const char *buf,
 505                                enum xtg_sysfs_ioctl_opcode opcode)
 506{
 507        struct xtg_dev_info *tg = to_xtg_dev_info(dev);
 508        unsigned long wrval;
 509        ssize_t status, rdval = 0;
 510
 511        if (opcode > XTG_GET_STREAM_TRANSFERCNT) {
 512                status = kstrtoul(buf, 16, &wrval);
 513                if (status < 0)
 514                        return status;
 515        }
 516
 517        switch (opcode) {
 518        case XTG_GET_MASTER_CMP_STS:
 519                rdval = (readl(tg->regs + XTG_MCNTL_OFFSET) &
 520                                XTG_MCNTL_MSTEN_MASK) ? 1 : 0;
 521                break;
 522
 523        case XTG_GET_SLV_CTRL_REG:
 524                rdval = readl(tg->regs + XTG_SCNTL_OFFSET);
 525                break;
 526
 527        case XTG_GET_ERR_STS:
 528                rdval = readl(tg->regs + XTG_ERR_STS_OFFSET) &
 529                                XTG_ERR_ALL_ERRS_MASK;
 530                break;
 531
 532        case XTG_GET_CFG_STS:
 533                rdval = readl(tg->regs + XTG_CFG_STS_OFFSET);
 534                break;
 535
 536        case XTG_GET_LAST_VALID_INDEX:
 537                rdval = (tg->last_wr_valid_idx << 16) |
 538                                tg->last_rd_valid_idx;
 539                break;
 540
 541        case XTG_GET_DEVICE_ID:
 542                rdval = tg->id;
 543                break;
 544
 545        case XTG_GET_RESOURCE:
 546                rdval = (unsigned long)tg->regs;
 547                break;
 548
 549        case XTG_GET_STATIC_ENABLE:
 550                rdval = readl(tg->regs + XTG_STATIC_CNTL_OFFSET);
 551                break;
 552
 553        case XTG_GET_STATIC_BURSTLEN:
 554                rdval = readl(tg->regs + XTG_STATIC_LEN_OFFSET);
 555                break;
 556
 557        case XTG_GET_STATIC_TRANSFERDONE:
 558                rdval = (readl(tg->regs + XTG_STATIC_CNTL_OFFSET) &
 559                                XTG_STATIC_CNTL_TD_MASK);
 560                break;
 561
 562        case XTG_GET_STREAM_ENABLE:
 563                rdval = readl(tg->regs + XTG_STREAM_CNTL_OFFSET);
 564                break;
 565
 566        case XTG_GET_STREAM_TRANSFERLEN:
 567                rdval = (readl(tg->regs + XTG_STREAM_TL_OFFSET) &
 568                                XTG_STREAM_TL_TLEN_MASK);
 569                break;
 570
 571        case XTG_GET_STREAM_TRANSFERCNT:
 572                rdval = ((readl(tg->regs + XTG_STREAM_TL_OFFSET) &
 573                                XTG_STREAM_TL_TCNT_MASK) >>
 574                                XTG_STREAM_TL_TCNT_SHIFT);
 575                break;
 576
 577        case XTG_START_MASTER_LOGIC:
 578                if (wrval)
 579                        writel(readl(tg->regs + XTG_MCNTL_OFFSET) |
 580                                        XTG_MCNTL_MSTEN_MASK,
 581                                tg->regs + XTG_MCNTL_OFFSET);
 582                break;
 583
 584        case XTG_SET_SLV_CTRL_REG:
 585                writel(wrval, tg->regs + XTG_SCNTL_OFFSET);
 586                break;
 587
 588        case XTG_ENABLE_ERRORS:
 589                wrval &= XTG_ERR_ALL_ERRS_MASK;
 590                writel(readl(tg->regs + XTG_ERR_EN_OFFSET) | wrval,
 591                        tg->regs + XTG_ERR_EN_OFFSET);
 592                break;
 593
 594        case XTG_CLEAR_ERRORS:
 595                wrval &= XTG_ERR_ALL_ERRS_MASK;
 596                writel(readl(tg->regs + XTG_ERR_STS_OFFSET) | wrval,
 597                        tg->regs + XTG_ERR_STS_OFFSET);
 598                break;
 599
 600        case XTG_ENABLE_INTRS:
 601                if (wrval & XTG_MASTER_CMP_INTR) {
 602                        pr_info("Enabling Master Complete Interrupt\n");
 603                        writel(readl(tg->regs + XTG_ERR_EN_OFFSET) |
 604                                        XTG_ERR_EN_MSTIRQEN_MASK,
 605                                tg->regs + XTG_ERR_EN_OFFSET);
 606                }
 607                if (wrval & XTG_MASTER_ERR_INTR) {
 608                        pr_info("Enabling Interrupt on Master Errors\n");
 609                        writel(readl(tg->regs + XTG_MSTERR_INTR_OFFSET) |
 610                                        XTG_MSTERR_INTR_MINTREN_MASK,
 611                                tg->regs + XTG_MSTERR_INTR_OFFSET);
 612                }
 613                if (wrval & XTG_SLAVE_ERR_INTR) {
 614                        pr_info("Enabling Interrupt on Slave Errors\n");
 615                        writel(readl(tg->regs + XTG_SCNTL_OFFSET) |
 616                                        XTG_SCNTL_ERREN_MASK,
 617                                tg->regs + XTG_SCNTL_OFFSET);
 618                }
 619                break;
 620
 621        case XTG_CLEAR_MRAM:
 622                if (wrval)
 623                        xtg_access_rams(tg, tg->xtg_mram_offset,
 624                                XTG_MASTER_RAM_SIZE, XTG_WRITE_RAM |
 625                                XTG_WRITE_RAM_ZERO, NULL);
 626                break;
 627
 628        case XTG_CLEAR_CRAM:
 629                if (wrval)
 630                        xtg_access_rams(tg, XTG_COMMAND_RAM_OFFSET,
 631                                XTG_COMMAND_RAM_SIZE, XTG_WRITE_RAM |
 632                                XTG_WRITE_RAM_ZERO, NULL);
 633                break;
 634
 635        case XTG_CLEAR_PRAM:
 636                if (wrval)
 637                        xtg_access_rams(tg, XTG_PARAM_RAM_OFFSET,
 638                                XTG_PARAM_RAM_SIZE, XTG_WRITE_RAM |
 639                                XTG_WRITE_RAM_ZERO, NULL);
 640                break;
 641
 642        case XTG_SET_STATIC_ENABLE:
 643                if (wrval) {
 644                        wrval &= XTG_STATIC_CNTL_STEN_MASK;
 645                        writel(readl(tg->regs + XTG_STATIC_CNTL_OFFSET) | wrval,
 646                        tg->regs + XTG_STATIC_CNTL_OFFSET);
 647                } else {
 648                        writel(readl(tg->regs + XTG_STATIC_CNTL_OFFSET) &
 649                                ~XTG_STATIC_CNTL_STEN_MASK,
 650                                tg->regs + XTG_STATIC_CNTL_OFFSET);
 651                }
 652                break;
 653
 654        case XTG_SET_STATIC_BURSTLEN:
 655                writel(wrval, tg->regs + XTG_STATIC_LEN_OFFSET);
 656                break;
 657
 658        case XTG_SET_STATIC_TRANSFERDONE:
 659                wrval |= XTG_STATIC_CNTL_TD_MASK;
 660                writel(readl(tg->regs + XTG_STATIC_CNTL_OFFSET) | wrval,
 661                        tg->regs + XTG_STATIC_CNTL_OFFSET);
 662                break;
 663
 664        case XTG_SET_STREAM_ENABLE:
 665                if (wrval) {
 666                        wrval &= XTG_STREAM_CNTL_STEN_MASK;
 667                        writel(readl(tg->regs + XTG_STREAM_CNTL_OFFSET) | wrval,
 668                        tg->regs + XTG_STREAM_CNTL_OFFSET);
 669                } else {
 670                        writel(readl(tg->regs + XTG_STREAM_CNTL_OFFSET) &
 671                        ~XTG_STREAM_CNTL_STEN_MASK,
 672                        tg->regs + XTG_STREAM_CNTL_OFFSET);
 673                }
 674                break;
 675
 676        case XTG_SET_STREAM_TRANSFERLEN:
 677                wrval &= XTG_STREAM_TL_TLEN_MASK;
 678                writel(readl(tg->regs + XTG_STREAM_TL_OFFSET) | wrval,
 679                        tg->regs + XTG_STREAM_TL_OFFSET);
 680                break;
 681
 682        case XTG_SET_STREAM_TRANSFERCNT:
 683                wrval = ((wrval << XTG_STREAM_TL_TCNT_SHIFT) &
 684                                XTG_STREAM_TL_TCNT_MASK);
 685                writel(readl(tg->regs + XTG_STREAM_TL_OFFSET) | wrval,
 686                        tg->regs + XTG_STREAM_TL_OFFSET);
 687                break;
 688
 689        default:
 690                break;
 691        }
 692
 693        return rdval;
 694}
 695
 696/* Sysfs functions */
 697
 698static ssize_t id_show(struct device *dev,
 699                struct device_attribute *attr, char *buf)
 700{
 701        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_DEVICE_ID);
 702
 703        return snprintf(buf, PAGE_SIZE, "%d\n", rdval);
 704}
 705static DEVICE_ATTR_RO(id);
 706
 707static ssize_t resource_show(struct device *dev,
 708                struct device_attribute *attr, char *buf)
 709{
 710        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_RESOURCE);
 711
 712        return snprintf(buf, PAGE_SIZE, "0x%08x\n", rdval);
 713}
 714static DEVICE_ATTR_RO(resource);
 715
 716static ssize_t master_start_stop_show(struct device *dev,
 717                struct device_attribute *attr, char *buf)
 718{
 719        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_MASTER_CMP_STS);
 720
 721        return snprintf(buf, PAGE_SIZE, "%d\n", rdval);
 722}
 723
 724static ssize_t master_start_stop_store(struct device *dev,
 725                struct device_attribute *attr, const char *buf, size_t size)
 726{
 727        xtg_sysfs_ioctl(dev, buf, XTG_START_MASTER_LOGIC);
 728
 729        return size;
 730}
 731static DEVICE_ATTR_RW(master_start_stop);
 732
 733static ssize_t config_slave_status_show(struct device *dev,
 734                struct device_attribute *attr, char *buf)
 735{
 736        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_SLV_CTRL_REG);
 737
 738        return snprintf(buf, PAGE_SIZE, "0x%08x\n", rdval);
 739}
 740
 741static ssize_t config_slave_status_store(struct device *dev,
 742                struct device_attribute *attr, const char *buf, size_t size)
 743{
 744        xtg_sysfs_ioctl(dev, buf, XTG_SET_SLV_CTRL_REG);
 745
 746        return size;
 747}
 748static DEVICE_ATTR_RW(config_slave_status);
 749
 750static ssize_t err_sts_show(struct device *dev,
 751                struct device_attribute *attr, char *buf)
 752{
 753        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_ERR_STS);
 754
 755        return snprintf(buf, PAGE_SIZE, "0x%08x\n", rdval);
 756}
 757
 758static ssize_t err_sts_store(struct device *dev,
 759                struct device_attribute *attr, const char *buf, size_t size)
 760{
 761        xtg_sysfs_ioctl(dev, buf, XTG_CLEAR_ERRORS);
 762
 763        return size;
 764}
 765static DEVICE_ATTR_RW(err_sts);
 766
 767static ssize_t err_en_store(struct device *dev,
 768                struct device_attribute *attr, const char *buf, size_t size)
 769{
 770        xtg_sysfs_ioctl(dev, buf, XTG_ENABLE_ERRORS);
 771
 772        return size;
 773}
 774static DEVICE_ATTR_WO(err_en);
 775
 776static ssize_t intr_en_store(struct device *dev,
 777                struct device_attribute *attr, const char *buf, size_t size)
 778{
 779        xtg_sysfs_ioctl(dev, buf, XTG_ENABLE_INTRS);
 780
 781        return size;
 782}
 783static DEVICE_ATTR_WO(intr_en);
 784
 785static ssize_t last_valid_index_show(struct device *dev,
 786                struct device_attribute *attr, char *buf)
 787{
 788        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_LAST_VALID_INDEX);
 789
 790        return snprintf(buf, PAGE_SIZE, "0x%08x\n", rdval);
 791}
 792static DEVICE_ATTR_RO(last_valid_index);
 793
 794static ssize_t config_sts_show(struct device *dev,
 795                struct device_attribute *attr, char *buf)
 796{
 797        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_CFG_STS);
 798
 799        return snprintf(buf, PAGE_SIZE, "0x%08x\n", rdval);
 800}
 801static DEVICE_ATTR_RO(config_sts);
 802
 803static ssize_t mram_clear_store(struct device *dev,
 804                struct device_attribute *attr, const char *buf, size_t size)
 805{
 806        xtg_sysfs_ioctl(dev, buf, XTG_CLEAR_MRAM);
 807
 808        return size;
 809}
 810static DEVICE_ATTR_WO(mram_clear);
 811
 812static ssize_t cram_clear_store(struct device *dev,
 813                struct device_attribute *attr, const char *buf, size_t size)
 814{
 815        xtg_sysfs_ioctl(dev, buf, XTG_CLEAR_CRAM);
 816
 817        return size;
 818}
 819static DEVICE_ATTR_WO(cram_clear);
 820
 821static ssize_t pram_clear_store(struct device *dev,
 822                struct device_attribute *attr, const char *buf, size_t size)
 823{
 824        xtg_sysfs_ioctl(dev, buf, XTG_CLEAR_CRAM);
 825
 826        return size;
 827}
 828static DEVICE_ATTR_WO(pram_clear);
 829
 830static ssize_t static_enable_show(struct device *dev,
 831                struct device_attribute *attr, char *buf)
 832{
 833        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_STATIC_ENABLE);
 834
 835        return snprintf(buf, PAGE_SIZE, "0x%08x\n", rdval);
 836}
 837
 838static ssize_t static_enable_store(struct device *dev,
 839                struct device_attribute *attr, const char *buf, size_t size)
 840{
 841        xtg_sysfs_ioctl(dev, buf, XTG_SET_STATIC_ENABLE);
 842
 843        return size;
 844}
 845static DEVICE_ATTR_RW(static_enable);
 846
 847static ssize_t static_burstlen_show(struct device *dev,
 848                struct device_attribute *attr, char *buf)
 849{
 850        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_STATIC_BURSTLEN);
 851
 852        return snprintf(buf, PAGE_SIZE, "%d\n", rdval);
 853}
 854
 855static ssize_t static_burstlen_store(struct device *dev,
 856                struct device_attribute *attr, const char *buf, size_t size)
 857{
 858        xtg_sysfs_ioctl(dev, buf, XTG_SET_STATIC_BURSTLEN);
 859
 860        return size;
 861}
 862static DEVICE_ATTR_RW(static_burstlen);
 863
 864static ssize_t static_transferdone_show(struct device *dev,
 865                struct device_attribute *attr, char *buf)
 866{
 867        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_STATIC_TRANSFERDONE);
 868
 869        return snprintf(buf, PAGE_SIZE, "%d\n", rdval);
 870}
 871
 872static ssize_t static_transferdone_store(struct device *dev,
 873                struct device_attribute *attr, const char *buf, size_t size)
 874{
 875        xtg_sysfs_ioctl(dev, buf, XTG_SET_STATIC_TRANSFERDONE);
 876
 877        return size;
 878}
 879static DEVICE_ATTR_RW(static_transferdone);
 880
 881static ssize_t reset_static_transferdone_show(struct device *dev,
 882                struct device_attribute *attr, char *buf)
 883{
 884        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_STATIC_TRANSFERDONE);
 885        if (rdval == XTG_STATIC_CNTL_RESET_MASK)
 886                rdval = 1;
 887        else
 888                rdval = 0;
 889        return snprintf(buf, PAGE_SIZE, "%d\n", rdval);
 890}
 891static DEVICE_ATTR_RO(reset_static_transferdone);
 892
 893static ssize_t stream_enable_show(struct device *dev,
 894                struct device_attribute *attr, char *buf)
 895{
 896        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_STREAM_ENABLE);
 897
 898        return snprintf(buf, PAGE_SIZE, "0x%08x\n", rdval);
 899}
 900
 901static ssize_t stream_enable_store(struct device *dev,
 902                struct device_attribute *attr, const char *buf, size_t size)
 903{
 904        xtg_sysfs_ioctl(dev, buf, XTG_SET_STREAM_ENABLE);
 905
 906        return size;
 907}
 908static DEVICE_ATTR_RW(stream_enable);
 909
 910static ssize_t stream_transferlen_show(struct device *dev,
 911                struct device_attribute *attr, char *buf)
 912{
 913        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_STREAM_TRANSFERLEN);
 914
 915        return snprintf(buf, PAGE_SIZE, "%d\n", rdval);
 916}
 917
 918static ssize_t stream_transferlen_store(struct device *dev,
 919                struct device_attribute *attr, const char *buf, size_t size)
 920{
 921        xtg_sysfs_ioctl(dev, buf, XTG_SET_STREAM_TRANSFERLEN);
 922
 923        return size;
 924}
 925static DEVICE_ATTR_RW(stream_transferlen);
 926
 927static ssize_t stream_transfercnt_show(struct device *dev,
 928                struct device_attribute *attr, char *buf)
 929{
 930        ssize_t rdval = xtg_sysfs_ioctl(dev, buf, XTG_GET_STREAM_TRANSFERCNT);
 931
 932        return snprintf(buf, PAGE_SIZE, "%d\n", rdval);
 933}
 934
 935static ssize_t stream_transfercnt_store(struct device *dev,
 936                struct device_attribute *attr, const char *buf, size_t size)
 937{
 938        xtg_sysfs_ioctl(dev, buf, XTG_SET_STREAM_TRANSFERCNT);
 939
 940        return size;
 941}
 942static DEVICE_ATTR_RW(stream_transfercnt);
 943
 944static ssize_t xtg_pram_read(struct file *filp, struct kobject *kobj,
 945                                struct bin_attribute *bin_attr,
 946                                char *buf, loff_t off, size_t count)
 947{
 948        pr_info("No read access to Parameter RAM\n");
 949
 950        return 0;
 951}
 952
 953static ssize_t xtg_pram_write(struct file *filp, struct kobject *kobj,
 954                                struct bin_attribute *bin_attr,
 955                                char *buf, loff_t off, size_t count)
 956{
 957        struct xtg_dev_info *tg =
 958                to_xtg_dev_info(container_of(kobj, struct device, kobj));
 959        u32 *data = (u32 *)buf;
 960
 961        if (off >= XTG_PARAM_RAM_SIZE) {
 962                pr_err("Requested Write len exceeds 2K PRAM size\n");
 963                return -ENOMEM;
 964        }
 965
 966        if (count >= XTG_PARAM_RAM_SIZE)
 967                count = XTG_PARAM_RAM_SIZE;
 968
 969        /* Program each command */
 970        if (count == sizeof(struct xtg_pram)) {
 971                struct xtg_pram *cmdp = (struct xtg_pram *)buf;
 972                u32 param_word;
 973
 974                if (!cmdp)
 975                        return -EINVAL;
 976
 977                if (cmdp->is_valid_req == VALID_SIG) {
 978                        /* Prepare parameter word */
 979                        xtg_prepare_param_word(tg, cmdp, &param_word);
 980
 981                        count = XTG_PRAM_BYTES_PER_ENTRY;
 982                        data = &param_word;
 983
 984                        /* Maximum command entries are 256 */
 985                        if (cmdp->index > MAX_NUM_ENTRIES)
 986                                return -EINVAL;
 987
 988                        /* Calculate the block index */
 989                        if (cmdp->is_write_block)
 990                                off = XTG_PRM_RAM_BLOCK_SIZE +
 991                                                cmdp->index * count;
 992                        else
 993                                off = cmdp->index * count;
 994                }
 995        }
 996
 997        off += XTG_PARAM_RAM_OFFSET;
 998        xtg_access_rams(tg, off, count, XTG_WRITE_RAM, data);
 999
1000        return count;
1001}
1002
1003static ssize_t xtg_pram_mmap(struct file *filp, struct kobject *kobj,
1004                                struct bin_attribute *attr,
1005                                struct vm_area_struct *vma)
1006{
1007        struct xtg_dev_info *tg =
1008                to_xtg_dev_info(container_of(kobj, struct device, kobj));
1009        int ret;
1010
1011        vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1012        vma->vm_flags |= VM_IO;
1013
1014        ret = remap_pfn_range(vma, vma->vm_start, (tg->phys_base_addr +
1015                        XTG_PARAM_RAM_OFFSET) >> PAGE_SHIFT,
1016                        XTG_PARAM_RAM_SIZE, vma->vm_page_prot);
1017        return ret;
1018}
1019
1020static struct bin_attribute xtg_pram_attr = {
1021        .attr = {
1022                .name = "parameter_ram",
1023                .mode = S_IRUGO | S_IWUSR,
1024        },
1025        .size = XTG_PARAM_RAM_SIZE,
1026        .read = xtg_pram_read,
1027        .write = xtg_pram_write,
1028        .mmap = xtg_pram_mmap,
1029};
1030
1031static ssize_t xtg_cram_read(struct file *filp, struct kobject *kobj,
1032                                struct bin_attribute *bin_attr,
1033                                char *buf, loff_t off, size_t count)
1034{
1035        struct xtg_dev_info *tg =
1036                to_xtg_dev_info(container_of(kobj, struct device, kobj));
1037
1038        off += XTG_COMMAND_RAM_OFFSET;
1039        xtg_access_rams(tg, off, count, XTG_READ_RAM, (u32 *)buf);
1040
1041        return count;
1042}
1043
1044static ssize_t xtg_cram_write(struct file *filp, struct kobject *kobj,
1045                                struct bin_attribute *bin_attr,
1046                                char *buf, loff_t off, size_t count)
1047{
1048        struct xtg_dev_info *tg =
1049                to_xtg_dev_info(container_of(kobj, struct device, kobj));
1050        u32 *data = (u32 *)buf;
1051
1052        if (off >= XTG_COMMAND_RAM_SIZE) {
1053                pr_err("Requested Write len exceeds 8K CRAM size\n");
1054                return -ENOMEM;
1055        }
1056
1057        /* Program each command */
1058        if (count == sizeof(struct xtg_cram)) {
1059                struct xtg_cram *cmdp = (struct xtg_cram *)buf;
1060                u32 cmd_words[CMD_WDS + EXT_WDS];
1061
1062                if (!cmdp)
1063                        return -EINVAL;
1064
1065                if (cmdp->is_valid_req == VALID_SIG) {
1066                        /* Prepare command words */
1067                        xtg_prepare_cmd_words(tg, cmdp, cmd_words);
1068                        count = XTG_CRAM_BYTES_PER_ENTRY;
1069                        data = cmd_words;
1070
1071                        /* Maximum command entries are 256 */
1072                        if (cmdp->index > MAX_NUM_ENTRIES)
1073                                return -EINVAL;
1074
1075                        /* Calculate the block index */
1076                        if (cmdp->is_write_block)
1077                                off = XTG_CMD_RAM_BLOCK_SIZE +
1078                                                cmdp->index * count;
1079                        else
1080                                off = cmdp->index * count;
1081
1082                        /* Store the valid command index */
1083                        if (cmdp->valid_cmd) {
1084                                if (cmdp->is_write_block)
1085                                        tg->last_wr_valid_idx =
1086                                                        cmdp->index;
1087                                else
1088                                        tg->last_rd_valid_idx =
1089                                                        cmdp->index;
1090                        }
1091                }
1092        }
1093
1094        off += XTG_COMMAND_RAM_OFFSET;
1095        xtg_access_rams(tg, off, count, XTG_WRITE_RAM, data);
1096
1097        return count;
1098}
1099
1100static ssize_t xtg_cram_mmap(struct file *filp, struct kobject *kobj,
1101                                struct bin_attribute *attr,
1102                                struct vm_area_struct *vma)
1103{
1104        struct xtg_dev_info *tg =
1105                to_xtg_dev_info(container_of(kobj, struct device, kobj));
1106        int ret;
1107
1108        vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1109        vma->vm_flags |= VM_IO;
1110
1111        ret = remap_pfn_range(vma, vma->vm_start, (tg->phys_base_addr +
1112                        XTG_COMMAND_RAM_OFFSET) >> PAGE_SHIFT,
1113                        XTG_COMMAND_RAM_SIZE + XTG_EXTCMD_RAM_SIZE,
1114                        vma->vm_page_prot);
1115        return ret;
1116}
1117
1118static struct bin_attribute xtg_cram_attr = {
1119        .attr = {
1120                .name = "command_ram",
1121                .mode = S_IRUGO | S_IWUSR,
1122        },
1123        .size = XTG_COMMAND_RAM_SIZE,
1124        .read = xtg_cram_read,
1125        .write = xtg_cram_write,
1126        .mmap = xtg_cram_mmap,
1127};
1128
1129static ssize_t xtg_mram_read(struct file *filp, struct kobject *kobj,
1130                                struct bin_attribute *bin_attr,
1131                                char *buf, loff_t off, size_t count)
1132{
1133        struct xtg_dev_info *tg =
1134                to_xtg_dev_info(container_of(kobj, struct device, kobj));
1135
1136        off += tg->xtg_mram_offset;
1137        xtg_access_rams(tg, off, count, XTG_READ_RAM, (u32 *)buf);
1138
1139        return count;
1140}
1141
1142static ssize_t xtg_mram_write(struct file *filp, struct kobject *kobj,
1143                                struct bin_attribute *bin_attr,
1144                                char *buf, loff_t off, size_t count)
1145{
1146        struct xtg_dev_info *tg =
1147                to_xtg_dev_info(container_of(kobj, struct device, kobj));
1148
1149        if (off >= XTG_MASTER_RAM_SIZE) {
1150                pr_err("Requested Write len exceeds 8K MRAM size\n");
1151                return -ENOMEM;
1152        }
1153
1154        off += tg->xtg_mram_offset;
1155        xtg_access_rams(tg, off, count, XTG_WRITE_RAM, (u32 *)buf);
1156
1157        return count;
1158}
1159
1160static ssize_t xtg_mram_mmap(struct file *filp, struct kobject *kobj,
1161                                struct bin_attribute *attr,
1162                                struct vm_area_struct *vma)
1163{
1164        struct xtg_dev_info *tg =
1165                to_xtg_dev_info(container_of(kobj, struct device, kobj));
1166        int ret;
1167
1168        vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1169        vma->vm_flags |= VM_IO;
1170
1171        ret = remap_pfn_range(vma, vma->vm_start, (tg->phys_base_addr +
1172                        tg->xtg_mram_offset) >> PAGE_SHIFT,
1173                        XTG_MASTER_RAM_SIZE,
1174                        vma->vm_page_prot);
1175        return ret;
1176}
1177
1178static struct bin_attribute xtg_mram_attr = {
1179        .attr = {
1180                .name = "master_ram",
1181                .mode = S_IRUGO | S_IWUSR,
1182        },
1183        .size = XTG_MASTER_RAM_SIZE,
1184        .read = xtg_mram_read,
1185        .write = xtg_mram_write,
1186        .mmap = xtg_mram_mmap,
1187};
1188
1189static struct bin_attribute *xtg_bin_attrs[] = {
1190        &xtg_mram_attr,
1191        &xtg_pram_attr,
1192        &xtg_cram_attr,
1193        NULL,
1194};
1195
1196static const struct attribute *xtg_attrs[] = {
1197        &dev_attr_id.attr,
1198        &dev_attr_resource.attr,
1199        &dev_attr_master_start_stop.attr,
1200        &dev_attr_config_slave_status.attr,
1201        &dev_attr_err_en.attr,
1202        &dev_attr_err_sts.attr,
1203        &dev_attr_intr_en.attr,
1204        &dev_attr_last_valid_index.attr,
1205        &dev_attr_config_sts.attr,
1206        &dev_attr_mram_clear.attr,
1207        &dev_attr_cram_clear.attr,
1208        &dev_attr_pram_clear.attr,
1209        &dev_attr_static_enable.attr,
1210        &dev_attr_static_burstlen.attr,
1211        &dev_attr_static_transferdone.attr,
1212        &dev_attr_stream_transfercnt.attr,
1213        &dev_attr_stream_transferlen.attr,
1214        &dev_attr_stream_enable.attr,
1215        &dev_attr_reset_static_transferdone.attr,
1216        NULL,
1217};
1218
1219static const struct attribute_group xtg_attributes = {
1220        .attrs = (struct attribute **)xtg_attrs,
1221        .bin_attrs = xtg_bin_attrs,
1222};
1223/**
1224 * xtg_cmp_intr_handler - Master Complete Interrupt handler
1225 * @irq: IRQ number
1226 * @data: Pointer to the xtg_dev_info structure
1227 *
1228 * Return: IRQ_HANDLED always
1229 */
1230static irqreturn_t xtg_cmp_intr_handler(int irq, void *data)
1231{
1232        struct xtg_dev_info *tg = (struct xtg_dev_info *)data;
1233
1234        writel(readl(tg->regs + XTG_ERR_STS_OFFSET) |
1235                        XTG_ERR_STS_MSTDONE_MASK,
1236                tg->regs + XTG_ERR_STS_OFFSET);
1237
1238        return IRQ_HANDLED;
1239}
1240
1241/**
1242 * xtg_err_intr_handler - Master/Slave Error Interrupt handler
1243 * @irq: IRQ number
1244 * @data: Pointer to the xtg_dev_info structure
1245 *
1246 * Return: IRQ_HANDLED always
1247 */
1248static irqreturn_t xtg_err_intr_handler(int irq, void *data)
1249{
1250        struct xtg_dev_info *tg = (struct xtg_dev_info *)data;
1251        u32 value;
1252
1253        value = readl(tg->regs + XTG_ERR_STS_OFFSET) &
1254                        XTG_ERR_ALL_ERRS_MASK;
1255
1256        if (value) {
1257                dev_err(tg->dev, "Found errors 0x%08x\n", value);
1258                writel(readl(tg->regs + XTG_ERR_STS_OFFSET) | value,
1259                        tg->regs + XTG_ERR_STS_OFFSET);
1260        }
1261
1262        return IRQ_HANDLED;
1263}
1264
1265/**
1266 * xtg_probe - Driver probe function
1267 * @pdev: Pointer to the platform_device structure
1268 *
1269 * This is the driver probe routine. It does all the memory
1270 * allocation and creates sysfs entires for the device.
1271 *
1272 * Return: 0 on success and failure value on error
1273 */
1274static int xtg_probe(struct platform_device *pdev)
1275{
1276        struct xtg_dev_info *tg;
1277        struct device_node *node;
1278        struct resource *res;
1279        struct device *dev;
1280        int err, irq, var;
1281
1282        tg = devm_kzalloc(&pdev->dev, sizeof(*tg), GFP_KERNEL);
1283        if (!tg)
1284                return -ENOMEM;
1285
1286        tg->dev = &(pdev->dev);
1287        dev = tg->dev;
1288        node = pdev->dev.of_node;
1289
1290        /* Map the registers */
1291        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1292        tg->regs = devm_ioremap_resource(&pdev->dev, res);
1293        if (IS_ERR(tg->regs))
1294                return PTR_ERR(tg->regs);
1295
1296
1297        /* Save physical base address */
1298        tg->phys_base_addr = res->start;
1299
1300        /* Get the device instance id */
1301        err = of_property_read_u32(node, "xlnx,device-id", &tg->id);
1302        if (err < 0) {
1303                dev_err(&pdev->dev, "unable to read property");
1304                return err;
1305        }
1306
1307        /* Map the error interrupt, if it exists in the device tree. */
1308        irq = platform_get_irq_byname(pdev, "err-out");
1309        if (irq < 0) {
1310                dev_dbg(&pdev->dev, "unable to get err irq");
1311        } else {
1312                err = devm_request_irq(&pdev->dev, irq, xtg_err_intr_handler,
1313                                        0, dev_name(&pdev->dev), tg);
1314                if (err < 0) {
1315                        dev_err(&pdev->dev, "unable to request irq %d", irq);
1316                        return err;
1317                }
1318        }
1319
1320        /* Map the completion interrupt, if it exists in the device tree. */
1321        irq = platform_get_irq_byname(pdev, "irq-out");
1322        if (irq < 0) {
1323                dev_dbg(&pdev->dev, "unable to get cmp irq");
1324        } else {
1325                err = devm_request_irq(&pdev->dev, irq, xtg_cmp_intr_handler,
1326                                        0, dev_name(&pdev->dev), tg);
1327                if (err < 0) {
1328                        dev_err(&pdev->dev, "unable to request irq %d", irq);
1329                        return err;
1330                }
1331        }
1332
1333        /*
1334         * Create sysfs file entries for the device
1335         */
1336        err = sysfs_create_group(&dev->kobj, &xtg_attributes);
1337        if (err < 0) {
1338                dev_err(tg->dev, "unable to create sysfs entries\n");
1339                return err;
1340        }
1341
1342        /*
1343         * Initialize the write and read valid index values.
1344         * Possible range of values for these variables is <0 255>.
1345         */
1346        tg->last_wr_valid_idx = -1;
1347        tg->last_rd_valid_idx = -1;
1348
1349        dev_set_drvdata(&pdev->dev, tg);
1350
1351        /* Update the Proper MasterRam offset */
1352        tg->xtg_mram_offset = XTG_MASTER_RAM_OFFSET;
1353        var = readl(tg->regs + XTG_MCNTL_OFFSET) >> XTG_MCNTL_REV_SHIFT;
1354        if (var == XTG_INIT_VERSION)
1355                tg->xtg_mram_offset = XTG_MASTER_RAM_INIT_OFFSET;
1356
1357        dev_info(&pdev->dev, "Probing xilinx traffic generator success\n");
1358
1359        return 0;
1360}
1361
1362/**
1363 * xtg_remove - Driver remove function
1364 * @pdev: Pointer to the platform_device structure
1365 *
1366 * This function frees all the resources allocated to the device.
1367 *
1368 * Return: 0 always
1369 */
1370static int xtg_remove(struct platform_device *pdev)
1371{
1372        struct xtg_dev_info *tg;
1373        struct device *dev;
1374
1375        tg = dev_get_drvdata(&pdev->dev);
1376        dev = tg->dev;
1377        sysfs_remove_group(&dev->kobj, &xtg_attributes);
1378
1379
1380        return 0;
1381}
1382
1383static struct of_device_id xtg_of_match[] = {
1384        { .compatible = "xlnx,axi-traffic-gen", },
1385        { /* end of table */ }
1386};
1387MODULE_DEVICE_TABLE(of, xtg_of_match);
1388
1389static struct platform_driver xtg_driver = {
1390        .driver = {
1391                .name = "xilinx-trafgen",
1392                .of_match_table = xtg_of_match,
1393        },
1394        .probe = xtg_probe,
1395        .remove = xtg_remove,
1396};
1397
1398module_platform_driver(xtg_driver);
1399
1400MODULE_AUTHOR("Xilinx Inc.");
1401MODULE_DESCRIPTION("Xilinx Traffic Generator driver");
1402MODULE_LICENSE("GPL");
1403