1/*-
2 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3 * All rights reserved.
4 *
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: releng/11.0/sys/riscv/htif/htif.h 295041 2016-01-29 15:12:31Z br $
35 */
36
37#define	HTIF_DEV_ID_SHIFT	(56)
38#define	HTIF_DEV_ID_MASK	(0xfful << HTIF_DEV_ID_SHIFT)
39#define	HTIF_CMD_SHIFT		(48)
40#define	HTIF_CMD_MASK		(0xfful << HTIF_CMD_SHIFT)
41#define	HTIF_DATA_SHIFT		(0)
42#define	HTIF_DATA_MASK		(0xffffffff << HTIF_DATA_SHIFT)
43
44#define	HTIF_CMD_READ			(0x00ul)
45#define	HTIF_CMD_WRITE			(0x01ul)
46#define	HTIF_CMD_READ_CONTROL_REG	(0x02ul)
47#define	HTIF_CMD_WRITE_CONTROL_REG	(0x03ul)
48#define	HTIF_CMD_IDENTIFY		(0xfful)
49#define	 IDENTIFY_PADDR_SHIFT		8
50#define	 IDENTIFY_IDENT			0xff
51
52#define	HTIF_NDEV		(256)
53#define	HTIF_ID_LEN		(64)
54#define	HTIF_ALIGN		(64)
55
56#define	HTIF_DEV_CMD(entry)	((entry & HTIF_CMD_MASK) >> HTIF_CMD_SHIFT)
57#define	HTIF_DEV_ID(entry)	((entry & HTIF_DEV_ID_MASK) >> HTIF_DEV_ID_SHIFT)
58#define	HTIF_DEV_DATA(entry)	((entry & HTIF_DATA_MASK) >> HTIF_DATA_SHIFT)
59
60/* bus softc */
61struct htif_softc {
62	struct resource		*res[1];
63	void			*ihl[1];
64	device_t		dev;
65	uint64_t		identify_id;
66	uint64_t		identify_done;
67};
68
69/* device private data */
70struct htif_dev_ivars {
71	char			*id;
72	int			index;
73	device_t		dev;
74	struct htif_softc	*sc;
75};
76
77uint64_t htif_command(uint64_t);
78int htif_setup_intr(int id, void *func, void *arg);
79int htif_read_ivar(device_t dev, device_t child, int which, uintptr_t *result);
80
81enum htif_device_ivars {
82	HTIF_IVAR_INDEX,
83	HTIF_IVAR_ID,
84};
85
86/*
87 * Simplified accessors for HTIF devices
88 */
89#define HTIF_ACCESSOR(var, ivar, type)	\
90	__BUS_ACCESSOR(htif, var, HTIF, ivar, type)
91
92HTIF_ACCESSOR(index, INDEX, int);
93HTIF_ACCESSOR(id, ID, char *);
94