]>
Commit | Line | Data |
---|---|---|
3fffc223 TS |
1 | /* |
2 | * QEMU SMBus EEPROM device | |
5fafdf24 | 3 | * |
3fffc223 | 4 | * Copyright (c) 2007 Arastra, Inc. |
5fafdf24 | 5 | * |
3fffc223 TS |
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 | ||
87ecb68b PB |
25 | #include "hw.h" |
26 | #include "i2c.h" | |
27 | #include "smbus.h" | |
3fffc223 TS |
28 | |
29 | //#define DEBUG | |
30 | ||
31 | typedef struct SMBusEEPROMDevice { | |
1ea96673 | 32 | SMBusDevice smbusdev; |
bf2782d7 | 33 | void *data; |
3fffc223 TS |
34 | uint8_t offset; |
35 | } SMBusEEPROMDevice; | |
36 | ||
37 | static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read) | |
38 | { | |
39 | #ifdef DEBUG | |
ab7d9131 | 40 | printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read); |
3fffc223 TS |
41 | #endif |
42 | } | |
43 | ||
44 | static void eeprom_send_byte(SMBusDevice *dev, uint8_t val) | |
45 | { | |
46 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
47 | #ifdef DEBUG | |
ab7d9131 AZ |
48 | printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", |
49 | dev->i2c.address, val); | |
3fffc223 TS |
50 | #endif |
51 | eeprom->offset = val; | |
52 | } | |
53 | ||
54 | static uint8_t eeprom_receive_byte(SMBusDevice *dev) | |
55 | { | |
56 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
bf2782d7 GH |
57 | uint8_t *data = eeprom->data; |
58 | uint8_t val = data[eeprom->offset++]; | |
3fffc223 | 59 | #ifdef DEBUG |
ab7d9131 AZ |
60 | printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n", |
61 | dev->i2c.address, val); | |
3fffc223 TS |
62 | #endif |
63 | return val; | |
64 | } | |
65 | ||
0ff596d0 | 66 | static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len) |
3fffc223 TS |
67 | { |
68 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
0ff596d0 | 69 | int n; |
3fffc223 | 70 | #ifdef DEBUG |
ab7d9131 AZ |
71 | printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", |
72 | dev->i2c.address, cmd, buf[0]); | |
3fffc223 | 73 | #endif |
0ff596d0 PB |
74 | /* An page write operation is not a valid SMBus command. |
75 | It is a block write without a length byte. Fortunately we | |
76 | get the full block anyway. */ | |
77 | /* TODO: Should this set the current location? */ | |
78 | if (cmd + len > 256) | |
79 | n = 256 - cmd; | |
80 | else | |
81 | n = len; | |
82 | memcpy(eeprom->data + cmd, buf, n); | |
83 | len -= n; | |
84 | if (len) | |
85 | memcpy(eeprom->data, buf + n, len); | |
3fffc223 TS |
86 | } |
87 | ||
0ff596d0 | 88 | static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n) |
3fffc223 TS |
89 | { |
90 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
0ff596d0 PB |
91 | /* If this is the first byte then set the current position. */ |
92 | if (n == 0) | |
93 | eeprom->offset = cmd; | |
94 | /* As with writes, we implement block reads without the | |
95 | SMBus length byte. */ | |
96 | return eeprom_receive_byte(dev); | |
3fffc223 TS |
97 | } |
98 | ||
81a322d4 | 99 | static int smbus_eeprom_init(SMBusDevice *dev) |
3fffc223 | 100 | { |
1ea96673 | 101 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev; |
3b46e624 | 102 | |
3fffc223 | 103 | eeprom->offset = 0; |
81a322d4 | 104 | return 0; |
3fffc223 | 105 | } |
1ea96673 PB |
106 | |
107 | static SMBusDeviceInfo smbus_eeprom_info = { | |
074f2fff GH |
108 | .i2c.qdev.name = "smbus-eeprom", |
109 | .i2c.qdev.size = sizeof(SMBusEEPROMDevice), | |
ee6847d1 | 110 | .i2c.qdev.props = (Property[]) { |
bf2782d7 GH |
111 | DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data), |
112 | DEFINE_PROP_END_OF_LIST(), | |
ee6847d1 | 113 | }, |
1ea96673 PB |
114 | .init = smbus_eeprom_init, |
115 | .quick_cmd = eeprom_quick_cmd, | |
116 | .send_byte = eeprom_send_byte, | |
117 | .receive_byte = eeprom_receive_byte, | |
118 | .write_data = eeprom_write_data, | |
119 | .read_data = eeprom_read_data | |
120 | }; | |
121 | ||
122 | static void smbus_eeprom_register_devices(void) | |
123 | { | |
074f2fff | 124 | smbus_register_device(&smbus_eeprom_info); |
1ea96673 PB |
125 | } |
126 | ||
127 | device_init(smbus_eeprom_register_devices) |