enc28j60.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #ifndef EN28J60_H_INCLUDED
  2. #define EN28J60_H_INCLUDED
  3. #include <stdint.h>
  4. #include <rtthread.h>
  5. #include <drivers/spi.h>
  6. #include <netif/ethernetif.h>
  7. // ENC28J60 Control Registers
  8. // Control register definitions are a combination of address,
  9. // bank number, and Ethernet/MAC/PHY indicator bits.
  10. // - Register address (bits 0-4)
  11. // - Bank number (bits 5-6)
  12. // - MAC/PHY indicator (bit 7)
  13. #define ADDR_MASK 0x1F
  14. #define BANK_MASK 0x60
  15. #define SPRD_MASK 0x80
  16. // All-bank registers
  17. #define EIE 0x1B
  18. #define EIR 0x1C
  19. #define ESTAT 0x1D
  20. #define ECON2 0x1E
  21. #define ECON1 0x1F
  22. // Bank 0 registers
  23. #define ERDPTL (0x00|0x00)
  24. #define ERDPTH (0x01|0x00)
  25. #define EWRPTL (0x02|0x00)
  26. #define EWRPTH (0x03|0x00)
  27. #define ETXSTL (0x04|0x00)
  28. #define ETXSTH (0x05|0x00)
  29. #define ETXNDL (0x06|0x00)
  30. #define ETXNDH (0x07|0x00)
  31. #define ERXSTL (0x08|0x00)
  32. #define ERXSTH (0x09|0x00)
  33. #define ERXNDL (0x0A|0x00)
  34. #define ERXNDH (0x0B|0x00)
  35. #define ERXRDPTL (0x0C|0x00)
  36. #define ERXRDPTH (0x0D|0x00)
  37. #define ERXWRPTL (0x0E|0x00)
  38. #define ERXWRPTH (0x0F|0x00)
  39. #define EDMASTL (0x10|0x00)
  40. #define EDMASTH (0x11|0x00)
  41. #define EDMANDL (0x12|0x00)
  42. #define EDMANDH (0x13|0x00)
  43. #define EDMADSTL (0x14|0x00)
  44. #define EDMADSTH (0x15|0x00)
  45. #define EDMACSL (0x16|0x00)
  46. #define EDMACSH (0x17|0x00)
  47. // Bank 1 registers
  48. #define EHT0 (0x00|0x20)
  49. #define EHT1 (0x01|0x20)
  50. #define EHT2 (0x02|0x20)
  51. #define EHT3 (0x03|0x20)
  52. #define EHT4 (0x04|0x20)
  53. #define EHT5 (0x05|0x20)
  54. #define EHT6 (0x06|0x20)
  55. #define EHT7 (0x07|0x20)
  56. #define EPMM0 (0x08|0x20)
  57. #define EPMM1 (0x09|0x20)
  58. #define EPMM2 (0x0A|0x20)
  59. #define EPMM3 (0x0B|0x20)
  60. #define EPMM4 (0x0C|0x20)
  61. #define EPMM5 (0x0D|0x20)
  62. #define EPMM6 (0x0E|0x20)
  63. #define EPMM7 (0x0F|0x20)
  64. #define EPMCSL (0x10|0x20)
  65. #define EPMCSH (0x11|0x20)
  66. #define EPMOL (0x14|0x20)
  67. #define EPMOH (0x15|0x20)
  68. #define EWOLIE (0x16|0x20)
  69. #define EWOLIR (0x17|0x20)
  70. #define ERXFCON (0x18|0x20)
  71. #define EPKTCNT (0x19|0x20)
  72. // Bank 2 registers
  73. #define MACON1 (0x00|0x40|0x80)
  74. #define MACON2 (0x01|0x40|0x80)
  75. #define MACON3 (0x02|0x40|0x80)
  76. #define MACON4 (0x03|0x40|0x80)
  77. #define MABBIPG (0x04|0x40|0x80)
  78. #define MAIPGL (0x06|0x40|0x80)
  79. #define MAIPGH (0x07|0x40|0x80)
  80. #define MACLCON1 (0x08|0x40|0x80)
  81. #define MACLCON2 (0x09|0x40|0x80)
  82. #define MAMXFLL (0x0A|0x40|0x80)
  83. #define MAMXFLH (0x0B|0x40|0x80)
  84. #define MAPHSUP (0x0D|0x40|0x80)
  85. #define MICON (0x11|0x40|0x80)
  86. #define MICMD (0x12|0x40|0x80)
  87. #define MIREGADR (0x14|0x40|0x80)
  88. #define MIWRL (0x16|0x40|0x80)
  89. #define MIWRH (0x17|0x40|0x80)
  90. #define MIRDL (0x18|0x40|0x80)
  91. #define MIRDH (0x19|0x40|0x80)
  92. // Bank 3 registers
  93. #define MAADR1 (0x00|0x60|0x80)
  94. #define MAADR0 (0x01|0x60|0x80)
  95. #define MAADR3 (0x02|0x60|0x80)
  96. #define MAADR2 (0x03|0x60|0x80)
  97. #define MAADR5 (0x04|0x60|0x80)
  98. #define MAADR4 (0x05|0x60|0x80)
  99. #define EBSTSD (0x06|0x60)
  100. #define EBSTCON (0x07|0x60)
  101. #define EBSTCSL (0x08|0x60)
  102. #define EBSTCSH (0x09|0x60)
  103. #define MISTAT (0x0A|0x60|0x80)
  104. #define EREVID (0x12|0x60)
  105. #define ECOCON (0x15|0x60)
  106. #define EFLOCON (0x17|0x60)
  107. #define EPAUSL (0x18|0x60)
  108. #define EPAUSH (0x19|0x60)
  109. // PHY registers
  110. #define PHCON1 0x00
  111. #define PHSTAT1 0x01
  112. #define PHHID1 0x02
  113. #define PHHID2 0x03
  114. #define PHCON2 0x10
  115. #define PHSTAT2 0x11
  116. #define PHIE 0x12
  117. #define PHIR 0x13
  118. #define PHLCON 0x14
  119. // ENC28J60 ERXFCON Register Bit Definitions
  120. #define ERXFCON_UCEN 0x80
  121. #define ERXFCON_ANDOR 0x40
  122. #define ERXFCON_CRCEN 0x20
  123. #define ERXFCON_PMEN 0x10
  124. #define ERXFCON_MPEN 0x08
  125. #define ERXFCON_HTEN 0x04
  126. #define ERXFCON_MCEN 0x02
  127. #define ERXFCON_BCEN 0x01
  128. // ENC28J60 EIE Register Bit Definitions
  129. #define EIE_INTIE 0x80
  130. #define EIE_PKTIE 0x40
  131. #define EIE_DMAIE 0x20
  132. #define EIE_LINKIE 0x10
  133. #define EIE_TXIE 0x08
  134. #define EIE_WOLIE 0x04
  135. #define EIE_TXERIE 0x02
  136. #define EIE_RXERIE 0x01
  137. // ENC28J60 EIR Register Bit Definitions
  138. #define EIR_PKTIF 0x40
  139. #define EIR_DMAIF 0x20
  140. #define EIR_LINKIF 0x10
  141. #define EIR_TXIF 0x08
  142. #define EIR_WOLIF 0x04
  143. #define EIR_TXERIF 0x02
  144. #define EIR_RXERIF 0x01
  145. // ENC28J60 ESTAT Register Bit Definitions
  146. #define ESTAT_INT 0x80
  147. #define ESTAT_LATECOL 0x10
  148. #define ESTAT_RXBUSY 0x04
  149. #define ESTAT_TXABRT 0x02
  150. #define ESTAT_CLKRDY 0x01
  151. // ENC28J60 ECON2 Register Bit Definitions
  152. #define ECON2_AUTOINC 0x80
  153. #define ECON2_PKTDEC 0x40
  154. #define ECON2_PWRSV 0x20
  155. #define ECON2_VRPS 0x08
  156. // ENC28J60 ECON1 Register Bit Definitions
  157. #define ECON1_TXRST 0x80
  158. #define ECON1_RXRST 0x40
  159. #define ECON1_DMAST 0x20
  160. #define ECON1_CSUMEN 0x10
  161. #define ECON1_TXRTS 0x08
  162. #define ECON1_RXEN 0x04
  163. #define ECON1_BSEL1 0x02
  164. #define ECON1_BSEL0 0x01
  165. // ENC28J60 MACON1 Register Bit Definitions
  166. #define MACON1_LOOPBK 0x10
  167. #define MACON1_TXPAUS 0x08
  168. #define MACON1_RXPAUS 0x04
  169. #define MACON1_PASSALL 0x02
  170. #define MACON1_MARXEN 0x01
  171. // ENC28J60 MACON2 Register Bit Definitions
  172. #define MACON2_MARST 0x80
  173. #define MACON2_RNDRST 0x40
  174. #define MACON2_MARXRST 0x08
  175. #define MACON2_RFUNRST 0x04
  176. #define MACON2_MATXRST 0x02
  177. #define MACON2_TFUNRST 0x01
  178. // ENC28J60 MACON3 Register Bit Definitions
  179. #define MACON3_PADCFG2 0x80
  180. #define MACON3_PADCFG1 0x40
  181. #define MACON3_PADCFG0 0x20
  182. #define MACON3_TXCRCEN 0x10
  183. #define MACON3_PHDRLEN 0x08
  184. #define MACON3_HFRMLEN 0x04
  185. #define MACON3_FRMLNEN 0x02
  186. #define MACON3_FULDPX 0x01
  187. // ENC28J60 MACON4 Register Bit Definitions
  188. #define MACON4_DEFER (1<<6)
  189. #define MACON4_BPEN (1<<5)
  190. #define MACON4_NOBKOFF (1<<4)
  191. // ENC28J60 MICMD Register Bit Definitions
  192. #define MICMD_MIISCAN 0x02
  193. #define MICMD_MIIRD 0x01
  194. // ENC28J60 MISTAT Register Bit Definitions
  195. #define MISTAT_NVALID 0x04
  196. #define MISTAT_SCAN 0x02
  197. #define MISTAT_BUSY 0x01
  198. // ENC28J60 PHY PHCON1 Register Bit Definitions
  199. #define PHCON1_PRST 0x8000
  200. #define PHCON1_PLOOPBK 0x4000
  201. #define PHCON1_PPWRSV 0x0800
  202. #define PHCON1_PDPXMD 0x0100
  203. // ENC28J60 PHY PHSTAT1 Register Bit Definitions
  204. #define PHSTAT1_PFDPX 0x1000
  205. #define PHSTAT1_PHDPX 0x0800
  206. #define PHSTAT1_LLSTAT 0x0004
  207. #define PHSTAT1_JBSTAT 0x0002
  208. /* ENC28J60 PHY PHSTAT2 Register Bit Definitions */
  209. #define PHSTAT2_TXSTAT (1 << 13)
  210. #define PHSTAT2_RXSTAT (1 << 12)
  211. #define PHSTAT2_COLSTAT (1 << 11)
  212. #define PHSTAT2_LSTAT (1 << 10)
  213. #define PHSTAT2_DPXSTAT (1 << 9)
  214. #define PHSTAT2_PLRITY (1 << 5)
  215. // ENC28J60 PHY PHCON2 Register Bit Definitions
  216. #define PHCON2_FRCLINK 0x4000
  217. #define PHCON2_TXDIS 0x2000
  218. #define PHCON2_JABBER 0x0400
  219. #define PHCON2_HDLDIS 0x0100
  220. /* ENC28J60 PHY PHIE Register Bit Definitions */
  221. #define PHIE_PLNKIE (1 << 4)
  222. #define PHIE_PGEIE (1 << 1)
  223. /* ENC28J60 PHY PHIR Register Bit Definitions */
  224. #define PHIR_PLNKIF (1 << 4)
  225. #define PHIR_PGEIF (1 << 1)
  226. // ENC28J60 Packet Control Byte Bit Definitions
  227. #define PKTCTRL_PHUGEEN 0x08
  228. #define PKTCTRL_PPADEN 0x04
  229. #define PKTCTRL_PCRCEN 0x02
  230. #define PKTCTRL_POVERRIDE 0x01
  231. /* ENC28J60 Transmit Status Vector */
  232. #define TSV_TXBYTECNT 0
  233. #define TSV_TXCOLLISIONCNT 16
  234. #define TSV_TXCRCERROR 20
  235. #define TSV_TXLENCHKERROR 21
  236. #define TSV_TXLENOUTOFRANGE 22
  237. #define TSV_TXDONE 23
  238. #define TSV_TXMULTICAST 24
  239. #define TSV_TXBROADCAST 25
  240. #define TSV_TXPACKETDEFER 26
  241. #define TSV_TXEXDEFER 27
  242. #define TSV_TXEXCOLLISION 28
  243. #define TSV_TXLATECOLLISION 29
  244. #define TSV_TXGIANT 30
  245. #define TSV_TXUNDERRUN 31
  246. #define TSV_TOTBYTETXONWIRE 32
  247. #define TSV_TXCONTROLFRAME 48
  248. #define TSV_TXPAUSEFRAME 49
  249. #define TSV_BACKPRESSUREAPP 50
  250. #define TSV_TXVLANTAGFRAME 51
  251. #define TSV_SIZE 7
  252. #define TSV_BYTEOF(x) ((x) / 8)
  253. #define TSV_BITMASK(x) (1 << ((x) % 8))
  254. #define TSV_GETBIT(x, y) (((x)[TSV_BYTEOF(y)] & TSV_BITMASK(y)) ? 1 : 0)
  255. /* ENC28J60 Receive Status Vector */
  256. #define RSV_RXLONGEVDROPEV 16
  257. #define RSV_CARRIEREV 18
  258. #define RSV_CRCERROR 20
  259. #define RSV_LENCHECKERR 21
  260. #define RSV_LENOUTOFRANGE 22
  261. #define RSV_RXOK 23
  262. #define RSV_RXMULTICAST 24
  263. #define RSV_RXBROADCAST 25
  264. #define RSV_DRIBBLENIBBLE 26
  265. #define RSV_RXCONTROLFRAME 27
  266. #define RSV_RXPAUSEFRAME 28
  267. #define RSV_RXUNKNOWNOPCODE 29
  268. #define RSV_RXTYPEVLAN 30
  269. #define RSV_SIZE 6
  270. #define RSV_BITMASK(x) (1 << ((x) - 16))
  271. #define RSV_GETBIT(x, y) (((x) & RSV_BITMASK(y)) ? 1 : 0)
  272. // SPI operation codes
  273. #define ENC28J60_READ_CTRL_REG 0x00
  274. #define ENC28J60_READ_BUF_MEM 0x3A
  275. #define ENC28J60_WRITE_CTRL_REG 0x40
  276. #define ENC28J60_WRITE_BUF_MEM 0x7A
  277. #define ENC28J60_BIT_FIELD_SET 0x80
  278. #define ENC28J60_BIT_FIELD_CLR 0xA0
  279. #define ENC28J60_SOFT_RESET 0xFF
  280. // The RXSTART_INIT should be zero. See Rev. B4 Silicon Errata
  281. // buffer boundaries applied to internal 8K ram
  282. // the entire available packet buffer space is allocated
  283. //
  284. #define MAX_TX_PACKAGE_SIZE (1536)
  285. // start with recbuf at 0/
  286. #define RXSTART_INIT 0x0
  287. // receive buffer end
  288. #define RXSTOP_INIT (0x1FFF - MAX_TX_PACKAGE_SIZE*2) - 1
  289. // start TX buffer at 0x1FFF-0x0600, pace for one full ethernet frame (~1500 bytes)
  290. #define TXSTART_INIT (0x1FFF - MAX_TX_PACKAGE_SIZE*2)
  291. // stp TX buffer at end of mem
  292. #define TXSTOP_INIT 0x1FFF
  293. // max frame length which the conroller will accept:
  294. #define MAX_FRAMELEN 1518
  295. #define MAX_ADDR_LEN 6
  296. struct net_device
  297. {
  298. /* inherit from ethernet device */
  299. struct eth_device parent;
  300. /* interface address info. */
  301. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  302. rt_uint8_t emac_rev;
  303. rt_uint8_t phy_rev;
  304. rt_uint8_t phy_pn;
  305. rt_uint32_t phy_id;
  306. /* spi device */
  307. struct rt_spi_device *spi_device;
  308. struct rt_mutex lock;
  309. };
  310. /* export function */
  311. extern rt_err_t enc28j60_attach(const char *spi_device_name);
  312. extern void enc28j60_isr(void);
  313. #endif // EN28J60_H_INCLUDED