Merged r9433:9527 from features/gr-usrp2 into trunk. Adds usrp2 and gr-usrp2 top...
[debian/gnuradio] / usrp2 / firmware / lib / eth_mac.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "eth_mac.h"
20 #include "memory_map.h"
21 #include "bool.h"
22 #include "eth_phy.h"    // for simulation constants
23 #include "mdelay.h"
24
25
26 #define PHY_ADDR 1
27
28 void
29 eth_mac_set_addr(const u2_mac_addr_t *src)
30 {
31   int i;
32
33   // tell mac our source address and enable automatic insertion on Tx.
34   eth_mac->mac_tx_add_prom_wr = 0;      // just in case
35   for (i = 0; i < 6; i++){
36     eth_mac->mac_tx_add_prom_add = i;
37     eth_mac->mac_tx_add_prom_data = src->addr[i];
38     eth_mac->mac_tx_add_prom_wr = 1;
39     mdelay(1);
40     eth_mac->mac_tx_add_prom_wr = 0;
41     mdelay(1);
42   }
43   eth_mac->mac_tx_add_en = 1;  // overwrite pkt src addr field with this stuff
44
45   // set up receive destination address filter
46   eth_mac->mac_rx_add_prom_wr = 0;      // just in case
47   for (i = 0; i < 6; i++){
48     eth_mac->mac_rx_add_prom_add = i;
49     eth_mac->mac_rx_add_prom_data = src->addr[i];
50     eth_mac->mac_rx_add_prom_wr = 1;
51     mdelay(1);
52     eth_mac->mac_rx_add_prom_wr = 0;
53     mdelay(1);
54   }
55   // eth_mac->mac_rx_add_chk_en = 1;  // FIXME enable when everything's working
56 }
57
58
59 void 
60 eth_mac_init(const u2_mac_addr_t *src)
61 {
62   eth_mac->miimoder = 25;       // divider from CPU clock (50MHz/25 = 2MHz)
63
64   eth_mac_set_addr(src);
65
66   // set rx flow control high and low water marks
67   // unsigned int lwmark = (2*2048 + 64)/4; // 2 * 2048-byte frames + 1 * 64-byte pause frame
68   // eth_mac->fc_hwmark = lwmark + 2048/4;  // plus a 2048-byte frame
69
70   eth_mac->fc_lwmark = 600;             // there are currently 2047 lines in the fifo
71   eth_mac->fc_hwmark = 1200;
72
73   //eth_mac->tx_pause_en = 0;           // pay attn to pause frames sent to us
74   //eth_mac->pause_quanta_set = 38;     // a bit more than 1 max frame 16kb/512 + fudge
75   //eth_mac->pause_frame_send_en = 0;   // enable sending pause frames
76 }
77
78 int
79 eth_mac_read_rmon(int addr)
80 {
81   int t;
82   
83   eth_mac->rmon_rd_addr = addr;
84   eth_mac->rmon_rd_apply = 1;
85   while(eth_mac->rmon_rd_grant == 0)
86     ;
87
88   t = eth_mac->rmon_rd_dout;
89   eth_mac->rmon_rd_apply = 0;
90   return t;
91 }
92
93 int
94 eth_mac_miim_read(int addr)
95 {
96   if (hwconfig_simulation_p()){
97     switch(addr){
98     case PHY_LINK_AN:
99       return LANSR_MASTER | LANSR_LINK_GOOD | LANSR_SPEED_1000;
100     default:
101       return 0;
102     }
103   }
104
105   int phy_addr = PHY_ADDR;
106   eth_mac->miiaddress = ((addr & 0x1f) << 8) | phy_addr;
107   eth_mac->miicommand = MIIC_RSTAT;
108
109   while((eth_mac->miistatus & MIIS_BUSY) != 0)
110     ;
111
112   return eth_mac->miirx_data;
113 }
114
115 void
116 eth_mac_miim_write(int addr, int value)
117 {
118   int phy_addr = PHY_ADDR;
119   eth_mac->miiaddress = ((addr & 0x1f) << 8) | phy_addr;
120   eth_mac->miitx_data = value;
121   eth_mac->miicommand = MIIC_WCTRLDATA;
122
123   while((eth_mac->miistatus & MIIS_BUSY) != 0)
124     ;
125 }
126
127 int
128 eth_mac_miim_read_status(void)
129 {
130   if (hwconfig_simulation_p())
131     return 0;
132
133   return eth_mac->miistatus;
134 }