Statistics
| Branch: | Revision:

root / hw / smbus_eeprom.c @ 079d0b7f

History | View | Annotate | Download (4.7 kB)

1 3fffc223 ths
/*
2 3fffc223 ths
 * QEMU SMBus EEPROM device
3 5fafdf24 ths
 *
4 3fffc223 ths
 * Copyright (c) 2007 Arastra, Inc.
5 5fafdf24 ths
 *
6 3fffc223 ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 3fffc223 ths
 * of this software and associated documentation files (the "Software"), to deal
8 3fffc223 ths
 * in the Software without restriction, including without limitation the rights
9 3fffc223 ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 3fffc223 ths
 * copies of the Software, and to permit persons to whom the Software is
11 3fffc223 ths
 * furnished to do so, subject to the following conditions:
12 3fffc223 ths
 *
13 3fffc223 ths
 * The above copyright notice and this permission notice shall be included in
14 3fffc223 ths
 * all copies or substantial portions of the Software.
15 3fffc223 ths
 *
16 3fffc223 ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 3fffc223 ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 3fffc223 ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 3fffc223 ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 3fffc223 ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 3fffc223 ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 3fffc223 ths
 * THE SOFTWARE.
23 3fffc223 ths
 */
24 3fffc223 ths
25 87ecb68b pbrook
#include "hw.h"
26 87ecb68b pbrook
#include "i2c.h"
27 87ecb68b pbrook
#include "smbus.h"
28 3fffc223 ths
29 3fffc223 ths
//#define DEBUG
30 3fffc223 ths
31 3fffc223 ths
typedef struct SMBusEEPROMDevice {
32 1ea96673 Paul Brook
    SMBusDevice smbusdev;
33 bf2782d7 Gerd Hoffmann
    void *data;
34 3fffc223 ths
    uint8_t offset;
35 3fffc223 ths
} SMBusEEPROMDevice;
36 3fffc223 ths
37 3fffc223 ths
static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
38 3fffc223 ths
{
39 3fffc223 ths
#ifdef DEBUG
40 ab7d9131 balrog
    printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
41 3fffc223 ths
#endif
42 3fffc223 ths
}
43 3fffc223 ths
44 3fffc223 ths
static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
45 3fffc223 ths
{
46 3fffc223 ths
    SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
47 3fffc223 ths
#ifdef DEBUG
48 ab7d9131 balrog
    printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n",
49 ab7d9131 balrog
           dev->i2c.address, val);
50 3fffc223 ths
#endif
51 3fffc223 ths
    eeprom->offset = val;
52 3fffc223 ths
}
53 3fffc223 ths
54 3fffc223 ths
static uint8_t eeprom_receive_byte(SMBusDevice *dev)
55 3fffc223 ths
{
56 3fffc223 ths
    SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
57 bf2782d7 Gerd Hoffmann
    uint8_t *data = eeprom->data;
58 bf2782d7 Gerd Hoffmann
    uint8_t val = data[eeprom->offset++];
59 3fffc223 ths
#ifdef DEBUG
60 ab7d9131 balrog
    printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
61 ab7d9131 balrog
           dev->i2c.address, val);
62 3fffc223 ths
#endif
63 3fffc223 ths
    return val;
64 3fffc223 ths
}
65 3fffc223 ths
66 0ff596d0 pbrook
static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
67 3fffc223 ths
{
68 3fffc223 ths
    SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
69 0ff596d0 pbrook
    int n;
70 3fffc223 ths
#ifdef DEBUG
71 ab7d9131 balrog
    printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
72 ab7d9131 balrog
           dev->i2c.address, cmd, buf[0]);
73 3fffc223 ths
#endif
74 0ff596d0 pbrook
    /* An page write operation is not a valid SMBus command.
75 0ff596d0 pbrook
       It is a block write without a length byte.  Fortunately we
76 0ff596d0 pbrook
       get the full block anyway.  */
77 0ff596d0 pbrook
    /* TODO: Should this set the current location?  */
78 0ff596d0 pbrook
    if (cmd + len > 256)
79 0ff596d0 pbrook
        n = 256 - cmd;
80 0ff596d0 pbrook
    else
81 0ff596d0 pbrook
        n = len;
82 0ff596d0 pbrook
    memcpy(eeprom->data + cmd, buf, n);
83 0ff596d0 pbrook
    len -= n;
84 0ff596d0 pbrook
    if (len)
85 0ff596d0 pbrook
        memcpy(eeprom->data, buf + n, len);
