Home | History | Annotate | Line # | Download | only in mii
mii_bitbang.c revision 1.2
      1  1.2    lukem /*	$NetBSD: mii_bitbang.c,v 1.2 2001/04/30 01:19:40 lukem Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  thorpej  * NASA Ames Research Center.
     10  1.1  thorpej  *
     11  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.1  thorpej  * modification, are permitted provided that the following conditions
     13  1.1  thorpej  * are met:
     14  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.1  thorpej  *    must display the following acknowledgement:
     21  1.1  thorpej  *	This product includes software developed by the NetBSD
     22  1.1  thorpej  *	Foundation, Inc. and its contributors.
     23  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.1  thorpej  *    from this software without specific prior written permission.
     26  1.1  thorpej  *
     27  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.1  thorpej /*
     41  1.1  thorpej  * Common module for bit-bang'ing the MII.
     42  1.1  thorpej  */
     43  1.1  thorpej 
     44  1.1  thorpej #include <sys/param.h>
     45  1.1  thorpej #include <sys/device.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <dev/mii/mii.h>
     48  1.1  thorpej #include <dev/mii/mii_bitbang.h>
     49  1.1  thorpej 
     50  1.1  thorpej void	mii_bitbang_sync __P((struct device *, mii_bitbang_ops_t));
     51  1.1  thorpej void	mii_bitbang_sendbits __P((struct device *, mii_bitbang_ops_t,
     52  1.1  thorpej 	    u_int32_t, int));
     53  1.1  thorpej 
     54  1.1  thorpej #define	WRITE(x)							\
     55  1.1  thorpej do {									\
     56  1.1  thorpej 	ops->mbo_write(sc, (x));					\
     57  1.1  thorpej 	delay(1);							\
     58  1.2    lukem } while (/* CONSTCOND */ 0)
     59  1.1  thorpej 
     60  1.1  thorpej #define	READ		ops->mbo_read(sc)
     61  1.1  thorpej 
     62  1.1  thorpej #define	MDO		ops->mbo_bits[MII_BIT_MDO]
     63  1.1  thorpej #define	MDI		ops->mbo_bits[MII_BIT_MDI]
     64  1.1  thorpej #define	MDC		ops->mbo_bits[MII_BIT_MDC]
     65  1.1  thorpej #define	MDIRPHY		ops->mbo_bits[MII_BIT_DIR_HOST_PHY]
     66  1.1  thorpej #define	MDIRHOST	ops->mbo_bits[MII_BIT_DIR_PHY_HOST]
     67  1.1  thorpej 
     68  1.1  thorpej /*
     69  1.1  thorpej  * mii_bitbang_sync:
     70  1.1  thorpej  *
     71  1.1  thorpej  *	Synchronize the MII.
     72  1.1  thorpej  */
     73  1.1  thorpej void
     74  1.1  thorpej mii_bitbang_sync(sc, ops)
     75  1.1  thorpej 	struct device *sc;
     76  1.1  thorpej 	mii_bitbang_ops_t ops;
     77  1.1  thorpej {
     78  1.1  thorpej 	int i, v;
     79  1.1  thorpej 
     80  1.1  thorpej 	v = MDIRPHY | MDO;
     81  1.1  thorpej 
     82  1.1  thorpej 	WRITE(v);
     83  1.1  thorpej 	for (i = 0; i < 32; i++) {
     84  1.1  thorpej 		WRITE(v | MDC);
     85  1.1  thorpej 		WRITE(v);
     86  1.1  thorpej 	}
     87  1.1  thorpej }
     88  1.1  thorpej 
     89  1.1  thorpej /*
     90  1.1  thorpej  * mii_bitbang_sendbits:
     91  1.1  thorpej  *
     92  1.1  thorpej  *	Send a series of bits to the MII.
     93  1.1  thorpej  */
     94  1.1  thorpej void
     95  1.1  thorpej mii_bitbang_sendbits(sc, ops, data, nbits)
     96  1.1  thorpej 	struct device *sc;
     97  1.1  thorpej 	mii_bitbang_ops_t ops;
     98  1.1  thorpej 	u_int32_t data;
     99  1.1  thorpej 	int nbits;
    100  1.1  thorpej {
    101  1.1  thorpej 	int i, v;
    102  1.1  thorpej 
    103  1.1  thorpej 	v = MDIRPHY;
    104  1.1  thorpej 	WRITE(v);
    105  1.1  thorpej 
    106  1.1  thorpej 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
    107  1.1  thorpej 		if (data & i)
    108  1.1  thorpej 			v |= MDO;
    109  1.1  thorpej 		else
    110  1.1  thorpej 			v &= ~MDO;
    111  1.1  thorpej 		WRITE(v);
    112  1.1  thorpej 		WRITE(v | MDC);
    113  1.1  thorpej 		WRITE(v);
    114  1.1  thorpej 	}
    115  1.1  thorpej }
    116  1.1  thorpej 
    117  1.1  thorpej /*
    118  1.1  thorpej  * mii_bitbang_readreg:
    119  1.1  thorpej  *
    120  1.1  thorpej  *	Read a PHY register by bit-bang'ing the MII.
    121  1.1  thorpej  */
    122  1.1  thorpej int
    123  1.1  thorpej mii_bitbang_readreg(sc, ops, phy, reg)
    124  1.1  thorpej 	struct device *sc;
    125  1.1  thorpej 	mii_bitbang_ops_t ops;
    126  1.1  thorpej 	int phy, reg;
    127  1.1  thorpej {
    128  1.1  thorpej 	int val = 0, err = 0, i;
    129  1.1  thorpej 
    130  1.1  thorpej 	mii_bitbang_sync(sc, ops);
    131  1.1  thorpej 
    132  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
    133  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_READ, 2);
    134  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, phy, 5);
    135  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, reg, 5);
    136  1.1  thorpej 
    137  1.1  thorpej 	/* Switch direction to PHY->host, without a clock transition. */
    138  1.1  thorpej 	WRITE(MDIRHOST);
    139  1.1  thorpej 
    140  1.1  thorpej 	/* Turnaround clock. */
    141  1.1  thorpej 	WRITE(MDIRHOST | MDC);
    142  1.1  thorpej 	WRITE(MDIRHOST);
    143  1.1  thorpej 
    144  1.1  thorpej 	/* Check for error. */
    145  1.1  thorpej 	err = READ & MDI;
    146  1.1  thorpej 
    147  1.1  thorpej 	/* Idle clock. */
    148  1.1  thorpej 	WRITE(MDIRHOST | MDC);
    149  1.1  thorpej 	WRITE(MDIRHOST);
    150  1.1  thorpej 
    151  1.1  thorpej 	for (i = 0; i < 16; i++) {
    152  1.1  thorpej 		val <<= 1;
    153  1.1  thorpej 		/* Read data prior to clock low-high transition. */
    154  1.1  thorpej 		if (err == 0 && (READ & MDI) != 0)
    155  1.1  thorpej 			val |= 1;
    156  1.1  thorpej 
    157  1.1  thorpej 		WRITE(MDIRHOST | MDC);
    158  1.1  thorpej 		WRITE(MDIRHOST);
    159  1.1  thorpej 	}
    160  1.1  thorpej 
    161  1.1  thorpej 	/* Set direction to host->PHY, without a clock transition. */
    162  1.1  thorpej 	WRITE(MDIRPHY);
    163  1.1  thorpej 
    164  1.1  thorpej 	return (err ? 0 : val);
    165  1.1  thorpej }
    166  1.1  thorpej 
    167  1.1  thorpej /*
    168  1.1  thorpej  * mii_bitbang_writereg:
    169  1.1  thorpej  *
    170  1.1  thorpej  *	Write a PHY register by bit-bang'ing the MII.
    171  1.1  thorpej  */
    172  1.1  thorpej void
    173  1.1  thorpej mii_bitbang_writereg(sc, ops, phy, reg, val)
    174  1.1  thorpej 	struct device *sc;
    175  1.1  thorpej 	mii_bitbang_ops_t ops;
    176  1.1  thorpej 	int phy, reg, val;
    177  1.1  thorpej {
    178  1.1  thorpej 
    179  1.1  thorpej 	mii_bitbang_sync(sc, ops);
    180  1.1  thorpej 
    181  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
    182  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_WRITE, 2);
    183  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, phy, 5);
    184  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, reg, 5);
    185  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_ACK, 2);
    186  1.1  thorpej 	mii_bitbang_sendbits(sc, ops, val, 16);
    187  1.1  thorpej 
    188  1.1  thorpej 	WRITE(MDIRPHY);
    189  1.1  thorpej }
    190