OpenCores
URL https://opencores.org/ocsvn/ha1588/ha1588/trunk

Subversion Repositories ha1588

[/] [ha1588/] [trunk/] [rtl/] [tsu/] [ptp_parser.v] - Blame information for rev 57

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 34 edn_walter
/*
2 38 edn_walter
 * ptp_parser.v
3 34 edn_walter
 *
4 37 edn_walter
 * Copyright (c) 2012, BABY&HW. All rights reserved.
5 34 edn_walter
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301  USA
20
 */
21
 
22 4 ash_riple
`timescale 1ns/1ns
23
 
24
module ptp_parser (
25
  input        clk, rst,
26 10 edn_walter
  input [31:0] int_data,
27
  input        int_valid,
28
  input        int_sop,
29
  input        int_eop,
30
  input [ 1:0] int_mod,
31 4 ash_riple
 
32 43 edn_walter
  input [ 7:0] ptp_msgid_mask,
33
 
34 4 ash_riple
  output reg        ptp_found,
35 37 edn_walter
  output reg [31:0] ptp_infor
36 4 ash_riple
);
37
 
38 57 edn_walter
// constant values
39 56 edn_walter
parameter c_vlan_tpid_1 = 16'h8100;
40
parameter c_vlan_tpid_2 = 16'h88a8;
41
parameter c_vlan_tpid_3 = 16'h9100;
42
 
43 57 edn_walter
parameter c_mpls_type_1 = 16'h8847;
44
parameter c_mpls_type_2 = 16'h8848;
45
 
46
parameter c_ipv4_type   = 16'h0800;
47
parameter c_ipv6_type   = 16'h86dd;
48
 
49
parameter c_ptp2_type   = 16'h88f7;
50
parameter c_ptp4_port_1 = 16'h013f;
51
parameter c_ptp4_port_2 = 16'h0140;
52
 
53
// buffer data input
54 10 edn_walter
reg [31:0] int_data_d1;
55 4 ash_riple
always @(posedge rst or posedge clk) begin
56
  if (rst) begin
57 10 edn_walter
    int_data_d1  <= 32'h00000000;
58 4 ash_riple
  end
59
  else begin
60 10 edn_walter
    if (int_valid) begin
61
      int_data_d1  <= int_data;
62 4 ash_riple
    end
63
  end
64
end
65
 
66 29 edn_walter
// packet parser: counter
67 10 edn_walter
reg [ 9:0] int_cnt, bypass_ipv4_cnt, bypass_ipv6_cnt, bypass_udp_cnt, ptp_cnt;
68 35 edn_walter
reg bypass_vlan, ptp_l2, bypass_mpls, bypass_ipv4, bypass_ipv6, found_udp, bypass_udp, ptp_l4, ptp_event;
69 4 ash_riple
always @(posedge rst or posedge clk) begin
70 10 edn_walter
  if (rst) begin
71
    int_cnt <= 10'd0;
72
    bypass_ipv4_cnt <= 10'd0;
73 11 edn_walter
    bypass_ipv6_cnt <= 10'd0;
74 10 edn_walter
    bypass_udp_cnt <= 10'd0;
75
  end
76
  else begin
77 29 edn_walter
    if (int_valid && int_sop)
78 10 edn_walter
      int_cnt <= 10'd0;
79 29 edn_walter
    else if (int_valid)
80 35 edn_walter
      int_cnt <= int_cnt + 10'd1 - bypass_vlan - bypass_mpls - (bypass_ipv4 || bypass_ipv6 || bypass_udp);
81 10 edn_walter
 
82 29 edn_walter
    if (int_valid && int_sop)
83 10 edn_walter
      bypass_ipv4_cnt <= 10'd0;
84 29 edn_walter
    else if (int_valid && bypass_ipv4)
85 10 edn_walter
      bypass_ipv4_cnt <= bypass_ipv4_cnt + 10'd1;
86
 
87 29 edn_walter
    if (int_valid && int_sop)
88 11 edn_walter
      bypass_ipv6_cnt <= 10'd0;
89 29 edn_walter
    else if (int_valid && bypass_ipv6)
90 11 edn_walter
      bypass_ipv6_cnt <= bypass_ipv6_cnt + 10'd1;
91
 
92 29 edn_walter
    if (int_valid && int_sop)
93 10 edn_walter
      bypass_udp_cnt <= 10'd0;
94 29 edn_walter
    else if (int_valid && bypass_udp)
95 10 edn_walter
      bypass_udp_cnt <= bypass_udp_cnt + 10'd1;
96 29 edn_walter
 
97
    if (int_valid && int_sop)
98
      ptp_cnt <= 10'd0;
99
    else if (int_valid && (ptp_l2 || (bypass_udp_cnt>=10'd2 && ptp_l4)))
100
      ptp_cnt <= ptp_cnt + 10'd1;
101 10 edn_walter
  end
102 4 ash_riple
end
103
 
104 29 edn_walter
// packet parser: comparator
105 4 ash_riple
always @(posedge rst or posedge clk) begin
106
  if (rst) begin
107 10 edn_walter
    bypass_vlan  <= 1'b0;
108 35 edn_walter
    bypass_mpls  <= 1'b0;
109 10 edn_walter
    bypass_ipv4  <= 1'b0;
110 11 edn_walter
    bypass_ipv6  <= 1'b0;
111 10 edn_walter
    found_udp    <= 1'b0;
112
    bypass_udp   <= 1'b0;
113
    ptp_l2    <= 1'b0;
114
    ptp_l4    <= 1'b0;
115 4 ash_riple
    ptp_event <= 1'b0;
116 10 edn_walter
  end
117 29 edn_walter
  else if (int_valid && int_sop) begin
118 10 edn_walter
    bypass_vlan  <= 1'b0;
119 35 edn_walter
    bypass_mpls  <= 1'b0;
120 10 edn_walter
    bypass_ipv4  <= 1'b0;
121 11 edn_walter
    bypass_ipv6  <= 1'b0;
122 10 edn_walter
    found_udp    <= 1'b0;
123
    bypass_udp   <= 1'b0;
124
    ptp_l2    <= 1'b0;
125
    ptp_l4    <= 1'b0;
126
    ptp_event <= 1'b0;
127
  end
128
  else begin
129
    // bypass vlan
130 56 edn_walter
    if      (int_valid && int_cnt==10'd4 && (int_data[31:16]==c_vlan_tpid_1 || int_data[31:16]==c_vlan_tpid_2 || int_data[31:16]==c_vlan_tpid_3))  // ether_type == vlan
131 10 edn_walter
      bypass_vlan <= 1'b1;
132 56 edn_walter
    else if (int_valid && int_cnt==10'd5 && (int_data[31:16]==c_vlan_tpid_1 || int_data[31:16]==c_vlan_tpid_2 || int_data[31:16]==c_vlan_tpid_3) && bypass_vlan)  // vlan_type == vlan
133 12 edn_walter
      bypass_vlan <= 1'b1;
134 29 edn_walter
    else if (int_valid && bypass_vlan)
135 10 edn_walter
      bypass_vlan <= 1'b0;
136
 
137 35 edn_walter
    // bypass mpls
138 52 edn_walter
    if      (int_valid && (int_cnt==10'd4 || bypass_vlan && int_cnt==10'd5) &&
139 57 edn_walter
            (int_data[31:16]==c_mpls_type_1 || int_data[31:16]==c_mpls_type_2))  // ether_type == mpls
140 35 edn_walter
      bypass_mpls <= 1'b1;
141 52 edn_walter
    else if (int_valid &&  int_cnt==10'd5 && bypass_mpls &&
142 35 edn_walter
             int_data[24]==1'b0)  // bottom of label stack == 0
143
      bypass_mpls <= 1'b1;
144
    else if (int_valid && bypass_mpls)
145
      bypass_mpls <= 1'b0;
146
 
147 10 edn_walter
    // bypass ipv4
148 52 edn_walter
    if      (int_valid && (int_cnt==10'd4 || (bypass_vlan || bypass_mpls) && int_cnt==10'd5) && bypass_ipv4_cnt==10'd0 &&
149 57 edn_walter
            (int_data[31:16]==c_ipv4_type || bypass_mpls) && int_data[15:12]==4'h4)  // ether_type == ipv4, ip_version == 4
150 10 edn_walter
      bypass_ipv4 <= 1'b1;
151 29 edn_walter
    else if (int_valid && bypass_ipv4_cnt==10'd4)
152 10 edn_walter
      bypass_ipv4 <= 1'b0;
153
 
154
    // bypass ipv6
155 52 edn_walter
    if      (int_valid && (int_cnt==10'd4 || (bypass_vlan || bypass_mpls) && int_cnt==10'd5) && bypass_ipv6_cnt==10'd0 &&
156 57 edn_walter
            (int_data[31:16]==c_ipv6_type || bypass_mpls) && int_data[15:12]==4'h6)  // ether_type == ipv6, ip_version == 6
157 11 edn_walter
      bypass_ipv6 <= 1'b1;
158 29 edn_walter
    else if (int_valid && bypass_ipv6_cnt==10'd9)
159 11 edn_walter
      bypass_ipv6 <= 1'b0;
160 10 edn_walter
 
161 52 edn_walter
    // check if it is udp
162 29 edn_walter
    if      (int_valid && bypass_ipv4_cnt==10'd1 && int_data[ 7: 0]== 8'h11)  // ipv4_protocol == udp
163 10 edn_walter
      found_udp <= 1'b1;
164 29 edn_walter
    else if (int_valid && bypass_ipv6_cnt==10'd1 && int_data[31:24]== 8'h11)  // ipv6_protocol == udp
165 11 edn_walter
      found_udp <= 1'b1;
166 10 edn_walter
 
167
    // bypass udp
168 29 edn_walter
    if      (int_valid && bypass_ipv4_cnt==10'd4 && bypass_udp_cnt==10'd0 && found_udp)  // ipv4_udp
169 10 edn_walter
      bypass_udp <= 1'b1;
170 29 edn_walter
    else if (int_valid && bypass_ipv6_cnt==10'd9 && bypass_udp_cnt==10'd0 && found_udp)  // ipv6_udp
171 11 edn_walter
      bypass_udp <= 1'b1;
172 29 edn_walter
    else if (int_valid && bypass_udp_cnt==10'd2)
173 11 edn_walter
      bypass_udp <= 1'b0;
174 10 edn_walter
 
175
    // check if it is L2 PTP
176 57 edn_walter
    if (int_valid && (int_cnt==10'd4 || bypass_vlan && int_cnt==10'd5) && int_data[31:16]==c_ptp2_type)  // ether_type == ptp
177 10 edn_walter
      ptp_l2 <= 1'b1;
178
    // check if it is L4 PTP
179 29 edn_walter
    if (int_valid && bypass_udp_cnt==10'd0 && bypass_udp &&
180 57 edn_walter
       (int_data[31:16]==c_ptp4_port_1 || int_data[31:16]==c_ptp4_port_2))  // udp_dest_port == ptp_event || ptp_general
181 10 edn_walter
      ptp_l4 <= 1'b1;
182
 
183
    // check if it is PTP Event message
184 57 edn_walter
    if      (int_valid && (int_cnt==10'd4 || bypass_vlan && int_cnt==10'd5) && int_data[31:16]==c_ptp2_type &&
185 43 edn_walter
            (ptp_msgid_mask[int_data[11: 8]]))  // ptp_message_id == ptp_event
186 10 edn_walter
      ptp_event <= 1'b1;
187 52 edn_walter
    else if (int_valid && int_cnt==10'd5 && bypass_udp_cnt==10'd1 && ptp_l4 &&
188 43 edn_walter
            (ptp_msgid_mask[int_data[11: 8]]))  // ptp_message_id == ptp_event 
189 10 edn_walter
      ptp_event <= 1'b1;
190
  end
191
end
192
 
193 29 edn_walter
// ptp message
194
reg [31:0] ptp_data;
195 37 edn_walter
reg [ 3:0] ptp_msgid;
196
reg [15:0] ptp_seqid;
197
reg [11:0] ptp_cksum;
198 10 edn_walter
always @(posedge rst or posedge clk) begin
199
  if (rst) begin
200 29 edn_walter
    ptp_data  <= 32'd0;
201 7 edn_walter
    ptp_msgid <= 4'd0;
202 8 edn_walter
    ptp_seqid <= 16'd0;
203 37 edn_walter
    ptp_cksum <= 12'd0;
204 4 ash_riple
  end
205 29 edn_walter
  else if (int_valid && int_sop) begin
206
    ptp_data  <= 32'd0;
207 7 edn_walter
    ptp_msgid <= 4'd0;
208 8 edn_walter
    ptp_seqid <= 16'd0;
209 37 edn_walter
    ptp_cksum <= 12'd0;
210 4 ash_riple
  end
211
  else begin
212 10 edn_walter
    // get PTP identification information as additional information to Timestamp
213 29 edn_walter
    // ptp message body
214
    if (int_valid && (ptp_l2 || (bypass_udp_cnt>=10'd2 && ptp_l4)))
215
      ptp_data <= {int_data_d1[15:0], int_data[31:16]};
216 10 edn_walter
    // message id
217 29 edn_walter
    if (int_valid && ptp_cnt==10'd1)
218 37 edn_walter
      ptp_msgid <= ptp_data[27:24];
219 10 edn_walter
    // sequence id
220 29 edn_walter
    if (int_valid && ptp_cnt==10'd8)
221 37 edn_walter
      ptp_seqid <= ptp_data[15:0];
222
    // sum up clock id and source port id
223
    if (int_valid && ptp_cnt==10'd6)
224
      ptp_cksum <= ptp_data[31:24] + ptp_data[23:16] + ptp_data[15: 8] + ptp_data[ 7: 0] + ptp_cksum;
225
    if (int_valid && ptp_cnt==10'd7)
226
      ptp_cksum <= ptp_data[31:24] + ptp_data[23:16] + ptp_data[15: 8] + ptp_data[ 7: 0] + ptp_cksum;
227
    if (int_valid && ptp_cnt==10'd8)
228
      ptp_cksum <= ptp_data[31:24] + ptp_data[23:16]                                     + ptp_cksum;
229 4 ash_riple
  end
230
end
231
 
232 29 edn_walter
// parser output
233 4 ash_riple
always @(posedge rst or posedge clk) begin
234
  if (rst) begin
235
    ptp_found <=  1'b0;
236 37 edn_walter
    ptp_infor <= 32'd0;
237 4 ash_riple
  end
238 29 edn_walter
  else if (int_valid && int_sop) begin
239 4 ash_riple
    ptp_found <=  1'b0;
240 37 edn_walter
    ptp_infor <= 32'd0;
241 4 ash_riple
  end
242 29 edn_walter
  else if (int_valid && ptp_cnt==10'd9) begin
243 4 ash_riple
    ptp_found <=  ptp_event;
244 37 edn_walter
    ptp_infor <= {ptp_msgid, ptp_cksum, ptp_seqid};  // 4+12+16
245 4 ash_riple
  end
246
end
247
 
248
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.