s3c4510b_eth.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /***********************************************************************
  2. *
  3. * Copyright (c) 2004 Cucy Systems (http://www.cucy.com)
  4. * Curt Brune <curt@cucy.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. * Description: Ethernet interface for Samsung S3C4510B SoC
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <net.h>
  29. #include <asm/hardware.h>
  30. #include "s3c4510b_eth.h"
  31. static TX_FrameDescriptor txFDbase[ETH_MaxTxFrames];
  32. static MACFrame txFrameBase[ETH_MaxTxFrames];
  33. static RX_FrameDescriptor rxFDbase[PKTBUFSRX];
  34. static ETH m_eth;
  35. static s32 TxFDinit( ETH *eth) {
  36. s32 i;
  37. MACFrame *txFrmBase;
  38. /* disable cache for access to the TX buffers */
  39. txFrmBase = (MACFrame *)( (u32)txFrameBase | CACHE_DISABLE_MASK);
  40. /* store start of Tx descriptors and set current */
  41. eth->m_curTX_FD = (TX_FrameDescriptor *) ((u32)txFDbase | CACHE_DISABLE_MASK);
  42. eth->m_baseTX_FD = eth->m_curTX_FD;
  43. for ( i = 0; i < ETH_MaxTxFrames; i++) {
  44. eth->m_baseTX_FD[i].m_frameDataPtr.bf.dataPtr = (u32)&txFrmBase[i];
  45. eth->m_baseTX_FD[i].m_frameDataPtr.bf.owner = 0x0; /* CPU owner */
  46. eth->m_baseTX_FD[i].m_opt.ui = 0x0;
  47. eth->m_baseTX_FD[i].m_status.ui = 0x0;
  48. eth->m_baseTX_FD[i].m_nextFD = &eth->m_baseTX_FD[i+1];
  49. }
  50. /* make the list circular */
  51. eth->m_baseTX_FD[i-1].m_nextFD = &eth->m_baseTX_FD[0];
  52. PUT_REG( REG_BDMATXPTR, (u32)eth->m_curTX_FD);
  53. return 0;
  54. }
  55. static s32 RxFDinit( ETH *eth) {
  56. s32 i;
  57. /* MACFrame *rxFrmBase; */
  58. /* disable cache for access to the RX buffers */
  59. /* rxFrmBase = (MACFrame *)( (u32)rxFrameBase | CACHE_DISABLE_MASK); */
  60. /* store start of Rx descriptors and set current */
  61. eth->m_curRX_FD = (RX_FrameDescriptor *)((u32)rxFDbase | CACHE_DISABLE_MASK);
  62. eth->m_baseRX_FD = eth->m_curRX_FD;
  63. for ( i = 0; i < PKTBUFSRX; i++) {
  64. eth->m_baseRX_FD[i].m_frameDataPtr.bf.dataPtr = (u32)NetRxPackets[i] | CACHE_DISABLE_MASK;
  65. eth->m_baseRX_FD[i].m_frameDataPtr.bf.owner = 0x1; /* BDMA owner */
  66. eth->m_baseRX_FD[i].m_reserved = 0x0;
  67. eth->m_baseRX_FD[i].m_status.ui = 0x0;
  68. eth->m_baseRX_FD[i].m_nextFD = &eth->m_baseRX_FD[i+1];
  69. }
  70. /* make the list circular */
  71. eth->m_baseRX_FD[i-1].m_nextFD = &eth->m_baseRX_FD[0];
  72. PUT_REG( REG_BDMARXPTR, (u32)eth->m_curRX_FD);
  73. return 0;
  74. }
  75. /*
  76. * Public u-boot interface functions below
  77. */
  78. int eth_init(bd_t *bis)
  79. {
  80. ETH *eth = &m_eth;
  81. /* store our MAC address */
  82. eth->m_mac = bis->bi_enetaddr;
  83. /* setup DBMA and MAC */
  84. PUT_REG( REG_BDMARXCON, ETH_BRxRS); /* reset BDMA RX machine */
  85. PUT_REG( REG_BDMATXCON, ETH_BTxRS); /* reset BDMA TX machine */
  86. PUT_REG( REG_MACCON , ETH_SwReset); /* reset MAC machine */
  87. PUT_REG( REG_BDMARXLSZ, sizeof(MACFrame));
  88. PUT_REG( REG_MACCON , 0); /* reset MAC machine */
  89. /* init frame descriptors */
  90. TxFDinit( eth);
  91. RxFDinit( eth);
  92. /* init the CAM with our MAC address */
  93. PUT_REG( REG_CAM_BASE, (eth->m_mac[0] << 24) |
  94. (eth->m_mac[1] << 16) |
  95. (eth->m_mac[2] << 8) |
  96. (eth->m_mac[3]));
  97. PUT_REG( REG_CAM_BASE + 0x4, (eth->m_mac[4] << 24) |
  98. (eth->m_mac[5] << 16));
  99. /* enable CAM address 1 -- the MAC we just loaded */
  100. PUT_REG( REG_CAMEN, 0x1);
  101. PUT_REG( REG_CAMCON,
  102. ETH_BroadAcc | /* accept broadcast packetes */
  103. ETH_CompEn); /* enable compare mode (check against the CAM) */
  104. /* configure the BDMA Transmitter control */
  105. PUT_REG( REG_BDMATXCON,
  106. ETH_BTxBRST | /* BDMA Tx burst size 16 words */
  107. ETH_BTxMSL110 | /* BDMA Tx wait to fill 6/8 of the BDMA */
  108. ETH_BTxSTSKO | /* BDMA Tx interrupt(Stop) on non-owner TX FD */
  109. ETH_BTxEn); /* BDMA Tx Enable */
  110. /* configure the MAC Transmitter control */
  111. PUT_REG( REG_MACTXCON,
  112. ETH_EnComp | /* interrupt when the MAC transmits or discards packet */
  113. ETH_TxEn); /* MAC transmit enable */
  114. /* configure the BDMA Receiver control */
  115. PUT_REG( REG_BDMARXCON,
  116. ETH_BRxBRST | /* BDMA Rx Burst Size 16 words */
  117. ETH_BRxSTSKO | /* BDMA Rx interrupt(Stop) on non-owner RX FD */
  118. ETH_BRxMAINC | /* BDMA Rx Memory Address increment */
  119. ETH_BRxDIE | /* BDMA Rx Every Received Frame Interrupt Enable */
  120. ETH_BRxNLIE | /* BDMA Rx NULL List Interrupt Enable */
  121. ETH_BRxNOIE | /* BDMA Rx Not Owner Interrupt Enable */
  122. ETH_BRxLittle | /* BDMA Rx Little endian */
  123. ETH_BRxEn); /* BDMA Rx Enable */
  124. /* configure the MAC Receiver control */
  125. PUT_REG( REG_MACRXCON,
  126. ETH_RxEn); /* MAC ETH_RxEn */
  127. return 0;
  128. }
  129. /* Send a packet */
  130. s32 eth_send(volatile void *packet, s32 length)
  131. {
  132. u32 i;
  133. ETH *eth = &m_eth;
  134. if ( eth->m_curTX_FD->m_frameDataPtr.bf.owner) {
  135. printf("eth_send(): TX Frame. CPU not owner.\n");
  136. return -1;
  137. }
  138. /* copy user data into frame data pointer */
  139. memcpy((void *)((u32)(eth->m_curTX_FD->m_frameDataPtr.bf.dataPtr)),
  140. (void *)packet,
  141. length);
  142. /* Set TX Frame flags */
  143. eth->m_curTX_FD->m_opt.bf.widgetAlign = 0;
  144. eth->m_curTX_FD->m_opt.bf.frameDataDir = 1;
  145. eth->m_curTX_FD->m_opt.bf.littleEndian = 1;
  146. eth->m_curTX_FD->m_opt.bf.macTxIrqEnbl = 1;
  147. eth->m_curTX_FD->m_opt.bf.no_crc = 0;
  148. eth->m_curTX_FD->m_opt.bf.no_padding = 0;
  149. /* Set TX Frame length */
  150. eth->m_curTX_FD->m_status.bf.len = length;
  151. /* Change ownership to BDMA */
  152. eth->m_curTX_FD->m_frameDataPtr.bf.owner = 1;
  153. /* Enable MAC and BDMA Tx control register */
  154. SET_REG( REG_BDMATXCON, ETH_BTxEn);
  155. SET_REG( REG_MACTXCON, ETH_TxEn);
  156. /* poll on TX completion status */
  157. while ( !eth->m_curTX_FD->m_status.bf.complete) {
  158. /* sleep */
  159. for ( i = 0; i < 0x10000; i ++);
  160. }
  161. /* Change the Tx frame descriptor for next use */
  162. eth->m_curTX_FD = eth->m_curTX_FD->m_nextFD;
  163. return 0;
  164. }
  165. /* Check for received packets */
  166. s32 eth_rx (void)
  167. {
  168. s32 nLen = 0;
  169. ETH *eth = &m_eth;
  170. /* check if packet ready */
  171. if ( (GET_REG( REG_BDMASTAT)) & ETH_S_BRxRDF) {
  172. /* process all waiting packets */
  173. while ( !eth->m_curRX_FD->m_frameDataPtr.bf.owner) {
  174. nLen = eth->m_curRX_FD->m_status.bf.len;
  175. /* call back u-boot -- may call eth_send() */
  176. NetReceive ((u8 *)eth->m_curRX_FD->m_frameDataPtr.ui, nLen);
  177. /* set owner back to CPU */
  178. eth->m_curRX_FD->m_frameDataPtr.bf.owner = 1;
  179. /* clear status */
  180. eth->m_curRX_FD->m_status.ui = 0x0;
  181. /* advance to next descriptor */
  182. eth->m_curRX_FD = eth->m_curRX_FD->m_nextFD;
  183. /* clear received frame bit */
  184. PUT_REG( REG_BDMASTAT, ETH_S_BRxRDF);
  185. }
  186. }
  187. return nLen;
  188. }
  189. /* Halt ethernet engine */
  190. void eth_halt(void)
  191. {
  192. /* disable MAC */
  193. PUT_REG( REG_MACCON, ETH_HaltReg);
  194. }