Statistics
| Branch: | Revision:

root / hw / ppce500_pci.c @ 4f5e19e6

History | View | Annotate | Download (10.4 kB)

1
/*
2
 * QEMU PowerPC E500 embedded processors pci controller emulation
3
 *
4
 * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
5
 *
6
 * Author: Yu Liu,     <yu.liu@freescale.com>
7
 *
8
 * This file is derived from hw/ppc4xx_pci.c,
9
 * the copyright for that material belongs to the original owners.
10
 *
11
 * This is free software; you can redistribute it and/or modify
12
 * it under the terms of  the GNU General  Public License as published by
13
 * the Free Software Foundation;  either version 2 of the  License, or
14
 * (at your option) any later version.
15
 */
16

    
17
#include "hw.h"
18
#include "ppc.h"
19
#include "ppce500.h"
20
#include "pci.h"
21
#include "pci_host.h"
22
#include "bswap.h"
23
#include "qemu-log.h"
24

    
25
#ifdef DEBUG_PCI
26
#define pci_debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
27
#else
28
#define pci_debug(fmt, ...)
29
#endif
30

    
31
#define PCIE500_CFGADDR       0x0
32
#define PCIE500_CFGDATA       0x4
33
#define PCIE500_REG_BASE      0xC00
34
#define PCIE500_REG_SIZE      (0x1000 - PCIE500_REG_BASE)
35

    
36
#define PPCE500_PCI_CONFIG_ADDR         0x0
37
#define PPCE500_PCI_CONFIG_DATA         0x4
38
#define PPCE500_PCI_INTACK              0x8
39

    
40
#define PPCE500_PCI_OW1                 (0xC20 - PCIE500_REG_BASE)
41
#define PPCE500_PCI_OW2                 (0xC40 - PCIE500_REG_BASE)
42
#define PPCE500_PCI_OW3                 (0xC60 - PCIE500_REG_BASE)
43
#define PPCE500_PCI_OW4                 (0xC80 - PCIE500_REG_BASE)
44
#define PPCE500_PCI_IW3                 (0xDA0 - PCIE500_REG_BASE)
45
#define PPCE500_PCI_IW2                 (0xDC0 - PCIE500_REG_BASE)
46
#define PPCE500_PCI_IW1                 (0xDE0 - PCIE500_REG_BASE)
47

    
48
#define PPCE500_PCI_GASKET_TIMR         (0xE20 - PCIE500_REG_BASE)
49

    
50
#define PCI_POTAR               0x0
51
#define PCI_POTEAR              0x4
52
#define PCI_POWBAR              0x8
53
#define PCI_POWAR               0x10
54

    
55
#define PCI_PITAR               0x0
56
#define PCI_PIWBAR              0x8
57
#define PCI_PIWBEAR             0xC
58
#define PCI_PIWAR               0x10
59

    
60
#define PPCE500_PCI_NR_POBS     5
61
#define PPCE500_PCI_NR_PIBS     3
62

    
63
struct  pci_outbound {
64
    uint32_t potar;
65
    uint32_t potear;
66
    uint32_t powbar;
67
    uint32_t powar;
68
};
69

    
70
struct pci_inbound {
71
    uint32_t pitar;
72
    uint32_t piwbar;
73
    uint32_t piwbear;
74
    uint32_t piwar;
75
};
76

    
77
struct PPCE500PCIState {
78
    struct pci_outbound pob[PPCE500_PCI_NR_POBS];
79
    struct pci_inbound pib[PPCE500_PCI_NR_PIBS];
80
    uint32_t gasket_time;
81
    PCIHostState pci_state;
82
    PCIDevice *pci_dev;
83
};
84

    
85
typedef struct PPCE500PCIState PPCE500PCIState;
86

    
87
static uint32_t pcie500_cfgaddr_readl(void *opaque, target_phys_addr_t addr)
88
{
89
    PPCE500PCIState *pci = opaque;
90

    
91
    pci_debug("%s: (addr:" TARGET_FMT_plx ") -> value:%x\n", __func__, addr,
92
              pci->pci_state.config_reg);
93
    return pci->pci_state.config_reg;
94
}
95

    
96
static CPUReadMemoryFunc * const pcie500_cfgaddr_read[] = {
97
    &pcie500_cfgaddr_readl,
98
    &pcie500_cfgaddr_readl,
99
    &pcie500_cfgaddr_readl,
100
};
101

    
102
static void pcie500_cfgaddr_writel(void *opaque, target_phys_addr_t addr,
103
                                  uint32_t value)
