eth.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 "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_ERXNDA 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. /*
  129. * Receive Filter Register (ERXFCON) bits
  130. */
  131. #define ENC_RFR_UCEN 0x80
  132. #define ENC_RFR_ANDOR 0x40
  133. #define ENC_RFR_CRCEN 0x20
  134. #define ENC_RFR_PMEN 0x10
  135. #define ENC_RFR_MPEN 0x08
  136. #define ENC_RFR_HTEN 0x04
  137. #define ENC_RFR_MCEN 0x02
  138. #define ENC_RFR_BCEN 0x01
  139. /*
  140. * ECON1 Register Bits
  141. */
  142. #define ENC_ECON1_TXRST 0x80
  143. #define ENC_ECON1_RXRST 0x40
  144. #define ENC_ECON1_DMAST 0x20
  145. #define ENC_ECON1_CSUMEN 0x10
  146. #define ENC_ECON1_TXRTS 0x08
  147. #define ENC_ECON1_RXEN 0x04
  148. #define ENC_ECON1_BSEL1 0x02
  149. #define ENC_ECON1_BSEL0 0x01
  150. /*
  151. * ECON2 Register Bits
  152. */
  153. #define ENC_ECON2_AUTOINC 0x80
  154. #define ENC_ECON2_PKTDEC 0x40
  155. #define ENC_ECON2_PWRSV 0x20
  156. #define ENC_ECON2_VRPS 0x08
  157. /*
  158. * EIR Register Bits
  159. */
  160. #define ENC_EIR_PKTIF 0x40
  161. #define ENC_EIR_DMAIF 0x20
  162. #define ENC_EIR_LINKIF 0x10
  163. #define ENC_EIR_TXIF 0x08
  164. #define ENC_EIR_WOLIF 0x04
  165. #define ENC_EIR_TXERIF 0x02
  166. #define ENC_EIR_RXERIF 0x01
  167. /*
  168. * ESTAT Register Bits
  169. */
  170. #define ENC_ESTAT_INT 0x80
  171. #define ENC_ESTAT_LATECOL 0x10
  172. #define ENC_ESTAT_RXBUSY 0x04
  173. #define ENC_ESTAT_TXABRT 0x02
  174. #define ENC_ESTAT_CLKRDY 0x01
  175. /*
  176. * EIE Register Bits
  177. */
  178. #define ENC_EIE_INTIE 0x80
  179. #define ENC_EIE_PKTIE 0x40
  180. #define ENC_EIE_DMAIE 0x20
  181. #define ENC_EIE_LINKIE 0x10
  182. #define ENC_EIE_TXIE 0x08
  183. #define ENC_EIE_WOLIE 0x04
  184. #define ENC_EIE_TXERIE 0x02
  185. #define ENC_EIE_RXERIE 0x01
  186. /*
  187. * MACON1 Register Bits
  188. */
  189. #define ENC_MACON1_LOOPBK 0x10
  190. #define ENC_MACON1_TXPAUS 0x08
  191. #define ENC_MACON1_RXPAUS 0x04
  192. #define ENC_MACON1_PASSALL 0x02
  193. #define ENC_MACON1_MARXEN 0x01
  194. /*
  195. * MACON2 Register Bits
  196. */
  197. #define ENC_MACON2_MARST 0x80
  198. #define ENC_MACON2_RNDRST 0x40
  199. #define ENC_MACON2_MARXRST 0x08
  200. #define ENC_MACON2_RFUNRST 0x04
  201. #define ENC_MACON2_MATXRST 0x02
  202. #define ENC_MACON2_TFUNRST 0x01
  203. /*
  204. * MACON3 Register Bits
  205. */
  206. #define ENC_MACON3_PADCFG2 0x80
  207. #define ENC_MACON3_PADCFG1 0x40
  208. #define ENC_MACON3_PADCFG0 0x20
  209. #define ENC_MACON3_TXCRCEN 0x10
  210. #define ENC_MACON3_PHDRLEN 0x08
  211. #define ENC_MACON3_HFRMEN 0x04
  212. #define ENC_MACON3_FRMLNEN 0x02
  213. #define ENC_MACON3_FULDPX 0x01
  214. /*
  215. * MICMD Register Bits
  216. */
  217. #define ENC_MICMD_MIISCAN 0x02
  218. #define ENC_MICMD_MIIRD 0x01
  219. /*
  220. * MISTAT Register Bits
  221. */
  222. #define ENC_MISTAT_NVALID 0x04
  223. #define ENC_MISTAT_SCAN 0x02
  224. #define ENC_MISTAT_BUSY 0x01
  225. /*
  226. * PHID1 and PHID2 values
  227. */
  228. #define ENC_PHID1_VALUE 0x0083
  229. #define ENC_PHID2_VALUE 0x1400
  230. #define ENC_PHID2_MASK 0xFC00
  231. #define ENC_SPI_SLAVE_CS 0x00010000 /* pin P1.16 */
  232. #define ENC_RESET 0x00020000 /* pin P1.17 */
  233. #define FAILSAFE_VALUE 5000
  234. /*
  235. * Controller memory layout:
  236. *
  237. * 0x0000 - 0x17ff 6k bytes receive buffer
  238. * 0x1800 - 0x1fff 2k bytes transmit buffer
  239. */
  240. /* Use the lower memory for receiver buffer. See errata pt. 5 */
  241. #define ENC_RX_BUF_START 0x0000
  242. #define ENC_TX_BUF_START 0x1800
  243. /* maximum frame length */
  244. #define ENC_MAX_FRM_LEN 1518
  245. #define enc_enable() PUT32(IO1CLR, ENC_SPI_SLAVE_CS)
  246. #define enc_disable() PUT32(IO1SET, ENC_SPI_SLAVE_CS)
  247. #define enc_cfg_spi() spi_set_cfg(0, 0, 0); spi_set_clock(8);
  248. static unsigned char encReadReg (unsigned char regNo);
  249. static void encWriteReg (unsigned char regNo, unsigned char data);
  250. static void encWriteRegRetry (unsigned char regNo, unsigned char data, int c);
  251. static void encReadBuff (unsigned short length, unsigned char *pBuff);
  252. static void encWriteBuff (unsigned short length, unsigned char *pBuff);
  253. static void encBitSet (unsigned char regNo, unsigned char data);
  254. static void encBitClr (unsigned char regNo, unsigned char data);
  255. static void encReset (void);
  256. static void encInit (unsigned char *pEthAddr);
  257. static unsigned short phyRead (unsigned char addr);
  258. static void encPoll (void);
  259. static void encRx (void);
  260. #define m_nic_read(reg) encReadReg(reg)
  261. #define m_nic_write(reg, data) encWriteReg(reg, data)
  262. #define m_nic_write_retry(reg, data, count) encWriteRegRetry(reg, data, count)
  263. #define m_nic_read_data(len, buf) encReadBuff((len), (buf))
  264. #define m_nic_write_data(len, buf) encWriteBuff((len), (buf))
  265. /* bit field set */
  266. #define m_nic_bfs(reg, data) encBitSet(reg, data)
  267. /* bit field clear */
  268. #define m_nic_bfc(reg, data) encBitClr(reg, data)
  269. static unsigned char bank = 0; /* current bank in enc28j60 */
  270. static unsigned char next_pointer_lsb;
  271. static unsigned char next_pointer_msb;
  272. static unsigned char buffer[ENC_MAX_FRM_LEN];
  273. static int rxResetCounter = 0;
  274. #define RX_RESET_COUNTER 1000;
  275. /*-----------------------------------------------------------------------------
  276. * Returns 0 when failes otherwize 1
  277. */
  278. int eth_init (bd_t * bis)
  279. {
  280. /* configure GPIO */
  281. (*((volatile unsigned long *) IO1DIR)) |= ENC_SPI_SLAVE_CS;
  282. (*((volatile unsigned long *) IO1DIR)) |= ENC_RESET;
  283. /* CS and RESET active low */
  284. PUT32 (IO1SET, ENC_SPI_SLAVE_CS);
  285. PUT32 (IO1SET, ENC_RESET);
  286. spi_init ();
  287. /* initialize controller */
  288. encReset ();
  289. encInit (bis->bi_enetaddr);
  290. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_RXEN); /* enable receive */
  291. return 0;
  292. }
  293. int eth_send (volatile void *packet, int length)
  294. {
  295. /* check frame length, etc. */
  296. /* TODO: */
  297. /* switch to bank 0 */
  298. m_nic_bfc (CTL_REG_ECON1, (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  299. /* set EWRPT */
  300. m_nic_write (CTL_REG_EWRPTL, (ENC_TX_BUF_START & 0xff));
  301. m_nic_write (CTL_REG_EWRPTH, (ENC_TX_BUF_START >> 8));
  302. /* set ETXST */
  303. m_nic_write (CTL_REG_ETXSTL, ENC_TX_BUF_START & 0xFF);
  304. m_nic_write (CTL_REG_ETXSTH, ENC_TX_BUF_START >> 8);
  305. /* write packet */
  306. m_nic_write_data (length, (unsigned char *) packet);
  307. /* set ETXND */
  308. m_nic_write (CTL_REG_ETXNDL, (length + ENC_TX_BUF_START) & 0xFF);
  309. m_nic_write (CTL_REG_ETXNDH, (length + ENC_TX_BUF_START) >> 8);
  310. /* set ECON1.TXRTS */
  311. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_TXRTS);
  312. return 0;
  313. }
  314. /*****************************************************************************
  315. * This function resets the receiver only. This function may be called from
  316. * interrupt-context.
  317. */
  318. static void encReceiverReset (void)
  319. {
  320. unsigned char econ1;
  321. econ1 = m_nic_read (CTL_REG_ECON1);
  322. if ((econ1 & ENC_ECON1_RXRST) == 0) {
  323. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_RXRST);
  324. rxResetCounter = RX_RESET_COUNTER;
  325. }
  326. }
  327. /*****************************************************************************
  328. * receiver reset timer
  329. */
  330. static void encReceiverResetCallback (void)
  331. {
  332. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_RXRST);
  333. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_RXEN); /* enable receive */
  334. }
  335. /*-----------------------------------------------------------------------------
  336. * Check for received packets. Call NetReceive for each packet. The return
  337. * value is ignored by the caller.
  338. */
  339. int eth_rx (void)
  340. {
  341. if (rxResetCounter > 0 && --rxResetCounter == 0) {
  342. encReceiverResetCallback ();
  343. }
  344. encPoll ();
  345. return 0;
  346. }
  347. void eth_halt (void)
  348. {
  349. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_RXEN); /* disable receive */
  350. }
  351. /*****************************************************************************/
  352. static void encPoll (void)
  353. {
  354. unsigned char eir_reg;
  355. volatile unsigned char estat_reg;
  356. unsigned char pkt_cnt;
  357. /* clear global interrupt enable bit in enc28j60 */
  358. m_nic_bfc (CTL_REG_EIE, ENC_EIE_INTIE);
  359. estat_reg = m_nic_read (CTL_REG_ESTAT);
  360. eir_reg = m_nic_read (CTL_REG_EIR);
  361. if (eir_reg & ENC_EIR_TXIF) {
  362. /* clear TXIF bit in EIR */
  363. m_nic_bfc (CTL_REG_EIR, ENC_EIR_TXIF);
  364. }
  365. /* We have to use pktcnt and not pktif bit, see errata pt. 6 */
  366. /* move to bank 1 */
  367. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  368. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  369. /* read pktcnt */
  370. pkt_cnt = m_nic_read (CTL_REG_EPKTCNT);
  371. if (pkt_cnt > 0) {
  372. if ((eir_reg & ENC_EIR_PKTIF) == 0) {
  373. /*printf("encPoll: pkt cnt > 0, but pktif not set\n"); */
  374. }
  375. encRx ();
  376. /* clear PKTIF bit in EIR, this should not need to be done but it
  377. seems like we get problems if we do not */
  378. m_nic_bfc (CTL_REG_EIR, ENC_EIR_PKTIF);
  379. }
  380. if (eir_reg & ENC_EIR_RXERIF) {
  381. printf ("encPoll: rx error\n");
  382. m_nic_bfc (CTL_REG_EIR, ENC_EIR_RXERIF);
  383. }
  384. if (eir_reg & ENC_EIR_TXERIF) {
  385. printf ("encPoll: tx error\n");
  386. m_nic_bfc (CTL_REG_EIR, ENC_EIR_TXERIF);
  387. }
  388. /* set global interrupt enable bit in enc28j60 */
  389. m_nic_bfs (CTL_REG_EIE, ENC_EIE_INTIE);
  390. }
  391. static void encRx (void)
  392. {
  393. unsigned short pkt_len;
  394. unsigned short copy_len;
  395. unsigned short status;
  396. unsigned char eir_reg;
  397. unsigned char pkt_cnt = 0;
  398. /* switch to bank 0 */
  399. m_nic_bfc (CTL_REG_ECON1, (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  400. m_nic_write (CTL_REG_ERDPTL, next_pointer_lsb);
  401. m_nic_write (CTL_REG_ERDPTH, next_pointer_msb);
  402. do {
  403. m_nic_read_data (6, buffer);
  404. next_pointer_lsb = buffer[0];
  405. next_pointer_msb = buffer[1];
  406. pkt_len = buffer[2];
  407. pkt_len |= (unsigned short) buffer[3] << 8;
  408. status = buffer[4];
  409. status |= (unsigned short) buffer[5] << 8;
  410. if (pkt_len <= ENC_MAX_FRM_LEN) {
  411. copy_len = pkt_len;
  412. } else {
  413. copy_len = 0;
  414. /* p_priv->stats.rx_dropped++; */
  415. /* we will drop this packet */
  416. }
  417. if ((status & (1L << 7)) == 0) { /* check Received Ok bit */
  418. copy_len = 0;
  419. /* p_priv->stats.rx_errors++; */
  420. }
  421. if (copy_len > 0) {
  422. m_nic_read_data (copy_len, buffer);
  423. }
  424. /* advance read pointer to next pointer */
  425. m_nic_write (CTL_REG_ERDPTL, next_pointer_lsb);
  426. m_nic_write (CTL_REG_ERDPTH, next_pointer_msb);
  427. /* decrease packet counter */
  428. m_nic_bfs (CTL_REG_ECON2, ENC_ECON2_PKTDEC);
  429. /* move to bank 1 */
  430. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  431. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  432. /* read pktcnt */
  433. pkt_cnt = m_nic_read (CTL_REG_EPKTCNT);
  434. /* switch to bank 0 */
  435. m_nic_bfc (CTL_REG_ECON1,
  436. (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  437. if (copy_len == 0) {
  438. eir_reg = m_nic_read (CTL_REG_EIR);
  439. encReceiverReset ();
  440. printf ("eth_rx: copy_len=0\n");
  441. continue;
  442. }
  443. NetReceive ((unsigned char *) buffer, pkt_len);
  444. eir_reg = m_nic_read (CTL_REG_EIR);
  445. } while (pkt_cnt); /* Use EPKTCNT not EIR.PKTIF flag, see errata pt. 6 */
  446. m_nic_write (CTL_REG_ERXRDPTL, next_pointer_lsb);
  447. m_nic_write (CTL_REG_ERXRDPTH, next_pointer_msb);
  448. }
  449. static void encWriteReg (unsigned char regNo, unsigned char data)
  450. {
  451. spi_lock ();
  452. enc_cfg_spi ();
  453. enc_enable ();
  454. spi_write (0x40 | regNo); /* write in regNo */
  455. spi_write (data);
  456. enc_disable ();
  457. enc_enable ();
  458. spi_write (0x1f); /* write reg 0x1f */
  459. enc_disable ();
  460. spi_unlock ();
  461. }
  462. static void encWriteRegRetry (unsigned char regNo, unsigned char data, int c)
  463. {
  464. unsigned char readback;
  465. int i;
  466. spi_lock ();
  467. for (i = 0; i < c; i++) {
  468. enc_cfg_spi ();
  469. enc_enable ();
  470. spi_write (0x40 | regNo); /* write in regNo */
  471. spi_write (data);
  472. enc_disable ();
  473. enc_enable ();
  474. spi_write (0x1f); /* write reg 0x1f */
  475. enc_disable ();
  476. spi_unlock (); /* we must unlock spi first */
  477. readback = encReadReg (regNo);
  478. spi_lock ();
  479. if (readback == data)
  480. break;
  481. }
  482. spi_unlock ();
  483. if (i == c) {
  484. printf ("enc28j60: write reg %d failed\n", regNo);
  485. }
  486. }
  487. static unsigned char encReadReg (unsigned char regNo)
  488. {
  489. unsigned char rxByte;
  490. spi_lock ();
  491. enc_cfg_spi ();
  492. enc_enable ();
  493. spi_write (0x1f); /* read reg 0x1f */
  494. bank = spi_read () & 0x3;
  495. enc_disable ();
  496. enc_enable ();
  497. spi_write (regNo);
  498. rxByte = spi_read ();
  499. /* check if MAC or MII register */
  500. if (((bank == 2) && (regNo <= 0x1a)) ||
  501. ((bank == 3) && (regNo <= 0x05 || regNo == 0x0a))) {
  502. /* ignore first byte and read another byte */
  503. rxByte = spi_read ();
  504. }
  505. enc_disable ();
  506. spi_unlock ();
  507. return rxByte;
  508. }
  509. static void encReadBuff (unsigned short length, unsigned char *pBuff)
  510. {
  511. spi_lock ();
  512. enc_cfg_spi ();
  513. enc_enable ();
  514. spi_write (0x20 | 0x1a); /* read buffer memory */
  515. while (length--) {
  516. if (pBuff != NULL)
  517. *pBuff++ = spi_read ();
  518. else
  519. spi_write (0);
  520. }
  521. enc_disable ();
  522. spi_unlock ();
  523. }
  524. static void encWriteBuff (unsigned short length, unsigned char *pBuff)
  525. {
  526. spi_lock ();
  527. enc_cfg_spi ();
  528. enc_enable ();
  529. spi_write (0x60 | 0x1a); /* write buffer memory */
  530. spi_write (0x00); /* control byte */
  531. while (length--)
  532. spi_write (*pBuff++);
  533. enc_disable ();
  534. spi_unlock ();
  535. }
  536. static void encBitSet (unsigned char regNo, unsigned char data)
  537. {
  538. spi_lock ();
  539. enc_cfg_spi ();
  540. enc_enable ();
  541. spi_write (0x80 | regNo); /* bit field set */
  542. spi_write (data);
  543. enc_disable ();
  544. spi_unlock ();
  545. }
  546. static void encBitClr (unsigned char regNo, unsigned char data)
  547. {
  548. spi_lock ();
  549. enc_cfg_spi ();
  550. enc_enable ();
  551. spi_write (0xA0 | regNo); /* bit field clear */
  552. spi_write (data);
  553. enc_disable ();
  554. spi_unlock ();
  555. }
  556. static void encReset (void)
  557. {
  558. spi_lock ();
  559. enc_cfg_spi ();
  560. enc_enable ();
  561. spi_write (0xff); /* soft reset */
  562. enc_disable ();
  563. spi_unlock ();
  564. /* sleep 1 ms. See errata pt. 2 */
  565. udelay (1000);
  566. #if 0
  567. (*((volatile unsigned long *) IO1CLR)) &= ENC_RESET;
  568. mdelay (5);
  569. (*((volatile unsigned long *) IO1SET)) &= ENC_RESET;
  570. #endif
  571. }
  572. static void encInit (unsigned char *pEthAddr)
  573. {
  574. unsigned short phid1 = 0;
  575. unsigned short phid2 = 0;
  576. /* switch to bank 0 */
  577. m_nic_bfc (CTL_REG_ECON1, (ENC_ECON1_BSEL1 | ENC_ECON1_BSEL0));
  578. /*
  579. * Setup the buffer space. The reset values are valid for the
  580. * other pointers.
  581. */
  582. #if 0
  583. /* We shall not write to ERXST, see errata pt. 5. Instead we
  584. have to make sure that ENC_RX_BUS_START is 0. */
  585. m_nic_write_retry (CTL_REG_ERXSTL, (ENC_RX_BUF_START & 0xFF), 1);
  586. m_nic_write_retry (CTL_REG_ERXSTH, (ENC_RX_BUF_START >> 8), 1);
  587. #endif
  588. m_nic_write_retry (CTL_REG_ERDPTL, (ENC_RX_BUF_START & 0xFF), 1);
  589. m_nic_write_retry (CTL_REG_ERDPTH, (ENC_RX_BUF_START >> 8), 1);
  590. next_pointer_lsb = (ENC_RX_BUF_START & 0xFF);
  591. next_pointer_msb = (ENC_RX_BUF_START >> 8);
  592. /*
  593. * For tracking purposes, the ERXRDPT registers should be programmed with
  594. * the same value. This is the read pointer.
  595. */
  596. m_nic_write (CTL_REG_ERXRDPTL, (ENC_RX_BUF_START & 0xFF));
  597. m_nic_write_retry (CTL_REG_ERXRDPTH, (ENC_RX_BUF_START >> 8), 1);
  598. /* Setup receive filters. */
  599. /* move to bank 1 */
  600. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  601. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  602. /* OR-filtering, Unicast, CRC-check and broadcast */
  603. m_nic_write_retry (CTL_REG_ERXFCON,
  604. (ENC_RFR_UCEN | ENC_RFR_CRCEN | ENC_RFR_BCEN), 1);
  605. /* Wait for Oscillator Start-up Timer (OST). */
  606. while ((m_nic_read (CTL_REG_ESTAT) & ENC_ESTAT_CLKRDY) == 0) {
  607. static int cnt = 0;
  608. if (cnt++ >= 1000) {
  609. cnt = 0;
  610. }
  611. }
  612. /* verify identification */
  613. phid1 = phyRead (PHY_REG_PHID1);
  614. phid2 = phyRead (PHY_REG_PHID2);
  615. if (phid1 != ENC_PHID1_VALUE
  616. || (phid2 & ENC_PHID2_MASK) != ENC_PHID2_VALUE) {
  617. printf ("ERROR: failed to identify controller\n");
  618. printf ("phid1 = %x, phid2 = %x\n",
  619. phid1, (phid2 & ENC_PHID2_MASK));
  620. printf ("should be phid1 = %x, phid2 = %x\n",
  621. ENC_PHID1_VALUE, ENC_PHID2_VALUE);
  622. }
  623. /*
  624. * --- MAC Initialization ---
  625. */
  626. /* Pull MAC out of Reset */
  627. /* switch to bank 2 */
  628. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  629. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  630. /* clear MAC reset bits */
  631. m_nic_write_retry (CTL_REG_MACON2, 0, 1);
  632. /* enable MAC to receive frames */
  633. m_nic_write_retry (CTL_REG_MACON1, ENC_MACON1_MARXEN, 10);
  634. /* configure pad, tx-crc and duplex */
  635. /* TODO maybe enable FRMLNEN */
  636. m_nic_write_retry (CTL_REG_MACON3,
  637. (ENC_MACON3_PADCFG0 | ENC_MACON3_TXCRCEN), 10);
  638. /* set maximum frame length */
  639. m_nic_write_retry (CTL_REG_MAMXFLL, (ENC_MAX_FRM_LEN & 0xff), 10);
  640. m_nic_write_retry (CTL_REG_MAMXFLH, (ENC_MAX_FRM_LEN >> 8), 10);
  641. /*
  642. * Set MAC back-to-back inter-packet gap. Recommended 0x12 for half duplex
  643. * and 0x15 for full duplex.
  644. */
  645. m_nic_write_retry (CTL_REG_MABBIPG, 0x12, 10);
  646. /* Set (low byte) Non-Back-to_Back Inter-Packet Gap. Recommended 0x12 */
  647. m_nic_write_retry (CTL_REG_MAIPGL, 0x12, 10);
  648. /*
  649. * Set (high byte) Non-Back-to_Back Inter-Packet Gap. Recommended
  650. * 0x0c for half-duplex. Nothing for full-duplex
  651. */
  652. m_nic_write_retry (CTL_REG_MAIPGH, 0x0C, 10);
  653. /* set MAC address */
  654. /* switch to bank 3 */
  655. m_nic_bfs (CTL_REG_ECON1, (ENC_ECON1_BSEL0 | ENC_ECON1_BSEL1));
  656. m_nic_write_retry (CTL_REG_MAADR0, pEthAddr[5], 1);
  657. m_nic_write_retry (CTL_REG_MAADR1, pEthAddr[4], 1);
  658. m_nic_write_retry (CTL_REG_MAADR2, pEthAddr[3], 1);
  659. m_nic_write_retry (CTL_REG_MAADR3, pEthAddr[2], 1);
  660. m_nic_write_retry (CTL_REG_MAADR4, pEthAddr[1], 1);
  661. m_nic_write_retry (CTL_REG_MAADR5, pEthAddr[0], 1);
  662. /*
  663. * Receive settings
  664. */
  665. /* auto-increment RX-pointer when reading a received packet */
  666. m_nic_bfs (CTL_REG_ECON2, ENC_ECON2_AUTOINC);
  667. /* enable interrupts */
  668. m_nic_bfs (CTL_REG_EIE, ENC_EIE_PKTIE);
  669. m_nic_bfs (CTL_REG_EIE, ENC_EIE_TXIE);
  670. m_nic_bfs (CTL_REG_EIE, ENC_EIE_RXERIE);
  671. m_nic_bfs (CTL_REG_EIE, ENC_EIE_TXERIE);
  672. m_nic_bfs (CTL_REG_EIE, ENC_EIE_INTIE);
  673. }
  674. /*****************************************************************************
  675. *
  676. * Description:
  677. * Read PHY registers.
  678. *
  679. * NOTE! This function will change to Bank 2.
  680. *
  681. * Params:
  682. * [in] addr address of the register to read
  683. *
  684. * Returns:
  685. * The value in the register
  686. */
  687. static unsigned short phyRead (unsigned char addr)
  688. {
  689. unsigned short ret = 0;
  690. /* move to bank 2 */
  691. m_nic_bfc (CTL_REG_ECON1, ENC_ECON1_BSEL0);
  692. m_nic_bfs (CTL_REG_ECON1, ENC_ECON1_BSEL1);
  693. /* write address to MIREGADR */
  694. m_nic_write (CTL_REG_MIREGADR, addr);
  695. /* set MICMD.MIIRD */
  696. m_nic_write (CTL_REG_MICMD, ENC_MICMD_MIIRD);
  697. /* poll MISTAT.BUSY bit until operation is complete */
  698. while ((m_nic_read (CTL_REG_MISTAT) & ENC_MISTAT_BUSY) != 0) {
  699. static int cnt = 0;
  700. if (cnt++ >= 1000) {
  701. /* GJ - this seems extremely dangerous! */
  702. /* printf("#"); */
  703. cnt = 0;
  704. }
  705. }
  706. /* clear MICMD.MIIRD */
  707. m_nic_write (CTL_REG_MICMD, 0);
  708. ret = (m_nic_read (CTL_REG_MIRDH) << 8);
  709. ret |= (m_nic_read (CTL_REG_MIRDL) & 0xFF);
  710. return ret;
  711. }