1*54d766aeSmpi /* $OpenBSD: uni_n.c,v 1.17 2015/03/30 13:45:02 mpi Exp $ */
2d9a5f17fSdrahn
3d9a5f17fSdrahn /*
4*54d766aeSmpi * Copyright (c) 2013 Martin Pieuchot
59230ee6aSdrahn * Copyright (c) 1998-2001 Dale Rahn.
69230ee6aSdrahn * All rights reserved.
7d9a5f17fSdrahn *
8d9a5f17fSdrahn * Redistribution and use in source and binary forms, with or without
9d9a5f17fSdrahn * modification, are permitted provided that the following conditions
10d9a5f17fSdrahn * are met:
11d9a5f17fSdrahn * 1. Redistributions of source code must retain the above copyright
12d9a5f17fSdrahn * notice, this list of conditions and the following disclaimer.
13d9a5f17fSdrahn * 2. Redistributions in binary form must reproduce the above copyright
14d9a5f17fSdrahn * notice, this list of conditions and the following disclaimer in the
15d9a5f17fSdrahn * documentation and/or other materials provided with the distribution.
16d9a5f17fSdrahn *
17d9a5f17fSdrahn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d9a5f17fSdrahn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d9a5f17fSdrahn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d9a5f17fSdrahn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d9a5f17fSdrahn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d9a5f17fSdrahn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d9a5f17fSdrahn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d9a5f17fSdrahn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d9a5f17fSdrahn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d9a5f17fSdrahn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d9a5f17fSdrahn */
28d9a5f17fSdrahn
29d9a5f17fSdrahn #include <sys/param.h>
30d9a5f17fSdrahn #include <sys/device.h>
31d9a5f17fSdrahn #include <sys/systm.h>
32d9a5f17fSdrahn
33d9a5f17fSdrahn #include <machine/bus.h>
34d9a5f17fSdrahn #include <machine/autoconf.h>
35d9a5f17fSdrahn
36d9a5f17fSdrahn #include <dev/ofw/openfirm.h>
37d9a5f17fSdrahn
38*54d766aeSmpi #define UNINORTH_CLK_OFFSET 0x20
39*54d766aeSmpi #define UNINORTH_POW_OFFSET 0x30
40*54d766aeSmpi #define UNINORTH_STA_OFFSET 0x70
41*54d766aeSmpi #define UNINORTH_MPIC_OFFSET 0xe0
42*54d766aeSmpi
43*54d766aeSmpi #define UNINORTH_PCICLOCK_CTL 0x01
44*54d766aeSmpi #define UNINORTH_ETHERNET_CTL 0x02
45*54d766aeSmpi #define UNINORTH_FIREWIRE_CTL 0x04
46*54d766aeSmpi
47*54d766aeSmpi #define UNINORTH_POW_NORMAL 0x00
48*54d766aeSmpi #define UNINORTH_POW_IDLE 0x01
49*54d766aeSmpi #define UNINORTH_POW_SLEEP 0x02
50*54d766aeSmpi
51*54d766aeSmpi #define UNINORTH_MPIC_RESET 0x02
52*54d766aeSmpi #define UNINORTH_MPIC_ENABLE 0x04
53*54d766aeSmpi
54*54d766aeSmpi #define UNINORTH_SLEEPING 0x01
55*54d766aeSmpi #define UNINORTH_RUNNING 0x02
56*54d766aeSmpi
57*54d766aeSmpi
58d9a5f17fSdrahn struct memc_softc {
59d9a5f17fSdrahn struct device sc_dev;
606f49038bSdrahn struct ppc_bus_space sc_membus_space;
616f49038bSdrahn
62*54d766aeSmpi uint8_t *sc_baseaddr;
63d9a5f17fSdrahn };
646f49038bSdrahn
656f49038bSdrahn int memcmatch(struct device *, void *, void *);
666f49038bSdrahn void memcattach(struct device *, struct device *, void *);
676f49038bSdrahn void memc_attach_children(struct memc_softc *sc, int memc_node);
686f49038bSdrahn int memc_print(void *aux, const char *name);
696f49038bSdrahn
70d9a5f17fSdrahn struct cfdriver memc_cd = {
71d9a5f17fSdrahn NULL, "memc", DV_DULL
72d9a5f17fSdrahn };
73*54d766aeSmpi
74*54d766aeSmpi const struct cfattach memc_ca = {
75d9a5f17fSdrahn sizeof(struct memc_softc), memcmatch, memcattach
76d9a5f17fSdrahn };
77d9a5f17fSdrahn
78*54d766aeSmpi void memc_sleep(void);
79*54d766aeSmpi void memc_resume(void);
80*54d766aeSmpi uint32_t memc_read(struct memc_softc *sc, int);
81*54d766aeSmpi void memc_write(struct memc_softc *sc, int, uint32_t);
82*54d766aeSmpi void memc_enable(struct memc_softc *, int, uint32_t);
83*54d766aeSmpi void memc_disable(struct memc_softc *, int, uint32_t);
84d9a5f17fSdrahn
85d9a5f17fSdrahn int
memcmatch(struct device * parent,void * cf,void * aux)86093da1aaSdrahn memcmatch(struct device *parent, void *cf, void *aux)
87d9a5f17fSdrahn {
88d9a5f17fSdrahn struct confargs *ca = aux;
89d9a5f17fSdrahn
90*54d766aeSmpi if (strcmp(ca->ca_name, "memc") != 0)
91*54d766aeSmpi return (0);
92*54d766aeSmpi
93*54d766aeSmpi return (1);
94d9a5f17fSdrahn }
95d9a5f17fSdrahn
966f49038bSdrahn void
memcattach(struct device * parent,struct device * self,void * aux)97093da1aaSdrahn memcattach(struct device *parent, struct device *self, void *aux)
98d9a5f17fSdrahn {
99d9a5f17fSdrahn struct memc_softc *sc = (struct memc_softc *)self;
100903f1858Smpi struct confargs *ca = aux;
101*54d766aeSmpi uint32_t rev, reg[2];
102*54d766aeSmpi char name[32];
103903f1858Smpi int len;
104d9a5f17fSdrahn
105*54d766aeSmpi OF_getprop(ca->ca_node, "reg", ®, sizeof(reg));
106*54d766aeSmpi
107903f1858Smpi len = OF_getprop(ca->ca_node, "name", name, sizeof(name));
108093da1aaSdrahn if (len > 0)
109d9a5f17fSdrahn name[len] = 0;
110093da1aaSdrahn
111*54d766aeSmpi /* Map the first page in order to access the registers */
112*54d766aeSmpi if (strcmp(name, "u3") == 0 || strcmp(name, "u4") == 0)
113*54d766aeSmpi sc->sc_baseaddr = mapiodev(reg[1], PAGE_SIZE);
114*54d766aeSmpi else
115*54d766aeSmpi sc->sc_baseaddr = mapiodev(reg[0], PAGE_SIZE);
116*54d766aeSmpi
117*54d766aeSmpi /* Enable the ethernet clock */
118*54d766aeSmpi memc_enable(sc, UNINORTH_CLK_OFFSET, UNINORTH_ETHERNET_CTL);
119903f1858Smpi len = OF_getprop(ca->ca_node, "device-rev", &rev, sizeof(rev));
120903f1858Smpi if (len < 0)
121903f1858Smpi rev = 0;
122903f1858Smpi
123903f1858Smpi printf (": %s rev 0x%x\n", name, rev);
1246f49038bSdrahn
1256f49038bSdrahn memc_attach_children(sc, ca->ca_node);
1266f49038bSdrahn }
1276f49038bSdrahn
1286f49038bSdrahn void
memc_attach_children(struct memc_softc * sc,int memc_node)1296f49038bSdrahn memc_attach_children(struct memc_softc *sc, int memc_node)
1306f49038bSdrahn {
1316f49038bSdrahn struct confargs ca;
1326f49038bSdrahn int node, namelen;
1336f49038bSdrahn u_int32_t reg[20];
134*54d766aeSmpi int32_t intr[8];
1356f49038bSdrahn char name[32];
1366f49038bSdrahn
1376f49038bSdrahn ca.ca_iot = &sc->sc_membus_space;
1386f49038bSdrahn ca.ca_dmat = 0; /* XXX */
1396f49038bSdrahn ca.ca_baseaddr = 0; /* XXX */
1406f49038bSdrahn sc->sc_membus_space.bus_base = ca.ca_baseaddr;
1416f49038bSdrahn
1426f49038bSdrahn for (node = OF_child(memc_node); node; node = OF_peer(node)) {
1436f49038bSdrahn namelen = OF_getprop(node, "name", name, sizeof(name));
1446f49038bSdrahn if (namelen < 0)
1456f49038bSdrahn continue;
1466f49038bSdrahn if (namelen >= sizeof(name))
1476f49038bSdrahn continue;
1486f49038bSdrahn name[namelen] = 0;
1496f49038bSdrahn
1506f49038bSdrahn ca.ca_name = name;
1516f49038bSdrahn ca.ca_node = node;
1526f49038bSdrahn ca.ca_nreg = OF_getprop(node, "reg", reg, sizeof(reg));
1536f49038bSdrahn ca.ca_reg = reg;
154*54d766aeSmpi ca.ca_nintr = OF_getprop(node, "AAPL,interrupts", intr,
155*54d766aeSmpi sizeof(intr));
156*54d766aeSmpi if (ca.ca_nintr == -1)
157*54d766aeSmpi ca.ca_nintr = OF_getprop(node, "interrupts", intr,
158*54d766aeSmpi sizeof(intr));
159*54d766aeSmpi ca.ca_intr = intr;
160*54d766aeSmpi
161*54d766aeSmpi if (strcmp(ca.ca_name, "mpic") == 0)
162*54d766aeSmpi memc_enable(sc, UNINORTH_MPIC_OFFSET,
163*54d766aeSmpi UNINORTH_MPIC_RESET|UNINORTH_MPIC_ENABLE);
1646f49038bSdrahn
1656f49038bSdrahn config_found((struct device *)sc, &ca, memc_print);
1666f49038bSdrahn }
1676f49038bSdrahn }
1686f49038bSdrahn
1696f49038bSdrahn int
memc_print(void * aux,const char * name)1706f49038bSdrahn memc_print(void *aux, const char *name)
1716f49038bSdrahn {
1726f49038bSdrahn struct confargs *ca = aux;
1736f49038bSdrahn /* we dont want extra stuff printing */
1746f49038bSdrahn if (name)
175f93ad0cbSkettenis printf("\"%s\" at %s", ca->ca_name, name);
1766f49038bSdrahn if (ca->ca_nreg > 0)
1776f49038bSdrahn printf(" offset 0x%x", ca->ca_reg[0]);
1786f49038bSdrahn return UNCONF;
179d9a5f17fSdrahn }
180093da1aaSdrahn
18199ff212cSmiod void
memc_sleep(void)182*54d766aeSmpi memc_sleep(void)
183d9a5f17fSdrahn {
184*54d766aeSmpi struct memc_softc *sc = memc_cd.cd_devs[0];
185d9a5f17fSdrahn
186*54d766aeSmpi memc_write(sc, UNINORTH_STA_OFFSET, UNINORTH_SLEEPING);
187*54d766aeSmpi DELAY(10);
188*54d766aeSmpi memc_write(sc, UNINORTH_POW_OFFSET, UNINORTH_POW_SLEEP);
189*54d766aeSmpi DELAY(10);
190d9a5f17fSdrahn }
191*54d766aeSmpi
192*54d766aeSmpi void
memc_resume(void)193*54d766aeSmpi memc_resume(void)
194*54d766aeSmpi {
195*54d766aeSmpi struct memc_softc *sc = memc_cd.cd_devs[0];
196*54d766aeSmpi
197*54d766aeSmpi memc_write(sc, UNINORTH_POW_OFFSET, UNINORTH_POW_NORMAL);
198*54d766aeSmpi DELAY(10);
199*54d766aeSmpi memc_write(sc, UNINORTH_STA_OFFSET, UNINORTH_RUNNING);
200*54d766aeSmpi DELAY(100); /* XXX */
201d9a5f17fSdrahn }
202*54d766aeSmpi
203*54d766aeSmpi uint32_t
memc_read(struct memc_softc * sc,int offset)204*54d766aeSmpi memc_read(struct memc_softc *sc, int offset)
205*54d766aeSmpi {
206*54d766aeSmpi return in32(sc->sc_baseaddr + offset);
207*54d766aeSmpi }
208*54d766aeSmpi
209*54d766aeSmpi void
memc_write(struct memc_softc * sc,int offset,uint32_t value)210*54d766aeSmpi memc_write(struct memc_softc *sc, int offset, uint32_t value)
211*54d766aeSmpi {
212*54d766aeSmpi out32(sc->sc_baseaddr + offset, value);
213*54d766aeSmpi }
214*54d766aeSmpi
215*54d766aeSmpi void
memc_enable(struct memc_softc * sc,int offset,uint32_t bits)216*54d766aeSmpi memc_enable(struct memc_softc *sc, int offset, uint32_t bits)
217*54d766aeSmpi {
218*54d766aeSmpi bits |= memc_read(sc, offset);
219*54d766aeSmpi memc_write(sc, offset, bits);
220*54d766aeSmpi }
221*54d766aeSmpi
222*54d766aeSmpi void
memc_disable(struct memc_softc * sc,int offset,uint32_t bits)223*54d766aeSmpi memc_disable(struct memc_softc *sc, int offset, uint32_t bits)
224*54d766aeSmpi {
225*54d766aeSmpi bits = memc_read(sc, offset) & ~bits;
226*54d766aeSmpi memc_write(sc, offset, bits);
22701a12621Smiod }
228