104
{
105
    PPCE500PCIState *controller = opaque;
106

    
107
    pci_debug("%s: value:%x -> (addr:" TARGET_FMT_plx ")\n", __func__, value,
108
              addr);
109
    controller->pci_state.config_reg = value & ~0x3;
110
}
111

    
112
static CPUWriteMemoryFunc * const pcie500_cfgaddr_write[] = {
113
    &pcie500_cfgaddr_writel,
114
    &pcie500_cfgaddr_writel,
115
    &pcie500_cfgaddr_writel,
116
};
117

    
118
static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr)
119
{
120
    PPCE500PCIState *pci = opaque;
121
    unsigned long win;
122
    uint32_t value = 0;
123

    
124
    win = addr & 0xfe0;
125

    
126
    switch (win) {
127
    case PPCE500_PCI_OW1:
128
    case PPCE500_PCI_OW2:
129
    case PPCE500_PCI_OW3:
130
    case PPCE500_PCI_OW4:
131
        switch (addr & 0xC) {
132
        case PCI_POTAR: value = pci->pob[(addr >> 5) & 0x7].potar; break;
133
        case PCI_POTEAR: value = pci->pob[(addr >> 5) & 0x7].potear; break;
134
        case PCI_POWBAR: value = pci->pob[(addr >> 5) & 0x7].powbar; break;
135
        case PCI_POWAR: value = pci->pob[(addr >> 5) & 0x7].powar; break;
136
        default: break;
137
        }
138
        break;
139

    
140
    case PPCE500_PCI_IW3:
141
    case PPCE500_PCI_IW2:
142
    case PPCE500_PCI_IW1:
143
        switch (addr & 0xC) {
144
        case PCI_PITAR: value = pci->pib[(addr >> 5) & 0x3].pitar; break;
145
        case PCI_PIWBAR: value = pci->pib[(addr >> 5) & 0x3].piwbar; break;
146
        case PCI_PIWBEAR: value = pci->pib[(addr >> 5) & 0x3].piwbear; break;
147
        case PCI_PIWAR: value = pci->pib[(addr >> 5) & 0x3].piwar; break;
148
        default: break;
149
        };
150
        break;
151

    
152
    case PPCE500_PCI_GASKET_TIMR:
153
        value = pci->gasket_time;
154
        break;
155

    
156
    default:
157
        break;
158
    }
159

    
160
    pci_debug("%s: win:%lx(addr:" TARGET_FMT_plx ") -> value:%x\n", __func__,
161
              win, addr, value);
162
    return value;
163
}
164

    
165
static CPUReadMemoryFunc * const e500_pci_reg_read[] = {
166
    &pci_reg_read4,
167
    &pci_reg_read4,
168
    &pci_reg_read4,
169
};
170

    
171
static void pci_reg_write4(void *opaque, target_phys_addr_t addr,
172
                               uint32_t value)
