ether.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH
  3. * Copyright (C) 2010 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. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  37. *
  38. * Permission to use, copy, modify, and distribute this software for any
  39. * purpose with or without fee is hereby granted, provided that the above
  40. * copyright notice and this permission notice appear in all copies, and that
  41. * the name of Digital Equipment Corporation not be used in advertising or
  42. * publicity pertaining to distribution of the document or software without
  43. * specific, written prior permission.
  44. *
  45. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  46. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  47. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  48. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  49. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  50. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  51. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  52. * SOFTWARE.
  53. */
  54. /*
  55. * $Id: ether.c 4115 2012-04-12 21:06:13Z olereinhardt $
  56. */
  57. #include <string.h>
  58. #include <avr/io.h>
  59. #include "config.h"
  60. #include "rtlregs.h"
  61. #include "util.h"
  62. #include "eboot.h"
  63. #include "arp.h"
  64. #include "ether.h"
  65. /*!
  66. * \addtogroup xgStack
  67. */
  68. /*@{*/
  69. #define NIC_PAGE_SIZE 0x100
  70. #define NIC_START_PAGE 0x40
  71. #define NIC_STOP_PAGE 0x60
  72. #define NIC_TX_PAGES 6
  73. #define NIC_TX_BUFFERS 2
  74. #define NIC_FIRST_TX_PAGE NIC_START_PAGE
  75. #define NIC_FIRST_RX_PAGE (NIC_FIRST_TX_PAGE + NIC_TX_PAGES * NIC_TX_BUFFERS)
  76. #define TX_PAGES 12
  77. unsigned char EEPROM_read(unsigned int uiAddress)
  78. {
  79. /* Wait for completion of previous write */
  80. #if defined(__AVR_ATmega2561__)
  81. while(EECR & (1<<EEPE));
  82. #else
  83. while(EECR & (1<<EEWE));
  84. #endif
  85. /* Set up address register */
  86. EEAR = uiAddress;
  87. /* Start eeprom read by writing EERE */
  88. EECR |= (1<<EERE);
  89. /* Return data from data register */
  90. return EEDR;
  91. }
  92. /*!
  93. * Realtek packet header.
  94. */
  95. struct nic_pkt_header {
  96. u_char ph_status; /*!< \brief Status, contents of RSR register */
  97. u_char ph_nextpg; /*!< \brief Page for next packet */
  98. u_short ph_size; /*!< \brief Size of header and packet in octets */
  99. };
  100. static void EmulateNicEeprom(void)
  101. {
  102. register u_int cnt;
  103. /*
  104. * Prepare the EEPROM emulation port bits. Configure the EEDO
  105. * and the EEMU lines as outputs and set both lines to high.
  106. */
  107. PORTC = 0xC0;
  108. DDRC = 0xC0;
  109. Delay(20);
  110. /*
  111. * Force the chip to re-read the EEPROM contents.
  112. */
  113. NIC_CR = NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1;
  114. NIC_PG3_EECR = NIC_EECR_EEM0;
  115. /*
  116. * No external memory access beyond this point.
  117. */
  118. #if defined(__AVR_ATmega2561__) || defined(__AVR_AT90CAN128__)
  119. XMCRA &= ~_BV(SRE);
  120. #else
  121. MCUCR &= ~_BV(SRE);
  122. #endif
  123. /*
  124. * Loop until the chip stops toggling our EESK input.
  125. */
  126. do {
  127. cnt = 0;
  128. if(bit_is_set(PINC, 5)) {
  129. while(++cnt && bit_is_set(PINC, 5));
  130. }
  131. else {
  132. while(++cnt && bit_is_clear(PINC, 5));
  133. }
  134. } while(cnt);
  135. /*
  136. * Enable memory interface.
  137. */
  138. #if defined(__AVR_ATmega2561__) || defined(__AVR_AT90CAN128__)
  139. XMCRA |= _BV(SRE);
  140. #else
  141. MCUCR |= _BV(SRE);
  142. #endif
  143. /* Reset port outputs to default. */
  144. PORTC = 0x00;
  145. DDRC = 0x00;
  146. /* Wait until controller ready. */
  147. while(NIC_CR != (NIC_CR_STP | NIC_CR_RD2));
  148. }
  149. /*!
  150. * \brief Initialize the NIC.
  151. *
  152. * For further description of the initialization
  153. * please refer to the original Ethernut code.
  154. */
  155. int NicInit(void)
  156. {
  157. u_char c;
  158. /*
  159. * Enable external data and address
  160. * bus.
  161. */
  162. #if defined(__AVR_ATmega2561__) || defined(__AVR_AT90CAN128__)
  163. XMCRA = (_BV(SRE) | _BV(SRW10));
  164. #else
  165. MCUCR = (_BV(SRE) | _BV(SRW));
  166. #endif
  167. c = NIC_RESET;
  168. Delay(5);
  169. NIC_RESET = c;
  170. Delay(1000);
  171. EmulateNicEeprom();
  172. NIC_PG0_IMR = 0;
  173. NIC_PG0_ISR = 0xff;
  174. NIC_CR = NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1;
  175. NIC_PG3_EECR = NIC_EECR_EEM0 | NIC_EECR_EEM1;
  176. NIC_PG3_CONFIG3 = 0;
  177. NIC_PG3_CONFIG2 = NIC_CONFIG2_BSELB;
  178. NIC_PG3_EECR = 0;
  179. Delay(200);
  180. NIC_CR = NIC_CR_STP | NIC_CR_RD2;
  181. NIC_PG0_DCR = NIC_DCR_LS | NIC_DCR_FT1;
  182. NIC_PG0_RBCR0 = 0;
  183. NIC_PG0_RBCR1 = 0;
  184. NIC_PG0_RCR = NIC_RCR_MON;
  185. NIC_PG0_TCR = NIC_TCR_LB0;
  186. NIC_PG0_TPSR = NIC_FIRST_TX_PAGE;
  187. NIC_PG0_BNRY = NIC_STOP_PAGE - 1;
  188. NIC_PG0_PSTART = NIC_FIRST_RX_PAGE;
  189. NIC_PG0_PSTOP = NIC_STOP_PAGE;
  190. NIC_PG0_ISR = 0xff;
  191. NIC_CR = NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0;
  192. NIC_PG1_PAR0 = confnet.cdn_mac[0];
  193. NIC_PG1_PAR1 = confnet.cdn_mac[1];
  194. NIC_PG1_PAR2 = confnet.cdn_mac[2];
  195. NIC_PG1_PAR3 = confnet.cdn_mac[3];
  196. NIC_PG1_PAR4 = confnet.cdn_mac[4];
  197. NIC_PG1_PAR5 = confnet.cdn_mac[5];
  198. NIC_PG1_MAR0 = 0;
  199. NIC_PG1_MAR1 = 0;
  200. NIC_PG1_MAR2 = 0;
  201. NIC_PG1_MAR3 = 0;
  202. NIC_PG1_MAR4 = 0;
  203. NIC_PG1_MAR5 = 0;
  204. NIC_PG1_MAR6 = 0;
  205. NIC_PG1_MAR7 = 0;
  206. NIC_PG1_CURR = NIC_START_PAGE + TX_PAGES;
  207. NIC_CR = NIC_CR_STP | NIC_CR_RD2;
  208. NIC_PG0_RCR = NIC_RCR_AB;
  209. NIC_PG0_ISR = 0xff;
  210. NIC_PG0_IMR = 0;
  211. NIC_CR = NIC_CR_STA | NIC_CR_RD2;
  212. NIC_PG0_TCR = 0;
  213. Delay(1000);
  214. return 0;
  215. }
  216. /*!
  217. * \brief Send an Ethernet frame.
  218. *
  219. * \param dmac Destination MAC address.
  220. * \param type Frame type.
  221. * \param len Frame size.
  222. *
  223. * \return 0 on success, -1 otherwise.
  224. */
  225. int EtherOutput(u_char * dmac, u_short type, u_short len)
  226. {
  227. u_short i;
  228. u_short sz;
  229. u_char *cp;
  230. ETHERHDR *eh;
  231. if (type == ETHERTYPE_ARP) {
  232. eh = &arpframe.eth_hdr;
  233. cp = (u_char *) & arpframe;
  234. } else {
  235. eh = &sframe.eth_hdr;
  236. cp = (u_char *) & sframe;
  237. }
  238. for (i = 0; i < 6; i++)
  239. eh->ether_shost[i] = confnet.cdn_mac[i];
  240. if (dmac) {
  241. for (i = 0; i < 6; i++)
  242. eh->ether_dhost[i] = dmac[i];
  243. } else {
  244. for (i = 0; i < 6; i++)
  245. eh->ether_dhost[i] = 0xFF;
  246. }
  247. eh->ether_type = type;
  248. if ((len += sizeof(ETHERHDR)) < 60)
  249. sz = 60;
  250. else
  251. sz = len;
  252. NIC_CR = NIC_CR_STA | NIC_CR_RD2;
  253. NIC_PG0_RBCR0 = (u_char) sz;
  254. NIC_PG0_RBCR1 = (u_char) (sz >> 8);
  255. NIC_PG0_RSAR0 = 0;
  256. NIC_PG0_RSAR1 = NIC_FIRST_TX_PAGE;
  257. NIC_CR = NIC_CR_STA | NIC_CR_RD1;
  258. /*
  259. * Transfer frame.
  260. */
  261. for (i = 0; i < len; i++)
  262. NIC_IOPORT = *cp++;
  263. for (i = len; i < sz; i++)
  264. NIC_IOPORT = 0;
  265. /*
  266. * Complete remote dma.
  267. */
  268. NIC_CR = NIC_CR_STA | NIC_CR_RD2;
  269. for (i = 0; i <= 20; i++)
  270. if (NIC_PG0_ISR & NIC_ISR_RDC)
  271. break;
  272. NIC_PG0_ISR = NIC_ISR_RDC;
  273. /*
  274. * Number of bytes to be transmitted.
  275. */
  276. NIC_PG0_TBCR0 = (sz & 0xff);
  277. NIC_PG0_TBCR1 = ((sz >> 8) & 0xff);
  278. /*
  279. * First page of packet to be transmitted.
  280. */
  281. NIC_PG0_TPSR = NIC_FIRST_TX_PAGE;
  282. /*
  283. * Start transmission.
  284. */
  285. NIC_CR = NIC_CR_STA | NIC_CR_TXP | NIC_CR_RD2;
  286. /*
  287. * Wait until transmission is completed or aborted.
  288. */
  289. while (NIC_CR & NIC_CR_TXP);
  290. return 0;
  291. }
  292. /*!
  293. * \brief Receive an Ethernet frame.
  294. *
  295. * \param tms Return with timeout after the specified
  296. * number of waiting loops. On a 14 Mhz ATmega
  297. * this value represents approximately the number
  298. * of milliseconds to wait.
  299. *
  300. * \return The number of bytes received, 0 on timeout
  301. * or -1 in case of a failure.
  302. */
  303. int EtherInput(u_short type, u_short tms)
  304. {
  305. int rc = 0;
  306. u_short maxl = 0;
  307. u_char isr = 0;
  308. struct nic_pkt_header hdr;
  309. u_char *buf;
  310. u_char bnry;
  311. u_char curr;
  312. u_short i;
  313. u_char wtc;
  314. while (tms && rc == 0) {
  315. /*
  316. * Wait until frame received, timeout or receiver error.
  317. */
  318. for (wtc = 1; tms; wtc++) {
  319. isr = NIC_PG0_ISR;
  320. if (isr & NIC_ISR_RXE) {
  321. return -1;
  322. }
  323. if (isr & NIC_ISR_PRX) {
  324. break;
  325. }
  326. if (wtc == 0)
  327. tms--;
  328. }
  329. if ((isr & NIC_ISR_PRX) == 0) {
  330. NIC_PG0_ISR = isr;
  331. break;
  332. }
  333. /*
  334. * Get the current page pointer. It points to the page where the NIC
  335. * will start saving the next incoming packet.
  336. */
  337. NIC_CR = NIC_CR_STA | NIC_CR_RD2 | NIC_CR_PS0;
  338. curr = NIC_PG1_CURR;
  339. NIC_CR = NIC_CR_STA | NIC_CR_RD2;
  340. /*
  341. * Get the pointer to the last page we read from. The following page
  342. * is the one where we start reading. If it's equal to the current
  343. * page pointer, then there's nothing to read.
  344. */
  345. if ((bnry = NIC_PG0_BNRY + 1) >= NIC_STOP_PAGE)
  346. bnry = NIC_FIRST_RX_PAGE;
  347. if (bnry == curr) {
  348. NIC_PG0_ISR = isr;
  349. continue;
  350. }
  351. /*
  352. * Read the NIC specific packet header.
  353. */
  354. NIC_PG0_RBCR0 = sizeof(struct nic_pkt_header);
  355. NIC_PG0_RBCR1 = 0;
  356. NIC_PG0_RSAR0 = 0;
  357. NIC_PG0_RSAR1 = bnry;
  358. buf = (u_char *) & hdr;
  359. NIC_CR = NIC_CR_STA | NIC_CR_RD0;
  360. for (i = 0; i < sizeof(struct nic_pkt_header); i++)
  361. *buf++ = NIC_IOPORT;
  362. /*
  363. * Complete remote dma.
  364. */
  365. NIC_CR = NIC_CR_STA | NIC_CR_RD2;
  366. for (i = 0; i <= 20; i++) {
  367. if (NIC_PG0_ISR & NIC_ISR_RDC)
  368. break;
  369. }
  370. NIC_PG0_ISR = NIC_ISR_RDC;
  371. /*
  372. * Check frame length. If it's outside limits, we assume
  373. * that the NIC is corrupted. Reset and retry.
  374. */
  375. if (hdr.ph_size < 60 + sizeof(struct nic_pkt_header) || hdr.ph_size > 1518 + sizeof(struct nic_pkt_header)) {
  376. NicInit();
  377. continue;
  378. }
  379. /*
  380. * Check packet status.
  381. */
  382. if ((hdr.ph_status & 0x0F) == 1) {
  383. rc = hdr.ph_size - sizeof(struct nic_pkt_header);
  384. /*
  385. * Set remote dma byte count and
  386. * start address. Don't read the
  387. * header again.
  388. */
  389. NIC_PG0_RBCR0 = (u_char) rc;
  390. NIC_PG0_RBCR1 = (u_char) ((u_short) rc >> 8);
  391. NIC_PG0_RSAR0 = sizeof(struct nic_pkt_header);
  392. NIC_PG0_RSAR1 = bnry;
  393. /*
  394. * Perform the read.
  395. */
  396. NIC_CR = NIC_CR_STA | NIC_CR_RD0;
  397. buf = (u_char *) & rframe;
  398. maxl = sizeof(rframe) < (u_short)rc ? sizeof(rframe) : (u_short)rc;
  399. for (i = 0; i < maxl; i++) {
  400. *buf++ = NIC_IOPORT;
  401. }
  402. for (; i < (u_short) rc; i++)
  403. NIC_IOPORT;
  404. /*
  405. * Complete remote dma.
  406. */
  407. NIC_CR = NIC_CR_STA | NIC_CR_RD2;
  408. for (i = 0; i <= 20; i++) {
  409. if (NIC_PG0_ISR & NIC_ISR_RDC)
  410. break;
  411. }
  412. NIC_PG0_ISR = NIC_ISR_RDC;
  413. }
  414. /*
  415. * Set boundary register to the last page we read.
  416. */
  417. if (--hdr.ph_nextpg < NIC_FIRST_RX_PAGE)
  418. hdr.ph_nextpg = NIC_STOP_PAGE - 1;
  419. NIC_PG0_BNRY = hdr.ph_nextpg;
  420. /*
  421. * Handle incoming ARP requests.
  422. */
  423. if (rframe.eth_hdr.ether_type != type) {
  424. if (rframe.eth_hdr.ether_type == ETHERTYPE_ARP)
  425. ArpRespond();
  426. rc = 0;
  427. }
  428. }
  429. return rc;
  430. }
  431. /*@}*/