ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU SMBus EEPROM device |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 4 | * Copyright (c) 2007 Arastra, Inc. |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 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 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 25 | #include "hw.h" |
| 26 | #include "i2c.h" |
| 27 | #include "smbus.h" |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 28 | |
| 29 | //#define DEBUG |
| 30 | |
| 31 | typedef struct SMBusEEPROMDevice { |
Paul Brook | 1ea9667 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 32 | SMBusDevice smbusdev; |
Gerd Hoffmann | bf2782d | 2009-08-03 17:35:33 +0200 | [diff] [blame] | 33 | void *data; |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 34 | uint8_t offset; |
| 35 | } SMBusEEPROMDevice; |
| 36 | |
| 37 | static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read) |
| 38 | { |
| 39 | #ifdef DEBUG |
balrog | ab7d913 | 2008-06-02 01:48:27 +0000 | [diff] [blame] | 40 | printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read); |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 41 | #endif |
| 42 | } |
| 43 | |
| 44 | static void eeprom_send_byte(SMBusDevice *dev, uint8_t val) |
| 45 | { |
| 46 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
| 47 | #ifdef DEBUG |
balrog | ab7d913 | 2008-06-02 01:48:27 +0000 | [diff] [blame] | 48 | printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", |
| 49 | dev->i2c.address, val); |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 50 | #endif |
| 51 | eeprom->offset = val; |
| 52 | } |
| 53 | |
| 54 | static uint8_t eeprom_receive_byte(SMBusDevice *dev) |
| 55 | { |
| 56 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
Gerd Hoffmann | bf2782d | 2009-08-03 17:35:33 +0200 | [diff] [blame] | 57 | uint8_t *data = eeprom->data; |
| 58 | uint8_t val = data[eeprom->offset++]; |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 59 | #ifdef DEBUG |
balrog | ab7d913 | 2008-06-02 01:48:27 +0000 | [diff] [blame] | 60 | printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n", |
| 61 | dev->i2c.address, val); |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 62 | #endif |
| 63 | return val; |
| 64 | } |
| 65 | |
pbrook | 0ff596d | 2007-05-23 00:03:59 +0000 | [diff] [blame] | 66 | static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len) |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 67 | { |
| 68 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
pbrook | 0ff596d | 2007-05-23 00:03:59 +0000 | [diff] [blame] | 69 | int n; |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 70 | #ifdef DEBUG |
balrog | ab7d913 | 2008-06-02 01:48:27 +0000 | [diff] [blame] | 71 | printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", |
| 72 | dev->i2c.address, cmd, buf[0]); |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 73 | #endif |
pbrook | 0ff596d | 2007-05-23 00:03:59 +0000 | [diff] [blame] | 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); |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
pbrook | 0ff596d | 2007-05-23 00:03:59 +0000 | [diff] [blame] | 88 | static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n) |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 89 | { |
| 90 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; |
pbrook | 0ff596d | 2007-05-23 00:03:59 +0000 | [diff] [blame] | 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); |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Isaku Yamahata | a88df0b | 2011-04-05 11:07:06 +0900 | [diff] [blame] | 99 | static int smbus_eeprom_initfn(SMBusDevice *dev) |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 100 | { |
Paul Brook | 1ea9667 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 101 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 102 | |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 103 | eeprom->offset = 0; |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 104 | return 0; |
ths | 3fffc22 | 2007-02-02 03:13:18 +0000 | [diff] [blame] | 105 | } |
Paul Brook | 1ea9667 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 106 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 107 | static Property smbus_eeprom_properties[] = { |
| 108 | DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data), |
| 109 | DEFINE_PROP_END_OF_LIST(), |
| 110 | }; |
| 111 | |
Anthony Liguori | b5ea932 | 2011-12-04 20:39:20 -0600 | [diff] [blame] | 112 | static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data) |
| 113 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 114 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | b5ea932 | 2011-12-04 20:39:20 -0600 | [diff] [blame] | 115 | SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass); |
| 116 | |
| 117 | sc->init = smbus_eeprom_initfn; |
| 118 | sc->quick_cmd = eeprom_quick_cmd; |
| 119 | sc->send_byte = eeprom_send_byte; |
| 120 | sc->receive_byte = eeprom_receive_byte; |
| 121 | sc->write_data = eeprom_write_data; |
| 122 | sc->read_data = eeprom_read_data; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 123 | dc->props = smbus_eeprom_properties; |
Anthony Liguori | b5ea932 | 2011-12-04 20:39:20 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 126 | static TypeInfo smbus_eeprom_info = { |
| 127 | .name = "smbus-eeprom", |
| 128 | .parent = TYPE_SMBUS_DEVICE, |
| 129 | .instance_size = sizeof(SMBusEEPROMDevice), |
| 130 | .class_init = smbus_eeprom_class_initfn, |
Paul Brook | 1ea9667 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 133 | static void smbus_eeprom_register_types(void) |
Paul Brook | 1ea9667 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 134 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 135 | type_register_static(&smbus_eeprom_info); |
Paul Brook | 1ea9667 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 138 | type_init(smbus_eeprom_register_types) |
Isaku Yamahata | a88df0b | 2011-04-05 11:07:06 +0900 | [diff] [blame] | 139 | |
| 140 | void smbus_eeprom_init(i2c_bus *smbus, int nb_eeprom, |
| 141 | const uint8_t *eeprom_spd, int eeprom_spd_size) |
| 142 | { |
| 143 | int i; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 144 | uint8_t *eeprom_buf = g_malloc0(8 * 256); /* XXX: make this persistent */ |
Isaku Yamahata | a88df0b | 2011-04-05 11:07:06 +0900 | [diff] [blame] | 145 | if (eeprom_spd_size > 0) { |
| 146 | memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size); |
| 147 | } |
| 148 | |
| 149 | for (i = 0; i < nb_eeprom; i++) { |
| 150 | DeviceState *eeprom; |
| 151 | eeprom = qdev_create((BusState *)smbus, "smbus-eeprom"); |
| 152 | qdev_prop_set_uint8(eeprom, "address", 0x50 + i); |
| 153 | qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256)); |
| 154 | qdev_init_nofail(eeprom); |
| 155 | } |
| 156 | } |