173
{
174
    PPCE500PCIState *pci = opaque;
175
    unsigned long win;
176

    
177
    win = addr & 0xfe0;
178

    
179
    pci_debug("%s: value:%x -> win:%lx(addr:" TARGET_FMT_plx ")\n",
180
              __func__, value, win, addr);
181

    
182
    switch (win) {
183
    case PPCE500_PCI_OW1:
184
    case PPCE500_PCI_OW2:
185
    case PPCE500_PCI_OW3:
186
    case PPCE500_PCI_OW4:
187
        switch (addr & 0xC) {
188
        case PCI_POTAR: pci->pob[(addr >> 5) & 0x7].potar = value; break;
189
        case PCI_POTEAR: pci->pob[(addr >> 5) & 0x7].potear = value; break;
190
        case PCI_POWBAR: pci->pob[(addr >> 5) & 0x7].powbar = value; break;
191
        case PCI_POWAR: pci->pob[(addr >> 5) & 0x7].powar = value; break;
192
        default: break;
193
        };
194
        break;
195

    
196
    case PPCE500_PCI_IW3:
197
    case PPCE500_PCI_IW2:
198
    case PPCE500_PCI_IW1:
199
        switch (addr & 0xC) {
200
        case PCI_PITAR: pci->pib[(addr >> 5) & 0x3].pitar = value; break;
201
        case PCI_PIWBAR: pci->pib[(addr >> 5) & 0x3].piwbar = value; break;
202
        case PCI_PIWBEAR: pci->pib[(addr >> 5) & 0x3].piwbear = value; break;
203
        case PCI_PIWAR: pci->pib[(addr >> 5) & 0x3].piwar = value; break;
204
        default: break;
205
        };
206
        break;
207

    
208
    case PPCE500_PCI_GASKET_TIMR:
209
        pci->gasket_time = value;
210
        break;
211

    
212
    default:
213
        break;
214
    };
215
}
216

    
217
static CPUWriteMemoryFunc * const e500_pci_reg_write[] = {
218
    &pci_reg_write4,
219
    &pci_reg_write4,
220
    &pci_reg_write4,
221
};
222

    
223
static int mpc85xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
224
{
225
    int devno = pci_dev->devfn >> 3, ret = 0;
226

    
227
    switch (devno) {
228
        /* Two PCI slot */
229
        case 0x11:
230
        case 0x12:
231
            ret = (irq_num + devno - 0x10) % 4;
232
            break;
233
        default:
234
            printf("Error:%s:unknow dev number\n", __func__);
235
    }
236

    
237
    pci_debug("%s: devfn %x irq %d -> %d  devno:%x\n", __func__,
238
           pci_dev->devfn, irq_num, ret, devno);
239

    
240
    return ret;
241
}
242

    
243
static void mpc85xx_pci_set_irq(void *opaque, int irq_num, int level)
244
{
245
    qemu_irq *pic = opaque;
246

    
247
    pci_debug("%s: PCI irq %d, level:%d\n", __func__, irq_num, level);
248

    
249
    qemu_set_irq(pic[irq_num], level);
250
}
251

    
252
static void ppce500_pci_save(QEMUFile *f, void *opaque)
253
{
254
    PPCE500PCIState *controller = opaque;
255
    int i;
256

    
257
    pci_device_save(controller->pci_dev, f);
258

    
259
    for (i = 0; i < PPCE500_PCI_NR_POBS; i++) {
260
        qemu_put_be32s(f, &controller->pob[i].potar);
261
        qemu_put_be32s(f, &controller->pob[i].potear);
262
        qemu_put_be32s(f, &controller->pob[i].powbar);
263
        qemu_put_be32s(f, &controller->pob[i].powar);
264
    }
265

    
266
    for (i = 0; i < PPCE500_PCI_NR_PIBS; i++) {
267
        qemu_put_be32s(f, &controller->pib[i].pitar);
268
        qemu_put_be32s(f, &controller->pib[i].piwbar);
269
        qemu_put_be32s(f, &controller->pib[i].piwbear);
270
        qemu_put_be32s(f, &controller->pib[i].piwar);
271
    }
272
    qemu_put_be32s(f, &controller->gasket_time);
273
}
274

    
275
static int ppce500_pci_load(QEMUFile *f, void *opaque, int version_id)
276
{
277
    PPCE500PCIState *controller = opaque;
278
    int i;
279

    
280
    if (version_id != 1)
281
        return -EINVAL;
282

    
283
    pci_device_load(controller->pci_dev, f);
284

    
285
    for (i = 0; i < PPCE500_PCI_NR_POBS; i++) {
286
        qemu_get_be32s(f, &controller->pob[i].potar);
287
        qemu_get_be32s(f, &controller->pob[i].potear);
288
        qemu_get_be32s(f, &controller->pob[i].powbar);
289
        qemu_get_be32s(f, &controller->pob[i].powar);
290
    }
291

    
292
    for (i = 0; i < PPCE500_PCI_NR_PIBS; i++) {
293
        qemu_get_be32s(f, &controller->pib[i].pitar);
294
        qemu_get_be32s(f, &controller->pib[i].piwbar);
295
        qemu_get_be32s(f, &controller->pib[i].piwbear);
296
        qemu_get_be32s(f, &controller->pib[i].piwar);
297
    }
298
    qemu_get_be32s(f, &controller->gasket_time);
299

    
300
    return 0;
301
}
302

    
303
PCIBus *ppce500_pci_init(qemu_irq pci_irqs[4], target_phys_addr_t registers)
304
{
305
    PPCE500PCIState *controller;
306
    PCIDevice *d;
307
    int index;
308
    static int ppce500_pci_id;
309

    
310
    controller = qemu_mallocz(sizeof(PPCE500PCIState));
311

    
312
    controller->pci_state.bus = pci_register_bus(NULL, "pci",
313
                                                 mpc85xx_pci_set_irq,
314
                                                 mpc85xx_pci_map_irq,
315
                                                 pci_irqs, 0x88, 4);
316
    d = pci_register_device(controller->pci_state.bus,
317
                            "host bridge", sizeof(PCIDevice),
318
                            0, NULL, NULL);
319

    
320
    pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_FREESCALE);
321
    pci_config_set_device_id(d->config, PCI_DEVICE_ID_MPC8533E);
322
    pci_config_set_class(d->config, PCI_CLASS_PROCESSOR_POWERPC);
323

    
324
    controller->pci_dev = d;
325

    
326
    /* CFGADDR */
327
    index = cpu_register_io_memory(pcie500_cfgaddr_read,
328
                                   pcie500_cfgaddr_write, controller);
329
    if (index < 0)
330
        goto free;
331
    cpu_register_physical_memory(registers + PCIE500_CFGADDR, 4, index);
332

    
333
    /* CFGDATA */
334
    index = pci_host_data_register_io_memory(&controller->pci_state);
335
    if (index < 0)
336
        goto free;
337
    cpu_register_physical_memory(registers + PCIE500_CFGDATA, 4, index);
338

    
339
    index = cpu_register_io_memory(e500_pci_reg_read,
340
                                   e500_pci_reg_write, controller);
341
    if (index < 0)
342
        goto free;
343
    cpu_register_physical_memory(registers + PCIE500_REG_BASE,
344
                                   PCIE500_REG_SIZE, index);
345

    
346
    /* XXX load/save code not tested. */
347
    register_savevm("ppce500_pci", ppce500_pci_id++, 1,
348
                    ppce500_pci_save, ppce500_pci_load, controller);
349

    
350
    return controller->pci_state.bus;
351

    
352
free:
353
    printf("%s error\n", __func__);
354
    qemu_free(controller);
355
    return NULL;
356
}