root / hw / smbus_eeprom.c @ 008ff9d7
History | View | Annotate | Download (3.5 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 | 3fffc223 | ths | #include "vl.h" |
26 | 3fffc223 | ths | |
27 | 3fffc223 | ths | //#define DEBUG
|
28 | 3fffc223 | ths | |
29 | 3fffc223 | ths | typedef struct SMBusEEPROMDevice { |
30 | 3fffc223 | ths | SMBusDevice dev; |
31 | 3fffc223 | ths | uint8_t *data; |
32 | 3fffc223 | ths | uint8_t offset; |
33 | 3fffc223 | ths | } SMBusEEPROMDevice; |
34 | 3fffc223 | ths | |
35 | 3fffc223 | ths | static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read) |
36 | 3fffc223 | ths | { |
37 | 3fffc223 | ths | #ifdef DEBUG
|
38 | 3fffc223 | ths | printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->addr, read);
|
39 | 3fffc223 | ths | #endif
|
40 | 3fffc223 | ths | } |
41 | 3fffc223 | ths | |
42 | 3fffc223 | ths | static void eeprom_send_byte(SMBusDevice *dev, uint8_t val) |
43 | 3fffc223 | ths | { |
44 | 3fffc223 | ths | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
45 | 3fffc223 | ths | #ifdef DEBUG
|
46 | 3fffc223 | ths | printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", dev->addr, val);
|
47 | 3fffc223 | ths | #endif
|
48 | 3fffc223 | ths | eeprom->offset = val; |
49 | 3fffc223 | ths | } |
50 | 3fffc223 | ths | |
51 | 3fffc223 | ths | static uint8_t eeprom_receive_byte(SMBusDevice *dev)
|
52 | 3fffc223 | ths | { |
53 | 3fffc223 | ths | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
54 | 3fffc223 | ths | uint8_t val = eeprom->data[eeprom->offset++]; |
55 | 3fffc223 | ths | #ifdef DEBUG
|
56 | 3fffc223 | ths | printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n", dev->addr, val);
|
57 | 3fffc223 | ths | #endif
|
58 | 3fffc223 | ths | return val;
|
59 | 3fffc223 | ths | } |
60 | 3fffc223 | ths | |
61 | 0ff596d0 | pbrook | static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len) |
62 | 3fffc223 | ths | { |
63 | 3fffc223 | ths | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
64 | 0ff596d0 | pbrook | int n;
|
65 | 3fffc223 | ths | #ifdef DEBUG
|
66 | 3fffc223 | ths | printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", dev->addr,
|
67 | 0ff596d0 | pbrook | cmd, buf[0]);
|
68 | 3fffc223 | ths | #endif
|
69 | 0ff596d0 | pbrook | /* An page write operation is not a valid SMBus command.
|
70 | 0ff596d0 | pbrook | It is a block write without a length byte. Fortunately we
|
71 | 0ff596d0 | pbrook | get the full block anyway. */
|
72 | 0ff596d0 | pbrook | /* TODO: Should this set the current location? */
|
73 | 0ff596d0 | pbrook | if (cmd + len > 256) |
74 | 0ff596d0 | pbrook | n = 256 - cmd;
|
75 | 0ff596d0 | pbrook | else
|
76 | 0ff596d0 | pbrook | n = len; |
77 | 0ff596d0 | pbrook | memcpy(eeprom->data + cmd, buf, n); |
78 | 0ff596d0 | pbrook | len -= n; |
79 | 0ff596d0 | pbrook | if (len)
|
80 | 0ff596d0 | pbrook | memcpy(eeprom->data, buf + n, len); |
81 | 3fffc223 | ths | } |
82 | 3fffc223 | ths | |
83 | 0ff596d0 | pbrook | static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n) |
84 | 3fffc223 | ths | { |
85 | 3fffc223 | ths | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
86 | 0ff596d0 | pbrook | /* If this is the first byte then set the current position. */
|
87 | 0ff596d0 | pbrook | if (n == 0) |
88 | 0ff596d0 | pbrook | eeprom->offset = cmd; |
89 | 0ff596d0 | pbrook | /* As with writes, we implement block reads without the
|
90 | 0ff596d0 | pbrook | SMBus length byte. */
|
91 | 0ff596d0 | pbrook | return eeprom_receive_byte(dev);
|
92 | 3fffc223 | ths | } |
93 | 3fffc223 | ths | |
94 | 0ff596d0 | pbrook | void smbus_eeprom_device_init(i2c_bus *bus, uint8_t addr, uint8_t *buf)
|
95 | 3fffc223 | ths | { |
96 | 0ff596d0 | pbrook | SMBusEEPROMDevice *eeprom; |
97 | 3b46e624 | ths | |
98 | 0ff596d0 | pbrook | eeprom = (SMBusEEPROMDevice *)smbus_device_init(bus, addr, |
99 | 0ff596d0 | pbrook | sizeof(SMBusEEPROMDevice));
|
100 | 0ff596d0 | pbrook | |
101 | 3fffc223 | ths | eeprom->dev.quick_cmd = eeprom_quick_cmd; |
102 | 3fffc223 | ths | eeprom->dev.send_byte = eeprom_send_byte; |
103 | 3fffc223 | ths | eeprom->dev.receive_byte = eeprom_receive_byte; |
104 | 0ff596d0 | pbrook | eeprom->dev.write_data = eeprom_write_data; |
105 | 0ff596d0 | pbrook | eeprom->dev.read_data = eeprom_read_data; |
106 | 3fffc223 | ths | eeprom->data = buf; |
107 | 3fffc223 | ths | eeprom->offset = 0;
|
108 | 3fffc223 | ths | } |