Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_mdio.c revision 1.1.4.2
      1 /*-
      2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #define MII_PRIVATE
     31 
     32 #include "locators.h"
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_mdio.c,v 1.1.4.2 2012/10/30 17:18:59 yamt Exp $");
     37 
     38 #include <sys/bus.h>
     39 #include <sys/device.h>
     40 #include <sys/intr.h>
     41 #include <sys/systm.h>
     42 
     43 #include <arm/broadcom/bcm53xx_reg.h>
     44 #include <arm/broadcom/bcm53xx_var.h>
     45 
     46 static int bcmmdio_ccb_match(device_t, cfdata_t, void *);
     47 static void bcmmdio_ccb_attach(device_t, device_t, void *);
     48 
     49 CFATTACH_DECL_NEW(bcmmdio_ccb, sizeof(struct bcmmdio_softc),
     50 	bcmmdio_ccb_match, bcmmdio_ccb_attach, NULL, NULL);
     51 
     52 static inline uint32_t
     53 bcmmdio_read_4(struct bcmmdio_softc *sc, bus_size_t o)
     54 {
     55 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
     56 }
     57 
     58 static inline void
     59 bcmmdio_write_4(struct bcmmdio_softc *sc, bus_size_t o, uint32_t v)
     60 {
     61 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
     62 }
     63 
     64 static int
     65 bcmmdio_ccb_match(device_t parent, cfdata_t cf, void *aux)
     66 {
     67 	struct bcmccb_attach_args * const ccbaa = aux;
     68 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
     69 
     70 	if (strcmp(cf->cf_name, loc->loc_name))
     71 		return 0;
     72 
     73 	KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
     74 
     75 	return 1;
     76 }
     77 
     78 static void
     79 bcmmdio_ccb_attach(device_t parent, device_t self, void *aux)
     80 {
     81 	struct bcmmdio_softc * const sc = device_private(self);
     82 	struct bcmccb_attach_args * const ccbaa = aux;
     83 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
     84 
     85 	sc->sc_dev = self;
     86 
     87 	sc->sc_bst = ccbaa->ccbaa_ccb_bst;
     88 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
     89 
     90 	bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
     91 	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
     92 
     93 	uint32_t miimgt = bcmmdio_read_4(sc, MIIMGT);
     94 	uint32_t div = __SHIFTOUT(miimgt, MIIMGT_MDCDIV);
     95 	if (div == 0) {
     96 		div = 33;			// divisor to get 2MHz
     97 		miimgt &= ~MIIMGT_MDCDIV;
     98 		miimgt |= __SHIFTIN(div, MIIMGT_MDCDIV);
     99 		bcmmdio_write_4(sc, MIIMGT, miimgt);
    100 	}
    101 	uint32_t freq = 66000000 / div;
    102 
    103 	aprint_naive("\n");
    104 	aprint_normal(": MDIO bus @ %u MHz\n", freq / 1000000);
    105 }
    106 
    107 static void
    108 bcmmdio_select_bus(struct bcmmdio_softc *sc, int phy)
    109 {
    110 	uint32_t mgt = bcmmdio_read_4(sc, MIIMGT);
    111 	uint32_t new_mgt = mgt;
    112 
    113 	KASSERT((mgt & MIIMGT_BSY) == 0);
    114 	if (__BIT(phy) & MII_INTERNAL) {
    115 		new_mgt &= ~MIIMGT_EXT;
    116 	} else {
    117 		new_mgt |= MIIMGT_EXT;
    118 	}
    119 
    120 	if (mgt != new_mgt)
    121 		bcmmdio_write_4(sc, MIIMGT, new_mgt);
    122 }
    123 
    124 static void
    125 bcmmdio_busywait(struct bcmmdio_softc *sc)
    126 {
    127 	while (bcmmdio_read_4(sc, MIIMGT) & MIIMGT_BSY) {
    128 		delay(1);
    129 	}
    130 }
    131 
    132 int
    133 bcmmdio_mii_readreg(device_t self, int phy, int reg)
    134 {
    135 	struct bcmmdio_softc * const sc = device_private(self);
    136 
    137 	mutex_enter(sc->sc_lock);
    138 
    139 	bcmmdio_select_bus(sc, phy);
    140 
    141 	bcmmdio_write_4(sc, MIICMD, MIICMD_RD(phy, reg));
    142 
    143 	bcmmdio_busywait(sc);
    144 
    145 	int val = __SHIFTOUT(bcmmdio_read_4(sc, MIICMD), MIICMD_DATA);
    146 	mutex_exit(sc->sc_lock);
    147 	return val;
    148 }
    149 
    150 void
    151 bcmmdio_mii_writereg(device_t self, int phy, int reg, int val)
    152 {
    153 	struct bcmmdio_softc * const sc = device_private(self);
    154 
    155 	KASSERT((val & 0xffff) == val);
    156 
    157 	mutex_enter(sc->sc_lock);
    158 
    159 	bcmmdio_select_bus(sc, phy);
    160 
    161 	bcmmdio_write_4(sc, MIICMD, MIICMD_WR(phy, reg, val));
    162 
    163 	bcmmdio_busywait(sc);
    164 
    165 	mutex_exit(sc->sc_lock);
    166 }
    167