Home | History | Annotate | Line # | Download | only in mii
mii_bitbang.c revision 1.12.88.1
      1  1.12.88.1  christos /*	$NetBSD: mii_bitbang.c,v 1.12.88.1 2019/06/10 22:07:14 christos 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.12.88.1  christos __KERNEL_RCSID(0, "$NetBSD: mii_bitbang.c,v 1.12.88.1 2019/06/10 22:07:14 christos 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.12   xtraeme mii_bitbang_sync(device_t sc, mii_bitbang_ops_t ops)
     67        1.1   thorpej {
     68        1.4     perry 	int i;
     69  1.12.88.1  christos 	uint32_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.12   xtraeme 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.12.88.1  christos 	uint32_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.12.88.1  christos mii_bitbang_readreg(device_t sc, mii_bitbang_ops_t ops, int phy, int reg,
    113  1.12.88.1  christos 	uint16_t *val)
    114        1.1   thorpej {
    115  1.12.88.1  christos 	int err = 0, i;
    116  1.12.88.1  christos 	uint16_t data = 0;
    117        1.1   thorpej 
    118        1.1   thorpej 	mii_bitbang_sync(sc, ops);
    119        1.1   thorpej 
    120        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
    121        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_READ, 2);
    122        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, phy, 5);
    123        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, reg, 5);
    124        1.1   thorpej 
    125        1.1   thorpej 	/* Switch direction to PHY->host, without a clock transition. */
    126        1.1   thorpej 	WRITE(MDIRHOST);
    127        1.1   thorpej 
    128        1.1   thorpej 	/* Turnaround clock. */
    129        1.1   thorpej 	WRITE(MDIRHOST | MDC);
    130        1.1   thorpej 	WRITE(MDIRHOST);
    131        1.1   thorpej 
    132        1.1   thorpej 	/* Check for error. */
    133        1.1   thorpej 	err = READ & MDI;
    134        1.1   thorpej 
    135        1.1   thorpej 	/* Idle clock. */
    136        1.1   thorpej 	WRITE(MDIRHOST | MDC);
    137        1.1   thorpej 	WRITE(MDIRHOST);
    138        1.1   thorpej 
    139        1.1   thorpej 	for (i = 0; i < 16; i++) {
    140  1.12.88.1  christos 		data <<= 1;
    141        1.1   thorpej 		/* Read data prior to clock low-high transition. */
    142        1.1   thorpej 		if (err == 0 && (READ & MDI) != 0)
    143  1.12.88.1  christos 			data |= 1;
    144        1.1   thorpej 
    145        1.1   thorpej 		WRITE(MDIRHOST | MDC);
    146        1.1   thorpej 		WRITE(MDIRHOST);
    147        1.1   thorpej 	}
    148        1.1   thorpej 
    149        1.1   thorpej 	/* Set direction to host->PHY, without a clock transition. */
    150        1.1   thorpej 	WRITE(MDIRPHY);
    151        1.1   thorpej 
    152  1.12.88.1  christos 	if (err == 0)
    153  1.12.88.1  christos 		*val = data;
    154  1.12.88.1  christos 	else
    155  1.12.88.1  christos 		err = -1;
    156  1.12.88.1  christos 
    157  1.12.88.1  christos 	return err;
    158        1.1   thorpej }
    159        1.1   thorpej 
    160        1.1   thorpej /*
    161        1.1   thorpej  * mii_bitbang_writereg:
    162        1.1   thorpej  *
    163        1.1   thorpej  *	Write a PHY register by bit-bang'ing the MII.
    164        1.1   thorpej  */
    165  1.12.88.1  christos int
    166  1.12.88.1  christos mii_bitbang_writereg(device_t sc, mii_bitbang_ops_t ops, int phy, int reg,
    167  1.12.88.1  christos     uint16_t val)
    168        1.1   thorpej {
    169        1.1   thorpej 
    170        1.1   thorpej 	mii_bitbang_sync(sc, ops);
    171        1.1   thorpej 
    172        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
    173        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_WRITE, 2);
    174        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, phy, 5);
    175        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, reg, 5);
    176        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, MII_COMMAND_ACK, 2);
    177        1.1   thorpej 	mii_bitbang_sendbits(sc, ops, val, 16);
    178        1.1   thorpej 
    179        1.1   thorpej 	WRITE(MDIRPHY);
    180  1.12.88.1  christos 
    181  1.12.88.1  christos 	return 0;
    182        1.1   thorpej }
    183