if_le.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* $OpenBSD: if_le.c,v 1.16 2014/12/22 02:28:52 tedu Exp $ */
  2. /* $NetBSD: if_le.c,v 1.17 2001/05/30 11:46:35 mrg Exp $ */
  3. /*-
  4. * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
  9. * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "bpfilter.h"
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/mbuf.h>
  36. #include <sys/syslog.h>
  37. #include <sys/socket.h>
  38. #include <sys/device.h>
  39. #include <sys/malloc.h>
  40. #include <net/if.h>
  41. #include <net/if_media.h>
  42. #include <netinet/in.h>
  43. #include <netinet/if_ether.h>
  44. #include <machine/bus.h>
  45. #include <machine/intr.h>
  46. #include <machine/autoconf.h>
  47. #include <dev/sbus/sbusvar.h>
  48. #include <dev/sbus/lebuffervar.h> /*XXX*/
  49. #include <dev/ic/lancereg.h>
  50. #include <dev/ic/lancevar.h>
  51. #include <dev/ic/am7990reg.h>
  52. #include <dev/ic/am7990var.h>
  53. /*
  54. * LANCE registers.
  55. */
  56. #define LEREG1_RDP 0 /* Register Data port */
  57. #define LEREG1_RAP 2 /* Register Address port */
  58. struct le_softc {
  59. struct am7990_softc sc_am7990; /* glue to MI code */
  60. bus_space_tag_t sc_bustag;
  61. bus_dma_tag_t sc_dmatag;
  62. bus_dmamap_t sc_dmamap;
  63. bus_space_handle_t sc_reg;
  64. };
  65. #define MEMSIZE 0x4000 /* LANCE memory size */
  66. int lematch_sbus(struct device *, void *, void *);
  67. void leattach_sbus(struct device *, struct device *, void *);
  68. /*
  69. * Media types supported.
  70. */
  71. static int lemedia[] = {
  72. IFM_ETHER | IFM_10_5
  73. };
  74. struct cfattach le_sbus_ca = {
  75. sizeof(struct le_softc), lematch_sbus, leattach_sbus
  76. };
  77. void le_sbus_wrcsr(struct lance_softc *, uint16_t, uint16_t);
  78. uint16_t le_sbus_rdcsr(struct lance_softc *, uint16_t);
  79. void
  80. le_sbus_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
  81. {
  82. struct le_softc *lesc = (struct le_softc *)sc;
  83. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
  84. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2,
  85. BUS_SPACE_BARRIER_WRITE);
  86. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
  87. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2,
  88. BUS_SPACE_BARRIER_WRITE);
  89. #if defined(SUN4M)
  90. /*
  91. * We need to flush the SBus->MBus write buffers. This can most
  92. * easily be accomplished by reading back the register that we
  93. * just wrote (thanks to Chris Torek for this solution).
  94. */
  95. if (CPU_ISSUN4M) {
  96. volatile uint16_t discard;
  97. discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
  98. LEREG1_RDP);
  99. }
  100. #endif
  101. }
  102. uint16_t
  103. le_sbus_rdcsr(struct lance_softc *sc, uint16_t port)
  104. {
  105. struct le_softc *lesc = (struct le_softc *)sc;
  106. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
  107. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2,
  108. BUS_SPACE_BARRIER_WRITE);
  109. return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
  110. }
  111. int
  112. lematch_sbus(struct device *parent, void *vcf, void *aux)
  113. {
  114. struct cfdata *cf = vcf;
  115. struct sbus_attach_args *sa = aux;
  116. return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
  117. }
  118. void
  119. leattach_sbus(struct device *parent, struct device *self, void *aux)
  120. {
  121. struct sbus_attach_args *sa = aux;
  122. struct le_softc *lesc = (struct le_softc *)self;
  123. struct lance_softc *sc = &lesc->sc_am7990.lsc;
  124. bus_dma_tag_t dmatag;
  125. /* XXX the following declarations should be elsewhere */
  126. extern void myetheraddr(u_char *);
  127. extern struct cfdriver lebuffer_cd;
  128. lesc->sc_bustag = sa->sa_bustag;
  129. lesc->sc_dmatag = dmatag = sa->sa_dmatag;
  130. if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
  131. sa->sa_reg[0].sbr_offset, sa->sa_reg[0].sbr_size,
  132. BUS_SPACE_MAP_LINEAR, 0, &lesc->sc_reg) != 0) {
  133. printf(": cannot map registers\n");
  134. return;
  135. }
  136. /*
  137. * Look for an "unallocated" lebuffer and pair it with
  138. * this `le' device on the assumption that we're on
  139. * a pre-historic ROM that doesn't establish le<=>lebuffer
  140. * parent-child relationships.
  141. */
  142. if (lebuffer_cd.cd_ndevs != 0) {
  143. struct lebuf_softc *lebuf;
  144. int i;
  145. for (i = 0; i < lebuffer_cd.cd_ndevs; i++) {
  146. lebuf = (struct lebuf_softc *)lebuffer_cd.cd_devs[i];
  147. if (lebuf == NULL || lebuf->attached != 0)
  148. continue;
  149. sc->sc_mem = lebuf->sc_buffer;
  150. sc->sc_memsize = lebuf->sc_bufsiz;
  151. /* Lance view is offset by buffer location */
  152. sc->sc_addr = 0;
  153. lebuf->attached = 1;
  154. /* That old black magic... */
  155. sc->sc_conf3 = getpropint(sa->sa_node,
  156. "busmaster-regval",
  157. LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
  158. break;
  159. }
  160. }
  161. if (sc->sc_mem == 0) {
  162. bus_dma_segment_t seg;
  163. int rseg, error;
  164. /* Get a DMA handle */
  165. if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
  166. BUS_DMA_NOWAIT|BUS_DMA_24BIT, &lesc->sc_dmamap)) != 0) {
  167. printf(": DMA map create error %d\n", error);
  168. return;
  169. }
  170. /* Allocate DMA buffer */
  171. if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, 0,
  172. &seg, 1, &rseg, BUS_DMA_NOWAIT|BUS_DMA_24BIT)) != 0){
  173. printf(": DMA buffer allocation error %d\n", error);
  174. return;
  175. }
  176. /* Map DMA buffer into kernel space */
  177. if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
  178. (caddr_t *)&sc->sc_mem,
  179. BUS_DMA_NOWAIT|BUS_DMA_COHERENT|BUS_DMA_24BIT)) != 0) {
  180. printf(": DMA buffer map error %d\n", error);
  181. bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
  182. return;
  183. }
  184. /* Load DMA buffer */
  185. if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem,
  186. MEMSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT|BUS_DMA_24BIT)) != 0) {
  187. printf(": DMA buffer map load error %d\n", error);
  188. bus_dmamem_free(dmatag, &seg, rseg);
  189. bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE);
  190. return;
  191. }
  192. sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
  193. sc->sc_memsize = MEMSIZE;
  194. sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
  195. }
  196. myetheraddr(sc->sc_arpcom.ac_enaddr);
  197. sc->sc_supmedia = lemedia;
  198. sc->sc_nsupmedia = nitems(lemedia);
  199. sc->sc_defaultmedia = sc->sc_supmedia[sc->sc_nsupmedia - 1];
  200. sc->sc_copytodesc = lance_copytobuf_contig;
  201. sc->sc_copyfromdesc = lance_copyfrombuf_contig;
  202. sc->sc_copytobuf = lance_copytobuf_contig;
  203. sc->sc_copyfrombuf = lance_copyfrombuf_contig;
  204. sc->sc_zerobuf = lance_zerobuf_contig;
  205. sc->sc_rdcsr = le_sbus_rdcsr;
  206. sc->sc_wrcsr = le_sbus_wrcsr;
  207. am7990_config(&lesc->sc_am7990);
  208. /* Establish interrupt handler */
  209. if (sa->sa_nintr != 0)
  210. (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
  211. IPL_NET, 0, am7990_intr, sc, self->dv_xname);
  212. }