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