PageRenderTime 27ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/netbsd/sys/dev/isa/cs89x0isa.c

https://github.com/kame/kame
C | 378 lines | 192 code | 42 blank | 144 comment | 36 complexity | 76000dcc5bf660fc56c9ca1434c3fc6f MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. /* $NetBSD: cs89x0isa.c,v 1.3 2001/12/31 22:07:58 thorpej Exp $ */
  2. /*
  3. * Copyright 1997
  4. * Digital Equipment Corporation. All rights reserved.
  5. *
  6. * This software is furnished under license and may be used and
  7. * copied only in accordance with the following terms and conditions.
  8. * Subject to these conditions, you may download, copy, install,
  9. * use, modify and distribute this software in source and/or binary
  10. * form. No title or ownership is transferred hereby.
  11. *
  12. * 1) Any source code used, modified or distributed must reproduce
  13. * and retain this copyright notice and list of conditions as
  14. * they appear in the source file.
  15. *
  16. * 2) No right is granted to use any trade name, trademark, or logo of
  17. * Digital Equipment Corporation. Neither the "Digital Equipment
  18. * Corporation" name nor any trademark or logo of Digital Equipment
  19. * Corporation may be used to endorse or promote products derived
  20. * from this software without the prior written permission of
  21. * Digital Equipment Corporation.
  22. *
  23. * 3) This software is provided "AS-IS" and any express or implied
  24. * warranties, including but not limited to, any implied warranties
  25. * of merchantability, fitness for a particular purpose, or
  26. * non-infringement are disclaimed. In no event shall DIGITAL be
  27. * liable for any damages whatsoever, and in particular, DIGITAL
  28. * shall not be liable for special, indirect, consequential, or
  29. * incidental damages or damages for lost profits, loss of
  30. * revenue or loss of use, whether such damages arise in contract,
  31. * negligence, tort, under statute, in equity, at law or otherwise,
  32. * even if advised of the possibility of such damage.
  33. */
  34. /* isa dma routines for cs89x0 */
  35. #include <sys/cdefs.h>
  36. __KERNEL_RCSID(0, "$NetBSD: cs89x0isa.c,v 1.3 2001/12/31 22:07:58 thorpej Exp $");
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/mbuf.h>
  40. #include <sys/socket.h>
  41. #include <sys/device.h>
  42. #include "rnd.h"
  43. #if NRND > 0
  44. #include <sys/rnd.h>
  45. #endif
  46. #include <net/if.h>
  47. #include <net/if_ether.h>
  48. #include <net/if_media.h>
  49. #include <machine/bus.h>
  50. #include <dev/isa/isareg.h>
  51. #include <dev/isa/isavar.h>
  52. #include <dev/isa/isadmavar.h>
  53. #include <dev/ic/cs89x0reg.h>
  54. #include <dev/ic/cs89x0var.h>
  55. #include <dev/isa/cs89x0isavar.h>
  56. #define DMA_STATUS_BITS 0x0007 /* bit masks for checking DMA status */
  57. #define DMA_STATUS_OK 0x0004
  58. void
  59. cs_isa_dma_attach(struct cs_softc *sc)
  60. {
  61. struct cs_softc_isa *isc = (void *)sc;
  62. if (isc->sc_drq == ISACF_DRQ_DEFAULT)
  63. printf("%s: DMA channel unspecified, not using DMA\n",
  64. sc->sc_dev.dv_xname);
  65. else if (isc->sc_drq < 5 || isc->sc_drq > 7)
  66. printf("%s: invalid DMA channel, not using DMA\n",
  67. sc->sc_dev.dv_xname);
  68. else {
  69. bus_size_t maxsize;
  70. bus_addr_t dma_addr;
  71. maxsize = isa_dmamaxsize(isc->sc_ic, isc->sc_drq);
  72. if (maxsize < CS8900_DMASIZE) {
  73. printf("%s: max DMA size %lu is less than required %d\n",
  74. sc->sc_dev.dv_xname, (u_long)maxsize,
  75. CS8900_DMASIZE);
  76. goto after_dma_block;
  77. }
  78. if (isa_dmamap_create(isc->sc_ic, isc->sc_drq,
  79. CS8900_DMASIZE, BUS_DMA_NOWAIT) != 0) {
  80. printf("%s: unable to create ISA DMA map\n",
  81. sc->sc_dev.dv_xname);
  82. goto after_dma_block;
  83. }
  84. if (isa_dmamem_alloc(isc->sc_ic, isc->sc_drq,
  85. CS8900_DMASIZE, &dma_addr, BUS_DMA_NOWAIT) != 0) {
  86. printf("%s: unable to allocate DMA buffer\n",
  87. sc->sc_dev.dv_xname);
  88. goto after_dma_block;
  89. }
  90. if (isa_dmamem_map(isc->sc_ic, isc->sc_drq, dma_addr,
  91. CS8900_DMASIZE, &isc->sc_dmabase,
  92. BUS_DMA_NOWAIT | BUS_DMA_COHERENT /* XXX */ ) != 0) {
  93. printf("%s: unable to map DMA buffer\n",
  94. sc->sc_dev.dv_xname);
  95. isa_dmamem_free(isc->sc_ic, isc->sc_drq, dma_addr,
  96. CS8900_DMASIZE);
  97. goto after_dma_block;
  98. }
  99. isc->sc_dmasize = CS8900_DMASIZE;
  100. sc->sc_cfgflags |= CFGFLG_DMA_MODE;
  101. isc->sc_dmaaddr = dma_addr;
  102. after_dma_block:
  103. ;
  104. }
  105. }
  106. void cs_isa_dma_chipinit(struct cs_softc *sc)
  107. {
  108. struct cs_softc_isa *isc = (void *)sc;
  109. if (sc->sc_cfgflags & CFGFLG_DMA_MODE) {
  110. /*
  111. * First we program the DMA controller and ensure the memory
  112. * buffer is valid. If it isn't then we just go on without
  113. * DMA.
  114. */
  115. if (isa_dmastart(isc->sc_ic, isc->sc_drq, isc->sc_dmabase,
  116. isc->sc_dmasize, NULL, DMAMODE_READ | DMAMODE_LOOPDEMAND,
  117. BUS_DMA_NOWAIT)) {
  118. /* XXX XXX XXX */
  119. panic("%s: unable to start DMA\n", sc->sc_dev.dv_xname);
  120. }
  121. isc->sc_dmacur = isc->sc_dmabase;
  122. /* interrupt when a DMA'd frame is received */
  123. CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
  124. RX_CFG_ALL_IE | RX_CFG_RX_DMA_ONLY);
  125. /*
  126. * set the DMA burst bit so we don't tie up the bus for too
  127. * long.
  128. */
  129. if (isc->sc_dmasize == 16384) {
  130. CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
  131. ((CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) &
  132. ~BUS_CTL_DMA_SIZE) | BUS_CTL_DMA_BURST));
  133. } else { /* use 64K */
  134. CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
  135. CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) |
  136. BUS_CTL_DMA_SIZE | BUS_CTL_DMA_BURST);
  137. }
  138. CS_WRITE_PACKET_PAGE(sc, PKTPG_DMA_CHANNEL, isc->sc_drq - 5);
  139. }
  140. }
  141. void cs_process_rx_dma(struct cs_softc *sc)
  142. {
  143. struct cs_softc_isa *isc = (void *)sc;
  144. struct ifnet *ifp;
  145. u_int16_t num_dma_frames;
  146. u_int16_t pkt_length;
  147. u_int16_t status;
  148. u_int to_copy;
  149. char *dma_mem_ptr;
  150. struct mbuf *m;
  151. u_char *pBuff;
  152. int pad;
  153. /* initialise the pointers */
  154. ifp = &sc->sc_ethercom.ec_if;
  155. /* Read the number of frames DMAed. */
  156. num_dma_frames = CS_READ_PACKET_PAGE(sc, PKTPG_DMA_FRAME_COUNT);
  157. num_dma_frames &= (u_int16_t) (0x0fff);
  158. /*
  159. * Loop till number of DMA frames ready to read is zero. After
  160. * reading the frame out of memory we must check if any have been
  161. * received while we were processing
  162. */
  163. while (num_dma_frames != 0) {
  164. dma_mem_ptr = isc->sc_dmacur;
  165. /*
  166. * process all of the dma frames in memory
  167. *
  168. * This loop relies on the dma_mem_ptr variable being set to the
  169. * next frames start address.
  170. */
  171. for (; num_dma_frames > 0; num_dma_frames--) {
  172. /*
  173. * Get the length and status of the packet. Only the
  174. * status is guarenteed to be at dma_mem_ptr, ie need
  175. * to check for wraparound before reading the length
  176. */
  177. status = *((unsigned short *) dma_mem_ptr)++;
  178. if (dma_mem_ptr > (isc->sc_dmabase + isc->sc_dmasize)) {
  179. dma_mem_ptr = isc->sc_dmabase;
  180. }
  181. pkt_length = *((unsigned short *) dma_mem_ptr)++;
  182. /* Do some sanity checks on the length and status. */
  183. if ((pkt_length > ETHER_MAX_LEN) ||
  184. ((status & DMA_STATUS_BITS) != DMA_STATUS_OK)) {
  185. /*
  186. * the SCO driver kills the adapter in this
  187. * situation
  188. */
  189. /*
  190. * should increment the error count and reset
  191. * the dma operation.
  192. */
  193. printf("%s: cs_process_rx_dma: DMA buffer out of sync about to reset\n",
  194. sc->sc_dev.dv_xname);
  195. ifp->if_ierrors++;
  196. /* skip the rest of the DMA buffer */
  197. isa_dmaabort(isc->sc_ic, isc->sc_drq);
  198. /* now reset the chip and reinitialise */
  199. cs_init(&sc->sc_ethercom.ec_if);
  200. return;
  201. }
  202. /* Check the status of the received packet. */
  203. if (status & RX_EVENT_RX_OK) {
  204. /* get a new mbuf */
  205. MGETHDR(m, M_DONTWAIT, MT_DATA);
  206. if (m == 0) {
  207. printf("%s: cs_process_rx_dma: unable to allocate mbuf\n",
  208. sc->sc_dev.dv_xname);
  209. ifp->if_ierrors++;
  210. /*
  211. * couldn't allocate an mbuf so
  212. * things are not good, may as well
  213. * drop all the packets I think.
  214. */
  215. CS_READ_PACKET_PAGE(sc,
  216. PKTPG_DMA_FRAME_COUNT);
  217. /* now reset DMA operation */
  218. isa_dmaabort(isc->sc_ic, isc->sc_drq);
  219. /*
  220. * now reset the chip and
  221. * reinitialise
  222. */
  223. cs_init(&sc->sc_ethercom.ec_if);
  224. return;
  225. }
  226. /*
  227. * save processing by always using a mbuf
  228. * cluster, guarenteed to fit packet
  229. */
  230. MCLGET(m, M_DONTWAIT);
  231. if ((m->m_flags & M_EXT) == 0) {
  232. /* couldn't allocate an mbuf cluster */
  233. printf("%s: cs_process_rx_dma: unable to allocate a cluster\n",
  234. sc->sc_dev.dv_xname);
  235. m_freem(m);
  236. /* skip the frame */
  237. CS_READ_PACKET_PAGE(sc, PKTPG_DMA_FRAME_COUNT);
  238. isa_dmaabort(isc->sc_ic, isc->sc_drq);
  239. /*
  240. * now reset the chip and
  241. * reinitialise
  242. */
  243. cs_init(&sc->sc_ethercom.ec_if);
  244. return;
  245. }
  246. m->m_pkthdr.rcvif = ifp;
  247. m->m_pkthdr.len = pkt_length;
  248. m->m_len = pkt_length;
  249. /*
  250. * align ip header on word boundary for
  251. * ipintr
  252. */
  253. pad = ALIGN(sizeof(struct ether_header)) -
  254. sizeof(struct ether_header);
  255. m->m_data += pad;
  256. /*
  257. * set up the buffer pointer to point to the
  258. * data area
  259. */
  260. pBuff = mtod(m, char *);
  261. /*
  262. * Read the frame into free_pktbuf
  263. * The buffer is circular buffer, either
  264. * 16K or 64K in length.
  265. *
  266. * need to check where the end of the buffer
  267. * is and go back to the start.
  268. */
  269. if ((dma_mem_ptr + pkt_length) <
  270. (isc->sc_dmabase + isc->sc_dmasize)) {
  271. /*
  272. * No wrap around. Copy the frame
  273. * header
  274. */
  275. memcpy(pBuff, dma_mem_ptr, pkt_length);
  276. dma_mem_ptr += pkt_length;
  277. } else {
  278. to_copy = (u_int)
  279. ((isc->sc_dmabase + isc->sc_dmasize) -
  280. dma_mem_ptr);
  281. /* Copy the first half of the frame. */
  282. memcpy(pBuff, dma_mem_ptr, to_copy);
  283. pBuff += to_copy;
  284. /*
  285. * Rest of the frame is to be read
  286. * from the first byte of the DMA
  287. * memory.
  288. */
  289. /*
  290. * Get the number of bytes leftout in
  291. * the frame.
  292. */
  293. to_copy = pkt_length - to_copy;
  294. dma_mem_ptr = isc->sc_dmabase;
  295. /* Copy rest of the frame. */
  296. memcpy(pBuff, dma_mem_ptr, to_copy);
  297. dma_mem_ptr += to_copy;
  298. }
  299. cs_ether_input(sc, m);
  300. }
  301. /* (status & RX_OK) */
  302. else {
  303. /* the frame was not received OK */
  304. /* Increment the input error count */
  305. ifp->if_ierrors++;
  306. /*
  307. * If debugging is enabled then log error
  308. * messages if we got any.
  309. */
  310. if ((ifp->if_flags & IFF_DEBUG) &&
  311. status != REG_NUM_RX_EVENT)
  312. cs_print_rx_errors(sc, status);
  313. }
  314. /*
  315. * now update the current frame pointer. the
  316. * dma_mem_ptr should point to the next packet to be
  317. * received, without the alignment considerations.
  318. *
  319. * The cs8900 pads all frames to start at the next 32bit
  320. * aligned addres. hence we need to pad our offset
  321. * pointer.
  322. */
  323. dma_mem_ptr += 3;
  324. dma_mem_ptr = (char *)
  325. ((long) dma_mem_ptr & 0xfffffffc);
  326. if (dma_mem_ptr < (isc->sc_dmabase + isc->sc_dmasize)) {
  327. isc->sc_dmacur = dma_mem_ptr;
  328. } else {
  329. dma_mem_ptr = isc->sc_dmacur = isc->sc_dmabase;
  330. }
  331. } /* for all frames */
  332. /* Read the number of frames DMAed again. */
  333. num_dma_frames = CS_READ_PACKET_PAGE(sc, PKTPG_DMA_FRAME_COUNT);
  334. num_dma_frames &= (u_int16_t) (0x0fff);
  335. } /* while there are frames left */
  336. }