xref: /dpdk/drivers/raw/ifpga/base/opae_i2c.h (revision e41856b515cece86e2082d97f4e2a83181df5f82)
1473c88f9SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2473c88f9SBruce Richardson  * Copyright(c) 2010-2019 Intel Corporation
3473c88f9SBruce Richardson  */
4473c88f9SBruce Richardson 
5473c88f9SBruce Richardson #ifndef _OPAE_I2C_H
6473c88f9SBruce Richardson #define _OPAE_I2C_H
7473c88f9SBruce Richardson 
8473c88f9SBruce Richardson #include "opae_osdep.h"
9473c88f9SBruce Richardson 
10473c88f9SBruce Richardson #define ALTERA_I2C_TFR_CMD	0x00	/* Transfer Command register */
11473c88f9SBruce Richardson #define ALTERA_I2C_TFR_CMD_STA	BIT(9)	/* send START before byte */
12473c88f9SBruce Richardson #define ALTERA_I2C_TFR_CMD_STO	BIT(8)	/* send STOP after byte */
13473c88f9SBruce Richardson #define ALTERA_I2C_TFR_CMD_RW_D	BIT(0)	/* Direction of transfer */
14473c88f9SBruce Richardson #define ALTERA_I2C_RX_DATA	0x04	/* RX data FIFO register */
15473c88f9SBruce Richardson #define ALTERA_I2C_CTRL		0x8	/* Control register */
16473c88f9SBruce Richardson #define ALTERA_I2C_CTRL_RXT_SHFT	4	/* RX FIFO Threshold */
17473c88f9SBruce Richardson #define ALTERA_I2C_CTRL_TCT_SHFT	2	/* TFER CMD FIFO Threshold */
18473c88f9SBruce Richardson #define ALTERA_I2C_CTRL_BSPEED	BIT(1)	/* Bus Speed */
19473c88f9SBruce Richardson #define ALTERA_I2C_CTRL_EN	BIT(0)	/* Enable Core */
20473c88f9SBruce Richardson #define ALTERA_I2C_ISER		0xc	/* Interrupt Status Enable register */
21473c88f9SBruce Richardson #define ALTERA_I2C_ISER_RXOF_EN	BIT(4)	/* Enable RX OVERFLOW IRQ */
22473c88f9SBruce Richardson #define ALTERA_I2C_ISER_ARB_EN	BIT(3)	/* Enable ARB LOST IRQ */
23473c88f9SBruce Richardson #define ALTERA_I2C_ISER_NACK_EN	BIT(2)	/* Enable NACK DET IRQ */
24473c88f9SBruce Richardson #define ALTERA_I2C_ISER_RXRDY_EN	BIT(1)	/* Enable RX Ready IRQ */
25473c88f9SBruce Richardson #define ALTERA_I2C_ISER_TXRDY_EN	BIT(0)	/* Enable TX Ready IRQ */
26473c88f9SBruce Richardson #define ALTERA_I2C_ISR		0x10	/* Interrupt Status register */
27473c88f9SBruce Richardson #define ALTERA_I2C_ISR_RXOF		BIT(4)	/* RX OVERFLOW */
28473c88f9SBruce Richardson #define ALTERA_I2C_ISR_ARB		BIT(3)	/* ARB LOST */
29473c88f9SBruce Richardson #define ALTERA_I2C_ISR_NACK		BIT(2)	/* NACK DET */
30473c88f9SBruce Richardson #define ALTERA_I2C_ISR_RXRDY		BIT(1)	/* RX Ready */
31473c88f9SBruce Richardson #define ALTERA_I2C_ISR_TXRDY		BIT(0)	/* TX Ready */
32473c88f9SBruce Richardson #define ALTERA_I2C_STATUS	0x14	/* Status register */
33473c88f9SBruce Richardson #define ALTERA_I2C_STAT_CORE		BIT(0)	/* Core Status */
34473c88f9SBruce Richardson #define ALTERA_I2C_TC_FIFO_LVL	0x18   /* Transfer FIFO LVL register */
35473c88f9SBruce Richardson #define ALTERA_I2C_RX_FIFO_LVL	0x1c	/* Receive FIFO LVL register */
36473c88f9SBruce Richardson #define ALTERA_I2C_SCL_LOW	0x20	/* SCL low count register */
37473c88f9SBruce Richardson #define ALTERA_I2C_SCL_HIGH	0x24	/* SCL high count register */
38473c88f9SBruce Richardson #define ALTERA_I2C_SDA_HOLD	0x28	/* SDA hold count register */
39473c88f9SBruce Richardson 
40473c88f9SBruce Richardson #define ALTERA_I2C_ALL_IRQ	(ALTERA_I2C_ISR_RXOF | ALTERA_I2C_ISR_ARB | \
41473c88f9SBruce Richardson 				 ALTERA_I2C_ISR_NACK | ALTERA_I2C_ISR_RXRDY | \
42473c88f9SBruce Richardson 				 ALTERA_I2C_ISR_TXRDY)
43473c88f9SBruce Richardson 
44473c88f9SBruce Richardson #define ALTERA_I2C_THRESHOLD	0
45473c88f9SBruce Richardson #define ALTERA_I2C_DFLT_FIFO_SZ	8
46473c88f9SBruce Richardson #define ALTERA_I2C_TIMEOUT_US  250000 /* 250ms */
47473c88f9SBruce Richardson 
48473c88f9SBruce Richardson #define I2C_PARAM 0x8
49473c88f9SBruce Richardson #define I2C_CTRL  0x10
50473c88f9SBruce Richardson #define I2C_CTRL_R    BIT_ULL(9)
51473c88f9SBruce Richardson #define I2C_CTRL_W    BIT_ULL(8)
52473c88f9SBruce Richardson #define I2C_CTRL_ADDR_MASK GENMASK_ULL(3, 0)
53473c88f9SBruce Richardson #define I2C_READ 0x18
54473c88f9SBruce Richardson #define I2C_READ_DATA_VALID BIT_ULL(32)
55473c88f9SBruce Richardson #define I2C_READ_DATA_MASK GENMASK_ULL(31, 0)
56473c88f9SBruce Richardson #define I2C_WRITE 0x20
57473c88f9SBruce Richardson #define I2C_WRITE_DATA_MASK GENMASK_ULL(31, 0)
58473c88f9SBruce Richardson 
59473c88f9SBruce Richardson #define ALTERA_I2C_100KHZ  0
60473c88f9SBruce Richardson #define ALTERA_I2C_400KHZ  1
61473c88f9SBruce Richardson 
62473c88f9SBruce Richardson /* i2c slave using 16bit address */
63473c88f9SBruce Richardson #define I2C_FLAG_ADDR16  1
64473c88f9SBruce Richardson 
65473c88f9SBruce Richardson #define I2C_XFER_RETRY 10
66473c88f9SBruce Richardson 
67473c88f9SBruce Richardson struct i2c_core_param {
68473c88f9SBruce Richardson 	union {
69473c88f9SBruce Richardson 		u64 info;
70473c88f9SBruce Richardson 		struct {
71473c88f9SBruce Richardson 			u16 fifo_depth:9;
72473c88f9SBruce Richardson 			u8 interface:1;
73473c88f9SBruce Richardson 			/*reference clock of I2C core in MHz*/
74473c88f9SBruce Richardson 			u32 ref_clk:10;
75473c88f9SBruce Richardson 			/*Max I2C interface freq*/
76473c88f9SBruce Richardson 			u8 max_req:4;
77473c88f9SBruce Richardson 			u64 devid:32;
78473c88f9SBruce Richardson 			/* number of MAC address*/
79473c88f9SBruce Richardson 			u8 nu_macs:8;
80473c88f9SBruce Richardson 		};
81473c88f9SBruce Richardson 	};
82473c88f9SBruce Richardson };
83473c88f9SBruce Richardson 
84473c88f9SBruce Richardson struct altera_i2c_dev {
85473c88f9SBruce Richardson 	u8 *base;
86473c88f9SBruce Richardson 	struct i2c_core_param i2c_param;
87473c88f9SBruce Richardson 	u32 fifo_size;
88473c88f9SBruce Richardson 	u32 bus_clk_rate; /* i2c bus clock */
89473c88f9SBruce Richardson 	u32 i2c_clk; /* i2c input clock */
90473c88f9SBruce Richardson 	struct i2c_msg *msg;
91473c88f9SBruce Richardson 	size_t msg_len;
92473c88f9SBruce Richardson 	int msg_err;
93473c88f9SBruce Richardson 	u32 isr_mask;
94473c88f9SBruce Richardson 	u8 *buf;
95473c88f9SBruce Richardson 	int (*xfer)(struct altera_i2c_dev *dev, struct i2c_msg *msg, int num);
964a19f891STianfei Zhang 	pthread_mutex_t lock;
97*e41856b5SWei Huang 	pthread_mutex_t *mutex;  /* multi-process mutex from adapter */
98473c88f9SBruce Richardson };
99473c88f9SBruce Richardson 
100473c88f9SBruce Richardson /**
101473c88f9SBruce Richardson  * struct i2c_msg: an I2C message
102473c88f9SBruce Richardson  */
103473c88f9SBruce Richardson struct i2c_msg {
104473c88f9SBruce Richardson 	unsigned int addr;
105473c88f9SBruce Richardson 	unsigned int flags;
106473c88f9SBruce Richardson 	unsigned int len;
107473c88f9SBruce Richardson 	u8 *buf;
108473c88f9SBruce Richardson };
109473c88f9SBruce Richardson 
110473c88f9SBruce Richardson #define I2C_MAX_OFFSET_LEN 4
111473c88f9SBruce Richardson 
112473c88f9SBruce Richardson enum i2c_msg_flags {
113473c88f9SBruce Richardson 	I2C_M_TEN = 0x0010, /*ten-bit chip address*/
114473c88f9SBruce Richardson 	I2C_M_RD  = 0x0001, /*read data*/
115473c88f9SBruce Richardson 	I2C_M_STOP = 0x8000, /*send stop after this message*/
116473c88f9SBruce Richardson };
117473c88f9SBruce Richardson 
118473c88f9SBruce Richardson struct altera_i2c_dev *altera_i2c_probe(void *base);
1194a19f891STianfei Zhang void altera_i2c_remove(struct altera_i2c_dev *dev);
120473c88f9SBruce Richardson int i2c_read(struct altera_i2c_dev *dev, int flags, unsigned int slave_addr,
121473c88f9SBruce Richardson 		u32 offset, u8 *buf, u32 count);
122473c88f9SBruce Richardson int i2c_write(struct altera_i2c_dev *dev, int flags, unsigned int slave_addr,
123473c88f9SBruce Richardson 		u32 offset, u8 *buffer, int len);
124473c88f9SBruce Richardson int i2c_read8(struct altera_i2c_dev *dev, unsigned int slave_addr, u32 offset,
125473c88f9SBruce Richardson 		u8 *buf, u32 count);
126473c88f9SBruce Richardson int i2c_read16(struct altera_i2c_dev *dev, unsigned int slave_addr, u32 offset,
127473c88f9SBruce Richardson 		u8 *buf, u32 count);
128473c88f9SBruce Richardson int i2c_write8(struct altera_i2c_dev *dev, unsigned int slave_addr, u32 offset,
129473c88f9SBruce Richardson 		u8 *buf, u32 count);
130473c88f9SBruce Richardson int i2c_write16(struct altera_i2c_dev *dev, unsigned int slave_addr, u32 offset,
131473c88f9SBruce Richardson 		u8 *buf, u32 count);
132473c88f9SBruce Richardson #endif
133