1 /*        $NetBSD: if_ne_mainbus.c,v 1.1 2011/02/19 10:46:28 kiyohara Exp $     */
2 /*
3  * Copyright (c) 2010 KIYOHARA Takashi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: if_ne_mainbus.c,v 1.1 2011/02/19 10:46:28 kiyohara Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/device.h>
34 #include <sys/errno.h>
35 
36 #include <machine/autoconf.h>
37 #include <machine/intr.h>
38 #include <machine/mmeye.h>
39 
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_ether.h>
43 #include <net/if_media.h>
44 
45 #include <dev/ic/dp8390reg.h>
46 #include <dev/ic/dp8390var.h>
47 
48 #include <dev/ic/ne2000reg.h>
49 #include <dev/ic/ne2000var.h>
50 
51 #include "locators.h"
52 
53 static int ne_mainbus_match(device_t, cfdata_t , void *);
54 static void ne_mainbus_attach(device_t, device_t, void *);
55 
56 CFATTACH_DECL_NEW(ne_mainbus, sizeof(struct ne2000_softc),
57     ne_mainbus_match, ne_mainbus_attach, NULL, NULL);
58 
59 static int
ne_mainbus_match(device_t parent,cfdata_t match,void * aux)60 ne_mainbus_match(device_t parent, cfdata_t match, void *aux)
61 {
62           struct mainbus_attach_args *ma = aux;
63           bus_space_tag_t nict, asict;
64           bus_space_handle_t nich, asich;
65           int rv = 0;
66 
67           if (strcmp(ma->ma_name, match->cf_name) != 0)
68                     return 0;
69 
70           /* Disallow wildcarded values. */
71           if (ma->ma_addr1 == MAINBUSCF_ADDR1_DEFAULT ||
72               ma->ma_irq1 == MAINBUSCF_IRQ1_DEFAULT)
73                     return 0;
74 
75           /* Make sure this is a valid NE[12]000 i/o address. */
76           if ((ma->ma_addr1 & 0x1f) != 0)
77                     return 0;
78 
79           /* Map i/o space. */
80           nict = SH3_BUS_SPACE_PCMCIA_IO;
81           if (bus_space_map(nict, ma->ma_addr1, NE2000_NPORTS, 0, &nich))
82                     return 0;
83 
84           if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
85               NE2000_ASIC_NPORTS, &asich))
86                     goto out;
87           asict = nict;
88 
89           /* Look for an NE2000-compatible card. */
90           rv = ne2000_detect(nict, nich, asict, asich);
91 
92  out:
93           bus_space_unmap(nict, nich, NE2000_NPORTS);
94           return rv;
95 }
96 
97 void
ne_mainbus_attach(device_t parent,device_t self,void * aux)98 ne_mainbus_attach(device_t parent, device_t self, void *aux)
99 {
100           struct ne2000_softc *sc = device_private(self);
101           struct dp8390_softc *dsc = &sc->sc_dp8390;
102           struct mainbus_attach_args *ma = aux;
103           bus_space_tag_t nict, asict;
104           bus_space_handle_t nich, asich;
105           int netype;
106           const char *typestr;
107           void *ih;
108 
109           dsc->sc_dev = self;
110           aprint_normal("\n");
111 
112           /* Map i/o space. */
113           nict = SH3_BUS_SPACE_PCMCIA_IO;
114           if (bus_space_map(nict, ma->ma_addr1, NE2000_NPORTS, 0, &nich)) {
115                     aprint_error_dev(self, "can't map i/o space\n");
116                     return;
117           }
118 
119           if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
120               NE2000_ASIC_NPORTS, &asich)) {
121                     aprint_error_dev(self, "can't subregion i/o space\n");
122                     return;
123           }
124           asict = nict;
125 
126           dsc->sc_regt = nict;
127           dsc->sc_regh = nich;
128 
129           sc->sc_asict = asict;
130           sc->sc_asich = asich;
131 
132           /*
133            * Detect it again, so we can print some information about the
134            * interface.
135            */
136           netype = ne2000_detect(nict, nich, asict, asich);
137           switch (netype) {
138           case NE2000_TYPE_RTL8019:
139                     typestr = "NE2000 (RTL8019)";
140                     break;
141 
142           case NE2000_TYPE_NE1000:
143           case NE2000_TYPE_NE2000:
144           default:
145                     aprint_error_dev(self, "where did the card go?!\n");
146                     return;
147           }
148 
149           aprint_normal_dev(self, "%s Ethernet\n", typestr);
150 
151           /* This interface is always enabled. */
152           dsc->sc_enabled = 1;
153 
154           /*
155            * Do generic NE2000 attach.  This will read the station address
156            * from the EEPROM.
157            */
158           ne2000_attach(sc, NULL);
159 
160           /* Establish the interrupt handler. */
161           ih = mmeye_intr_establish(ma->ma_irq1, IST_LEVEL, IPL_NET,
162               dp8390_intr, dsc);
163           if (ih == NULL)
164                     aprint_error_dev(self,
165                         "couldn't establish interrupt handler\n");
166 }
167