iPXE
mii_bit.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Sylvie Barlow <sylvie.c.barlow@gmail.com>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <unistd.h>
28 #include <ipxe/bitbash.h>
29 #include <ipxe/mii_bit.h>
30 
31 /**
32  * Transfer bits over MII bit-bashing interface
33  *
34  * @v basher Bit basher
35  * @v mask Mask
36  * @v write Data to write
37  * @ret read Data read
38  */
39 static uint32_t mii_bit_xfer ( struct bit_basher *basher,
40  uint32_t mask, uint32_t write ) {
41  uint32_t read = 0;
42  int bit;
43 
44  for ( ; mask ; mask >>= 1 ) {
45 
46  /* Delay */
47  udelay ( 1 );
48 
49  /* Write bit to basher */
50  write_bit ( basher, MII_BIT_MDIO, ( write & mask ) );
51 
52  /* Read bit from basher */
53  bit = read_bit ( basher, MII_BIT_MDIO );
54  read <<= 1;
55  read |= ( bit & 1 );
56 
57  /* Set clock high */
58  write_bit ( basher, MII_BIT_MDC, 1 );
59 
60  /* Delay */
61  udelay ( 1 );
62 
63  /* Set clock low */
64  write_bit ( basher, MII_BIT_MDC, 0 );
65  }
66  return read;
67 }
68 
69 /**
70  * Read or write via MII bit-bashing interface
71  *
72  * @v basher Bit basher
73  * @v phy PHY address
74  * @v reg Register address
75  * @v data Data to write
76  * @v cmd Command
77  * @ret data Data read
78  */
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 ) {
82 
83  /* Initiate drive for write */
84  write_bit ( basher, MII_BIT_DRIVE, 1 );
85 
86  /* Write start */
88 
89  /* Write command */
90  mii_bit_xfer ( basher, MII_BIT_CMD_MASK, cmd );
91 
92  /* Write PHY address */
93  mii_bit_xfer ( basher, MII_BIT_PHY_MASK, phy );
94 
95  /* Write register address */
96  mii_bit_xfer ( basher, MII_BIT_REG_MASK, reg );
97 
98  /* Switch drive to read if applicable */
99  write_bit ( basher, MII_BIT_DRIVE, ( cmd & MII_BIT_CMD_RW ) );
100 
101  /* Allow space for turnaround */
103 
104  /* Read or write data */
105  data = mii_bit_xfer (basher, MII_BIT_DATA_MASK, data );
106 
107  /* Initiate drive for read */
108  write_bit ( basher, MII_BIT_DRIVE, 0 );
109 
110  return data;
111 }
112 
113 /**
114  * Read from MII register
115  *
116  * @v mdio MII interface
117  * @v phy PHY address
118  * @v reg Register address
119  * @ret data Data read, or negative error
120  */
121 static int mii_bit_read ( struct mii_interface *mdio, unsigned int phy,
122  unsigned int reg ) {
123  struct mii_bit_basher *miibit =
124  container_of ( mdio, struct mii_bit_basher, mdio );
125  struct bit_basher *basher = &miibit->basher;
126 
127  return mii_bit_rw ( basher, phy, reg, 0, MII_BIT_CMD_READ );
128 }
129 
130 /**
131  * Write to MII register
132  *
133  * @v mdio MII interface
134  * @v phy PHY address
135  * @v reg Register address
136  * @v data Data to write
137  * @ret rc Return status code
138  */
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;
144 
145  mii_bit_rw ( basher, phy, reg, data, MII_BIT_CMD_WRITE );
146  return 0;
147 }
148 
149 /** MII bit basher operations */
150 static struct mii_operations mii_bit_op = {
151  .read = mii_bit_read,
152  .write = mii_bit_write,
153 };
154 
155 /**
156  * Initialise bit-bashing interface
157  *
158  * @v miibit MII bit basher
159  */
160 void init_mii_bit_basher ( struct mii_bit_basher *miibit ) {
161  mdio_init ( &miibit->mdio, &mii_bit_op );
162 };
struct option_descriptor read[1]
Definition: nvo_cmd.c:115
#define MII_BIT_DATA_MASK
Data mask.
Definition: mii_bit.h:30
struct mii_interface mdio
MII interface.
Definition: mii_bit.h:35
MII data direction.
Definition: mii_bit.h:47
static unsigned int unsigned int reg
Definition: myson.h:162
static unsigned int unsigned int bit
Definition: bigint.h:208
void init_mii_bit_basher(struct mii_bit_basher *miibit)
Initialise bit-bashing interface.
Definition: mii_bit.c:160
static unsigned int mii_bit_rw(struct bit_basher *basher, unsigned int phy, unsigned int reg, unsigned int data, unsigned int cmd)
Read or write via MII bit-bashing interface.
Definition: mii_bit.c:79
int(* read)(struct mii_interface *mdio, unsigned int phy, unsigned int reg)
Read from MII register.
Definition: mii.h:27
MII data.
Definition: mii_bit.h:45
#define MII_BIT_START_MASK
Start mask.
Definition: mii_bit.h:16
#define MII_BIT_SWITCH_MASK
Switch mask.
Definition: mii_bit.h:28
static void mdio_init(struct mii_interface *mdio, struct mii_operations *op)
Initialise MII interface.
Definition: mii.h:63
MII clock.
Definition: mii_bit.h:43
#define MII_BIT_CMD_RW
Command read or write.
Definition: mii_bit.h:21
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static int mii_bit_write(struct mii_interface *mdio, unsigned int phy, unsigned int reg, unsigned int data)
Write to MII register.
Definition: mii_bit.c:139
#define MII_BIT_CMD_WRITE
Command write.
Definition: mii_bit.h:20
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60
Bit-bashing interfaces.
MII bit-bashing interface.
A bit-bashing interface.
Definition: bitbash.h:55
#define MII_BIT_SWITCH
Switch.
Definition: mii_bit.h:27
device nvs write
Definition: threewire.h:61
#define MII_BIT_CMD_READ
Command read.
Definition: mii_bit.h:19
static struct mii_operations mii_bit_op
MII bit basher operations.
Definition: mii_bit.c:150
unsigned int uint32_t
Definition: stdint.h:12
static uint32_t mii_bit_xfer(struct bit_basher *basher, uint32_t mask, uint32_t write)
Transfer bits over MII bit-bashing interface.
Definition: mii_bit.c:39
int read_bit(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition: bitbash.c:60
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
MII interface operations.
Definition: mii.h:18
#define MII_BIT_PHY_MASK
PHY mask.
Definition: mii_bit.h:23
#define MII_BIT_CMD_MASK
Command mask.
Definition: mii_bit.h:18
struct bit_basher basher
Bit-bashing interface.
Definition: mii_bit.h:37
An MII interface.
Definition: mii.h:43
uint8_t data[48]
Additional event data.
Definition: ena.h:22
A bit-bashing MII interface.
Definition: mii_bit.h:33
#define MII_BIT_START
Start.
Definition: mii_bit.h:15
#define MII_BIT_REG_MASK
Register mask.
Definition: mii_bit.h:25
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
static int mii_bit_read(struct mii_interface *mdio, unsigned int phy, unsigned int reg)
Read from MII register.
Definition: mii_bit.c:121
void write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition: bitbash.c:44