macb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * $Log: macb.c,v $
  36. *
  37. */
  38. #include <cfg/os.h>
  39. #include <cfg/dev.h>
  40. #include <cfg/arch/gpio.h>
  41. #include <arch/avr32.h>
  42. #include <arch/avr32/gpio.h>
  43. #include <string.h>
  44. #include <sys/atom.h>
  45. #include <sys/heap.h>
  46. #include <sys/thread.h>
  47. #include <sys/event.h>
  48. #include <sys/timer.h>
  49. #include <sys/confnet.h>
  50. #include <netinet/if_ether.h>
  51. #include <net/ether.h>
  52. #include <net/if_var.h>
  53. #include <dev/irqreg.h>
  54. #include <dev/gpio.h>
  55. #include <avr32/io.h>
  56. #ifdef NUTDEBUG
  57. #include <stdio.h>
  58. #endif
  59. #ifndef NUT_THREAD_NICRXSTACK
  60. #define NUT_THREAD_NICRXSTACK 768
  61. #endif
  62. #ifndef EMAC_RX_BUFFERS
  63. #define EMAC_RX_BUFFERS 32
  64. #endif
  65. #define EMAC_RX_BUFSIZ 128
  66. #define EMAC_TX_BUFFERS 2
  67. #ifndef EMAC_TX_BUFSIZ
  68. #define EMAC_TX_BUFSIZ 1536
  69. #endif
  70. #ifndef EMAC_LINK_LOOPS
  71. #define EMAC_LINK_LOOPS 1000000
  72. #endif
  73. /*!
  74. * \addtogroup xgDm9161aRegs
  75. */
  76. /*@{*/
  77. #define NIC_PHY_BMCR 0x00 /*!< \brief Basic mode control register. */
  78. #define NIC_PHY_BMCR_COLTEST 0x0080 /*!< \brief Collision test. */
  79. #define NIC_PHY_BMCR_FDUPLEX 0x0100 /*!< \brief Full duplex mode. */
  80. #define NIC_PHY_BMCR_ANEGSTART 0x0200 /*!< \brief Restart auto negotiation. */
  81. #define NIC_PHY_BMCR_ISOLATE 0x0400 /*!< \brief Isolate from MII. */
  82. #define NIC_PHY_BMCR_PWRDN 0x0800 /*!< \brief Power-down. */
  83. #define NIC_PHY_BMCR_ANEGENA 0x1000 /*!< \brief Enable auto negotiation. */
  84. #define NIC_PHY_BMCR_100MBPS 0x2000 /*!< \brief Select 100 Mbps. */
  85. #define NIC_PHY_BMCR_LOOPBACK 0x4000 /*!< \brief Enable loopback mode. */
  86. #define NIC_PHY_BMCR_RESET 0x8000 /*!< \brief Software reset. */
  87. #define NIC_PHY_BMSR 0x01 /*!< \brief Basic mode status register. */
  88. #define NIC_PHY_BMSR_ANCOMPL 0x0020 /*!< \brief Auto negotiation complete. */
  89. #define NIC_PHY_BMSR_LINKSTAT 0x0004 /*!< \brief Link status. */
  90. #define NIC_PHY_ID1 0x02 /*!< \brief PHY identifier register 1. */
  91. #define NIC_PHY_ID2 0x03 /*!< \brief PHY identifier register 2. */
  92. #define NIC_PHY_ANAR 0x04 /*!< \brief Auto negotiation advertisement register. */
  93. #define NIC_PHY_ANLPAR 0x05 /*!< \brief Auto negotiation link partner availability register. */
  94. #define NIC_PHY_ANEG_NP 0x8000 /*!< \brief Next page available. */
  95. #define NIC_PHY_ANEG_ACK 0x4000 /*!< \brief Ability data reception acknowledged. */
  96. #define NIC_PHY_ANEG_RF 0x2000 /*!< \brief Remote fault. */
  97. #define NIC_PHY_ANEG_FCS 0x0400 /*!< \brief Flow control supported. */
  98. #define NIC_PHY_ANEG_T4 0x0200 /*!< \brief 100BASE-T4 supported. */
  99. #define NIC_PHY_ANEG_TX_FDX 0x0100 /*!< \brief 100BASE-T full duplex supported. */
  100. #define NIC_PHY_ANEG_TX_HDX 0x0080 /*!< \brief 100BASE-T half duplex supported. */
  101. #define NIC_PHY_ANEG_10_FDX 0x0040 /*!< \brief 10BASE-T full duplex supported. */
  102. #define NIC_PHY_ANEG_10_HDX 0x0020 /*!< \brief 10BASE-T half duplex supported. */
  103. #define NIC_PHY_ANEG_BINSEL 0x001F /*!< \brief Binary encoded protocol selector. */
  104. #define NIC_PHY_ANER 0x06 /*!< \brief Auto negotiation expansion register. */
  105. /*!
  106. * \name Advertisement control register.
  107. */
  108. #define NIC_PHY_ADVERTISE_SLCT 0x001f //!< Selector bits
  109. #define NIC_PHY_ADVERTISE_CSMA 0x0001 //!< Only selector supported
  110. #define NIC_PHY_ADVERTISE_10HALF 0x0020 //!< Try for 10mbps half-duplex
  111. #define NIC_PHY_ADVERTISE_1000XFULL 0x0020 //!< Try for 1000BASE-X full-duplex
  112. #define NIC_PHY_ADVERTISE_10FULL 0x0040 //!< Try for 10mbps full-duplex
  113. #define NIC_PHY_ADVERTISE_1000XHALF 0x0040 //!< Try for 1000BASE-X half-duplex
  114. #define NIC_PHY_ADVERTISE_100HALF 0x0080 //!< Try for 100mbps half-duplex
  115. #define NIC_PHY_ADVERTISE_1000XPAUSE 0x0080 //!< Try for 1000BASE-X pause
  116. #define NIC_PHY_ADVERTISE_100FULL 0x0100 //!< Try for 100mbps full-duplex
  117. #define NIC_PHY_ADVERTISE_1000XPSE_ASYM 0x0100 //!< Try for 1000BASE-X asym pause
  118. #define NIC_PHY_ADVERTISE_100BASE4 0x0200 //!< Try for 100mbps 4k packets
  119. #define NIC_PHY_ADVERTISE_PAUSE_CAP 0x0400 //!< Try for pause
  120. #define NIC_PHY_ADVERTISE_PAUSE_ASYM 0x0800 //!< Try for asymetric pause
  121. #define NIC_PHY_ADVERTISE_RESV 0x1000 //!< Unused...
  122. #define NIC_PHY_ADVERTISE_RFAULT 0x2000 //!< Say we can detect faults
  123. #define NIC_PHY_ADVERTISE_LPACK 0x4000 //!< Ack link partners response
  124. #define NIC_PHY_ADVERTISE_NPAGE 0x8000 //!< Next page bit
  125. #define NIC_PHY_ADVERTISE_FULL (NIC_PHY_ADVERTISE_100FULL | NIC_PHY_ADVERTISE_10FULL | NIC_PHY_ADVERTISE_CSMA)
  126. #define NIC_PHY_ADVERTISE_ALL (NIC_PHY_ADVERTISE_10HALF | NIC_PHY_ADVERTISE_10FULL | \
  127. NIC_PHY_ADVERTISE_100HALF | NIC_PHY_ADVERTISE_100FULL)
  128. /*@}*/
  129. /*!
  130. * \brief PHY address.
  131. *
  132. * Any other than 0 seems to create problems with Atmel's evaluation kits.
  133. */
  134. #ifndef NIC_PHY_ADDR
  135. #define NIC_PHY_ADDR 0
  136. #endif
  137. #ifndef NIC_PHY_UID
  138. #define NIC_PHY_UID 0xffffffff
  139. #endif
  140. /*!
  141. * The EVK1100 board is delivered with RMII by default. Thus.
  142. * we use the reduced MII for this CPU. However, this should be
  143. * handled by the Configurator.
  144. */
  145. #define PHY_MODE_RMII
  146. /*!
  147. * \brief Network interface controller information structure.
  148. */
  149. struct _EMACINFO {
  150. #ifdef NUT_PERFMON
  151. uint32_t ni_rx_packets; /*!< Number of packets received. */
  152. uint32_t ni_tx_packets; /*!< Number of packets sent. */
  153. uint32_t ni_overruns; /*!< Number of packet overruns. */
  154. uint32_t ni_rx_frame_errors; /*!< Number of frame errors. */
  155. uint32_t ni_rx_crc_errors; /*!< Number of CRC errors. */
  156. uint32_t ni_rx_missed_errors; /*!< Number of missed packets. */
  157. #endif
  158. HANDLE volatile ni_rx_rdy; /*!< Receiver event queue. */
  159. HANDLE volatile ni_tx_rdy; /*!< Transmitter event queue. */
  160. HANDLE ni_mutex; /*!< Access mutex semaphore. */
  161. volatile int ni_tx_queued; /*!< Number of packets in transmission queue. */
  162. volatile int ni_tx_quelen; /*!< Number of bytes in transmission queue not sent. */
  163. volatile int ni_insane; /*!< Set by error detection. */
  164. int ni_iomode; /*!< 8 or 16 bit access. 32 bit is not supported. */
  165. };
  166. /*!
  167. * \brief Network interface controller information type.
  168. */
  169. typedef struct _EMACINFO EMACINFO;
  170. /*
  171. * TODO: Buffers and their descriptors should be part of the EMACINFO
  172. * structure. Actually there will be no dual Ethernet chip (sure?),
  173. * but just to keep the code clean.
  174. */
  175. /*! Receive Transfer descriptor structure.
  176. */
  177. typedef struct _RxTdDescriptor {
  178. uint32_t addr;
  179. uint32_t status;
  180. } RxTdDescriptor;
  181. //! @}
  182. /*! Transmit Transfer descriptor structure.
  183. */
  184. //! @{
  185. typedef struct _TxTdDescriptor {
  186. uint32_t addr;
  187. uint32_t status;
  188. } TxTdDescriptor;
  189. //! @}
  190. static volatile TxTdDescriptor txBufTab[EMAC_TX_BUFFERS];
  191. static volatile uint8_t txBuf[EMAC_TX_BUFFERS * EMAC_TX_BUFSIZ] NUT_ALIGNED_TYPE(4);
  192. static unsigned int txBufIdx;
  193. static volatile RxTdDescriptor rxBufTab[EMAC_RX_BUFFERS];
  194. static volatile uint8_t rxBuf[EMAC_RX_BUFFERS * EMAC_RX_BUFSIZ] NUT_ALIGNED_TYPE(4);
  195. static unsigned int rxBufIdx;
  196. #define RXBUF_OWNERSHIP 0x00000001
  197. #define RXBUF_WRAP 0x00000002
  198. #define RXBUF_ADDRMASK 0xFFFFFFFC
  199. #define RXS_BROADCAST_ADDR 0x80000000 /*!< \brief Broadcast address detected. */
  200. #define RXS_MULTICAST_HASH 0x40000000 /*!< \brief Multicast hash match. */
  201. #define RXS_UNICAST_HASH 0x20000000 /*!< \brief Unicast hash match. */
  202. #define RXS_EXTERNAL_ADDR 0x10000000 /*!< \brief External address match. */
  203. #define RXS_SA1_ADDR 0x04000000 /*!< \brief Specific address register 1 match. */
  204. #define RXS_SA2_ADDR 0x02000000 /*!< \brief Specific address register 2 match. */
  205. #define RXS_SA3_ADDR 0x01000000 /*!< \brief Specific address register 3 match. */
  206. #define RXS_SA4_ADDR 0x00800000 /*!< \brief Specific address register 4 match. */
  207. #define RXS_TYPE_ID 0x00400000 /*!< \brief Type ID match. */
  208. #define RXS_VLAN_TAG 0x00200000 /*!< \brief VLAN tag detected. */
  209. #define RXS_PRIORITY_TAG 0x00100000 /*!< \brief Priority tag detected. */
  210. #define RXS_VLAN_PRIORITY 0x000E0000 /*!< \brief VLAN priority. */
  211. #define RXS_CFI_IND 0x00010000 /*!< \brief Concatenation format indicator. */
  212. #define RXS_EOF 0x00008000 /*!< \brief End of frame. */
  213. #define RXS_SOF 0x00004000 /*!< \brief Start of frame. */
  214. #define RXS_RBF_OFFSET 0x00003000 /*!< \brief Receive buffer offset mask. */
  215. #define RXS_LENGTH_FRAME 0x000007FF /*!< \brief Length of frame including FCS. */
  216. #define TXS_USED 0x80000000 /*!< \brief Used buffer. */
  217. #define TXS_WRAP 0x40000000 /*!< \brief Last descriptor. */
  218. #define TXS_ERROR 0x20000000 /*!< \brief Retry limit exceeded. */
  219. #define TXS_UNDERRUN 0x10000000 /*!< \brief Transmit underrun. */
  220. #define TXS_NO_BUFFER 0x08000000 /*!< \brief Buffer exhausted. */
  221. #define TXS_NO_CRC 0x00010000 /*!< \brief CRC not appended. */
  222. #define TXS_LAST_BUFF 0x00008000 /*!< \brief Last buffer of frame. */
  223. /*!
  224. * \addtogroup xgNutArchArmAt91Emac
  225. */
  226. /*@{*/
  227. /*!
  228. * \brief Read contents of PHY register.
  229. *
  230. * \param reg PHY register number.
  231. *
  232. * \return Contents of the specified register.
  233. */
  234. static uint16_t phy_inw(volatile avr32_macb_t * macb, uint8_t reg)
  235. {
  236. uint16_t value;
  237. // initiate transaction: enable management port
  238. macb->ncr |= AVR32_MACB_NCR_MPE_MASK;
  239. // Write the PHY configuration frame to the MAN register
  240. macb->man = (AVR32_MACB_SOF_MASK & (0x01 << AVR32_MACB_SOF_OFFSET)) // SOF
  241. | (2 << AVR32_MACB_CODE_OFFSET) // Code
  242. | (2 << AVR32_MACB_RW_OFFSET) // Read operation
  243. | ((NIC_PHY_ADDR & 0x1f) << AVR32_MACB_PHYA_OFFSET) // Phy Add
  244. | (reg << AVR32_MACB_REGA_OFFSET); // Reg Add
  245. // wait for PHY to be ready
  246. while (!(macb->nsr & AVR32_MACB_NSR_IDLE_MASK));
  247. // read the register value in maintenance register
  248. value = macb->man & 0x0000ffff;
  249. // disable management port
  250. macb->ncr &= ~AVR32_MACB_NCR_MPE_MASK;
  251. return value;
  252. }
  253. /*!
  254. * \brief Write value to PHY register.
  255. *
  256. * \param reg PHY register number.
  257. * \param val Value to write.
  258. */
  259. static void phy_outw(volatile avr32_macb_t * macb, uint8_t reg, uint16_t val)
  260. {
  261. // initiate transaction : enable management port
  262. macb->ncr |= AVR32_MACB_NCR_MPE_MASK;
  263. // Write the PHY configuration frame to the MAN register
  264. macb->man = ((AVR32_MACB_SOF_MASK & (0x01 << AVR32_MACB_SOF_OFFSET)) // SOF
  265. | (2 << AVR32_MACB_CODE_OFFSET) // Code
  266. | (1 << AVR32_MACB_RW_OFFSET) // Write operation
  267. | ((NIC_PHY_ADDR & 0x1f) << AVR32_MACB_PHYA_OFFSET) // Phy Add
  268. | (reg << AVR32_MACB_REGA_OFFSET)) // Reg Add
  269. | (val & 0xffff); // Data
  270. // wait for PHY to be ready
  271. while (!(macb->nsr & AVR32_MACB_NSR_IDLE_MASK));
  272. // disable management port
  273. macb->ncr &= ~AVR32_MACB_NCR_MPE_MASK;
  274. }
  275. /*!
  276. * \brief Probe PHY.
  277. *
  278. * \return 0 on success, -1 otherwise.
  279. */
  280. static int probePhy(volatile avr32_macb_t * macb)
  281. {
  282. uint32_t physID;
  283. uint16_t phyval;
  284. // Read Phy ID. Ignore revision number.
  285. physID = (phy_inw(macb, NIC_PHY_ID2) & 0xFFF0) | ((phy_inw(macb, NIC_PHY_ID1) << 16) & 0xFFFF0000);
  286. #if NIC_PHY_UID != 0xffffffff
  287. if (physID != (NIC_PHY_UID & 0xFFFFFFF0)) {
  288. return -1;
  289. }
  290. #endif
  291. phyval = NIC_PHY_ADVERTISE_CSMA | NIC_PHY_ADVERTISE_ALL;
  292. phy_outw(macb, NIC_PHY_ANAR, phyval);
  293. phyval = phy_inw(macb, NIC_PHY_BMCR);
  294. phyval |= (NIC_PHY_BMCR_ANEGSTART | NIC_PHY_BMCR_ANEGENA);
  295. phy_outw(macb, NIC_PHY_BMCR, phyval);
  296. /* Handle auto negotiation if configured. */
  297. phyval = phy_inw(macb, NIC_PHY_BMCR);
  298. if (phyval & NIC_PHY_BMCR_ANEGENA) {
  299. int loops = EMAC_LINK_LOOPS;
  300. /* Wait for auto negotiation completed. */
  301. phy_inw(macb, NIC_PHY_BMSR); /* Discard previously latched status. */
  302. while (--loops) {
  303. if (phy_inw(macb, NIC_PHY_BMSR) & NIC_PHY_BMSR_ANCOMPL) {
  304. break;
  305. }
  306. }
  307. /* Return error on link timeout. */
  308. if (loops == 0) {
  309. macb->ncr &= ~AVR32_MACB_NCR_MPE_MASK;
  310. return -1;
  311. }
  312. /*
  313. * Read link partner abilities and configure EMAC.
  314. */
  315. phyval = phy_inw(macb, NIC_PHY_ANLPAR);
  316. if (phyval & NIC_PHY_ANEG_TX_FDX) {
  317. /* 100Mb full duplex. */
  318. macb->ncfgr |= AVR32_MACB_SPD_MASK | AVR32_MACB_FD_MASK;
  319. } else if (phyval & NIC_PHY_ANEG_TX_HDX) {
  320. /* 100Mb half duplex. */
  321. macb->ncfgr = (macb->ncfgr & ~AVR32_MACB_FD_MASK) | AVR32_MACB_SPD_MASK;
  322. } else if (phyval & NIC_PHY_ANEG_10_FDX) {
  323. /* 10Mb full duplex. */
  324. macb->ncfgr = (macb->ncfgr & ~AVR32_MACB_SPD_MASK) | AVR32_MACB_FD_MASK;
  325. } else {
  326. /* 10Mb half duplex. */
  327. macb->ncfgr &= ~(AVR32_MACB_SPD_MASK | AVR32_MACB_FD_MASK);
  328. }
  329. }
  330. return 0;
  331. }
  332. /*!
  333. * \brief Reset the Ethernet controller.
  334. *
  335. * \return 0 on success, -1 otherwise.
  336. */
  337. static int EmacReset(NUTDEVICE * dev)
  338. {
  339. volatile avr32_macb_t *macb = (avr32_macb_t *) dev->dev_base;
  340. const uint32_t hclk_hz = NutArchClockGet(NUT_HWCLK_PERIPHERAL_B);
  341. /* Disable TX and RX */
  342. macb->ncr = 0;
  343. /* Clear status registers */
  344. macb->NCR.clrstat = 1;
  345. /* Clear all status flags */
  346. macb->tsr = ~0UL;
  347. macb->rsr = ~0UL;
  348. /* Disable all interrupts */
  349. NutEnterCritical();
  350. macb->idr = ~0UL;
  351. macb->isr;
  352. NutExitCritical();
  353. #if defined(PHY_MODE_RMII)
  354. macb->usrio &= ~AVR32_MACB_RMII_MASK;
  355. #else
  356. macb->usrio |= AVR32_MACB_RMII_MASK;
  357. #endif
  358. /* Set MII management clock divider */
  359. if (hclk_hz <= 20000000)
  360. macb->ncfgr |= (AVR32_MACB_NCFGR_CLK_DIV8 << AVR32_MACB_NCFGR_CLK_OFFSET);
  361. else if (hclk_hz <= 40000000)
  362. macb->ncfgr |= (AVR32_MACB_NCFGR_CLK_DIV16 << AVR32_MACB_NCFGR_CLK_OFFSET);
  363. else if (hclk_hz <= 80000000)
  364. macb->ncfgr |= (AVR32_MACB_NCFGR_CLK_DIV32 << AVR32_MACB_NCFGR_CLK_OFFSET);
  365. else
  366. macb->ncfgr |= (AVR32_MACB_NCFGR_CLK_DIV64 << AVR32_MACB_NCFGR_CLK_OFFSET);
  367. /* Wait for PHY ready. */
  368. NutDelay(255);
  369. return probePhy(macb);
  370. }
  371. /*
  372. * NIC interrupt entry.
  373. */
  374. static void EmacInterrupt(void *arg)
  375. {
  376. unsigned int isr;
  377. unsigned int event;
  378. NUTDEVICE *dev = (NUTDEVICE *) arg;
  379. volatile avr32_macb_t *macb = (avr32_macb_t *) dev->dev_base;
  380. EMACINFO *ni = dev->dev_dcb;
  381. /* Read interrupt status and disable interrupts. */
  382. isr = macb->isr;
  383. event = macb->rsr;
  384. /* Receiver interrupt. */
  385. if ((isr & AVR32_MACB_IMR_RCOMP_MASK) || (event & AVR32_MACB_REC_MASK)) {
  386. macb->rsr = AVR32_MACB_REC_MASK; // Clear
  387. macb->rsr; // Read to force the previous write
  388. macb->idr = AVR32_MACB_IDR_RCOMP_MASK | AVR32_MACB_IDR_ROVR_MASK | AVR32_MACB_IDR_RXUBR_MASK;
  389. NutEventPostFromIrq(&ni->ni_rx_rdy);
  390. }
  391. /* Transmitter interrupt. */
  392. if (isr & AVR32_MACB_IMR_TCOMP_MASK) {
  393. macb->tsr = AVR32_MACB_TSR_COMP_MASK; // Clear
  394. macb->tsr; // Read to force the previous write
  395. NutEventPostFromIrq(&ni->ni_tx_rdy);
  396. }
  397. }
  398. /*!
  399. * \brief Fetch the next packet out of the receive buffers.
  400. *
  401. * \return 0 on success, -1 otherwise.
  402. */
  403. static int EmacGetPacket(EMACINFO * ni, NETBUF ** nbp)
  404. {
  405. int rc = -1;
  406. unsigned int fbc = 0;
  407. unsigned int i;
  408. *nbp = NULL;
  409. /*
  410. * Search the next frame start. Release any fragment.
  411. */
  412. while ((rxBufTab[rxBufIdx].addr & RXBUF_OWNERSHIP) != 0 && (rxBufTab[rxBufIdx].status & RXS_SOF) == 0) {
  413. rxBufTab[rxBufIdx].addr &= ~(RXBUF_OWNERSHIP);
  414. rxBufIdx++;
  415. if (rxBufIdx >= EMAC_RX_BUFFERS) {
  416. rxBufIdx = 0;
  417. }
  418. }
  419. /*
  420. * Determine the size of the next frame.
  421. */
  422. i = rxBufIdx;
  423. while (rxBufTab[i].addr & RXBUF_OWNERSHIP) {
  424. if (i != rxBufIdx && (rxBufTab[i].status & RXS_SOF) != 0) {
  425. do {
  426. rxBufTab[rxBufIdx].addr &= ~(RXBUF_OWNERSHIP);
  427. rxBufIdx++;
  428. if (rxBufIdx >= EMAC_RX_BUFFERS) {
  429. rxBufIdx = 0;
  430. }
  431. } while ((rxBufTab[rxBufIdx].addr & RXBUF_OWNERSHIP) != 0 && (rxBufTab[rxBufIdx].status & RXS_SOF) == 0);
  432. break;
  433. }
  434. if ((fbc = rxBufTab[i].status & RXS_LENGTH_FRAME) != 0) {
  435. break;
  436. }
  437. i++;
  438. if (i >= EMAC_RX_BUFFERS) {
  439. i = 0;
  440. }
  441. }
  442. if (fbc) {
  443. /*
  444. * Receiving long packets is unexpected. Let's declare the
  445. * chip insane. Short packets will be handled by the caller.
  446. */
  447. // if (fbc > 1536) {
  448. // ni->ni_insane = 1;
  449. // } else
  450. {
  451. *nbp = NutNetBufAlloc(0, NBAF_DATALINK, (uint16_t) fbc);
  452. if (*nbp != NULL) {
  453. uint8_t *bp = (uint8_t *) (*nbp)->nb_dl.vp;
  454. unsigned int len;
  455. while (fbc) {
  456. if (fbc > EMAC_RX_BUFSIZ) {
  457. len = EMAC_RX_BUFSIZ;
  458. } else {
  459. len = fbc;
  460. }
  461. memcpy(bp, (void *) (rxBufTab[rxBufIdx].addr & RXBUF_ADDRMASK), len);
  462. rxBufTab[rxBufIdx].addr &= ~RXBUF_OWNERSHIP;
  463. rxBufIdx++;
  464. if (rxBufIdx >= EMAC_RX_BUFFERS) {
  465. rxBufIdx = 0;
  466. }
  467. fbc -= len;
  468. bp += len;
  469. }
  470. rc = 0;
  471. }
  472. }
  473. }
  474. return rc;
  475. }
  476. /*!
  477. * \brief Load a packet into the nic's transmit ring buffer.
  478. *
  479. * \todo This routine simply does not work. Any idea?
  480. *
  481. * \param nb Network buffer structure containing the packet to be sent.
  482. * The structure must have been allocated by a previous
  483. * call NutNetBufAlloc(). This routine will automatically
  484. * release the buffer in case of an error.
  485. *
  486. * \return 0 on success, -1 in case of any errors. Errors
  487. * will automatically release the network buffer
  488. * structure.
  489. */
  490. static int EmacPutPacket(int bufnum, NUTDEVICE * dev, NETBUF * nb)
  491. {
  492. volatile avr32_macb_t *macb = (avr32_macb_t *) dev->dev_base;
  493. int rc = -1;
  494. unsigned int sz;
  495. uint8_t *buf;
  496. EMACINFO *ni = dev->dev_dcb;
  497. /*
  498. * Calculate the number of bytes to be send. Do not send packets
  499. * larger than the Ethernet maximum transfer unit. The MTU
  500. * consist of 1500 data bytes plus the 14 byte Ethernet header
  501. * plus 4 bytes CRC. We check the data bytes only.
  502. */
  503. if ((sz = nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz) > ETHERMTU) {
  504. return -1;
  505. }
  506. sz += nb->nb_dl.sz;
  507. if (sz & 1) {
  508. sz++;
  509. }
  510. /* Disable EMAC interrupts. */
  511. NutIrqDisable(&sig_MACB);
  512. /* TODO: Check for link. */
  513. if (ni->ni_insane == 0) {
  514. buf = (uint8_t *) txBufTab[bufnum].addr;
  515. memcpy(buf, nb->nb_dl.vp, nb->nb_dl.sz);
  516. buf += nb->nb_dl.sz;
  517. memcpy(buf, nb->nb_nw.vp, nb->nb_nw.sz);
  518. buf += nb->nb_nw.sz;
  519. memcpy(buf, nb->nb_tp.vp, nb->nb_tp.sz);
  520. buf += nb->nb_tp.sz;
  521. memcpy(buf, nb->nb_ap.vp, nb->nb_ap.sz);
  522. sz |= TXS_LAST_BUFF;
  523. if (bufnum) {
  524. sz |= TXS_WRAP;
  525. }
  526. txBufTab[bufnum].status = sz;
  527. macb->ncr |= AVR32_MACB_TSTART_MASK;
  528. rc = 0;
  529. #ifdef NUT_PERFMON
  530. ni->ni_tx_packets++;
  531. #endif
  532. }
  533. /* Enable EMAC interrupts. */
  534. NutIrqEnable(&sig_MACB);
  535. return rc;
  536. }
  537. /*!
  538. * \brief Fire up the network interface.
  539. *
  540. * NIC interrupts must be disabled when calling this function.
  541. *
  542. * \param mac Six byte unique MAC address.
  543. */
  544. static int EmacStart(volatile avr32_macb_t * macb, const uint8_t * mac)
  545. {
  546. unsigned int i;
  547. /* Set local MAC address. */
  548. // Must be written SA1L then SA1H.
  549. macb->sa1b = (mac[3] << 24) | (mac[2] << 16) | (mac[1] << 8) | mac[0];
  550. macb->sa1t = (mac[5] << 8) | mac[4];
  551. /* Initialize receive buffer descriptors. */
  552. for (i = 0; i < EMAC_RX_BUFFERS - 1; i++) {
  553. rxBufTab[i].addr = (unsigned int) (&rxBuf[i * EMAC_RX_BUFSIZ]) & RXBUF_ADDRMASK;
  554. }
  555. rxBufTab[i].addr = ((unsigned int) (&rxBuf[i * EMAC_RX_BUFSIZ]) & RXBUF_ADDRMASK) | RXBUF_WRAP;
  556. macb->rbqp = (unsigned long) rxBufTab;
  557. /* Initialize transmit buffer descriptors. */
  558. for (i = 0; i < EMAC_TX_BUFFERS - 1; i++) {
  559. txBufTab[i].addr = (unsigned int) (&txBuf[i * EMAC_RX_BUFSIZ]);
  560. txBufTab[i].status = TXS_USED;
  561. }
  562. txBufTab[i].addr = (unsigned int) (&txBuf[i * EMAC_RX_BUFSIZ]);
  563. txBufTab[i].status = TXS_USED | TXS_WRAP;
  564. macb->tbqp = (unsigned long) txBufTab;
  565. /* Clear receiver status. */
  566. macb->rsr = AVR32_MACB_RSR_BNA_MASK | AVR32_MACB_RSR_OVR_MASK | AVR32_MACB_RSR_REC_MASK;
  567. macb->rsr;
  568. /* Discard FCS. */
  569. macb->ncfgr |= AVR32_MACB_NCFGR_DRFCS_MASK;
  570. /* Enable receiver, transmitter and statistics. */
  571. macb->ncr |= AVR32_MACB_NCR_RE_MASK | AVR32_MACB_NCR_TE_MASK;
  572. return 0;
  573. }
  574. /*! \fn EmacRxThread(void *arg)
  575. * \brief NIC receiver thread.
  576. *
  577. */
  578. THREAD(EmacRxThread, arg)
  579. {
  580. NUTDEVICE *dev = (NUTDEVICE *) arg;
  581. IFNET *ifn;
  582. EMACINFO *ni;
  583. NETBUF *nb;
  584. volatile avr32_macb_t *macb = (avr32_macb_t *) dev->dev_base;
  585. ifn = (IFNET *) dev->dev_icb;
  586. ni = (EMACINFO *) dev->dev_dcb;
  587. /*
  588. * This is a temporary hack. Due to a change in initialization,
  589. * we may not have got a MAC address yet. Wait until a valid one
  590. * has been set.
  591. */
  592. while (!ETHER_IS_UNICAST(ifn->if_mac)) {
  593. NutSleep(10);
  594. }
  595. /*
  596. * Do not continue unless we managed to start the NIC. We are
  597. * trapped here if the Ethernet link cannot be established.
  598. * This happens, for example, if no Ethernet cable is plugged
  599. * in.
  600. */
  601. while (EmacStart(macb, ifn->if_mac)) {
  602. EmacReset(dev);
  603. NutSleep(1000);
  604. }
  605. /* Initialize the access mutex. */
  606. NutEventPost(&ni->ni_mutex);
  607. /* Run at high priority. */
  608. NutThreadSetPriority(9);
  609. /* Enable receive and transmit interrupts. */
  610. macb->ier = AVR32_MACB_IER_ROVR_MASK | AVR32_MACB_IER_TCOMP_MASK | AVR32_MACB_IER_TUND_MASK |
  611. AVR32_MACB_IER_RXUBR_MASK | AVR32_MACB_IER_RCOMP_MASK;
  612. NutIrqEnable(&sig_MACB);
  613. for (;;) {
  614. /*
  615. * Wait for the arrival of new packets or poll the receiver every
  616. * 200 milliseconds. This short timeout helps a bit to deal with
  617. * the SAM9260 Ethernet problem.
  618. */
  619. NutEventWait(&ni->ni_rx_rdy, 200);
  620. /*
  621. * Fetch all packets from the NIC's internal buffer and pass
  622. * them to the registered handler.
  623. */
  624. while (EmacGetPacket(ni, &nb) == 0) {
  625. /* Discard short packets. */
  626. if (nb->nb_dl.sz < 60) {
  627. NutNetBufFree(nb);
  628. } else {
  629. (*ifn->if_recv) (dev, nb);
  630. }
  631. }
  632. macb->ier = AVR32_MACB_IER_ROVR_MASK | AVR32_MACB_IER_RXUBR_MASK | AVR32_MACB_IER_RCOMP_MASK;
  633. /* We got a weird chip, try to restart it. */
  634. while (ni->ni_insane) {
  635. EmacReset(dev);
  636. if (EmacStart(macb, ifn->if_mac) == 0) {
  637. ni->ni_insane = 0;
  638. ni->ni_tx_queued = 0;
  639. ni->ni_tx_quelen = 0;
  640. NutIrqEnable(&sig_MACB);
  641. } else {
  642. NutSleep(1000);
  643. }
  644. }
  645. }
  646. }
  647. /*!
  648. * \brief Send Ethernet packet.
  649. *
  650. * \param dev Identifies the device to use.
  651. * \param nb Network buffer structure containing the packet to be sent.
  652. * The structure must have been allocated by a previous
  653. * call NutNetBufAlloc().
  654. *
  655. * \return 0 on success, -1 in case of any errors.
  656. */
  657. int EmacOutput(NUTDEVICE * dev, NETBUF * nb)
  658. {
  659. volatile avr32_macb_t *macb = (avr32_macb_t *) dev->dev_base;
  660. static uint32_t mx_wait = 5000;
  661. int rc = -1;
  662. EMACINFO *ni = (EMACINFO *) dev->dev_dcb;
  663. /*
  664. * After initialization we are waiting for a long time to give
  665. * the PHY a chance to establish an Ethernet link.
  666. */
  667. while (rc) {
  668. if (ni->ni_insane) {
  669. break;
  670. }
  671. if (NutEventWait(&ni->ni_mutex, mx_wait)) {
  672. break;
  673. }
  674. /* Check for packet queue space. */
  675. if ((txBufTab[txBufIdx].status & TXS_USED) == 0) {
  676. if (NutEventWait(&ni->ni_tx_rdy, 500) && (txBufTab[txBufIdx].status & TXS_USED) == 0) {
  677. /* No queue space. Release the lock and give up. */
  678. txBufTab[txBufIdx].status |= TXS_USED;
  679. txBufIdx++;
  680. txBufIdx &= 1;
  681. NutEventPost(&ni->ni_mutex);
  682. break;
  683. }
  684. } else {
  685. if (macb->tsr & AVR32_MACB_TSR_UND_MASK) {
  686. txBufIdx = 0;
  687. macb->tsr = AVR32_MACB_TSR_UND_MASK;
  688. }
  689. if (macb->tsr & AVR32_MACB_TSR_COMP_MASK) {
  690. macb->tsr = AVR32_MACB_TSR_COMP_MASK;
  691. }
  692. if ((rc = EmacPutPacket(txBufIdx, dev, nb)) == 0) {
  693. txBufIdx++;
  694. txBufIdx &= 1;
  695. }
  696. }
  697. NutEventPost(&ni->ni_mutex);
  698. }
  699. /*
  700. * Probably no Ethernet link. Significantly reduce the waiting
  701. * time, so following transmission will soon return an error.
  702. */
  703. if (rc) {
  704. mx_wait = 500;
  705. } else {
  706. /* Ethernet works. Set a long waiting time in case we
  707. temporarily lose the link next time. */
  708. mx_wait = 5000;
  709. }
  710. return rc;
  711. }
  712. /*!
  713. * \brief Initialize Ethernet hardware.
  714. *
  715. * Applications should do not directly call this function. It is
  716. * automatically executed during during device registration by
  717. * NutRegisterDevice().
  718. *
  719. * \param dev Identifies the device to initialize.
  720. */
  721. int EmacInit(NUTDEVICE * dev)
  722. {
  723. EMACINFO *ni = (EMACINFO *) dev->dev_dcb;
  724. /* Reserve Pins with the GPIO Controller */
  725. #if !defined(PHY_MODE_RMII)
  726. gpio_enable_module_pin(AVR32_MACB_CRS_0_PIN, AVR32_MACB_CRS_0_FUNCTION);
  727. gpio_enable_module_pin(AVR32_MACB_COL_0_PIN, AVR32_MACB_COL_0_FUNCTION);
  728. gpio_enable_module_pin(AVR32_MACB_RX_CLK_0_PIN, AVR32_MACB_RX_CLK_0_FUNCTION);
  729. gpio_enable_module_pin(AVR32_MACB_TX_ER_0_PIN, AVR32_MACB_TX_ER_0_FUNCTION);
  730. #endif
  731. gpio_enable_module_pin(AVR32_MACB_MDC_0_PIN, AVR32_MACB_MDC_0_FUNCTION);
  732. gpio_enable_module_pin(AVR32_MACB_MDIO_0_PIN, AVR32_MACB_MDIO_0_FUNCTION);
  733. gpio_enable_module_pin(AVR32_MACB_RXD_0_PIN, AVR32_MACB_RXD_0_FUNCTION);
  734. gpio_enable_module_pin(AVR32_MACB_TXD_0_PIN, AVR32_MACB_TXD_0_FUNCTION);
  735. gpio_enable_module_pin(AVR32_MACB_RXD_1_PIN, AVR32_MACB_RXD_1_FUNCTION);
  736. gpio_enable_module_pin(AVR32_MACB_TXD_1_PIN, AVR32_MACB_TXD_1_FUNCTION);
  737. gpio_enable_module_pin(AVR32_MACB_TX_EN_0_PIN, AVR32_MACB_TX_EN_0_FUNCTION);
  738. gpio_enable_module_pin(AVR32_MACB_RX_ER_0_PIN, AVR32_MACB_RX_ER_0_FUNCTION);
  739. gpio_enable_module_pin(AVR32_MACB_RX_DV_0_PIN, AVR32_MACB_RX_DV_0_FUNCTION);
  740. gpio_enable_module_pin(AVR32_MACB_TX_CLK_0_PIN, AVR32_MACB_TX_CLK_0_FUNCTION);
  741. /* Reset the controller. */
  742. if (EmacReset(dev)) {
  743. return -1;
  744. }
  745. /* Clear EMACINFO structure. */
  746. memset(ni, 0, sizeof(EMACINFO));
  747. /* Register interrupt handler. */
  748. if (NutRegisterIrqHandler(&sig_MACB, EmacInterrupt, dev)) {
  749. return -1;
  750. }
  751. /* Start the receiver thread. */
  752. if (NutThreadCreate("emacrx", EmacRxThread, dev, NUT_THREAD_NICRXSTACK) == NULL) {
  753. return -1;
  754. }
  755. return 0;
  756. }
  757. static EMACINFO dcb_eth0;
  758. /*!
  759. * \brief Network interface information structure.
  760. *
  761. * Used to call.
  762. */
  763. static IFNET ifn_eth0 = {
  764. IFT_ETHER, /*!< \brief Interface type, if_type. */
  765. 0, /*!< \brief Interface flags, if_flags. */
  766. {0, 0, 0, 0, 0, 0}, /*!< \brief Hardware net address, if_mac. */
  767. 0, /*!< \brief IP address, if_local_ip. */
  768. 0, /*!< \brief Remote IP address for point to point, if_remote_ip. */
  769. 0, /*!< \brief IP network mask, if_mask. */
  770. ETHERMTU, /*!< \brief Maximum size of a transmission unit, if_mtu. */
  771. 0, /*!< \brief Packet identifier, if_pkt_id. */
  772. 0, /*!< \brief Linked list of arp entries, arpTable. */
  773. 0, /*!< \brief Linked list of multicast address entries, if_mcast. */
  774. NutEtherInput, /*!< \brief Routine to pass received data to, if_recv(). */
  775. EmacOutput, /*!< \brief Driver output routine, if_send(). */
  776. NutEtherOutput, /*!< \brief Media output routine, if_output(). */
  777. NULL /*!< \brief Interface specific control function, if_ioctl(). */
  778. #ifdef NUT_PERFMON
  779. , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  780. #endif
  781. };
  782. /*!
  783. * \brief Device information structure.
  784. *
  785. * A pointer to this structure must be passed to NutRegisterDevice()
  786. * to bind this Ethernet device driver to the Nut/OS kernel.
  787. * An application may then call NutNetIfConfig() with the name \em eth0
  788. * of this driver to initialize the network interface.
  789. *
  790. */
  791. NUTDEVICE devAvr32macb = {
  792. 0, /*!< \brief Pointer to next device. */
  793. {'e', 't', 'h', '0', 0, 0, 0, 0, 0}, /*!< \brief Unique device name. */
  794. IFTYP_NET, /*!< \brief Type of device. */
  795. AVR32_MACB_ADDRESS, /*!< \brief Base address. */
  796. 0, /*!< \brief First interrupt number. */
  797. &ifn_eth0, /*!< \brief Interface control block. */
  798. &dcb_eth0, /*!< \brief Driver control block. */
  799. EmacInit, /*!< \brief Driver initialization routine. */
  800. 0, /*!< \brief Driver specific control function. */
  801. 0, /*!< \brief Read from device. */
  802. 0, /*!< \brief Write to device. */
  803. #ifdef __HARVARD_ARCH__
  804. 0, /*!< \brief Write from program space data to device. */
  805. #endif
  806. 0, /*!< \brief Open a device or file. */
  807. 0, /*!< \brief Close a device or file. */
  808. 0, /*!< \brief Request file size. */
  809. 0, /*!< \brief Select function, optional, not yet implemented */
  810. };
  811. /*@}*/