/*-
* Copyright (c) 2024 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Nick Hudson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
syscon_lock(jh_sc->sc_syscon);
uint32_t val = syscon_read_4(jh_sc->sc_syscon, reg);
val &= ~(STARFIVE_GMAC_PHY_INFT_MASK << shift);
val |= STARFIVE_GMAC_PHY_INFT_RGMII << shift;
syscon_write_4(jh_sc->sc_syscon, reg, val);
if (jh_sc->sc_type == JH71X0ETH_GMAC) {
syscon_write_4(jh_sc->sc_syscon,
JH7100_SYSMAIN_REGISTER49_DLYCHAIN, 4);
}
syscon_unlock(jh_sc->sc_syscon);
}
int
jh71x0_eth_attach(struct jh71x0_eth_softc *jh_sc, struct fdt_attach_args *faa,
bus_space_handle_t *bshp)
{
const int phandle = faa->faa_phandle;
int phandle_phy;
bus_addr_t addr;
bus_size_t size;
int error;
int n;
error = fdtbus_get_reg(phandle, 0, &addr, &size);
if (error) {
aprint_error(": couldn't get registers\n");
return ENXIO;
}
int len;
const u_int * const syscon_data =
fdtbus_get_prop(phandle, "starfive,syscon", &len);
if (syscon_data == NULL) {
aprint_error(": couldn't get 'starfive,syscon' property\n");
return ENXIO;
}
if (len != 3 * sizeof(uint32_t)) {
aprint_error(": incorrect syscon data (len = %u)\n",
len);
return ENXIO;
}
const int syscon_phandle =
fdtbus_get_phandle_from_native(be32dec(&syscon_data[0]));
jh_sc->sc_syscon = fdtbus_syscon_lookup(syscon_phandle);
if (jh_sc->sc_syscon == NULL) {
aprint_error(": couldn't get syscon\n");
return ENXIO;
}