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