1*7804a487Sskrll /* $NetBSD: epsoc.c,v 1.16 2021/11/21 08:25:26 skrll Exp $ */
229803910Sjoff
329803910Sjoff /*
429803910Sjoff * Copyright (c) 2004 Jesse Off
529803910Sjoff * All rights reserved.
629803910Sjoff *
729803910Sjoff * Redistribution and use in source and binary forms, with or without
829803910Sjoff * modification, are permitted provided that the following conditions
929803910Sjoff * are met:
1029803910Sjoff * 1. Redistributions of source code must retain the above copyright
1129803910Sjoff * notice, this list of conditions and the following disclaimer.
1229803910Sjoff * 2. Redistributions in binary form must reproduce the above copyright
1329803910Sjoff * notice, this list of conditions and the following disclaimer in the
1429803910Sjoff * documentation and/or other materials provided with the distribution.
1529803910Sjoff *
1629803910Sjoff * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1729803910Sjoff * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1829803910Sjoff * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1929803910Sjoff * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2029803910Sjoff * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2129803910Sjoff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2229803910Sjoff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2329803910Sjoff * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2429803910Sjoff * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2529803910Sjoff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2629803910Sjoff * POSSIBILITY OF SUCH DAMAGE.
2729803910Sjoff */
2829803910Sjoff
2929803910Sjoff #include <sys/cdefs.h>
30*7804a487Sskrll __KERNEL_RCSID(0, "$NetBSD: epsoc.c,v 1.16 2021/11/21 08:25:26 skrll Exp $");
3129803910Sjoff
3229803910Sjoff #include <sys/types.h>
3329803910Sjoff #include <sys/param.h>
3429803910Sjoff #include <sys/systm.h>
3529803910Sjoff #include <sys/kernel.h>
3629803910Sjoff #include <sys/time.h>
3729803910Sjoff #include <sys/device.h>
3829803910Sjoff
39cf10107dSdyoung #include <sys/bus.h>
4029803910Sjoff #include <machine/intr.h>
4129803910Sjoff
4229803910Sjoff #include <arm/cpufunc.h>
4329803910Sjoff
4429803910Sjoff #include <arm/ep93xx/ep93xxreg.h>
4529803910Sjoff #include <arm/ep93xx/ep93xxvar.h>
4629803910Sjoff #include <arm/ep93xx/epsocvar.h>
4729803910Sjoff
4829803910Sjoff #include "locators.h"
4929803910Sjoff
50cbab9cadSchs static int epsoc_match(device_t, cfdata_t, void *);
51cbab9cadSchs static void epsoc_attach(device_t, device_t, void *);
52cbab9cadSchs static int epsoc_search(device_t, cfdata_t, const int *, void *);
5329803910Sjoff static int epsoc_print(void *, const char *);
54cbab9cadSchs static int epsoc_submatch(device_t, cfdata_t, const int *, void *);
5529803910Sjoff
56cbab9cadSchs CFATTACH_DECL_NEW(epsoc, sizeof(struct epsoc_softc),
5729803910Sjoff epsoc_match, epsoc_attach, NULL, NULL);
5829803910Sjoff
5929803910Sjoff struct epsoc_softc *epsoc_sc = NULL;
6029803910Sjoff
6129803910Sjoff static int
epsoc_match(device_t parent,cfdata_t match,void * aux)62cbab9cadSchs epsoc_match(device_t parent, cfdata_t match, void *aux)
6329803910Sjoff {
6429803910Sjoff bus_space_handle_t ioh;
6508a4aba7Sskrll uint32_t id, ret = 0;
6629803910Sjoff
6729803910Sjoff bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
6829803910Sjoff EP93XX_APB_SYSCON_SIZE, 0, &ioh);
6929803910Sjoff id = bus_space_read_4(&ep93xx_bs_tag, ioh, EP93XX_SYSCON_ChipID);
7029803910Sjoff if ((id & 0x01faffff) == 0x9213) ret = 1;
7129803910Sjoff bus_space_unmap(&ep93xx_bs_tag, ioh, EP93XX_APB_SYSCON_SIZE);
7229803910Sjoff return ret;
7329803910Sjoff }
7429803910Sjoff
7529803910Sjoff static void
epsoc_attach(device_t parent,device_t self,void * aux)76cbab9cadSchs epsoc_attach(device_t parent, device_t self, void *aux)
7729803910Sjoff {
7829803910Sjoff struct epsoc_softc *sc;
7908a4aba7Sskrll uint64_t fclk, pclk, hclk;
8008a4aba7Sskrll uint32_t id, clkset1;
8182ea600cShe const char *rev;
827959c31aShamajima int locs[EPSOCCF_NLOCS];
837959c31aShamajima struct epsoc_attach_args sa;
8429803910Sjoff
85cbab9cadSchs sc = device_private(self);
8629803910Sjoff
8729803910Sjoff if (epsoc_sc == NULL)
8829803910Sjoff epsoc_sc = sc;
8929803910Sjoff
9029803910Sjoff sc->sc_iot = &ep93xx_bs_tag;
9129803910Sjoff bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
9229803910Sjoff EP93XX_APB_SYSCON_SIZE, 0, &sc->sc_ioh);
9329803910Sjoff id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EP93XX_SYSCON_ChipID);
9429803910Sjoff switch(id >> 28) {
9529803910Sjoff case 0:
9629803910Sjoff rev = "A";
9729803910Sjoff break;
9829803910Sjoff case 1:
9929803910Sjoff rev = "B";
10029803910Sjoff break;
10129803910Sjoff case 2:
10229803910Sjoff rev = "C";
10329803910Sjoff break;
10429803910Sjoff case 3:
10529803910Sjoff rev = "D0";
10629803910Sjoff break;
10729803910Sjoff case 4:
10829803910Sjoff rev = "D1";
10929803910Sjoff break;
11029803910Sjoff case 5:
11129803910Sjoff rev = "E0";
11229803910Sjoff break;
11329803910Sjoff case 6:
11429803910Sjoff rev = "E1";
11529803910Sjoff break;
11629803910Sjoff default:
11729803910Sjoff rev = ">E1";
11829803910Sjoff break;
11929803910Sjoff }
12029803910Sjoff printf(": Cirrus Logic EP93xx SoC rev %s\n", rev);
12129803910Sjoff
12229803910Sjoff clkset1 = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
12329803910Sjoff EP93XX_SYSCON_ClkSet1);
12429803910Sjoff {
12529803910Sjoff int fclkdiv, hclkdiv, pclkdiv, pll1_ps;
12629803910Sjoff int pll1x1fbd1, pll1x2fbd2, pll1x2ipd;
12729803910Sjoff int hclkdivisors[] = {1, 2, 4, 5, 6, 8, 16, 32};
12829803910Sjoff
12929803910Sjoff fclkdiv = (clkset1 & 0x0e000000) >> 25;
13029803910Sjoff hclkdiv = (clkset1 & 0x00700000) >> 20;
13129803910Sjoff pclkdiv = (clkset1 & 0x000c0000) >> 18;
13229803910Sjoff pll1_ps = (clkset1 & 0x00030000) >> 16;
13329803910Sjoff pll1x1fbd1 = (clkset1 & 0x0000f800) >> 11;
13429803910Sjoff pll1x2fbd2 = (clkset1 & 0x000007e0) >> 5;
13529803910Sjoff pll1x2ipd = (clkset1 & 0x0000001f);
13629803910Sjoff fclk = 14745600ULL * ((pll1x1fbd1 + 1) * (pll1x2fbd2 + 1)) /
13729803910Sjoff ((pll1x2ipd + 1) * (1 << pll1_ps));
13829803910Sjoff hclk = fclk / hclkdivisors[hclkdiv];
13929803910Sjoff pclk = hclk >> pclkdiv;
14029803910Sjoff if (fclkdiv > 4) fclkdiv = 0;
14129803910Sjoff if (clkset1 & 0x00800000) /* nBYP1 bypasses PLL */
14229803910Sjoff fclk = fclk >> fclkdiv;
14329803910Sjoff else
14429803910Sjoff fclk = 14745600ULL;
14529803910Sjoff }
146a1f606d3Slukem printf("%s: fclk %lld.%02lld MHz hclk %lld.%02lld MHz pclk %lld.%02lld MHz\n",
147cbab9cadSchs device_xname(self),
14829803910Sjoff fclk / 1000000, (fclk % 1000000 + 5000) / 10000,
14929803910Sjoff hclk / 1000000, (hclk % 1000000 + 5000) / 10000,
15029803910Sjoff pclk / 1000000, (pclk % 1000000 + 5000) / 10000);
15129803910Sjoff
15229803910Sjoff sc->sc_fclk = fclk;
15329803910Sjoff sc->sc_hclk = hclk;
15429803910Sjoff sc->sc_pclk = pclk;
15529803910Sjoff
15629803910Sjoff /* get busdma tag for the platform */
15729803910Sjoff sc->sc_dmat = ep93xx_bus_dma_init(&ep93xx_bus_dma);
15829803910Sjoff
15929803910Sjoff /*
16029803910Sjoff * Attach each devices
1617959c31aShamajima * some device is used by other system device. so attach first.
16229803910Sjoff */
1637959c31aShamajima sa.sa_iot = sc->sc_iot;
1647959c31aShamajima sa.sa_dmat = sc->sc_dmat;
1657959c31aShamajima sa.sa_hclk = sc->sc_hclk;
1667959c31aShamajima sa.sa_pclk = sc->sc_pclk;
1677959c31aShamajima locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_TIMERS;
1682685996bSthorpej config_found(self, &sa, epsoc_print,
169c7fb772bSthorpej CFARGS(.submatch = epsoc_submatch,
170c7fb772bSthorpej .locators = locs));
1712685996bSthorpej
1727959c31aShamajima locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_GPIO;
173cbab9cadSchs sa.sa_gpio = device_private(
1742685996bSthorpej config_found(self, &sa, epsoc_print,
175c7fb772bSthorpej CFARGS(.submatch = epsoc_submatch,
176c7fb772bSthorpej .locators = locs)));
17729803910Sjoff
1782685996bSthorpej config_search(self, &sa,
179c7fb772bSthorpej CFARGS(.search = epsoc_search));
18029803910Sjoff }
18129803910Sjoff
18229803910Sjoff int
epsoc_submatch(device_t parent,cfdata_t cf,const int * ldesc,void * aux)183cbab9cadSchs epsoc_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
1847959c31aShamajima {
1857959c31aShamajima struct epsoc_attach_args *sa = aux;
1867959c31aShamajima
1877959c31aShamajima if (cf->cf_loc[EPSOCCF_ADDR] == ldesc[EPSOCCF_ADDR]) {
1887959c31aShamajima sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
1897959c31aShamajima sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
1907959c31aShamajima sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
1917959c31aShamajima return (config_match(parent, cf, aux));
1927959c31aShamajima } else
1937959c31aShamajima return (0);
1947959c31aShamajima }
1957959c31aShamajima
1967959c31aShamajima int
epsoc_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)197cbab9cadSchs epsoc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
19829803910Sjoff {
1997959c31aShamajima struct epsoc_attach_args *sa = aux;
20029803910Sjoff
2017959c31aShamajima sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
2027959c31aShamajima sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
2037959c31aShamajima sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
20429803910Sjoff
2052685996bSthorpej if (config_probe(parent, cf, aux))
206c7fb772bSthorpej config_attach(parent, cf, aux, epsoc_print, CFARGS_NONE);
20729803910Sjoff
20829803910Sjoff return (0);
20929803910Sjoff }
21029803910Sjoff
21129803910Sjoff static int
epsoc_print(void * aux,const char * name)212454af1c0Sdsl epsoc_print(void *aux, const char *name)
21329803910Sjoff {
214cbab9cadSchs struct epsoc_attach_args *sa = aux;
21529803910Sjoff
21629803910Sjoff if (sa->sa_size)
21729803910Sjoff aprint_normal(" addr 0x%lx", sa->sa_addr);
21829803910Sjoff if (sa->sa_size > 1)
21929803910Sjoff aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
22029803910Sjoff if (sa->sa_intr > 1)
22129803910Sjoff aprint_normal(" intr %d", sa->sa_intr);
22229803910Sjoff
22329803910Sjoff return (UNCONF);
22429803910Sjoff }
225