ftmac100.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Faraday FTMAC100 Ethernet
  3. *
  4. * (C) Copyright 2009 Faraday Technology
  5. * Po-Yu Chuang <ratbert@faraday-tech.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <common.h>
  23. #include <malloc.h>
  24. #include <net.h>
  25. #include <asm/io.h>
  26. #include "ftmac100.h"
  27. #define ETH_ZLEN 60
  28. struct ftmac100_data {
  29. struct ftmac100_txdes txdes[1];
  30. struct ftmac100_rxdes rxdes[PKTBUFSRX];
  31. int rx_index;
  32. };
  33. /*
  34. * Reset MAC
  35. */
  36. static void ftmac100_reset (struct eth_device *dev)
  37. {
  38. struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
  39. debug ("%s()\n", __func__);
  40. writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
  41. while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
  42. ;
  43. }
  44. /*
  45. * Set MAC address
  46. */
  47. static void ftmac100_set_mac (struct eth_device *dev, const unsigned char *mac)
  48. {
  49. struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
  50. unsigned int maddr = mac[0] << 8 | mac[1];
  51. unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
  52. debug ("%s(%x %x)\n", __func__, maddr, laddr);
  53. writel (maddr, &ftmac100->mac_madr);
  54. writel (laddr, &ftmac100->mac_ladr);
  55. }
  56. static void ftmac100_set_mac_from_env (struct eth_device *dev)
  57. {
  58. eth_getenv_enetaddr ("ethaddr", dev->enetaddr);
  59. ftmac100_set_mac (dev, dev->enetaddr);
  60. }
  61. /*
  62. * disable transmitter, receiver
  63. */
  64. static void ftmac100_halt (struct eth_device *dev)
  65. {
  66. struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
  67. debug ("%s()\n", __func__);
  68. writel (0, &ftmac100->maccr);
  69. }
  70. static int ftmac100_init (struct eth_device *dev, bd_t *bd)
  71. {
  72. struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
  73. struct ftmac100_data *priv = dev->priv;
  74. struct ftmac100_txdes *txdes = priv->txdes;
  75. struct ftmac100_rxdes *rxdes = priv->rxdes;
  76. unsigned int maccr;
  77. int i;
  78. debug ("%s()\n", __func__);
  79. ftmac100_reset (dev);
  80. /* set the ethernet address */
  81. ftmac100_set_mac_from_env (dev);
  82. /* disable all interrupts */
  83. writel (0, &ftmac100->imr);
  84. /* initialize descriptors */
  85. priv->rx_index = 0;
  86. txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
  87. rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
  88. for (i = 0; i < PKTBUFSRX; i++) {
  89. /* RXBUF_BADR */
  90. rxdes[i].rxdes2 = (unsigned int)NetRxPackets[i];
  91. rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
  92. rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
  93. }
  94. /* transmit ring */
  95. writel ((unsigned int)txdes, &ftmac100->txr_badr);
  96. /* receive ring */
  97. writel ((unsigned int)rxdes, &ftmac100->rxr_badr);
  98. /* poll receive descriptor automatically */
  99. writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
  100. /* enable transmitter, receiver */
  101. maccr = FTMAC100_MACCR_XMT_EN |
  102. FTMAC100_MACCR_RCV_EN |
  103. FTMAC100_MACCR_XDMA_EN |
  104. FTMAC100_MACCR_RDMA_EN |
  105. FTMAC100_MACCR_CRC_APD |
  106. FTMAC100_MACCR_ENRX_IN_HALFTX |
  107. FTMAC100_MACCR_RX_RUNT |
  108. FTMAC100_MACCR_RX_BROADPKT;
  109. writel (maccr, &ftmac100->maccr);
  110. return 0;
  111. }
  112. /*
  113. * Get a data block via Ethernet
  114. */
  115. static int ftmac100_recv (struct eth_device *dev)
  116. {
  117. struct ftmac100_data *priv = dev->priv;
  118. struct ftmac100_rxdes *curr_des;
  119. unsigned short rxlen;
  120. curr_des = &priv->rxdes[priv->rx_index];
  121. if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
  122. return -1;
  123. if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
  124. FTMAC100_RXDES0_CRC_ERR |
  125. FTMAC100_RXDES0_FTL |
  126. FTMAC100_RXDES0_RUNT |
  127. FTMAC100_RXDES0_RX_ODD_NB)) {
  128. return -1;
  129. }
  130. rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
  131. debug ("%s(): RX buffer %d, %x received\n",
  132. __func__, priv->rx_index, rxlen);
  133. /* pass the packet up to the protocol layers. */
  134. NetReceive ((void *)curr_des->rxdes2, rxlen);
  135. /* release buffer to DMA */
  136. curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
  137. priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
  138. return 0;
  139. }
  140. /*
  141. * Send a data block via Ethernet
  142. */
  143. static int
  144. ftmac100_send (struct eth_device *dev, volatile void *packet, int length)
  145. {
  146. struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
  147. struct ftmac100_data *priv = dev->priv;
  148. int tmo;
  149. struct ftmac100_txdes *curr_des = priv->txdes;
  150. if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
  151. debug ("%s(): no TX descriptor available\n", __func__);
  152. return -1;
  153. }
  154. debug ("%s(%x, %x)\n", __func__, (int)packet, length);
  155. length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
  156. /* initiate a transmit sequence */
  157. curr_des->txdes2 = (unsigned int)packet; /* TXBUF_BADR */
  158. curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
  159. curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
  160. FTMAC100_TXDES1_LTS |
  161. FTMAC100_TXDES1_TXBUF_SIZE (length);
  162. curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
  163. /* start transmit */
  164. writel (1, &ftmac100->txpd);
  165. /* wait for transfer to succeed */
  166. tmo = get_timer (0) + 5 * CONFIG_SYS_HZ;
  167. while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
  168. if (get_timer (0) >= tmo) {
  169. debug ("%s(): timed out\n", __func__);
  170. return -1;
  171. }
  172. }
  173. debug ("%s(): packet sent\n", __func__);
  174. return 0;
  175. }
  176. int ftmac100_initialize (bd_t *bd)
  177. {
  178. struct eth_device *dev;
  179. struct ftmac100_data *priv;
  180. dev = malloc (sizeof *dev);
  181. if (!dev) {
  182. printf ("%s(): failed to allocate dev\n", __func__);
  183. goto out;
  184. }
  185. /* Transmit and receive descriptors should align to 16 bytes */
  186. priv = memalign (16, sizeof (struct ftmac100_data));
  187. if (!priv) {
  188. printf ("%s(): failed to allocate priv\n", __func__);
  189. goto free_dev;
  190. }
  191. memset (dev, 0, sizeof (*dev));
  192. memset (priv, 0, sizeof (*priv));
  193. sprintf (dev->name, "FTMAC100");
  194. dev->iobase = CONFIG_FTMAC100_BASE;
  195. dev->init = ftmac100_init;
  196. dev->halt = ftmac100_halt;
  197. dev->send = ftmac100_send;
  198. dev->recv = ftmac100_recv;
  199. dev->priv = priv;
  200. eth_register (dev);
  201. return 1;
  202. free_dev:
  203. free (dev);
  204. out:
  205. return 0;
  206. }