xref: /netbsd-src/sys/arch/powerpc/ibm4xx/dev/rgmii.c (revision 16031f7d46f56c21335839c17974dddd9f9800b4)
1*16031f7dSrin /* $NetBSD: rgmii.c,v 1.2 2020/07/06 09:34:17 rin Exp $ */
22692e2e2Skiyohara /*
32692e2e2Skiyohara  * Copyright (c) 2010 KIYOHARA Takashi
42692e2e2Skiyohara  * All rights reserved.
52692e2e2Skiyohara  *
62692e2e2Skiyohara  * Redistribution and use in source and binary forms, with or without
72692e2e2Skiyohara  * modification, are permitted provided that the following conditions
82692e2e2Skiyohara  * are met:
92692e2e2Skiyohara  * 1. Redistributions of source code must retain the above copyright
102692e2e2Skiyohara  *    notice, this list of conditions and the following disclaimer.
112692e2e2Skiyohara  * 2. Redistributions in binary form must reproduce the above copyright
122692e2e2Skiyohara  *    notice, this list of conditions and the following disclaimer in the
132692e2e2Skiyohara  *    documentation and/or other materials provided with the distribution.
142692e2e2Skiyohara  *
152692e2e2Skiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162692e2e2Skiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
172692e2e2Skiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
182692e2e2Skiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
192692e2e2Skiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
202692e2e2Skiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
212692e2e2Skiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222692e2e2Skiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
232692e2e2Skiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
242692e2e2Skiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
252692e2e2Skiyohara  * POSSIBILITY OF SUCH DAMAGE.
262692e2e2Skiyohara  */
27*16031f7dSrin 
282692e2e2Skiyohara #include <sys/cdefs.h>
29*16031f7dSrin __KERNEL_RCSID(0, "$NetBSD: rgmii.c,v 1.2 2020/07/06 09:34:17 rin Exp $");
302692e2e2Skiyohara 
312692e2e2Skiyohara #include <sys/param.h>
322692e2e2Skiyohara #include <sys/bus.h>
332692e2e2Skiyohara #include <sys/device.h>
342692e2e2Skiyohara 
352692e2e2Skiyohara #include <net/if.h>
362692e2e2Skiyohara #include <net/if_media.h>
372692e2e2Skiyohara 
382692e2e2Skiyohara #include <powerpc/ibm4xx/dev/rgmiireg.h>
392692e2e2Skiyohara #include <powerpc/ibm4xx/dev/rmiivar.h>
402692e2e2Skiyohara #include <powerpc/ibm4xx/dev/opbvar.h>
412692e2e2Skiyohara 
422692e2e2Skiyohara 
432692e2e2Skiyohara static void rgmii_enable(device_t, int);
442692e2e2Skiyohara static void rgmii_disable(device_t, int);
452692e2e2Skiyohara static void rgmii_speed(device_t, int, int);
462692e2e2Skiyohara 
472692e2e2Skiyohara 
482692e2e2Skiyohara void
rgmii_attach(device_t self,int instance,void (** enable)(device_t,int),void (** disable)(device_t,int),void (** speed)(device_t,int,int))492692e2e2Skiyohara rgmii_attach(device_t self, int instance,
502692e2e2Skiyohara 	     void (**enable)(device_t, int),
512692e2e2Skiyohara 	     void (**disable)(device_t, int),
522692e2e2Skiyohara 	     void (**speed)(device_t, int, int))
532692e2e2Skiyohara {
542692e2e2Skiyohara 	struct opb_softc *sc = device_private(self);
552692e2e2Skiyohara 	uint32_t ssr;
562692e2e2Skiyohara 
572692e2e2Skiyohara 	instance %= 2;
582692e2e2Skiyohara 
592692e2e2Skiyohara 	rgmii_disable(self, instance);
602692e2e2Skiyohara 	ssr = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR);
612692e2e2Skiyohara 	ssr &= ~SSR_SP(instance, SSR_SP_MASK);
622692e2e2Skiyohara 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR, ssr);
632692e2e2Skiyohara 
642692e2e2Skiyohara 	*enable = rgmii_enable;
652692e2e2Skiyohara 	*disable = rgmii_disable;
662692e2e2Skiyohara 	*speed = rgmii_speed;
672692e2e2Skiyohara }
682692e2e2Skiyohara 
692692e2e2Skiyohara static void
rgmii_enable(device_t self,int instance)702692e2e2Skiyohara rgmii_enable(device_t self, int instance)
712692e2e2Skiyohara {
722692e2e2Skiyohara 	struct opb_softc *sc = device_private(self);
732692e2e2Skiyohara 	uint32_t fer;
742692e2e2Skiyohara 
752692e2e2Skiyohara 	instance %= 2;
762692e2e2Skiyohara 
772692e2e2Skiyohara 	fer = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER);
782692e2e2Skiyohara 	fer &= ~FER_MDIOEN_MASK;
792692e2e2Skiyohara 	fer |= FER_MDIOEN(instance);
802692e2e2Skiyohara 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER, fer);
812692e2e2Skiyohara }
822692e2e2Skiyohara 
832692e2e2Skiyohara static void
rgmii_disable(device_t self,int instance)842692e2e2Skiyohara rgmii_disable(device_t self, int instance)
852692e2e2Skiyohara {
862692e2e2Skiyohara 	struct opb_softc *sc = device_private(self);
872692e2e2Skiyohara 	uint32_t fer;
882692e2e2Skiyohara 
892692e2e2Skiyohara 	instance %= 2;
902692e2e2Skiyohara 
912692e2e2Skiyohara 	fer = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER);
922692e2e2Skiyohara 	fer &= ~FER_MDIOEN_MASK;
932692e2e2Skiyohara 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER, fer);
942692e2e2Skiyohara }
952692e2e2Skiyohara 
962692e2e2Skiyohara static void
rgmii_speed(device_t self,int instance,int speed)972692e2e2Skiyohara rgmii_speed(device_t self, int instance, int speed)
982692e2e2Skiyohara {
992692e2e2Skiyohara 	struct opb_softc *sc = device_private(self);
1002692e2e2Skiyohara 	uint32_t ssr;
1012692e2e2Skiyohara 
1022692e2e2Skiyohara 	instance %= 2;
1032692e2e2Skiyohara 
1042692e2e2Skiyohara 	ssr = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR);
1052692e2e2Skiyohara 	ssr &= ~SSR_SP(instance, SSR_SP_MASK);
1062692e2e2Skiyohara 	switch (speed) {
1072692e2e2Skiyohara 	case IFM_1000_T:
1082692e2e2Skiyohara 		ssr |= SSR_SP(instance, SSR_SP_1000MBPS);
1092692e2e2Skiyohara 		break;
1102692e2e2Skiyohara 	case IFM_100_TX:
1112692e2e2Skiyohara 		ssr |= SSR_SP(instance, SSR_SP_100MBPS);
1122692e2e2Skiyohara 		break;
1132692e2e2Skiyohara 	case IFM_10_T:
1142692e2e2Skiyohara 		ssr |= SSR_SP(instance, SSR_SP_10MBPS);
1152692e2e2Skiyohara 		break;
1162692e2e2Skiyohara 	}
1172692e2e2Skiyohara 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR, ssr);
1182692e2e2Skiyohara }
119