7 * Copyright (c) 2007 Arastra, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/i2c/i2c.h"
30 #define TYPE_SMBUS_DEVICE "smbus-device"
31 #define SMBUS_DEVICE(obj) \
32 OBJECT_CHECK(SMBusDevice, (obj), TYPE_SMBUS_DEVICE)
33 #define SMBUS_DEVICE_CLASS(klass) \
34 OBJECT_CLASS_CHECK(SMBusDeviceClass, (klass), TYPE_SMBUS_DEVICE)
35 #define SMBUS_DEVICE_GET_CLASS(obj) \
36 OBJECT_GET_CLASS(SMBusDeviceClass, (obj), TYPE_SMBUS_DEVICE)
38 typedef struct SMBusDevice SMBusDevice
;
40 typedef struct SMBusDeviceClass
42 I2CSlaveClass parent_class
;
43 void (*quick_cmd
)(SMBusDevice
*dev
, uint8_t read
);
44 void (*send_byte
)(SMBusDevice
*dev
, uint8_t val
);
45 uint8_t (*receive_byte
)(SMBusDevice
*dev
);
46 /* We can't distinguish between a word write and a block write with
47 length 1, so pass the whole data block including the length byte
48 (if present). The device is responsible figuring out what type of
50 void (*write_data
)(SMBusDevice
*dev
, uint8_t cmd
, uint8_t *buf
, int len
);
51 /* Likewise we can't distinguish between different reads, or even know
52 the length of the read until the read is complete, so read data a
53 byte at a time. The device is responsible for adding the length
54 byte on block reads. */
55 uint8_t (*read_data
)(SMBusDevice
*dev
, uint8_t cmd
, int n
);
59 /* The SMBus protocol is implemented on top of I2C. */
62 /* Remaining fields for internal use only. */
65 uint8_t data_buf
[34]; /* command + len + 32 bytes of data. */
69 /* Master device commands. */
70 int smbus_quick_command(I2CBus
*bus
, uint8_t addr
, int read
);
71 int smbus_receive_byte(I2CBus
*bus
, uint8_t addr
);
72 int smbus_send_byte(I2CBus
*bus
, uint8_t addr
, uint8_t data
);
73 int smbus_read_byte(I2CBus
*bus
, uint8_t addr
, uint8_t command
);
74 int smbus_write_byte(I2CBus
*bus
, uint8_t addr
, uint8_t command
, uint8_t data
);
75 int smbus_read_word(I2CBus
*bus
, uint8_t addr
, uint8_t command
);
76 int smbus_write_word(I2CBus
*bus
, uint8_t addr
, uint8_t command
, uint16_t data
);
79 * Do a block transfer from an I2C device. If recv_len is set, then the
80 * first received byte is a length field and is used to know how much data
81 * to receive. Otherwise receive "len" bytes. If send_cmd is set, send
82 * the command byte first before receiving the data.
84 int smbus_read_block(I2CBus
*bus
, uint8_t addr
, uint8_t command
, uint8_t *data
,
85 int len
, bool recv_len
, bool send_cmd
);
88 * Do a block transfer to an I2C device. If send_len is set, send the
89 * "len" value before the data.
91 int smbus_write_block(I2CBus
*bus
, uint8_t addr
, uint8_t command
, uint8_t *data
,
92 int len
, bool send_len
);
94 void smbus_eeprom_init_one(I2CBus
*smbus
, uint8_t address
, uint8_t *eeprom_buf
);
95 void smbus_eeprom_init(I2CBus
*smbus
, int nb_eeprom
,
96 const uint8_t *eeprom_spd
, int size
);
98 enum sdram_type
{ SDR
= 0x4, DDR
= 0x7, DDR2
= 0x8 };
99 uint8_t *spd_data_generate(enum sdram_type type
, ram_addr_t size
, Error
**errp
);