enc28j60.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * (C) Copyright 2010
  3. * Reinhard Meyer, EMK Elektronik, reinhard.meyer@emk-elektronik.de
  4. * Martin Krause, Martin.Krause@tqs.de
  5. * reworked original enc28j60.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <net.h>
  24. #include <spi.h>
  25. #include <malloc.h>
  26. #include <netdev.h>
  27. #include <miiphy.h>
  28. #include "enc28j60.h"
  29. /*
  30. * IMPORTANT: spi_claim_bus() and spi_release_bus()
  31. * are called at begin and end of each of the following functions:
  32. * enc_miiphy_read(), enc_miiphy_write(), enc_write_hwaddr(),
  33. * enc_init(), enc_recv(), enc_send(), enc_halt()
  34. * ALL other functions assume that the bus has already been claimed!
  35. * Since NetReceive() might call enc_send() in return, the bus must be
  36. * released, NetReceive() called and claimed again.
  37. */
  38. /*
  39. * Controller memory layout.
  40. * We only allow 1 frame for transmission and reserve the rest
  41. * for reception to handle as many broadcast packets as possible.
  42. * Also use the memory from 0x0000 for receiver buffer. See errata pt. 5
  43. * 0x0000 - 0x19ff 6656 bytes receive buffer
  44. * 0x1a00 - 0x1fff 1536 bytes transmit buffer =
  45. * control(1)+frame(1518)+status(7)+reserve(10).
  46. */
  47. #define ENC_RX_BUF_START 0x0000
  48. #define ENC_RX_BUF_END 0x19ff
  49. #define ENC_TX_BUF_START 0x1a00
  50. #define ENC_TX_BUF_END 0x1fff
  51. #define ENC_MAX_FRM_LEN 1518
  52. #define RX_RESET_COUNTER 1000
  53. /*
  54. * For non data transfer functions, like phy read/write, set hwaddr, init
  55. * we do not need a full, time consuming init including link ready wait.
  56. * This enum helps to bring the chip through the minimum necessary inits.
  57. */
  58. enum enc_initstate {none=0, setupdone, linkready};
  59. typedef struct enc_device {
  60. struct eth_device *dev; /* back pointer */
  61. struct spi_slave *slave;
  62. int rx_reset_counter;
  63. u16 next_pointer;
  64. u8 bank; /* current bank in enc28j60 */
  65. enum enc_initstate initstate;
  66. } enc_dev_t;
  67. /*
  68. * enc_bset: set bits in a common register
  69. * enc_bclr: clear bits in a common register
  70. *
  71. * making the reg parameter u8 will give a compile time warning if the
  72. * functions are called with a register not accessible in all Banks
  73. */
  74. static void enc_bset(enc_dev_t *enc, const u8 reg, const u8 data)
  75. {
  76. u8 dout[2];
  77. dout[0] = CMD_BFS(reg);
  78. dout[1] = data;
  79. spi_xfer(enc->slave, 2 * 8, dout, NULL,
  80. SPI_XFER_BEGIN | SPI_XFER_END);
  81. }
  82. static void enc_bclr(enc_dev_t *enc, const u8 reg, const u8 data)
  83. {
  84. u8 dout[2];
  85. dout[0] = CMD_BFC(reg);
  86. dout[1] = data;
  87. spi_xfer(enc->slave, 2 * 8, dout, NULL,
  88. SPI_XFER_BEGIN | SPI_XFER_END);
  89. }
  90. /*
  91. * high byte of the register contains bank number:
  92. * 0: no bank switch necessary
  93. * 1: switch to bank 0
  94. * 2: switch to bank 1
  95. * 3: switch to bank 2
  96. * 4: switch to bank 3
  97. */
  98. static void enc_set_bank(enc_dev_t *enc, const u16 reg)
  99. {
  100. u8 newbank = reg >> 8;
  101. if (newbank == 0 || newbank == enc->bank)
  102. return;
  103. switch (newbank) {
  104. case 1:
  105. enc_bclr(enc, CTL_REG_ECON1,
  106. ENC_ECON1_BSEL0 | ENC_ECON1_BSEL1);
  107. break;
  108. case 2:
  109. enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_BSEL0);
  110. enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_BSEL1);
  111. break;
  112. case 3:
  113. enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_BSEL0);
  114. enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_BSEL1);
  115. break;
  116. case 4:
  117. enc_bset(enc, CTL_REG_ECON1,
  118. ENC_ECON1_BSEL0 | ENC_ECON1_BSEL1);
  119. break;
  120. }
  121. enc->bank = newbank;
  122. }
  123. /*
  124. * local functions to access SPI
  125. *
  126. * reg: register inside ENC28J60
  127. * data: 8/16 bits to write
  128. * c: number of retries
  129. *
  130. * enc_r8: read 8 bits
  131. * enc_r16: read 16 bits
  132. * enc_w8: write 8 bits
  133. * enc_w16: write 16 bits
  134. * enc_w8_retry: write 8 bits, verify and retry
  135. * enc_rbuf: read from ENC28J60 into buffer
  136. * enc_wbuf: write from buffer into ENC28J60
  137. */
  138. /*
  139. * MAC and MII registers need a 3 byte SPI transfer to read,
  140. * all other registers need a 2 byte SPI transfer.
  141. */
  142. static int enc_reg2nbytes(const u16 reg)
  143. {
  144. /* check if MAC or MII register */
  145. return ((reg >= CTL_REG_MACON1 && reg <= CTL_REG_MIRDH) ||
  146. (reg >= CTL_REG_MAADR1 && reg <= CTL_REG_MAADR4) ||
  147. (reg == CTL_REG_MISTAT)) ? 3 : 2;
  148. }
  149. /*
  150. * Read a byte register
  151. */
  152. static u8 enc_r8(enc_dev_t *enc, const u16 reg)
  153. {
  154. u8 dout[3];
  155. u8 din[3];
  156. int nbytes = enc_reg2nbytes(reg);
  157. enc_set_bank(enc, reg);
  158. dout[0] = CMD_RCR(reg);
  159. spi_xfer(enc->slave, nbytes * 8, dout, din,
  160. SPI_XFER_BEGIN | SPI_XFER_END);
  161. return din[nbytes-1];
  162. }
  163. /*
  164. * Read a L/H register pair and return a word.
  165. * Must be called with the L register's address.
  166. */
  167. static u16 enc_r16(enc_dev_t *enc, const u16 reg)
  168. {
  169. u8 dout[3];
  170. u8 din[3];
  171. u16 result;
  172. int nbytes = enc_reg2nbytes(reg);
  173. enc_set_bank(enc, reg);
  174. dout[0] = CMD_RCR(reg);
  175. spi_xfer(enc->slave, nbytes * 8, dout, din,
  176. SPI_XFER_BEGIN | SPI_XFER_END);
  177. result = din[nbytes-1];
  178. dout[0]++; /* next register */
  179. spi_xfer(enc->slave, nbytes * 8, dout, din,
  180. SPI_XFER_BEGIN | SPI_XFER_END);
  181. result |= din[nbytes-1] << 8;
  182. return result;
  183. }
  184. /*
  185. * Write a byte register
  186. */
  187. static void enc_w8(enc_dev_t *enc, const u16 reg, const u8 data)
  188. {
  189. u8 dout[2];
  190. enc_set_bank(enc, reg);
  191. dout[0] = CMD_WCR(reg);
  192. dout[1] = data;
  193. spi_xfer(enc->slave, 2 * 8, dout, NULL,
  194. SPI_XFER_BEGIN | SPI_XFER_END);
  195. }
  196. /*
  197. * Write a L/H register pair.
  198. * Must be called with the L register's address.
  199. */
  200. static void enc_w16(enc_dev_t *enc, const u16 reg, const u16 data)
  201. {
  202. u8 dout[2];
  203. enc_set_bank(enc, reg);
  204. dout[0] = CMD_WCR(reg);
  205. dout[1] = data;
  206. spi_xfer(enc->slave, 2 * 8, dout, NULL,
  207. SPI_XFER_BEGIN | SPI_XFER_END);
  208. dout[0]++; /* next register */
  209. dout[1] = data >> 8;
  210. spi_xfer(enc->slave, 2 * 8, dout, NULL,
  211. SPI_XFER_BEGIN | SPI_XFER_END);
  212. }
  213. /*
  214. * Write a byte register, verify and retry
  215. */
  216. static void enc_w8_retry(enc_dev_t *enc, const u16 reg, const u8 data, const int c)
  217. {
  218. u8 dout[2];
  219. u8 readback;
  220. int i;
  221. enc_set_bank(enc, reg);
  222. for (i = 0; i < c; i++) {
  223. dout[0] = CMD_WCR(reg);
  224. dout[1] = data;
  225. spi_xfer(enc->slave, 2 * 8, dout, NULL,
  226. SPI_XFER_BEGIN | SPI_XFER_END);
  227. readback = enc_r8(enc, reg);
  228. if (readback == data)
  229. break;
  230. /* wait 1ms */
  231. udelay(1000);
  232. }
  233. if (i == c) {
  234. printf("%s: write reg 0x%03x failed\n", enc->dev->name, reg);
  235. }
  236. }
  237. /*
  238. * Read ENC RAM into buffer
  239. */
  240. static void enc_rbuf(enc_dev_t *enc, const u16 length, u8 *buf)
  241. {
  242. u8 dout[1];
  243. dout[0] = CMD_RBM;
  244. spi_xfer(enc->slave, 8, dout, NULL, SPI_XFER_BEGIN);
  245. spi_xfer(enc->slave, length * 8, NULL, buf, SPI_XFER_END);
  246. #ifdef DEBUG
  247. puts("Rx:\n");
  248. print_buffer(0, buf, 1, length, 0);
  249. #endif
  250. }
  251. /*
  252. * Write buffer into ENC RAM
  253. */
  254. static void enc_wbuf(enc_dev_t *enc, const u16 length, const u8 *buf, const u8 control)
  255. {
  256. u8 dout[2];
  257. dout[0] = CMD_WBM;
  258. dout[1] = control;
  259. spi_xfer(enc->slave, 2 * 8, dout, NULL, SPI_XFER_BEGIN);
  260. spi_xfer(enc->slave, length * 8, buf, NULL, SPI_XFER_END);
  261. #ifdef DEBUG
  262. puts("Tx:\n");
  263. print_buffer(0, buf, 1, length, 0);
  264. #endif
  265. }
  266. /*
  267. * Try to claim the SPI bus.
  268. * Print error message on failure.
  269. */
  270. static int enc_claim_bus(enc_dev_t *enc)
  271. {
  272. int rc = spi_claim_bus(enc->slave);
  273. if (rc)
  274. printf("%s: failed to claim SPI bus\n", enc->dev->name);
  275. return rc;
  276. }
  277. /*
  278. * Release previously claimed SPI bus.
  279. * This function is mainly for symmetry to enc_claim_bus().
  280. * Let the toolchain decide to inline it...
  281. */
  282. static void enc_release_bus(enc_dev_t *enc)
  283. {
  284. spi_release_bus(enc->slave);
  285. }
  286. /*
  287. * Read PHY register
  288. */
  289. static u16 phy_read(enc_dev_t *enc, const u8 addr)
  290. {
  291. uint64_t etime;
  292. u8 status;
  293. enc_w8(enc, CTL_REG_MIREGADR, addr);
  294. enc_w8(enc, CTL_REG_MICMD, ENC_MICMD_MIIRD);
  295. /* 1 second timeout - only happens on hardware problem */
  296. etime = get_ticks() + get_tbclk();
  297. /* poll MISTAT.BUSY bit until operation is complete */
  298. do
  299. {
  300. status = enc_r8(enc, CTL_REG_MISTAT);
  301. } while (get_ticks() <= etime && (status & ENC_MISTAT_BUSY));
  302. if (status & ENC_MISTAT_BUSY) {
  303. printf("%s: timeout reading phy\n", enc->dev->name);
  304. return 0;
  305. }
  306. enc_w8(enc, CTL_REG_MICMD, 0);
  307. return enc_r16(enc, CTL_REG_MIRDL);
  308. }
  309. /*
  310. * Write PHY register
  311. */
  312. static void phy_write(enc_dev_t *enc, const u8 addr, const u16 data)
  313. {
  314. uint64_t etime;
  315. u8 status;
  316. enc_w8(enc, CTL_REG_MIREGADR, addr);
  317. enc_w16(enc, CTL_REG_MIWRL, data);
  318. /* 1 second timeout - only happens on hardware problem */
  319. etime = get_ticks() + get_tbclk();
  320. /* poll MISTAT.BUSY bit until operation is complete */
  321. do
  322. {
  323. status = enc_r8(enc, CTL_REG_MISTAT);
  324. } while (get_ticks() <= etime && (status & ENC_MISTAT_BUSY));
  325. if (status & ENC_MISTAT_BUSY) {
  326. printf("%s: timeout writing phy\n", enc->dev->name);
  327. return;
  328. }
  329. }
  330. /*
  331. * Verify link status, wait if necessary
  332. *
  333. * Note: with a 10 MBit/s only PHY there is no autonegotiation possible,
  334. * half/full duplex is a pure setup matter. For the time being, this driver
  335. * will setup in half duplex mode only.
  336. */
  337. static int enc_phy_link_wait(enc_dev_t *enc)
  338. {
  339. u16 status;
  340. int duplex;
  341. uint64_t etime;
  342. #ifdef CONFIG_ENC_SILENTLINK
  343. /* check if we have a link, then just return */
  344. status = phy_read(enc, PHY_REG_PHSTAT1);
  345. if (status & ENC_PHSTAT1_LLSTAT)
  346. return 0;
  347. #endif
  348. /* wait for link with 1 second timeout */
  349. etime = get_ticks() + get_tbclk();
  350. while (get_ticks() <= etime) {
  351. status = phy_read(enc, PHY_REG_PHSTAT1);
  352. if (status & ENC_PHSTAT1_LLSTAT) {
  353. /* now we have a link */
  354. status = phy_read(enc, PHY_REG_PHSTAT2);
  355. duplex = (status & ENC_PHSTAT2_DPXSTAT) ? 1 : 0;
  356. printf("%s: link up, 10Mbps %s-duplex\n",
  357. enc->dev->name, duplex ? "full" : "half");
  358. return 0;
  359. }
  360. udelay(1000);
  361. }
  362. /* timeout occured */
  363. printf("%s: link down\n", enc->dev->name);
  364. return 1;
  365. }
  366. /*
  367. * This function resets the receiver only.
  368. */
  369. static void enc_reset_rx(enc_dev_t *enc)
  370. {
  371. u8 econ1;
  372. econ1 = enc_r8(enc, CTL_REG_ECON1);
  373. if ((econ1 & ENC_ECON1_RXRST) == 0) {
  374. enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_RXRST);
  375. enc->rx_reset_counter = RX_RESET_COUNTER;
  376. }
  377. }
  378. /*
  379. * Reset receiver and reenable it.
  380. */
  381. static void enc_reset_rx_call(enc_dev_t *enc)
  382. {
  383. enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_RXRST);
  384. enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_RXEN);
  385. }
  386. /*
  387. * Copy a packet from the receive ring and forward it to
  388. * the protocol stack.
  389. */
  390. static void enc_receive(enc_dev_t *enc)
  391. {
  392. u8 *packet = (u8 *)NetRxPackets[0];
  393. u16 pkt_len;
  394. u16 copy_len;
  395. u16 status;
  396. u8 eir_reg;
  397. u8 pkt_cnt = 0;
  398. u16 rxbuf_rdpt;
  399. u8 hbuf[6];
  400. enc_w16(enc, CTL_REG_ERDPTL, enc->next_pointer);
  401. do {
  402. enc_rbuf(enc, 6, hbuf);
  403. enc->next_pointer = hbuf[0] | (hbuf[1] << 8);
  404. pkt_len = hbuf[2] | (hbuf[3] << 8);
  405. status = hbuf[4] | (hbuf[5] << 8);
  406. debug("next_pointer=$%04x pkt_len=%u status=$%04x\n",
  407. enc->next_pointer, pkt_len, status);
  408. if (pkt_len <= ENC_MAX_FRM_LEN)
  409. copy_len = pkt_len;
  410. else
  411. copy_len = 0;
  412. if ((status & (1L << 7)) == 0) /* check Received Ok bit */
  413. copy_len = 0;
  414. /* check if next pointer is resonable */
  415. if (enc->next_pointer >= ENC_TX_BUF_START)
  416. copy_len = 0;
  417. if (copy_len > 0) {
  418. enc_rbuf(enc, copy_len, packet);
  419. }
  420. /* advance read pointer to next pointer */
  421. enc_w16(enc, CTL_REG_ERDPTL, enc->next_pointer);
  422. /* decrease packet counter */
  423. enc_bset(enc, CTL_REG_ECON2, ENC_ECON2_PKTDEC);
  424. /*
  425. * Only odd values should be written to ERXRDPTL,
  426. * see errata B4 pt.13
  427. */
  428. rxbuf_rdpt = enc->next_pointer - 1;
  429. if ((rxbuf_rdpt < enc_r16(enc, CTL_REG_ERXSTL)) ||
  430. (rxbuf_rdpt > enc_r16(enc, CTL_REG_ERXNDL))) {
  431. enc_w16(enc, CTL_REG_ERXRDPTL,
  432. enc_r16(enc, CTL_REG_ERXNDL));
  433. } else {
  434. enc_w16(enc, CTL_REG_ERXRDPTL, rxbuf_rdpt);
  435. }
  436. /* read pktcnt */
  437. pkt_cnt = enc_r8(enc, CTL_REG_EPKTCNT);
  438. if (copy_len == 0) {
  439. eir_reg = enc_r8(enc, CTL_REG_EIR);
  440. enc_reset_rx(enc);
  441. printf("%s: receive copy_len=0\n", enc->dev->name);
  442. continue;
  443. }
  444. /*
  445. * Because NetReceive() might call enc_send(), we need to
  446. * release the SPI bus, call NetReceive(), reclaim the bus
  447. */
  448. enc_release_bus(enc);
  449. NetReceive(packet, pkt_len);
  450. if (enc_claim_bus(enc))
  451. return;
  452. eir_reg = enc_r8(enc, CTL_REG_EIR);
  453. } while (pkt_cnt);
  454. /* Use EPKTCNT not EIR.PKTIF flag, see errata pt. 6 */
  455. }
  456. /*
  457. * Poll for completely received packets.
  458. */
  459. static void enc_poll(enc_dev_t *enc)
  460. {
  461. u8 eir_reg;
  462. u8 estat_reg;
  463. u8 pkt_cnt;
  464. #ifdef CONFIG_USE_IRQ
  465. /* clear global interrupt enable bit in enc28j60 */
  466. enc_bclr(enc, CTL_REG_EIE, ENC_EIE_INTIE);
  467. #endif
  468. estat_reg = enc_r8(enc, CTL_REG_ESTAT);
  469. eir_reg = enc_r8(enc, CTL_REG_EIR);
  470. if (eir_reg & ENC_EIR_TXIF) {
  471. /* clear TXIF bit in EIR */
  472. enc_bclr(enc, CTL_REG_EIR, ENC_EIR_TXIF);
  473. }
  474. /* We have to use pktcnt and not pktif bit, see errata pt. 6 */
  475. pkt_cnt = enc_r8(enc, CTL_REG_EPKTCNT);
  476. if (pkt_cnt > 0) {
  477. if ((eir_reg & ENC_EIR_PKTIF) == 0) {
  478. debug("enc_poll: pkt cnt > 0, but pktif not set\n");
  479. }
  480. enc_receive(enc);
  481. /*
  482. * clear PKTIF bit in EIR, this should not need to be done
  483. * but it seems like we get problems if we do not
  484. */
  485. enc_bclr(enc, CTL_REG_EIR, ENC_EIR_PKTIF);
  486. }
  487. if (eir_reg & ENC_EIR_RXERIF) {
  488. printf("%s: rx error\n", enc->dev->name);
  489. enc_bclr(enc, CTL_REG_EIR, ENC_EIR_RXERIF);
  490. }
  491. if (eir_reg & ENC_EIR_TXERIF) {
  492. printf("%s: tx error\n", enc->dev->name);
  493. enc_bclr(enc, CTL_REG_EIR, ENC_EIR_TXERIF);
  494. }
  495. #ifdef CONFIG_USE_IRQ
  496. /* set global interrupt enable bit in enc28j60 */
  497. enc_bset(enc, CTL_REG_EIE, ENC_EIE_INTIE);
  498. #endif
  499. }
  500. /*
  501. * Completely Reset the ENC
  502. */
  503. static void enc_reset(enc_dev_t *enc)
  504. {
  505. u8 dout[1];
  506. dout[0] = CMD_SRC;
  507. spi_xfer(enc->slave, 8, dout, NULL,
  508. SPI_XFER_BEGIN | SPI_XFER_END);
  509. /* sleep 1 ms. See errata pt. 2 */
  510. udelay(1000);
  511. }
  512. /*
  513. * Initialisation data for most of the ENC registers
  514. */
  515. static const u16 enc_initdata[] = {
  516. /*
  517. * Setup the buffer space. The reset values are valid for the
  518. * other pointers.
  519. *
  520. * We shall not write to ERXST, see errata pt. 5. Instead we
  521. * have to make sure that ENC_RX_BUS_START is 0.
  522. */
  523. CTL_REG_ERXSTL, ENC_RX_BUF_START,
  524. CTL_REG_ERXSTH, ENC_RX_BUF_START >> 8,
  525. CTL_REG_ERXNDL, ENC_RX_BUF_END,
  526. CTL_REG_ERXNDH, ENC_RX_BUF_END >> 8,
  527. CTL_REG_ERDPTL, ENC_RX_BUF_START,
  528. CTL_REG_ERDPTH, ENC_RX_BUF_START >> 8,
  529. /*
  530. * Set the filter to receive only good-CRC, unicast and broadcast
  531. * frames.
  532. * Note: some DHCP servers return their answers as broadcasts!
  533. * So its unwise to remove broadcast from this. This driver
  534. * might incur receiver overruns with packet loss on a broadcast
  535. * flooded network.
  536. */
  537. CTL_REG_ERXFCON, ENC_RFR_BCEN | ENC_RFR_UCEN | ENC_RFR_CRCEN,
  538. /* enable MAC to receive frames */
  539. CTL_REG_MACON1,
  540. ENC_MACON1_MARXEN | ENC_MACON1_TXPAUS | ENC_MACON1_RXPAUS,
  541. /* configure pad, tx-crc and duplex */
  542. CTL_REG_MACON3,
  543. ENC_MACON3_PADCFG0 | ENC_MACON3_TXCRCEN |
  544. ENC_MACON3_FRMLNEN,
  545. /* Allow infinite deferals if the medium is continously busy */
  546. CTL_REG_MACON4, ENC_MACON4_DEFER,
  547. /* Late collisions occur beyond 63 bytes */
  548. CTL_REG_MACLCON2, 63,
  549. /*
  550. * Set (low byte) Non-Back-to_Back Inter-Packet Gap.
  551. * Recommended 0x12
  552. */
  553. CTL_REG_MAIPGL, 0x12,
  554. /*
  555. * Set (high byte) Non-Back-to_Back Inter-Packet Gap.
  556. * Recommended 0x0c for half-duplex. Nothing for full-duplex
  557. */
  558. CTL_REG_MAIPGH, 0x0C,
  559. /* set maximum frame length */
  560. CTL_REG_MAMXFLL, ENC_MAX_FRM_LEN,
  561. CTL_REG_MAMXFLH, ENC_MAX_FRM_LEN >> 8,
  562. /*
  563. * Set MAC back-to-back inter-packet gap.
  564. * Recommended 0x12 for half duplex
  565. * and 0x15 for full duplex.
  566. */
  567. CTL_REG_MABBIPG, 0x12,
  568. /* end of table */
  569. 0xffff
  570. };
  571. /*
  572. * Wait for the XTAL oscillator to become ready
  573. */
  574. static int enc_clock_wait(enc_dev_t *enc)
  575. {
  576. uint64_t etime;
  577. /* one second timeout */
  578. etime = get_ticks() + get_tbclk();
  579. /*
  580. * Wait for CLKRDY to become set (i.e., check that we can
  581. * communicate with the ENC)
  582. */
  583. do
  584. {
  585. if (enc_r8(enc, CTL_REG_ESTAT) & ENC_ESTAT_CLKRDY)
  586. return 0;
  587. } while (get_ticks() <= etime);
  588. printf("%s: timeout waiting for CLKRDY\n", enc->dev->name);
  589. return -1;
  590. }
  591. /*
  592. * Write the MAC address into the ENC
  593. */
  594. static int enc_write_macaddr(enc_dev_t *enc)
  595. {
  596. unsigned char *p = enc->dev->enetaddr;
  597. enc_w8_retry(enc, CTL_REG_MAADR5, *p++, 5);
  598. enc_w8_retry(enc, CTL_REG_MAADR4, *p++, 5);
  599. enc_w8_retry(enc, CTL_REG_MAADR3, *p++, 5);
  600. enc_w8_retry(enc, CTL_REG_MAADR2, *p++, 5);
  601. enc_w8_retry(enc, CTL_REG_MAADR1, *p++, 5);
  602. enc_w8_retry(enc, CTL_REG_MAADR0, *p, 5);
  603. return 0;
  604. }
  605. /*
  606. * Setup most of the ENC registers
  607. */
  608. static int enc_setup(enc_dev_t *enc)
  609. {
  610. u16 phid1 = 0;
  611. u16 phid2 = 0;
  612. const u16 *tp;
  613. /* reset enc struct values */
  614. enc->next_pointer = ENC_RX_BUF_START;
  615. enc->rx_reset_counter = RX_RESET_COUNTER;
  616. enc->bank = 0xff; /* invalidate current bank in enc28j60 */
  617. /* verify PHY identification */
  618. phid1 = phy_read(enc, PHY_REG_PHID1);
  619. phid2 = phy_read(enc, PHY_REG_PHID2) & ENC_PHID2_MASK;
  620. if (phid1 != ENC_PHID1_VALUE || phid2 != ENC_PHID2_VALUE) {
  621. printf("%s: failed to identify PHY. Found %04x:%04x\n",
  622. enc->dev->name, phid1, phid2);
  623. return -1;
  624. }
  625. /* now program registers */
  626. for (tp = enc_initdata; *tp != 0xffff; tp += 2)
  627. enc_w8_retry(enc, tp[0], tp[1], 10);
  628. /*
  629. * Prevent automatic loopback of data beeing transmitted by setting
  630. * ENC_PHCON2_HDLDIS
  631. */
  632. phy_write(enc, PHY_REG_PHCON2, (1<<8));
  633. /*
  634. * LEDs configuration
  635. * LEDA: LACFG = 0100 -> display link status
  636. * LEDB: LBCFG = 0111 -> display TX & RX activity
  637. * STRCH = 1 -> LED pulses
  638. */
  639. phy_write(enc, PHY_REG_PHLCON, 0x0472);
  640. /* Reset PDPXMD-bit => half duplex */
  641. phy_write(enc, PHY_REG_PHCON1, 0);
  642. #ifdef CONFIG_USE_IRQ
  643. /* enable interrupts */
  644. enc_bset(enc, CTL_REG_EIE, ENC_EIE_PKTIE);
  645. enc_bset(enc, CTL_REG_EIE, ENC_EIE_TXIE);
  646. enc_bset(enc, CTL_REG_EIE, ENC_EIE_RXERIE);
  647. enc_bset(enc, CTL_REG_EIE, ENC_EIE_TXERIE);
  648. enc_bset(enc, CTL_REG_EIE, ENC_EIE_INTIE);
  649. #endif
  650. return 0;
  651. }
  652. /*
  653. * Check if ENC has been initialized.
  654. * If not, try to initialize it.
  655. * Remember initialized state in struct.
  656. */
  657. static int enc_initcheck(enc_dev_t *enc, const enum enc_initstate requiredstate)
  658. {
  659. if (enc->initstate >= requiredstate)
  660. return 0;
  661. if (enc->initstate < setupdone) {
  662. /* Initialize the ENC only */
  663. enc_reset(enc);
  664. /* if any of functions fails, skip the rest and return an error */
  665. if (enc_clock_wait(enc) || enc_setup(enc) || enc_write_macaddr(enc)) {
  666. return -1;
  667. }
  668. enc->initstate = setupdone;
  669. }
  670. /* if that's all we need, return here */
  671. if (enc->initstate >= requiredstate)
  672. return 0;
  673. /* now wait for link ready condition */
  674. if (enc_phy_link_wait(enc)) {
  675. return -1;
  676. }
  677. enc->initstate = linkready;
  678. return 0;
  679. }
  680. #if defined(CONFIG_CMD_MII)
  681. /*
  682. * Read a PHY register.
  683. *
  684. * This function is registered with miiphy_register().
  685. */
  686. int enc_miiphy_read(const char *devname, u8 phy_adr, u8 reg, u16 *value)
  687. {
  688. struct eth_device *dev = eth_get_dev_by_name(devname);
  689. enc_dev_t *enc;
  690. if (!dev || phy_adr != 0)
  691. return -1;
  692. enc = dev->priv;
  693. if (enc_claim_bus(enc))
  694. return -1;
  695. if (enc_initcheck(enc, setupdone)) {
  696. enc_release_bus(enc);
  697. return -1;
  698. }
  699. *value = phy_read(enc, reg);
  700. enc_release_bus(enc);
  701. return 0;
  702. }
  703. /*
  704. * Write a PHY register.
  705. *
  706. * This function is registered with miiphy_register().
  707. */
  708. int enc_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value)
  709. {
  710. struct eth_device *dev = eth_get_dev_by_name(devname);
  711. enc_dev_t *enc;
  712. if (!dev || phy_adr != 0)
  713. return -1;
  714. enc = dev->priv;
  715. if (enc_claim_bus(enc))
  716. return -1;
  717. if (enc_initcheck(enc, setupdone)) {
  718. enc_release_bus(enc);
  719. return -1;
  720. }
  721. phy_write(enc, reg, value);
  722. enc_release_bus(enc);
  723. return 0;
  724. }
  725. #endif
  726. /*
  727. * Write hardware (MAC) address.
  728. *
  729. * This function entered into eth_device structure.
  730. */
  731. static int enc_write_hwaddr(struct eth_device *dev)
  732. {
  733. enc_dev_t *enc = dev->priv;
  734. if (enc_claim_bus(enc))
  735. return -1;
  736. if (enc_initcheck(enc, setupdone)) {
  737. enc_release_bus(enc);
  738. return -1;
  739. }
  740. enc_release_bus(enc);
  741. return 0;
  742. }
  743. /*
  744. * Initialize ENC28J60 for use.
  745. *
  746. * This function entered into eth_device structure.
  747. */
  748. static int enc_init(struct eth_device *dev, bd_t *bis)
  749. {
  750. enc_dev_t *enc = dev->priv;
  751. if (enc_claim_bus(enc))
  752. return -1;
  753. if (enc_initcheck(enc, linkready)) {
  754. enc_release_bus(enc);
  755. return -1;
  756. }
  757. /* enable receive */
  758. enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_RXEN);
  759. enc_release_bus(enc);
  760. return 0;
  761. }
  762. /*
  763. * Check for received packets.
  764. *
  765. * This function entered into eth_device structure.
  766. */
  767. static int enc_recv(struct eth_device *dev)
  768. {
  769. enc_dev_t *enc = dev->priv;
  770. if (enc_claim_bus(enc))
  771. return -1;
  772. if (enc_initcheck(enc, linkready)) {
  773. enc_release_bus(enc);
  774. return -1;
  775. }
  776. /* Check for dead receiver */
  777. if (enc->rx_reset_counter > 0)
  778. enc->rx_reset_counter--;
  779. else
  780. enc_reset_rx_call(enc);
  781. enc_poll(enc);
  782. enc_release_bus(enc);
  783. return 0;
  784. }
  785. /*
  786. * Send a packet.
  787. *
  788. * This function entered into eth_device structure.
  789. *
  790. * Should we wait here until we have a Link? Or shall we leave that to
  791. * protocol retries?
  792. */
  793. static int enc_send(
  794. struct eth_device *dev,
  795. volatile void *packet,
  796. int length)
  797. {
  798. enc_dev_t *enc = dev->priv;
  799. if (enc_claim_bus(enc))
  800. return -1;
  801. if (enc_initcheck(enc, linkready)) {
  802. enc_release_bus(enc);
  803. return -1;
  804. }
  805. /* setup transmit pointers */
  806. enc_w16(enc, CTL_REG_EWRPTL, ENC_TX_BUF_START);
  807. enc_w16(enc, CTL_REG_ETXNDL, length + ENC_TX_BUF_START);
  808. enc_w16(enc, CTL_REG_ETXSTL, ENC_TX_BUF_START);
  809. /* write packet to ENC */
  810. enc_wbuf(enc, length, (u8 *) packet, 0x00);
  811. /*
  812. * Check that the internal transmit logic has not been altered
  813. * by excessive collisions. Reset transmitter if so.
  814. * See Errata B4 12 and 14.
  815. */
  816. if (enc_r8(enc, CTL_REG_EIR) & ENC_EIR_TXERIF) {
  817. enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_TXRST);
  818. enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_TXRST);
  819. }
  820. enc_bclr(enc, CTL_REG_EIR, (ENC_EIR_TXERIF | ENC_EIR_TXIF));
  821. /* start transmitting */
  822. enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_TXRTS);
  823. enc_release_bus(enc);
  824. return 0;
  825. }
  826. /*
  827. * Finish use of ENC.
  828. *
  829. * This function entered into eth_device structure.
  830. */
  831. static void enc_halt(struct eth_device *dev)
  832. {
  833. enc_dev_t *enc = dev->priv;
  834. if (enc_claim_bus(enc))
  835. return;
  836. /* Just disable receiver */
  837. enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_RXEN);
  838. enc_release_bus(enc);
  839. }
  840. /*
  841. * This is the only exported function.
  842. *
  843. * It may be called several times with different bus:cs combinations.
  844. */
  845. int enc28j60_initialize(unsigned int bus, unsigned int cs,
  846. unsigned int max_hz, unsigned int mode)
  847. {
  848. struct eth_device *dev;
  849. enc_dev_t *enc;
  850. /* try to allocate, check and clear eth_device object */
  851. dev = malloc(sizeof(*dev));
  852. if (!dev) {
  853. return -1;
  854. }
  855. memset(dev, 0, sizeof(*dev));
  856. /* try to allocate, check and clear enc_dev_t object */
  857. enc = malloc(sizeof(*enc));
  858. if (!enc) {
  859. free(dev);
  860. return -1;
  861. }
  862. memset(enc, 0, sizeof(*enc));
  863. /* try to setup the SPI slave */
  864. enc->slave = spi_setup_slave(bus, cs, max_hz, mode);
  865. if (!enc->slave) {
  866. printf("enc28j60: invalid SPI device %i:%i\n", bus, cs);
  867. free(enc);
  868. free(dev);
  869. return -1;
  870. }
  871. enc->dev = dev;
  872. /* now fill the eth_device object */
  873. dev->priv = enc;
  874. dev->init = enc_init;
  875. dev->halt = enc_halt;
  876. dev->send = enc_send;
  877. dev->recv = enc_recv;
  878. dev->write_hwaddr = enc_write_hwaddr;
  879. sprintf(dev->name, "enc%i.%i", bus, cs);
  880. eth_register(dev);
  881. #if defined(CONFIG_CMD_MII)
  882. miiphy_register(dev->name, enc_miiphy_read, enc_miiphy_write);
  883. #endif
  884. return 0;
  885. }