if_le_ledma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* $OpenBSD: if_le_ledma.c,v 1.18 2014/12/22 02:28:52 tedu Exp $ */
  2. /* $NetBSD: if_le_ledma.c,v 1.14 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/ic/lsi64854reg.h>
  49. #include <dev/ic/lsi64854var.h>
  50. #include <dev/ic/lancereg.h>
  51. #include <dev/ic/lancevar.h>
  52. #include <dev/ic/am7990reg.h>
  53. #include <dev/ic/am7990var.h>
  54. /*
  55. * LANCE registers.
  56. */
  57. #define LEREG1_RDP 0 /* Register Data port */
  58. #define LEREG1_RAP 2 /* Register Address port */
  59. struct le_softc {
  60. struct am7990_softc sc_am7990; /* glue to MI code */
  61. bus_space_tag_t sc_bustag;
  62. bus_dmamap_t sc_dmamap;
  63. bus_space_handle_t sc_reg; /* LANCE registers */
  64. struct lsi64854_softc *sc_dma; /* pointer to my dma */
  65. u_int sc_laddr; /* LANCE DMA address */
  66. };
  67. #define MEMSIZE (16*1024) /* LANCE memory size */
  68. #define LEDMA_BOUNDARY (16*1024*1024) /* must not cross 16MB boundary */
  69. int lematch_ledma(struct device *, void *, void *);
  70. void leattach_ledma(struct device *, struct device *, void *);
  71. /*
  72. * Media types supported by the Sun4m.
  73. */
  74. static int lemedia[] = {
  75. IFM_ETHER | IFM_10_T,
  76. IFM_ETHER | IFM_10_5,
  77. IFM_ETHER | IFM_AUTO
  78. };
  79. void le_ledma_setutp(struct lance_softc *);
  80. void le_ledma_setaui(struct lance_softc *);
  81. int lemediachange(struct lance_softc *);
  82. void lemediastatus(struct lance_softc *, struct ifmediareq *);
  83. struct cfattach le_ledma_ca = {
  84. sizeof(struct le_softc), lematch_ledma, leattach_ledma
  85. };
  86. void le_ledma_wrcsr(struct lance_softc *, uint16_t, uint16_t);
  87. uint16_t le_ledma_rdcsr(struct lance_softc *, uint16_t);
  88. void le_ledma_hwreset(struct lance_softc *);
  89. void le_ledma_hwinit(struct lance_softc *);
  90. void le_ledma_nocarrier(struct lance_softc *);
  91. void
  92. le_ledma_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
  93. {
  94. struct le_softc *lesc = (struct le_softc *)sc;
  95. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
  96. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2,
  97. BUS_SPACE_BARRIER_WRITE);
  98. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
  99. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2,
  100. BUS_SPACE_BARRIER_WRITE);
  101. #if defined(SUN4M)
  102. /*
  103. * We need to flush the SBus->MBus write buffers. This can most
  104. * easily be accomplished by reading back the register that we
  105. * just wrote (thanks to Chris Torek for this solution).
  106. */
  107. if (CPU_ISSUN4M) {
  108. volatile uint16_t discard;
  109. discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
  110. LEREG1_RDP);
  111. }
  112. #endif
  113. }
  114. uint16_t
  115. le_ledma_rdcsr(struct lance_softc *sc, uint16_t port)
  116. {
  117. struct le_softc *lesc = (struct le_softc *)sc;
  118. bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
  119. bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2,
  120. BUS_SPACE_BARRIER_WRITE);
  121. return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
  122. }
  123. void
  124. le_ledma_setutp(struct lance_softc *sc)
  125. {
  126. struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
  127. u_int32_t csr;
  128. csr = L64854_GCSR(dma);
  129. csr |= E_TP_AUI;
  130. L64854_SCSR(dma, csr);
  131. delay(20000); /* must not touch le for 20ms */
  132. }
  133. void
  134. le_ledma_setaui(struct lance_softc *sc)
  135. {
  136. struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
  137. u_int32_t csr;
  138. csr = L64854_GCSR(dma);
  139. csr &= ~E_TP_AUI;
  140. L64854_SCSR(dma, csr);
  141. delay(20000); /* must not touch le for 20ms */
  142. }
  143. int
  144. lemediachange(struct lance_softc *sc)
  145. {
  146. struct ifmedia *ifm = &sc->sc_ifmedia;
  147. if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
  148. return (EINVAL);
  149. /*
  150. * Switch to the selected media. If autoselect is
  151. * set, we don't really have to do anything. We'll
  152. * switch to the other media when we detect loss of
  153. * carrier.
  154. */
  155. switch (IFM_SUBTYPE(ifm->ifm_media)) {
  156. case IFM_10_T:
  157. le_ledma_setutp(sc);
  158. break;
  159. case IFM_10_5:
  160. le_ledma_setaui(sc);
  161. break;
  162. case IFM_AUTO:
  163. break;
  164. default:
  165. return (EINVAL);
  166. }
  167. return (0);
  168. }
  169. void
  170. lemediastatus(struct lance_softc *sc, struct ifmediareq *ifmr)
  171. {
  172. struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
  173. /*
  174. * Notify the world which media we're currently using.
  175. */
  176. if (L64854_GCSR(dma) & E_TP_AUI)
  177. ifmr->ifm_active = IFM_ETHER|IFM_10_T;
  178. else
  179. ifmr->ifm_active = IFM_ETHER|IFM_10_5;
  180. }
  181. void
  182. le_ledma_hwreset(struct lance_softc *sc)
  183. {
  184. struct le_softc *lesc = (struct le_softc *)sc;
  185. struct lsi64854_softc *dma = lesc->sc_dma;
  186. u_int32_t csr;
  187. u_int aui_bit;
  188. /*
  189. * Reset DMA channel.
  190. */
  191. csr = L64854_GCSR(dma);
  192. aui_bit = csr & E_TP_AUI;
  193. DMA_RESET(dma);
  194. /* Write bits 24-31 of Lance address */
  195. bus_space_write_4(dma->sc_bustag, dma->sc_regs, L64854_REG_ENBAR,
  196. lesc->sc_laddr & 0xff000000);
  197. DMA_ENINTR(dma);
  198. /*
  199. * Disable E-cache invalidates on chip writes.
  200. * Retain previous cable selection bit.
  201. */
  202. csr = L64854_GCSR(dma);
  203. csr |= (E_DSBL_WR_INVAL | aui_bit);
  204. L64854_SCSR(dma, csr);
  205. delay(20000); /* must not touch le for 20ms */
  206. }
  207. void
  208. le_ledma_hwinit(struct lance_softc *sc)
  209. {
  210. /*
  211. * Make sure we're using the currently-enabled media type.
  212. * XXX Actually, this is probably unnecessary, now.
  213. */
  214. switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_cur->ifm_media)) {
  215. case IFM_10_T:
  216. le_ledma_setutp(sc);
  217. break;
  218. case IFM_10_5:
  219. le_ledma_setaui(sc);
  220. break;
  221. }
  222. }
  223. void
  224. le_ledma_nocarrier(struct lance_softc *sc)
  225. {
  226. struct le_softc *lesc = (struct le_softc *)sc;
  227. /*
  228. * Check if the user has requested a certain cable type, and
  229. * if so, honor that request.
  230. */
  231. if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
  232. switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_media)) {
  233. case IFM_10_5:
  234. case IFM_AUTO:
  235. printf("%s: lost carrier on UTP port"
  236. ", switching to AUI port\n", sc->sc_dev.dv_xname);
  237. le_ledma_setaui(sc);
  238. }
  239. } else {
  240. switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_media)) {
  241. case IFM_10_T:
  242. case IFM_AUTO:
  243. printf("%s: lost carrier on AUI port"
  244. ", switching to UTP port\n", sc->sc_dev.dv_xname);
  245. le_ledma_setutp(sc);
  246. }
  247. }
  248. }
  249. int
  250. lematch_ledma(struct device *parent, void *vcf, void *aux)
  251. {
  252. struct cfdata *cf = vcf;
  253. struct sbus_attach_args *sa = aux;
  254. return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
  255. }
  256. void
  257. leattach_ledma(struct device *parent, struct device *self, void *aux)
  258. {
  259. struct sbus_attach_args *sa = aux;
  260. struct le_softc *lesc = (struct le_softc *)self;
  261. struct lsi64854_softc *lsi = (struct lsi64854_softc *)parent;
  262. struct lance_softc *sc = &lesc->sc_am7990.lsc;
  263. bus_dma_tag_t dmatag = sa->sa_dmatag;
  264. bus_dma_segment_t seg;
  265. int rseg, error;
  266. /* XXX the following declarations should be elsewhere */
  267. extern void myetheraddr(u_char *);
  268. lesc->sc_bustag = sa->sa_bustag;
  269. /* Establish link to `ledma' device */
  270. lesc->sc_dma = lsi;
  271. lesc->sc_dma->sc_client = lesc;
  272. /* Map device registers */
  273. if (sbus_bus_map(sa->sa_bustag,
  274. sa->sa_slot,
  275. sa->sa_offset,
  276. sa->sa_size,
  277. BUS_SPACE_MAP_LINEAR,
  278. 0, &lesc->sc_reg) != 0) {
  279. printf("%s @ ledma: cannot map registers\n", self->dv_xname);
  280. return;
  281. }
  282. /* Allocate buffer memory */
  283. sc->sc_memsize = MEMSIZE;
  284. /* Get a DMA handle */
  285. if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE,
  286. LEDMA_BOUNDARY, BUS_DMA_NOWAIT,
  287. &lesc->sc_dmamap)) != 0) {
  288. printf("%s: DMA map create error %d\n", self->dv_xname, error);
  289. return;
  290. }
  291. /* Allocate DMA buffer */
  292. if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, LEDMA_BOUNDARY,
  293. &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
  294. printf("%s @ ledma: DMA buffer alloc error %d\n",
  295. self->dv_xname, error);
  296. return;
  297. }
  298. /* Map DMA buffer into kernel space */
  299. if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
  300. (caddr_t *)&sc->sc_mem,
  301. BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
  302. printf("%s @ ledma: DMA buffer map error %d\n",
  303. self->dv_xname, error);
  304. bus_dmamem_free(dmatag, &seg, rseg);
  305. return;
  306. }
  307. /* Load DMA buffer */
  308. if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem,
  309. MEMSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
  310. printf("%s: DMA buffer map load error %d\n",
  311. self->dv_xname, error);
  312. bus_dmamem_free(dmatag, &seg, rseg);
  313. bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE);
  314. return;
  315. }
  316. lesc->sc_laddr = lesc->sc_dmamap->dm_segs[0].ds_addr;
  317. sc->sc_addr = lesc->sc_laddr & 0xffffff;
  318. sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
  319. myetheraddr(sc->sc_arpcom.ac_enaddr);
  320. sc->sc_mediachange = lemediachange;
  321. sc->sc_mediastatus = lemediastatus;
  322. sc->sc_supmedia = lemedia;
  323. sc->sc_nsupmedia = nitems(lemedia);
  324. sc->sc_defaultmedia = sc->sc_supmedia[sc->sc_nsupmedia - 1];
  325. sc->sc_copytodesc = lance_copytobuf_contig;
  326. sc->sc_copyfromdesc = lance_copyfrombuf_contig;
  327. sc->sc_copytobuf = lance_copytobuf_contig;
  328. sc->sc_copyfrombuf = lance_copyfrombuf_contig;
  329. sc->sc_zerobuf = lance_zerobuf_contig;
  330. sc->sc_rdcsr = le_ledma_rdcsr;
  331. sc->sc_wrcsr = le_ledma_wrcsr;
  332. sc->sc_hwinit = le_ledma_hwinit;
  333. sc->sc_nocarrier = le_ledma_nocarrier;
  334. sc->sc_hwreset = le_ledma_hwreset;
  335. /* Establish interrupt handler */
  336. if (sa->sa_nintr != 0)
  337. (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0,
  338. am7990_intr, sc, self->dv_xname);
  339. am7990_config(&lesc->sc_am7990);
  340. /* now initialize DMA */
  341. le_ledma_hwreset(sc);
  342. }