1//
2// Copyright 2016 Ettus Research
3// Copyright 2018 Ettus Research, a National Instruments Company
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6//
7
8module mdio_master #(
9   parameter        REG_AWIDTH  = 32,
10   parameter        REG_BASE    = 'h0,
11   parameter [7:0]  MDC_DIVIDER = 8'd200
12) (
13   // Clock and reset
14   input                  clk,
15   input                  rst,
16   // MDIO ports
17   output reg             mdc,
18   output reg             mdio_out,
19   output reg             mdio_tri,   // Assert to tristate driver.
20   input                  mdio_in,
21   // Register ports
22   input                  reg_wr_req,
23   input [REG_AWIDTH-1:0] reg_wr_addr,
24   input [31:0]           reg_wr_data,
25   input                  reg_rd_req,
26   input [REG_AWIDTH-1:0] reg_rd_addr,
27   output reg             reg_rd_resp,
28   output reg [31:0]      reg_rd_data
29);
30
31   localparam [7:0]
32      IDLE = 0,
33      PREAMBLE1 = 1,
34      PREAMBLE2 = 2,
35      PREAMBLE3 = 3,
36      PREAMBLE4 = 4,
37      PREAMBLE5 = 5,
38      PREAMBLE6 = 6,
39      PREAMBLE7 = 7,
40      PREAMBLE8 = 8,
41      PREAMBLE9 = 9,
42      PREAMBLE10 = 10,
43      PREAMBLE11 = 11,
44      PREAMBLE12 = 12,
45      PREAMBLE13 = 13,
46      PREAMBLE14 = 14,
47      PREAMBLE15 = 15,
48      PREAMBLE16 = 16,
49      PREAMBLE17 = 17,
50      PREAMBLE18 = 18,
51      PREAMBLE19 = 19,
52      PREAMBLE20 = 20,
53      PREAMBLE21 = 21,
54      PREAMBLE22 = 22,
55      PREAMBLE23 = 23,
56      PREAMBLE24 = 24,
57      PREAMBLE25 = 25,
58      PREAMBLE26 = 26,
59      PREAMBLE27 = 27,
60      PREAMBLE28 = 28,
61      PREAMBLE29 = 29,
62      PREAMBLE30 = 30,
63      PREAMBLE31 = 31,
64      PREAMBLE32 = 32,
65      START1 = 33,
66      C22_START2 = 34,
67      C45_START2 = 35,
68      OP1 = 36,
69      OP2 = 37,
70      PRTAD1 = 38,
71      PRTAD2 = 39,
72      PRTAD3 = 40,
73      PRTAD4 = 41,
74      PRTAD5 = 42,
75      DEVAD1 = 43,
76      DEVAD2 = 44,
77      DEVAD3 = 45,
78      DEVAD4 = 46,
79      DEVAD5 = 47,
80      TA1 = 48,
81      TA2 = 49,
82      TA3 = 50,
83      READ1 = 51,
84      READ2 = 52,
85      READ3 = 53,
86      READ4 = 54,
87      READ5 = 55,
88      READ6 = 56,
89      READ7 = 57,
90      READ8 = 58,
91      READ9 = 59,
92      READ10 = 60,
93      READ11 = 61,
94      READ12 = 62,
95      READ13 = 63,
96      READ14 = 64,
97      READ15 = 65,
98      READ16 = 66,
99      WRITE1 = 67,
100      WRITE2 = 68,
101      WRITE3 = 69,
102      WRITE4 = 70,
103      WRITE5 = 71,
104      WRITE6 = 72,
105      WRITE7 = 73,
106      WRITE8 = 74,
107      WRITE9 = 75,
108      WRITE10 = 76,
109      WRITE11 = 77,
110      WRITE12 = 78,
111      WRITE13 = 79,
112      WRITE14 = 80,
113      WRITE15 = 81,
114      WRITE16 = 82,
115      C45_ADDR1 = 83,
116      C45_ADDR2 = 84,
117      C45_ADDR3 = 85,
118      C45_ADDR4 = 86,
119      C45_ADDR5 = 87,
120      C45_ADDR6 = 88,
121      C45_ADDR7 = 89,
122      C45_ADDR8 = 90,
123      C45_ADDR9 = 91,
124      C45_ADDR10 = 92,
125      C45_ADDR11 = 93,
126      C45_ADDR12 = 94,
127      C45_ADDR13 = 95,
128      C45_ADDR14 = 96,
129      C45_ADDR15 = 97,
130      C45_ADDR16 = 98,
131      PREIDLE = 99
132   ;
133
134   localparam REG_MDIO_DATA         = REG_BASE + 'h0;
135   localparam REG_MDIO_ADDR         = REG_BASE + 'h4;
136   localparam REG_MDIO_OP           = REG_BASE + 'h8;
137   localparam REG_MDIO_CTRL_STATUS  = REG_BASE + 'hC;
138
139   reg [15:0]  mdio_read_data, mdio_write_data;
140   reg [15:0]  mdio_address;
141   reg [12:0]  mdio_operation;
142   reg [7:0]   mdc_clk_count;
143   reg         mdc_falling_edge;
144   reg         mdio_running;
145   reg         mdio_done;
146   reg [7:0]   state;
147
148   always @(posedge clk) begin
149      if (rst) begin
150         mdio_write_data <= 16'h0;
151         mdio_address    <= 16'h0;
152         mdio_operation  <= 13'h0;
153         mdio_running    <= 1'b0;
154      end else begin
155         // Handshake to MDIO state machine to reset running flag in status.
156         // Wait for falling MDC edge to prevent S/W race condition occuring
157         // where done flag still asserted but running flag now cleared (repeatedly).
158         if (mdio_done && mdc_falling_edge)
159            mdio_running <= 1'b0;
160
161         // Readable registers
162         if (reg_rd_req) begin
163            reg_rd_resp <= 1'b1;
164            case (reg_rd_addr)
165               REG_MDIO_DATA:
166                  reg_rd_data <= {16'h0, mdio_read_data};
167               REG_MDIO_ADDR:
168                  reg_rd_data <= {16'h0, mdio_address};
169               REG_MDIO_OP:
170                  reg_rd_data <= {16'h0, mdio_operation};
171               REG_MDIO_CTRL_STATUS:
172                  reg_rd_data <= {31'b0, mdio_running};
173               default:
174                  reg_rd_resp <= 1'b0;
175            endcase
176         end else if (reg_rd_resp) begin
177            reg_rd_resp <= 1'b0;
178         end
179
180         // Writable registers
181         if (reg_wr_req) begin
182            case(reg_wr_addr)
183               REG_MDIO_DATA:
184                  mdio_write_data <= reg_wr_data[15:0];
185               REG_MDIO_ADDR:
186                  mdio_address    <= reg_wr_data[15:0];
187               REG_MDIO_OP:
188                  mdio_operation  <= reg_wr_data[12:0];
189               REG_MDIO_CTRL_STATUS:
190                  if (reg_wr_data[0])
191                     mdio_running <= 1'b1;   // Trigger mdio operation here. Cleared by state machine at end of bus transaction.
192            endcase
193         end
194      end
195   end
196
197   //
198   // Produce mdc clock as a signal synchronously from Wishbone clock.
199   //
200   always @(posedge clk) begin
201      if (rst) begin
202         mdc_clk_count     <= 8'd1;
203         mdc               <= 1'b0;
204         mdc_falling_edge  <= 1'b0;
205      end else if (mdc_clk_count == (MDC_DIVIDER/2)) begin
206         mdc_clk_count     <= 8'd1;
207         mdc               <= ~mdc;
208         mdc_falling_edge  <= mdc;
209      end else begin
210         mdc_clk_count     <= mdc_clk_count + 8'd1;
211         mdc_falling_edge  <= 1'b0;
212      end
213   end
214
215   //
216   // MDIO state machine
217   //
218   always @(posedge clk) begin
219      if (rst) begin
220         mdio_tri <= 1'b1;
221         mdio_out <= 1'b0;
222         mdio_done <= 1'b0;
223         mdio_read_data <= 16'b0;
224         state <= IDLE;
225      end else if (mdc_falling_edge) begin
226         // This is the MDIO bus controller. Use falling edge of MDC.
227         mdio_tri <= 1'b1;
228         mdio_out <= 1'b0;
229         mdio_done <= 1'b0;
230         case(state)
231            // IDLE.
232            // In Clause 22 & 45 the master of the MDIO bus is tristate during idle.
233            IDLE: begin
234               mdio_tri <= 1'b1;
235               mdio_out <= 1'b0;
236               if (mdio_running)
237                  state <= PREAMBLE1;
238            end
239            // Preamble. All MDIO transactions begin witrh 32bits of 1 bits as a preamble.
240            PREAMBLE1: begin
241               mdio_tri <= 1'b0;
242               mdio_out <= 1'b1;
243               state <= PREAMBLE2;
244            end
245            PREAMBLE2: begin
246               mdio_tri <= 1'b0;
247               mdio_out <= 1'b1;
248               state <= PREAMBLE3;
249            end
250            PREAMBLE3: begin
251               mdio_tri <= 1'b0;
252               mdio_out <= 1'b1;
253               state <= PREAMBLE4;
254            end
255            PREAMBLE4: begin
256               mdio_tri <= 1'b0;
257               mdio_out <= 1'b1;
258               state <= PREAMBLE5;
259            end
260            PREAMBLE5: begin
261               mdio_tri <= 1'b0;
262               mdio_out <= 1'b1;
263               state <= PREAMBLE6;
264            end
265            PREAMBLE6: begin
266               mdio_tri <= 1'b0;
267               mdio_out <= 1'b1;
268               state <= PREAMBLE7;
269            end
270            PREAMBLE7: begin
271               mdio_tri <= 1'b0;
272               mdio_out <= 1'b1;
273               state <= PREAMBLE8;
274            end
275            PREAMBLE8: begin
276               mdio_tri <= 1'b0;
277               mdio_out <= 1'b1;
278               state <= PREAMBLE9;
279            end
280            PREAMBLE9: begin
281               mdio_tri <= 1'b0;
282               mdio_out <= 1'b1;
283               state <= PREAMBLE10;
284            end
285            PREAMBLE10: begin
286               mdio_tri <= 1'b0;
287               mdio_out <= 1'b1;
288               state <= PREAMBLE11;
289            end
290            PREAMBLE11: begin
291               mdio_tri <= 1'b0;
292               mdio_out <= 1'b1;
293               state <= PREAMBLE12;
294            end
295            PREAMBLE12: begin
296               mdio_tri <= 1'b0;
297               mdio_out <= 1'b1;
298               state <= PREAMBLE13;
299            end
300            PREAMBLE13: begin
301               mdio_tri <= 1'b0;
302               mdio_out <= 1'b1;
303               state <= PREAMBLE14;
304            end
305            PREAMBLE14: begin
306               mdio_tri <= 1'b0;
307               mdio_out <= 1'b1;
308               state <= PREAMBLE15;
309            end
310            PREAMBLE15: begin
311               mdio_tri <= 1'b0;
312               mdio_out <= 1'b1;
313               state <= PREAMBLE16;
314            end
315            PREAMBLE16: begin
316               mdio_tri <= 1'b0;
317               mdio_out <= 1'b1;
318               state <= PREAMBLE17;
319            end
320            PREAMBLE17: begin
321               mdio_tri <= 1'b0;
322               mdio_out <= 1'b1;
323               state <= PREAMBLE18;
324            end
325            PREAMBLE18: begin
326               mdio_tri <= 1'b0;
327               mdio_out <= 1'b1;
328               state <= PREAMBLE19;
329            end
330            PREAMBLE19: begin
331               mdio_tri <= 1'b0;
332               mdio_out <= 1'b1;
333               state <= PREAMBLE20;
334            end
335            PREAMBLE20: begin
336               mdio_tri <= 1'b0;
337               mdio_out <= 1'b1;
338               state <= PREAMBLE21;
339            end
340            PREAMBLE21: begin
341               mdio_tri <= 1'b0;
342               mdio_out <= 1'b1;
343               state <= PREAMBLE22;
344            end
345            PREAMBLE22: begin
346               mdio_tri <= 1'b0;
347               mdio_out <= 1'b1;
348               state <= PREAMBLE23;
349            end
350            PREAMBLE23: begin
351               mdio_tri <= 1'b0;
352               mdio_out <= 1'b1;
353               state <= PREAMBLE24;
354            end
355            PREAMBLE24: begin
356               mdio_tri <= 1'b0;
357               mdio_out <= 1'b1;
358               state <= PREAMBLE25;
359            end
360            PREAMBLE25: begin
361               mdio_tri <= 1'b0;
362               mdio_out <= 1'b1;
363               state <= PREAMBLE26;
364            end
365            PREAMBLE26: begin
366               mdio_tri <= 1'b0;
367               mdio_out <= 1'b1;
368               state <= PREAMBLE27;
369            end
370            PREAMBLE27: begin
371               mdio_tri <= 1'b0;
372               mdio_out <= 1'b1;
373               state <= PREAMBLE28;
374            end
375            PREAMBLE28: begin
376               mdio_tri <= 1'b0;
377               mdio_out <= 1'b1;
378               state <= PREAMBLE29;
379            end
380            PREAMBLE29: begin
381               mdio_tri <= 1'b0;
382               mdio_out <= 1'b1;
383               state <= PREAMBLE30;
384            end
385            PREAMBLE30: begin
386               mdio_tri <= 1'b0;
387               mdio_out <= 1'b1;
388               state <= PREAMBLE31;
389            end
390            PREAMBLE31: begin
391               mdio_tri <= 1'b0;
392               mdio_out <= 1'b1;
393               state <= PREAMBLE32;
394            end
395            PREAMBLE32: begin
396               mdio_tri <= 1'b0;
397               mdio_out <= 1'b1;
398               state <= START1;
399            end
400            // Start code for Clause 22 is 01 and Clause 45 is 00
401            START1: begin
402               mdio_tri <= 1'b0;
403               mdio_out <= 1'b0;
404               if (mdio_operation[12])
405                  // Clause 45 bit set.
406                  state <= C45_START2;
407               else
408                  state <= C22_START2;
409            end
410            // 2nd Clause 22 start bit is a 1
411            C22_START2: begin
412               mdio_tri <= 1'b0;
413               mdio_out <= 1'b1;
414               state <= OP1;
415            end
416            // 2nd Clause 45 start bit is a 0
417            C45_START2: begin
418               mdio_tri <= 1'b0;
419               mdio_out <= 1'b0;
420               state <= OP1;
421            end
422            // Both Clause 22 & 45 use 2 bits for operation and are compatable.
423            // Note we don't screen here for illegal Clause 22 ops.
424            OP1: begin
425               mdio_tri <= 1'b0;
426               mdio_out <= mdio_operation[11];
427               state <= OP2;
428            end
429            OP2: begin
430               mdio_tri <= 1'b0;
431               mdio_out <= mdio_operation[10];
432               state <= PRTAD1;
433            end
434            // Both Clause 22 & 45 use 2 sucsessive 5 bit fields to form a hierarchical address
435            // though it's used slightly different between the 2 standards.
436            PRTAD1: begin
437               mdio_tri <= 1'b0;
438               mdio_out <= mdio_operation[9];
439               state <= PRTAD2;
440            end
441            PRTAD2: begin
442               mdio_tri <= 1'b0;
443               mdio_out <= mdio_operation[8];
444               state <= PRTAD3;
445            end
446            PRTAD3: begin
447               mdio_tri <= 1'b0;
448               mdio_out <= mdio_operation[7];
449               state <= PRTAD4;
450            end
451            PRTAD4: begin
452               mdio_tri <= 1'b0;
453               mdio_out <= mdio_operation[6];
454               state <= PRTAD5;
455            end
456            PRTAD5: begin
457               mdio_tri <= 1'b0;
458               mdio_out <= mdio_operation[5];
459               state <= DEVAD1;
460            end
461            DEVAD1: begin
462               mdio_tri <= 1'b0;
463               mdio_out <= mdio_operation[4];
464               state <= DEVAD2;
465            end
466            DEVAD2: begin
467               mdio_tri <= 1'b0;
468               mdio_out <= mdio_operation[3];
469               state <= DEVAD3;
470            end
471            DEVAD3: begin
472               mdio_tri <= 1'b0;
473               mdio_out <= mdio_operation[2];
474               state <= DEVAD4;
475            end
476            DEVAD4: begin
477               mdio_tri <= 1'b0;
478               mdio_out <= mdio_operation[1];
479               state <= DEVAD5;
480            end
481            DEVAD5: begin
482               mdio_tri <= 1'b0;
483               mdio_out <= mdio_operation[0];
484               state <= TA1;
485            end
486            // Both Clause 22 & Clause 45 use the same turn around on the bus.
487            // Reads have Z as the first bit and 0 driven by the slave for the 2nd bit.
488            // Note that slaves drive the bus on the rising edge of MDC.
489            // Writes and Address cycles have 10 driven by the master.
490            TA1: begin
491               if (mdio_operation[11] == 1'b0) // Write/Address
492               begin
493                  mdio_tri <= 1'b0;
494                  mdio_out <= 1'b1;
495                  state <= TA2;
496               end
497               else // Read
498               begin
499                  mdio_tri <= 1'b1;
500                  state <= TA3;
501               end
502            end
503            TA2: begin
504               mdio_tri <= 1'b0;
505               mdio_out <= 1'b0;
506               if ( !mdio_operation[12]) // Clause 22 Write
507                  state <= WRITE1;
508               else if (mdio_operation[10]) // Clause 45 Write
509                  state <= WRITE1;
510               else // Clause 45 ADDRESS
511                  state <= C45_ADDR1;
512            end
513            TA3: begin
514               mdio_tri <= 1'b1;
515               state <= READ1;
516            end
517            // Clause 22 Reads and both forms of clause 45 Reads have the same bus transaction from here out.
518            READ1: begin
519               mdio_tri <= 1'b1;
520               mdio_read_data[15] <= mdio_in;
521               state <= READ2;
522            end
523            READ2: begin
524               mdio_tri <= 1'b1;
525               mdio_read_data[14] <= mdio_in;
526               state <= READ3;
527            end
528            READ3: begin
529               mdio_tri <= 1'b1;
530               mdio_read_data[13] <= mdio_in;
531               state <= READ4;
532            end
533            READ4: begin
534               mdio_tri <= 1'b1;
535               mdio_read_data[12] <= mdio_in;
536               state <= READ5;
537            end
538            READ5: begin
539               mdio_tri <= 1'b1;
540               mdio_read_data[11] <= mdio_in;
541               state <= READ6;
542            end
543            READ6: begin
544               mdio_tri <= 1'b1;
545               mdio_read_data[10] <= mdio_in;
546               state <= READ7;
547            end
548            READ7: begin
549               mdio_tri <= 1'b1;
550               mdio_read_data[9] <= mdio_in;
551               state <= READ8;
552            end
553            READ8: begin
554               mdio_tri <= 1'b1;
555               mdio_read_data[8] <= mdio_in;
556               state <= READ9;
557            end
558            READ9: begin
559               mdio_tri <= 1'b1;
560               mdio_read_data[7] <= mdio_in;
561               state <= READ10;
562            end
563            READ10: begin
564               mdio_tri <= 1'b1;
565               mdio_read_data[6] <= mdio_in;
566               state <= READ11;
567            end
568            READ11: begin
569               mdio_tri <= 1'b1;
570               mdio_read_data[5] <= mdio_in;
571               state <= READ12;
572            end
573            READ12: begin
574               mdio_tri <= 1'b1;
575               mdio_read_data[4] <= mdio_in;
576               state <= READ13;
577            end
578            READ13: begin
579               mdio_tri <= 1'b1;
580               mdio_read_data[3] <= mdio_in;
581               state <= READ14;
582            end
583            READ14: begin
584               mdio_tri <= 1'b1;
585               mdio_read_data[2] <= mdio_in;
586               state <= READ15;
587            end
588            READ15: begin
589               mdio_tri <= 1'b1;
590               mdio_read_data[1] <= mdio_in;
591               state <= READ16;
592            end
593            READ16: begin
594               mdio_tri <= 1'b1;
595               mdio_read_data[0] <= mdio_in;
596               state <= PREIDLE;
597               mdio_done <= 1'b1;
598            end
599            // Write 16bits of data for all types of Write.
600            WRITE1: begin
601               mdio_tri <= 1'b0;
602               mdio_out <= mdio_write_data[15];
603               state <= WRITE2;
604            end
605            WRITE2: begin
606               mdio_tri <= 1'b0;
607               mdio_out <= mdio_write_data[14];
608               state <= WRITE3;
609            end
610            WRITE3: begin
611               mdio_tri <= 1'b0;
612               mdio_out <= mdio_write_data[13];
613               state <= WRITE4;
614            end
615            WRITE4: begin
616               mdio_tri <= 1'b0;
617               mdio_out <= mdio_write_data[12];
618               state <= WRITE5;
619            end
620            WRITE5: begin
621               mdio_tri <= 1'b0;
622               mdio_out <= mdio_write_data[11];
623               state <= WRITE6;
624            end
625            WRITE6: begin
626               mdio_tri <= 1'b0;
627               mdio_out <= mdio_write_data[10];
628               state <= WRITE7;
629            end
630            WRITE7: begin
631               mdio_tri <= 1'b0;
632               mdio_out <= mdio_write_data[9];
633               state <= WRITE8;
634            end
635            WRITE8: begin
636               mdio_tri <= 1'b0;
637               mdio_out <= mdio_write_data[8];
638               state <= WRITE9;
639            end
640            WRITE9: begin
641               mdio_tri <= 1'b0;
642               mdio_out <= mdio_write_data[7];
643               state <= WRITE10;
644            end
645            WRITE10: begin
646               mdio_tri <= 1'b0;
647               mdio_out <= mdio_write_data[6];
648               state <= WRITE11;
649            end
650            WRITE11: begin
651               mdio_tri <= 1'b0;
652               mdio_out <= mdio_write_data[5];
653               state <= WRITE12;
654            end
655            WRITE12: begin
656               mdio_tri <= 1'b0;
657               mdio_out <= mdio_write_data[4];
658               state <= WRITE13;
659            end
660            WRITE13: begin
661               mdio_tri <= 1'b0;
662               mdio_out <= mdio_write_data[3];
663               state <= WRITE14;
664            end
665            WRITE14: begin
666               mdio_tri <= 1'b0;
667               mdio_out <= mdio_write_data[2];
668               state <= WRITE15;
669            end
670            WRITE15: begin
671               mdio_tri <= 1'b0;
672               mdio_out <= mdio_write_data[1];
673               state <= WRITE16;
674            end
675            WRITE16: begin
676               mdio_tri <= 1'b0;
677               mdio_out <= mdio_write_data[0];
678               state <= PREIDLE;
679               mdio_done <= 1'b1;
680            end
681            // Write 16bits of address for a Clause 45 Address transaction
682            C45_ADDR1: begin
683               mdio_tri <= 1'b0;
684               mdio_out <= mdio_address[15];
685               state <= C45_ADDR2;
686            end
687            C45_ADDR2: begin
688               mdio_tri <= 1'b0;
689               mdio_out <= mdio_address[14];
690               state <= C45_ADDR3;
691            end
692            C45_ADDR3: begin
693               mdio_tri <= 1'b0;
694               mdio_out <= mdio_address[13];
695               state <= C45_ADDR4;
696            end
697            C45_ADDR4: begin
698               mdio_tri <= 1'b0;
699               mdio_out <= mdio_address[12];
700               state <= C45_ADDR5;
701            end
702            C45_ADDR5: begin
703               mdio_tri <= 1'b0;
704               mdio_out <= mdio_address[11];
705               state <= C45_ADDR6;
706            end
707            C45_ADDR6: begin
708               mdio_tri <= 1'b0;
709               mdio_out <= mdio_address[10];
710               state <= C45_ADDR7;
711            end
712            C45_ADDR7: begin
713               mdio_tri <= 1'b0;
714               mdio_out <= mdio_address[9];
715               state <= C45_ADDR8;
716            end
717            C45_ADDR8: begin
718               mdio_tri <= 1'b0;
719               mdio_out <= mdio_address[8];
720               state <= C45_ADDR9;
721            end
722            C45_ADDR9: begin
723               mdio_tri <= 1'b0;
724               mdio_out <= mdio_address[7];
725               state <= C45_ADDR10;
726            end
727            C45_ADDR10: begin
728               mdio_tri <= 1'b0;
729               mdio_out <= mdio_address[6];
730               state <= C45_ADDR11;
731            end
732            C45_ADDR11: begin
733               mdio_tri <= 1'b0;
734               mdio_out <= mdio_address[5];
735               state <= C45_ADDR12;
736            end
737            C45_ADDR12: begin
738               mdio_tri <= 1'b0;
739               mdio_out <= mdio_address[4];
740               state <= C45_ADDR13;
741            end
742            C45_ADDR13: begin
743               mdio_tri <= 1'b0;
744               mdio_out <= mdio_address[3];
745               state <= C45_ADDR14;
746            end
747            C45_ADDR14: begin
748               mdio_tri <= 1'b0;
749               mdio_out <= mdio_address[2];
750               state <= C45_ADDR15;
751            end
752            C45_ADDR15: begin
753               mdio_tri <= 1'b0;
754               mdio_out <= mdio_address[1];
755               state <= C45_ADDR16;
756            end
757            C45_ADDR16: begin
758               mdio_tri <= 1'b0;
759               mdio_out <= mdio_address[0];
760               state <= PREIDLE;
761               mdio_done <= 1'b1;
762            end
763            // PREIDLE allows the mdio_running bit to reset.
764            PREIDLE: begin
765               state <= IDLE;
766            end
767         endcase // case(state)
768      end // if (mdc_falling_edge)
769   end
770
771endmodule
772
773