enc28j60.c 23 KB

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