86 3fffc223 ths
}
87 3fffc223 ths
88 0ff596d0 pbrook
static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
89 3fffc223 ths
{
90 3fffc223 ths
    SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
91 0ff596d0 pbrook
    /* If this is the first byte then set the current position.  */
92 0ff596d0 pbrook
    if (n == 0)
93 0ff596d0 pbrook
        eeprom->offset = cmd;
94 0ff596d0 pbrook
    /* As with writes, we implement block reads without the
95 0ff596d0 pbrook
       SMBus length byte.  */
96 0ff596d0 pbrook
    return eeprom_receive_byte(dev);
97 3fffc223 ths
}
98 3fffc223 ths
99 a88df0b9 Isaku Yamahata
static int smbus_eeprom_initfn(SMBusDevice *dev)
100 3fffc223 ths
{
101 1ea96673 Paul Brook
    SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
102 3b46e624 ths
103 3fffc223 ths
    eeprom->offset = 0;
104 81a322d4 Gerd Hoffmann
    return 0;
105 3fffc223 ths
}
106 1ea96673 Paul Brook
107 39bffca2 Anthony Liguori
static Property smbus_eeprom_properties[] = {
108 39bffca2 Anthony Liguori
    DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data),
109 39bffca2 Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
110 39bffca2 Anthony Liguori
};
111 39bffca2 Anthony Liguori
112 b5ea9327 Anthony Liguori
static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data)
113 b5ea9327 Anthony Liguori
{
114 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
115 b5ea9327 Anthony Liguori
    SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass);
116 b5ea9327 Anthony Liguori
117 b5ea9327 Anthony Liguori
    sc->init = smbus_eeprom_initfn;
118 b5ea9327 Anthony Liguori
    sc->quick_cmd = eeprom_quick_cmd;
119 b5ea9327 Anthony Liguori
    sc->send_byte = eeprom_send_byte;
120 b5ea9327 Anthony Liguori
    sc->receive_byte = eeprom_receive_byte;
121 b5ea9327 Anthony Liguori
    sc->write_data = eeprom_write_data;
122 b5ea9327 Anthony Liguori
    sc->read_data = eeprom_read_data;
123 39bffca2 Anthony Liguori
    dc->props = smbus_eeprom_properties;
124 b5ea9327 Anthony Liguori
}
125 b5ea9327 Anthony Liguori
126 39bffca2 Anthony Liguori
static TypeInfo smbus_eeprom_info = {
127 39bffca2 Anthony Liguori
    .name          = "smbus-eeprom",
128 39bffca2 Anthony Liguori
    .parent        = TYPE_SMBUS_DEVICE,
129 39bffca2 Anthony Liguori
    .instance_size = sizeof(SMBusEEPROMDevice),
130 39bffca2 Anthony Liguori
    .class_init    = smbus_eeprom_class_initfn,
131 1ea96673 Paul Brook
};
132 1ea96673 Paul Brook
133 1ea96673 Paul Brook
static void smbus_eeprom_register_devices(void)
134 1ea96673 Paul Brook
{
135 39bffca2 Anthony Liguori
    type_register_static(&smbus_eeprom_info);
136 1ea96673 Paul Brook
}
137 1ea96673 Paul Brook
138 1ea96673 Paul Brook
device_init(smbus_eeprom_register_devices)
139 a88df0b9 Isaku Yamahata
140 a88df0b9 Isaku Yamahata
void smbus_eeprom_init(i2c_bus *smbus, int nb_eeprom,
141 a88df0b9 Isaku Yamahata
                       const uint8_t *eeprom_spd, int eeprom_spd_size)
142 a88df0b9 Isaku Yamahata
{
143 a88df0b9 Isaku Yamahata
    int i;
144 7267c094 Anthony Liguori
    uint8_t *eeprom_buf = g_malloc0(8 * 256); /* XXX: make this persistent */
145 a88df0b9 Isaku Yamahata
    if (eeprom_spd_size > 0) {
146 a88df0b9 Isaku Yamahata
        memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
147 a88df0b9 Isaku Yamahata
    }
148 a88df0b9 Isaku Yamahata
149 a88df0b9 Isaku Yamahata
    for (i = 0; i < nb_eeprom; i++) {
150 a88df0b9 Isaku Yamahata
        DeviceState *eeprom;
151 a88df0b9 Isaku Yamahata
        eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
152 a88df0b9 Isaku Yamahata
        qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
153 a88df0b9 Isaku Yamahata
        qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
154 a88df0b9 Isaku Yamahata
        qdev_init_nofail(eeprom);
155 a88df0b9 Isaku Yamahata
    }
156 a88df0b9 Isaku Yamahata
}