if_smsh_gxio.c revision 1.3 1 1.3 kiyohara /* $NetBSD: if_smsh_gxio.c,v 1.3 2010/08/28 04:30:24 kiyohara Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2008 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara
28 1.1 kiyohara #include <sys/cdefs.h>
29 1.3 kiyohara __KERNEL_RCSID(0, "$NetBSD: if_smsh_gxio.c,v 1.3 2010/08/28 04:30:24 kiyohara Exp $");
30 1.1 kiyohara
31 1.1 kiyohara #include <sys/param.h>
32 1.1 kiyohara #include <sys/device.h>
33 1.1 kiyohara #include <sys/errno.h>
34 1.1 kiyohara #include <sys/bus.h>
35 1.1 kiyohara #include <sys/systm.h>
36 1.1 kiyohara
37 1.1 kiyohara #include <evbarm/gumstix/gumstixvar.h>
38 1.1 kiyohara
39 1.1 kiyohara #include <net/if.h>
40 1.1 kiyohara #include <net/if_ether.h>
41 1.1 kiyohara #include <net/if_media.h>
42 1.1 kiyohara
43 1.1 kiyohara #include <dev/mii/miivar.h>
44 1.1 kiyohara
45 1.1 kiyohara #include <dev/ic/lan9118var.h>
46 1.1 kiyohara #include <dev/ic/lan9118reg.h>
47 1.1 kiyohara
48 1.1 kiyohara #include "locators.h"
49 1.1 kiyohara
50 1.1 kiyohara
51 1.1 kiyohara static int smsh_gxio_match(device_t, struct cfdata *, void *);
52 1.1 kiyohara static void smsh_gxio_attach(device_t, device_t, void *);
53 1.1 kiyohara
54 1.3 kiyohara static int ether_serial_digit = 1;
55 1.1 kiyohara
56 1.3 kiyohara CFATTACH_DECL_NEW(smsh_gxio, sizeof(struct lan9118_softc),
57 1.1 kiyohara smsh_gxio_match, smsh_gxio_attach, NULL, NULL);
58 1.1 kiyohara
59 1.1 kiyohara
60 1.1 kiyohara /* ARGSUSED */
61 1.1 kiyohara static int
62 1.1 kiyohara smsh_gxio_match(device_t parent, struct cfdata *match, void *aux)
63 1.1 kiyohara {
64 1.1 kiyohara struct gxio_attach_args *gxa = aux;
65 1.1 kiyohara bus_space_tag_t iot = gxa->gxa_iot;
66 1.1 kiyohara bus_space_handle_t ioh;
67 1.1 kiyohara uint32_t val;
68 1.1 kiyohara int rv = 0;
69 1.1 kiyohara
70 1.1 kiyohara /* Disallow wildcarded values. */
71 1.1 kiyohara if (gxa->gxa_addr == GXIOCF_ADDR_DEFAULT)
72 1.1 kiyohara return 0;
73 1.1 kiyohara if (gxa->gxa_gpirq == GXIOCF_GPIRQ_DEFAULT)
74 1.1 kiyohara return 0;
75 1.1 kiyohara
76 1.1 kiyohara if (bus_space_map(iot, gxa->gxa_addr, LAN9118_IOSIZE, 0, &ioh) != 0)
77 1.1 kiyohara return 0;
78 1.1 kiyohara
79 1.1 kiyohara bus_space_write_4(iot, ioh, LAN9118_BYTE_TEST, 0);
80 1.1 kiyohara val = bus_space_read_4(iot, ioh, LAN9118_BYTE_TEST);
81 1.1 kiyohara if (val == LAN9118_BYTE_TEST_VALUE)
82 1.1 kiyohara /* Assume we have an SMSC LAN9117 */
83 1.1 kiyohara rv = 1;
84 1.1 kiyohara if (ether_serial_digit > 15)
85 1.1 kiyohara rv = 0;
86 1.1 kiyohara
87 1.1 kiyohara bus_space_unmap(iot, ioh, LAN9118_IOSIZE);
88 1.1 kiyohara return rv;
89 1.1 kiyohara }
90 1.1 kiyohara
91 1.1 kiyohara /* ARGSUSED */
92 1.1 kiyohara static void
93 1.1 kiyohara smsh_gxio_attach(device_t parent, device_t self, void *aux)
94 1.1 kiyohara {
95 1.3 kiyohara struct lan9118_softc *sc = device_private(self);
96 1.1 kiyohara struct gxio_attach_args *gxa = aux;
97 1.3 kiyohara void *ih;
98 1.1 kiyohara
99 1.1 kiyohara KASSERT(system_serial_high != 0 || system_serial_low != 0);
100 1.1 kiyohara
101 1.1 kiyohara sc->sc_dev = self;
102 1.1 kiyohara
103 1.1 kiyohara /* Map i/o space. */
104 1.1 kiyohara if (bus_space_map(gxa->gxa_iot, gxa->gxa_addr, LAN9118_IOSIZE, 0,
105 1.1 kiyohara &sc->sc_ioh))
106 1.3 kiyohara panic("smsh_gxio_attach: can't map i/o space");
107 1.1 kiyohara sc->sc_iot = gxa->gxa_iot;
108 1.1 kiyohara
109 1.1 kiyohara sc->sc_enaddr[0] = ((system_serial_high >> 8) & 0xfe) | 0x02;
110 1.1 kiyohara sc->sc_enaddr[1] = system_serial_high;
111 1.1 kiyohara sc->sc_enaddr[2] = system_serial_low >> 24;
112 1.1 kiyohara sc->sc_enaddr[3] = system_serial_low >> 16;
113 1.1 kiyohara sc->sc_enaddr[4] = system_serial_low >> 8;
114 1.1 kiyohara sc->sc_enaddr[5] = (system_serial_low & 0xc0) |
115 1.1 kiyohara (1 << 4) | ((ether_serial_digit++) & 0x0f);
116 1.1 kiyohara sc->sc_flags = LAN9118_FLAGS_NO_EEPROM;
117 1.1 kiyohara if (lan9118_attach(sc) != 0) {
118 1.1 kiyohara bus_space_unmap(sc->sc_iot, sc->sc_ioh, LAN9118_IOSIZE);
119 1.1 kiyohara return;
120 1.1 kiyohara }
121 1.1 kiyohara
122 1.1 kiyohara /* Establish the interrupt handler. */
123 1.3 kiyohara ih = gxio_intr_establish(gxa->gxa_sc,
124 1.1 kiyohara gxa->gxa_gpirq, IST_EDGE_FALLING, IPL_NET, lan9118_intr, sc);
125 1.3 kiyohara if (ih == NULL) {
126 1.1 kiyohara aprint_error_dev(self,
127 1.1 kiyohara "couldn't establish interrupt handler\n");
128 1.1 kiyohara bus_space_unmap(sc->sc_iot, sc->sc_ioh, LAN9118_IOSIZE);
129 1.1 kiyohara return;
130 1.1 kiyohara }
131 1.1 kiyohara }
132