Home | History | Annotate | Line # | Download | only in mii
mii_bitbang.c revision 1.10.72.1
      1  1.10.72.1     yamt /*	$NetBSD: mii_bitbang.c,v 1.10.72.1 2008/05/18 12:34:13 yamt 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  *
     20        1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21        1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24        1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25        1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26        1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27        1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1  thorpej  */
     32        1.1  thorpej 
     33        1.1  thorpej /*
     34        1.1  thorpej  * Common module for bit-bang'ing the MII.
     35        1.1  thorpej  */
     36        1.5    lukem 
     37        1.5    lukem #include <sys/cdefs.h>
     38  1.10.72.1     yamt __KERNEL_RCSID(0, "$NetBSD: mii_bitbang.c,v 1.10.72.1 2008/05/18 12:34:13 yamt Exp $");
     39        1.1  thorpej 
     40        1.1  thorpej #include <sys/param.h>
     41        1.1  thorpej #include <sys/device.h>
     42        1.1  thorpej 
     43        1.1  thorpej #include <dev/mii/mii.h>
     44        1.1  thorpej #include <dev/mii/mii_bitbang.h>
     45        1.1  thorpej 
     46        1.1  thorpej #define	WRITE(x)							\
     47        1.1  thorpej do {									\
     48        1.1  thorpej 	ops->mbo_write(sc, (x));					\
     49        1.1  thorpej 	delay(1);							\
     50        1.2    lukem } while (/* CONSTCOND */ 0)
     51        1.1  thorpej 
     52        1.1  thorpej #define	READ		ops->mbo_read(sc)
     53        1.1  thorpej 
     54        1.1  thorpej #define	MDO		ops->mbo_bits[MII_BIT_MDO]
     55        1.1  thorpej #define	MDI		ops->mbo_bits[MII_BIT_MDI]
     56        1.1  thorpej #define	MDC		ops->mbo_bits[MII_BIT_MDC]
     57        1.1  thorpej #define	MDIRPHY		ops->mbo_bits[MII_BIT_DIR_HOST_PHY]
     58        1.1  thorpej #define	MDIRHOST	ops->mbo_bits[MII_BIT_DIR_PHY_HOST]
     59        1.1  thorpej 
     60        1.1  thorpej /*
     61        1.1  thorpej  * mii_bitbang_sync:
     62        1.1  thorpej  *
     63        1.1  thorpej  *	Synchronize the MII.
     64        1.1  thorpej  */
     65        1.6  thorpej static void
     66  1.10.72.1     yamt mii_bitbang_sync(device_t sc, mii_bitbang_ops_t ops)
     67        1.1  thorpej {
     68        1.4    perry 	int i;
     69        1.4    perry 	u_int32_t v;
     70        1.1  thorpej 
     71        1.1  thorpej 	v = MDIRPHY | MDO;
     72        1.1  thorpej 
     73        1.1  thorpej 	WRITE(v);
     74        1.1  thorpej 	for (i = 0; i < 32; i++) {
     75        1.1  thorpej 		WRITE(v | MDC);
     76        1.1  thorpej 		WRITE(v);
     77        1.1  thorpej 	}
     78        1.1  thorpej }
     79        1.1  thorpej 
     80        1.1  thorpej /*
     81        1.1  thorpej  * mii_bitbang_sendbits:
     82        1.1  thorpej  *
     83        1.1  thorpej  *	Send a series of bits to the MII.
     84        1.1  thorpej  */
     85        1.6  thorpej static void
     86  1.10.72.1     yamt mii_bitbang_sendbits(device_t sc, mii_bitbang_ops_t ops, uint32_t data,
     87        1.3  thorpej     int nbits)
     88        1.1  thorpej {
     89        1.4    perry 	int i;
     90        1.4    perry 	u_int32_t v;
     91        1.1  thorpej 
     92        1.1  thorpej 	v = MDIRPHY;
     93        1.1  thorpej 	WRITE(v);
     94        1.1  thorpej 
     95        1.1  thorpej 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
     96        1.1  thorpej 		if (data & i)
     97        1.1  thorpej 			v |= MDO;
     98        1.1  thorpej 		else
     99        1.1  thorpej 			v &= ~MDO;
    100        1.1  thorpej 		WRITE(v);
    101        1.1  thorpej 		WRITE(v | MDC);
    102        1.1  thorpej 		WRITE(v);
    103        1.1  thorpej 	}
    104        1.1  thorpej }
    105        1.1  thorpej 
    106        1.1  thorpej /*
    107        1.1  thorpej  * mii_bitbang_readreg:
    108        1.1  thorpej  *
    109        1.1  thorpej  *	Read a PHY register by bit-bang'ing the MII.
    110        1.1  thorpej  */
    111        1.1  thorpej int
    112  1.10.72.1     yamt mii_bitbang_readreg(device_t sc, mii_bitbang_ops_t ops, int phy, int reg)
    113        1.1  thorpej {
    114        1.1  thorpej 	int val = 0, err = 0, i;
    115        1.1  thorpej 
    116        1.1  thorpej 	mii_bitbang_sync(sc, ops);
    117        1.1  thorpej 
    118        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
    119        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_READ, 2);
    120        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, phy, 5);
    121        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, reg, 5);
    122        1.1  thorpej 
    123        1.1  thorpej 	/* Switch direction to PHY->host, without a clock transition. */
    124        1.1  thorpej 	WRITE(MDIRHOST);
    125        1.1  thorpej 
    126        1.1  thorpej 	/* Turnaround clock. */
    127        1.1  thorpej 	WRITE(MDIRHOST | MDC);
    128        1.1  thorpej 	WRITE(MDIRHOST);
    129        1.1  thorpej 
    130        1.1  thorpej 	/* Check for error. */
    131        1.1  thorpej 	err = READ & MDI;
    132        1.1  thorpej 
    133        1.1  thorpej 	/* Idle clock. */
    134        1.1  thorpej 	WRITE(MDIRHOST | MDC);
    135        1.1  thorpej 	WRITE(MDIRHOST);
    136        1.1  thorpej 
    137        1.1  thorpej 	for (i = 0; i < 16; i++) {
    138        1.1  thorpej 		val <<= 1;
    139        1.1  thorpej 		/* Read data prior to clock low-high transition. */
    140        1.1  thorpej 		if (err == 0 && (READ & MDI) != 0)
    141        1.1  thorpej 			val |= 1;
    142        1.1  thorpej 
    143        1.1  thorpej 		WRITE(MDIRHOST | MDC);
    144        1.1  thorpej 		WRITE(MDIRHOST);
    145        1.1  thorpej 	}
    146        1.1  thorpej 
    147        1.1  thorpej 	/* Set direction to host->PHY, without a clock transition. */
    148        1.1  thorpej 	WRITE(MDIRPHY);
    149        1.1  thorpej 
    150        1.1  thorpej 	return (err ? 0 : val);
    151        1.1  thorpej }
    152        1.1  thorpej 
    153        1.1  thorpej /*
    154        1.1  thorpej  * mii_bitbang_writereg:
    155        1.1  thorpej  *
    156        1.1  thorpej  *	Write a PHY register by bit-bang'ing the MII.
    157        1.1  thorpej  */
    158        1.1  thorpej void
    159  1.10.72.1     yamt mii_bitbang_writereg(device_t sc, mii_bitbang_ops_t ops, int phy,
    160  1.10.72.1     yamt 	int reg, int val)
    161        1.1  thorpej {
    162        1.1  thorpej 
    163        1.1  thorpej 	mii_bitbang_sync(sc, ops);
    164        1.1  thorpej 
    165        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
    166        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_WRITE, 2);
    167        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, phy, 5);
    168        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, reg, 5);
    169        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_ACK, 2);
    170        1.1  thorpej 	mii_bitbang_sendbits(sc, ops, val, 16);
    171        1.1  thorpej 
    172        1.1  thorpej 	WRITE(MDIRPHY);
    173        1.1  thorpej }
    174