smsc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Copyright (C) 2001-2006 by egnite Software GmbH
  3. * Copyright (C) 2009 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * $Id: smsc.c 4115 2012-04-12 21:06:13Z olereinhardt $
  37. *
  38. * WARNING! Do not use any part of Basemon for your own applications. WARNING!
  39. *
  40. * This is not a typical application sample. It overrides parts of Nut/OS to
  41. * keep it running on broken hardware.
  42. */
  43. #include <stdio.h>
  44. #include <sys/timer.h>
  45. #include "utils.h"
  46. #include "uart.h"
  47. #include "smscregs.h"
  48. #include "smsc.h"
  49. /*!
  50. * \brief Select specified PHY register for reading or writing.
  51. *
  52. * \param reg PHY register number.
  53. * \param we Should be 1 for write access, 0 for read access.
  54. *
  55. * \return Contents of the PHY interface rgister.
  56. */
  57. static uint8_t NicPhyRegSelect(uint8_t reg, uint8_t we)
  58. {
  59. uint8_t rs;
  60. uint8_t msk;
  61. uint8_t i;
  62. nic_bs(3);
  63. rs = (nic_inlb(NIC_MGMT) & ~(MGMT_MCLK | MGMT_MDO)) | MGMT_MDOE;
  64. /* Send idle pattern. */
  65. for (i = 0; i < 33; i++) {
  66. nic_outlb(NIC_MGMT, rs | MGMT_MDO);
  67. nic_outlb(NIC_MGMT, rs | MGMT_MDO | MGMT_MCLK);
  68. }
  69. /* Send start sequence. */
  70. nic_outlb(NIC_MGMT, rs);
  71. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  72. nic_outlb(NIC_MGMT, rs | MGMT_MDO);
  73. nic_outlb(NIC_MGMT, rs | MGMT_MDO | MGMT_MCLK);
  74. /* Write or read mode. */
  75. if (we) {
  76. nic_outlb(NIC_MGMT, rs);
  77. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  78. nic_outlb(NIC_MGMT, rs | MGMT_MDO);
  79. nic_outlb(NIC_MGMT, rs | MGMT_MDO | MGMT_MCLK);
  80. } else {
  81. nic_outlb(NIC_MGMT, rs | MGMT_MDO);
  82. nic_outlb(NIC_MGMT, rs | MGMT_MDO | MGMT_MCLK);
  83. nic_outlb(NIC_MGMT, rs);
  84. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  85. }
  86. /* Send PHY address. Zero is used for the internal PHY. */
  87. for (i = 0; i < 5; i++) {
  88. nic_outlb(NIC_MGMT, rs);
  89. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  90. }
  91. /* Send PHY register number. */
  92. for (msk = 0x10; msk; msk >>= 1) {
  93. if (reg & msk) {
  94. nic_outlb(NIC_MGMT, rs | MGMT_MDO);
  95. nic_outlb(NIC_MGMT, rs | MGMT_MDO | MGMT_MCLK);
  96. } else {
  97. nic_outlb(NIC_MGMT, rs);
  98. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  99. }
  100. }
  101. nic_outlb(NIC_MGMT, rs);
  102. return rs;
  103. }
  104. /*!
  105. * \brief Read contents of PHY register.
  106. *
  107. * \param reg PHY register number.
  108. *
  109. * \return Contents of the specified register.
  110. */
  111. static uint16_t NicPhyRead(uint8_t reg)
  112. {
  113. uint16_t rc = 0;
  114. uint8_t rs;
  115. uint8_t i;
  116. /* Select register for reading. */
  117. rs = NicPhyRegSelect(reg, 0);
  118. /* Switch data direction. */
  119. rs &= ~MGMT_MDOE;
  120. nic_outlb(NIC_MGMT, rs);
  121. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  122. /* Clock data in. */
  123. for (i = 0; i < 16; i++) {
  124. nic_outlb(NIC_MGMT, rs);
  125. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  126. rc <<= 1;
  127. rc |= (nic_inlb(NIC_MGMT) & MGMT_MDI) != 0;
  128. }
  129. /* This will set the clock line to low. */
  130. nic_outlb(NIC_MGMT, rs);
  131. return rc;
  132. }
  133. /*!
  134. * \brief Write value to PHY register.
  135. *
  136. * \param reg PHY register number.
  137. * \param val Value to write.
  138. */
  139. static void NicPhyWrite(uint8_t reg, uint16_t val)
  140. {
  141. uint16_t msk;
  142. uint8_t rs;
  143. /* Select register for writing. */
  144. rs = NicPhyRegSelect(reg, 1);
  145. /* Switch data direction dummy. */
  146. nic_outlb(NIC_MGMT, rs | MGMT_MDO);
  147. nic_outlb(NIC_MGMT, rs | MGMT_MDO | MGMT_MCLK);
  148. nic_outlb(NIC_MGMT, rs);
  149. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  150. /* Clock data out. */
  151. for (msk = 0x8000; msk; msk >>= 1) {
  152. if (val & msk) {
  153. nic_outlb(NIC_MGMT, rs | MGMT_MDO);
  154. nic_outlb(NIC_MGMT, rs | MGMT_MDO | MGMT_MCLK);
  155. } else {
  156. nic_outlb(NIC_MGMT, rs);
  157. nic_outlb(NIC_MGMT, rs | MGMT_MCLK);
  158. }
  159. }
  160. /* Set clock line low and output line int z-state. */
  161. nic_outlb(NIC_MGMT, rs & ~MGMT_MDOE);
  162. }
  163. /*!
  164. * \brief Configure the internal PHY.
  165. *
  166. * Reset the PHY and initiate auto-negotiation.
  167. */
  168. static int NicPhyConfig(void)
  169. {
  170. uint16_t phy_sor;
  171. uint16_t phy_sr;
  172. uint16_t phy_to;
  173. uint16_t mode;
  174. /*
  175. * Reset the PHY and wait until this self clearing bit
  176. * becomes zero. We sleep 63 ms before each poll and
  177. * give up after 3 retries.
  178. */
  179. NicPhyWrite(NIC_PHYCR, PHYCR_RST);
  180. for (phy_to = 0;; phy_to++) {
  181. Delay(100000);
  182. if ((NicPhyRead(NIC_PHYCR) & PHYCR_RST) == 0)
  183. break;
  184. if (phy_to > 3) {
  185. return -1;
  186. }
  187. }
  188. Delay(200000);
  189. /* Store PHY status output. */
  190. phy_sor = NicPhyRead(NIC_PHYSOR);
  191. /* Enable PHY interrupts. */
  192. NicPhyWrite(NIC_PHYMSK, PHYMSK_MLOSSSYN | PHYMSK_MCWRD | PHYMSK_MSSD |
  193. PHYMSK_MESD | PHYMSK_MRPOL | PHYMSK_MJAB | PHYMSK_MSPDDT | PHYMSK_MDPLDT);
  194. /* Set RPC register. */
  195. mode = RPCR_ANEG | RPCR_LEDA_PAT | RPCR_LEDB_PAT;
  196. nic_bs(0);
  197. nic_outw(NIC_RPCR, mode);
  198. /*
  199. * Advertise our capabilities, initiate auto negotiation
  200. * and wait until this has been completed.
  201. */
  202. NicPhyWrite(NIC_PHYANAD, PHYANAD_TX_FDX | PHYANAD_TX_HDX | PHYANAD_10FDX | PHYANAD_10_HDX | PHYANAD_CSMA);
  203. for (phy_to = 0, phy_sr = 0;; phy_to++) {
  204. /* Give up after long time wait. */
  205. if (phy_to >= 32) {
  206. return -1;
  207. }
  208. /* Restart auto negotiation every 4 seconds or on failures. */
  209. if ((phy_to & 127) == 0 /* || (phy_sr & PHYSR_REM_FLT) != 0 */) {
  210. NicPhyWrite(NIC_PHYCR, PHYCR_ANEG_EN | PHYCR_ANEG_RST);
  211. Delay(200000);
  212. }
  213. /* Check if link status detected. */
  214. phy_sr = NicPhyRead(NIC_PHYSR);
  215. if (phy_sr & PHYSR_ANEG_ACK)
  216. break;
  217. Delay(300000);
  218. }
  219. return 0;
  220. }
  221. /*!
  222. * \brief Wait until MMU is ready.
  223. *
  224. * Poll the MMU command register until \ref MMUCR_BUSY
  225. * is cleared.
  226. *
  227. * \param tmo Timeout in milliseconds.
  228. *
  229. * \return 0 on success or -1 on timeout.
  230. */
  231. static INLINE int NicMmuWait(uint16_t tmo)
  232. {
  233. while (tmo--) {
  234. if ((nic_inlb(NIC_MMUCR) & MMUCR_BUSY) == 0)
  235. break;
  236. Delay(2000);
  237. }
  238. return tmo ? 0 : -1;
  239. }
  240. /*!
  241. * \brief Reset the Ethernet controller.
  242. *
  243. * \return 0 on success, -1 otherwise.
  244. */
  245. static int NicReset(void)
  246. {
  247. /* Disable all interrupts. */
  248. nic_outlb(NIC_MSK, 0);
  249. /* MAC and PHY software reset. */
  250. nic_bs(0);
  251. nic_outw(NIC_RCR, RCR_SOFT_RST);
  252. /* Enable Ethernet protocol handler. */
  253. nic_bs(1);
  254. nic_outw(NIC_CR, CR_EPH_EN);
  255. Delay(20000);
  256. /* Disable transmit and receive. */
  257. nic_bs(0);
  258. nic_outw(NIC_RCR, 0);
  259. nic_outw(NIC_TCR, 0);
  260. /* Enable auto release. */
  261. nic_bs(1);
  262. nic_outw(NIC_CTR, CTR_AUTO_RELEASE);
  263. /* Reset MMU. */
  264. nic_bs(2);
  265. nic_outlb(NIC_MMUCR, MMU_RST);
  266. if (NicMmuWait(1000))
  267. return -1;
  268. return 0;
  269. }
  270. int SmscDetect(void)
  271. {
  272. uint8_t bv;
  273. /* High byte of base select is always 0x33. */
  274. if((bv = nic_inhb(NIC_BSR)) != 0x33) {
  275. return -1;
  276. }
  277. /* Read revision. */
  278. nic_bs(3);
  279. if(((bv = nic_inlb(NIC_REV)) & 0xF0) != 0x90) {
  280. return -1;
  281. }
  282. return 0;
  283. }
  284. /*!
  285. * \brief Test NIC interrupt line.
  286. */
  287. static int SmscTestInterrupt(void)
  288. {
  289. uint8_t tmo;
  290. #if defined (__AVR__)
  291. /*
  292. * Set PE5 to input. This is our interrupt signal line.
  293. */
  294. cbi(DDRE, 5);
  295. if (NicReset()) {
  296. puts("reset failed");
  297. return -1;
  298. }
  299. if(bit_is_set(PINE, 5)) {
  300. puts("IRQ stuck");
  301. return -1;
  302. }
  303. #endif
  304. /* Enable receiver. */
  305. nic_bs(3);
  306. nic_outlb(NIC_ERCV, 7);
  307. nic_bs(0);
  308. nic_outw(NIC_RCR, RCR_RXEN);
  309. /* Enable transmitter and padding. */
  310. nic_outw(NIC_TCR, TCR_PAD_EN | TCR_TXENA);
  311. /* Configure the PHY. */
  312. if (NicPhyConfig()) {
  313. puts("link failed");
  314. return -1;
  315. }
  316. /* Allocate packet buffer space. */
  317. nic_bs(2);
  318. nic_outlb(NIC_MMUCR, MMU_ALO);
  319. if (NicMmuWait(100))
  320. return -1;
  321. /* Enable interrupts including allocation success. */
  322. nic_outlb(NIC_MSK, INT_ALLOC);
  323. /* Wait for allocation success. */
  324. tmo = 255;
  325. while ((nic_inlb(NIC_IST) & INT_ALLOC) == 0) {
  326. if(--tmo == 0) {
  327. puts("IRQ failed");
  328. return -1;
  329. }
  330. Delay(2000);
  331. }
  332. #if defined (__AVR__)
  333. if(bit_is_clear(PINE, 5)) {
  334. puts("no IRQ");
  335. return -1;
  336. }
  337. #endif
  338. return 0;
  339. }
  340. /*!
  341. * \brief Test NIC RAM buffer.
  342. */
  343. static int SmscTestBuffer(void)
  344. {
  345. ureg_t i;
  346. ureg_t tmo;
  347. uint16_t cnt;
  348. uint16_t val;
  349. /* Disable interrupts. */
  350. if (NicReset()) {
  351. puts("reset failed");
  352. return -1;
  353. }
  354. for(i = 0; i < 3; i++) {
  355. /* Allocate packet buffer space. */
  356. nic_bs(2);
  357. nic_outlb(NIC_MMUCR, MMU_ALO);
  358. if (NicMmuWait(100)) {
  359. printf("Alloc failed\n");
  360. return -1;
  361. }
  362. /* Wait for allocation success. */
  363. tmo = 255;
  364. while ((nic_inlb(NIC_IST) & INT_ALLOC) == 0) {
  365. if(--tmo == 0) {
  366. puts("IRQ failed");
  367. return -1;
  368. }
  369. Delay(2000);
  370. }
  371. /*
  372. * Transfer the allocated packet number to TX packet number.
  373. */
  374. nic_outlb(NIC_PNR, nic_inhb(NIC_PNR));
  375. /*
  376. * Reset the pointer register, no auto increment.
  377. */
  378. nic_outw(NIC_PTR, 0);
  379. /*
  380. * Data write.
  381. */
  382. #ifdef HEARTBEAT_BIT
  383. HeartBeat();
  384. #endif
  385. for(cnt = 0; cnt < 1024; cnt++) {
  386. val = ~cnt;
  387. nic_outw(NIC_PTR, cnt * 2);
  388. nic_outlb(NIC_DATA, (uint8_t)val);
  389. nic_outw(NIC_PTR, cnt * 2 + 1);
  390. nic_outlb(NIC_DATA, val >> 8);
  391. }
  392. /*
  393. * Data read.
  394. */
  395. #ifdef HEARTBEAT_BIT
  396. HeartBeat();
  397. #endif
  398. for(cnt = 0; cnt < 1024; cnt++) {
  399. nic_outw(NIC_PTR, PTR_READ | (cnt * 2));
  400. if((val = ~nic_inw(NIC_DATA)) != cnt) {
  401. printf("bad val %04X at %04X\n", val, cnt);
  402. return -1;
  403. }
  404. }
  405. }
  406. return 0;
  407. }
  408. /*!
  409. * \brief Test NIC functions.
  410. */
  411. int SmscTest(void)
  412. {
  413. if(SmscTestInterrupt()) {
  414. puts("IRQ failed");
  415. return -1;
  416. }
  417. if(SmscTestBuffer()) {
  418. puts("Buf failed");
  419. return -1;
  420. }
  421. puts("OK");
  422. return 0;
  423. }
  424. /*
  425. * \brief Continously send baroadcasts.
  426. */
  427. void SmscSend(void)
  428. {
  429. uint8_t mac[] = { 0x00, 0x06, 0x98, 0x00, 0x00, 0x00 };
  430. ureg_t i;
  431. uint16_t sz;
  432. uint32_t cnt = 0;
  433. printf("\nInit controller...");
  434. if (NicReset()) {
  435. puts("reset failed");
  436. return;
  437. }
  438. /* Enable receiver. */
  439. nic_bs(3);
  440. nic_outlb(NIC_ERCV, 7);
  441. nic_bs(0);
  442. //nic_outw(NIC_RCR, RCR_RXEN);
  443. /* Enable transmitter and padding. */
  444. nic_outw(NIC_TCR, TCR_PAD_EN | TCR_TXENA);
  445. /* Configure the PHY. */
  446. if (NicPhyConfig()) {
  447. puts("link failed");
  448. return;
  449. }
  450. /* Set MAC address. */
  451. nic_bs(1);
  452. for (i = 0; i < 6; i++)
  453. nic_outlb(NIC_IAR + i, mac[i]);
  454. puts("OK");
  455. for(cnt = 0;; cnt++) {
  456. Delay(500000);
  457. printf("\r%lu", cnt);
  458. sz = 1500;
  459. /* Allocate packet buffer space. */
  460. nic_bs(2);
  461. nic_outlb(NIC_MMUCR, MMU_ALO);
  462. if (NicMmuWait(100)) {
  463. puts("Alloc");
  464. break;
  465. }
  466. /* Enable allocation interrupt. */
  467. nic_outlb(NIC_MSK, INT_ALLOC);
  468. /*
  469. * Wait for allocation success. This fails quite often, possibly
  470. * because we do not clear our receive buffer.
  471. */
  472. if ((nic_inlb(NIC_IST) & INT_ALLOC) == 0) {
  473. puts(" Alloc");
  474. Delay(500000);
  475. if ((nic_inlb(NIC_IST) & INT_ALLOC) == 0) {
  476. nic_outlb(NIC_MMUCR, MMU_RST);
  477. NicMmuWait(1000);
  478. nic_outlb(NIC_MMUCR, MMU_ALO);
  479. if (NicMmuWait(100) || (nic_inlb(NIC_IST) & INT_ALLOC) == 0) {
  480. break;
  481. }
  482. }
  483. }
  484. /* Disable interrupts. */
  485. nic_outlb(NIC_MSK, 0);
  486. nic_outlb(NIC_PNR, nic_inhb(NIC_PNR));
  487. nic_outw(NIC_PTR, 0x4000);
  488. /* Transfer control word. */
  489. nic_outlb(NIC_DATA, 0);
  490. nic_outlb(NIC_DATA, 0);
  491. /* Transfer the byte count. */
  492. nic_outw(NIC_DATA, sz);
  493. /* Transfer the Ethernet frame. */
  494. for (i = 0; i < 6; i++) {
  495. nic_outlb(NIC_DATA, 0xFF);
  496. }
  497. for (i = 0; i < 6; i++) {
  498. nic_outlb(NIC_DATA, mac[i]);
  499. }
  500. nic_outlb(NIC_DATA, 0x08);
  501. nic_outlb(NIC_DATA, 0x00);
  502. /*
  503. * Add pad bytes.
  504. */
  505. while (sz--) {
  506. nic_outlb(NIC_DATA, 0x00);
  507. }
  508. /* Transfer the control word. */
  509. nic_outlb(NIC_DATA, 0);
  510. nic_outlb(NIC_DATA, 0);
  511. /* Enqueue packet. */
  512. if (NicMmuWait(100)) {
  513. puts("Enqueue");
  514. break;
  515. }
  516. nic_outlb(NIC_MMUCR, MMU_ENQ);
  517. if (GetChar())
  518. break;
  519. }
  520. }
  521. void SmscLoop(void)
  522. {
  523. printf_P(presskey_P);
  524. for (;;) {
  525. nic_bs(3);
  526. printf("\rrev=0x%02X ", nic_inlb(NIC_REV));
  527. if (GetChar()) {
  528. puts("");
  529. return;
  530. }
  531. }
  532. }