enc28j60.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  1. /****************************************************************************
  2. * drivers/net/enc28j60.c
  3. *
  4. * Copyright (C) 2010-2012, 2014-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * References:
  8. * - ENC28J60 Data Sheet, Stand-Alone Ethernet Controller with SPI Interface,
  9. * DS39662C, 2008 Microchip Technology Inc.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. * 3. Neither the name NuttX nor the names of its contributors may be
  22. * used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #if defined(CONFIG_NET) && defined(CONFIG_ENC28J60)
  44. #include <stdint.h>
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include <time.h>
  48. #include <string.h>
  49. #include <debug.h>
  50. #include <errno.h>
  51. #include <arpa/inet.h>
  52. #include <nuttx/irq.h>
  53. #include <nuttx/arch.h>
  54. #include <nuttx/wdog.h>
  55. #include <nuttx/spi/spi.h>
  56. #include <nuttx/wqueue.h>
  57. #include <nuttx/clock.h>
  58. #include <nuttx/net/enc28j60.h>
  59. #include <nuttx/net/net.h>
  60. #include <nuttx/net/arp.h>
  61. #include <nuttx/net/netdev.h>
  62. #ifdef CONFIG_NET_PKT
  63. # include <nuttx/net/pkt.h>
  64. #endif
  65. #include "enc28j60.h"
  66. /****************************************************************************
  67. * Pre-processor Definitions
  68. ****************************************************************************/
  69. /* Configuration ************************************************************/
  70. /* ENC28J60 Configuration Settings:
  71. *
  72. * CONFIG_ENC28J60 - Enabled ENC28J60 support
  73. * CONFIG_ENC28J60_SPIMODE - Controls the SPI mode
  74. * CONFIG_ENC28J60_FREQUENCY - Define to use a different bus frequency
  75. * CONFIG_ENC28J60_NINTERFACES - Specifies the number of physical ENC28J60
  76. * devices that will be supported.
  77. * CONFIG_ENC28J60_HALFDUPPLEX - Default is full duplex
  78. */
  79. /* The ENC28J60 spec says that it supports SPI mode 0,0 only: "The
  80. * implementation used on this device supports SPI mode 0,0 only. In
  81. * addition, the SPI port requires that SCK be at Idle in a low state;
  82. * selectable clock polarity is not supported." However, sometimes you
  83. * need to tinker with these things.
  84. */
  85. #ifndef CONFIG_ENC28J60_SPIMODE
  86. # define CONFIG_ENC28J60_SPIMODE SPIDEV_MODE0
  87. #endif
  88. /* CONFIG_ENC28J60_NINTERFACES determines the number of physical interfaces
  89. * that will be supported.
  90. */
  91. #ifndef CONFIG_ENC28J60_NINTERFACES
  92. # define CONFIG_ENC28J60_NINTERFACES 1
  93. #endif
  94. /* CONFIG_NET_ETH_PKTSIZE must always be defined */
  95. #if !defined(CONFIG_NET_ETH_PKTSIZE) && (CONFIG_NET_ETH_PKTSIZE <= MAX_FRAMELEN)
  96. # error "CONFIG_NET_ETH_PKTSIZE is not valid for the ENC28J60"
  97. #endif
  98. /* We need to have the work queue to handle SPI interrupts */
  99. #if !defined(CONFIG_SCHED_WORKQUEUE)
  100. # error "Worker thread support is required (CONFIG_SCHED_WORKQUEUE)"
  101. #endif
  102. /* The low priority work queue is preferred. If it is not enabled, LPWORK
  103. * will be the same as HPWORK.
  104. *
  105. * NOTE: However, the network should NEVER run on the high priority work
  106. * queue! That queue is intended only to service short back end interrupt
  107. * processing that never suspends. Suspending the high priority work queue
  108. * may bring the system to its knees!
  109. */
  110. #define ENCWORK LPWORK
  111. /* CONFIG_ENC28J60_DUMPPACKET will dump the contents of each packet to the console. */
  112. #ifdef CONFIG_ENC28J60_DUMPPACKET
  113. # define enc_dumppacket(m,a,n) lib_dumpbuffer(m,a,n)
  114. #else
  115. # define enc_dumppacket(m,a,n)
  116. #endif
  117. /* Low-level register debug */
  118. #if !defined(CONFIG_DEBUG_FEATURES) || !defined(CONFIG_DEBUG_NET)
  119. # undef CONFIG_ENC28J60_REGDEBUG
  120. #endif
  121. /* Timing *******************************************************************/
  122. /* TX poll deley = 1 seconds. CLK_TCK is the number of clock ticks per second */
  123. #define ENC_WDDELAY (1*CLK_TCK)
  124. /* TX timeout = 1 minute */
  125. #define ENC_TXTIMEOUT (60*CLK_TCK)
  126. /* Poll timeout */
  127. #define ENC_POLLTIMEOUT MSEC2TICK(50)
  128. /* Packet Memory ************************************************************/
  129. /* Packet memory layout */
  130. #define ALIGNED_BUFSIZE ((CONFIG_NET_ETH_PKTSIZE + 255) & ~255)
  131. /* Work around Errata #5 (spurious reset of ERXWRPT to 0) by placing the RX
  132. * FIFO at the beginning of packet memory.
  133. */
  134. #define ERRATA5 1
  135. #if ERRATA5
  136. # define PKTMEM_RX_START 0x0000 /* RX buffer must be at addr 0 for errata 5 */
  137. # define PKTMEM_RX_END (PKTMEM_END-ALIGNED_BUFSIZE) /* RX buffer length is total SRAM minus TX buffer */
  138. # define PKTMEM_TX_START (PKTMEM_RX_END+1) /* Start TX buffer after */
  139. # define PKTMEM_TX_ENDP1 (PKTMEM_TX_START+ALIGNED_BUFSIZE) /* Allow TX buffer for one frame */
  140. #else
  141. # define PKTMEM_TX_START 0x0000 /* Start TX buffer at 0 */
  142. # define PKTMEM_TX_ENDP1 ALIGNED_BUFSIZE /* Allow TX buffer for one frame */
  143. # define PKTMEM_RX_START PKTMEM_TX_ENDP1 /* Followed by RX buffer */
  144. # define PKTMEM_RX_END PKTMEM_END /* RX buffer goes to the end of SRAM */
  145. #endif
  146. /* Misc. Helper Macros ******************************************************/
  147. #define enc_rdgreg(priv,ctrlreg) \
  148. enc_rdgreg2(priv, ENC_RCR | GETADDR(ctrlreg))
  149. #define enc_wrgreg(priv,ctrlreg,wrdata) \
  150. enc_wrgreg2(priv, ENC_WCR | GETADDR(ctrlreg), wrdata)
  151. #define enc_bfcgreg(priv,ctrlreg,clrbits) \
  152. enc_wrgreg2(priv, ENC_BFC | GETADDR(ctrlreg), clrbits)
  153. #define enc_bfsgreg(priv,ctrlreg,setbits) \
  154. enc_wrgreg2(priv, ENC_BFS | GETADDR(ctrlreg), setbits)
  155. /* This is a helper pointer for accessing the contents of the Ethernet header */
  156. #define BUF ((struct eth_hdr_s *)priv->dev.d_buf)
  157. /* Debug ********************************************************************/
  158. #ifdef CONFIG_ENC28J60_REGDEBUG
  159. # define enc_wrdump(a,v) \
  160. syslog(LOG_DEBUG, "ENC28J60: %02x<-%02x\n", a, v);
  161. # define enc_rddump(a,v) \
  162. syslog(LOG_DEBUG, "ENC28J60: %02x->%02x\n", a, v);
  163. # define enc_cmddump(c) \
  164. syslog(LOG_DEBUG, "ENC28J60: CMD: %02x\n", c);
  165. # define enc_bmdump(c,b,s) \
  166. syslog(LOG_DEBUG, "ENC28J60: CMD: %02x buffer: %p length: %d\n", c, b, s);
  167. #else
  168. # define enc_wrdump(a,v)
  169. # define enc_rddump(a,v)
  170. # define enc_cmddump(c)
  171. # define enc_bmdump(c,b,s)
  172. #endif
  173. /****************************************************************************
  174. * Private Types
  175. ****************************************************************************/
  176. /* The state of the interface */
  177. enum enc_state_e
  178. {
  179. ENCSTATE_UNINIT = 0, /* The interface is in an uninitialized state */
  180. ENCSTATE_DOWN, /* The interface is down */
  181. ENCSTATE_UP /* The interface is up */
  182. };
  183. /* The enc_driver_s encapsulates all state information for a single hardware
  184. * interface
  185. */
  186. struct enc_driver_s
  187. {
  188. /* Device control */
  189. uint8_t ifstate; /* Interface state: See ENCSTATE_* */
  190. uint8_t bank; /* Currently selected bank */
  191. uint16_t nextpkt; /* Next packet address */
  192. FAR const struct enc_lower_s *lower; /* Low-level MCU-specific support */
  193. /* Timing */
  194. WDOG_ID txpoll; /* TX poll timer */
  195. WDOG_ID txtimeout; /* TX timeout timer */
  196. /* If we don't own the SPI bus, then we cannot do SPI accesses from the
  197. * interrupt handler.
  198. */
  199. struct work_s irqwork; /* Interrupt continuation work queue support */
  200. struct work_s towork; /* Tx timeout work queue support */
  201. struct work_s pollwork; /* Poll timeout work queue support */
  202. /* This is the contained SPI driver intstance */
  203. FAR struct spi_dev_s *spi;
  204. /* This holds the information visible to the NuttX network */
  205. struct net_driver_s dev; /* Interface understood by the network */
  206. };
  207. /****************************************************************************
  208. * Private Data
  209. ****************************************************************************/
  210. /* A single packet buffer is used */
  211. static uint8_t g_pktbuf[MAX_NETDEV_PKTSIZE + CONFIG_NET_GUARDSIZE];
  212. /* Driver status structure */
  213. static struct enc_driver_s g_enc28j60[CONFIG_ENC28J60_NINTERFACES];
  214. /****************************************************************************
  215. * Private Function Prototypes
  216. ****************************************************************************/
  217. /* Low-level SPI helpers */
  218. static inline void enc_configspi(FAR struct spi_dev_s *spi);
  219. static void enc_lock(FAR struct enc_driver_s *priv);
  220. static inline void enc_unlock(FAR struct enc_driver_s *priv);
  221. /* SPI control register access */
  222. static uint8_t enc_rdgreg2(FAR struct enc_driver_s *priv, uint8_t cmd);
  223. static void enc_wrgreg2(FAR struct enc_driver_s *priv, uint8_t cmd,
  224. uint8_t wrdata);
  225. static inline void enc_src(FAR struct enc_driver_s *priv);
  226. static void enc_setbank(FAR struct enc_driver_s *priv, uint8_t bank);
  227. static uint8_t enc_rdbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg);
  228. static void enc_wrbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
  229. uint8_t wrdata);
  230. static int enc_waitbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
  231. uint8_t bits, uint8_t value);
  232. #if 0 /* Sometimes useful */
  233. static void enc_rxdump(FAR struct enc_driver_s *priv);
  234. static void enc_txdump(FAR struct enc_driver_s *priv);
  235. #endif
  236. /* SPI buffer transfers */
  237. static void enc_rdbuffer(FAR struct enc_driver_s *priv, FAR uint8_t *buffer,
  238. size_t buflen);
  239. static inline void enc_wrbuffer(FAR struct enc_driver_s *priv,
  240. FAR const uint8_t *buffer, size_t buflen);
  241. /* PHY register access */
  242. static uint16_t enc_rdphy(FAR struct enc_driver_s *priv, uint8_t phyaddr);
  243. static void enc_wrphy(FAR struct enc_driver_s *priv, uint8_t phyaddr,
  244. uint16_t phydata);
  245. /* Common TX logic */
  246. static int enc_transmit(FAR struct enc_driver_s *priv);
  247. static int enc_txpoll(struct net_driver_s *dev);
  248. /* Interrupt handling */
  249. static void enc_linkstatus(FAR struct enc_driver_s *priv);
  250. static void enc_txif(FAR struct enc_driver_s *priv);
  251. static void enc_txerif(FAR struct enc_driver_s *priv);
  252. static void enc_txerif(FAR struct enc_driver_s *priv);
  253. static void enc_rxerif(FAR struct enc_driver_s *priv);
  254. static void enc_rxdispatch(FAR struct enc_driver_s *priv);
  255. static void enc_pktif(FAR struct enc_driver_s *priv);
  256. static void enc_irqworker(FAR void *arg);
  257. static int enc_interrupt(int irq, FAR void *context, FAR void *arg);
  258. /* Watchdog timer expirations */
  259. static void enc_toworker(FAR void *arg);
  260. static void enc_txtimeout(int argc, uint32_t arg, ...);
  261. static void enc_pollworker(FAR void *arg);
  262. static void enc_polltimer(int argc, uint32_t arg, ...);
  263. /* NuttX callback functions */
  264. static int enc_ifup(struct net_driver_s *dev);
  265. static int enc_ifdown(struct net_driver_s *dev);
  266. static int enc_txavail(struct net_driver_s *dev);
  267. #ifdef CONFIG_NET_MCASTGROUP
  268. static int enc_addmac(struct net_driver_s *dev, FAR const uint8_t *mac);
  269. static int enc_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac);
  270. #endif
  271. /* Initialization */
  272. static void enc_pwrsave(FAR struct enc_driver_s *priv);
  273. static void enc_pwrfull(FAR struct enc_driver_s *priv);
  274. static void enc_setmacaddr(FAR struct enc_driver_s *priv);
  275. static int enc_reset(FAR struct enc_driver_s *priv);
  276. /****************************************************************************
  277. * Private Functions
  278. ****************************************************************************/
  279. /****************************************************************************
  280. * Name: enc_configspi
  281. *
  282. * Description:
  283. * Configure the SPI for use with the ENC28J60
  284. *
  285. * Input Parameters:
  286. * spi - Reference to the SPI driver structure
  287. *
  288. * Returned Value:
  289. * None
  290. *
  291. * Assumptions:
  292. *
  293. ****************************************************************************/
  294. static inline void enc_configspi(FAR struct spi_dev_s *spi)
  295. {
  296. /* Configure SPI for the ENC28J60. */
  297. SPI_SETMODE(spi, CONFIG_ENC28J60_SPIMODE);
  298. SPI_SETBITS(spi, 8);
  299. (void)SPI_HWFEATURES(spi, 0);
  300. (void)SPI_SETFREQUENCY(spi, CONFIG_ENC28J60_FREQUENCY);
  301. }
  302. /****************************************************************************
  303. * Name: enc_lock
  304. *
  305. * Description:
  306. * Select the SPI, locking and re-configuring if necessary
  307. *
  308. * Input Parameters:
  309. * spi - Reference to the SPI driver structure
  310. *
  311. * Returned Value:
  312. * None
  313. *
  314. * Assumptions:
  315. *
  316. ****************************************************************************/
  317. static void enc_lock(FAR struct enc_driver_s *priv)
  318. {
  319. /* Lock the SPI bus in case there are multiple devices competing for the SPI
  320. * bus.
  321. */
  322. SPI_LOCK(priv->spi, true);
  323. /* Now make sure that the SPI bus is configured for the ENC28J60 (it
  324. * might have gotten configured for a different device while unlocked)
  325. */
  326. SPI_SETMODE(priv->spi, CONFIG_ENC28J60_SPIMODE);
  327. SPI_SETBITS(priv->spi, 8);
  328. (void)SPI_HWFEATURES(priv->spi, 0);
  329. (void)SPI_SETFREQUENCY(priv->spi, CONFIG_ENC28J60_FREQUENCY);
  330. }
  331. /****************************************************************************
  332. * Name: enc_unlock
  333. *
  334. * Description:
  335. * De-select the SPI
  336. *
  337. * Input Parameters:
  338. * spi - Reference to the SPI driver structure
  339. *
  340. * Returned Value:
  341. * None
  342. *
  343. * Assumptions:
  344. *
  345. ****************************************************************************/
  346. static inline void enc_unlock(FAR struct enc_driver_s *priv)
  347. {
  348. /* Relinquish the lock on the bus. */
  349. SPI_LOCK(priv->spi, false);
  350. }
  351. /****************************************************************************
  352. * Name: enc_rdgreg2
  353. *
  354. * Description:
  355. * Read a global register (EIE, EIR, ESTAT, ECON2, or ECON1). The cmd
  356. * include the CMD 'OR'd with the global address register.
  357. *
  358. * Input Parameters:
  359. * priv - Reference to the driver state structure
  360. * cmd - The full command to received (cmd | address)
  361. *
  362. * Returned Value:
  363. * The value read from the register
  364. *
  365. * Assumptions:
  366. *
  367. ****************************************************************************/
  368. static uint8_t enc_rdgreg2(FAR struct enc_driver_s *priv, uint8_t cmd)
  369. {
  370. uint8_t rddata;
  371. DEBUGASSERT(priv && priv->spi);
  372. /* Select ENC28J60 chip */
  373. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
  374. /* Send the read command and collect the data. The sequence requires
  375. * 16-clocks: 8 to clock out the cmd + 8 to clock in the data.
  376. */
  377. (void)SPI_SEND(priv->spi, cmd); /* Clock out the command */
  378. rddata = SPI_SEND(priv->spi, 0); /* Clock in the data */
  379. /* De-select ENC28J60 chip */
  380. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
  381. enc_rddump(cmd, rddata);
  382. return rddata;
  383. }
  384. /****************************************************************************
  385. * Name: enc_wrgreg2
  386. *
  387. * Description:
  388. * Write to a global register (EIE, EIR, ESTAT, ECON2, or ECON1). The cmd
  389. * include the CMD 'OR'd with the global address register.
  390. *
  391. * Input Parameters:
  392. * priv - Reference to the driver state structure
  393. * cmd - The full command to received (cmd | address)
  394. * wrdata - The data to send
  395. *
  396. * Returned Value:
  397. * None
  398. *
  399. * Assumptions:
  400. *
  401. ****************************************************************************/
  402. static void enc_wrgreg2(FAR struct enc_driver_s *priv, uint8_t cmd,
  403. uint8_t wrdata)
  404. {
  405. DEBUGASSERT(priv && priv->spi);
  406. /* Select ENC28J60 chip */
  407. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
  408. /* Send the write command and data. The sequence requires 16-clocks:
  409. * 8 to clock out the cmd + 8 to clock out the data.
  410. */
  411. (void)SPI_SEND(priv->spi, cmd); /* Clock out the command */
  412. (void)SPI_SEND(priv->spi, wrdata); /* Clock out the data */
  413. /* De-select ENC28J60 chip. */
  414. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
  415. enc_wrdump(cmd, wrdata);
  416. }
  417. /****************************************************************************
  418. * Name: enc_src
  419. *
  420. * Description:
  421. * Send the single byte system reset command (SRC).
  422. *
  423. * "The System Reset Command (SRC) allows the host controller to issue a
  424. * System Soft Reset command. Unlike other SPI commands, the SRC is
  425. * only a single byte command and does not operate on any register. The
  426. * command is started by pulling the CS pin low. The SRC opcode is the
  427. * sent, followed by a 5-bit Soft Reset command constant of 1Fh. The
  428. * SRC operation is terminated by raising the CS pin."
  429. *
  430. * Input Parameters:
  431. * priv - Reference to the driver state structure
  432. *
  433. * Returned Value:
  434. * None
  435. *
  436. * Assumptions:
  437. *
  438. ****************************************************************************/
  439. static inline void enc_src(FAR struct enc_driver_s *priv)
  440. {
  441. DEBUGASSERT(priv && priv->spi);
  442. /* Select ENC28J60 chip */
  443. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
  444. /* Send the system reset command. */
  445. (void)SPI_SEND(priv->spi, ENC_SRC);
  446. /* Check CLKRDY bit to see when the reset is complete. There is an errata
  447. * that says the CLKRDY may be invalid. We'll wait a couple of msec to
  448. * workaround this condition.
  449. *
  450. * Also, "After a System Reset, all PHY registers should not be read or
  451. * written to until at least 50 µs have passed since the Reset has ended.
  452. * All registers will revert to their Reset default values. The dual
  453. * port buffer memory will maintain state throughout the System Reset."
  454. */
  455. up_mdelay(2);
  456. /* while ((enc_rdgreg(priv, ENC_ESTAT) & ESTAT_CLKRDY) != 0); */
  457. /* De-select ENC28J60 chip. */
  458. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
  459. enc_cmddump(ENC_SRC);
  460. }
  461. /****************************************************************************
  462. * Name: enc_setbank
  463. *
  464. * Description:
  465. * Set the bank for these next control register access.
  466. *
  467. * Assumption:
  468. * The caller has exclusive access to the SPI bus
  469. *
  470. * Input Parameters:
  471. * priv - Reference to the driver state structure
  472. * bank - The bank to select (0-3)
  473. *
  474. * Returned Value:
  475. * None
  476. *
  477. * Assumptions:
  478. *
  479. ****************************************************************************/
  480. static void enc_setbank(FAR struct enc_driver_s *priv, uint8_t bank)
  481. {
  482. /* Check if the bank setting has changed */
  483. if (bank != priv->bank)
  484. {
  485. /* Select bank 0 (just so that all of the bits are cleared) */
  486. enc_bfcgreg(priv, ENC_ECON1, ECON1_BSEL_MASK);
  487. /* Then OR in bits to get the correct bank */
  488. if (bank != 0)
  489. {
  490. enc_bfsgreg(priv, ENC_ECON1, (bank << ECON1_BSEL_SHIFT));
  491. }
  492. /* Then remember the bank setting */
  493. priv->bank = bank;
  494. }
  495. }
  496. /****************************************************************************
  497. * Name: enc_rdbreg
  498. *
  499. * Description:
  500. * Read from a banked control register using the RCR command.
  501. *
  502. * Input Parameters:
  503. * priv - Reference to the driver state structure
  504. * ctrlreg - Bit encoded address of banked register to read
  505. *
  506. * Returned Value:
  507. * The byte read from the banked register
  508. *
  509. * Assumptions:
  510. *
  511. ****************************************************************************/
  512. static uint8_t enc_rdbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg)
  513. {
  514. uint8_t rddata;
  515. DEBUGASSERT(priv && priv->spi);
  516. /* Set the bank */
  517. enc_setbank(priv, GETBANK(ctrlreg));
  518. /* Re-select ENC28J60 chip */
  519. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
  520. /* Send the RCR command and collect the data. How we collect the data
  521. * depends on if this is a PHY/CAN or not. The normal sequence requires
  522. * 16-clocks: 8 to clock out the cmd and 8 to clock in the data.
  523. */
  524. (void)SPI_SEND(priv->spi, ENC_RCR | GETADDR(ctrlreg)); /* Clock out the command */
  525. if (ISPHYMAC(ctrlreg))
  526. {
  527. /* The PHY/MAC sequence requires 24-clocks: 8 to clock out the cmd,
  528. * 8 dummy bits, and 8 to clock in the PHY/MAC data.
  529. */
  530. (void)SPI_SEND(priv->spi, 0); /* Clock in the dummy byte */
  531. }
  532. rddata = SPI_SEND(priv->spi, 0); /* Clock in the data */
  533. /* De-select ENC28J60 chip */
  534. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
  535. enc_rddump(ENC_RCR | GETADDR(ctrlreg), rddata);
  536. return rddata;
  537. }
  538. /****************************************************************************
  539. * Name: enc_wrbreg
  540. *
  541. * Description:
  542. * Write to a banked control register using the WCR command. Unlike
  543. * reading, this same SPI sequence works for normal, MAC, and PHY
  544. * registers.
  545. *
  546. * Input Parameters:
  547. * priv - Reference to the driver state structure
  548. * ctrlreg - Bit encoded address of banked register to write
  549. * wrdata - The data to send
  550. *
  551. * Returned Value:
  552. * None
  553. *
  554. * Assumptions:
  555. *
  556. ****************************************************************************/
  557. static void enc_wrbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
  558. uint8_t wrdata)
  559. {
  560. DEBUGASSERT(priv && priv->spi);
  561. /* Set the bank */
  562. enc_setbank(priv, GETBANK(ctrlreg));
  563. /* Re-select ENC28J60 chip */
  564. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
  565. /* Send the WCR command and data. The sequence requires 16-clocks:
  566. * 8 to clock out the cmd + 8 to clock out the data.
  567. */
  568. (void)SPI_SEND(priv->spi, ENC_WCR | GETADDR(ctrlreg)); /* Clock out the command */
  569. (void)SPI_SEND(priv->spi, wrdata); /* Clock out the data */
  570. /* De-select ENC28J60 chip. */
  571. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
  572. enc_wrdump(ENC_WCR | GETADDR(ctrlreg), wrdata);
  573. }
  574. /****************************************************************************
  575. * Name: enc_waitbreg
  576. *
  577. * Description:
  578. * Wait until banked register bit(s) take a specific value (or a timeout
  579. * occurs).
  580. *
  581. * Input Parameters:
  582. * priv - Reference to the driver state structure
  583. * ctrlreg - Bit encoded address of banked register to check
  584. * bits - The bits to check (a mask)
  585. * value - The value of the bits to return (value under mask)
  586. *
  587. * Returned Value:
  588. * OK on success, negated errno on failure
  589. *
  590. * Assumptions:
  591. *
  592. ****************************************************************************/
  593. static int enc_waitbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
  594. uint8_t bits, uint8_t value)
  595. {
  596. clock_t start = clock_systimer();
  597. clock_t elapsed;
  598. uint8_t rddata;
  599. /* Loop until the exit condition is met */
  600. do
  601. {
  602. /* Read the byte from the requested banked register */
  603. rddata = enc_rdbreg(priv, ctrlreg);
  604. elapsed = clock_systimer() - start;
  605. }
  606. while ((rddata & bits) != value && elapsed < ENC_POLLTIMEOUT);
  607. return (rddata & bits) == value ? OK : -ETIMEDOUT;
  608. }
  609. /****************************************************************************
  610. * Name: enc_txdump enc_rxdump
  611. *
  612. * Description:
  613. * Dump registers associated with receiving or sending packets.
  614. *
  615. * Input Parameters:
  616. * priv - Reference to the driver state structure
  617. *
  618. * Returned Value:
  619. * None
  620. *
  621. * Assumptions:
  622. *
  623. ****************************************************************************/
  624. #if 0 /* Sometimes useful */
  625. static void enc_rxdump(FAR struct enc_driver_s *priv)
  626. {
  627. syslog(LOG_DEBUG, "Rx Registers:\n");
  628. syslog(LOG_DEBUG, " EIE: %02x EIR: %02x\n",
  629. enc_rdgreg(priv, ENC_EIE), enc_rdgreg(priv, ENC_EIR));
  630. syslog(LOG_DEBUG, " ESTAT: %02x ECON1: %02x ECON2: %02x\n",
  631. enc_rdgreg(priv, ENC_ESTAT), enc_rdgreg(priv, ENC_ECON1),
  632. enc_rdgreg(priv, ENC_ECON2));
  633. syslog(LOG_DEBUG, " ERXST: %02x %02x\n",
  634. enc_rdbreg(priv, ENC_ERXSTH), enc_rdbreg(priv, ENC_ERXSTL));
  635. syslog(LOG_DEBUG, " ERXND: %02x %02x\n",
  636. enc_rdbreg(priv, ENC_ERXNDH), enc_rdbreg(priv, ENC_ERXNDL));
  637. syslog(LOG_DEBUG, " ERXRDPT: %02x %02x\n",
  638. enc_rdbreg(priv, ENC_ERXRDPTH), enc_rdbreg(priv, ENC_ERXRDPTL));
  639. syslog(LOG_DEBUG, " ERXFCON: %02x EPKTCNT: %02x\n",
  640. enc_rdbreg(priv, ENC_ERXFCON), enc_rdbreg(priv, ENC_EPKTCNT));
  641. syslog(LOG_DEBUG, " MACON1: %02x MACON3: %02x\n",
  642. enc_rdbreg(priv, ENC_MACON1), enc_rdbreg(priv, ENC_MACON3));
  643. syslog(LOG_DEBUG, " MAMXFL: %02x %02x\n",
  644. enc_rdbreg(priv, ENC_MAMXFLH), enc_rdbreg(priv, ENC_MAMXFLL));
  645. syslog(LOG_DEBUG, " MAADR: %02x:%02x:%02x:%02x:%02x:%02x\n",
  646. enc_rdbreg(priv, ENC_MAADR1), enc_rdbreg(priv, ENC_MAADR2),
  647. enc_rdbreg(priv, ENC_MAADR3), enc_rdbreg(priv, ENC_MAADR4),
  648. enc_rdbreg(priv, ENC_MAADR5), enc_rdbreg(priv, ENC_MAADR6));
  649. }
  650. #endif
  651. #if 0 /* Sometimes useful */
  652. static void enc_txdump(FAR struct enc_driver_s *priv)
  653. {
  654. syslog(LOG_DEBUG, "Tx Registers:\n");
  655. syslog(LOG_DEBUG, " EIE: %02x EIR: %02x\n",
  656. enc_rdgreg(priv, ENC_EIE), enc_rdgreg(priv, ENC_EIR));
  657. syslog(LOG_DEBUG, " ESTAT: %02x ECON1: %02x\n",
  658. enc_rdgreg(priv, ENC_ESTAT), enc_rdgreg(priv, ENC_ECON1));
  659. syslog(LOG_DEBUG, " ETXST: %02x %02x\n",
  660. enc_rdbreg(priv, ENC_ETXSTH), enc_rdbreg(priv, ENC_ETXSTL));
  661. syslog(LOG_DEBUG, " ETXND: %02x %02x\n",
  662. enc_rdbreg(priv, ENC_ETXNDH), enc_rdbreg(priv, ENC_ETXNDL));
  663. syslog(LOG_DEBUG, " MACON1: %02x MACON3: %02x MACON4: %02x\n",
  664. enc_rdbreg(priv, ENC_MACON1), enc_rdbreg(priv, ENC_MACON3),
  665. enc_rdbreg(priv, ENC_MACON4));
  666. syslog(LOG_DEBUG, " MACON1: %02x MACON3: %02x MACON4: %02x\n",
  667. enc_rdbreg(priv, ENC_MACON1), enc_rdbreg(priv, ENC_MACON3),
  668. enc_rdbreg(priv, ENC_MACON4));
  669. syslog(LOG_DEBUG, " MABBIPG: %02x MAIPG %02x %02x\n",
  670. enc_rdbreg(priv, ENC_MABBIPG), enc_rdbreg(priv, ENC_MAIPGH),
  671. enc_rdbreg(priv, ENC_MAIPGL));
  672. syslog(LOG_DEBUG, " MACLCON1: %02x MACLCON2: %02x\n",
  673. enc_rdbreg(priv, ENC_MACLCON1), enc_rdbreg(priv, ENC_MACLCON2));
  674. syslog(LOG_DEBUG, " MAMXFL: %02x %02x\n",
  675. enc_rdbreg(priv, ENC_MAMXFLH), enc_rdbreg(priv, ENC_MAMXFLL));
  676. }
  677. #endif
  678. /****************************************************************************
  679. * Name: enc_rdbuffer
  680. *
  681. * Description:
  682. * Read a buffer of data.
  683. *
  684. * Input Parameters:
  685. * priv - Reference to the driver state structure
  686. * buffer - A pointer to the buffer to read into
  687. * buflen - The number of bytes to read
  688. *
  689. * Returned Value:
  690. * None
  691. *
  692. * Assumptions:
  693. * Read pointer is set to the correct address
  694. *
  695. ****************************************************************************/
  696. static void enc_rdbuffer(FAR struct enc_driver_s *priv, FAR uint8_t *buffer,
  697. size_t buflen)
  698. {
  699. DEBUGASSERT(priv && priv->spi);
  700. /* Select ENC28J60 chip */
  701. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
  702. /* Send the read buffer memory command (ignoring the response) */
  703. (void)SPI_SEND(priv->spi, ENC_RBM);
  704. /* Then read the buffer data */
  705. SPI_RECVBLOCK(priv->spi, buffer, buflen);
  706. /* De-select ENC28J60 chip. */
  707. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
  708. enc_bmdump(ENC_WBM, buffer, buflen);
  709. }
  710. /****************************************************************************
  711. * Name: enc_wrbuffer
  712. *
  713. * Description:
  714. * Write a buffer of data.
  715. *
  716. * Input Parameters:
  717. * priv - Reference to the driver state structure
  718. * buffer - A pointer to the buffer to write from
  719. * buflen - The number of bytes to write
  720. *
  721. * Returned Value:
  722. * None
  723. *
  724. * Assumptions:
  725. * Read pointer is set to the correct address
  726. *
  727. ****************************************************************************/
  728. static inline void enc_wrbuffer(FAR struct enc_driver_s *priv,
  729. FAR const uint8_t *buffer, size_t buflen)
  730. {
  731. DEBUGASSERT(priv && priv->spi);
  732. /* Select ENC28J60 chip
  733. *
  734. * "The WBM command is started by lowering the CS pin. ..."
  735. */
  736. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
  737. /* Send the write buffer memory command (ignoring the response)
  738. *
  739. * "...The [3-bit]WBM opcode should then be sent to the ENC28J60,
  740. * followed by the 5-bit constant, 1Ah."
  741. */
  742. (void)SPI_SEND(priv->spi, ENC_WBM);
  743. /* "...the ENC28J60 requires a single per packet control byte to
  744. * precede the packet for transmission."
  745. *
  746. * POVERRIDE: Per Packet Override bit (Not set):
  747. * 1 = The values of PCRCEN, PPADEN and PHUGEEN will override the
  748. * configuration defined by MACON3.
  749. * 0 = The values in MACON3 will be used to determine how the packet
  750. * will be transmitted
  751. * PCRCEN: Per Packet CRC Enable bit (Set, but won't be used because
  752. * POVERRIDE is zero).
  753. * PPADEN: Per Packet Padding Enable bit (Set, but won't be used because
  754. * POVERRIDE is zero).
  755. * PHUGEEN: Per Packet Huge Frame Enable bit (Set, but won't be used
  756. * because POVERRIDE is zero).
  757. */
  758. (void)SPI_SEND(priv->spi,
  759. (PKTCTRL_PCRCEN | PKTCTRL_PPADEN | PKTCTRL_PHUGEEN));
  760. /* Then send the buffer
  761. *
  762. * "... After the WBM command and constant are sent, the data to
  763. * be stored in the memory pointed to by EWRPT should be shifted
  764. * out MSb first to the ENC28J60. After 8 data bits are received,
  765. * the Write Pointer will automatically increment if AUTOINC is
  766. * set. The host controller can continue to provide clocks on the
  767. * SCK pin and send data on the SI pin, without raising CS, to
  768. * keep writing to the memory. In this manner, with AUTOINC
  769. * enabled, it is possible to continuously write sequential bytes
  770. * to the buffer memory without any extra SPI command
  771. * overhead.
  772. */
  773. SPI_SNDBLOCK(priv->spi, buffer, buflen);
  774. /* De-select ENC28J60 chip
  775. *
  776. * "The WBM command is terminated by bringing up the CS pin. ..."
  777. */
  778. SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
  779. enc_bmdump(ENC_WBM, buffer, buflen+1);
  780. }
  781. /****************************************************************************
  782. * Name: enc_rdphy
  783. *
  784. * Description:
  785. * Read 16-bits of PHY data.
  786. *
  787. * Input Parameters:
  788. * priv - Reference to the driver state structure
  789. * phyaddr - The PHY register address
  790. *
  791. * Returned Value:
  792. * 16-bit value read from the PHY
  793. *
  794. * Assumptions:
  795. *
  796. ****************************************************************************/
  797. static uint16_t enc_rdphy(FAR struct enc_driver_s *priv, uint8_t phyaddr)
  798. {
  799. uint16_t data = 0;
  800. /* "To read from a PHY register:
  801. *
  802. * 1. Write the address of the PHY register to read from into the MIREGADR
  803. * register.
  804. */
  805. enc_wrbreg(priv, ENC_MIREGADR, phyaddr);
  806. /* 2. Set the MICMD.MIIRD bit. The read operation begins and the
  807. * MISTAT.BUSY bit is set.
  808. */
  809. enc_wrbreg(priv, ENC_MICMD, MICMD_MIIRD);
  810. /* 3. Wait 10.24 µs. Poll the MISTAT.BUSY bit to be certain that the
  811. * operation is complete. While busy, the host controller should not
  812. * start any MIISCAN operations or write to the MIWRH register.
  813. *
  814. * When the MAC has obtained the register contents, the BUSY bit will
  815. * clear itself.
  816. */
  817. up_udelay(12);
  818. if (enc_waitbreg(priv, ENC_MISTAT, MISTAT_BUSY, 0x00) == OK)
  819. {
  820. /* 4. Clear the MICMD.MIIRD bit. */
  821. enc_wrbreg(priv, ENC_MICMD, 0x00);
  822. /* 5. Read the desired data from the MIRDL and MIRDH registers. The
  823. * order that these bytes are accessed is unimportant."
  824. */
  825. data = (uint16_t)enc_rdbreg(priv, ENC_MIRDL);
  826. data |= (uint16_t)enc_rdbreg(priv, ENC_MIRDH) << 8;
  827. }
  828. return data;
  829. }
  830. /****************************************************************************
  831. * Name: enc_wrphy
  832. *
  833. * Description:
  834. * write 16-bits of PHY data.
  835. *
  836. * Input Parameters:
  837. * priv - Reference to the driver state structure
  838. * phyaddr - The PHY register address
  839. * phydata - 16-bit data to write to the PHY
  840. *
  841. * Returned Value:
  842. * None
  843. *
  844. * Assumptions:
  845. *
  846. ****************************************************************************/
  847. static void enc_wrphy(FAR struct enc_driver_s *priv, uint8_t phyaddr,
  848. uint16_t phydata)
  849. {
  850. /* "To write to a PHY register:
  851. *
  852. * 1. Write the address of the PHY register to write to into the
  853. * MIREGADR register.
  854. */
  855. enc_wrbreg(priv, ENC_MIREGADR, phyaddr);
  856. /* 2. Write the lower 8 bits of data to write into the MIWRL register. */
  857. enc_wrbreg(priv, ENC_MIWRL, phydata);
  858. /* 3. Write the upper 8 bits of data to write into the MIWRH register.
  859. * Writing to this register automatically begins the MIIM transaction,
  860. * so it must be written to after MIWRL. The MISTAT.BUSY bit becomes
  861. * set.
  862. */
  863. enc_wrbreg(priv, ENC_MIWRH, phydata >> 8);
  864. /* The PHY register will be written after the MIIM operation completes,
  865. * which takes 10.24 µs. When the write operation has completed, the BUSY
  866. * bit will clear itself.
  867. *
  868. * The host controller should not start any MIISCAN or MIIRD operations
  869. * while busy."
  870. */
  871. up_udelay(12);
  872. enc_waitbreg(priv, ENC_MISTAT, MISTAT_BUSY, 0x00);
  873. }
  874. /****************************************************************************
  875. * Name: enc_transmit
  876. *
  877. * Description:
  878. * Start hardware transmission. Called either from:
  879. *
  880. * - pkif interrupt when an application responds to the receipt of data
  881. * by trying to send something, or
  882. * - From watchdog based polling.
  883. *
  884. * Input Parameters:
  885. * priv - Reference to the driver state structure
  886. *
  887. * Returned Value:
  888. * OK on success; a negated errno on failure
  889. *
  890. * Assumptions:
  891. *
  892. ****************************************************************************/
  893. static int enc_transmit(FAR struct enc_driver_s *priv)
  894. {
  895. uint16_t txend;
  896. /* Increment statistics */
  897. ninfo("Sending packet, pktlen: %d\n", priv->dev.d_len);
  898. NETDEV_TXPACKETS(&priv->dev);
  899. /* Verify that the hardware is ready to send another packet. The driver
  900. * starts a transmission process by setting ECON1.TXRTS. When the packet is
  901. * finished transmitting or is aborted due to an error/cancellation, the
  902. * ECON1.TXRTS bit will be cleared.
  903. *
  904. * NOTE: If we got here, then we have committed to sending a packet.
  905. * higher level logic must have assured that (1) there is no transmission
  906. * in progress, and that (2) TX-related interrupts are disabled.
  907. */
  908. DEBUGASSERT((enc_rdgreg(priv, ENC_ECON1) & ECON1_TXRTS) == 0);
  909. /* Send the packet: address=priv->dev.d_buf, length=priv->dev.d_len */
  910. enc_dumppacket("Transmit Packet", priv->dev.d_buf, priv->dev.d_len);
  911. /* Set transmit buffer start (is this necessary?). */
  912. enc_wrbreg(priv, ENC_ETXSTL, PKTMEM_TX_START & 0xff);
  913. enc_wrbreg(priv, ENC_ETXSTH, PKTMEM_TX_START >> 8);
  914. /* Reset the write pointer to start of transmit buffer */
  915. enc_wrbreg(priv, ENC_EWRPTL, PKTMEM_TX_START & 0xff);
  916. enc_wrbreg(priv, ENC_EWRPTH, PKTMEM_TX_START >> 8);
  917. /* Set the TX End pointer based on the size of the packet to send. Note
  918. * that the offset accounts for the control byte at the beginning the
  919. * buffer plus the size of the packet data.
  920. */
  921. txend = PKTMEM_TX_START + priv->dev.d_len;
  922. enc_wrbreg(priv, ENC_ETXNDL, txend & 0xff);
  923. enc_wrbreg(priv, ENC_ETXNDH, txend >> 8);
  924. /* Send the WBM command and copy the packet itself into the transmit
  925. * buffer at the position of the EWRPT register.
  926. */
  927. enc_wrbuffer(priv, priv->dev.d_buf, priv->dev.d_len);
  928. /* Set TXRTS to send the packet in the transmit buffer */
  929. enc_bfsgreg(priv, ENC_ECON1, ECON1_TXRTS);
  930. /* Setup the TX timeout watchdog (perhaps restarting the timer). Note:
  931. * Is there a race condition. Could the TXIF interrupt occur before
  932. * the timer is started?
  933. */
  934. (void)wd_start(priv->txtimeout, ENC_TXTIMEOUT, enc_txtimeout, 1,
  935. (wdparm_t)priv);
  936. return OK;
  937. }
  938. /****************************************************************************
  939. * Name: enc_txpoll
  940. *
  941. * Description:
  942. * The transmitter is available, check if the network has any outgoing packets ready
  943. * to send. This is a callback from devif_poll(). devif_poll() may be called:
  944. *
  945. * 1. When the preceding TX packet send is complete,
  946. * 2. When the preceding TX packet send timesout and the interface is reset
  947. * 3. During normal TX polling
  948. *
  949. * Input Parameters:
  950. * dev - Reference to the NuttX driver state structure
  951. *
  952. * Returned Value:
  953. * OK on success; a negated errno on failure
  954. *
  955. * Assumptions:
  956. * Interrupts are enabled but the caller holds the network lock.
  957. *
  958. ****************************************************************************/
  959. static int enc_txpoll(struct net_driver_s *dev)
  960. {
  961. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
  962. /* If the polling resulted in data that should be sent out on the network,
  963. * the field d_len is set to a value > 0.
  964. */
  965. ninfo("Poll result: d_len=%d\n", priv->dev.d_len);
  966. if (priv->dev.d_len > 0)
  967. {
  968. /* Look up the destination MAC address and add it to the Ethernet
  969. * header.
  970. */
  971. #ifdef CONFIG_NET_IPv4
  972. #ifdef CONFIG_NET_IPv6
  973. if (IFF_IS_IPv4(priv->dev.d_flags))
  974. #endif
  975. {
  976. arp_out(&priv->dev);
  977. }
  978. #endif /* CONFIG_NET_IPv4 */
  979. #ifdef CONFIG_NET_IPv6
  980. #ifdef CONFIG_NET_IPv4
  981. else
  982. #endif
  983. {
  984. neighbor_out(&priv->dev);
  985. }
  986. #endif /* CONFIG_NET_IPv6 */
  987. if (!devif_loopback(&priv->dev))
  988. {
  989. /* Send the packet */
  990. enc_transmit(priv);
  991. /* Stop the poll now because we can queue only one packet */
  992. return -EBUSY;
  993. }
  994. }
  995. /* If zero is returned, the polling will continue until all connections have
  996. * been examined.
  997. */
  998. return OK;
  999. }
  1000. /****************************************************************************
  1001. * Name: enc_linkstatus
  1002. *
  1003. * Description:
  1004. * The current link status can be obtained from the PHSTAT1.LLSTAT or
  1005. * PHSTAT2.LSTAT.
  1006. *
  1007. * Input Parameters:
  1008. * priv - Reference to the driver state structure
  1009. *
  1010. * Returned Value:
  1011. * None
  1012. *
  1013. * Assumptions:
  1014. *
  1015. ****************************************************************************/
  1016. static void enc_linkstatus(FAR struct enc_driver_s *priv)
  1017. {
  1018. #if 0
  1019. uint16_t regval = enc_rdphy(priv, ENC_PHSTAT2);
  1020. priv->duplex = ((regval & PHSTAT2_DPXSTAT) != 0);
  1021. priv->carrier = ((regval & PHSTAT2_LSTAT) != 0);
  1022. #endif
  1023. }
  1024. /****************************************************************************
  1025. * Name: enc_txif
  1026. *
  1027. * Description:
  1028. * An TXIF interrupt was received indicating that the last TX packet(s) is
  1029. * done
  1030. *
  1031. * Input Parameters:
  1032. * priv - Reference to the driver state structure
  1033. *
  1034. * Returned Value:
  1035. * None
  1036. *
  1037. * Assumptions:
  1038. * Interrupts are enabled but the caller holds the network lock.
  1039. *
  1040. ****************************************************************************/
  1041. static void enc_txif(FAR struct enc_driver_s *priv)
  1042. {
  1043. /* Update statistics */
  1044. NETDEV_TXDONE(&priv->dev);
  1045. /* Clear the request to send bit */
  1046. enc_bfcgreg(priv, ENC_ECON1, ECON1_TXRTS);
  1047. /* If no further xmits are pending, then cancel the TX timeout */
  1048. wd_cancel(priv->txtimeout);
  1049. /* Then poll the network for new XMIT data */
  1050. (void)devif_poll(&priv->dev, enc_txpoll);
  1051. }
  1052. /****************************************************************************
  1053. * Name: enc_txerif
  1054. *
  1055. * Description:
  1056. * An TXERIF interrupt was received indicating that a TX abort has occurred.
  1057. *
  1058. * Input Parameters:
  1059. * priv - Reference to the driver state structure
  1060. *
  1061. * Returned Value:
  1062. * None
  1063. *
  1064. * Assumptions:
  1065. *
  1066. ****************************************************************************/
  1067. static void enc_txerif(FAR struct enc_driver_s *priv)
  1068. {
  1069. /* Update statistics */
  1070. NETDEV_TXERRORS(&priv->dev);
  1071. /* Reset TX */
  1072. enc_bfsgreg(priv, ENC_ECON1, ECON1_TXRST);
  1073. enc_bfcgreg(priv, ENC_ECON1, ECON1_TXRST | ECON1_TXRTS);
  1074. /* Here we really should re-transmit (I fact, if we want half duplex to
  1075. * work right, then it is necessary to do this!):
  1076. *
  1077. * 1. Read the TSV:
  1078. * - Read ETXNDL to get the end pointer
  1079. * - Read 7 bytes from that pointer + 1 using ENC_RMB.
  1080. * 2. Determine if we need to retransmit. Check the LATE COLLISION bit, if
  1081. * set, then we need to transmit.
  1082. * 3. Retranmit by resetting ECON1_TXRTS.
  1083. */
  1084. #ifdef CONFIG_ENC28J60_HALFDUPLEX
  1085. # error "Missing logic for half duplex"
  1086. #endif
  1087. }
  1088. /****************************************************************************
  1089. * Name: enc_rxerif
  1090. *
  1091. * Description:
  1092. * An RXERIF interrupt was received indicating that the last TX packet(s) is
  1093. * done
  1094. *
  1095. * Input Parameters:
  1096. * priv - Reference to the driver state structure
  1097. *
  1098. * Returned Value:
  1099. * None
  1100. *
  1101. * Assumptions:
  1102. *
  1103. ****************************************************************************/
  1104. static void enc_rxerif(FAR struct enc_driver_s *priv)
  1105. {
  1106. /* REVISIT: Update statistics */
  1107. }
  1108. /****************************************************************************
  1109. * Name: enc_rxdispatch
  1110. *
  1111. * Description:
  1112. * Give the newly received packet to the network.
  1113. *
  1114. * Input Parameters:
  1115. * priv - Reference to the driver state structure
  1116. *
  1117. * Returned Value:
  1118. * None
  1119. *
  1120. * Assumptions:
  1121. * Interrupts are enabled but the caller holds the network lock.
  1122. *
  1123. ****************************************************************************/
  1124. static void enc_rxdispatch(FAR struct enc_driver_s *priv)
  1125. {
  1126. #ifdef CONFIG_NET_PKT
  1127. /* When packet sockets are enabled, feed the frame into the packet tap */
  1128. pkt_input(&priv->dev);
  1129. #endif
  1130. /* We only accept IP packets of the configured type and ARP packets */
  1131. #ifdef CONFIG_NET_IPv4
  1132. if (BUF->type == HTONS(ETHTYPE_IP))
  1133. {
  1134. ninfo("IPv4 frame\n");
  1135. NETDEV_RXIPV4(&priv->dev);
  1136. /* Handle ARP on input then give the IPv4 packet to the network
  1137. * layer
  1138. */
  1139. arp_ipin(&priv->dev);
  1140. ipv4_input(&priv->dev);
  1141. /* If the above function invocation resulted in data that should be
  1142. * sent out on the network, the field d_len will set to a value > 0.
  1143. */
  1144. if (priv->dev.d_len > 0)
  1145. {
  1146. /* Update the Ethernet header with the correct MAC address */
  1147. #ifdef CONFIG_NET_IPv6
  1148. if (IFF_IS_IPv4(priv->dev.d_flags))
  1149. #endif
  1150. {
  1151. arp_out(&priv->dev);
  1152. }
  1153. #ifdef CONFIG_NET_IPv6
  1154. else
  1155. {
  1156. neighbor_out(&priv->dev);
  1157. }
  1158. #endif
  1159. /* And send the packet */
  1160. enc_transmit(priv);
  1161. }
  1162. }
  1163. else
  1164. #endif
  1165. #ifdef CONFIG_NET_IPv6
  1166. if (BUF->type == HTONS(ETHTYPE_IP6))
  1167. {
  1168. ninfo("Iv6 frame\n");
  1169. NETDEV_RXIPV6(&priv->dev);
  1170. /* Give the IPv6 packet to the network layer */
  1171. ipv6_input(&priv->dev);
  1172. /* If the above function invocation resulted in data that should be
  1173. * sent out on the network, the field d_len will set to a value > 0.
  1174. */
  1175. if (priv->dev.d_len > 0)
  1176. {
  1177. /* Update the Ethernet header with the correct MAC address */
  1178. #ifdef CONFIG_NET_IPv4
  1179. if (IFF_IS_IPv4(priv->dev.d_flags))
  1180. {
  1181. arp_out(&priv->dev);
  1182. }
  1183. else
  1184. #endif
  1185. #ifdef CONFIG_NET_IPv6
  1186. {
  1187. neighbor_out(&priv->dev);
  1188. }
  1189. #endif
  1190. /* And send the packet */
  1191. enc_transmit(priv);
  1192. }
  1193. }
  1194. else
  1195. #endif
  1196. #ifdef CONFIG_NET_ARP
  1197. if (BUF->type == htons(ETHTYPE_ARP))
  1198. {
  1199. ninfo("ARP packet received (%02x)\n", BUF->type);
  1200. NETDEV_RXARP(&priv->dev);
  1201. arp_arpin(&priv->dev);
  1202. /* If the above function invocation resulted in data that should be
  1203. * sent out on the network, the field d_len will set to a value > 0.
  1204. */
  1205. if (priv->dev.d_len > 0)
  1206. {
  1207. enc_transmit(priv);
  1208. }
  1209. }
  1210. else
  1211. #endif
  1212. {
  1213. nwarn("WARNING: Unsupported packet type dropped (%02x)\n",
  1214. htons(BUF->type));
  1215. NETDEV_RXDROPPED(&priv->dev);
  1216. }
  1217. }
  1218. /****************************************************************************
  1219. * Name: enc_pktif
  1220. *
  1221. * Description:
  1222. * An interrupt was received indicating the availability of a new RX packet
  1223. *
  1224. * Input Parameters:
  1225. * priv - Reference to the driver state structure
  1226. *
  1227. * Returned Value:
  1228. * None
  1229. *
  1230. * Assumptions:
  1231. * Interrupts are enabled but the caller holds the network lock.
  1232. *
  1233. ****************************************************************************/
  1234. static void enc_pktif(FAR struct enc_driver_s *priv)
  1235. {
  1236. uint8_t rsv[6];
  1237. uint16_t pktlen;
  1238. uint16_t rxstat;
  1239. /* Update statistics */
  1240. NETDEV_RXPACKETS(&priv->dev);
  1241. /* Set the read pointer to the start of the received packet (ERDPT) */
  1242. DEBUGASSERT(priv->nextpkt <= PKTMEM_RX_END);
  1243. enc_wrbreg(priv, ENC_ERDPTL, (priv->nextpkt));
  1244. enc_wrbreg(priv, ENC_ERDPTH, (priv->nextpkt) >> 8);
  1245. /* Read the next packet pointer and the 4 byte read status vector (RSV)
  1246. * at the beginning of the received packet. (ERDPT should auto-increment
  1247. * and wrap to the beginning of the read buffer as necessary)
  1248. */
  1249. enc_rdbuffer(priv, rsv, 6);
  1250. /* Decode the new next packet pointer, and the RSV. The
  1251. * RSV is encoded as:
  1252. *
  1253. * Bits 0-15: Indicates length of the received frame. This includes the
  1254. * destination address, source address, type/length, data,
  1255. * padding and CRC fields. This field is stored in little-
  1256. * endian format.
  1257. * Bits 16-31: Bit encoded RX status.
  1258. */
  1259. priv->nextpkt = (uint16_t)rsv[1] << 8 | (uint16_t)rsv[0];
  1260. pktlen = (uint16_t)rsv[3] << 8 | (uint16_t)rsv[2];
  1261. rxstat = (uint16_t)rsv[5] << 8 | (uint16_t)rsv[4];
  1262. ninfo("Receiving packet, nextpkt: %04x pktlen: %d rxstat: %04x\n",
  1263. priv->nextpkt, pktlen, rxstat);
  1264. /* Check if the packet was received OK */
  1265. if ((rxstat & RXSTAT_OK) == 0)
  1266. {
  1267. nerr("ERROR: RXSTAT: %04x\n", rxstat);
  1268. NETDEV_RXERRORS(&priv->dev);
  1269. }
  1270. /* Check for a usable packet length (4 added for the CRC) */
  1271. else if (pktlen > (CONFIG_NET_ETH_PKTSIZE + 4) || pktlen <= (ETH_HDRLEN + 4))
  1272. {
  1273. nerr("ERROR: Bad packet size dropped (%d)\n", pktlen);
  1274. NETDEV_RXERRORS(&priv->dev);
  1275. }
  1276. /* Otherwise, read and process the packet */
  1277. else
  1278. {
  1279. /* Save the packet length (without the 4 byte CRC) in priv->dev.d_len */
  1280. priv->dev.d_len = pktlen - 4;
  1281. /* Copy the data data from the receive buffer to priv->dev.d_buf.
  1282. * ERDPT should be correctly positioned from the last call to to
  1283. * end_rdbuffer (above).
  1284. */
  1285. enc_rdbuffer(priv, priv->dev.d_buf, priv->dev.d_len);
  1286. enc_dumppacket("Received Packet", priv->dev.d_buf, priv->dev.d_len);
  1287. /* Dispatch the packet to the network */
  1288. enc_rxdispatch(priv);
  1289. }
  1290. /* Move the RX read pointer to the start of the next received packet.
  1291. * This frees the memory we just read.
  1292. */
  1293. enc_wrbreg(priv, ENC_ERXRDPTL, (priv->nextpkt));
  1294. enc_wrbreg(priv, ENC_ERXRDPTH, (priv->nextpkt) >> 8);
  1295. /* Decrement the packet counter indicate we are done with this packet */
  1296. enc_bfsgreg(priv, ENC_ECON2, ECON2_PKTDEC);
  1297. }
  1298. /****************************************************************************
  1299. * Name: enc_irqworker
  1300. *
  1301. * Description:
  1302. * Perform interrupt handling logic outside of the interrupt handler (on
  1303. * the work queue thread).
  1304. *
  1305. * Input Parameters:
  1306. * arg - The reference to the driver structure (case to void*)
  1307. *
  1308. * Returned Value:
  1309. * None
  1310. *
  1311. * Assumptions:
  1312. *
  1313. ****************************************************************************/
  1314. static void enc_irqworker(FAR void *arg)
  1315. {
  1316. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
  1317. uint8_t eir;
  1318. DEBUGASSERT(priv);
  1319. /* Get exclusive access to both the network and the SPI bus. */
  1320. net_lock();
  1321. enc_lock(priv);
  1322. /* Disable further interrupts by clearing the global interrupt enable bit.
  1323. * "After an interrupt occurs, the host controller should clear the global
  1324. * enable bit for the interrupt pin before servicing the interrupt. Clearing
  1325. * the enable bit will cause the interrupt pin to return to the non-asserted
  1326. * state (high). Doing so will prevent the host controller from missing a
  1327. * falling edge should another interrupt occur while the immediate interrupt
  1328. * is being serviced."
  1329. */
  1330. enc_bfcgreg(priv, ENC_EIE, EIE_INTIE);
  1331. /* Loop until all interrupts have been processed (EIR==0). Note that
  1332. * there is no infinite loop check... if there are always pending interrupts,
  1333. * we are just broken.
  1334. */
  1335. while ((eir = enc_rdgreg(priv, ENC_EIR) & EIR_ALLINTS) != 0)
  1336. {
  1337. /* Handle interrupts according to interrupt register register bit
  1338. * settings.
  1339. */
  1340. ninfo("EIR: %02x\n", eir);
  1341. /* DMAIF: The DMA interrupt indicates that the DMA module has completed
  1342. * its memory copy or checksum calculation. Additionally, this interrupt
  1343. * will be caused if the host controller cancels a DMA operation by
  1344. * manually clearing the DMAST bit. Once set, DMAIF can only be cleared
  1345. * by the host controller or by a Reset condition.
  1346. */
  1347. if ((eir & EIR_DMAIF) != 0) /* DMA interrupt */
  1348. {
  1349. /* Not used by this driver. Just clear the interrupt request. */
  1350. enc_bfcgreg(priv, ENC_EIR, EIR_DMAIF);
  1351. }
  1352. /* LINKIF: The LINKIF indicates that the link status has changed.
  1353. * The actual current link status can be obtained from the
  1354. * PHSTAT1.LLSTAT or PHSTAT2.LSTAT. Unlike other interrupt sources, the
  1355. * link status change interrupt is created in the integrated PHY
  1356. * module.
  1357. *
  1358. * To receive it, the host controller must set the PHIE.PLNKIE and
  1359. * PGEIE bits. After setting the two PHY interrupt enable bits, the
  1360. * LINKIF bit will then shadow the contents of the PHIR.PGIF bit.
  1361. *
  1362. * Once LINKIF is set, it can only be cleared by the host controller or
  1363. * by a Reset. The LINKIF bit is read-only. Performing an MII read on
  1364. * the PHIR register will clear the LINKIF, PGIF and PLNKIF bits
  1365. * automatically and allow for future link status change interrupts.
  1366. */
  1367. if ((eir & EIR_LINKIF) != 0) /* Link change interrupt */
  1368. {
  1369. enc_linkstatus(priv); /* Get current link status */
  1370. enc_rdphy(priv, ENC_PHIR); /* Clear the LINKIF interrupt */
  1371. }
  1372. /* TXIF: The Transmit Interrupt Flag (TXIF) is used to indicate that
  1373. * the requested packet transmission has ended. Upon transmission
  1374. * completion, abort or transmission cancellation by the host
  1375. * controller, the EIR.TXIF flag will be set to 1.
  1376. *
  1377. * Once TXIF is set, it can only be cleared by the host controller
  1378. * or by a Reset condition. Once processed, the host controller should
  1379. * use the BFC command to clear the EIR.TXIF bit.
  1380. */
  1381. if ((eir & EIR_TXIF) != 0) /* Transmit interrupt */
  1382. {
  1383. enc_txif(priv); /* Handle TX completion */
  1384. enc_bfcgreg(priv, ENC_EIR, EIR_TXIF); /* Clear the TXIF interrupt */
  1385. }
  1386. /* TXERIF: The Transmit Error Interrupt Flag (TXERIF) is used to
  1387. * indicate that a transmit abort has occurred. An abort can occur
  1388. * because of any of the following:
  1389. *
  1390. * 1. Excessive collisions occurred as defined by the Retransmission
  1391. * Maximum (RETMAX) bits in the MACLCON1 register.
  1392. * 2. A late collision occurred as defined by the Collision Window
  1393. * (COLWIN) bits in the MACLCON2 register.
  1394. * 3. A collision after transmitting 64 bytes occurred (ESTAT.LATECOL
  1395. * set).
  1396. * 4. The transmission was unable to gain an opportunity to transmit
  1397. * the packet because the medium was constantly occupied for too long.
  1398. * The deferral limit (2.4287 ms) was reached and the MACON4.DEFER bit
  1399. * was clear.
  1400. * 5. An attempt to transmit a packet larger than the maximum frame
  1401. * length defined by the MAMXFL registers was made without setting
  1402. * the MACON3.HFRMEN bit or per packet POVERRIDE and PHUGEEN bits.
  1403. *
  1404. * Upon any of these conditions, the EIR.TXERIF flag is set to 1. Once
  1405. * set, it can only be cleared by the host controller or by a Reset
  1406. * condition.
  1407. *
  1408. * After a transmit abort, the TXRTS bit will be cleared, the
  1409. * ESTAT.TXABRT bit will be set and the transmit status vector will be
  1410. * written at ETXND + 1. The MAC will not automatically attempt to
  1411. * retransmit the packet. The host controller may wish to read the
  1412. * transmit status vector and LATECOL bit to determine the cause of
  1413. * the abort. After determining the problem and solution, the host
  1414. * controller should clear the LATECOL (if set) and TXABRT bits so
  1415. * that future aborts can be detected accurately.
  1416. *
  1417. * In Full-Duplex mode, condition 5 is the only one that should cause
  1418. * this interrupt. Collisions and other problems related to sharing
  1419. * the network are not possible on full-duplex networks. The conditions
  1420. * which cause the transmit error interrupt meet the requirements of the
  1421. * transmit interrupt. As a result, when this interrupt occurs, TXIF
  1422. * will also be simultaneously set.
  1423. */
  1424. if ((eir & EIR_TXERIF) != 0) /* Transmit Error Interrupts */
  1425. {
  1426. enc_txerif(priv); /* Handle the TX error */
  1427. enc_bfcgreg(priv, ENC_EIR, EIR_TXERIF); /* Clear the TXERIF interrupt */
  1428. }
  1429. /* PKTIF The Receive Packet Pending Interrupt Flag (PKTIF) is used to
  1430. * indicate the presence of one or more data packets in the receive
  1431. * buffer and to provide a notification means for the arrival of new
  1432. * packets. When the receive buffer has at least one packet in it,
  1433. * EIR.PKTIF will be set. In other words, this interrupt flag will be
  1434. * set anytime the Ethernet Packet Count register (EPKTCNT) is non-zero.
  1435. *
  1436. * The PKTIF bit can only be cleared by the host controller or by a Reset
  1437. * condition. In order to clear PKTIF, the EPKTCNT register must be
  1438. * decremented to 0. If the last data packet in the receive buffer is
  1439. * processed, EPKTCNT will become zero and the PKTIF bit will automatically
  1440. * be cleared.
  1441. */
  1442. /* Ignore PKTIF because is unreliable. Use EPKTCNT instead */
  1443. /* if ((eir & EIR_PKTIF) != 0) */
  1444. {
  1445. uint8_t pktcnt = enc_rdbreg(priv, ENC_EPKTCNT);
  1446. if (pktcnt > 0)
  1447. {
  1448. ninfo("EPKTCNT: %02x\n", pktcnt);
  1449. /* Handle packet receipt */
  1450. enc_pktif(priv);
  1451. }
  1452. }
  1453. /* RXERIF: The Receive Error Interrupt Flag (RXERIF) is used to
  1454. * indicate a receive buffer overflow condition. Alternately, this
  1455. * interrupt may indicate that too many packets are in the receive
  1456. * buffer and more cannot be stored without overflowing the EPKTCNT
  1457. * register. When a packet is being received and the receive buffer
  1458. * runs completely out of space, or EPKTCNT is 255 and cannot be
  1459. * incremented, the packet being received will be aborted (permanently
  1460. * lost) and the EIR.RXERIF bit will be set to 1.
  1461. *
  1462. * Once set, RXERIF can only be cleared by the host controller or by a
  1463. * Reset condition. Normally, upon the receive error condition, the
  1464. * host controller would process any packets pending from the receive
  1465. * buffer and then make additional room for future packets by
  1466. * advancing the ERXRDPT registers (low byte first) and decrementing
  1467. * the EPKTCNT register.
  1468. *
  1469. * Once processed, the host controller should use the BFC command to
  1470. * clear the EIR.RXERIF bit.
  1471. */
  1472. if ((eir & EIR_RXERIF) != 0) /* Receive Error Interrupts */
  1473. {
  1474. enc_rxerif(priv); /* Handle the RX error */
  1475. enc_bfcgreg(priv, ENC_EIR, EIR_RXERIF); /* Clear the RXERIF interrupt */
  1476. }
  1477. }
  1478. /* Enable GPIO interrupts */
  1479. priv->lower->enable(priv->lower);
  1480. /* Enable Ethernet interrupts */
  1481. enc_bfsgreg(priv, ENC_EIE, EIE_INTIE);
  1482. /* Release lock on the SPI bus and the network */
  1483. enc_unlock(priv);
  1484. net_unlock();
  1485. }
  1486. /****************************************************************************
  1487. * Name: enc_interrupt
  1488. *
  1489. * Description:
  1490. * Hardware interrupt handler
  1491. *
  1492. * Input Parameters:
  1493. * irq - Number of the IRQ that generated the interrupt
  1494. * context - Interrupt register state save info (architecture-specific)
  1495. *
  1496. * Returned Value:
  1497. * OK on success
  1498. *
  1499. * Assumptions:
  1500. *
  1501. ****************************************************************************/
  1502. static int enc_interrupt(int irq, FAR void *context, FAR void *arg)
  1503. {
  1504. FAR struct enc_driver_s *priv;
  1505. DEBUGASSERT(arg != NULL);
  1506. priv = (FAR struct enc_driver_s *)arg;
  1507. /* In complex environments, we cannot do SPI transfers from the interrupt
  1508. * handler because semaphores are probably used to lock the SPI bus. In
  1509. * this case, we will defer processing to the worker thread. This is also
  1510. * much kinder in the use of system resources and is, therefore, probably
  1511. * a good thing to do in any event.
  1512. */
  1513. DEBUGASSERT(work_available(&priv->irqwork));
  1514. /* Notice that further GPIO interrupts are disabled until the work is
  1515. * actually performed. This is to prevent overrun of the worker thread.
  1516. * Interrupts are re-enabled in enc_irqworker() when the work is completed.
  1517. */
  1518. priv->lower->disable(priv->lower);
  1519. return work_queue(ENCWORK, &priv->irqwork, enc_irqworker, (FAR void *)priv, 0);
  1520. }
  1521. /****************************************************************************
  1522. * Name: enc_toworker
  1523. *
  1524. * Description:
  1525. * Our TX watchdog timed out. This is the worker thread continuation of
  1526. * the watchdog timer interrupt. Reset the hardware and start again.
  1527. *
  1528. * Input Parameters:
  1529. * arg - The reference to the driver structure (case to void*)
  1530. *
  1531. * Returned Value:
  1532. * None
  1533. *
  1534. * Assumptions:
  1535. *
  1536. ****************************************************************************/
  1537. static void enc_toworker(FAR void *arg)
  1538. {
  1539. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
  1540. int ret;
  1541. nerr("ERROR: Tx timeout\n");
  1542. DEBUGASSERT(priv);
  1543. /* Get exclusive access to the network */
  1544. net_lock();
  1545. /* Increment statistics and dump debug info */
  1546. NETDEV_TXTIMEOUTS(&priv->dev);
  1547. /* Then reset the hardware: Take the interface down, then bring it
  1548. * back up
  1549. */
  1550. ret = enc_ifdown(&priv->dev);
  1551. DEBUGASSERT(ret == OK);
  1552. ret = enc_ifup(&priv->dev);
  1553. DEBUGASSERT(ret == OK);
  1554. UNUSED(ret);
  1555. /* Then poll the network for new XMIT data */
  1556. (void)devif_poll(&priv->dev, enc_txpoll);
  1557. /* Release lock on the network */
  1558. net_unlock();
  1559. }
  1560. /****************************************************************************
  1561. * Name: enc_txtimeout
  1562. *
  1563. * Description:
  1564. * Our TX watchdog timed out. Called from the timer interrupt handler.
  1565. * The last TX never completed. Perform work on the worker thread.
  1566. *
  1567. * Input Parameters:
  1568. * argc - The number of available arguments
  1569. * arg - The first argument
  1570. *
  1571. * Returned Value:
  1572. * None
  1573. *
  1574. * Assumptions:
  1575. *
  1576. ****************************************************************************/
  1577. static void enc_txtimeout(int argc, uint32_t arg, ...)
  1578. {
  1579. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
  1580. int ret;
  1581. /* In complex environments, we cannot do SPI transfers from the timeout
  1582. * handler because semaphores are probably used to lock the SPI bus. In
  1583. * this case, we will defer processing to the worker thread. This is also
  1584. * much kinder in the use of system resources and is, therefore, probably
  1585. * a good thing to do in any event.
  1586. */
  1587. DEBUGASSERT(priv && work_available(&priv->towork));
  1588. /* Notice that Tx timeout watchdog is not active so further Tx timeouts
  1589. * can occur until we restart the Tx timeout watchdog.
  1590. */
  1591. ret = work_queue(ENCWORK, &priv->towork, enc_toworker, (FAR void *)priv, 0);
  1592. DEBUGASSERT(ret == OK);
  1593. UNUSED(ret);
  1594. }
  1595. /****************************************************************************
  1596. * Name: enc_pollworker
  1597. *
  1598. * Description:
  1599. * Periodic timer handler continuation.
  1600. *
  1601. * Input Parameters:
  1602. * argc - The number of available arguments
  1603. * arg - The first argument
  1604. *
  1605. * Returned Value:
  1606. * None
  1607. *
  1608. * Assumptions:
  1609. *
  1610. ****************************************************************************/
  1611. static void enc_pollworker(FAR void *arg)
  1612. {
  1613. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
  1614. DEBUGASSERT(priv);
  1615. /* Get exclusive access to both the network and the SPI bus. */
  1616. net_lock();
  1617. enc_lock(priv);
  1618. /* Verify that the hardware is ready to send another packet. The driver
  1619. * start a transmission process by setting ECON1.TXRTS. When the packet is
  1620. * finished transmitting or is aborted due to an error/cancellation, the
  1621. * ECON1.TXRTS bit will be cleared.
  1622. */
  1623. if ((enc_rdgreg(priv, ENC_ECON1) & ECON1_TXRTS) == 0)
  1624. {
  1625. /* Yes.. update TCP timing states and poll the network for new XMIT data. Hmmm..
  1626. * looks like a bug here to me. Does this mean if there is a transmit
  1627. * in progress, we will missing TCP time state updates?
  1628. */
  1629. (void)devif_timer(&priv->dev, enc_txpoll);
  1630. }
  1631. /* Release lock on the SPI bus and the network */
  1632. enc_unlock(priv);
  1633. net_unlock();
  1634. /* Setup the watchdog poll timer again */
  1635. (void)wd_start(priv->txpoll, ENC_WDDELAY, enc_polltimer, 1,
  1636. (wdparm_t)arg);
  1637. }
  1638. /****************************************************************************
  1639. * Name: enc_polltimer
  1640. *
  1641. * Description:
  1642. * Periodic timer handler. Called from the timer interrupt handler.
  1643. *
  1644. * Input Parameters:
  1645. * argc - The number of available arguments
  1646. * arg - The first argument
  1647. *
  1648. * Returned Value:
  1649. * None
  1650. *
  1651. * Assumptions:
  1652. *
  1653. ****************************************************************************/
  1654. static void enc_polltimer(int argc, uint32_t arg, ...)
  1655. {
  1656. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
  1657. int ret;
  1658. /* In complex environments, we cannot do SPI transfers from the timeout
  1659. * handler because semaphores are probably used to lock the SPI bus. In
  1660. * this case, we will defer processing to the worker thread. This is also
  1661. * much kinder in the use of system resources and is, therefore, probably
  1662. * a good thing to do in any event.
  1663. */
  1664. DEBUGASSERT(priv && work_available(&priv->pollwork));
  1665. /* Notice that poll watchdog is not active so further poll timeouts can
  1666. * occur until we restart the poll timeout watchdog.
  1667. */
  1668. ret = work_queue(ENCWORK, &priv->pollwork, enc_pollworker, (FAR void *)priv, 0);
  1669. DEBUGASSERT(ret == OK);
  1670. UNUSED(ret);
  1671. }
  1672. /****************************************************************************
  1673. * Name: enc_ifup
  1674. *
  1675. * Description:
  1676. * NuttX Callback: Bring up the Ethernet interface when an IP address is
  1677. * provided
  1678. *
  1679. * Input Parameters:
  1680. * dev - Reference to the NuttX driver state structure
  1681. *
  1682. * Returned Value:
  1683. * None
  1684. *
  1685. * Assumptions:
  1686. *
  1687. ****************************************************************************/
  1688. static int enc_ifup(struct net_driver_s *dev)
  1689. {
  1690. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
  1691. int ret;
  1692. ninfo("Bringing up: %d.%d.%d.%d\n",
  1693. dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff,
  1694. (dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24);
  1695. /* Lock the SPI bus so that we have exclusive access */
  1696. enc_lock(priv);
  1697. /* Initialize Ethernet interface, set the MAC address, and make sure that
  1698. * the ENC28J80 is not in power save mode.
  1699. */
  1700. ret = enc_reset(priv);
  1701. if (ret == OK)
  1702. {
  1703. enc_setmacaddr(priv);
  1704. enc_pwrfull(priv);
  1705. /* Enable interrupts at the ENC28J60. Interrupts are still disabled
  1706. * at the interrupt controller.
  1707. */
  1708. enc_wrphy(priv, ENC_PHIE, PHIE_PGEIE | PHIE_PLNKIE);
  1709. enc_bfcgreg(priv, ENC_EIR, EIR_ALLINTS);
  1710. enc_wrgreg(priv, ENC_EIE, EIE_INTIE | EIE_PKTIE | EIE_LINKIE |
  1711. EIE_TXIE | EIE_TXERIE | EIE_RXERIE);
  1712. /* Enable the receiver */
  1713. enc_bfsgreg(priv, ENC_ECON1, ECON1_RXEN);
  1714. /* Set and activate a timer process */
  1715. (void)wd_start(priv->txpoll, ENC_WDDELAY, enc_polltimer, 1,
  1716. (wdparm_t)priv);
  1717. /* Mark the interface up and enable the Ethernet interrupt at the
  1718. * controller
  1719. */
  1720. priv->ifstate = ENCSTATE_UP;
  1721. priv->lower->enable(priv->lower);
  1722. }
  1723. /* Un-lock the SPI bus */
  1724. enc_unlock(priv);
  1725. return ret;
  1726. }
  1727. /****************************************************************************
  1728. * Name: enc_ifdown
  1729. *
  1730. * Description:
  1731. * NuttX Callback: Stop the interface.
  1732. *
  1733. * Input Parameters:
  1734. * dev - Reference to the NuttX driver state structure
  1735. *
  1736. * Returned Value:
  1737. * None
  1738. *
  1739. * Assumptions:
  1740. *
  1741. ****************************************************************************/
  1742. static int enc_ifdown(struct net_driver_s *dev)
  1743. {
  1744. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
  1745. irqstate_t flags;
  1746. int ret;
  1747. ninfo("Taking down: %d.%d.%d.%d\n",
  1748. dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff,
  1749. (dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24);
  1750. /* Lock the SPI bus so that we have exclusive access */
  1751. enc_lock(priv);
  1752. /* Disable the Ethernet interrupt */
  1753. flags = enter_critical_section();
  1754. priv->lower->disable(priv->lower);
  1755. /* Cancel the TX poll timer and TX timeout timers */
  1756. wd_cancel(priv->txpoll);
  1757. wd_cancel(priv->txtimeout);
  1758. /* Reset the device and leave in the power save state */
  1759. ret = enc_reset(priv);
  1760. enc_pwrsave(priv);
  1761. priv->ifstate = ENCSTATE_DOWN;
  1762. leave_critical_section(flags);
  1763. /* Un-lock the SPI bus */
  1764. enc_unlock(priv);
  1765. return ret;
  1766. }
  1767. /****************************************************************************
  1768. * Name: enc_txavail
  1769. *
  1770. * Description:
  1771. * Driver callback invoked when new TX data is available. This is a
  1772. * stimulus perform an out-of-cycle poll and, thereby, reduce the TX
  1773. * latency.
  1774. *
  1775. * Input Parameters:
  1776. * dev - Reference to the NuttX driver state structure
  1777. *
  1778. * Returned Value:
  1779. * None
  1780. *
  1781. * Assumptions:
  1782. * Called in normal user mode
  1783. *
  1784. ****************************************************************************/
  1785. static int enc_txavail(struct net_driver_s *dev)
  1786. {
  1787. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
  1788. irqstate_t flags;
  1789. /* Lock the SPI bus so that we have exclusive access */
  1790. enc_lock(priv);
  1791. /* Ignore the notification if the interface is not yet up */
  1792. flags = enter_critical_section();
  1793. if (priv->ifstate == ENCSTATE_UP)
  1794. {
  1795. /* Check if the hardware is ready to send another packet. The driver
  1796. * starts a transmission process by setting ECON1.TXRTS. When the packet is
  1797. * finished transmitting or is aborted due to an error/cancellation, the
  1798. * ECON1.TXRTS bit will be cleared.
  1799. */
  1800. if ((enc_rdgreg(priv, ENC_ECON1) & ECON1_TXRTS) == 0)
  1801. {
  1802. /* The interface is up and TX is idle; poll the network for new XMIT data */
  1803. (void)devif_poll(&priv->dev, enc_txpoll);
  1804. }
  1805. }
  1806. /* Un-lock the SPI bus */
  1807. leave_critical_section(flags);
  1808. enc_unlock(priv);
  1809. return OK;
  1810. }
  1811. /****************************************************************************
  1812. * Name: enc_addmac
  1813. *
  1814. * Description:
  1815. * NuttX Callback: Add the specified MAC address to the hardware multicast
  1816. * address filtering
  1817. *
  1818. * Input Parameters:
  1819. * dev - Reference to the NuttX driver state structure
  1820. * mac - The MAC address to be added
  1821. *
  1822. * Returned Value:
  1823. * None
  1824. *
  1825. * Assumptions:
  1826. *
  1827. ****************************************************************************/
  1828. #ifdef CONFIG_NET_MCASTGROUP
  1829. static int enc_addmac(struct net_driver_s *dev, FAR const uint8_t *mac)
  1830. {
  1831. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
  1832. /* Lock the SPI bus so that we have exclusive access */
  1833. enc_lock(priv);
  1834. /* Add the MAC address to the hardware multicast routing table */
  1835. #warning "Multicast MAC support not implemented"
  1836. /* Un-lock the SPI bus */
  1837. enc_unlock(priv);
  1838. return OK;
  1839. }
  1840. #endif
  1841. /****************************************************************************
  1842. * Name: enc_rmmac
  1843. *
  1844. * Description:
  1845. * NuttX Callback: Remove the specified MAC address from the hardware multicast
  1846. * address filtering
  1847. *
  1848. * Input Parameters:
  1849. * dev - Reference to the NuttX driver state structure
  1850. * mac - The MAC address to be removed
  1851. *
  1852. * Returned Value:
  1853. * None
  1854. *
  1855. * Assumptions:
  1856. *
  1857. ****************************************************************************/
  1858. #ifdef CONFIG_NET_MCASTGROUP
  1859. static int enc_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac)
  1860. {
  1861. FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
  1862. /* Lock the SPI bus so that we have exclusive access */
  1863. enc_lock(priv);
  1864. /* Add the MAC address to the hardware multicast routing table */
  1865. #warning "Multicast MAC support not implemented"
  1866. /* Un-lock the SPI bus */
  1867. enc_unlock(priv);
  1868. return OK;
  1869. }
  1870. #endif
  1871. /****************************************************************************
  1872. * Name: enc_pwrsave
  1873. *
  1874. * Description:
  1875. * The ENC28J60 may be commanded to power-down via the SPI interface.
  1876. * When powered down, it will no longer be able to transmit and receive
  1877. * any packets. To maximize power savings:
  1878. *
  1879. * 1. Turn off packet reception by clearing ECON1.RXEN.
  1880. * 2. Wait for any in-progress packets to finish being received by
  1881. * polling ESTAT.RXBUSY. This bit should be clear before proceeding.
  1882. * 3. Wait for any current transmissions to end by confirming ECON1.TXRTS
  1883. * is clear.
  1884. * 4. Set ECON2.VRPS (if not already set).
  1885. * 5. Enter Sleep by setting ECON2.PWRSV. All MAC, MII and PHY registers
  1886. * become inaccessible as a result. Setting PWRSV also clears
  1887. * ESTAT.CLKRDY automatically.
  1888. *
  1889. * In Sleep mode, all registers and buffer memory will maintain their
  1890. * states. The ETH registers and buffer memory will still be accessible
  1891. * by the host controller. Additionally, the clock driver will continue
  1892. * to operate. The CLKOUT function will be unaffected.
  1893. *
  1894. * Input Parameters:
  1895. * priv - Reference to the driver state structure
  1896. *
  1897. * Returned Value:
  1898. * None
  1899. *
  1900. * Assumptions:
  1901. *
  1902. ****************************************************************************/
  1903. static void enc_pwrsave(FAR struct enc_driver_s *priv)
  1904. {
  1905. ninfo("Set PWRSV\n");
  1906. /* 1. Turn off packet reception by clearing ECON1.RXEN. */
  1907. enc_bfcgreg(priv, ENC_ECON1, ECON1_RXEN);
  1908. /* 2. Wait for any in-progress packets to finish being received by
  1909. * polling ESTAT.RXBUSY. This bit should be clear before proceeding.
  1910. */
  1911. if (enc_waitbreg(priv, ENC_ESTAT, ESTAT_RXBUSY, 0) == OK)
  1912. {
  1913. /* 3. Wait for any current transmissions to end by confirming
  1914. * ECON1.TXRTS is clear.
  1915. */
  1916. enc_waitbreg(priv, ENC_ECON1, ECON1_TXRTS, 0);
  1917. /* 4. Set ECON2.VRPS (if not already set). */
  1918. /* enc_bfsgreg(priv, ENC_ECON2, ECON2_VRPS); <-- Set in enc_reset() */
  1919. /* 5. Enter Sleep by setting ECON2.PWRSV. */
  1920. enc_bfsgreg(priv, ENC_ECON2, ECON2_PWRSV);
  1921. }
  1922. }
  1923. /****************************************************************************
  1924. * Name: enc_pwrfull
  1925. *
  1926. * Description:
  1927. * When normal operation is desired, the host controller must perform
  1928. * a slightly modified procedure:
  1929. *
  1930. * 1. Wake-up by clearing ECON2.PWRSV.
  1931. * 2. Wait at least 300 ìs for the PHY to stabilize. To accomplish the
  1932. * delay, the host controller may poll ESTAT.CLKRDY and wait for it
  1933. * to become set.
  1934. * 3. Restore receive capability by setting ECON1.RXEN.
  1935. *
  1936. * After leaving Sleep mode, there is a delay of many milliseconds
  1937. * before a new link is established (assuming an appropriate link
  1938. * partner is present). The host controller may wish to wait until
  1939. * the link is established before attempting to transmit any packets.
  1940. * The link status can be determined by polling the PHSTAT2.LSTAT bit.
  1941. * Alternatively, the link change interrupt may be used if it is
  1942. * enabled.
  1943. *
  1944. * Input Parameters:
  1945. * priv - Reference to the driver state structure
  1946. *
  1947. * Returned Value:
  1948. * None
  1949. *
  1950. * Assumptions:
  1951. *
  1952. ****************************************************************************/
  1953. static void enc_pwrfull(FAR struct enc_driver_s *priv)
  1954. {
  1955. ninfo("Clear PWRSV\n");
  1956. /* 1. Wake-up by clearing ECON2.PWRSV. */
  1957. enc_bfcgreg(priv, ENC_ECON2, ECON2_PWRSV);
  1958. /* 2. Wait at least 300 ìs for the PHY to stabilize. To accomplish the
  1959. * delay, the host controller may poll ESTAT.CLKRDY and wait for it to
  1960. * become set.
  1961. */
  1962. enc_waitbreg(priv, ENC_ESTAT, ESTAT_CLKRDY, ESTAT_CLKRDY);
  1963. /* 3. Restore receive capability by setting ECON1.RXEN.
  1964. *
  1965. * The caller will do this when it is read to receive packets
  1966. */
  1967. }
  1968. /****************************************************************************
  1969. * Name: enc_setmacaddr
  1970. *
  1971. * Description:
  1972. * Set the MAC address to the configured value. This is done after ifup
  1973. * or after a TX timeout. Note that this means that the interface must
  1974. * be down before configuring the MAC addr.
  1975. *
  1976. * Input Parameters:
  1977. * priv - Reference to the driver state structure
  1978. *
  1979. * Returned Value:
  1980. * None
  1981. *
  1982. * Assumptions:
  1983. *
  1984. ****************************************************************************/
  1985. static void enc_setmacaddr(FAR struct enc_driver_s *priv)
  1986. {
  1987. /* Program the hardware with it's MAC address (for filtering).
  1988. * MAADR1 MAC Address Byte 1 (MAADR<47:40>), OUI Byte 1
  1989. * MAADR2 MAC Address Byte 2 (MAADR<39:32>), OUI Byte 2
  1990. * MAADR3 MAC Address Byte 3 (MAADR<31:24>), OUI Byte 3
  1991. * MAADR4 MAC Address Byte 4 (MAADR<23:16>)
  1992. * MAADR5 MAC Address Byte 5 (MAADR<15:8>)
  1993. * MAADR6 MAC Address Byte 6 (MAADR<7:0>)
  1994. */
  1995. enc_wrbreg(priv, ENC_MAADR1, priv->dev.d_mac.ether.ether_addr_octet[0]);
  1996. enc_wrbreg(priv, ENC_MAADR2, priv->dev.d_mac.ether.ether_addr_octet[1]);
  1997. enc_wrbreg(priv, ENC_MAADR3, priv->dev.d_mac.ether.ether_addr_octet[2]);
  1998. enc_wrbreg(priv, ENC_MAADR4, priv->dev.d_mac.ether.ether_addr_octet[3]);
  1999. enc_wrbreg(priv, ENC_MAADR5, priv->dev.d_mac.ether.ether_addr_octet[4]);
  2000. enc_wrbreg(priv, ENC_MAADR6, priv->dev.d_mac.ether.ether_addr_octet[5]);
  2001. }
  2002. /****************************************************************************
  2003. * Name: enc_reset
  2004. *
  2005. * Description:
  2006. * Stop, reset, re-initialize, and restart the ENC28J60. This is done
  2007. * initially, on ifup, and after a TX timeout.
  2008. *
  2009. * Input Parameters:
  2010. * priv - Reference to the driver state structure
  2011. *
  2012. * Returned Value:
  2013. * None
  2014. *
  2015. * Assumptions:
  2016. *
  2017. ****************************************************************************/
  2018. static int enc_reset(FAR struct enc_driver_s *priv)
  2019. {
  2020. uint8_t regval;
  2021. nwarn("WARNING: Reset\n");
  2022. /* Configure SPI for the ENC28J60 */
  2023. enc_configspi(priv->spi);
  2024. /* Reset the ENC28J60 */
  2025. enc_src(priv);
  2026. /* Initialize ECON1: Clear ECON1 */
  2027. enc_wrgreg(priv, ENC_ECON1, 0x00);
  2028. /* Initialize ECON2: Enable address auto increment and voltage
  2029. * regulator powersave.
  2030. */
  2031. enc_wrgreg(priv, ENC_ECON2, ECON2_AUTOINC | ECON2_VRPS);
  2032. /* Initialize receive buffer.
  2033. * First, set the receive buffer start address.
  2034. */
  2035. priv->nextpkt = PKTMEM_RX_START;
  2036. enc_wrbreg(priv, ENC_ERXSTL, PKTMEM_RX_START & 0xff);
  2037. enc_wrbreg(priv, ENC_ERXSTH, PKTMEM_RX_START >> 8);
  2038. /* Set the receive data pointer */
  2039. enc_wrbreg(priv, ENC_ERXRDPTL, PKTMEM_RX_START & 0xff);
  2040. enc_wrbreg(priv, ENC_ERXRDPTH, PKTMEM_RX_START >> 8);
  2041. /* Set the receive buffer end. */
  2042. enc_wrbreg(priv, ENC_ERXNDL, PKTMEM_RX_END & 0xff);
  2043. enc_wrbreg(priv, ENC_ERXNDH, PKTMEM_RX_END >> 8);
  2044. /* Set transmit buffer start. */
  2045. enc_wrbreg(priv, ENC_ETXSTL, PKTMEM_TX_START & 0xff);
  2046. enc_wrbreg(priv, ENC_ETXSTH, PKTMEM_TX_START >> 8);
  2047. /* Check if we are actually communicating with the ENC28J60. If its
  2048. * 0x00 or 0xff, then we are probably not communicating correctly
  2049. * via SPI.
  2050. */
  2051. regval = enc_rdbreg(priv, ENC_EREVID);
  2052. if (regval == 0x00 || regval == 0xff)
  2053. {
  2054. nerr("ERROR: Bad Rev ID: %02x\n", regval);
  2055. return -ENODEV;
  2056. }
  2057. ninfo("Rev ID: %02x\n", regval);
  2058. /* Set filter mode: unicast OR broadcast AND crc valid */
  2059. enc_wrbreg(priv, ENC_ERXFCON, ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN);
  2060. /* Enable MAC receive */
  2061. enc_wrbreg(priv, ENC_MACON1, MACON1_MARXEN | MACON1_TXPAUS | MACON1_RXPAUS);
  2062. /* Enable automatic padding and CRC operations */
  2063. #ifdef CONFIG_ENC28J60_HALFDUPLEX
  2064. enc_wrbreg(priv, ENC_MACON3, MACON3_PADCFG0 | MACON3_TXCRCEN | MACON3_FRMLNEN);
  2065. enc_wrbreg(priv, ENC_MACON4, MACON4_DEFER); /* Defer transmission enable */
  2066. /* Set Non-Back-to-Back Inter-Packet Gap */
  2067. enc_wrbreg(priv, ENC_MAIPGL, 0x12);
  2068. enc_wrbreg(priv, ENC_MAIPGH, 0x0c);
  2069. /* Set Back-to-Back Inter-Packet Gap */
  2070. enc_wrbreg(priv, ENC_MABBIPG, 0x12);
  2071. #else
  2072. /* Set filter mode: unicast OR broadcast AND crc valid AND Full Duplex */
  2073. enc_wrbreg(priv, ENC_MACON3,
  2074. MACON3_PADCFG0 | MACON3_TXCRCEN | MACON3_FRMLNEN | MACON3_FULDPX);
  2075. /* Set Non-Back-to-Back Inter-Packet Gap */
  2076. enc_wrbreg(priv, ENC_MAIPGL, 0x12);
  2077. /* Set Back-to-Back Inter-Packet Gap */
  2078. enc_wrbreg(priv, ENC_MABBIPG, 0x15);
  2079. #endif
  2080. /* Set the maximum packet size which the controller will accept */
  2081. enc_wrbreg(priv, ENC_MAMXFLL, CONFIG_NET_ETH_PKTSIZE & 0xff);
  2082. enc_wrbreg(priv, ENC_MAMXFLH, CONFIG_NET_ETH_PKTSIZE >> 8);
  2083. /* Configure LEDs (No, just use the defaults for now) */
  2084. /* enc_wrphy(priv, ENC_PHLCON, ??); */
  2085. /* Setup up PHCON1 & 2 */
  2086. #ifdef CONFIG_ENC28J60_HALFDUPLEX
  2087. enc_wrphy(priv, ENC_PHCON1, 0x00);
  2088. enc_wrphy(priv, ENC_PHCON2, PHCON2_HDLDIS);
  2089. #else
  2090. enc_wrphy(priv, ENC_PHCON1, PHCON1_PDPXMD);
  2091. enc_wrphy(priv, ENC_PHCON2, 0x00);
  2092. #endif
  2093. return OK;
  2094. }
  2095. /****************************************************************************
  2096. * Public Functions
  2097. ****************************************************************************/
  2098. /****************************************************************************
  2099. * Name: enc_initialize
  2100. *
  2101. * Description:
  2102. * Initialize the Ethernet driver. The ENC28J60 device is assumed to be
  2103. * in the post-reset state upon entry to this function.
  2104. *
  2105. * Input Parameters:
  2106. * spi - A reference to the platform's SPI driver for the ENC28J60
  2107. * lower - The MCU-specific interrupt used to control low-level MCU
  2108. * functions (i.e., ENC28J60 GPIO interrupts).
  2109. * devno - If more than one ENC28J60 is supported, then this is the
  2110. * zero based number that identifies the ENC28J60;
  2111. *
  2112. * Returned Value:
  2113. * OK on success; Negated errno on failure.
  2114. *
  2115. * Assumptions:
  2116. *
  2117. ****************************************************************************/
  2118. int enc_initialize(FAR struct spi_dev_s *spi,
  2119. FAR const struct enc_lower_s *lower, unsigned int devno)
  2120. {
  2121. FAR struct enc_driver_s *priv;
  2122. DEBUGASSERT(devno < CONFIG_ENC28J60_NINTERFACES);
  2123. priv = &g_enc28j60[devno];
  2124. /* Initialize the driver structure */
  2125. memset(g_enc28j60, 0, CONFIG_ENC28J60_NINTERFACES*sizeof(struct enc_driver_s));
  2126. priv->dev.d_buf = g_pktbuf; /* Single packet buffer */
  2127. priv->dev.d_ifup = enc_ifup; /* I/F down callback */
  2128. priv->dev.d_ifdown = enc_ifdown; /* I/F up (new IP address) callback */
  2129. priv->dev.d_txavail = enc_txavail; /* New TX data callback */
  2130. #ifdef CONFIG_NET_MCASTGROUP
  2131. priv->dev.d_addmac = enc_addmac; /* Add multicast MAC address */
  2132. priv->dev.d_rmmac = enc_rmmac; /* Remove multicast MAC address */
  2133. #endif
  2134. priv->dev.d_private = priv; /* Used to recover private state from dev */
  2135. /* Create a watchdog for timing polling for and timing of transmissions */
  2136. priv->txpoll = wd_create(); /* Create periodic poll timer */
  2137. priv->txtimeout = wd_create(); /* Create TX timeout timer */
  2138. priv->spi = spi; /* Save the SPI instance */
  2139. priv->lower = lower; /* Save the low-level MCU interface */
  2140. /* The interface should be in the down state. However, this function is called
  2141. * too early in initialization to perform the ENC28J60 reset in enc_ifdown. We
  2142. * are depending upon the fact that the application level logic will call enc_ifdown
  2143. * later to reset the ENC28J60. NOTE: The MAC address will not be set up until
  2144. * enc_ifup() is called. That gives the app time to set the MAC address before
  2145. * bringing the interface up.
  2146. */
  2147. priv->ifstate = ENCSTATE_UNINIT;
  2148. /* Attach the interrupt to the driver (but don't enable it yet) */
  2149. if (lower->attach(lower, enc_interrupt, priv) < 0)
  2150. {
  2151. /* We could not attach the ISR to the interrupt */
  2152. return -EAGAIN;
  2153. }
  2154. /* Register the device with the OS so that socket IOCTLs can be performed */
  2155. return netdev_register(&priv->dev, NET_LL_ETHERNET);
  2156. }
  2157. #endif /* CONFIG_NET && CONFIG_ENC28J60_NET */