if_le_lebuffer.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* $OpenBSD: if_le_lebuffer.c,v 1.12 2014/12/22 02:28:52 tedu Exp $ */
  2. /* $NetBSD: if_le_lebuffer.c,v 1.10 2002/03/11 16:00:56 pk 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>
  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_space_handle_t sc_reg; /* LANCE registers */
  63. };
  64. int lematch_lebuffer(struct device *, void *, void *);
  65. void leattach_lebuffer(struct device *, struct device *, void *);
  66. /*
  67. * Media types supported.
  68. */
  69. static int lemedia[] = {
  70. IFM_ETHER | IFM_10_T
  71. };
  72. struct cfattach le_lebuffer_ca = {
  73. sizeof(struct le_softc), lematch_lebuffer, leattach_lebuffer
  74. };
  75. extern struct cfdriver le_cd;
  76. struct cfdriver lebuffer_cd = {
  77. NULL, "lebuffer", DV_DULL
  78. };
  79. void le_lebuffer_wrcsr(struct lance_softc *, uint16_t, uint16_t);
  80. uint16_t le_lebuffer_rdcsr(struct lance_softc *, uint16_t);
  81. void
  82. le_lebuffer_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
  83. {
  84. struct le_softc *lesc = (struct le_softc *)sc;
  85. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
  86. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2,
  87. BUS_SPACE_BARRIER_WRITE);
  88. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
  89. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2,
  90. BUS_SPACE_BARRIER_WRITE);
  91. #if defined(SUN4M)
  92. /*
  93. * We need to flush the SBus->MBus write buffers. This can most
  94. * easily be accomplished by reading back the register that we
  95. * just wrote (thanks to Chris Torek for this solution).
  96. */
  97. if (CPU_ISSUN4M) {
  98. volatile uint16_t discard;
  99. discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
  100. LEREG1_RDP);
  101. }
  102. #endif
  103. }
  104. uint16_t
  105. le_lebuffer_rdcsr(struct lance_softc *sc, uint16_t port)
  106. {
  107. struct le_softc *lesc = (struct le_softc *)sc;
  108. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
  109. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2,
  110. BUS_SPACE_BARRIER_WRITE);
  111. return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
  112. }
  113. int
  114. lematch_lebuffer(struct device *parent, void *vcf, void *aux)
  115. {
  116. struct sbus_attach_args *sa = aux;
  117. struct cfdata *cf = vcf;
  118. return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
  119. }
  120. void
  121. leattach_lebuffer(struct device *parent, struct device *self, void *aux)
  122. {
  123. struct sbus_attach_args *sa = aux;
  124. struct le_softc *lesc = (struct le_softc *)self;
  125. struct lance_softc *sc = &lesc->sc_am7990.lsc;
  126. struct lebuf_softc *lebuf = (struct lebuf_softc *)parent;
  127. /* XXX the following declarations should be elsewhere */
  128. extern void myetheraddr(u_char *);
  129. lesc->sc_bustag = sa->sa_bustag;
  130. lesc->sc_dmatag = sa->sa_dmatag;
  131. if (sbus_bus_map(sa->sa_bustag,
  132. sa->sa_slot, sa->sa_offset, sa->sa_size,
  133. 0, 0, &lesc->sc_reg)) {
  134. printf(": cannot map registers\n");
  135. return;
  136. }
  137. sc->sc_mem = lebuf->sc_buffer;
  138. sc->sc_memsize = lebuf->sc_bufsiz;
  139. sc->sc_addr = 0; /* Lance view is offset by buffer location */
  140. lebuf->attached = 1;
  141. /* That old black magic... */
  142. sc->sc_conf3 = getpropint(sa->sa_node, "busmaster-regval",
  143. LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
  144. myetheraddr(sc->sc_arpcom.ac_enaddr);
  145. sc->sc_supmedia = lemedia;
  146. sc->sc_nsupmedia = nitems(lemedia);
  147. sc->sc_defaultmedia = sc->sc_supmedia[sc->sc_nsupmedia - 1];
  148. sc->sc_copytodesc = lance_copytobuf_contig;
  149. sc->sc_copyfromdesc = lance_copyfrombuf_contig;
  150. sc->sc_copytobuf = lance_copytobuf_contig;
  151. sc->sc_copyfrombuf = lance_copyfrombuf_contig;
  152. sc->sc_zerobuf = lance_zerobuf_contig;
  153. sc->sc_rdcsr = le_lebuffer_rdcsr;
  154. sc->sc_wrcsr = le_lebuffer_wrcsr;
  155. am7990_config(&lesc->sc_am7990);
  156. /* Establish interrupt handler */
  157. if (sa->sa_nintr != 0)
  158. (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
  159. IPL_NET, 0, am7990_intr, sc, self->dv_xname);
  160. }