root / hw / smbus_eeprom.c @ 1ffc346f
History | View | Annotate | Download (3.5 kB)
1 |
/*
|
---|---|
2 |
* QEMU SMBus EEPROM device
|
3 |
*
|
4 |
* Copyright (c) 2007 Arastra, Inc.
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
|
25 |
#include "hw.h" |
26 |
#include "i2c.h" |
27 |
#include "smbus.h" |
28 |
|
29 |
//#define DEBUG
|
30 |
|
31 |
typedef struct SMBusEEPROMDevice { |
32 |
SMBusDevice dev; |
33 |
uint8_t *data; |
34 |
uint8_t offset; |
35 |
} SMBusEEPROMDevice; |
36 |
|
37 |
static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read) |
38 |
{ |
39 |
#ifdef DEBUG
|
40 |
printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->addr, read);
|
41 |
#endif
|
42 |
} |
43 |
|
44 |
static void eeprom_send_byte(SMBusDevice *dev, uint8_t val) |
45 |
{ |
46 |
SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
47 |
#ifdef DEBUG
|
48 |
printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", dev->addr, val);
|
49 |
#endif
|
50 |
eeprom->offset = val; |
51 |
} |
52 |
|
53 |
static uint8_t eeprom_receive_byte(SMBusDevice *dev)
|
54 |
{ |
55 |
SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
56 |
uint8_t val = eeprom->data[eeprom->offset++]; |
57 |
#ifdef DEBUG
|
58 |
printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n", dev->addr, val);
|
59 |
#endif
|
60 |
return val;
|
61 |
} |
62 |
|
63 |
static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len) |
64 |
{ |
65 |
SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
66 |
int n;
|
67 |
#ifdef DEBUG
|
68 |
printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", dev->addr,
|
69 |
cmd, buf[0]);
|
70 |
#endif
|
71 |
/* An page write operation is not a valid SMBus command.
|
72 |
It is a block write without a length byte. Fortunately we
|
73 |
get the full block anyway. */
|
74 |
/* TODO: Should this set the current location? */
|
75 |
if (cmd + len > 256) |
76 |
n = 256 - cmd;
|
77 |
else
|
78 |
n = len; |
79 |
memcpy(eeprom->data + cmd, buf, n); |
80 |
len -= n; |
81 |
if (len)
|
82 |
memcpy(eeprom->data, buf + n, len); |
83 |
} |
84 |
|
85 |
static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n) |
86 |
{ |
87 |
SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
88 |
/* If this is the first byte then set the current position. */
|
89 |
if (n == 0) |
90 |
eeprom->offset = cmd; |
91 |
/* As with writes, we implement block reads without the
|
92 |
SMBus length byte. */
|
93 |
return eeprom_receive_byte(dev);
|
94 |
} |
95 |
|
96 |
void smbus_eeprom_device_init(i2c_bus *bus, uint8_t addr, uint8_t *buf)
|
97 |
{ |
98 |
SMBusEEPROMDevice *eeprom; |
99 |
|
100 |
eeprom = (SMBusEEPROMDevice *)smbus_device_init(bus, addr, |
101 |
sizeof(SMBusEEPROMDevice));
|
102 |
|
103 |
eeprom->dev.quick_cmd = eeprom_quick_cmd; |
104 |
eeprom->dev.send_byte = eeprom_send_byte; |
105 |
eeprom->dev.receive_byte = eeprom_receive_byte; |
106 |
eeprom->dev.write_data = eeprom_write_data; |
107 |
eeprom->dev.read_data = eeprom_read_data; |
108 |
eeprom->data = buf; |
109 |
eeprom->offset = 0;
|
110 |
} |