blob: 25121fa482c1e50df70048edcb5a237568898582 [file] [log] [blame]
j_mayer3cbee152007-10-28 23:42:18 +00001/*
2 * PowerMac NVRAM emulation
3 *
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw.h"
blueswir195efd112008-12-24 20:26:14 +000026#include "firmware_abi.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Andreas Färberbaec1912013-01-23 23:03:54 +000028#include "ppc/mac.h"
j_mayer3cbee152007-10-28 23:42:18 +000029
blueswir1ea026b22008-12-24 09:38:16 +000030/* debug NVR */
31//#define DEBUG_NVR
32
33#ifdef DEBUG_NVR
Blue Swirl001faf32009-05-13 17:53:17 +000034#define NVR_DPRINTF(fmt, ...) \
35 do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0)
blueswir1ea026b22008-12-24 09:38:16 +000036#else
Blue Swirl001faf32009-05-13 17:53:17 +000037#define NVR_DPRINTF(fmt, ...)
blueswir1ea026b22008-12-24 09:38:16 +000038#endif
39
aurel32bd89f432008-12-26 23:05:23 +000040#define DEF_SYSTEM_SIZE 0xc10
41
j_mayer3cbee152007-10-28 23:42:18 +000042/* Direct access to NVRAM */
Andreas Färber3743cca2013-01-23 23:03:58 +000043uint8_t macio_nvram_read(MacIONVRAMState *s, uint32_t addr)
j_mayer3cbee152007-10-28 23:42:18 +000044{
j_mayer3cbee152007-10-28 23:42:18 +000045 uint32_t ret;
46
Andreas Färber3743cca2013-01-23 23:03:58 +000047 if (addr < s->size) {
j_mayer3cbee152007-10-28 23:42:18 +000048 ret = s->data[addr];
Andreas Färber3743cca2013-01-23 23:03:58 +000049 } else {
j_mayer3cbee152007-10-28 23:42:18 +000050 ret = -1;
Andreas Färber3743cca2013-01-23 23:03:58 +000051 }
52 NVR_DPRINTF("read addr %04" PRIx32 " val %" PRIx8 "\n", addr, ret);
j_mayer3cbee152007-10-28 23:42:18 +000053
54 return ret;
55}
56
Andreas Färber3743cca2013-01-23 23:03:58 +000057void macio_nvram_write(MacIONVRAMState *s, uint32_t addr, uint8_t val)
j_mayer3cbee152007-10-28 23:42:18 +000058{
Andreas Färber3743cca2013-01-23 23:03:58 +000059 NVR_DPRINTF("write addr %04" PRIx32 " val %" PRIx8 "\n", addr, val);
60 if (addr < s->size) {
j_mayer3cbee152007-10-28 23:42:18 +000061 s->data[addr] = val;
Andreas Färber3743cca2013-01-23 23:03:58 +000062 }
j_mayer3cbee152007-10-28 23:42:18 +000063}
64
65/* macio style NVRAM device */
Avi Kivitya8170e52012-10-23 12:30:10 +020066static void macio_nvram_writeb(void *opaque, hwaddr addr,
Avi Kivity23c5e4c2011-08-08 16:09:17 +030067 uint64_t value, unsigned size)
j_mayer3cbee152007-10-28 23:42:18 +000068{
69 MacIONVRAMState *s = opaque;
j_mayer74e91152007-11-04 01:16:04 +000070
blueswir168af3f22009-02-07 10:48:26 +000071 addr = (addr >> s->it_shift) & (s->size - 1);
j_mayer3cbee152007-10-28 23:42:18 +000072 s->data[addr] = value;
Andreas Färber3743cca2013-01-23 23:03:58 +000073 NVR_DPRINTF("writeb addr %04" PHYS_PRIx " val %" PRIx64 "\n", addr, value);
j_mayer3cbee152007-10-28 23:42:18 +000074}
75
Avi Kivitya8170e52012-10-23 12:30:10 +020076static uint64_t macio_nvram_readb(void *opaque, hwaddr addr,
Avi Kivity23c5e4c2011-08-08 16:09:17 +030077 unsigned size)
j_mayer3cbee152007-10-28 23:42:18 +000078{
79 MacIONVRAMState *s = opaque;
80 uint32_t value;
81
blueswir168af3f22009-02-07 10:48:26 +000082 addr = (addr >> s->it_shift) & (s->size - 1);
j_mayer3cbee152007-10-28 23:42:18 +000083 value = s->data[addr];
blueswir1ea026b22008-12-24 09:38:16 +000084 NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
j_mayer3cbee152007-10-28 23:42:18 +000085
86 return value;
87}
88
Avi Kivity23c5e4c2011-08-08 16:09:17 +030089static const MemoryRegionOps macio_nvram_ops = {
90 .read = macio_nvram_readb,
91 .write = macio_nvram_writeb,
Andreas Färberd8c6d072013-01-23 23:03:59 +000092 .endianness = DEVICE_BIG_ENDIAN,
j_mayer3cbee152007-10-28 23:42:18 +000093};
94
Juan Quintela8e470f82010-12-03 01:59:09 +010095static const VMStateDescription vmstate_macio_nvram = {
96 .name = "macio_nvram",
97 .version_id = 1,
98 .minimum_version_id = 1,
99 .minimum_version_id_old = 1,
100 .fields = (VMStateField[]) {
101 VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size),
102 VMSTATE_END_OF_LIST()
103 }
104};
blueswir19b649972008-12-30 19:01:19 +0000105
blueswir19b649972008-12-30 19:01:19 +0000106
Andreas Färber95ed3b72013-01-23 23:04:00 +0000107static void macio_nvram_reset(DeviceState *dev)
blueswir16e6b7362008-12-28 18:27:10 +0000108{
109}
110
Andreas Färber95ed3b72013-01-23 23:04:00 +0000111static void macio_nvram_realizefn(DeviceState *dev, Error **errp)
j_mayer3cbee152007-10-28 23:42:18 +0000112{
Andreas Färber95ed3b72013-01-23 23:04:00 +0000113 SysBusDevice *d = SYS_BUS_DEVICE(dev);
114 MacIONVRAMState *s = MACIO_NVRAM(dev);
j_mayer74e91152007-11-04 01:16:04 +0000115
Andreas Färber95ed3b72013-01-23 23:04:00 +0000116 s->data = g_malloc0(s->size);
aurel323f7cbbb2008-12-26 23:05:15 +0000117
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300118 memory_region_init_io(&s->mem, &macio_nvram_ops, s, "macio-nvram",
Andreas Färber95ed3b72013-01-23 23:04:00 +0000119 s->size << s->it_shift);
120 sysbus_init_mmio(d, &s->mem);
j_mayer3cbee152007-10-28 23:42:18 +0000121}
122
Andreas Färber95ed3b72013-01-23 23:04:00 +0000123static void macio_nvram_unrealizefn(DeviceState *dev, Error **errp)
j_mayer74e91152007-11-04 01:16:04 +0000124{
Andreas Färber95ed3b72013-01-23 23:04:00 +0000125 MacIONVRAMState *s = MACIO_NVRAM(dev);
126
127 g_free(s->data);
128}
129
130static Property macio_nvram_properties[] = {
131 DEFINE_PROP_UINT32("size", MacIONVRAMState, size, 0),
132 DEFINE_PROP_UINT32("it_shift", MacIONVRAMState, it_shift, 0),
133 DEFINE_PROP_END_OF_LIST()
134};
135
136static void macio_nvram_class_init(ObjectClass *oc, void *data)
137{
138 DeviceClass *dc = DEVICE_CLASS(oc);
139
140 dc->realize = macio_nvram_realizefn;
141 dc->unrealize = macio_nvram_unrealizefn;
142 dc->reset = macio_nvram_reset;
143 dc->vmsd = &vmstate_macio_nvram;
144 dc->props = macio_nvram_properties;
145}
146
147static const TypeInfo macio_nvram_type_info = {
148 .name = TYPE_MACIO_NVRAM,
149 .parent = TYPE_SYS_BUS_DEVICE,
150 .instance_size = sizeof(MacIONVRAMState),
151 .class_init = macio_nvram_class_init,
152};
153
154static void macio_nvram_register_types(void)
155{
156 type_register_static(&macio_nvram_type_info);
j_mayer74e91152007-11-04 01:16:04 +0000157}
158
blueswir195efd112008-12-24 20:26:14 +0000159/* Set up a system OpenBIOS NVRAM partition */
j_mayer3cbee152007-10-28 23:42:18 +0000160void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
161{
blueswir195efd112008-12-24 20:26:14 +0000162 unsigned int i;
163 uint32_t start = 0, end;
164 struct OpenBIOS_nvpart_v1 *part_header;
j_mayer3cbee152007-10-28 23:42:18 +0000165
blueswir195efd112008-12-24 20:26:14 +0000166 // OpenBIOS nvram variables
167 // Variable partition
168 part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
169 part_header->signature = OPENBIOS_PART_SYSTEM;
170 pstrcpy(part_header->name, sizeof(part_header->name), "system");
171
172 end = start + sizeof(struct OpenBIOS_nvpart_v1);
173 for (i = 0; i < nb_prom_envs; i++)
174 end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]);
175
176 // End marker
177 nvr->data[end++] = '\0';
178
179 end = start + ((end - start + 15) & ~15);
aurel32bd89f432008-12-26 23:05:23 +0000180 /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
181 new variables. */
182 if (end < DEF_SYSTEM_SIZE)
183 end = DEF_SYSTEM_SIZE;
blueswir195efd112008-12-24 20:26:14 +0000184 OpenBIOS_finish_partition(part_header, end - start);
185
186 // free partition
187 start = end;
188 part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
189 part_header->signature = OPENBIOS_PART_FREE;
190 pstrcpy(part_header->name, sizeof(part_header->name), "free");
191
192 end = len;
193 OpenBIOS_finish_partition(part_header, end - start);
j_mayer3cbee152007-10-28 23:42:18 +0000194}
Andreas Färber95ed3b72013-01-23 23:04:00 +0000195
196type_init(macio_nvram_register_types)