2 * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
3 * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
42 #include "uart_emul.h"
44 static struct pci_devinst *lpc_bridge;
46 #define LPC_UART_NUM 2
47 static struct lpc_uart_softc {
48 struct uart_softc *uart_softc;
52 } lpc_uart_softc[LPC_UART_NUM];
54 static const char *lpc_uart_names[LPC_UART_NUM] = { "COM1", "COM2" };
57 * LPC device configuration is in the following form:
58 * <lpc_device_name>[,<options>]
59 * For e.g. "com1,stdio"
62 lpc_device_parse(const char *opts)
65 char *str, *cpy, *lpcdev;
68 str = cpy = strdup(opts);
69 lpcdev = strsep(&str, ",");
71 for (unit = 0; unit < LPC_UART_NUM; unit++) {
72 if (strcasecmp(lpcdev, lpc_uart_names[unit]) == 0) {
73 lpc_uart_softc[unit].opts = str;
88 lpc_uart_intr_assert(void *arg)
90 struct lpc_uart_softc *sc = arg;
94 ioapic_assert_pin(lpc_bridge->pi_vmctx, sc->irq);
98 lpc_uart_intr_deassert(void *arg)
100 struct lpc_uart_softc *sc = arg;
102 assert(sc->irq >= 0);
104 ioapic_deassert_pin(lpc_bridge->pi_vmctx, sc->irq);
108 lpc_uart_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
109 uint32_t *eax, void *arg)
112 struct lpc_uart_softc *sc = arg;
117 offset = port - sc->iobase;
120 *eax = uart_read(sc->uart_softc, offset);
122 uart_write(sc->uart_softc, offset, *eax);
130 struct lpc_uart_softc *sc;
131 struct inout_port iop;
136 for (unit = 0; unit < LPC_UART_NUM; unit++) {
137 sc = &lpc_uart_softc[unit];
138 name = lpc_uart_names[unit];
140 if (uart_legacy_alloc(unit, &sc->iobase, &sc->irq) != 0) {
141 fprintf(stderr, "Unable to allocate resources for "
142 "LPC device %s\n", name);
146 sc->uart_softc = uart_init(lpc_uart_intr_assert,
147 lpc_uart_intr_deassert, sc);
149 if (uart_set_backend(sc->uart_softc, sc->opts) != 0) {
150 fprintf(stderr, "Unable to initialize backend '%s' "
151 "for LPC device %s\n", sc->opts, name);
155 bzero(&iop, sizeof(struct inout_port));
157 iop.port = sc->iobase;
158 iop.size = UART_IO_BAR_SIZE;
159 iop.flags = IOPORT_F_INOUT;
160 iop.handler = lpc_uart_io_handler;
163 error = register_inout(&iop);
171 pci_lpc_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
172 int baridx, uint64_t offset, int size, uint64_t value)
177 pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
178 int baridx, uint64_t offset, int size)
183 #define LPC_DEV 0x7000
184 #define LPC_VENDOR 0x8086
187 pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
190 * Do not allow more than one LPC bridge to be configured.
192 if (lpc_bridge != NULL)
198 /* initialize config space */
199 pci_set_cfgdata16(pi, PCIR_DEVICE, LPC_DEV);
200 pci_set_cfgdata16(pi, PCIR_VENDOR, LPC_VENDOR);
201 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
202 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_ISA);
209 struct pci_devemu pci_de_lpc = {
211 .pe_init = pci_lpc_init,
212 .pe_barwrite = pci_lpc_write,
213 .pe_barread = pci_lpc_read
215 PCI_EMUL_SET(pci_de_lpc);