enc28j60.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation; either version 2 of
  5. * the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  15. * MA 02111-1307 USA
  16. */
  17. #include <config.h>
  18. #include <common.h>
  19. #ifdef CONFIG_ENC28J60
  20. #include <net.h>
  21. #include <asm/arch/hardware.h>
  22. #include <asm/arch/spi.h>
  23. /*
  24. * Control Registers in Bank 0
  25. */
  26. #define CTL_REG_ERDPTL 0x00
  27. #define CTL_REG_ERDPTH 0x01
  28. #define CTL_REG_EWRPTL 0x02
  29. #define CTL_REG_EWRPTH 0x03
  30. #define CTL_REG_ETXSTL 0x04
  31. #define CTL_REG_ETXSTH 0x05
  32. #define CTL_REG_ETXNDL 0x06
  33. #define CTL_REG_ETXNDH 0x07
  34. #define CTL_REG_ERXSTL 0x08
  35. #define CTL_REG_ERXSTH 0x09
  36. #define CTL_REG_ERXNDL 0x0A
  37. #define CTL_REG_ERXNDH 0x0B
  38. #define CTL_REG_ERXRDPTL 0x0C
  39. #define CTL_REG_ERXRDPTH 0x0D
  40. #define CTL_REG_ERXWRPTL 0x0E
  41. #define CTL_REG_ERXWRPTH 0x0F
  42. #define CTL_REG_EDMASTL 0x10
  43. #define CTL_REG_EDMASTH 0x11
  44. #define CTL_REG_EDMANDL 0x12
  45. #define CTL_REG_EDMANDH 0x13
  46. #define CTL_REG_EDMADSTL 0x14
  47. #define CTL_REG_EDMADSTH 0x15
  48. #define CTL_REG_EDMACSL 0x16
  49. #define CTL_REG_EDMACSH 0x17
  50. /* these are common in all banks */
  51. #define CTL_REG_EIE 0x1B
  52. #define CTL_REG_EIR 0x1C
  53. #define CTL_REG_ESTAT 0x1D
  54. #define CTL_REG_ECON2 0x1E
  55. #define CTL_REG_ECON1 0x1F
  56. /*
  57. * Control Registers in Bank 1
  58. */
  59. #define CTL_REG_EHT0 0x00
  60. #define CTL_REG_EHT1 0x01
  61. #define CTL_REG_EHT2 0x02
  62. #define CTL_REG_EHT3 0x03
  63. #define CTL_REG_EHT4 0x04
  64. #define CTL_REG_EHT5 0x05
  65. #define CTL_REG_EHT6 0x06
  66. #define CTL_REG_EHT7 0x07
  67. #define CTL_REG_EPMM0 0x08
  68. #define CTL_REG_EPMM1 0x09
  69. #define CTL_REG_EPMM2 0x0A
  70. #define CTL_REG_EPMM3 0x0B
  71. #define CTL_REG_EPMM4 0x0C
  72. #define CTL_REG_EPMM5 0x0D
  73. #define CTL_REG_EPMM6 0x0E
  74. #define CTL_REG_EPMM7 0x0F
  75. #define CTL_REG_EPMCSL 0x10
  76. #define CTL_REG_EPMCSH 0x11
  77. #define CTL_REG_EPMOL 0x14
  78. #define CTL_REG_EPMOH 0x15
  79. #define CTL_REG_EWOLIE 0x16
  80. #define CTL_REG_EWOLIR 0x17
  81. #define CTL_REG_ERXFCON 0x18
  82. #define CTL_REG_EPKTCNT 0x19
  83. /*
  84. * Control Registers in Bank 2
  85. */
  86. #define CTL_REG_MACON1 0x00
  87. #define CTL_REG_MACON2 0x01
  88. #define CTL_REG_MACON3 0x02
  89. #define CTL_REG_MACON4 0x03
  90. #define CTL_REG_MABBIPG 0x04
  91. #define CTL_REG_MAIPGL 0x06
  92. #define CTL_REG_MAIPGH 0x07
  93. #define CTL_REG_MACLCON1 0x08
  94. #define CTL_REG_MACLCON2 0x09
  95. #define CTL_REG_MAMXFLL 0x0A
  96. #define CTL_REG_MAMXFLH 0x0B
  97. #define CTL_REG_MAPHSUP 0x0D
  98. #define CTL_REG_MICON 0x11
  99. #define CTL_REG_MICMD 0x12
  100. #define CTL_REG_MIREGADR 0x14
  101. #define CTL_REG_MIWRL 0x16
  102. #define CTL_REG_MIWRH 0x17
  103. #define CTL_REG_MIRDL 0x18
  104. #define CTL_REG_MIRDH 0x19
  105. /*
  106. * Control Registers in Bank 3
  107. */
  108. #define CTL_REG_MAADR1 0x00
  109. #define CTL_REG_MAADR0 0x01
  110. #define CTL_REG_MAADR3 0x02
  111. #define CTL_REG_MAADR2 0x03
  112. #define CTL_REG_MAADR5 0x04
  113. #define CTL_REG_MAADR4 0x05
  114. #define CTL_REG_EBSTSD 0x06
  115. #define CTL_REG_EBSTCON 0x07
  116. #define CTL_REG_EBSTCSL 0x08
  117. #define CTL_REG_EBSTCSH 0x09
  118. #define CTL_REG_MISTAT 0x0A
  119. #define CTL_REG_EREVID 0x12
  120. #define CTL_REG_ECOCON 0x15
  121. #define CTL_REG_EFLOCON 0x17
  122. #define CTL_REG_EPAUSL 0x18
  123. #define CTL_REG_EPAUSH 0x19
  124. /*
  125. * PHY Register
  126. */
  127. #define PHY_REG_PHID1 0x02
  128. #define PHY_REG_PHID2 0x03
  129. /* taken from the Linux driver */
  130. #define PHY_REG_PHCON1 0x00
  131. #define PHY_REG_PHCON2 0x10
  132. #define PHY_REG_PHLCON 0x14
  133. /*
  134. * Receive Filter Register (ERXFCON) bits
  135. */
  136. #define ENC_RFR_UCEN 0x80
  137. #define ENC_RFR_ANDOR 0x40
  138. #define ENC_RFR_CRCEN 0x20
  139. #define ENC_RFR_PMEN 0x10
  140. #define ENC_RFR_MPEN 0x08
  141. #define ENC_RFR_HTEN 0x04
  142. #define ENC_RFR_MCEN 0x02
  143. #define ENC_RFR_BCEN 0x01
  144. /*
  145. * ECON1 Register Bits
  146. */
  147. #define ENC_ECON1_TXRST 0x80
  148. #define ENC_ECON1_RXRST 0x40
  149. #define ENC_ECON1_DMAST 0x20
  150. #define ENC_ECON1_CSUMEN 0x10
  151. #define ENC_ECON1_TXRTS 0x08
  152. #define ENC_ECON1_RXEN 0x04
  153. #define ENC_ECON1_BSEL1 0x02
  154. #define ENC_ECON1_BSEL0 0x01
  155. /*
  156. * ECON2 Register Bits
  157. */
  158. #define ENC_ECON2_AUTOINC 0x80
  159. #define ENC_ECON2_PKTDEC 0x40
  160. #define ENC_ECON2_PWRSV 0x20
  161. #define ENC_ECON2_VRPS 0x08
  162. /*
  163. * EIR Register Bits
  164. */
  165. #define ENC_EIR_PKTIF 0x40
  166. #define ENC_EIR_DMAIF 0x20
  167. #define ENC_EIR_LINKIF 0x10
  168. #define ENC_EIR_TXIF 0x08
  169. #define ENC_EIR_WOLIF 0x04
  170. #define ENC_EIR_TXERIF 0x02
  171. #define ENC_EIR_RXERIF 0x01
  172. /*
  173. * ESTAT Register Bits
  174. */
  175. #define ENC_ESTAT_INT 0x80
  176. #define ENC_ESTAT_LATECOL 0x10
  177. #define ENC_ESTAT_RXBUSY 0x04
  178. #define ENC_ESTAT_TXABRT 0x02
  179. #define ENC_ESTAT_CLKRDY 0x01
  180. /*
  181. * EIE Register Bits
  182. */
  183. #define ENC_EIE_INTIE 0x80
  184. #define ENC_EIE_PKTIE 0x40
  185. #define ENC_EIE_DMAIE 0x20
  186. #define ENC_EIE_LINKIE 0x10
  187. #define ENC_EIE_TXIE 0x08
  188. #define ENC_EIE_WOLIE 0x04
  189. #define ENC_EIE_TXERIE 0x02
  190. #define ENC_EIE_RXERIE 0x01
  191. /*
  192. * MACON1 Register Bits
  193. */
  194. #define ENC_MACON1_LOOPBK 0x10
  195. #define ENC_MACON1_TXPAUS 0x08
  196. #define ENC_MACON1_RXPAUS 0x04
  197. #define ENC_MACON1_PASSALL 0x02
  198. #define ENC_MACON1_MARXEN 0x01
  199. /*
  200. * MACON2 Register Bits
  201. */
  202. #define ENC_MACON2_MARST 0x80
  203. #define ENC_MACON2_RNDRST 0x40
  204. #define ENC_MACON2_MARXRST 0x08
  205. #define ENC_MACON2_RFUNRST 0x04
  206. #define ENC_MACON2_MATXRST 0x02
  207. #define ENC_MACON2_TFUNRST 0x01
  208. /*
  209. * MACON3 Register Bits
  210. */
  211. #define ENC_MACON3_PADCFG2 0x80
  212. #define ENC_MACON3_PADCFG1 0x40
  213. #define ENC_MACON3_PADCFG0 0x20
  214. #define ENC_MACON3_TXCRCEN 0x10
  215. #define ENC_MACON3_PHDRLEN 0x08
  216. #define ENC_MACON3_HFRMEN 0x04
  217. #define ENC_MACON3_FRMLNEN 0x02
  218. #define ENC_MACON3_FULDPX 0x01
  219. /*
  220. * MICMD Register Bits
  221. */
  222. #define ENC_MICMD_MIISCAN 0x02
  223. #define ENC_MICMD_MIIRD 0x01
  224. /*
  225. * MISTAT Register Bits
  226. */
  227. #define ENC_MISTAT_NVALID 0x04
  228. #define ENC_MISTAT_SCAN 0x02
  229. #define ENC_MISTAT_BUSY 0x01
  230. /*
  231. * PHID1 and PHID2 values
  232. */
  233. #define ENC_PHID1_VALUE 0x0083
  234. #define ENC_PHID2_VALUE 0x1400
  235. #define ENC_PHID2_MASK 0xFC00
  236. #define ENC_SPI_SLAVE_CS 0x00010000 /* pin P1.16 */
  237. #define ENC_RESET 0x00020000 /* pin P1.17 */
  238. #define FAILSAFE_VALUE 5000
  239. /*
  240. * Controller memory layout:
  241. *
  242. * 0x0000 - 0x17ff 6k bytes receive buffer
  243. * 0x1800 - 0x1fff 2k bytes transmit buffer
  244. */
  245. /* Use the lower memory for receiver buffer. See errata pt. 5 */
  246. #define ENC_RX_BUF_START 0x0000
  247. #define ENC_TX_BUF_START 0x1800
  248. /* taken from the Linux driver */
  249. #define ENC_RX_BUF_END 0x17ff
  250. #define ENC_TX_BUF_END 0x1fff
  251. /* maximum frame length */
  252. #define ENC_MAX_FRM_LEN 1518
  253. #define enc_enable() PUT32(IO1CLR, ENC_SPI_SLAVE_CS)
  254. #define enc_disable() PUT32(IO1SET, ENC_SPI_SLAVE_CS)
  255. #define enc_cfg_spi() spi_set_cfg(0, 0, 0); spi_set_clock(8);
  256. static unsigned char encReadReg (unsigned char regNo);
  257. static void encWriteReg (unsigned char regNo, unsigned char data);
  258. static void encWriteRegRetry (unsigned char regNo, unsigned char data, int c);
  259. static void encReadBuff (unsigned short length, unsigned char *pBuff);
  260. static void encWriteBuff (unsigned short length, unsigned char *pBuff);
  261. static void encBitSet (unsigned char regNo, unsigned char data);
  262. static void encBitClr (unsigned char regNo, unsigned char data);
  263. static void encReset (void);
  264. static void encInit (unsigned char *pEthAddr);
  265. static unsigned short phyRead (unsigned char addr);
  266. static void phyWrite(unsigned char, unsigned short);
  267. static void encPoll (void);
  268. static void encRx (void);
  269. #define m_nic_read(reg) encReadReg(reg)
  270. #define m_nic_write(reg, data) encWriteReg(reg, data)
  271. #define m_nic_write_retry(reg, data, count) encWriteRegRetry(reg, data, count)
  272. #define m_nic_read_data(len, buf) encReadBuff((len), (buf))
  273. #define m_nic_write_data(len, buf) encWriteBuff((len), (buf))
  274. /* bit field set */
  275. #define m_nic_bfs(reg, data) encBitSet(reg, data)
  276. /* bit field clear */
  277. #define m_nic_bfc(reg, data) encBitClr(reg, data)
  278. static unsigned char bank = 0; /* current bank in enc28j60 */
  279. static unsigned char next_pointer_lsb;
  280. static unsigned char next_pointer_msb;
  281. static unsigned char buffer[ENC_MAX_FRM_LEN];
  282. static int rxResetCounter = 0;
  283. #define RX_RESET_COUNTER 1000;
  284. /*-----------------------------------------------------------------------------
  285. * Always returns 0
  286. */
  287. int eth_init (bd_t * bis)
  288. {
  289. unsigned char estatVal;
  290. /* configure GPIO */
  291. (*((volatile unsigned long *) IO1DIR)) |= ENC_SPI_SLAVE_CS;
  292. (*((volatile unsigned long *) IO1DIR)) |= ENC_RESET;
  293. /* CS and RESET active low */
  294. PUT32 (IO1SET, ENC_SPI_SLAVE_CS);
  295. PUT32 (IO1SET, ENC_RESET);
  296. spi_init ();
  297. /* taken from the Linux driver - dangerous stuff here! */
  298. /* Wait for CLKRDY to become set (i.e., check that we can communicate with
  299. the ENC) */
  300. do
  301. {
  302. estatVal = m_nic_read(CTL_REG_ESTAT);
  303. } while ((estatVal & 0x08) || (~estatVal & ENC_ESTAT_CLKRDY));
  304. /* initialize controller */
  305. encReset ();
  306. encInit (bis->bi_enetaddr);
  307. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_RXEN); /* enable receive */
  308. return 0;
  309. }
  310. int eth_send (volatile void *packet, int length)
  311. {
  312. /* check frame length, etc. */
  313. /* TODO: */
  314. /* switch to bank 0 */
  315. m_nic_bfc (CTL_REG_ECON1, (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  316. /* set EWRPT */
  317. m_nic_write (CTL_REG_EWRPTL, (ENC_TX_BUF_START & 0xff));
  318. m_nic_write (CTL_REG_EWRPTH, (ENC_TX_BUF_START >> 8));
  319. /* set ETXND */
  320. m_nic_write (CTL_REG_ETXNDL, (length + ENC_TX_BUF_START) & 0xFF);
  321. m_nic_write (CTL_REG_ETXNDH, (length + ENC_TX_BUF_START) >> 8);
  322. /* set ETXST */
  323. m_nic_write (CTL_REG_ETXSTL, ENC_TX_BUF_START & 0xFF);
  324. m_nic_write (CTL_REG_ETXSTH, ENC_TX_BUF_START >> 8);
  325. /* write packet */
  326. m_nic_write_data (length, (unsigned char *) packet);
  327. /* taken from the Linux driver */
  328. /* Verify that the internal transmit logic has not been altered by excessive
  329. collisions. See Errata B4 12 and 14.
  330. */
  331. if (m_nic_read(CTL_REG_EIR) & ENC_EIR_TXERIF) {
  332. m_nic_bfs(CTL_REG_ECON1, ENC_ECON1_TXRST);
  333. m_nic_bfc(CTL_REG_ECON1, ENC_ECON1_TXRST);
  334. }
  335. m_nic_bfc(CTL_REG_EIR, (ENC_EIR_TXERIF | ENC_EIR_TXIF));
  336. /* set ECON1.TXRTS */
  337. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_TXRTS);
  338. return 0;
  339. }
  340. /*****************************************************************************
  341. * This function resets the receiver only. This function may be called from
  342. * interrupt-context.
  343. */
  344. static void encReceiverReset (void)
  345. {
  346. unsigned char econ1;
  347. econ1 = m_nic_read (CTL_REG_ECON1);
  348. if ((econ1 & ENC_ECON1_RXRST) == 0) {
  349. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_RXRST);
  350. rxResetCounter = RX_RESET_COUNTER;
  351. }
  352. }
  353. /*****************************************************************************
  354. * receiver reset timer
  355. */
  356. static void encReceiverResetCallback (void)
  357. {
  358. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_RXRST);
  359. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_RXEN); /* enable receive */
  360. }
  361. /*-----------------------------------------------------------------------------
  362. * Check for received packets. Call NetReceive for each packet. The return
  363. * value is ignored by the caller.
  364. */
  365. int eth_rx (void)
  366. {
  367. if (rxResetCounter > 0 && --rxResetCounter == 0) {
  368. encReceiverResetCallback ();
  369. }
  370. encPoll ();
  371. return 0;
  372. }
  373. void eth_halt (void)
  374. {
  375. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_RXEN); /* disable receive */
  376. }
  377. /*****************************************************************************/
  378. static void encPoll (void)
  379. {
  380. unsigned char eir_reg;
  381. volatile unsigned char estat_reg;
  382. unsigned char pkt_cnt;
  383. #ifdef CONFIG_USE_IRQ
  384. /* clear global interrupt enable bit in enc28j60 */
  385. m_nic_bfc (CTL_REG_EIE, ENC_EIE_INTIE);
  386. #endif
  387. estat_reg = m_nic_read (CTL_REG_ESTAT);
  388. eir_reg = m_nic_read (CTL_REG_EIR);
  389. if (eir_reg & ENC_EIR_TXIF) {
  390. /* clear TXIF bit in EIR */
  391. m_nic_bfc (CTL_REG_EIR, ENC_EIR_TXIF);
  392. }
  393. /* We have to use pktcnt and not pktif bit, see errata pt. 6 */
  394. /* move to bank 1 */
  395. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  396. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  397. /* read pktcnt */
  398. pkt_cnt = m_nic_read (CTL_REG_EPKTCNT);
  399. if (pkt_cnt > 0) {
  400. if ((eir_reg & ENC_EIR_PKTIF) == 0) {
  401. /*printf("encPoll: pkt cnt > 0, but pktif not set\n"); */
  402. }
  403. encRx ();
  404. /* clear PKTIF bit in EIR, this should not need to be done but it
  405. seems like we get problems if we do not */
  406. m_nic_bfc (CTL_REG_EIR, ENC_EIR_PKTIF);
  407. }
  408. if (eir_reg & ENC_EIR_RXERIF) {
  409. printf ("encPoll: rx error\n");
  410. m_nic_bfc (CTL_REG_EIR, ENC_EIR_RXERIF);
  411. }
  412. if (eir_reg & ENC_EIR_TXERIF) {
  413. printf ("encPoll: tx error\n");
  414. m_nic_bfc (CTL_REG_EIR, ENC_EIR_TXERIF);
  415. }
  416. #ifdef CONFIG_USE_IRQ
  417. /* set global interrupt enable bit in enc28j60 */
  418. m_nic_bfs (CTL_REG_EIE, ENC_EIE_INTIE);
  419. #endif
  420. }
  421. static void encRx (void)
  422. {
  423. unsigned short pkt_len;
  424. unsigned short copy_len;
  425. unsigned short status;
  426. unsigned char eir_reg;
  427. unsigned char pkt_cnt = 0;
  428. unsigned short rxbuf_rdpt;
  429. /* switch to bank 0 */
  430. m_nic_bfc (CTL_REG_ECON1, (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  431. m_nic_write (CTL_REG_ERDPTL, next_pointer_lsb);
  432. m_nic_write (CTL_REG_ERDPTH, next_pointer_msb);
  433. do {
  434. m_nic_read_data (6, buffer);
  435. next_pointer_lsb = buffer[0];
  436. next_pointer_msb = buffer[1];
  437. pkt_len = buffer[2];
  438. pkt_len |= (unsigned short) buffer[3] << 8;
  439. status = buffer[4];
  440. status |= (unsigned short) buffer[5] << 8;
  441. if (pkt_len <= ENC_MAX_FRM_LEN)
  442. copy_len = pkt_len;
  443. else
  444. copy_len = 0;
  445. if ((status & (1L << 7)) == 0) /* check Received Ok bit */
  446. copy_len = 0;
  447. /* taken from the Linux driver */
  448. /* check if next pointer is resonable */
  449. if ((((unsigned int)next_pointer_msb << 8) |
  450. (unsigned int)next_pointer_lsb) >= ENC_TX_BUF_START)
  451. copy_len = 0;
  452. if (copy_len > 0) {
  453. m_nic_read_data (copy_len, buffer);
  454. }
  455. /* advance read pointer to next pointer */
  456. m_nic_write (CTL_REG_ERDPTL, next_pointer_lsb);
  457. m_nic_write (CTL_REG_ERDPTH, next_pointer_msb);
  458. /* decrease packet counter */
  459. m_nic_bfs (CTL_REG_ECON2, ENC_ECON2_PKTDEC);
  460. /* taken from the Linux driver */
  461. /* Only odd values should be written to ERXRDPTL,
  462. * see errata B4 pt.13
  463. */
  464. rxbuf_rdpt = (next_pointer_msb << 8 | next_pointer_lsb) - 1;
  465. if ((rxbuf_rdpt < (m_nic_read(CTL_REG_ERXSTH) << 8 |
  466. m_nic_read(CTL_REG_ERXSTL))) || (rxbuf_rdpt >
  467. (m_nic_read(CTL_REG_ERXNDH) << 8 |
  468. m_nic_read(CTL_REG_ERXNDL)))) {
  469. m_nic_write(CTL_REG_ERXRDPTL, m_nic_read(CTL_REG_ERXNDL));
  470. m_nic_write(CTL_REG_ERXRDPTH, m_nic_read(CTL_REG_ERXNDH));
  471. } else {
  472. m_nic_write(CTL_REG_ERXRDPTL, rxbuf_rdpt & 0xFF);
  473. m_nic_write(CTL_REG_ERXRDPTH, rxbuf_rdpt >> 8);
  474. }
  475. /* move to bank 1 */
  476. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  477. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  478. /* read pktcnt */
  479. pkt_cnt = m_nic_read (CTL_REG_EPKTCNT);
  480. /* switch to bank 0 */
  481. m_nic_bfc (CTL_REG_ECON1,
  482. (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  483. if (copy_len == 0) {
  484. eir_reg = m_nic_read (CTL_REG_EIR);
  485. encReceiverReset ();
  486. printf ("eth_rx: copy_len=0\n");
  487. continue;
  488. }
  489. NetReceive ((unsigned char *) buffer, pkt_len);
  490. eir_reg = m_nic_read (CTL_REG_EIR);
  491. } while (pkt_cnt); /* Use EPKTCNT not EIR.PKTIF flag, see errata pt. 6 */
  492. }
  493. static void encWriteReg (unsigned char regNo, unsigned char data)
  494. {
  495. spi_lock ();
  496. enc_cfg_spi ();
  497. enc_enable ();
  498. spi_write (0x40 | regNo); /* write in regNo */
  499. spi_write (data);
  500. enc_disable ();
  501. enc_enable ();
  502. spi_write (0x1f); /* write reg 0x1f */
  503. enc_disable ();
  504. spi_unlock ();
  505. }
  506. static void encWriteRegRetry (unsigned char regNo, unsigned char data, int c)
  507. {
  508. unsigned char readback;
  509. int i;
  510. spi_lock ();
  511. for (i = 0; i < c; i++) {
  512. enc_cfg_spi ();
  513. enc_enable ();
  514. spi_write (0x40 | regNo); /* write in regNo */
  515. spi_write (data);
  516. enc_disable ();
  517. enc_enable ();
  518. spi_write (0x1f); /* write reg 0x1f */
  519. enc_disable ();
  520. spi_unlock (); /* we must unlock spi first */
  521. readback = encReadReg (regNo);
  522. spi_lock ();
  523. if (readback == data)
  524. break;
  525. }
  526. spi_unlock ();
  527. if (i == c) {
  528. printf ("enc28j60: write reg %d failed\n", regNo);
  529. }
  530. }
  531. static unsigned char encReadReg (unsigned char regNo)
  532. {
  533. unsigned char rxByte;
  534. spi_lock ();
  535. enc_cfg_spi ();
  536. enc_enable ();
  537. spi_write (0x1f); /* read reg 0x1f */
  538. bank = spi_read () & 0x3;
  539. enc_disable ();
  540. enc_enable ();
  541. spi_write (regNo);
  542. rxByte = spi_read ();
  543. /* check if MAC or MII register */
  544. if (((bank == 2) && (regNo <= 0x1a)) ||
  545. ((bank == 3) && (regNo <= 0x05 || regNo == 0x0a))) {
  546. /* ignore first byte and read another byte */
  547. rxByte = spi_read ();
  548. }
  549. enc_disable ();
  550. spi_unlock ();
  551. return rxByte;
  552. }
  553. static void encReadBuff (unsigned short length, unsigned char *pBuff)
  554. {
  555. spi_lock ();
  556. enc_cfg_spi ();
  557. enc_enable ();
  558. spi_write (0x20 | 0x1a); /* read buffer memory */
  559. while (length--) {
  560. if (pBuff != NULL)
  561. *pBuff++ = spi_read ();
  562. else
  563. spi_write (0);
  564. }
  565. enc_disable ();
  566. spi_unlock ();
  567. }
  568. static void encWriteBuff (unsigned short length, unsigned char *pBuff)
  569. {
  570. spi_lock ();
  571. enc_cfg_spi ();
  572. enc_enable ();
  573. spi_write (0x60 | 0x1a); /* write buffer memory */
  574. spi_write (0x00); /* control byte */
  575. while (length--)
  576. spi_write (*pBuff++);
  577. enc_disable ();
  578. spi_unlock ();
  579. }
  580. static void encBitSet (unsigned char regNo, unsigned char data)
  581. {
  582. spi_lock ();
  583. enc_cfg_spi ();
  584. enc_enable ();
  585. spi_write (0x80 | regNo); /* bit field set */
  586. spi_write (data);
  587. enc_disable ();
  588. spi_unlock ();
  589. }
  590. static void encBitClr (unsigned char regNo, unsigned char data)
  591. {
  592. spi_lock ();
  593. enc_cfg_spi ();
  594. enc_enable ();
  595. spi_write (0xA0 | regNo); /* bit field clear */
  596. spi_write (data);
  597. enc_disable ();
  598. spi_unlock ();
  599. }
  600. static void encReset (void)
  601. {
  602. spi_lock ();
  603. enc_cfg_spi ();
  604. enc_enable ();
  605. spi_write (0xff); /* soft reset */
  606. enc_disable ();
  607. spi_unlock ();
  608. /* sleep 1 ms. See errata pt. 2 */
  609. udelay (1000);
  610. }
  611. static void encInit (unsigned char *pEthAddr)
  612. {
  613. unsigned short phid1 = 0;
  614. unsigned short phid2 = 0;
  615. /* switch to bank 0 */
  616. m_nic_bfc (CTL_REG_ECON1, (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  617. /*
  618. * Setup the buffer space. The reset values are valid for the
  619. * other pointers.
  620. */
  621. /* We shall not write to ERXST, see errata pt. 5. Instead we
  622. have to make sure that ENC_RX_BUS_START is 0. */
  623. m_nic_write_retry (CTL_REG_ERXSTL, (ENC_RX_BUF_START & 0xFF), 1);
  624. m_nic_write_retry (CTL_REG_ERXSTH, (ENC_RX_BUF_START >> 8), 1);
  625. /* taken from the Linux driver */
  626. m_nic_write_retry (CTL_REG_ERXNDL, (ENC_RX_BUF_END & 0xFF), 1);
  627. m_nic_write_retry (CTL_REG_ERXNDH, (ENC_RX_BUF_END >> 8), 1);
  628. m_nic_write_retry (CTL_REG_ERDPTL, (ENC_RX_BUF_START & 0xFF), 1);
  629. m_nic_write_retry (CTL_REG_ERDPTH, (ENC_RX_BUF_START >> 8), 1);
  630. next_pointer_lsb = (ENC_RX_BUF_START & 0xFF);
  631. next_pointer_msb = (ENC_RX_BUF_START >> 8);
  632. /* verify identification */
  633. phid1 = phyRead (PHY_REG_PHID1);
  634. phid2 = phyRead (PHY_REG_PHID2);
  635. if (phid1 != ENC_PHID1_VALUE
  636. || (phid2 & ENC_PHID2_MASK) != ENC_PHID2_VALUE) {
  637. printf ("ERROR: failed to identify controller\n");
  638. printf ("phid1 = %x, phid2 = %x\n",
  639. phid1, (phid2 & ENC_PHID2_MASK));
  640. printf ("should be phid1 = %x, phid2 = %x\n",
  641. ENC_PHID1_VALUE, ENC_PHID2_VALUE);
  642. }
  643. /*
  644. * --- MAC Initialization ---
  645. */
  646. /* Pull MAC out of Reset */
  647. /* switch to bank 2 */
  648. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  649. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  650. /* enable MAC to receive frames */
  651. /* added some bits from the Linux driver */
  652. m_nic_write_retry (CTL_REG_MACON1
  653. ,(ENC_MACON1_MARXEN | ENC_MACON1_TXPAUS | ENC_MACON1_RXPAUS)
  654. ,10);
  655. /* configure pad, tx-crc and duplex */
  656. /* added a bit from the Linux driver */
  657. m_nic_write_retry (CTL_REG_MACON3
  658. ,(ENC_MACON3_PADCFG0 | ENC_MACON3_TXCRCEN | ENC_MACON3_FRMLNEN)
  659. ,10);
  660. /* added 4 new lines from the Linux driver */
  661. /* Allow infinite deferals if the medium is continously busy */
  662. m_nic_write_retry(CTL_REG_MACON4, (1<<6) /*ENC_MACON4_DEFER*/, 10);
  663. /* Late collisions occur beyond 63 bytes */
  664. m_nic_write_retry(CTL_REG_MACLCON2, 63, 10);
  665. /* Set (low byte) Non-Back-to_Back Inter-Packet Gap. Recommended 0x12 */
  666. m_nic_write_retry(CTL_REG_MAIPGL, 0x12, 10);
  667. /*
  668. * Set (high byte) Non-Back-to_Back Inter-Packet Gap. Recommended
  669. * 0x0c for half-duplex. Nothing for full-duplex
  670. */
  671. m_nic_write_retry(CTL_REG_MAIPGH, 0x0C, 10);
  672. /* set maximum frame length */
  673. m_nic_write_retry (CTL_REG_MAMXFLL, (ENC_MAX_FRM_LEN & 0xff), 10);
  674. m_nic_write_retry (CTL_REG_MAMXFLH, (ENC_MAX_FRM_LEN >> 8), 10);
  675. /*
  676. * Set MAC back-to-back inter-packet gap. Recommended 0x12 for half duplex
  677. * and 0x15 for full duplex.
  678. */
  679. m_nic_write_retry (CTL_REG_MABBIPG, 0x12, 10);
  680. /* set MAC address */
  681. /* switch to bank 3 */
  682. m_nic_bfs (CTL_REG_ECON1, (ENC_ECON1_BSEL0 | ENC_ECON1_BSEL1));
  683. m_nic_write_retry (CTL_REG_MAADR0, pEthAddr[5], 1);
  684. m_nic_write_retry (CTL_REG_MAADR1, pEthAddr[4], 1);
  685. m_nic_write_retry (CTL_REG_MAADR2, pEthAddr[3], 1);
  686. m_nic_write_retry (CTL_REG_MAADR3, pEthAddr[2], 1);
  687. m_nic_write_retry (CTL_REG_MAADR4, pEthAddr[1], 1);
  688. m_nic_write_retry (CTL_REG_MAADR5, pEthAddr[0], 1);
  689. /*
  690. * PHY Initialization taken from the Linux driver
  691. */
  692. /* Prevent automatic loopback of data beeing transmitted by setting
  693. ENC_PHCON2_HDLDIS */
  694. phyWrite(PHY_REG_PHCON2, (1<<8));
  695. /* LEDs configuration
  696. * LEDA: LACFG = 0100 -> display link status
  697. * LEDB: LBCFG = 0111 -> display TX & RX activity
  698. * STRCH = 1 -> LED pulses
  699. */
  700. phyWrite(PHY_REG_PHLCON, 0x0472);
  701. /* Reset PDPXMD-bit => half duplex */
  702. phyWrite(PHY_REG_PHCON1, 0);
  703. /*
  704. * Receive settings
  705. */
  706. #ifdef CONFIG_USE_IRQ
  707. /* enable interrupts */
  708. m_nic_bfs (CTL_REG_EIE, ENC_EIE_PKTIE);
  709. m_nic_bfs (CTL_REG_EIE, ENC_EIE_TXIE);
  710. m_nic_bfs (CTL_REG_EIE, ENC_EIE_RXERIE);
  711. m_nic_bfs (CTL_REG_EIE, ENC_EIE_TXERIE);
  712. m_nic_bfs (CTL_REG_EIE, ENC_EIE_INTIE);
  713. #endif
  714. }
  715. /*****************************************************************************
  716. *
  717. * Description:
  718. * Read PHY registers.
  719. *
  720. * NOTE! This function will change to Bank 2.
  721. *
  722. * Params:
  723. * [in] addr address of the register to read
  724. *
  725. * Returns:
  726. * The value in the register
  727. */
  728. static unsigned short phyRead (unsigned char addr)
  729. {
  730. unsigned short ret = 0;
  731. /* move to bank 2 */
  732. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  733. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  734. /* write address to MIREGADR */
  735. m_nic_write (CTL_REG_MIREGADR, addr);
  736. /* set MICMD.MIIRD */
  737. m_nic_write (CTL_REG_MICMD, ENC_MICMD_MIIRD);
  738. /* taken from the Linux driver */
  739. /* move to bank 3 */
  740. m_nic_bfs(CTL_REG_ECON1, ENC_ECON1_BSEL0);
  741. m_nic_bfs(CTL_REG_ECON1, ENC_ECON1_BSEL1);
  742. /* poll MISTAT.BUSY bit until operation is complete */
  743. while ((m_nic_read (CTL_REG_MISTAT) & ENC_MISTAT_BUSY) != 0) {
  744. static int cnt = 0;
  745. if (cnt++ >= 1000) {
  746. /* GJ - this seems extremely dangerous! */
  747. /* printf("#"); */
  748. cnt = 0;
  749. }
  750. }
  751. /* taken from the Linux driver */
  752. /* move to bank 2 */
  753. m_nic_bfc(CTL_REG_ECON1, ENC_ECON1_BSEL0);
  754. m_nic_bfs(CTL_REG_ECON1, ENC_ECON1_BSEL1);
  755. /* clear MICMD.MIIRD */
  756. m_nic_write (CTL_REG_MICMD, 0);
  757. ret = (m_nic_read (CTL_REG_MIRDH) << 8);
  758. ret |= (m_nic_read (CTL_REG_MIRDL) & 0xFF);
  759. return ret;
  760. }
  761. /*****************************************************************************
  762. *
  763. * Taken from the Linux driver.
  764. * Description:
  765. * Write PHY registers.
  766. *
  767. * NOTE! This function will change to Bank 3.
  768. *
  769. * Params:
  770. * [in] addr address of the register to write to
  771. * [in] data to be written
  772. *
  773. * Returns:
  774. * None
  775. */
  776. static void phyWrite(unsigned char addr, unsigned short data)
  777. {
  778. /* move to bank 2 */
  779. m_nic_bfc(CTL_REG_ECON1, ENC_ECON1_BSEL0);
  780. m_nic_bfs(CTL_REG_ECON1, ENC_ECON1_BSEL1);
  781. /* write address to MIREGADR */
  782. m_nic_write(CTL_REG_MIREGADR, addr);
  783. m_nic_write(CTL_REG_MIWRL, data & 0xff);
  784. m_nic_write(CTL_REG_MIWRH, data >> 8);
  785. /* move to bank 3 */
  786. m_nic_bfs(CTL_REG_ECON1, ENC_ECON1_BSEL0);
  787. m_nic_bfs(CTL_REG_ECON1, ENC_ECON1_BSEL1);
  788. /* poll MISTAT.BUSY bit until operation is complete */
  789. while((m_nic_read(CTL_REG_MISTAT) & ENC_MISTAT_BUSY) != 0) {
  790. static int cnt = 0;
  791. if(cnt++ >= 1000) {
  792. cnt = 0;
  793. }
  794. }
  795. }
  796. #endif /* CONFIG_ENC28J60 */