1/*-
2 * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
3 * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/types.h>
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include "inout.h"
40#include "ioapic.h"
41#include "pci_emul.h"
42#include "uart_emul.h"
43
44static struct pci_devinst *lpc_bridge;
45
46#define	LPC_UART_NUM	2
47static struct lpc_uart_softc {
48	struct uart_softc *uart_softc;
49	const char *opts;
50	int	iobase;
51	int	irq;
52} lpc_uart_softc[LPC_UART_NUM];
53
54static const char *lpc_uart_names[LPC_UART_NUM] = { "COM1", "COM2" };
55
56/*
57 * LPC device configuration is in the following form:
58 * <lpc_device_name>[,<options>]
59 * For e.g. "com1,stdio"
60 */
61int
62lpc_device_parse(const char *opts)
63{
64	int unit, error;
65	char *str, *cpy, *lpcdev;
66
67	error = -1;
68	str = cpy = strdup(opts);
69	lpcdev = strsep(&str, ",");
70	if (lpcdev != NULL) {
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;
74				error = 0;
75				goto done;
76			}
77		}
78	}
79
80done:
81	if (error)
82		free(cpy);
83
84	return (error);
85}
86
87static void
88lpc_uart_intr_assert(void *arg)
89{
90	struct lpc_uart_softc *sc = arg;
91
92	assert(sc->irq >= 0);
93
94	ioapic_assert_pin(lpc_bridge->pi_vmctx, sc->irq);
95}
96
97static void
98lpc_uart_intr_deassert(void *arg)
99{
100	struct lpc_uart_softc *sc = arg;
101
102	assert(sc->irq >= 0);
103
104	ioapic_deassert_pin(lpc_bridge->pi_vmctx, sc->irq);
105}
106
107static int
108lpc_uart_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
109		    uint32_t *eax, void *arg)
110{
111	int offset;
112	struct lpc_uart_softc *sc = arg;
113
114	if (bytes != 1)
115		return (-1);
116
117	offset = port - sc->iobase;
118
119	if (in)
120		*eax = uart_read(sc->uart_softc, offset);
121	else
122		uart_write(sc->uart_softc, offset, *eax);
123
124	return (0);
125}
126
127static int
128lpc_init(void)
129{
130	struct lpc_uart_softc *sc;
131	struct inout_port iop;
132	const char *name;
133	int unit, error;
134
135	/* COM1 and COM2 */
136	for (unit = 0; unit < LPC_UART_NUM; unit++) {
137		sc = &lpc_uart_softc[unit];
138		name = lpc_uart_names[unit];
139
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);
143			return (-1);
144		}
145
146		sc->uart_softc = uart_init(lpc_uart_intr_assert,
147				    lpc_uart_intr_deassert, sc);
148
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);
152			return (-1);
153		}
154
155		bzero(&iop, sizeof(struct inout_port));
156		iop.name = name;
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;
161		iop.arg = sc;
162
163		error = register_inout(&iop);
164		assert(error == 0);
165	}
166
167	return (0);
168}
169
170static void
171pci_lpc_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
172	       int baridx, uint64_t offset, int size, uint64_t value)
173{
174}
175
176uint64_t
177pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
178	      int baridx, uint64_t offset, int size)
179{
180	return (0);
181}
182
183#define	LPC_DEV		0x7000
184#define	LPC_VENDOR	0x8086
185
186static int
187pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
188{
189	/*
190	 * Do not allow more than one LPC bridge to be configured.
191	 */
192	if (lpc_bridge != NULL)
193		return (-1);
194
195	if (lpc_init() != 0)
196		return (-1);
197
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);
203
204	lpc_bridge = pi;
205
206	return (0);
207}
208
209struct pci_devemu pci_de_lpc = {
210	.pe_emu =	"lpc",
211	.pe_init =	pci_lpc_init,
212	.pe_barwrite =	pci_lpc_write,
213	.pe_barread =	pci_lpc_read
214};
215PCI_EMUL_SET(pci_de_lpc);
216