1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer,
12 *    without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 *    redistribution must be conditioned upon including a substantially
16 *    similar Disclaimer requirement for further binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
30 *
31 * $FreeBSD$
32 */
33
34#ifndef _BHND_CORES_PCI_BHND_PCIVAR_H_
35#define _BHND_CORES_PCI_BHND_PCIVAR_H_
36
37#include <sys/param.h>
38#include <sys/bus.h>
39
40/*
41 * Shared PCI Bridge/PCI Host Bridge definitions.
42 */
43
44DECLARE_CLASS(bhnd_pci_driver);
45struct bhnd_pci_softc;
46
47int		bhnd_pci_generic_probe(device_t dev);
48int		bhnd_pci_generic_attach(device_t dev);
49int		bhnd_pci_generic_detach(device_t dev);
50int		bhnd_pci_generic_suspend(device_t dev);
51int		bhnd_pci_generic_resume(device_t dev);
52
53uint32_t	bhnd_pcie_read_proto_reg(struct bhnd_pci_softc *sc,
54		    uint32_t addr);
55void		bhnd_pcie_write_proto_reg(struct bhnd_pci_softc *sc,
56		    uint32_t addr, uint32_t val);
57int		bhnd_pcie_mdio_read(struct bhnd_pci_softc *sc, int phy,
58		    int reg);
59int		bhnd_pcie_mdio_write(struct bhnd_pci_softc *sc, int phy,
60		    int reg, int val);
61int		bhnd_pcie_mdio_read_ext(struct bhnd_pci_softc *sc, int phy,
62		    int devaddr, int reg);
63int		bhnd_pcie_mdio_write_ext(struct bhnd_pci_softc *sc, int phy,
64		    int devaddr, int reg, int val);
65
66/** PCI register block layouts. */
67typedef enum {
68	BHND_PCI_REGFMT_PCI	= 0,	/* PCI register definitions */
69	BHND_PCI_REGFMT_PCIE	= 1,	/* PCIe-Gen1 register definitions */
70} bhnd_pci_regfmt_t;
71
72/** PCI (base driver) quirks */
73enum {
74	/**
75	 * The PCIe SerDes requires use of a non-standard Clause 22
76	 * address extension mechanism to access extended MDIO registers.
77	 */
78	BHND_PCI_QUIRK_SD_C22_EXTADDR          = (1<<0),
79};
80
81/**
82 * bhnd_pci child device info
83 */
84struct bhnd_pci_devinfo {
85	struct resource_list	resources;
86};
87
88/*
89 * Generic PCI bridge/end-point driver state.
90 *
91 * Must be first member of all subclass softc structures.
92 */
93struct bhnd_pci_softc {
94	device_t		 dev;		/**< pci device */
95	uint32_t		 quirks;	/**< quirk flags */
96	bhnd_pci_regfmt_t	 regfmt;	/**< register format */
97
98	struct mtx		 mtx;		/**< state mutex used to protect
99						     interdependent register
100						     accesses. */
101
102	struct bhnd_resource	*mem_res;	/**< device register block. */
103	int			 mem_rid;	/**< register block RID */
104};
105
106#define	BHND_PCI_LOCK_INIT(sc) \
107	mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
108	    "BHND PCI driver lock", MTX_DEF)
109#define	BHND_PCI_LOCK(sc)			mtx_lock(&(sc)->mtx)
110#define	BHND_PCI_UNLOCK(sc)			mtx_unlock(&(sc)->mtx)
111#define	BHND_PCI_LOCK_ASSERT(sc, what)	mtx_assert(&(sc)->mtx, what)
112#define	BHND_PCI_LOCK_DESTROY(sc)		mtx_destroy(&(sc)->mtx)
113
114/* BHND_PCI_*_REG_(GET|SET) implementation */
115#define	_BHND_PCI_REG_GET(_regval, _mask, _shift)		\
116	((_regval & _mask) >> _shift)
117#define _BHND_PCI_REG_SET(_regval, _mask, _shift, _setval)	\
118	(((_regval) & ~ _mask) | (((_setval) << _shift) & _mask))
119
120/**
121 * Extract a register value by applying _MASK and _SHIFT defines.
122 *
123 * @param _regv The register value containing the desired attribute
124 * @param _attr The register attribute name to which to append `_MASK`/`_SHIFT`
125 * suffixes.
126 */
127#define	BHND_PCI_REG_GET(_regv, _attr)	\
128	_BHND_PCI_REG_GET(_regv, _attr ## _MASK, _attr ## _SHIFT)
129
130/**
131 * Insert a value in @p _regv by applying _MASK and _SHIFT defines.
132 *
133 * @param _regv The current register value.
134 * @param _attr The register attribute name to which to append `_MASK`/`_SHIFT`
135 * suffixes.
136 * @param _val The value to be set in @p _regv.
137 */
138#define	BHND_PCI_REG_SET(_regv, _attr, _val)		\
139	_BHND_PCI_REG_SET(_regv, _attr ## _MASK, _attr ## _SHIFT, _val)
140
141/**
142 * Extract a value by applying _MASK and _SHIFT defines to the common
143 * PCI/PCIe register definition @p _regv
144 *
145 * @param _regf The PCI core register format (BHND_PCI_REGFMT_*).
146 * @param _regv The register value containing the desired attribute
147 * @param _attr The register attribute name to which to prepend the register
148 * definition prefix and append `_MASK`/`_SHIFT` suffixes.
149 */
150#define BHND_PCI_CMN_REG_GET(_regf, _regv, _attr)		\
151	_BHND_PCI_REG_GET(_regv,				\
152	    BHND_PCI_COMMON_REG((_regf), _attr ## _MASK),	\
153	    BHND_PCI_COMMON_REG((_regf), _attr ## _SHIFT))
154
155/**
156 * Insert a register value by applying _MASK and _SHIFT defines to the common
157 * PCI/PCIe register definition @p _regv
158 *
159 * @param _regf The PCI core register format (BHND_PCI_REGFMT_*).
160 * @param _regv The register value containing the desired attribute
161 * @param _attr The register attribute name to which to prepend the register
162 * definition prefix and append `_MASK`/`_SHIFT` suffixes.
163 * @param _val The value to bet set in @p _regv.
164 */
165#define BHND_PCI_CMN_REG_SET(_regf, _regv, _attr, _val)	\
166	_BHND_PCI_REG_SET(_regv,				\
167	    BHND_PCI_COMMON_REG((_regf), _attr ## _MASK),	\
168	    BHND_PCI_COMMON_REG((_regf), _attr ## _SHIFT),	\
169	    _val)
170
171/**
172 * Evaluates to the offset of a common PCI/PCIe register definition.
173 *
174 * This will trigger a compile-time error if the register is not defined
175 * for all supported PCI/PCIe cores.
176 *
177 * This should be optimized down to a constant value if the register constant
178 * is the same across the register definitions.
179 *
180 * @param _regf The PCI core register format (BHND_PCI_REGFMT_*).
181 * @param _name The base name of the register.
182 */
183#define	BHND_PCI_COMMON_REG(_regf, _name)	(			\
184	(_regf) == BHND_PCI_REGFMT_PCI ? BHND_PCI_ ## _name :		\
185	BHND_PCIE_ ## _name						\
186)
187
188#endif /* _BHND_CORES_PCI_BHND_PCIVAR_H_ */
189