2 * QEMU PowerPC PowerNV LPC controller
4 * Copyright (c) 2016, IBM Corporation.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "sysemu/sysemu.h"
22 #include "target/ppc/cpu.h"
23 #include "qapi/error.h"
26 #include "hw/ppc/pnv.h"
27 #include "hw/ppc/pnv_lpc.h"
28 #include "hw/ppc/pnv_xscom.h"
29 #include "hw/ppc/fdt.h"
40 /* OPB Master LS registers */
41 #define OPB_MASTER_LS_IRQ_STAT 0x50
42 #define OPB_MASTER_IRQ_LPC 0x00000800
43 #define OPB_MASTER_LS_IRQ_MASK 0x54
44 #define OPB_MASTER_LS_IRQ_POL 0x58
45 #define OPB_MASTER_LS_IRQ_INPUT 0x5c
47 /* LPC HC registers */
48 #define LPC_HC_FW_SEG_IDSEL 0x24
49 #define LPC_HC_FW_RD_ACC_SIZE 0x28
50 #define LPC_HC_FW_RD_1B 0x00000000
51 #define LPC_HC_FW_RD_2B 0x01000000
52 #define LPC_HC_FW_RD_4B 0x02000000
53 #define LPC_HC_FW_RD_16B 0x04000000
54 #define LPC_HC_FW_RD_128B 0x07000000
55 #define LPC_HC_IRQSER_CTRL 0x30
56 #define LPC_HC_IRQSER_EN 0x80000000
57 #define LPC_HC_IRQSER_QMODE 0x40000000
58 #define LPC_HC_IRQSER_START_MASK 0x03000000
59 #define LPC_HC_IRQSER_START_4CLK 0x00000000
60 #define LPC_HC_IRQSER_START_6CLK 0x01000000
61 #define LPC_HC_IRQSER_START_8CLK 0x02000000
62 #define LPC_HC_IRQMASK 0x34 /* same bit defs as LPC_HC_IRQSTAT */
63 #define LPC_HC_IRQSTAT 0x38
64 #define LPC_HC_IRQ_SERIRQ0 0x80000000 /* all bits down to ... */
65 #define LPC_HC_IRQ_SERIRQ16 0x00008000 /* IRQ16=IOCHK#, IRQ2=SMI# */
66 #define LPC_HC_IRQ_SERIRQ_ALL 0xffff8000
67 #define LPC_HC_IRQ_LRESET 0x00000400
68 #define LPC_HC_IRQ_SYNC_ABNORM_ERR 0x00000080
69 #define LPC_HC_IRQ_SYNC_NORESP_ERR 0x00000040
70 #define LPC_HC_IRQ_SYNC_NORM_ERR 0x00000020
71 #define LPC_HC_IRQ_SYNC_TIMEOUT_ERR 0x00000010
72 #define LPC_HC_IRQ_SYNC_TARG_TAR_ERR 0x00000008
73 #define LPC_HC_IRQ_SYNC_BM_TAR_ERR 0x00000004
74 #define LPC_HC_IRQ_SYNC_BM0_REQ 0x00000002
75 #define LPC_HC_IRQ_SYNC_BM1_REQ 0x00000001
76 #define LPC_HC_ERROR_ADDRESS 0x40
78 #define LPC_OPB_SIZE 0x100000000ull
80 #define ISA_IO_SIZE 0x00010000
81 #define ISA_MEM_SIZE 0x10000000
82 #define LPC_IO_OPB_ADDR 0xd0010000
83 #define LPC_IO_OPB_SIZE 0x00010000
84 #define LPC_MEM_OPB_ADDR 0xe0010000
85 #define LPC_MEM_OPB_SIZE 0x10000000
86 #define LPC_FW_OPB_ADDR 0xf0000000
87 #define LPC_FW_OPB_SIZE 0x10000000
89 #define LPC_OPB_REGS_OPB_ADDR 0xc0010000
90 #define LPC_OPB_REGS_OPB_SIZE 0x00002000
91 #define LPC_HC_REGS_OPB_ADDR 0xc0012000
92 #define LPC_HC_REGS_OPB_SIZE 0x00001000
95 static int pnv_lpc_dt_xscom(PnvXScomInterface
*dev
, void *fdt
, int xscom_offset
)
97 const char compat
[] = "ibm,power8-lpc\0ibm,lpc";
100 uint32_t lpc_pcba
= PNV_XSCOM_LPC_BASE
;
102 cpu_to_be32(lpc_pcba
),
103 cpu_to_be32(PNV_XSCOM_LPC_SIZE
)
106 name
= g_strdup_printf("isa@%x", lpc_pcba
);
107 offset
= fdt_add_subnode(fdt
, xscom_offset
, name
);
111 _FDT((fdt_setprop(fdt
, offset
, "reg", reg
, sizeof(reg
))));
112 _FDT((fdt_setprop_cell(fdt
, offset
, "#address-cells", 2)));
113 _FDT((fdt_setprop_cell(fdt
, offset
, "#size-cells", 1)));
114 _FDT((fdt_setprop(fdt
, offset
, "compatible", compat
, sizeof(compat
))));
119 * These read/write handlers of the OPB address space should be common
120 * with the P9 LPC Controller which uses direct MMIOs.
122 * TODO: rework to use address_space_stq() and address_space_ldq()
125 static bool opb_read(PnvLpcController
*lpc
, uint32_t addr
, uint8_t *data
,
128 /* XXX Handle access size limits and FW read caching here */
129 return !address_space_rw(&lpc
->opb_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
133 static bool opb_write(PnvLpcController
*lpc
, uint32_t addr
, uint8_t *data
,
136 /* XXX Handle access size limits here */
137 return !address_space_rw(&lpc
->opb_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
141 #define ECCB_CTL_READ PPC_BIT(15)
142 #define ECCB_CTL_SZ_LSH (63 - 7)
143 #define ECCB_CTL_SZ_MASK PPC_BITMASK(4, 7)
144 #define ECCB_CTL_ADDR_MASK PPC_BITMASK(32, 63)
146 #define ECCB_STAT_OP_DONE PPC_BIT(52)
147 #define ECCB_STAT_OP_ERR PPC_BIT(52)
148 #define ECCB_STAT_RD_DATA_LSH (63 - 37)
149 #define ECCB_STAT_RD_DATA_MASK (0xffffffff << ECCB_STAT_RD_DATA_LSH)
151 static void pnv_lpc_do_eccb(PnvLpcController
*lpc
, uint64_t cmd
)
153 /* XXX Check for magic bits at the top, addr size etc... */
154 unsigned int sz
= (cmd
& ECCB_CTL_SZ_MASK
) >> ECCB_CTL_SZ_LSH
;
155 uint32_t opb_addr
= cmd
& ECCB_CTL_ADDR_MASK
;
159 if (cmd
& ECCB_CTL_READ
) {
160 success
= opb_read(lpc
, opb_addr
, data
, sz
);
162 lpc
->eccb_stat_reg
= ECCB_STAT_OP_DONE
|
163 (((uint64_t)data
[0]) << 24 |
164 ((uint64_t)data
[1]) << 16 |
165 ((uint64_t)data
[2]) << 8 |
166 ((uint64_t)data
[3])) << ECCB_STAT_RD_DATA_LSH
;
168 lpc
->eccb_stat_reg
= ECCB_STAT_OP_DONE
|
169 (0xffffffffull
<< ECCB_STAT_RD_DATA_LSH
);
172 data
[0] = lpc
->eccb_data_reg
>> 24;
173 data
[1] = lpc
->eccb_data_reg
>> 16;
174 data
[2] = lpc
->eccb_data_reg
>> 8;
175 data
[3] = lpc
->eccb_data_reg
;
177 success
= opb_write(lpc
, opb_addr
, data
, sz
);
178 lpc
->eccb_stat_reg
= ECCB_STAT_OP_DONE
;
180 /* XXX Which error bit (if any) to signal OPB error ? */
183 static uint64_t pnv_lpc_xscom_read(void *opaque
, hwaddr addr
, unsigned size
)
185 PnvLpcController
*lpc
= PNV_LPC(opaque
);
186 uint32_t offset
= addr
>> 3;
189 switch (offset
& 3) {
195 val
= lpc
->eccb_stat_reg
;
196 lpc
->eccb_stat_reg
= 0;
199 val
= ((uint64_t)lpc
->eccb_data_reg
) << 32;
205 static void pnv_lpc_xscom_write(void *opaque
, hwaddr addr
,
206 uint64_t val
, unsigned size
)
208 PnvLpcController
*lpc
= PNV_LPC(opaque
);
209 uint32_t offset
= addr
>> 3;
211 switch (offset
& 3) {
213 pnv_lpc_do_eccb(lpc
, val
);
221 lpc
->eccb_data_reg
= val
>> 32;
226 static const MemoryRegionOps pnv_lpc_xscom_ops
= {
227 .read
= pnv_lpc_xscom_read
,
228 .write
= pnv_lpc_xscom_write
,
229 .valid
.min_access_size
= 8,
230 .valid
.max_access_size
= 8,
231 .impl
.min_access_size
= 8,
232 .impl
.max_access_size
= 8,
233 .endianness
= DEVICE_BIG_ENDIAN
,
236 static void pnv_lpc_eval_irqs(PnvLpcController
*lpc
)
238 bool lpc_to_opb_irq
= false;
240 /* Update LPC controller to OPB line */
241 if (lpc
->lpc_hc_irqser_ctrl
& LPC_HC_IRQSER_EN
) {
244 irqs
= lpc
->lpc_hc_irqstat
& lpc
->lpc_hc_irqmask
;
245 lpc_to_opb_irq
= (irqs
!= 0);
248 /* We don't honor the polarity register, it's pointless and unused
251 if (lpc_to_opb_irq
) {
252 lpc
->opb_irq_input
|= OPB_MASTER_IRQ_LPC
;
254 lpc
->opb_irq_input
&= ~OPB_MASTER_IRQ_LPC
;
257 /* Update OPB internal latch */
258 lpc
->opb_irq_stat
|= lpc
->opb_irq_input
& lpc
->opb_irq_mask
;
260 /* Reflect the interrupt */
261 pnv_psi_irq_set(lpc
->psi
, PSIHB_IRQ_LPC_I2C
, lpc
->opb_irq_stat
!= 0);
264 static uint64_t lpc_hc_read(void *opaque
, hwaddr addr
, unsigned size
)
266 PnvLpcController
*lpc
= opaque
;
267 uint64_t val
= 0xfffffffffffffffful
;
270 case LPC_HC_FW_SEG_IDSEL
:
271 val
= lpc
->lpc_hc_fw_seg_idsel
;
273 case LPC_HC_FW_RD_ACC_SIZE
:
274 val
= lpc
->lpc_hc_fw_rd_acc_size
;
276 case LPC_HC_IRQSER_CTRL
:
277 val
= lpc
->lpc_hc_irqser_ctrl
;
280 val
= lpc
->lpc_hc_irqmask
;
283 val
= lpc
->lpc_hc_irqstat
;
285 case LPC_HC_ERROR_ADDRESS
:
286 val
= lpc
->lpc_hc_error_addr
;
289 qemu_log_mask(LOG_UNIMP
, "LPC HC Unimplemented register: Ox%"
290 HWADDR_PRIx
"\n", addr
);
295 static void lpc_hc_write(void *opaque
, hwaddr addr
, uint64_t val
,
298 PnvLpcController
*lpc
= opaque
;
300 /* XXX Filter out reserved bits */
303 case LPC_HC_FW_SEG_IDSEL
:
304 /* XXX Actually figure out how that works as this impact
305 * memory regions/aliases
307 lpc
->lpc_hc_fw_seg_idsel
= val
;
309 case LPC_HC_FW_RD_ACC_SIZE
:
310 lpc
->lpc_hc_fw_rd_acc_size
= val
;
312 case LPC_HC_IRQSER_CTRL
:
313 lpc
->lpc_hc_irqser_ctrl
= val
;
314 pnv_lpc_eval_irqs(lpc
);
317 lpc
->lpc_hc_irqmask
= val
;
318 pnv_lpc_eval_irqs(lpc
);
321 lpc
->lpc_hc_irqstat
&= ~val
;
322 pnv_lpc_eval_irqs(lpc
);
324 case LPC_HC_ERROR_ADDRESS
:
327 qemu_log_mask(LOG_UNIMP
, "LPC HC Unimplemented register: Ox%"
328 HWADDR_PRIx
"\n", addr
);
332 static const MemoryRegionOps lpc_hc_ops
= {
334 .write
= lpc_hc_write
,
335 .endianness
= DEVICE_BIG_ENDIAN
,
337 .min_access_size
= 4,
338 .max_access_size
= 4,
341 .min_access_size
= 4,
342 .max_access_size
= 4,
346 static uint64_t opb_master_read(void *opaque
, hwaddr addr
, unsigned size
)
348 PnvLpcController
*lpc
= opaque
;
349 uint64_t val
= 0xfffffffffffffffful
;
352 case OPB_MASTER_LS_IRQ_STAT
:
353 val
= lpc
->opb_irq_stat
;
355 case OPB_MASTER_LS_IRQ_MASK
:
356 val
= lpc
->opb_irq_mask
;
358 case OPB_MASTER_LS_IRQ_POL
:
359 val
= lpc
->opb_irq_pol
;
361 case OPB_MASTER_LS_IRQ_INPUT
:
362 val
= lpc
->opb_irq_input
;
365 qemu_log_mask(LOG_UNIMP
, "OPB MASTER Unimplemented register: Ox%"
366 HWADDR_PRIx
"\n", addr
);
372 static void opb_master_write(void *opaque
, hwaddr addr
,
373 uint64_t val
, unsigned size
)
375 PnvLpcController
*lpc
= opaque
;
378 case OPB_MASTER_LS_IRQ_STAT
:
379 lpc
->opb_irq_stat
&= ~val
;
380 pnv_lpc_eval_irqs(lpc
);
382 case OPB_MASTER_LS_IRQ_MASK
:
383 lpc
->opb_irq_mask
= val
;
384 pnv_lpc_eval_irqs(lpc
);
386 case OPB_MASTER_LS_IRQ_POL
:
387 lpc
->opb_irq_pol
= val
;
388 pnv_lpc_eval_irqs(lpc
);
390 case OPB_MASTER_LS_IRQ_INPUT
:
394 qemu_log_mask(LOG_UNIMP
, "OPB MASTER Unimplemented register: Ox%"
395 HWADDR_PRIx
"\n", addr
);
399 static const MemoryRegionOps opb_master_ops
= {
400 .read
= opb_master_read
,
401 .write
= opb_master_write
,
402 .endianness
= DEVICE_BIG_ENDIAN
,
404 .min_access_size
= 4,
405 .max_access_size
= 4,
408 .min_access_size
= 4,
409 .max_access_size
= 4,
413 static void pnv_lpc_realize(DeviceState
*dev
, Error
**errp
)
415 PnvLpcController
*lpc
= PNV_LPC(dev
);
420 lpc
->lpc_hc_fw_rd_acc_size
= LPC_HC_FW_RD_4B
;
422 /* Create address space and backing MR for the OPB bus */
423 memory_region_init(&lpc
->opb_mr
, OBJECT(dev
), "lpc-opb", 0x100000000ull
);
424 address_space_init(&lpc
->opb_as
, &lpc
->opb_mr
, "lpc-opb");
426 /* Create ISA IO and Mem space regions which are the root of
427 * the ISA bus (ie, ISA address spaces). We don't create a
428 * separate one for FW which we alias to memory.
430 memory_region_init(&lpc
->isa_io
, OBJECT(dev
), "isa-io", ISA_IO_SIZE
);
431 memory_region_init(&lpc
->isa_mem
, OBJECT(dev
), "isa-mem", ISA_MEM_SIZE
);
433 /* Create windows from the OPB space to the ISA space */
434 memory_region_init_alias(&lpc
->opb_isa_io
, OBJECT(dev
), "lpc-isa-io",
435 &lpc
->isa_io
, 0, LPC_IO_OPB_SIZE
);
436 memory_region_add_subregion(&lpc
->opb_mr
, LPC_IO_OPB_ADDR
,
438 memory_region_init_alias(&lpc
->opb_isa_mem
, OBJECT(dev
), "lpc-isa-mem",
439 &lpc
->isa_mem
, 0, LPC_MEM_OPB_SIZE
);
440 memory_region_add_subregion(&lpc
->opb_mr
, LPC_MEM_OPB_ADDR
,
442 memory_region_init_alias(&lpc
->opb_isa_fw
, OBJECT(dev
), "lpc-isa-fw",
443 &lpc
->isa_mem
, 0, LPC_FW_OPB_SIZE
);
444 memory_region_add_subregion(&lpc
->opb_mr
, LPC_FW_OPB_ADDR
,
447 /* Create MMIO regions for LPC HC and OPB registers */
448 memory_region_init_io(&lpc
->opb_master_regs
, OBJECT(dev
), &opb_master_ops
,
449 lpc
, "lpc-opb-master", LPC_OPB_REGS_OPB_SIZE
);
450 memory_region_add_subregion(&lpc
->opb_mr
, LPC_OPB_REGS_OPB_ADDR
,
451 &lpc
->opb_master_regs
);
452 memory_region_init_io(&lpc
->lpc_hc_regs
, OBJECT(dev
), &lpc_hc_ops
, lpc
,
453 "lpc-hc", LPC_HC_REGS_OPB_SIZE
);
454 memory_region_add_subregion(&lpc
->opb_mr
, LPC_HC_REGS_OPB_ADDR
,
457 /* XScom region for LPC registers */
458 pnv_xscom_region_init(&lpc
->xscom_regs
, OBJECT(dev
),
459 &pnv_lpc_xscom_ops
, lpc
, "xscom-lpc",
462 /* get PSI object from chip */
463 obj
= object_property_get_link(OBJECT(dev
), "psi", &error
);
465 error_setg(errp
, "%s: required link 'psi' not found: %s",
466 __func__
, error_get_pretty(error
));
469 lpc
->psi
= PNV_PSI(obj
);
472 static void pnv_lpc_class_init(ObjectClass
*klass
, void *data
)
474 DeviceClass
*dc
= DEVICE_CLASS(klass
);
475 PnvXScomInterfaceClass
*xdc
= PNV_XSCOM_INTERFACE_CLASS(klass
);
477 xdc
->dt_xscom
= pnv_lpc_dt_xscom
;
479 dc
->realize
= pnv_lpc_realize
;
482 static const TypeInfo pnv_lpc_info
= {
483 .name
= TYPE_PNV_LPC
,
484 .parent
= TYPE_DEVICE
,
485 .instance_size
= sizeof(PnvLpcController
),
486 .class_init
= pnv_lpc_class_init
,
487 .interfaces
= (InterfaceInfo
[]) {
488 { TYPE_PNV_XSCOM_INTERFACE
},
493 static void pnv_lpc_register_types(void)
495 type_register_static(&pnv_lpc_info
);
498 type_init(pnv_lpc_register_types
)
500 /* If we don't use the built-in LPC interrupt deserializer, we need
501 * to provide a set of qirqs for the ISA bus or things will go bad.
503 * Most machines using pre-Naples chips (without said deserializer)
504 * have a CPLD that will collect the SerIRQ and shoot them as a
505 * single level interrupt to the P8 chip. So let's setup a hook
506 * for doing just that.
508 static void pnv_lpc_isa_irq_handler_cpld(void *opaque
, int n
, int level
)
510 PnvMachineState
*pnv
= PNV_MACHINE(qdev_get_machine());
511 uint32_t old_state
= pnv
->cpld_irqstate
;
512 PnvLpcController
*lpc
= PNV_LPC(opaque
);
515 pnv
->cpld_irqstate
|= 1u << n
;
517 pnv
->cpld_irqstate
&= ~(1u << n
);
520 if (pnv
->cpld_irqstate
!= old_state
) {
521 pnv_psi_irq_set(lpc
->psi
, PSIHB_IRQ_EXTERNAL
, pnv
->cpld_irqstate
!= 0);
525 static void pnv_lpc_isa_irq_handler(void *opaque
, int n
, int level
)
527 PnvLpcController
*lpc
= PNV_LPC(opaque
);
529 /* The Naples HW latches the 1 levels, clearing is done by SW */
531 lpc
->lpc_hc_irqstat
|= LPC_HC_IRQ_SERIRQ0
>> n
;
532 pnv_lpc_eval_irqs(lpc
);
536 qemu_irq
*pnv_lpc_isa_irq_create(PnvLpcController
*lpc
, int chip_type
,
539 /* Not all variants have a working serial irq decoder. If not,
540 * handling of LPC interrupts becomes a platform issue (some
541 * platforms have a CPLD to do it).
543 if (chip_type
== PNV_CHIP_POWER8NVL
) {
544 return qemu_allocate_irqs(pnv_lpc_isa_irq_handler
, lpc
, nirqs
);
546 return qemu_allocate_irqs(pnv_lpc_isa_irq_handler_cpld
, lpc
, nirqs
);