if_ep_pcmcia.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /* $OpenBSD: if_ep_pcmcia.c,v 1.45 2015/03/14 03:38:49 jsg Exp $ */
  2. /* $NetBSD: if_ep_pcmcia.c,v 1.16 1998/08/17 23:20:40 thorpej Exp $ */
  3. /*-
  4. * Copyright (c) 1998 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  9. * NASA Ames Research Center.
  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. /*
  33. * Copyright (c) 1997 Marc Horowitz. All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. * 3. All advertising materials mentioning features or use of this software
  44. * must display the following acknowledgement:
  45. * This product includes software developed by Marc Horowitz.
  46. * 4. The name of the author may not be used to endorse or promote products
  47. * derived from this software without specific prior written permission.
  48. *
  49. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  50. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  51. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  52. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  53. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  54. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  55. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  56. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  57. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  58. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. */
  60. #include "bpfilter.h"
  61. #include <sys/param.h>
  62. #include <sys/systm.h>
  63. #include <sys/mbuf.h>
  64. #include <sys/socket.h>
  65. #include <sys/ioctl.h>
  66. #include <sys/errno.h>
  67. #include <sys/syslog.h>
  68. #include <sys/selinfo.h>
  69. #include <sys/timeout.h>
  70. #include <sys/device.h>
  71. #include <net/if.h>
  72. #include <net/if_dl.h>
  73. #include <net/if_types.h>
  74. #include <net/netisr.h>
  75. #include <net/if_media.h>
  76. #include <netinet/in.h>
  77. #include <netinet/if_ether.h>
  78. #if NBPFILTER > 0
  79. #include <net/bpf.h>
  80. #endif
  81. #include <machine/cpu.h>
  82. #include <machine/bus.h>
  83. #include <dev/mii/mii.h>
  84. #include <dev/mii/miivar.h>
  85. #include <dev/ic/elink3var.h>
  86. #include <dev/pcmcia/pcmciareg.h>
  87. #include <dev/pcmcia/pcmciavar.h>
  88. #include <dev/pcmcia/pcmciadevs.h>
  89. int ep_pcmcia_match(struct device *, void *, void *);
  90. void ep_pcmcia_attach(struct device *, struct device *, void *);
  91. int ep_pcmcia_detach(struct device *, int);
  92. int ep_pcmcia_activate(struct device *, int);
  93. int ep_pcmcia_get_enaddr(struct pcmcia_tuple *, void *);
  94. #ifdef notyet
  95. int ep_pcmcia_enable(struct ep_softc *);
  96. void ep_pcmcia_disable(struct ep_softc *);
  97. void ep_pcmcia_disable1(struct ep_softc *);
  98. #endif
  99. int ep_pcmcia_enable1(struct ep_softc *);
  100. struct ep_pcmcia_softc {
  101. struct ep_softc sc_ep; /* real "ep" softc */
  102. /* PCMCIA-specific goo */
  103. struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
  104. int sc_io_window; /* our i/o window */
  105. struct pcmcia_function *sc_pf; /* our PCMCIA function */
  106. };
  107. struct cfattach ep_pcmcia_ca = {
  108. sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach,
  109. ep_pcmcia_detach, ep_pcmcia_activate
  110. };
  111. struct ep_pcmcia_product {
  112. u_int16_t epp_product; /* PCMCIA product ID */
  113. u_short epp_chipset; /* 3Com chipset used */
  114. int epp_flags; /* initial softc flags */
  115. int epp_expfunc; /* expected function */
  116. } ep_pcmcia_prod[] = {
  117. { PCMCIA_PRODUCT_3COM_3C562, EP_CHIPSET_3C509,
  118. 0, 0 },
  119. { PCMCIA_PRODUCT_3COM_3C589, EP_CHIPSET_3C509,
  120. 0, 0 },
  121. { PCMCIA_PRODUCT_3COM_3CXEM556, EP_CHIPSET_3C509,
  122. 0, 0 },
  123. { PCMCIA_PRODUCT_3COM_3CXEM556B,EP_CHIPSET_3C509,
  124. 0, 0 },
  125. { PCMCIA_PRODUCT_3COM_3C1, EP_CHIPSET_3C509,
  126. 0, 0 },
  127. { PCMCIA_PRODUCT_3COM_3CCFEM556BI, EP_CHIPSET_ROADRUNNER,
  128. EP_FLAGS_MII, 0 },
  129. { PCMCIA_PRODUCT_3COM_3C574, EP_CHIPSET_ROADRUNNER,
  130. EP_FLAGS_MII, 0 }
  131. };
  132. struct ep_pcmcia_product *ep_pcmcia_lookup(struct pcmcia_attach_args *);
  133. struct ep_pcmcia_product *
  134. ep_pcmcia_lookup(pa)
  135. struct pcmcia_attach_args *pa;
  136. {
  137. int i;
  138. for (i = 0; i < nitems(ep_pcmcia_prod); i++)
  139. if (pa->product == ep_pcmcia_prod[i].epp_product &&
  140. pa->pf->number == ep_pcmcia_prod[i].epp_expfunc)
  141. return &ep_pcmcia_prod[i];
  142. return (NULL);
  143. }
  144. int
  145. ep_pcmcia_match(parent, match, aux)
  146. struct device *parent;
  147. void *match, *aux;
  148. {
  149. struct pcmcia_attach_args *pa = aux;
  150. if (pa->manufacturer != PCMCIA_VENDOR_3COM)
  151. return (0);
  152. if (ep_pcmcia_lookup(pa) != NULL)
  153. return (1);
  154. return (0);
  155. }
  156. #ifdef notdef
  157. int
  158. ep_pcmcia_enable(sc)
  159. struct ep_softc *sc;
  160. {
  161. struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
  162. struct pcmcia_function *pf = psc->sc_pf;
  163. /* establish the interrupt. */
  164. sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr,
  165. sc, sc->sc_dev.dv_xname);
  166. if (sc->sc_ih == NULL) {
  167. printf("%s: couldn't establish interrupt\n",
  168. sc->sc_dev.dv_xname);
  169. return (1);
  170. }
  171. return (ep_pcmcia_enable1(sc));
  172. }
  173. #endif
  174. int
  175. ep_pcmcia_enable1(sc)
  176. struct ep_softc *sc;
  177. {
  178. struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
  179. struct pcmcia_function *pf = psc->sc_pf;
  180. int ret;
  181. if ((ret = pcmcia_function_enable(pf)))
  182. return (ret);
  183. if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
  184. (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
  185. (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556B)) {
  186. int reg;
  187. /* turn off the serial-disable bit */
  188. reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
  189. if (reg & 0x08) {
  190. reg &= ~0x08;
  191. pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
  192. }
  193. }
  194. return (ret);
  195. }
  196. #ifdef notyet
  197. void
  198. ep_pcmcia_disable(sc)
  199. struct ep_softc *sc;
  200. {
  201. struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
  202. pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
  203. ep_pcmcia_disable1(sc);
  204. }
  205. void
  206. ep_pcmcia_disable1(sc)
  207. struct ep_softc *sc;
  208. {
  209. struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
  210. pcmcia_function_disable(psc->sc_pf);
  211. }
  212. #endif
  213. void
  214. ep_pcmcia_attach(parent, self, aux)
  215. struct device *parent, *self;
  216. void *aux;
  217. {
  218. struct ep_pcmcia_softc *psc = (void *) self;
  219. struct ep_softc *sc = &psc->sc_ep;
  220. struct pcmcia_attach_args *pa = aux;
  221. struct pcmcia_config_entry *cfe;
  222. struct ep_pcmcia_product *epp;
  223. u_int8_t myla[ETHER_ADDR_LEN];
  224. u_int8_t *enaddr = NULL;
  225. const char *intrstr;
  226. int i;
  227. psc->sc_pf = pa->pf;
  228. cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
  229. /* Enable the card. */
  230. pcmcia_function_init(pa->pf, cfe);
  231. if (ep_pcmcia_enable1(sc))
  232. printf(": function enable failed\n");
  233. #ifdef notyet
  234. sc->enabled = 1;
  235. #endif
  236. if (cfe->num_memspace != 0)
  237. printf(": unexpected number of memory spaces %d should be 0\n",
  238. cfe->num_memspace);
  239. if (cfe->num_iospace != 1)
  240. printf(": unexpected number of I/O spaces %d should be 1\n",
  241. cfe->num_iospace);
  242. if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
  243. bus_addr_t maxaddr = (pa->pf->sc->iobase + pa->pf->sc->iosize);
  244. for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
  245. /*
  246. * the 3c562 can only use 0x??00-0x??7f
  247. * according to the Linux driver
  248. */
  249. if (i & 0x80)
  250. continue;
  251. if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
  252. cfe->iospace[0].length, &psc->sc_pcioh) == 0)
  253. break;
  254. }
  255. if (i >= maxaddr) {
  256. printf(": can't allocate i/o space\n");
  257. return;
  258. }
  259. } else {
  260. if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
  261. cfe->iospace[0].length, &psc->sc_pcioh))
  262. printf(": can't allocate i/o space\n");
  263. }
  264. sc->sc_iot = psc->sc_pcioh.iot;
  265. sc->sc_ioh = psc->sc_pcioh.ioh;
  266. if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
  267. PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
  268. &psc->sc_pcioh, &psc->sc_io_window)) {
  269. printf(": can't map i/o space\n");
  270. return;
  271. }
  272. printf(" port 0x%lx/%ld", psc->sc_pcioh.addr, psc->sc_pcioh.size);
  273. switch (pa->product) {
  274. case PCMCIA_PRODUCT_3COM_3C562:
  275. /*
  276. * 3c562a-c use this; 3c562d does it in the regular way.
  277. * we might want to check the revision and produce a warning
  278. * in the future.
  279. */
  280. /* FALLTHROUGH */
  281. case PCMCIA_PRODUCT_3COM_3C574:
  282. case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
  283. /*
  284. * Apparently, some 3c574s do it this way, as well.
  285. */
  286. if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
  287. enaddr = myla;
  288. break;
  289. }
  290. sc->bustype = EP_BUS_PCMCIA;
  291. epp = ep_pcmcia_lookup(pa);
  292. if (epp == NULL)
  293. panic("ep_pcmcia_attach: impossible");
  294. sc->ep_flags = epp->epp_flags;
  295. #ifdef notyet
  296. sc->enable = ep_pcmcia_enable;
  297. sc->disable = ep_pcmcia_disable;
  298. #endif
  299. /* establish the interrupt. */
  300. sc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_NET, epintr, sc,
  301. sc->sc_dev.dv_xname);
  302. intrstr = pcmcia_intr_string(psc->sc_pf, sc->sc_ih);
  303. if (*intrstr)
  304. printf(", %s", intrstr);
  305. printf(":");
  306. epconfig(sc, epp->epp_chipset, enaddr);
  307. #ifdef notyet
  308. sc->enabled = 0;
  309. ep_pcmcia_disable1(sc);
  310. #endif
  311. }
  312. int
  313. ep_pcmcia_detach(dev, flags)
  314. struct device *dev;
  315. int flags;
  316. {
  317. int rv;
  318. struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)dev;
  319. if ((rv = ep_detach(dev)) != 0)
  320. return (rv);
  321. pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
  322. pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
  323. return (0);
  324. }
  325. int
  326. ep_pcmcia_activate(dev, act)
  327. struct device *dev;
  328. int act;
  329. {
  330. struct ep_pcmcia_softc *sc = (struct ep_pcmcia_softc *)dev;
  331. struct ep_softc *esc = &sc->sc_ep;
  332. struct ifnet *ifp = &esc->sc_arpcom.ac_if;
  333. switch (act) {
  334. case DVACT_SUSPEND:
  335. ifp->if_timer = 0;
  336. if (ifp->if_flags & IFF_RUNNING)
  337. epstop(esc);
  338. if (sc->sc_ep.sc_ih)
  339. pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ep.sc_ih);
  340. sc->sc_ep.sc_ih = NULL;
  341. pcmcia_function_disable(sc->sc_pf);
  342. break;
  343. case DVACT_RESUME:
  344. pcmcia_function_enable(sc->sc_pf);
  345. sc->sc_ep.sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET,
  346. epintr, sc, esc->sc_dev.dv_xname);
  347. if (ifp->if_flags & IFF_UP)
  348. epinit(esc);
  349. break;
  350. case DVACT_DEACTIVATE:
  351. if (sc->sc_ep.sc_ih)
  352. pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ep.sc_ih);
  353. sc->sc_ep.sc_ih = NULL;
  354. pcmcia_function_disable(sc->sc_pf);
  355. break;
  356. }
  357. return (0);
  358. }
  359. int
  360. ep_pcmcia_get_enaddr(tuple, arg)
  361. struct pcmcia_tuple *tuple;
  362. void *arg;
  363. {
  364. u_int8_t *myla = arg;
  365. int i;
  366. /* this is 3c562a-c magic */
  367. if (tuple->code == 0x88) {
  368. if (tuple->length < ETHER_ADDR_LEN)
  369. return (0);
  370. for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
  371. myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
  372. myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
  373. }
  374. return (1);
  375. }
  376. return (0);
  377. }