1*52334306Syasuoka /* $OpenBSD: cn30xxasx.c,v 1.8 2022/12/28 01:39:21 yasuoka Exp $ */
24a04f2fdSsyuu
34a04f2fdSsyuu /*
44a04f2fdSsyuu * Copyright (c) 2007 Internet Initiative Japan, Inc.
54a04f2fdSsyuu * All rights reserved.
64a04f2fdSsyuu *
74a04f2fdSsyuu * Redistribution and use in source and binary forms, with or without
84a04f2fdSsyuu * modification, are permitted provided that the following conditions
94a04f2fdSsyuu * are met:
104a04f2fdSsyuu * 1. Redistributions of source code must retain the above copyright
114a04f2fdSsyuu * notice, this list of conditions and the following disclaimer.
124a04f2fdSsyuu * 2. Redistributions in binary form must reproduce the above copyright
134a04f2fdSsyuu * notice, this list of conditions and the following disclaimer in the
144a04f2fdSsyuu * documentation and/or other materials provided with the distribution.
154a04f2fdSsyuu *
16*52334306Syasuoka * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
174a04f2fdSsyuu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184a04f2fdSsyuu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*52334306Syasuoka * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
204a04f2fdSsyuu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214a04f2fdSsyuu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224a04f2fdSsyuu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234a04f2fdSsyuu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244a04f2fdSsyuu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254a04f2fdSsyuu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264a04f2fdSsyuu * SUCH DAMAGE.
274a04f2fdSsyuu */
284a04f2fdSsyuu
294a04f2fdSsyuu #include <sys/param.h>
304a04f2fdSsyuu #include <sys/systm.h>
314a04f2fdSsyuu #include <sys/malloc.h>
324a04f2fdSsyuu
334a04f2fdSsyuu #include <machine/octeonvar.h>
344a04f2fdSsyuu
354a04f2fdSsyuu #include <octeon/dev/cn30xxasxreg.h>
364a04f2fdSsyuu #include <octeon/dev/cn30xxasxvar.h>
374a04f2fdSsyuu
384a04f2fdSsyuu void
cn30xxasx_init(struct cn30xxasx_attach_args * aa,struct cn30xxasx_softc ** rsc)394a04f2fdSsyuu cn30xxasx_init(struct cn30xxasx_attach_args *aa,
404a04f2fdSsyuu struct cn30xxasx_softc **rsc)
414a04f2fdSsyuu {
424a04f2fdSsyuu struct cn30xxasx_softc *sc;
434a04f2fdSsyuu int status;
444a04f2fdSsyuu
454a04f2fdSsyuu sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
464a04f2fdSsyuu if (sc == NULL)
474a04f2fdSsyuu panic("can't allocate memory: %s", __func__);
484a04f2fdSsyuu
494a04f2fdSsyuu sc->sc_port = aa->aa_port;
504a04f2fdSsyuu sc->sc_regt = aa->aa_regt;
514a04f2fdSsyuu
524a04f2fdSsyuu status = bus_space_map(sc->sc_regt, ASX0_BASE, ASX0_SIZE, 0,
534a04f2fdSsyuu &sc->sc_regh);
544a04f2fdSsyuu if (status != 0)
554a04f2fdSsyuu panic("can't map %s space", "asx register");
564a04f2fdSsyuu
574a04f2fdSsyuu *rsc = sc;
584a04f2fdSsyuu }
594a04f2fdSsyuu
604a04f2fdSsyuu #define _ASX_RD8(sc, off) \
614a04f2fdSsyuu bus_space_read_8((sc)->sc_regt, (sc)->sc_regh, (off))
624a04f2fdSsyuu #define _ASX_WR8(sc, off, v) \
634a04f2fdSsyuu bus_space_write_8((sc)->sc_regt, (sc)->sc_regh, (off), (v))
644a04f2fdSsyuu
6534a927e5Smiod int cn30xxasx_enable_tx(struct cn30xxasx_softc *, int);
6634a927e5Smiod int cn30xxasx_enable_rx(struct cn30xxasx_softc *, int);
674a04f2fdSsyuu
684a04f2fdSsyuu int
cn30xxasx_enable(struct cn30xxasx_softc * sc,int enable)694a04f2fdSsyuu cn30xxasx_enable(struct cn30xxasx_softc *sc, int enable)
704a04f2fdSsyuu {
714a04f2fdSsyuu cn30xxasx_enable_tx(sc, enable);
724a04f2fdSsyuu cn30xxasx_enable_rx(sc, enable);
734a04f2fdSsyuu return 0;
744a04f2fdSsyuu }
754a04f2fdSsyuu
7634a927e5Smiod int
cn30xxasx_enable_tx(struct cn30xxasx_softc * sc,int enable)774a04f2fdSsyuu cn30xxasx_enable_tx(struct cn30xxasx_softc *sc, int enable)
784a04f2fdSsyuu {
794a04f2fdSsyuu uint64_t asx_tx_port;
804a04f2fdSsyuu
814a04f2fdSsyuu asx_tx_port = _ASX_RD8(sc, ASX0_TX_PRT_EN_OFFSET);
824a04f2fdSsyuu if (enable)
834a04f2fdSsyuu SET(asx_tx_port, 1 << sc->sc_port);
844a04f2fdSsyuu else
854a04f2fdSsyuu CLR(asx_tx_port, 1 << sc->sc_port);
864a04f2fdSsyuu _ASX_WR8(sc, ASX0_TX_PRT_EN_OFFSET, asx_tx_port);
874a04f2fdSsyuu return 0;
884a04f2fdSsyuu }
894a04f2fdSsyuu
9034a927e5Smiod int
cn30xxasx_enable_rx(struct cn30xxasx_softc * sc,int enable)914a04f2fdSsyuu cn30xxasx_enable_rx(struct cn30xxasx_softc *sc, int enable)
924a04f2fdSsyuu {
934a04f2fdSsyuu uint64_t asx_rx_port;
944a04f2fdSsyuu
954a04f2fdSsyuu asx_rx_port = _ASX_RD8(sc, ASX0_RX_PRT_EN_OFFSET);
964a04f2fdSsyuu if (enable)
974a04f2fdSsyuu SET(asx_rx_port, 1 << sc->sc_port);
984a04f2fdSsyuu else
994a04f2fdSsyuu CLR(asx_rx_port, 1 << sc->sc_port);
1004a04f2fdSsyuu _ASX_WR8(sc, ASX0_RX_PRT_EN_OFFSET, asx_rx_port);
1014a04f2fdSsyuu return 0;
1024a04f2fdSsyuu }
1034a04f2fdSsyuu
1044a04f2fdSsyuu int
cn30xxasx_clk_set(struct cn30xxasx_softc * sc,int tx_setting,int rx_setting)1051f085dceSjmatthew cn30xxasx_clk_set(struct cn30xxasx_softc *sc, int tx_setting, int rx_setting)
1064a04f2fdSsyuu {
1071f085dceSjmatthew _ASX_WR8(sc, ASX0_TX_CLK_SET0_OFFSET + 8 * sc->sc_port, tx_setting);
1081f085dceSjmatthew _ASX_WR8(sc, ASX0_RX_CLK_SET0_OFFSET + 8 * sc->sc_port, rx_setting);
1094a04f2fdSsyuu return 0;
1104a04f2fdSsyuu }
111