blob: 618e3a1cbd9e8942fd7e35002205e3f2a34a9553 [file] [log] [blame]
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2009 coresystems GmbH
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
Eric Biederman5899fd82003-04-24 06:25:08 +000018#ifndef PCI_OPS_H
19#define PCI_OPS_H
20
21#include <stdint.h>
Eric Biederman7a5416a2003-06-12 19:23:51 +000022#include <device/device.h>
Kyösti Mälkki3e6913b2019-03-02 16:26:10 +020023#include <device/pci_type.h>
Eric Biederman018d8dd2004-11-04 11:04:33 +000024#include <arch/pci_ops.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000025
Kyösti Mälkki92b52962019-03-01 08:08:28 +020026#ifdef __SIMPLE_DEVICE__
27
28/* Avoid name collisions as different stages have different signature
29 * for these functions. The _s_ stands for simple, fundamental IO or
30 * MMIO variant.
31 */
32#define pci_read_config8 pci_s_read_config8
33#define pci_read_config16 pci_s_read_config16
34#define pci_read_config32 pci_s_read_config32
35#define pci_write_config8 pci_s_write_config8
36#define pci_write_config16 pci_s_write_config16
37#define pci_write_config32 pci_s_write_config32
38#else
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020039
40#include <device/pci.h>
41
Kyösti Mälkki78d14322019-01-23 15:57:49 +020042const struct pci_bus_operations *pci_bus_default_ops(void);
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020043
44static __always_inline const struct pci_bus_operations *pci_bus_ops(void)
45{
46 return pci_bus_default_ops();
47}
48
49void __noreturn pcidev_die(void);
50
51static __always_inline void pcidev_assert(const struct device *dev)
52{
53 if (!dev)
54 pcidev_die();
55}
56
57static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +020058u8 pci_read_config8(const struct device *dev, u16 where)
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020059{
60 pcidev_assert(dev);
61 return pci_bus_ops()->read8(dev, where);
62}
63
64static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +020065u16 pci_read_config16(const struct device *dev, u16 where)
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020066{
67 pcidev_assert(dev);
68 return pci_bus_ops()->read16(dev, where);
69}
70
71static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +020072u32 pci_read_config32(const struct device *dev, u16 where)
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020073{
74 pcidev_assert(dev);
75 return pci_bus_ops()->read32(dev, where);
76}
77
78static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +020079void pci_write_config8(const struct device *dev, u16 where, u8 val)
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020080{
81 pcidev_assert(dev);
82 pci_bus_ops()->write8(dev, where, val);
83}
84
85static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +020086void pci_write_config16(const struct device *dev, u16 where, u16 val)
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020087{
88 pcidev_assert(dev);
89 pci_bus_ops()->write16(dev, where, val);
90}
91
92static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +020093void pci_write_config32(const struct device *dev, u16 where, u32 val)
Kyösti Mälkkie079e5c2019-01-23 16:15:48 +020094{
95 pcidev_assert(dev);
96 pci_bus_ops()->write32(dev, where, val);
97}
98
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -070099#endif
Stefan Reinauer43b29cf2009-03-06 19:11:52 +0000100
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100101#ifdef __SIMPLE_DEVICE__
Aaron Durbin75a62e72018-09-13 02:10:45 -0600102static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200103void pci_or_config8(pci_devfn_t dev, u16 where, u8 ormask)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100104#else
105static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200106void pci_or_config8(const struct device *dev, u16 where, u8 ormask)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100107#endif
Patrick Rudolphe56189c2018-04-18 10:11:59 +0200108{
109 u8 value = pci_read_config8(dev, where);
110 pci_write_config8(dev, where, value | ormask);
111}
112
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100113#ifdef __SIMPLE_DEVICE__
Aaron Durbin75a62e72018-09-13 02:10:45 -0600114static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200115void pci_or_config16(pci_devfn_t dev, u16 where, u16 ormask)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100116#else
117static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200118void pci_or_config16(const struct device *dev, u16 where, u16 ormask)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100119#endif
Patrick Rudolphe56189c2018-04-18 10:11:59 +0200120{
121 u16 value = pci_read_config16(dev, where);
122 pci_write_config16(dev, where, value | ormask);
123}
124
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100125#ifdef __SIMPLE_DEVICE__
Aaron Durbin75a62e72018-09-13 02:10:45 -0600126static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200127void pci_or_config32(pci_devfn_t dev, u16 where, u32 ormask)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100128#else
129static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200130void pci_or_config32(const struct device *dev, u16 where, u32 ormask)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100131#endif
Patrick Rudolphe56189c2018-04-18 10:11:59 +0200132{
133 u32 value = pci_read_config32(dev, where);
134 pci_write_config32(dev, where, value | ormask);
135}
136
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100137#ifdef __SIMPLE_DEVICE__
Aaron Durbin75a62e72018-09-13 02:10:45 -0600138static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200139void pci_update_config8(pci_devfn_t dev, u16 reg, u8 mask, u8 or)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100140#else
141static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200142void pci_update_config8(const struct device *dev, u16 reg, u8 mask, u8 or)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100143#endif
Patrick Rudolphe56189c2018-04-18 10:11:59 +0200144{
145 u8 reg8;
146
147 reg8 = pci_read_config8(dev, reg);
148 reg8 &= mask;
149 reg8 |= or;
150 pci_write_config8(dev, reg, reg8);
151}
152
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100153#ifdef __SIMPLE_DEVICE__
Aaron Durbin75a62e72018-09-13 02:10:45 -0600154static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200155void pci_update_config16(pci_devfn_t dev, u16 reg, u16 mask, u16 or)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100156#else
157static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200158void pci_update_config16(const struct device *dev, u16 reg, u16 mask, u16 or)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100159#endif
Patrick Rudolphe56189c2018-04-18 10:11:59 +0200160{
161 u16 reg16;
162
163 reg16 = pci_read_config16(dev, reg);
164 reg16 &= mask;
165 reg16 |= or;
166 pci_write_config16(dev, reg, reg16);
167}
168
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100169#ifdef __SIMPLE_DEVICE__
Aaron Durbin75a62e72018-09-13 02:10:45 -0600170static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200171void pci_update_config32(pci_devfn_t dev, u16 reg, u32 mask, u32 or)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100172#else
173static __always_inline
Kyösti Mälkki55172462019-03-07 11:13:29 +0200174void pci_update_config32(const struct device *dev, u16 reg, u32 mask, u32 or)
Elyes HAOUASf9e47cc2018-12-05 11:03:36 +0100175#endif
Patrick Rudolphe56189c2018-04-18 10:11:59 +0200176{
177 u32 reg32;
178
179 reg32 = pci_read_config32(dev, reg);
180 reg32 &= mask;
181 reg32 |= or;
182 pci_write_config32(dev, reg, reg32);
183}
184
Eric Biederman5899fd82003-04-24 06:25:08 +0000185#endif /* PCI_OPS_H */