ingenic_dme.c revision 1.1 1 /* $NetBSD: ingenic_dme.c,v 1.1 2015/03/10 18:15:47 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Michael Lorenz
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ingenic_dme.c,v 1.1 2015/03/10 18:15:47 macallan Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37 #include <sys/workqueue.h>
38
39 #include <mips/ingenic/ingenic_var.h>
40 #include <mips/ingenic/ingenic_regs.h>
41
42 #include <net/if.h>
43 #include <net/if_ether.h>
44 #include <net/if_media.h>
45
46 #include <dev/ic/dm9000var.h>
47 #include <dev/ic/dm9000reg.h>
48
49 #include "opt_ingenic.h"
50
51 static int ingenic_dme_match(device_t, struct cfdata *, void *);
52 static void ingenic_dme_attach(device_t, device_t, void *);
53 static int ingenic_dme_intr(void *);
54
55 CFATTACH_DECL_NEW(ingenic_dme, sizeof(struct dme_softc),
56 ingenic_dme_match, ingenic_dme_attach, NULL, NULL);
57
58 #define GPIO_DME_INT 19
59 #define GPIO_DME_INT_MASK (1 << GPIO_DME_INT)
60
61 /* ARGSUSED */
62 static int
63 ingenic_dme_match(device_t parent, struct cfdata *match, void *aux)
64 {
65 struct apbus_attach_args *aa = aux;
66
67 if (strcmp(aa->aa_name, "dme") != 0)
68 return 0;
69
70 return 1;
71 }
72
73 /* ARGSUSED */
74 static void
75 ingenic_dme_attach(device_t parent, device_t self, void *aux)
76 {
77 struct dme_softc *sc = device_private(self);
78 struct apbus_attach_args *aa = aux;
79 void *ih;
80 static uint8_t enaddr[ETHER_ADDR_LEN];
81 int error;
82
83 sc->sc_dev = self;
84
85 sc->sc_iot = aa->aa_bst;
86 sc->dme_io = JZ_DME_IO;
87 sc->dme_data = JZ_DME_DATA;
88 sc->sc_phy_initialized = 0;
89
90 if (aa->aa_addr == 0)
91 aa->aa_addr = JZ_DME_BASE;
92
93 error = bus_space_map(aa->aa_bst, aa->aa_addr, 4, 0, &sc->sc_ioh);
94 if (error) {
95 aprint_error_dev(self,
96 "can't map registers for %s: %d\n", aa->aa_name, error);
97 return;
98 }
99
100 aprint_naive(": DM9000 Ethernet controller\n");
101 aprint_normal(": DM9000 Ethernet controller\n");
102
103
104 /* make sure the chip is powered up and not in reset */
105 gpio_as_output(1, 25);
106 gpio_set(1, 25, 1);
107 gpio_as_output(5, 12);
108 gpio_set(5, 12, 1);
109
110 /* setup pins to talk to the chip */
111 gpio_as_dev0(1, 1);
112 gpio_as_dev0(0, 0);
113 gpio_as_dev0(0, 1);
114 gpio_as_dev0(0, 2);
115 gpio_as_dev0(0, 3);
116 gpio_as_dev0(0, 4);
117 gpio_as_dev0(0, 5);
118 gpio_as_dev0(0, 6);
119 gpio_as_dev0(0, 7);
120
121 gpio_as_dev0(0, 16);
122 gpio_as_dev0(0, 17);
123 gpio_as_dev0(0, 26);
124
125 /* DM9000 interrupt is on GPIO E pin 19 */
126 gpio_as_intr_level(4, GPIO_DME_INT);
127 ih = evbmips_intr_establish(13, ingenic_dme_intr, sc);
128
129 if (ih == NULL) {
130 aprint_error_dev(self, "failed to establish interrupt %d\n",
131 13);
132 goto fail;
133 }
134
135 /*
136 * XXX grab MAC address set by uboot
137 * I'm not sure uboot will program the MAC address into the chip when
138 * not netbooting, so this needs to go away
139 */
140 dme_read_c(sc, DM9000_PAB0, enaddr, 6);
141 dme_attach(sc, enaddr);
142 return;
143 fail:
144 if (ih) {
145 evbmips_intr_disestablish(ih);
146 }
147 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 4);
148 }
149
150 static int
151 ingenic_dme_intr(void *arg)
152 {
153 uint32_t reg;
154 int ret = 0;
155
156 /* see if it's us */
157 reg = readreg(JZ_GPIO_E_BASE + JZ_GPIO_FLAG);
158 if (reg & GPIO_DME_INT_MASK) {
159 /* yes, it's ours, handle it... */
160 ret = dme_intr(arg);
161 /* ... and clear it */
162 writereg(JZ_GPIO_E_BASE + JZ_GPIO_FLAGC, GPIO_DME_INT_MASK);
163 }
164 return ret;
165 }
166