1/*	$NetBSD: if_dme_gpmc.c,v 1.1 2010/09/08 22:49:49 ahoka Exp $	*/
2
3/*
4 * Copyright (c) 2010 Adam Hoka
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 2009 Paul Fleischer
31 * All rights reserved.
32 *
33 * 1. Redistributions of source code must retain the above copyright
34 *    notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 *    notice, this list of conditions and the following disclaimer in the
37 *    documentation and/or other materials provided with the distribution.
38 * 3. The name of the company nor the name of the author may be used to
39 *    endorse or promote products derived from this software without specific
40 *    prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
43 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
46 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 */
54
55#include <sys/cdefs.h>
56
57#include "opt_omap.h"
58
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/socket.h>
62#include <sys/device.h>
63
64#include <net/if.h>
65#include <net/if_ether.h>
66#include <net/if_media.h>
67
68#include <sys/bus.h>
69#include <machine/intr.h>
70
71#include <arch/arm/omap/omap2_gpmcvar.h>
72#include <arch/arm/omap/omap2_gpmcreg.h>
73
74#include <dev/ic/dm9000var.h>
75#include <dev/ic/dm9000reg.h>
76
77int	dme_gpmc_match(device_t, struct cfdata *, void *);
78void	dme_gpmc_attach(device_t, device_t, void *);
79
80CFATTACH_DECL_NEW(dme_gpmc, sizeof(struct dme_softc),
81    dme_gpmc_match, dme_gpmc_attach, NULL, NULL);
82
83int
84dme_gpmc_match(device_t parent, struct cfdata *cf, void *aux)
85{
86	struct gpmc_attach_args *gpmc = aux;
87	bus_space_tag_t iot = gpmc->gpmc_iot;
88	bus_space_handle_t ioh;
89
90	uint8_t vendor_id[2];
91	uint8_t product_id[2];
92	int result = 0;
93
94	/* Map memory to access the device during probing */
95	/* XXX should be done with two mappings */
96	if (bus_space_map(iot, gpmc->gpmc_addr, 0x404, 0, &ioh))
97		goto out;
98
99	/* Used to temporarily access the device for probing */
100	struct dme_softc sc = {
101		.sc_iot = iot,
102		.sc_ioh = ioh,
103		.dme_io = 0x0,
104		.dme_data = 0x400
105	};
106
107	vendor_id[0] = dme_read(&sc, DM9000_VID0);
108	vendor_id[1] = dme_read(&sc, DM9000_VID1);
109
110	product_id[0] = dme_read(&sc, DM9000_PID0);
111	product_id[1] = dme_read(&sc, DM9000_PID1);
112
113	if (vendor_id[0] == 0x46 && vendor_id[1] == 0x0a &&
114	    product_id[0] == 0x00 && product_id[1] == 0x90 ) {
115		result = 1;
116	}
117
118out:
119	bus_space_unmap(iot, ioh, 0x404);
120
121	return result;
122}
123
124
125void
126dme_gpmc_attach(device_t parent, device_t self, void *aux)
127{
128	struct dme_softc *sc = device_private(self);
129	struct gpmc_attach_args *gpmc = aux;
130	/* XXX read eeprom or derive from die id */
131#define DME_ETHER_ADDR_FIXED 0,0x0a,0xb1,0,1,0xff
132#ifdef DME_ETHER_ADDR_FIXED
133	static u_int8_t enaddr[ETHER_ADDR_LEN] = {DME_ETHER_ADDR_FIXED};
134#else
135#define enaddr NULL
136#endif
137	sc->sc_iot = gpmc->gpmc_iot;
138	sc->sc_dev = self;
139
140	/* XXX should be done with two mappings */
141	if (bus_space_map(sc->sc_iot,
142		gpmc->gpmc_addr, 0x404, 0, &sc->sc_ioh)) {
143		aprint_error(": unable to map i/o space\n");
144		return;
145	}
146
147	sc->sc_ih = intr_establish(gpmc->gpmc_intr, IPL_NET, IST_LEVEL_LOW,
148	    dme_intr, sc);
149
150	if (sc->sc_ih == NULL) {
151		aprint_error(": unable to establish interrupt\n");
152		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0x404);
153	}
154
155	sc->dme_io = 0x0;
156	sc->dme_data = 0x400;
157
158	aprint_normal("\n");
159
160	dme_attach(sc, enaddr);
161}
162