18c2ecf20Sopenharmony_ci/*****************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci *     Author: Xilinx, Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *     This program is free software; you can redistribute it and/or modify it
68c2ecf20Sopenharmony_ci *     under the terms of the GNU General Public License as published by the
78c2ecf20Sopenharmony_ci *     Free Software Foundation; either version 2 of the License, or (at your
88c2ecf20Sopenharmony_ci *     option) any later version.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
118c2ecf20Sopenharmony_ci *     AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
128c2ecf20Sopenharmony_ci *     SOLUTIONS FOR XILINX DEVICES.  BY PROVIDING THIS DESIGN, CODE,
138c2ecf20Sopenharmony_ci *     OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
148c2ecf20Sopenharmony_ci *     APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
158c2ecf20Sopenharmony_ci *     THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
168c2ecf20Sopenharmony_ci *     AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
178c2ecf20Sopenharmony_ci *     FOR YOUR IMPLEMENTATION.  XILINX EXPRESSLY DISCLAIMS ANY
188c2ecf20Sopenharmony_ci *     WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
198c2ecf20Sopenharmony_ci *     IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
208c2ecf20Sopenharmony_ci *     REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
218c2ecf20Sopenharmony_ci *     INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
228c2ecf20Sopenharmony_ci *     FOR A PARTICULAR PURPOSE.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *     (c) Copyright 2003-2007 Xilinx Inc.
258c2ecf20Sopenharmony_ci *     All rights reserved.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci *     You should have received a copy of the GNU General Public License along
288c2ecf20Sopenharmony_ci *     with this program; if not, write to the Free Software Foundation, Inc.,
298c2ecf20Sopenharmony_ci *     675 Mass Ave, Cambridge, MA 02139, USA.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci *****************************************************************************/
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#ifndef XILINX_HWICAP_H_	/* prevent circular inclusions */
348c2ecf20Sopenharmony_ci#define XILINX_HWICAP_H_	/* by using protection macros */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <linux/types.h>
378c2ecf20Sopenharmony_ci#include <linux/cdev.h>
388c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <linux/io.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistruct hwicap_drvdata {
438c2ecf20Sopenharmony_ci	u32 write_buffer_in_use;  /* Always in [0,3] */
448c2ecf20Sopenharmony_ci	u8 write_buffer[4];
458c2ecf20Sopenharmony_ci	u32 read_buffer_in_use;	  /* Always in [0,3] */
468c2ecf20Sopenharmony_ci	u8 read_buffer[4];
478c2ecf20Sopenharmony_ci	resource_size_t mem_start;/* phys. address of the control registers */
488c2ecf20Sopenharmony_ci	resource_size_t mem_end;  /* phys. address of the control registers */
498c2ecf20Sopenharmony_ci	resource_size_t mem_size;
508c2ecf20Sopenharmony_ci	void __iomem *base_address;/* virt. address of the control registers */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	struct device *dev;
538c2ecf20Sopenharmony_ci	struct cdev cdev;	/* Char device structure */
548c2ecf20Sopenharmony_ci	dev_t devt;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	const struct hwicap_driver_config *config;
578c2ecf20Sopenharmony_ci	const struct config_registers *config_regs;
588c2ecf20Sopenharmony_ci	void *private_data;
598c2ecf20Sopenharmony_ci	bool is_open;
608c2ecf20Sopenharmony_ci	struct mutex sem;
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct hwicap_driver_config {
648c2ecf20Sopenharmony_ci	/* Read configuration data given by size into the data buffer.
658c2ecf20Sopenharmony_ci	 * Return 0 if successful.
668c2ecf20Sopenharmony_ci	 */
678c2ecf20Sopenharmony_ci	int (*get_configuration)(struct hwicap_drvdata *drvdata, u32 *data,
688c2ecf20Sopenharmony_ci			u32 size);
698c2ecf20Sopenharmony_ci	/* Write configuration data given by size from the data buffer.
708c2ecf20Sopenharmony_ci	 * Return 0 if successful.
718c2ecf20Sopenharmony_ci	 */
728c2ecf20Sopenharmony_ci	int (*set_configuration)(struct hwicap_drvdata *drvdata, u32 *data,
738c2ecf20Sopenharmony_ci			u32 size);
748c2ecf20Sopenharmony_ci	/* Get the status register, bit pattern given by:
758c2ecf20Sopenharmony_ci	 * D8 - 0 = configuration error
768c2ecf20Sopenharmony_ci	 * D7 - 1 = alignment found
778c2ecf20Sopenharmony_ci	 * D6 - 1 = readback in progress
788c2ecf20Sopenharmony_ci	 * D5 - 0 = abort in progress
798c2ecf20Sopenharmony_ci	 * D4 - Always 1
808c2ecf20Sopenharmony_ci	 * D3 - Always 1
818c2ecf20Sopenharmony_ci	 * D2 - Always 1
828c2ecf20Sopenharmony_ci	 * D1 - Always 1
838c2ecf20Sopenharmony_ci	 * D0 - 1 = operation completed
848c2ecf20Sopenharmony_ci	 */
858c2ecf20Sopenharmony_ci	u32 (*get_status)(struct hwicap_drvdata *drvdata);
868c2ecf20Sopenharmony_ci	/* Reset the hw */
878c2ecf20Sopenharmony_ci	void (*reset)(struct hwicap_drvdata *drvdata);
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* Number of times to poll the done register. This has to be large
918c2ecf20Sopenharmony_ci * enough to allow an entire configuration to complete. If an entire
928c2ecf20Sopenharmony_ci * page (4kb) is configured at once, that could take up to 4k cycles
938c2ecf20Sopenharmony_ci * with a byte-wide icap interface. In most cases, this driver is
948c2ecf20Sopenharmony_ci * used with a much smaller fifo, but this should be sufficient in the
958c2ecf20Sopenharmony_ci * worst case.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ci#define XHI_MAX_RETRIES     5000
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/************ Constant Definitions *************/
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define XHI_PAD_FRAMES              0x1
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* Mask for calculating configuration packet headers */
1048c2ecf20Sopenharmony_ci#define XHI_WORD_COUNT_MASK_TYPE_1  0x7FFUL
1058c2ecf20Sopenharmony_ci#define XHI_WORD_COUNT_MASK_TYPE_2  0x1FFFFFUL
1068c2ecf20Sopenharmony_ci#define XHI_TYPE_MASK               0x7
1078c2ecf20Sopenharmony_ci#define XHI_REGISTER_MASK           0xF
1088c2ecf20Sopenharmony_ci#define XHI_OP_MASK                 0x3
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define XHI_TYPE_SHIFT              29
1118c2ecf20Sopenharmony_ci#define XHI_REGISTER_SHIFT          13
1128c2ecf20Sopenharmony_ci#define XHI_OP_SHIFT                27
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define XHI_TYPE_1                  1
1158c2ecf20Sopenharmony_ci#define XHI_TYPE_2                  2
1168c2ecf20Sopenharmony_ci#define XHI_OP_WRITE                2
1178c2ecf20Sopenharmony_ci#define XHI_OP_READ                 1
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* Address Block Types */
1208c2ecf20Sopenharmony_ci#define XHI_FAR_CLB_BLOCK           0
1218c2ecf20Sopenharmony_ci#define XHI_FAR_BRAM_BLOCK          1
1228c2ecf20Sopenharmony_ci#define XHI_FAR_BRAM_INT_BLOCK      2
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistruct config_registers {
1258c2ecf20Sopenharmony_ci	u32 CRC;
1268c2ecf20Sopenharmony_ci	u32 FAR;
1278c2ecf20Sopenharmony_ci	u32 FDRI;
1288c2ecf20Sopenharmony_ci	u32 FDRO;
1298c2ecf20Sopenharmony_ci	u32 CMD;
1308c2ecf20Sopenharmony_ci	u32 CTL;
1318c2ecf20Sopenharmony_ci	u32 MASK;
1328c2ecf20Sopenharmony_ci	u32 STAT;
1338c2ecf20Sopenharmony_ci	u32 LOUT;
1348c2ecf20Sopenharmony_ci	u32 COR;
1358c2ecf20Sopenharmony_ci	u32 MFWR;
1368c2ecf20Sopenharmony_ci	u32 FLR;
1378c2ecf20Sopenharmony_ci	u32 KEY;
1388c2ecf20Sopenharmony_ci	u32 CBC;
1398c2ecf20Sopenharmony_ci	u32 IDCODE;
1408c2ecf20Sopenharmony_ci	u32 AXSS;
1418c2ecf20Sopenharmony_ci	u32 C0R_1;
1428c2ecf20Sopenharmony_ci	u32 CSOB;
1438c2ecf20Sopenharmony_ci	u32 WBSTAR;
1448c2ecf20Sopenharmony_ci	u32 TIMER;
1458c2ecf20Sopenharmony_ci	u32 BOOTSTS;
1468c2ecf20Sopenharmony_ci	u32 CTL_1;
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/* Configuration Commands */
1508c2ecf20Sopenharmony_ci#define XHI_CMD_NULL                0
1518c2ecf20Sopenharmony_ci#define XHI_CMD_WCFG                1
1528c2ecf20Sopenharmony_ci#define XHI_CMD_MFW                 2
1538c2ecf20Sopenharmony_ci#define XHI_CMD_DGHIGH              3
1548c2ecf20Sopenharmony_ci#define XHI_CMD_RCFG                4
1558c2ecf20Sopenharmony_ci#define XHI_CMD_START               5
1568c2ecf20Sopenharmony_ci#define XHI_CMD_RCAP                6
1578c2ecf20Sopenharmony_ci#define XHI_CMD_RCRC                7
1588c2ecf20Sopenharmony_ci#define XHI_CMD_AGHIGH              8
1598c2ecf20Sopenharmony_ci#define XHI_CMD_SWITCH              9
1608c2ecf20Sopenharmony_ci#define XHI_CMD_GRESTORE            10
1618c2ecf20Sopenharmony_ci#define XHI_CMD_SHUTDOWN            11
1628c2ecf20Sopenharmony_ci#define XHI_CMD_GCAPTURE            12
1638c2ecf20Sopenharmony_ci#define XHI_CMD_DESYNCH             13
1648c2ecf20Sopenharmony_ci#define XHI_CMD_IPROG               15 /* Only in Virtex5 */
1658c2ecf20Sopenharmony_ci#define XHI_CMD_CRCC                16 /* Only in Virtex5 */
1668c2ecf20Sopenharmony_ci#define XHI_CMD_LTIMER              17 /* Only in Virtex5 */
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* Packet constants */
1698c2ecf20Sopenharmony_ci#define XHI_SYNC_PACKET             0xAA995566UL
1708c2ecf20Sopenharmony_ci#define XHI_DUMMY_PACKET            0xFFFFFFFFUL
1718c2ecf20Sopenharmony_ci#define XHI_NOOP_PACKET             (XHI_TYPE_1 << XHI_TYPE_SHIFT)
1728c2ecf20Sopenharmony_ci#define XHI_TYPE_2_READ ((XHI_TYPE_2 << XHI_TYPE_SHIFT) | \
1738c2ecf20Sopenharmony_ci			(XHI_OP_READ << XHI_OP_SHIFT))
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#define XHI_TYPE_2_WRITE ((XHI_TYPE_2 << XHI_TYPE_SHIFT) | \
1768c2ecf20Sopenharmony_ci			(XHI_OP_WRITE << XHI_OP_SHIFT))
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci#define XHI_TYPE2_CNT_MASK          0x07FFFFFF
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#define XHI_TYPE_1_PACKET_MAX_WORDS 2047UL
1818c2ecf20Sopenharmony_ci#define XHI_TYPE_1_HEADER_BYTES     4
1828c2ecf20Sopenharmony_ci#define XHI_TYPE_2_HEADER_BYTES     8
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* Constant to use for CRC check when CRC has been disabled */
1858c2ecf20Sopenharmony_ci#define XHI_DISABLED_AUTO_CRC       0x0000DEFCUL
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/* Meanings of the bits returned by get_status */
1888c2ecf20Sopenharmony_ci#define XHI_SR_CFGERR_N_MASK 0x00000100 /* Config Error Mask */
1898c2ecf20Sopenharmony_ci#define XHI_SR_DALIGN_MASK 0x00000080 /* Data Alignment Mask */
1908c2ecf20Sopenharmony_ci#define XHI_SR_RIP_MASK 0x00000040 /* Read back Mask */
1918c2ecf20Sopenharmony_ci#define XHI_SR_IN_ABORT_N_MASK 0x00000020 /* Select Map Abort Mask */
1928c2ecf20Sopenharmony_ci#define XHI_SR_DONE_MASK 0x00000001 /* Done bit Mask  */
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/**
1958c2ecf20Sopenharmony_ci * hwicap_type_1_read - Generates a Type 1 read packet header.
1968c2ecf20Sopenharmony_ci * @reg: is the address of the register to be read back.
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * Return:
1998c2ecf20Sopenharmony_ci * Generates a Type 1 read packet header, which is used to indirectly
2008c2ecf20Sopenharmony_ci * read registers in the configuration logic.  This packet must then
2018c2ecf20Sopenharmony_ci * be sent through the icap device, and a return packet received with
2028c2ecf20Sopenharmony_ci * the information.
2038c2ecf20Sopenharmony_ci */
2048c2ecf20Sopenharmony_cistatic inline u32 hwicap_type_1_read(u32 reg)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	return (XHI_TYPE_1 << XHI_TYPE_SHIFT) |
2078c2ecf20Sopenharmony_ci		(reg << XHI_REGISTER_SHIFT) |
2088c2ecf20Sopenharmony_ci		(XHI_OP_READ << XHI_OP_SHIFT);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/**
2128c2ecf20Sopenharmony_ci * hwicap_type_1_write - Generates a Type 1 write packet header
2138c2ecf20Sopenharmony_ci * @reg: is the address of the register to be read back.
2148c2ecf20Sopenharmony_ci *
2158c2ecf20Sopenharmony_ci * Return: Type 1 write packet header
2168c2ecf20Sopenharmony_ci */
2178c2ecf20Sopenharmony_cistatic inline u32 hwicap_type_1_write(u32 reg)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	return (XHI_TYPE_1 << XHI_TYPE_SHIFT) |
2208c2ecf20Sopenharmony_ci		(reg << XHI_REGISTER_SHIFT) |
2218c2ecf20Sopenharmony_ci		(XHI_OP_WRITE << XHI_OP_SHIFT);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci#endif
225