PIIX4 SMBus host, EEPROM device emulation, by Ed Swierk.
[qemu] / hw / smbus_eeprom.c
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 "vl.h"
26
27 //#define DEBUG
28
29 typedef struct SMBusEEPROMDevice {
30     SMBusDevice dev;
31     uint8_t *data;
32     uint8_t offset;
33 } SMBusEEPROMDevice;
34
35 static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
36 {
37 #ifdef DEBUG
38     printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->addr, read);
39 #endif
40 }
41
42 static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
43 {
44     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
45 #ifdef DEBUG
46     printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", dev->addr, val);
47 #endif
48     eeprom->offset = val;
49 }
50
51 static uint8_t eeprom_receive_byte(SMBusDevice *dev)
52 {
53     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
54     uint8_t val = eeprom->data[eeprom->offset++];
55 #ifdef DEBUG
56     printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n", dev->addr, val);
57 #endif
58     return val;
59 }
60
61 static void eeprom_write_byte(SMBusDevice *dev, uint8_t cmd, uint8_t val)
62 {
63     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
64 #ifdef DEBUG
65     printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", dev->addr,
66            cmd, val);
67 #endif
68     eeprom->data[cmd] = val;
69 }
70
71 static uint8_t eeprom_read_byte(SMBusDevice *dev, uint8_t cmd)
72 {
73     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
74     uint8_t val = eeprom->data[cmd];
75 #ifdef DEBUG
76     printf("eeprom_read_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", dev->addr,
77            cmd, val);
78 #endif
79     return val;
80 }
81
82 SMBusDevice *smbus_eeprom_device_init(uint8_t addr, uint8_t *buf)
83 {
84     SMBusEEPROMDevice *eeprom = qemu_mallocz(sizeof(SMBusEEPROMDevice));
85     eeprom->dev.addr = addr;
86     eeprom->dev.quick_cmd = eeprom_quick_cmd;
87     eeprom->dev.send_byte = eeprom_send_byte;
88     eeprom->dev.receive_byte = eeprom_receive_byte;
89     eeprom->dev.write_byte = eeprom_write_byte;
90     eeprom->dev.read_byte = eeprom_read_byte;
91     eeprom->data = buf;
92     eeprom->offset = 0;
93     return (SMBusDevice *) eeprom;
94 }