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