xref: /netbsd-src/sys/arch/evbarm/gumstix/if_smsh_gxio.c (revision ab81e3684b148d8bb6a605f86e0d9c990979e05d)
1 /*	$NetBSD: if_smsh_gxio.c,v 1.3 2010/08/28 04:30:24 kiyohara Exp $	*/
2 /*
3  * Copyright (c) 2008 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_smsh_gxio.c,v 1.3 2010/08/28 04:30:24 kiyohara Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/errno.h>
34 #include <sys/bus.h>
35 #include <sys/systm.h>
36 
37 #include <evbarm/gumstix/gumstixvar.h>
38 
39 #include <net/if.h>
40 #include <net/if_ether.h>
41 #include <net/if_media.h>
42 
43 #include <dev/mii/miivar.h>
44 
45 #include <dev/ic/lan9118var.h>
46 #include <dev/ic/lan9118reg.h>
47 
48 #include "locators.h"
49 
50 
51 static int smsh_gxio_match(device_t, struct cfdata *, void *);
52 static void smsh_gxio_attach(device_t, device_t, void *);
53 
54 static int ether_serial_digit = 1;
55 
56 CFATTACH_DECL_NEW(smsh_gxio, sizeof(struct lan9118_softc),
57     smsh_gxio_match, smsh_gxio_attach, NULL, NULL);
58 
59 
60 /* ARGSUSED */
61 static int
smsh_gxio_match(device_t parent,struct cfdata * match,void * aux)62 smsh_gxio_match(device_t parent, struct cfdata *match, void *aux)
63 {
64 	struct gxio_attach_args *gxa = aux;
65 	bus_space_tag_t iot = gxa->gxa_iot;
66 	bus_space_handle_t ioh;
67 	uint32_t val;
68 	int rv = 0;
69 
70 	/* Disallow wildcarded values. */
71 	if (gxa->gxa_addr == GXIOCF_ADDR_DEFAULT)
72 		return 0;
73 	if (gxa->gxa_gpirq == GXIOCF_GPIRQ_DEFAULT)
74 		return 0;
75 
76 	if (bus_space_map(iot, gxa->gxa_addr, LAN9118_IOSIZE, 0, &ioh) != 0)
77 		return 0;
78 
79 	bus_space_write_4(iot, ioh, LAN9118_BYTE_TEST, 0);
80 	val = bus_space_read_4(iot, ioh, LAN9118_BYTE_TEST);
81 	if (val == LAN9118_BYTE_TEST_VALUE)
82 		/* Assume we have an SMSC LAN9117 */
83 		rv = 1;
84 	if (ether_serial_digit > 15)
85 		rv = 0;
86 
87 	bus_space_unmap(iot, ioh, LAN9118_IOSIZE);
88 	return rv;
89 }
90 
91 /* ARGSUSED */
92 static void
smsh_gxio_attach(device_t parent,device_t self,void * aux)93 smsh_gxio_attach(device_t parent, device_t self, void *aux)
94 {
95 	struct lan9118_softc *sc = device_private(self);
96 	struct gxio_attach_args *gxa = aux;
97 	void *ih;
98 
99 	KASSERT(system_serial_high != 0 || system_serial_low != 0);
100 
101 	sc->sc_dev = self;
102 
103 	/* Map i/o space. */
104 	if (bus_space_map(gxa->gxa_iot, gxa->gxa_addr, LAN9118_IOSIZE, 0,
105 	    &sc->sc_ioh))
106 		panic("smsh_gxio_attach: can't map i/o space");
107 	sc->sc_iot = gxa->gxa_iot;
108 
109 	sc->sc_enaddr[0] = ((system_serial_high >> 8) & 0xfe) | 0x02;
110 	sc->sc_enaddr[1] = system_serial_high;
111 	sc->sc_enaddr[2] = system_serial_low >> 24;
112 	sc->sc_enaddr[3] = system_serial_low >> 16;
113 	sc->sc_enaddr[4] = system_serial_low >> 8;
114 	sc->sc_enaddr[5] = (system_serial_low & 0xc0) |
115 	    (1 << 4) | ((ether_serial_digit++) & 0x0f);
116 	sc->sc_flags = LAN9118_FLAGS_NO_EEPROM;
117 	if (lan9118_attach(sc) != 0) {
118 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LAN9118_IOSIZE);
119 		return;
120 	}
121 
122 	/* Establish the interrupt handler. */
123 	ih = gxio_intr_establish(gxa->gxa_sc,
124 	    gxa->gxa_gpirq, IST_EDGE_FALLING, IPL_NET, lan9118_intr, sc);
125 	if (ih == NULL) {
126 		aprint_error_dev(self,
127 		    "couldn't establish interrupt handler\n");
128 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LAN9118_IOSIZE);
129 		return;
130 	}
131 }
132