iPXE
i2c.h
Go to the documentation of this file.
1#ifndef _IPXE_I2C_H
2#define _IPXE_I2C_H
3
4/** @file
5 *
6 * I2C interface
7 *
8 */
9
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
12#include <stdint.h>
13#include <ipxe/bitbash.h>
14
15/** An I2C device
16 *
17 * An I2C device represents a specific slave device on an I2C bus. It
18 * is accessed via an I2C interface.
19 */
20struct i2c_device {
21 /** Address of this device
22 *
23 * The actual address sent on the bus will look like
24 *
25 * <start> <device address> <word address overflow> <r/w>
26 *
27 * The "word address overflow" is any excess bits from the
28 * word address, i.e. any portion that does not fit within the
29 * defined word address length.
30 */
31 unsigned int dev_addr;
32 /** Device address length, in bytes
33 *
34 * This is the number of bytes that comprise the device
35 * address, defined to be the portion that terminates with the
36 * read/write bit.
37 */
38 unsigned int dev_addr_len;
39 /** Word adddress length, in bytes
40 *
41 * This is the number of bytes that comprise the word address,
42 * defined to be the portion that starts after the read/write
43 * bit and ends before the first data byte.
44 *
45 * For some devices, this length will be zero (i.e. the word
46 * address is contained entirely within the "word address
47 * overflow").
48 */
49 unsigned int word_addr_len;
50};
51
52/** An I2C interface
53 *
54 * An I2C interface provides access to an I2C bus, via which I2C
55 * devices may be reached.
56 */
58 /**
59 * Read data from I2C device
60 *
61 * @v i2c I2C interface
62 * @v i2cdev I2C device
63 * @v offset Starting offset within the device
64 * @v data Data buffer
65 * @v len Length of data buffer
66 * @ret rc Return status code
67 */
68 int ( * read ) ( struct i2c_interface *i2c, struct i2c_device *i2cdev,
69 unsigned int offset, uint8_t *data,
70 unsigned int len );
71 /**
72 * Write data to I2C device
73 *
74 * @v i2c I2C interface
75 * @v i2cdev I2C device
76 * @v offset Starting offset within the device
77 * @v data Data buffer
78 * @v len Length of data buffer
79 * @ret rc Return status code
80 */
81 int ( * write ) ( struct i2c_interface *i2c, struct i2c_device *i2cdev,
82 unsigned int offset, const uint8_t *data,
83 unsigned int len );
84};
85
86/** A bit-bashing I2C interface
87 *
88 * This provides a standardised way to construct I2C buses via a
89 * bit-bashing interface.
90 */
92 /** I2C interface */
94 /** Bit-bashing interface */
96};
97
98/** Ten-bit address marker
99 *
100 * This value is ORed with the I2C device address to indicate a
101 * ten-bit address format on the bus.
102 */
103#define I2C_TENBIT_ADDRESS 0x7800
104
105/** An I2C write command */
106#define I2C_WRITE 0
107
108/** An I2C read command */
109#define I2C_READ 1
110
111/** Bit indices used for I2C bit-bashing interface */
112enum {
113 /** Serial clock */
115 /** Serial data */
117};
118
119/** Delay required for bit-bashing operation */
120#define I2C_UDELAY 5
121
122/** Maximum number of cycles to use when attempting a bus reset */
123#define I2C_RESET_MAX_CYCLES 32
124
125/**
126 * Check presence of I2C device
127 *
128 * @v i2c I2C interface
129 * @v i2cdev I2C device
130 * @ret rc Return status code
131 *
132 * Checks for the presence of the device on the I2C bus by attempting
133 * a zero-length write.
134 */
135static inline int i2c_check_presence ( struct i2c_interface *i2c,
136 struct i2c_device *i2cdev ) {
137 return i2c->write ( i2c, i2cdev, 0, NULL, 0 );
138}
139
140extern int init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit,
141 struct bit_basher_operations *bash_op );
142
143/**
144 * Initialise generic I2C EEPROM device
145 *
146 * @v i2cdev I2C device
147 */
148static inline __always_inline void
149init_i2c_eeprom ( struct i2c_device *i2cdev, unsigned int dev_addr ) {
150 i2cdev->dev_addr = dev_addr;
151 i2cdev->dev_addr_len = 1;
152 i2cdev->word_addr_len = 1;
153}
154
155/**
156 * Initialise Atmel AT24C11
157 *
158 * @v i2cdev I2C device
159 */
160static inline __always_inline void
161init_at24c11 ( struct i2c_device *i2cdev ) {
162 /* This chip has no device address; it must be the only chip
163 * on the bus. The word address is contained entirely within
164 * the device address field.
165 */
166 i2cdev->dev_addr = 0;
167 i2cdev->dev_addr_len = 1;
168 i2cdev->word_addr_len = 0;
169}
170
171#endif /* _IPXE_I2C_H */
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned char uint8_t
Definition stdint.h:10
Bit-bashing interfaces.
uint16_t offset
Offset to command line.
Definition bzimage.h:3
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define __always_inline
Declare a function to be always inline.
Definition compiler.h:611
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
static int i2c_check_presence(struct i2c_interface *i2c, struct i2c_device *i2cdev)
Check presence of I2C device.
Definition i2c.h:135
int init_i2c_bit_basher(struct i2c_bit_basher *i2cbit, struct bit_basher_operations *bash_op)
Initialise I2C bit-bashing interface.
Definition i2c_bit.c:387
static __always_inline void init_at24c11(struct i2c_device *i2cdev)
Initialise Atmel AT24C11.
Definition i2c.h:161
@ I2C_BIT_SDA
Serial data.
Definition i2c.h:116
@ I2C_BIT_SCL
Serial clock.
Definition i2c.h:114
static __always_inline void init_i2c_eeprom(struct i2c_device *i2cdev, unsigned int dev_addr)
Initialise generic I2C EEPROM device.
Definition i2c.h:149
Bit-bashing operations.
Definition bitbash.h:16
A bit-bashing interface.
Definition bitbash.h:56
A bit-bashing I2C interface.
Definition i2c.h:91
struct i2c_interface i2c
I2C interface.
Definition i2c.h:93
struct bit_basher basher
Bit-bashing interface.
Definition i2c.h:95
An I2C device.
Definition i2c.h:20
unsigned int dev_addr
Address of this device.
Definition i2c.h:31
unsigned int word_addr_len
Word adddress length, in bytes.
Definition i2c.h:49
unsigned int dev_addr_len
Device address length, in bytes.
Definition i2c.h:38
An I2C interface.
Definition i2c.h:57
int(* write)(struct i2c_interface *i2c, struct i2c_device *i2cdev, unsigned int offset, const uint8_t *data, unsigned int len)
Write data to I2C device.
Definition i2c.h:81
int(* read)(struct i2c_interface *i2c, struct i2c_device *i2cdev, unsigned int offset, uint8_t *data, unsigned int len)
Read data from I2C device.
Definition i2c.h:68