2 * Copyright (C) 2018 Sylvie Barlow <sylvie.c.barlow@gmail.com>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
28 #include <ipxe/bitbash.h>
29 #include <ipxe/mii_bit.h>
32 * Transfer bits over MII bit-bashing interface
34 * @v basher Bit basher
36 * @v write Data to write
39 static uint32_t mii_bit_xfer ( struct bit_basher
*basher
,
40 uint32_t mask
, uint32_t write
) {
44 for ( ; mask
; mask
>>= 1 ) {
49 /* Write bit to basher */
50 write_bit ( basher
, MII_BIT_MDIO
, ( write
& mask
) );
52 /* Read bit from basher */
53 bit
= read_bit ( basher
, MII_BIT_MDIO
);
58 write_bit ( basher
, MII_BIT_MDC
, 1 );
64 write_bit ( basher
, MII_BIT_MDC
, 0 );
70 * Read or write via MII bit-bashing interface
72 * @v basher Bit basher
74 * @v reg Register address
75 * @v data Data to write
79 static unsigned int mii_bit_rw ( struct bit_basher
*basher
,
80 unsigned int phy
, unsigned int reg
,
81 unsigned int data
, unsigned int cmd
) {
83 /* Initiate drive for write */
84 write_bit ( basher
, MII_BIT_DRIVE
, 1 );
87 mii_bit_xfer ( basher
, MII_BIT_START_MASK
, MII_BIT_START
);
90 mii_bit_xfer ( basher
, MII_BIT_CMD_MASK
, cmd
);
92 /* Write PHY address */
93 mii_bit_xfer ( basher
, MII_BIT_PHY_MASK
, phy
);
95 /* Write register address */
96 mii_bit_xfer ( basher
, MII_BIT_REG_MASK
, reg
);
98 /* Switch drive to read if applicable */
99 write_bit ( basher
, MII_BIT_DRIVE
, ( cmd
& MII_BIT_CMD_RW
) );
101 /* Allow space for turnaround */
102 mii_bit_xfer ( basher
, MII_BIT_SWITCH_MASK
, MII_BIT_SWITCH
);
104 /* Read or write data */
105 data
= mii_bit_xfer (basher
, MII_BIT_DATA_MASK
, data
);
107 /* Initiate drive for read */
108 write_bit ( basher
, MII_BIT_DRIVE
, 0 );
114 * Read from MII register
116 * @v mdio MII interface
118 * @v reg Register address
119 * @ret data Data read, or negative error
121 static int mii_bit_read ( struct mii_interface
*mdio
, unsigned int phy
,
123 struct mii_bit_basher
*miibit
=
124 container_of ( mdio
, struct mii_bit_basher
, mdio
);
125 struct bit_basher
*basher
= &miibit
->basher
;
127 return mii_bit_rw ( basher
, phy
, reg
, 0, MII_BIT_CMD_READ
);
131 * Write to MII register
133 * @v mdio MII interface
135 * @v reg Register address
136 * @v data Data to write
137 * @ret rc Return status code
139 static int mii_bit_write ( struct mii_interface
*mdio
, unsigned int phy
,
140 unsigned int reg
, unsigned int data
) {
141 struct mii_bit_basher
*miibit
=
142 container_of ( mdio
, struct mii_bit_basher
, mdio
);
143 struct bit_basher
*basher
= &miibit
->basher
;
145 mii_bit_rw ( basher
, phy
, reg
, data
, MII_BIT_CMD_WRITE
);
149 /** MII bit basher operations */
150 static struct mii_operations mii_bit_op
= {
151 .read
= mii_bit_read
,
152 .write
= mii_bit_write
,
156 * Initialise bit-bashing interface
158 * @v miibit MII bit basher
160 void init_mii_bit_basher ( struct mii_bit_basher
*miibit
) {
161 mdio_init ( &miibit
->mdio
, &mii_bit_op
);