1*55485da1Smsaitoh /* $NetBSD: ixgbe_osdep.c,v 1.9 2023/10/06 14:37:04 msaitoh Exp $ */
2dc7f84c8Smsaitoh
31160ab79Smsaitoh /******************************************************************************
41160ab79Smsaitoh
58467fefbSmsaitoh Copyright (c) 2001-2020, Intel Corporation
61160ab79Smsaitoh All rights reserved.
71160ab79Smsaitoh
81160ab79Smsaitoh Redistribution and use in source and binary forms, with or without
91160ab79Smsaitoh modification, are permitted provided that the following conditions are met:
101160ab79Smsaitoh
111160ab79Smsaitoh 1. Redistributions of source code must retain the above copyright notice,
121160ab79Smsaitoh this list of conditions and the following disclaimer.
131160ab79Smsaitoh
141160ab79Smsaitoh 2. Redistributions in binary form must reproduce the above copyright
151160ab79Smsaitoh notice, this list of conditions and the following disclaimer in the
161160ab79Smsaitoh documentation and/or other materials provided with the distribution.
171160ab79Smsaitoh
181160ab79Smsaitoh 3. Neither the name of the Intel Corporation nor the names of its
191160ab79Smsaitoh contributors may be used to endorse or promote products derived from
201160ab79Smsaitoh this software without specific prior written permission.
211160ab79Smsaitoh
221160ab79Smsaitoh THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
231160ab79Smsaitoh AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241160ab79Smsaitoh IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251160ab79Smsaitoh ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
261160ab79Smsaitoh LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
271160ab79Smsaitoh CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
281160ab79Smsaitoh SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
291160ab79Smsaitoh INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
301160ab79Smsaitoh CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
311160ab79Smsaitoh ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
321160ab79Smsaitoh POSSIBILITY OF SUCH DAMAGE.
331160ab79Smsaitoh
341160ab79Smsaitoh ******************************************************************************/
35a06ca633Smsaitoh /*$FreeBSD: head/sys/dev/ixgbe/ixgbe_osdep.c 327031 2017-12-20 18:15:06Z erj $*/
361160ab79Smsaitoh
37ab119b16Smsaitoh #include <sys/cdefs.h>
38*55485da1Smsaitoh __KERNEL_RCSID(0, "$NetBSD: ixgbe_osdep.c,v 1.9 2023/10/06 14:37:04 msaitoh Exp $");
39ab119b16Smsaitoh
401160ab79Smsaitoh #include "ixgbe_osdep.h"
411160ab79Smsaitoh #include "ixgbe.h"
421160ab79Smsaitoh
431160ab79Smsaitoh inline device_t
ixgbe_dev_from_hw(struct ixgbe_hw * hw)441160ab79Smsaitoh ixgbe_dev_from_hw(struct ixgbe_hw *hw)
451160ab79Smsaitoh {
46*55485da1Smsaitoh return ((struct ixgbe_softc *)hw->back)->dev;
471160ab79Smsaitoh }
481160ab79Smsaitoh
491160ab79Smsaitoh u16
ixgbe_read_pci_cfg(struct ixgbe_hw * hw,u32 reg)501160ab79Smsaitoh ixgbe_read_pci_cfg(struct ixgbe_hw *hw, u32 reg)
511160ab79Smsaitoh {
521160ab79Smsaitoh pci_chipset_tag_t pc = hw->back->osdep.pc;
531160ab79Smsaitoh pcitag_t tag = hw->back->osdep.tag;
541160ab79Smsaitoh
551160ab79Smsaitoh switch (reg % 4) {
561160ab79Smsaitoh case 0:
571160ab79Smsaitoh return pci_conf_read(pc, tag, reg) & __BITS(15, 0);
581160ab79Smsaitoh case 2:
591160ab79Smsaitoh return __SHIFTOUT(pci_conf_read(pc, tag, reg - 2),
601160ab79Smsaitoh __BITS(31, 16));
611160ab79Smsaitoh default:
621160ab79Smsaitoh panic("%s: invalid register (%" PRIx32, __func__, reg);
631160ab79Smsaitoh break;
641160ab79Smsaitoh }
651160ab79Smsaitoh }
661160ab79Smsaitoh
671160ab79Smsaitoh void
ixgbe_write_pci_cfg(struct ixgbe_hw * hw,u32 reg,u16 value)681160ab79Smsaitoh ixgbe_write_pci_cfg(struct ixgbe_hw *hw, u32 reg, u16 value)
691160ab79Smsaitoh {
701160ab79Smsaitoh pci_chipset_tag_t pc = hw->back->osdep.pc;
711160ab79Smsaitoh pcitag_t tag = hw->back->osdep.tag;
721160ab79Smsaitoh pcireg_t old;
731160ab79Smsaitoh
741160ab79Smsaitoh switch (reg % 4) {
751160ab79Smsaitoh case 0:
761160ab79Smsaitoh old = pci_conf_read(pc, tag, reg) & __BITS(31, 16);
771160ab79Smsaitoh pci_conf_write(pc, tag, reg, value | old);
781160ab79Smsaitoh break;
791160ab79Smsaitoh case 2:
801160ab79Smsaitoh old = pci_conf_read(pc, tag, reg - 2) & __BITS(15, 0);
811160ab79Smsaitoh pci_conf_write(pc, tag, reg - 2,
821160ab79Smsaitoh __SHIFTIN(value, __BITS(31, 16)) | old);
831160ab79Smsaitoh break;
841160ab79Smsaitoh default:
851160ab79Smsaitoh panic("%s: invalid register (%" PRIx32, __func__, reg);
861160ab79Smsaitoh break;
871160ab79Smsaitoh }
881160ab79Smsaitoh
891160ab79Smsaitoh return;
901160ab79Smsaitoh }
911160ab79Smsaitoh
921160ab79Smsaitoh inline u32
ixgbe_read_reg(struct ixgbe_hw * hw,u32 reg)931160ab79Smsaitoh ixgbe_read_reg(struct ixgbe_hw *hw, u32 reg)
941160ab79Smsaitoh {
95*55485da1Smsaitoh return bus_space_read_4(((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_tag,
96*55485da1Smsaitoh ((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_handle, reg);
971160ab79Smsaitoh }
981160ab79Smsaitoh
991160ab79Smsaitoh inline void
ixgbe_write_reg(struct ixgbe_hw * hw,u32 reg,u32 val)1001160ab79Smsaitoh ixgbe_write_reg(struct ixgbe_hw *hw, u32 reg, u32 val)
1011160ab79Smsaitoh {
102*55485da1Smsaitoh bus_space_write_4(((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_tag,
103*55485da1Smsaitoh ((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_handle,
1041160ab79Smsaitoh reg, val);
1051160ab79Smsaitoh }
1061160ab79Smsaitoh
1071160ab79Smsaitoh inline u32
ixgbe_read_reg_array(struct ixgbe_hw * hw,u32 reg,u32 offset)1081160ab79Smsaitoh ixgbe_read_reg_array(struct ixgbe_hw *hw, u32 reg, u32 offset)
1091160ab79Smsaitoh {
110*55485da1Smsaitoh return bus_space_read_4(((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_tag,
111*55485da1Smsaitoh ((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_handle,
1121160ab79Smsaitoh reg + (offset << 2));
1131160ab79Smsaitoh }
1141160ab79Smsaitoh
1151160ab79Smsaitoh inline void
ixgbe_write_reg_array(struct ixgbe_hw * hw,u32 reg,u32 offset,u32 val)1161160ab79Smsaitoh ixgbe_write_reg_array(struct ixgbe_hw *hw, u32 reg, u32 offset, u32 val)
1171160ab79Smsaitoh {
118*55485da1Smsaitoh bus_space_write_4(((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_tag,
119*55485da1Smsaitoh ((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_handle,
1201160ab79Smsaitoh reg + (offset << 2), val);
1211160ab79Smsaitoh }
122b9ae5f22Smsaitoh
123b9ae5f22Smsaitoh inline void
ixgbe_write_barrier(struct ixgbe_hw * hw)124b9ae5f22Smsaitoh ixgbe_write_barrier(struct ixgbe_hw *hw)
125b9ae5f22Smsaitoh {
126*55485da1Smsaitoh bus_space_barrier(((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_tag,
127*55485da1Smsaitoh ((struct ixgbe_softc *)hw->back)->osdep.mem_bus_space_handle,
128*55485da1Smsaitoh 0, ((struct ixgbe_softc *)hw->back)->osdep.mem_size,
129b9ae5f22Smsaitoh BUS_SPACE_BARRIER_WRITE);
130b9ae5f22Smsaitoh }
131