source: asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/targets/TARGET_RENESAS/TARGET_RZA1XX/ethernet_api.c@ 374

Last change on this file since 374 was 374, checked in by coas-nagasima, 5 years ago

mbed関連を更新
シリアルドライバをmbedのHALを使うよう変更
ファイルディスクリプタの処理を更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 26.9 KB
Line 
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <string.h>
17#include "ethernet_api.h"
18#include "cmsis.h"
19#include "mbed_interface.h"
20#include "mbed_toolchain.h"
21#include "mbed_error.h"
22#include "iodefine.h"
23#include "ethernetext_api.h"
24
25#if DEVICE_ETHERNET
26
27/* Descriptor info */
28#define SIZE_OF_BUFFER (1600) /* Must be an integral multiple of 32 */
29#define MAX_SEND_SIZE (1514)
30/* Ethernet Descriptor Value Define */
31#define TD0_TFP_TOP_BOTTOM (0x30000000)
32#define TD0_TACT (0x80000000)
33#define TD0_TDLE (0x40000000)
34#define RD0_RACT (0x80000000)
35#define RD0_RDLE (0x40000000)
36#define RD0_RFE (0x08000000)
37#define RD0_RCSE (0x04000000)
38#define RD0_RFS (0x03FF0000)
39#define RD0_RCS (0x0000FFFF)
40#define RD0_RFS_RFOF (0x02000000)
41#define RD0_RFS_RUAF (0x00400000)
42#define RD0_RFS_RRF (0x00100000)
43#define RD0_RFS_RTLF (0x00080000)
44#define RD0_RFS_RTSF (0x00040000)
45#define RD0_RFS_PRE (0x00020000)
46#define RD0_RFS_CERF (0x00010000)
47#define RD0_RFS_ERROR (RD0_RFS_RFOF | RD0_RFS_RUAF | RD0_RFS_RRF | RD0_RFS_RTLF | \
48 RD0_RFS_RTSF | RD0_RFS_PRE | RD0_RFS_CERF)
49#define RD1_RDL_MSK (0x0000FFFF)
50/* PHY Register */
51#define BASIC_MODE_CONTROL_REG (0)
52#define BASIC_MODE_STATUS_REG (1)
53#define PHY_IDENTIFIER1_REG (2)
54#define PHY_IDENTIFIER2_REG (3)
55#define PHY_SP_CTL_STS_REG (31)
56/* MII management interface access */
57#define PHY_ADDR (0) /* Confirm the pin connection of the PHY-LSI */
58#define PHY_ST (1)
59#define PHY_WRITE (1)
60#define PHY_READ (2)
61#define MDC_WAIT (6) /* 400ns/4 */
62#define BASIC_STS_MSK_LINK (0x0004) /* Link Status */
63#define BASIC_STS_MSK_AUTO_CMP (0x0020) /* Auto-Negotiate Complete */
64#define M_PHY_ID (0xFFFFFFF0)
65#define PHY_ID_LAN8710A (0x0007C0F0)
66/* ETHERPIR0 */
67#define PIR0_MDI (0x00000008)
68#define PIR0_MDO (0x00000004)
69#define PIR0_MMD (0x00000002)
70#define PIR0_MDC (0x00000001)
71#define PIR0_MDC_HIGH (0x00000001)
72#define PIR0_MDC_LOW (0x00000000)
73/* ETHEREDRRR0 */
74#define EDRRR0_RR (0x00000001)
75/* ETHEREDTRR0 */
76#define EDTRR0_TR (0x00000003)
77/* software wait */
78#define LOOP_100us (6700) /* Loop counter for software wait 6666=100us/((1/400MHz)*6cyc) */
79
80#define EDMAC_EESIPR_INI_RECV (0x0205001F) /* 0x02000000 : Detect reception suspended */
81 /* 0x00040000 : Detect frame reception */
82 /* 0x00010000 : Receive FIFO overflow */
83 /* 0x00000010 : Residual bit frame reception */
84 /* 0x00000008 : Long frame reception */
85 /* 0x00000004 : Short frame reception */
86 /* 0x00000002 : PHY-LSI reception error */
87 /* 0x00000001 : Receive frame CRC error */
88#define EDMAC_EESIPR_INI_EtherC (0x00400000) /* 0x00400000 : E-MAC status register */
89
90void ethernet_address(char *);
91void ethernet_set_link(int, int);
92
93
94/* Send descriptor */
95typedef struct tag_edmac_send_desc {
96 uint32_t td0;
97 uint32_t td1;
98 uint8_t *td2;
99 uint32_t padding4;
100} edmac_send_desc_t;
101
102/* Receive descriptor */
103typedef struct tag_edmac_recv_desc {
104 uint32_t rd0;
105 uint32_t rd1;
106 uint8_t *rd2;
107 uint32_t padding4;
108} edmac_recv_desc_t;
109
110/* memory */
111/* The whole transmit/receive descriptors (must be allocated in 16-byte boundaries) */
112/* Transmit/receive buffers (must be allocated in 16-byte boundaries) */
113#if defined(__ICCARM__)
114#pragma data_alignment=16
115static uint8_t ethernet_nc_memory[(sizeof(edmac_send_desc_t) * NUM_OF_TX_DESCRIPTOR) +
116 (sizeof(edmac_recv_desc_t) * NUM_OF_RX_DESCRIPTOR) +
117 (NUM_OF_TX_DESCRIPTOR * SIZE_OF_BUFFER) +
118 (NUM_OF_RX_DESCRIPTOR * SIZE_OF_BUFFER)] //16 bytes aligned!
119 @ ".mirrorram";
120#else
121static uint8_t ethernet_nc_memory[(sizeof(edmac_send_desc_t) * NUM_OF_TX_DESCRIPTOR) +
122 (sizeof(edmac_recv_desc_t) * NUM_OF_RX_DESCRIPTOR) +
123 (NUM_OF_TX_DESCRIPTOR * SIZE_OF_BUFFER) +
124 (NUM_OF_RX_DESCRIPTOR * SIZE_OF_BUFFER)]
125 __attribute((section("NC_BSS"),aligned(16))); //16 bytes aligned!
126#endif
127static int32_t rx_read_offset; /* read offset */
128static int32_t tx_wite_offset; /* write offset */
129static uint32_t send_top_index;
130static uint32_t recv_top_index;
131static int32_t Interrupt_priority;
132static edmac_send_desc_t *p_eth_desc_dsend = NULL;
133static edmac_recv_desc_t *p_eth_desc_drecv = NULL;
134static edmac_recv_desc_t *p_recv_end_desc = NULL;
135static ethernetext_cb_fnc *p_recv_cb_fnc = NULL;
136static char mac_addr[6] = {0x00, 0x02, 0xF7, 0xF0, 0x00, 0x00}; /* MAC Address */
137static uint32_t phy_id = 0;
138static uint32_t start_stop = 1; /* 0:stop 1:start */
139static uint32_t tsu_ten_tmp = 0;
140
141volatile struct st_ether_from_tsu_adrh0* ETHER_FROM_TSU_ADRH0_ARRAY[ ETHER_FROM_TSU_ADRH0_ARRAY_COUNT ] =
142 /* ->MISRA 11.3 */ /* ->SEC R2.7.1 */
143 ETHER_FROM_TSU_ADRH0_ARRAY_ADDRESS_LIST;
144 /* <-MISRA 11.3 */ /* <-SEC R2.7.1 */
145
146/* function */
147static void lan_reg_reset(void);
148static void lan_desc_create(void);
149static void lan_reg_set(int32_t link);
150static uint16_t phy_reg_read(uint16_t reg_addr);
151static void phy_reg_write(uint16_t reg_addr, uint16_t data);
152static void mii_preamble(void);
153static void mii_cmd(uint16_t reg_addr, uint32_t option);
154static void mii_reg_read(uint16_t *data);
155static void mii_reg_write(uint16_t data);
156static void mii_z(void);
157static void mii_write_1(void);
158static void mii_write_0(void);
159static void set_ether_pir(uint32_t set_data);
160static void wait_100us(int32_t wait_cnt);
161
162
163int ethernetext_init(ethernet_cfg_t *p_ethcfg) {
164 int32_t i;
165 uint16_t val;
166
167 CPGSTBCR7 &= ~(CPG_STBCR7_BIT_MSTP74); /* enable ETHER clock */
168
169 /* P4_2(PHY Reset) */
170 GPIOP4 &= ~0x0004; /* Outputs low level */
171 GPIOPMC4 &= ~0x0004; /* Port mode */
172 GPIOPM4 &= ~0x0004; /* Output mode */
173
174 /* GPIO P1 P1_14(ET_COL) */
175 GPIOPMC1 |= 0x4000;
176 GPIOPFCAE1 &= ~0x4000;
177 GPIOPFCE1 |= 0x4000;
178 GPIOPFC1 |= 0x4000;
179
180 /* P3_0(ET_TXCLK), P3_3(ET_MDIO), P3_4(ET_RXCLK), P3_5(ET_RXER), P3_6(ET_RXDV) */
181 GPIOPMC3 |= 0x0079;
182 GPIOPFCAE3 &= ~0x0079;
183 GPIOPFCE3 &= ~0x0079;
184 GPIOPFC3 |= 0x0079;
185 GPIOPIPC3 |= 0x0079;
186
187 /* P5_9(ET_MDC) */
188 GPIOPMC5 |= 0x0200;
189 GPIOPFCAE5 &= ~0x0200;
190 GPIOPFCE5 &= ~0x0200;
191 GPIOPFC5 |= 0x0200;
192 GPIOPIPC5 |= 0x0200;
193
194 /* P10_1(ET_TXER), P10_2(ET_TXEN), P10_3(ET_CRS), P10_4(ET_TXD0), P10_5(ET_TXD1) */
195 /* P10_6(ET_TXD2), P10_7(ET_TXD3), P10_8(ET_RXD0), P10_9(ET_RXD1), P10_10(ET_RXD2), P10_11(ET_RXD3) */
196 GPIOPMC10 |= 0x0FFE;
197 GPIOPFCAE10 &= ~0x0FFE;
198 GPIOPFCE10 |= 0x0FFE;
199 GPIOPFC10 |= 0x0FFE;
200 GPIOPIPC10 |= 0x0FFE;
201
202 /* Resets the E-MAC,E-DMAC */
203 lan_reg_reset();
204
205 /* PHY Reset */
206 GPIOP4 &= ~0x0004; /* P4_2 Outputs low level */
207 wait_100us(250); /* 25msec */
208 GPIOP4 |= 0x0004; /* P4_2 Outputs high level */
209 wait_100us(100); /* 10msec */
210
211 /* Resets the PHY-LSI */
212 phy_reg_write(BASIC_MODE_CONTROL_REG, 0x8000);
213 for (i = 10000; i > 0; i--) {
214 val = phy_reg_read(BASIC_MODE_CONTROL_REG);
215 if (((uint32_t)val & 0x8000uL) == 0) {
216 break; /* Reset complete */
217 }
218 }
219
220 phy_id = ((uint32_t)phy_reg_read(PHY_IDENTIFIER1_REG) << 16)
221 | (uint32_t)phy_reg_read(PHY_IDENTIFIER2_REG);
222
223 Interrupt_priority = p_ethcfg->int_priority;
224 p_recv_cb_fnc = p_ethcfg->recv_cb;
225 start_stop = 1;
226
227 if (p_ethcfg->ether_mac != NULL) {
228 (void)memcpy(mac_addr, p_ethcfg->ether_mac, sizeof(mac_addr));
229 } else {
230 ethernet_address(mac_addr); /* Get MAC Address */
231 }
232
233 return 0;
234}
235
236void ethernetext_start_stop(int32_t mode) {
237 if (mode == 1) {
238 /* start */
239 ETHEREDTRR0 |= EDTRR0_TR;
240 ETHEREDRRR0 |= EDRRR0_RR;
241 start_stop = 1;
242 } else {
243 /* stop */
244 ETHEREDTRR0 &= ~EDTRR0_TR;
245 ETHEREDRRR0 &= ~EDRRR0_RR;
246 start_stop = 0;
247 }
248}
249
250int ethernetext_chk_link_mode(void) {
251 int32_t link;
252 uint16_t data;
253
254 if ((phy_id & M_PHY_ID) == PHY_ID_LAN8710A) {
255 data = phy_reg_read(PHY_SP_CTL_STS_REG);
256 switch (((uint32_t)data >> 2) & 0x00000007) {
257 case 0x0001:
258 link = HALF_10M;
259 break;
260 case 0x0005:
261 link = FULL_10M;
262 break;
263 case 0x0002:
264 link = HALF_TX;
265 break;
266 case 0x0006:
267 link = FULL_TX;
268 break;
269 default:
270 link = NEGO_FAIL;
271 break;
272 }
273 } else {
274 link = NEGO_FAIL;
275 }
276
277 return link;
278}
279
280void ethernetext_set_link_mode(int32_t link) {
281 lan_reg_reset(); /* Resets the E-MAC,E-DMAC */
282 lan_desc_create(); /* Initialize of buffer memory */
283 lan_reg_set(link); /* E-DMAC, E-MAC initialization */
284}
285
286void ethernetext_add_multicast_group(const uint8_t *addr) {
287 uint32_t cnt;
288 uint32_t tmp_data_h;
289 uint32_t tmp_data_l;
290
291 if (tsu_ten_tmp == 0xFFFFFFFF) {
292 ethernetext_set_all_multicast(1);
293 } else {
294 tmp_data_h = ((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) | ((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]);
295 tmp_data_l = ((uint32_t)addr[4] << 8) | ((uint32_t)addr[5]);
296
297 for (cnt = 0; cnt < 32; cnt++) {
298 if ((tsu_ten_tmp & (0x80000000 >> cnt)) == 0) {
299 while ((ETHERTSU_ADSBSY & 0x00000001) != 0) {
300 ;
301 }
302 ETHER_FROM_TSU_ADRH0_ARRAY[cnt]->TSU_ADRH0 = tmp_data_h;
303 while ((ETHERTSU_ADSBSY & 0x00000001) != 0) {
304 ;
305 }
306 ETHER_FROM_TSU_ADRH0_ARRAY[cnt]->TSU_ADRL0 = tmp_data_l;
307 if ((ETHERECMR0 & 0x00002000) != 0) {
308 ETHERTSU_TEN |= (0x80000000 >> cnt);
309 }
310 tsu_ten_tmp |= (0x80000000 >> cnt);
311 break;
312 }
313 }
314 }
315}
316
317void ethernetext_remove_multicast_group(const uint8_t *addr) {
318 uint32_t cnt;
319 uint32_t tmp_data_h;
320 uint32_t tmp_data_l;
321
322 tmp_data_h = ((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) | ((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]);
323 tmp_data_l = ((uint32_t)addr[4] << 8) | ((uint32_t)addr[5]);
324
325 for (cnt = 0; cnt< 32; cnt++) {
326 if ((ETHER_FROM_TSU_ADRH0_ARRAY[cnt]->TSU_ADRH0 == tmp_data_h) &&
327 (ETHER_FROM_TSU_ADRH0_ARRAY[cnt]->TSU_ADRL0 == tmp_data_l)) {
328 while ((ETHERTSU_ADSBSY & 0x00000001) != 0) {
329 ;
330 }
331 ETHER_FROM_TSU_ADRH0_ARRAY[cnt]->TSU_ADRH0 = 0;
332 while ((ETHERTSU_ADSBSY & 0x00000001) != 0) {
333 ;
334 }
335 ETHER_FROM_TSU_ADRH0_ARRAY[cnt]->TSU_ADRL0 = 0;
336
337 ETHERTSU_TEN &= ~(0x80000000 >> cnt);
338 tsu_ten_tmp &= ~(0x80000000 >> cnt);
339 break;
340 }
341 }
342}
343
344void ethernetext_set_all_multicast(int all) {
345 if (all != 0) {
346 ETHERECMR0 &= ~(0x00002000);
347 ETHERTSU_TEN = 0x00000000;
348 } else {
349 ETHERECMR0 |= 0x00002000;
350 ETHERTSU_TEN = tsu_ten_tmp;
351 }
352}
353
354
355int ethernet_init() {
356 ethernet_cfg_t ethcfg;
357
358 ethcfg.int_priority = 5;
359 ethcfg.recv_cb = NULL;
360 ethcfg.ether_mac = NULL;
361 ethernetext_init(&ethcfg);
362 ethernet_set_link(-1, 0); /* Auto-Negotiation */
363
364 return 0;
365}
366
367void ethernet_free() {
368 ETHERARSTR |= 0x00000001; /* ETHER software reset */
369 CPGSTBCR7 |= CPG_STBCR7_BIT_MSTP74; /* disable ETHER clock */
370}
371
372int ethernet_write(const char *data, int slen) {
373 edmac_send_desc_t *p_send_desc;
374 int32_t copy_size;
375
376 if ((p_eth_desc_dsend == NULL) || (data == NULL) || (slen < 0)
377 || (tx_wite_offset < 0) || (tx_wite_offset >= MAX_SEND_SIZE)) {
378 copy_size = 0;
379 } else {
380 p_send_desc = &p_eth_desc_dsend[send_top_index]; /* Current descriptor */
381 if ((p_send_desc->td0 & TD0_TACT) != 0) {
382 copy_size = 0;
383 } else {
384 copy_size = MAX_SEND_SIZE - tx_wite_offset;
385 if (copy_size > slen) {
386 copy_size = slen;
387 }
388 (void)memcpy(&p_send_desc->td2[tx_wite_offset], data, copy_size);
389 tx_wite_offset += copy_size;
390 }
391 }
392
393 return copy_size;
394}
395
396int ethernet_send() {
397 edmac_send_desc_t *p_send_desc;
398 int32_t ret;
399
400 if ((p_eth_desc_dsend == NULL) || (tx_wite_offset <= 0)) {
401 ret = 0;
402 } else {
403 /* Transfer 1 frame */
404 p_send_desc = &p_eth_desc_dsend[send_top_index]; /* Current descriptor */
405
406 /* Sets the frame length */
407 p_send_desc->td1 = ((uint32_t)tx_wite_offset << 16);
408 tx_wite_offset = 0;
409
410 /* Sets the transmit descriptor to transmit again */
411 p_send_desc->td0 &= (TD0_TACT | TD0_TDLE | TD0_TFP_TOP_BOTTOM);
412 p_send_desc->td0 |= TD0_TACT;
413 if ((start_stop == 1) && ((ETHEREDTRR0 & EDTRR0_TR) != EDTRR0_TR)) {
414 ETHEREDTRR0 |= EDTRR0_TR;
415 }
416
417 /* Update the current descriptor */
418 send_top_index++;
419 if (send_top_index >= NUM_OF_TX_DESCRIPTOR) {
420 send_top_index = 0;
421 }
422 ret = 1;
423 }
424
425 return ret;
426}
427
428int ethernet_receive() {
429 edmac_recv_desc_t *p_recv_desc;
430 int32_t receive_size = 0;
431
432 if (p_eth_desc_drecv != NULL) {
433 if (p_recv_end_desc != NULL) {
434 /* Sets the receive descriptor to receive again */
435 p_recv_end_desc->rd0 &= (RD0_RACT | RD0_RDLE);
436 p_recv_end_desc->rd0 |= RD0_RACT;
437 if ((start_stop == 1) && ((ETHEREDRRR0 & EDRRR0_RR) == 0)) {
438 ETHEREDRRR0 |= EDRRR0_RR;
439 }
440 p_recv_end_desc = NULL;
441 }
442
443 p_recv_desc = &p_eth_desc_drecv[recv_top_index]; /* Current descriptor */
444 if ((p_recv_desc->rd0 & RD0_RACT) == 0) {
445 /* Receives 1 frame */
446 if (((p_recv_desc->rd0 & RD0_RFE) != 0) && ((p_recv_desc->rd0 & RD0_RFS_ERROR) != 0)) {
447 /* Receive frame error */
448 /* Sets the receive descriptor to receive again */
449 p_recv_desc->rd0 &= (RD0_RACT | RD0_RDLE);
450 p_recv_desc->rd0 |= RD0_RACT;
451 if ((start_stop == 1) && ((ETHEREDRRR0 & EDRRR0_RR) == 0)) {
452 ETHEREDRRR0 |= EDRRR0_RR;
453 }
454 } else {
455 /* Copies the received frame */
456 rx_read_offset = 0;
457 p_recv_end_desc = p_recv_desc;
458 receive_size = (p_recv_desc->rd1 & RD1_RDL_MSK); /* number of bytes received */
459 }
460
461 /* Update the current descriptor */
462 recv_top_index++;
463 if (recv_top_index >= NUM_OF_TX_DESCRIPTOR) {
464 recv_top_index = 0;
465 }
466 }
467 }
468
469 return receive_size;
470}
471
472int ethernet_read(char *data, int dlen) {
473 edmac_recv_desc_t *p_recv_desc = p_recv_end_desc; /* Read top descriptor */
474 int32_t copy_size;
475
476 if ((data == NULL) || (dlen < 0) || (p_recv_desc == NULL)) {
477 copy_size = 0;
478 } else {
479 copy_size = (p_recv_desc->rd1 & RD1_RDL_MSK) - rx_read_offset;
480 if (copy_size > dlen) {
481 copy_size = dlen;
482 }
483 (void)memcpy(data, &p_recv_desc->rd2[rx_read_offset], (size_t)copy_size);
484 rx_read_offset += copy_size;
485 }
486
487 return copy_size;
488}
489
490void ethernet_address(char *mac) {
491 if (mac != NULL) {
492 mbed_mac_address(mac); /* Get MAC Address */
493 }
494}
495
496int ethernet_link(void) {
497 int32_t ret;
498 uint16_t data;
499
500 data = phy_reg_read(BASIC_MODE_STATUS_REG);
501 if (((uint32_t)data & BASIC_STS_MSK_LINK) != 0) {
502 ret = 1;
503 } else {
504 ret = 0;
505 }
506
507 return ret;
508}
509
510void ethernet_set_link(int speed, int duplex) {
511 uint16_t data;
512 int32_t i;
513 int32_t link;
514
515 if ((speed < 0) || (speed > 1)) {
516 data = 0x1000; /* Auto-Negotiation Enable */
517 phy_reg_write(BASIC_MODE_CONTROL_REG, data);
518 for (i = 0; i < 1000; i++) {
519 data = phy_reg_read(BASIC_MODE_STATUS_REG);
520 if (((uint32_t)data & BASIC_STS_MSK_AUTO_CMP) != 0) {
521 break;
522 }
523 wait_100us(10);
524 }
525 } else {
526 data = (uint16_t)(((uint32_t)speed << 13) | ((uint32_t)duplex << 8));
527 phy_reg_write(BASIC_MODE_CONTROL_REG, data);
528 wait_100us(1);
529 }
530
531 link = ethernetext_chk_link_mode();
532 ethernetext_set_link_mode(link);
533}
534
535void INT_Ether(void) {
536 uint32_t stat_edmac;
537 uint32_t stat_etherc;
538
539 /* Clear the interrupt request flag */
540 stat_edmac = (ETHEREESR0 & ETHEREESIPR0); /* Targets are restricted to allowed interrupts */
541 ETHEREESR0 = stat_edmac;
542 /* Reception-related */
543 if (stat_edmac & EDMAC_EESIPR_INI_RECV) {
544 if (p_recv_cb_fnc != NULL) {
545 p_recv_cb_fnc();
546 }
547 }
548 /* E-MAC-related */
549 if (stat_edmac & EDMAC_EESIPR_INI_EtherC) {
550 /* Clear the interrupt request flag */
551 stat_etherc = (ETHERECSR0 & ETHERECSIPR0); /* Targets are restricted to allowed interrupts */
552 ETHERECSR0 = stat_etherc;
553 }
554}
555
556static void lan_reg_reset(void) {
557 volatile int32_t j = 400; /* Wait for B dia 256 cycles ((I dia/B dia)*256)/6cyc = 8*256/6 = 342 */
558
559 ETHERARSTR |= 0x00000001; /* ETHER software reset */
560 while (j--) {
561 /* Do Nothing */
562 }
563
564 ETHEREDSR0 |= 0x00000003; /* E-DMAC software reset */
565 ETHEREDMR0 |= 0x00000003; /* Set SWRR and SWRT simultaneously */
566
567 /* Check clear software reset */
568 while ((ETHEREDMR0 & 0x00000003) != 0) {
569 /* Do Nothing */
570 }
571}
572
573static void lan_desc_create(void) {
574 int32_t i;
575 uint8_t *p_memory_top;
576
577 (void)memset((void *)ethernet_nc_memory, 0, sizeof(ethernet_nc_memory));
578 p_memory_top = ethernet_nc_memory;
579
580 /* Descriptor area configuration */
581 p_eth_desc_dsend = (edmac_send_desc_t *)p_memory_top;
582 p_memory_top += (sizeof(edmac_send_desc_t) * NUM_OF_TX_DESCRIPTOR);
583 p_eth_desc_drecv = (edmac_recv_desc_t *)p_memory_top;
584 p_memory_top += (sizeof(edmac_recv_desc_t) * NUM_OF_RX_DESCRIPTOR);
585
586 /* Transmit descriptor */
587 for (i = 0; i < NUM_OF_TX_DESCRIPTOR; i++) {
588 p_eth_desc_dsend[i].td2 = p_memory_top; /* TD2 TBA */
589 p_memory_top += SIZE_OF_BUFFER;
590 p_eth_desc_dsend[i].td1 = 0; /* TD1 TDL */
591 p_eth_desc_dsend[i].td0 = TD0_TFP_TOP_BOTTOM; /* TD0:1frame/1buf1buf, transmission disabled */
592 }
593 p_eth_desc_dsend[i - 1].td0 |= TD0_TDLE; /* Set the last descriptor */
594
595 /* Receive descriptor */
596 for (i = 0; i < NUM_OF_RX_DESCRIPTOR; i++) {
597 p_eth_desc_drecv[i].rd2 = p_memory_top; /* RD2 RBA */
598 p_memory_top += SIZE_OF_BUFFER;
599 p_eth_desc_drecv[i].rd1 = ((uint32_t)SIZE_OF_BUFFER << 16); /* RD1 RBL */
600 p_eth_desc_drecv[i].rd0 = RD0_RACT; /* RD0:reception enabled */
601 }
602 p_eth_desc_drecv[i - 1].rd0 |= RD0_RDLE; /* Set the last descriptor */
603
604 /* Initialize descriptor management information */
605 send_top_index = 0;
606 recv_top_index = 0;
607 rx_read_offset = 0;
608 tx_wite_offset = 0;
609 p_recv_end_desc = NULL;
610}
611
612static void lan_reg_set(int32_t link) {
613 int32_t prm = link & PROMISCUOUS_MODE;
614 link &= ~PROMISCUOUS_MODE;
615
616 /* MAC address setting */
617 ETHERMAHR0 = ((uint8_t)mac_addr[0] << 24)
618 | ((uint8_t)mac_addr[1] << 16)
619 | ((uint8_t)mac_addr[2] << 8)
620 | (uint8_t)mac_addr[3];
621 ETHERMALR0 = ((uint8_t)mac_addr[4] << 8)
622 | (uint8_t)mac_addr[5];
623
624 /* E-DMAC */
625 ETHERTDLAR0 = (uint32_t)&p_eth_desc_dsend[0];
626 ETHERRDLAR0 = (uint32_t)&p_eth_desc_drecv[0];
627 ETHERTDFAR0 = (uint32_t)&p_eth_desc_dsend[0];
628 ETHERRDFAR0 = (uint32_t)&p_eth_desc_drecv[0];
629 ETHERTDFXR0 = (uint32_t)&p_eth_desc_dsend[NUM_OF_TX_DESCRIPTOR - 1];
630 ETHERRDFXR0 = (uint32_t)&p_eth_desc_drecv[NUM_OF_RX_DESCRIPTOR - 1];
631 ETHERTDFFR0 |= 0x00000001; /* TDLF Transmit Descriptor Queue Last Flag : Last descriptor (1) */
632 ETHERRDFFR0 |= 0x00000001; /* RDLF Receive Descriptor Queue Last Flag : Last descriptor (1) */
633 ETHEREDMR0 |= 0x00000040; /* Little endian */
634 ETHERTRSCER0 &= ~0x0003009F; /* All clear */
635 ETHERTFTR0 &= ~0x000007FF; /* TFT[10:0] Transmit FIFO Threshold : Store and forward modes (H'000) */
636 ETHERFDR0 |= 0x00000707; /* Transmit FIFO Size:2048 bytes, Receive FIFO Size:2048 bytes */
637 ETHERRMCR0 |= 0x00000001; /* RNC Receive Enable Control : Continuous reception enabled (1) */
638 ETHERFCFTR0 &= ~0x001F00FF;
639 ETHERFCFTR0 |= 0x00070007;
640 ETHERRPADIR0 &= ~0x001FFFFF; /* Padding Size:No padding insertion, Padding Slot:Inserts at first byte */
641
642 /* E-MAC */
643 ETHERECMR0 &= ~0x04BF2063; /* All clear */
644 ETHERRFLR0 &= ~0x0003FFFF; /* RFL[17:0] Receive Frame Length : 1518 bytes (H'00000) */
645 ETHERAPR0 &= ~0x0000FFFF; /* AP[15:0] Automatic PAUSE : Flow control is disabled (H'0000) */
646 ETHERMPR0 &= ~0x0000FFFF; /* MP[15:0] Manual PAUSE : Flow control is disabled (H'0000) */
647 ETHERTPAUSER0 &= ~0x0000FFFF; /* Upper Limit for Automatic PAUSE Frame : Retransmit count is unlimited */
648 ETHERCSMR &= ~0xC000003F; /* The result of checksum is not written back to the receive descriptor */
649 if ((link == FULL_TX) || (link == FULL_10M) || (link == NEGO_FAIL)) {
650 ETHERECMR0 |= 0x00000002; /* Set to full-duplex mode */
651 } else {
652 ETHERECMR0 &= ~0x00000002; /* Set to half-duplex mode */
653 }
654 ETHERECMR0 |= 0x00002000; /* MCT = 1 */
655
656 /* Interrupt-related */
657 if (p_recv_cb_fnc != NULL) {
658 ETHEREESR0 |= 0xFF7F009F; /* Clear all status (by writing 1) */
659 ETHEREESIPR0 |= 0x00040000; /* FR Frame Reception (1) */
660 ETHERECSR0 |= 0x00000011; /* Clear all status (clear by writing 1) */
661 ETHERECSIPR0 &= ~0x00000011; /* PFROIP Disable, ICDIP Disable */
662 /*InterruptHandlerRegister(ETHERI_IRQn, INT_Ether); /* Ethernet interrupt handler registration */
663 GIC_SetPriority(ETHERI_IRQn, Interrupt_priority); /* Ethernet interrupt priority */
664 GIC_SetConfiguration(ETHERI_IRQn, 1);
665 GIC_EnableIRQ(ETHERI_IRQn); /* Enables the E-DMAC interrupt */
666 }
667
668 if (prm) {
669 ETHERECMR0 |= 0x00000001; /* Promiscuous Mode */
670 }
671
672 ETHERECMR0 |= 0x00000060; /* RE Enable, TE Enable */
673
674 /* Enable transmission/reception */
675 if ((start_stop == 1) && ((ETHEREDRRR0 & 0x00000001) == 0)) {
676 ETHEREDRRR0 |= 0x00000001; /* RR */
677 }
678}
679
680static uint16_t phy_reg_read(uint16_t reg_addr) {
681 uint16_t data;
682
683 mii_preamble();
684 mii_cmd(reg_addr, PHY_READ);
685 mii_z();
686 mii_reg_read(&data);
687 mii_z();
688
689 return data;
690}
691
692static void phy_reg_write(uint16_t reg_addr, uint16_t data) {
693 mii_preamble();
694 mii_cmd(reg_addr, PHY_WRITE);
695 mii_write_1();
696 mii_write_0();
697 mii_reg_write(data);
698 mii_z();
699}
700
701static void mii_preamble(void) {
702 int32_t i = 32;
703
704 for (i = 32; i > 0; i--) {
705 /* 1 is output via the MII (Media Independent Interface) block. */
706 mii_write_1();
707 }
708}
709
710static void mii_cmd(uint16_t reg_addr, uint32_t option) {
711 int32_t i;
712 uint16_t data = 0;
713
714 data |= (PHY_ST << 14); /* ST code */
715 data |= (option << 12); /* OP code */
716 data |= (PHY_ADDR << 7); /* PHY Address */
717 data |= (uint16_t)(reg_addr << 2); /* Reg Address */
718 for (i = 14; i > 0; i--) {
719 if ((data & 0x8000) == 0) {
720 mii_write_0();
721 } else {
722 mii_write_1();
723 }
724 data <<= 1;
725 }
726}
727
728static void mii_reg_read(uint16_t *data) {
729 int32_t i;
730 uint16_t reg_data = 0;
731
732 /* Data are read in one bit at a time */
733 for (i = 16; i > 0; i--) {
734 set_ether_pir(PIR0_MDC_LOW);
735 set_ether_pir(PIR0_MDC_HIGH);
736 reg_data <<= 1;
737 reg_data |= (uint16_t)((ETHERPIR0 & PIR0_MDI) >> 3); /* MDI read */
738 set_ether_pir(PIR0_MDC_HIGH);
739 set_ether_pir(PIR0_MDC_LOW);
740 }
741 *data = reg_data;
742}
743
744static void mii_reg_write(uint16_t data) {
745 int32_t i;
746
747 /* Data are written one bit at a time */
748 for (i = 16; i > 0; i--) {
749 if ((data & 0x8000) == 0) {
750 mii_write_0();
751 } else {
752 mii_write_1();
753 }
754 data <<= 1;
755 }
756}
757
758static void mii_z(void) {
759 set_ether_pir(PIR0_MDC_LOW);
760 set_ether_pir(PIR0_MDC_HIGH);
761 set_ether_pir(PIR0_MDC_HIGH);
762 set_ether_pir(PIR0_MDC_LOW);
763}
764
765static void mii_write_1(void) {
766 set_ether_pir(PIR0_MDO | PIR0_MMD);
767 set_ether_pir(PIR0_MDO | PIR0_MMD | PIR0_MDC);
768 set_ether_pir(PIR0_MDO | PIR0_MMD | PIR0_MDC);
769 set_ether_pir(PIR0_MDO | PIR0_MMD);
770}
771
772static void mii_write_0(void) {
773 set_ether_pir(PIR0_MMD);
774 set_ether_pir(PIR0_MMD | PIR0_MDC);
775 set_ether_pir(PIR0_MMD | PIR0_MDC);
776 set_ether_pir(PIR0_MMD);
777}
778
779static void set_ether_pir(uint32_t set_data) {
780 int32_t i;
781
782 for (i = MDC_WAIT; i > 0; i--) {
783 ETHERPIR0 = set_data;
784 }
785}
786
787static void wait_100us(int32_t wait_cnt) {
788 volatile int32_t j = LOOP_100us * wait_cnt;
789
790 while (--j) {
791 /* Do Nothing */
792 }
793}
794#endif /* DEVICE_ETHERNET */
Note: See TracBrowser for help on using the repository browser.