cs8900.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (C) 2002-2003 by Call Direct Cellular Solutions Pty. Ltd. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this
  14. * software must display the following acknowledgement:
  15. *
  16. * This product includes software developed by Call Direct Cellular Solutions Pty. Ltd.
  17. * and its contributors.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY CALL DIRECT CELLULAR SOLUTIONS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CALL DIRECT
  23. * CELLULAR SOLUTIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.calldirect.com.au/
  33. *
  34. * -
  35. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  36. *
  37. * Redistribution and use in source and binary forms, with or without
  38. * modification, are permitted provided that the following conditions
  39. * are met:
  40. *
  41. * 1. Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * 2. Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in the
  45. * documentation and/or other materials provided with the distribution.
  46. * 3. All advertising materials mentioning features or use of this
  47. * software must display the following acknowledgement:
  48. *
  49. * This product includes software developed by egnite Software GmbH
  50. * and its contributors.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  55. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  56. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  57. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  58. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  59. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  60. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  61. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  62. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  63. * SUCH DAMAGE.
  64. *
  65. * For additional information see http://www.ethernut.de/
  66. *
  67. * -
  68. * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
  69. *
  70. * This file is distributed in the hope that it will be useful, but WITHOUT
  71. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  72. * FITNESS FOR A PARTICULAR PURPOSE.
  73. *
  74. * You can redistribute this file and/or modify it under the terms of the GNU
  75. * General Public License (GPL) as published by the Free Software Foundation;
  76. * either version 2 of the License, or (at your discretion) any later version.
  77. * See the accompanying file "copying-gpl.txt" for more details.
  78. *
  79. * As a special exception to the GPL, permission is granted for additional
  80. * uses of the text contained in this file. See the accompanying file
  81. * "copying-liquorice.txt" for details.
  82. * -
  83. * Portions Copyright (c) 1983, 1993 by
  84. * The Regents of the University of California. All rights reserved.
  85. *
  86. * Redistribution and use in source and binary forms, with or without
  87. * modification, are permitted provided that the following conditions
  88. * are met:
  89. * 1. Redistributions of source code must retain the above copyright
  90. * notice, this list of conditions and the following disclaimer.
  91. * 2. Redistributions in binary form must reproduce the above copyright
  92. * notice, this list of conditions and the following disclaimer in the
  93. * documentation and/or other materials provided with the distribution.
  94. * 3. All advertising materials mentioning features or use of this software
  95. * must display the following acknowledgement:
  96. * This product includes software developed by the University of
  97. * California, Berkeley and its contributors.
  98. * 4. Neither the name of the University nor the names of its contributors
  99. * may be used to endorse or promote products derived from this software
  100. * without specific prior written permission.
  101. *
  102. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  103. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  104. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  105. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  106. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  107. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  108. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  109. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  110. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  111. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  112. * SUCH DAMAGE.
  113. * -
  114. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  115. *
  116. * Permission to use, copy, modify, and distribute this software for any
  117. * purpose with or without fee is hereby granted, provided that the above
  118. * copyright notice and this permission notice appear in all copies, and that
  119. * the name of Digital Equipment Corporation not be used in advertising or
  120. * publicity pertaining to distribution of the document or software without
  121. * specific, written prior permission.
  122. *
  123. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  124. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  125. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  126. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  127. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  128. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  129. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  130. * SOFTWARE.
  131. */
  132. /*!
  133. * \file arch/avr/dev/cs8900.c
  134. * \brief AVR network driver for CS8900A.
  135. *
  136. * \verbatim
  137. * $Id: cs8900.c 4706 2012-10-06 17:42:01Z haraldkipp $
  138. * \endverbatim
  139. */
  140. /* Not ported. */
  141. #ifdef __GNUC__
  142. #include <cfg/os.h>
  143. #include <string.h>
  144. #include <avr/interrupt.h>
  145. #include <sys/heap.h>
  146. #include <sys/thread.h>
  147. #include <sys/event.h>
  148. #include <sys/timer.h>
  149. #include <sys/confnet.h>
  150. #include <dev/nicrtl.h>
  151. #include <netinet/if_ether.h>
  152. #include <net/ether.h>
  153. #include <net/if_var.h>
  154. #include <netinet/ip.h>
  155. #include <dev/irqreg.h>
  156. #include "cs8900.h"
  157. #ifdef NUTDEBUG
  158. #include <sys/osdebug.h>
  159. #include <net/netdebug.h>
  160. #endif
  161. /*!
  162. * \addtogroup xgNicCs8900
  163. */
  164. /*@{*/
  165. // Ethernet flags byte
  166. // Bit 0 = transmit byte flag
  167. uint8_t cs_flags;
  168. volatile uint16_t cs_base = 0x0000;
  169. void CSWrite16(uint16_t addr, uint16_t data)
  170. {
  171. uint16_t *p;
  172. p = (uint16_t *) addr;
  173. cli();
  174. *p = data;
  175. sei();
  176. }
  177. void CSWritePP16(uint16_t addr, uint16_t data)
  178. {
  179. uint16_t *p;
  180. cli();
  181. p = (uint16_t *) CS_PP_PTR;
  182. *p = addr;
  183. CSWrite16(CS_PP_DATA0, data);
  184. return;
  185. }
  186. uint16_t CSRead16(uint16_t addr)
  187. {
  188. uint16_t *p;
  189. uint16_t d;
  190. cli();
  191. p = (uint16_t *) addr;
  192. d = *p;
  193. sei();
  194. return d;
  195. }
  196. uint16_t CSReadPP16(uint16_t addr)
  197. {
  198. uint16_t *p;
  199. cli();
  200. p = (uint16_t *) CS_PP_PTR;
  201. *p = addr;
  202. return CSRead16(CS_PP_DATA0);
  203. }
  204. uint32_t CSReadPP32(unsigned int addr)
  205. {
  206. uint32_t l;
  207. uint32_t *p;
  208. cli();
  209. p = (uint32_t *) CS_PP_PTR;
  210. *p = addr;
  211. p = (uint32_t *) CS_PP_DATA0;
  212. l = *p;
  213. sei();
  214. return l;
  215. }
  216. void CSBeginFrame(void)
  217. {
  218. cs_flags &= ~1;
  219. }
  220. void CSEndFrame(void)
  221. {
  222. uint8_t *p;
  223. cli();
  224. p = (uint8_t *) CS_DATA_P0 + 1;
  225. sei();
  226. // If odd number of bytes in packet pad it out
  227. if (cs_flags & 1)
  228. p = 0;
  229. }
  230. void CSWriteFrameByte(uint8_t data)
  231. {
  232. uint8_t *p;
  233. if (cs_flags & 1)
  234. p = (uint8_t *) CS_DATA_P0 + 1;
  235. else
  236. p = (uint8_t *) CS_DATA_P0;
  237. *p = data;
  238. cs_flags ^= 1;
  239. }
  240. static int CSEthPutPacket(NUTDEVICE * dev, NETBUF * nb)
  241. {
  242. uint16_t i;
  243. uint16_t sz;
  244. uint8_t *p;
  245. NICINFO *ni;
  246. ni = (NICINFO *) dev->dev_dcb;
  247. //
  248. // Calculate the number of bytes to be send. Do not
  249. // send packets larger than 1536 bytes.
  250. //
  251. sz = nb->nb_dl.sz + nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz;
  252. if (sz >= 0x600) {
  253. NutNetBufFree(nb);
  254. return -1;
  255. }
  256. #if 0
  257. if (tcp_trace) {
  258. NutPrintFormat_P(dev_debug, PSTR("[ETHTX-%u]\r\n"), sz);
  259. NutPrintFlush(dev_debug);
  260. }
  261. #endif
  262. // Start transmission after entire frame is loaded into CS8900
  263. CSWrite16(CS_TX_CMD_I, 0xC0);
  264. // Set frame size
  265. CSWrite16(CS_TX_LEN_I, sz);
  266. // Wait for buffer space, but only for a while (200ms)
  267. // If the cable is disconnected this will never become true
  268. // If we don't get the go ahead within 200ms return 0 (Sucess)
  269. // And let the upper layers deal with re-transmission
  270. // If we return failure TCP sockets will close straight away which probably
  271. // isn't the correct behaviour
  272. i = 0;
  273. while ((CSReadPP16(CS_BUS_STAT) & 0x0100) == 0) {
  274. i++;
  275. if (i > 20)
  276. return 0;
  277. NutSleep(10);
  278. }
  279. //
  280. // Transfer ethernet physical header.
  281. //
  282. CSBeginFrame();
  283. p = nb->nb_dl.vp;
  284. for (i = 0; i < nb->nb_dl.sz; i++) {
  285. CSWriteFrameByte(*p++);
  286. }
  287. p = nb->nb_nw.vp;
  288. for (i = 0; i < nb->nb_nw.sz; i++) {
  289. CSWriteFrameByte(*p++);
  290. }
  291. p = nb->nb_tp.vp;
  292. for (i = 0; i < nb->nb_tp.sz; i++) {
  293. CSWriteFrameByte(*p++);
  294. }
  295. p = nb->nb_ap.vp;
  296. for (i = 0; i < nb->nb_ap.sz; i++) {
  297. CSWriteFrameByte(*p++);
  298. }
  299. CSEndFrame();
  300. return 0;
  301. }
  302. /*!
  303. * \brief Send Ethernet packet.
  304. *
  305. * \param dev Identifies the device to use.
  306. * \param nb Network buffer structure containing the packet to be sent.
  307. * The structure must have been allocated by a previous
  308. * call NutNetBufAlloc(). This buffer will be automatically
  309. * released in case of an error.
  310. *
  311. * \return 0 on success, -1 in case of any errors. Errors
  312. * will automatically release the network buffer
  313. * structure.
  314. */
  315. int CSNicOutput(NUTDEVICE * dev, NETBUF * nb)
  316. {
  317. int rc = -1;
  318. NICINFO *ni;
  319. ni = (NICINFO *) dev->dev_dcb;
  320. #if 0
  321. if (tcp_trace) {
  322. NutPrintFormat_P(dev_debug, PSTR("Enter EthOutput\r\n"));
  323. NutPrintFlush(dev_debug);
  324. }
  325. #endif
  326. if ((rc = CSEthPutPacket(dev, nb)) == 0)
  327. ni->ni_tx_packets++;
  328. return rc;
  329. }
  330. /*! \fn CSNICrx(void *arg)
  331. * \brief NIC receiver thread.
  332. *
  333. *
  334. * It runs with high priority.
  335. */
  336. THREAD(CSNICrx, arg)
  337. {
  338. NUTDEVICE *dev;
  339. IFNET *ifn;
  340. NICINFO *ni;
  341. NETBUF *nb;
  342. uint8_t *p;
  343. uint8_t *q;
  344. uint16_t i, m;
  345. volatile uint16_t l;
  346. dev = arg;
  347. ifn = (IFNET *) dev->dev_icb;
  348. ni = (NICINFO *) dev->dev_dcb;
  349. #if 0
  350. if (tcp_trace) {
  351. NutPrintFormat_P(dev_debug, PSTR("Enter ETHReceive\r\n"));
  352. NutPrintFlush(dev_debug);
  353. }
  354. #endif
  355. l = 0;
  356. NutThreadSetPriority(8);
  357. for (;;) {
  358. while ((CSReadPP16(CS_RX_EVENT) & 0x0100) == 0) {
  359. NutSleep(10);
  360. }
  361. #ifdef NUT_CS8900_OLD
  362. // Get the RxStatus But don't let the compiler do any optomisation
  363. asm volatile ("lds __tmp_reg__, %3" "\n\t"
  364. "mov %B0, __tmp_reg__" "\n\t" "lds __tmp_reg__, %2" "\n\t" "mov %A0, __tmp_reg__" "\n\t":"=r" (l)
  365. :"0"(l), "n"((unsigned short) (CS_DATA_P0)), "n"((unsigned short) (CS_DATA_P0 + 1))
  366. );
  367. // Get the Packet Length But don't let the compiler do any optomisation
  368. asm volatile ("lds __tmp_reg__, %3" "\n\t"
  369. "mov %B0, __tmp_reg__" "\n\t" "lds __tmp_reg__, %2" "\n\t" "mov %A0, __tmp_reg__" "\n\t":"=r" (l)
  370. :"0"(l), "n"((unsigned short) (CS_DATA_P0)), "n"((unsigned short) (CS_DATA_P0 + 1))
  371. );
  372. #else
  373. l = *(uint8_t *) (CS_DATA_P0 + 1) << 8 | *(uint8_t *) (CS_DATA_P0);
  374. l = *(uint8_t *) (CS_DATA_P0 + 1) << 8 | *(uint8_t *) (CS_DATA_P0);
  375. #endif
  376. //NutPrintFormat_P(dev_debug,PSTR("RxLength = %x \r\n"), l);
  377. //NutPrintFlush(dev_debug);
  378. // Account for odd length packets
  379. if (l & 1)
  380. m = l - 1;
  381. else
  382. m = l;
  383. nb = NutNetBufAlloc(0, NBAF_DATALINK, l);
  384. if (nb) {
  385. q = nb->nb_dl.vp;
  386. for (i = 0; i < m; i += 2) {
  387. p = (uint8_t *) CS_DATA_P0;
  388. *q++ = *p;
  389. p = (uint8_t *) CS_DATA_P0 + 1;
  390. *q++ = *p;
  391. }
  392. // Odd length packets
  393. if (m != l) {
  394. p = (uint8_t *) CS_DATA_P0;
  395. *q++ = *p;
  396. p = (uint8_t *) CS_DATA_P0 + 1;
  397. m = *p;
  398. }
  399. ni->ni_rx_packets++;
  400. (*ifn->if_recv) (dev, nb);
  401. }
  402. }
  403. }
  404. void CSSoftwareWakeup(void)
  405. {
  406. volatile uint16_t *p;
  407. p = (uint16_t *) CS_PP_PTR;
  408. *p = CS_SELF_CTRL;
  409. NutDelay(10);
  410. }
  411. void CSSoftwareReset(void)
  412. {
  413. volatile uint16_t *p;
  414. p = (uint16_t *) CS_PP_PTR;
  415. *p = CS_SELF_CTRL;
  416. p = (uint16_t *) CS_DATA_P0;
  417. *p = 0x0040;
  418. }
  419. /*!
  420. * \brief Initialize Ethernet Interface.
  421. *
  422. *
  423. * Applications typically do not use this function, but
  424. * call NutNetIfConfig().
  425. *
  426. * \param dev Identifies the device to initialize. The
  427. * structure must be properly set.
  428. */
  429. int CSNicInit(NUTDEVICE * dev)
  430. {
  431. uint16_t i;
  432. uint16_t j;
  433. IFNET *ifn;
  434. NICINFO *ni;
  435. #if 0
  436. if (tcp_trace) {
  437. NutPrintFormat_P(dev_debug, PSTR("Enter NicInit \r\n"));
  438. NutPrintFlush(dev_debug);
  439. }
  440. #endif
  441. cs_base = dev->dev_base;
  442. if (confnet.cd_size == 0)
  443. NutNetLoadConfig(dev->dev_name);
  444. ifn = dev->dev_icb;
  445. memcpy(ifn->if_mac, confnet.cdn_mac, 6);
  446. memset(dev->dev_dcb, 0, sizeof(NICINFO));
  447. ni = (NICINFO *) dev->dev_dcb;
  448. // Take CS8900 out of reset and wait for internal reset to complete
  449. #ifdef NUT_CS8900_OLD
  450. outb(PORTD, inb(PORTD) & ~RESETE);
  451. #else
  452. CSSoftwareWakeup();
  453. CSSoftwareReset();
  454. #endif
  455. NutDelay(100);
  456. // Check for presence
  457. if (CSReadPP16(CS_PROD_ID) != 0x630E)
  458. return -1;
  459. //
  460. // Copy our MAC address to the NIC
  461. //
  462. for (i = 0; i < 6; i += 2) {
  463. j = ifn->if_mac[i] << 8;
  464. j |= ifn->if_mac[i + 1];
  465. CSWritePP16(CS_IEEE_ADDR + i, j);
  466. j = CSReadPP16(CS_IEEE_ADDR + i);
  467. #if 0
  468. if (tcp_trace) {
  469. NutPrintFormat_P(dev_debug, PSTR("ADDR = %x\r\n"), j);
  470. NutPrintFlush(dev_debug);
  471. }
  472. #endif
  473. }
  474. //
  475. // Enable the Transmitter and Receiver
  476. //
  477. CSWritePP16(CS_LINE_CTRL, 0x00C0);
  478. //i = CSReadPP16(CS_LINE_CTRL);
  479. //NutPrintFormat_P(dev_debug,PSTR("CS_LINE_CTRL = %x\r\n"), i);
  480. CSWritePP16(CS_RX_CTL, 0x0F40);
  481. //i = CSReadPP16(CS_RX_CTL);
  482. //NutPrintFormat_P(dev_debug,PSTR("CS_RX_CTL = %x\r\n"), i);
  483. //
  484. // Start receiver thread
  485. //
  486. NutThreadCreate("csnicrx", CSNICrx, dev, 500);
  487. return 0;
  488. }
  489. /*@}*/
  490. #else
  491. void keep_icc_happy(void)
  492. {
  493. }
  494. #endif