blob: 03a32378c40d90362c9cf15b1ca5e4a9cd872ddf [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
Travis Geiselbrechteb946052008-09-07 22:32:49 -070023#include <debug.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070024#include <kernel/thread.h>
25#include <kernel/mutex.h>
26#include <platform/at91sam7.h>
27#include <platform/debug.h>
28
29#include <dev/ethernet.h>
30
31#include <malloc.h>
32#include <string.h>
33
34#include <hw/mii.h>
35
36void emac_init_send(void);
37
38#define PHYA 31
39
40static unsigned mi_rd(AT91EMAC *emac, unsigned addr)
41{
42 addr &= 0x1f;
43
44 thread_sleep(20);
45
46 emac->MAN =
47 (1 << 30) | /* sof: 01 */
48 (2 << 28) | /* rw: 10 = read */
49 (PHYA << 23) | /* phya: PHYA */
50 (addr << 18) | /* rega: addr */
51 (2 << 16); /* code: 10 */
52
53 while(!(emac->NSR & NSR_IDLE)) ;
54
55 thread_sleep(20);
56 return emac->MAN & 0xffff;
57}
58
59static void mi_wr(AT91EMAC *emac, unsigned addr, unsigned val)
60{
61 addr &= 0x1f;
62 val &= 0xffff;
63
64 emac->MAN =
65 (1 << 30) | /* sof: 01 */
66 (1 << 28) | /* rw: 01 = read */
67 (PHYA << 23) | /* phya: PHYA */
68 (addr << 18) | /* rega: addr */
69 (2 << 16) | /* code: 10 */
70 val; /* data: val */
71
72 while(!(emac->NSR & NSR_IDLE)) ;
73}
74
75#define PIN_EMAC_ALL 0x3ffff
76#define PIN_PHY_PD (1 << 18)
77#define PIN_PHY_IRQ (1 << 26)
78
79#define PIN_PHYAD0 (1 << 26)
80#define PIN_PHYAD1 (1 << 14)
81#define PIN_PHYAD2 (1 << 13)
82#define PIN_PHYAD3 (1 << 6)
83#define PIN_PHYAD4 (1 << 5)
84#define PIN_LPBK (1 << 15)
85#define PIN_ISOLATE (1 << 7)
86#define PIN_RMII_MODE (1 << 16)
87#define PIN_RMII_BTB (1 << 4)
88
89/* select RMII w/ BTB, 100mbps duplex autonegotiate
90** disable ISOLATE and LPBK
91** phya=00001b
92*/
93#define PIN_RESET_LOW (PIN_LPBK)
94
95int emac_init(void)
96{
97 AT91EMAC *emac = AT91EMAC_ADDR;
98 AT91PIO *piob = AT91PIOB_ADDR;
99 AT91PMC *pmc = AT91PMC_ADDR;
100 AT91RSTC *rstc = AT91RSTC_ADDR;
101
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700102 dprintf(INFO, "emac_init()\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700103
104 /* enable clock to EMAC */
105 pmc->PCER = (1 << PID_EMAC);
106
107 thread_sleep(10);
108
109 /* for reset, all lines are gpio inputs and pullups are
110 enabled or disabled per strapping mode defined above */
111 piob->pio_enable = PIN_EMAC_ALL | PIN_PHY_PD | PIN_PHY_IRQ;
112 piob->select_a = PIN_EMAC_ALL;
113 piob->pullup_enable = PIN_EMAC_ALL | PIN_PHY_IRQ;
114 piob->pullup_disable = PIN_LPBK | PIN_ISOLATE | PIN_RMII_MODE;
115 piob->output_disable = PIN_EMAC_ALL;
116
117 /* PHY PD becomes output and high (no powerdown mode */
118 piob->data_set = PIN_PHY_PD;
119 piob->output_enable = PIN_PHY_PD;
120
121 thread_sleep(30);
122
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700123 dprintf(INFO, "emac_init() - reset phy\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700124
125 /* assert the RST line and wait until the it deasserts */
126 rstc->CR = RSTC_KEY | RSTC_EXTRST;
127 while(rstc->SR & RSTC_NRSTL) ;
128
129 thread_sleep(30);
130
131 /* after reset all the gpios are assigned to the EMAC,
132 except for PHY_PD (which remains output and high) */
133 piob->pio_disable = PIN_EMAC_ALL;
134
135 emac->USRIO = USRIO_CLKEN;
136
137 thread_sleep(1000);
138
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700139 dprintf(INFO, "emac_init() - read state\n");
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700140
141 emac->NCR = NCR_MPE;
142 emac->NCFG = NCFG_CLK_d64;
143
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700144 dprintf(INFO, "bcr = %x\n", mi_rd(emac, MII_REG_BCR));
145 dprintf(INFO, "id1 = %x\n", mi_rd(emac, MII_REG_PHY_ID1));
146 dprintf(INFO, "id2 = %x\n", mi_rd(emac, MII_REG_PHY_ID2));
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700147
148#if 0
149 unsigned state, last;
150 last = 0xff;
151
152 for(;;) {
153 state = mi_rd(emac, MII_REG_100TX_PHY) & MII_100TX_MODE_MASK;
154 if(last != state) {
155 last = state;
156 char *name;
157 switch(state) {
158 case MII_100TX_MODE_AUTO:
159 name = "auto negotiate";
160 break;
161 case MII_100TX_MODE_10T_H:
162 name = "10-T half duplex";
163 break;
164 case MII_100TX_MODE_10T_F:
165 name = "10-T full duplex";
166 break;
167 case MII_100TX_MODE_100TX_H:
168 name = "100-TX half duplex";
169 break;
170 case MII_100TX_MODE_100TX_F:
171 name = "100-TX full duplex";
172 break;
173 case MII_100TX_MODE_ISOLATE:
174 name = "isolate";
175 break;
176 default:
177 name = "unknown";
178 }
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700179 dprintf(INFO, "link state: %s\n", name);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700180 }
181 thread_sleep(100);
182 }
183#endif
184
185 emac_init_send();
186
187 return 0;
188}
189
190#define XMIT_ENTRY_COUNT 32
191static emac_xmit_entry xmit_list[XMIT_ENTRY_COUNT];
192static unsigned xmit_next = 0;
193static mutex_t xmit_lock;
194
195void emac_init_send(void)
196{
197 AT91EMAC *emac = AT91EMAC_ADDR;
198 int i;
199
200 for(i = 0; i < XMIT_ENTRY_COUNT; i++) {
201 xmit_list[i].info = XMIT_USED;
202 xmit_list[i].addr = 0;
203 }
204 xmit_list[i-1].info |= XMIT_LAST;
205
206 emac->NCFG = NCFG_CLK_d64 | NCFG_SPD | NCFG_FD;
207 emac->NCR = NCR_TE | NCR_MPE;
208 emac->TBQP = (unsigned) xmit_list;
209
210 mutex_init(&xmit_lock);
211}
212
213int ethernet_send(void *data, unsigned len)
214{
215 AT91EMAC *emac = AT91EMAC_ADDR;
216
217 emac_xmit_entry *xe;
218 int waited = 0;
219
220 mutex_acquire(&xmit_lock);
221
222 xe = xmit_list + xmit_next;
223
224 while(!(xe->info & XMIT_USED)) {
225 thread_yield();
226 waited++;
227 }
228
Travis Geiselbrechteb946052008-09-07 22:32:49 -0700229 if(waited) dprintf(INFO, "W%d\n",waited);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700230
231 if(xe->addr != 0) {
232 free((void*) xe->addr);
233 }
234
235 xe->addr = (unsigned) data;
236 if(xmit_next == (XMIT_ENTRY_COUNT - 1)) {
237 xe->info = XMIT_LENGTH(len) | XMIT_LAST | XMIT_WRAP;
238 xmit_next = 0;
239 } else {
240 xe->info = XMIT_LENGTH(len) | XMIT_LAST;
241 xmit_next++;
242 }
243
244 emac->NCR |= NCR_TSTART;
245
246 mutex_release(&xmit_lock);
247
248 return 0;
249}
250