137c5dfcdSGanbold Tsagaankhuu /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro F. Giffuni *
4fe47fb7bSGanbold Tsagaankhuu * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@freebsd.org>
537c5dfcdSGanbold Tsagaankhuu * All rights reserved.
637c5dfcdSGanbold Tsagaankhuu *
737c5dfcdSGanbold Tsagaankhuu * Redistribution and use in source and binary forms, with or without
837c5dfcdSGanbold Tsagaankhuu * modification, are permitted provided that the following conditions
937c5dfcdSGanbold Tsagaankhuu * are met:
1037c5dfcdSGanbold Tsagaankhuu * 1. Redistributions of source code must retain the above copyright
1137c5dfcdSGanbold Tsagaankhuu * notice, this list of conditions and the following disclaimer.
1237c5dfcdSGanbold Tsagaankhuu * 2. Redistributions in binary form must reproduce the above copyright
1337c5dfcdSGanbold Tsagaankhuu * notice, this list of conditions and the following disclaimer in the
1437c5dfcdSGanbold Tsagaankhuu * documentation and/or other materials provided with the distribution.
1537c5dfcdSGanbold Tsagaankhuu *
1637c5dfcdSGanbold Tsagaankhuu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1737c5dfcdSGanbold Tsagaankhuu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1837c5dfcdSGanbold Tsagaankhuu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1937c5dfcdSGanbold Tsagaankhuu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2037c5dfcdSGanbold Tsagaankhuu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2137c5dfcdSGanbold Tsagaankhuu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2237c5dfcdSGanbold Tsagaankhuu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION
2337c5dfcdSGanbold Tsagaankhuu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2437c5dfcdSGanbold Tsagaankhuu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY
2537c5dfcdSGanbold Tsagaankhuu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2637c5dfcdSGanbold Tsagaankhuu * SUCH DAMAGE.
2737c5dfcdSGanbold Tsagaankhuu */
2837c5dfcdSGanbold Tsagaankhuu
2937c5dfcdSGanbold Tsagaankhuu #include <sys/param.h>
3037c5dfcdSGanbold Tsagaankhuu #include <sys/systm.h>
3137c5dfcdSGanbold Tsagaankhuu #include <sys/bus.h>
3237c5dfcdSGanbold Tsagaankhuu #include <sys/kernel.h>
3337c5dfcdSGanbold Tsagaankhuu #include <sys/module.h>
3437c5dfcdSGanbold Tsagaankhuu #include <sys/malloc.h>
3537c5dfcdSGanbold Tsagaankhuu #include <sys/rman.h>
3637c5dfcdSGanbold Tsagaankhuu #include <sys/timeet.h>
3737c5dfcdSGanbold Tsagaankhuu #include <sys/timetc.h>
3837c5dfcdSGanbold Tsagaankhuu #include <sys/watchdog.h>
3937c5dfcdSGanbold Tsagaankhuu #include <machine/bus.h>
4037c5dfcdSGanbold Tsagaankhuu #include <machine/cpu.h>
4137c5dfcdSGanbold Tsagaankhuu #include <machine/frame.h>
4237c5dfcdSGanbold Tsagaankhuu #include <machine/intr.h>
4337c5dfcdSGanbold Tsagaankhuu
4437c5dfcdSGanbold Tsagaankhuu #include <dev/ofw/openfirm.h>
4537c5dfcdSGanbold Tsagaankhuu #include <dev/ofw/ofw_bus.h>
4637c5dfcdSGanbold Tsagaankhuu #include <dev/ofw/ofw_bus_subr.h>
4737c5dfcdSGanbold Tsagaankhuu
4837c5dfcdSGanbold Tsagaankhuu #include "a10_sramc.h"
4937c5dfcdSGanbold Tsagaankhuu
5037c5dfcdSGanbold Tsagaankhuu #define SRAM_CTL1_CFG 0x04
51ce459692SJared McNeill #define CTL1_CFG_SRAMD_MAP_USB0 (1 << 0)
5237c5dfcdSGanbold Tsagaankhuu
5337c5dfcdSGanbold Tsagaankhuu struct a10_sramc_softc {
5437c5dfcdSGanbold Tsagaankhuu struct resource *res;
5537c5dfcdSGanbold Tsagaankhuu bus_space_tag_t bst;
5637c5dfcdSGanbold Tsagaankhuu bus_space_handle_t bsh;
5737c5dfcdSGanbold Tsagaankhuu };
5837c5dfcdSGanbold Tsagaankhuu
5937c5dfcdSGanbold Tsagaankhuu static struct a10_sramc_softc *a10_sramc_sc;
6037c5dfcdSGanbold Tsagaankhuu
6137c5dfcdSGanbold Tsagaankhuu #define sramc_read_4(sc, reg) \
6237c5dfcdSGanbold Tsagaankhuu bus_space_read_4((sc)->bst, (sc)->bsh, (reg))
6337c5dfcdSGanbold Tsagaankhuu #define sramc_write_4(sc, reg, val) \
6437c5dfcdSGanbold Tsagaankhuu bus_space_write_4((sc)->bst, (sc)->bsh, (reg), (val))
6537c5dfcdSGanbold Tsagaankhuu
6637c5dfcdSGanbold Tsagaankhuu static int
a10_sramc_probe(device_t dev)6737c5dfcdSGanbold Tsagaankhuu a10_sramc_probe(device_t dev)
6837c5dfcdSGanbold Tsagaankhuu {
6937c5dfcdSGanbold Tsagaankhuu
7043b227c8SJared McNeill if (ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-sram-controller")) {
7137c5dfcdSGanbold Tsagaankhuu device_set_desc(dev, "Allwinner sramc module");
7237c5dfcdSGanbold Tsagaankhuu return (BUS_PROBE_DEFAULT);
7337c5dfcdSGanbold Tsagaankhuu }
7437c5dfcdSGanbold Tsagaankhuu
7537c5dfcdSGanbold Tsagaankhuu return (ENXIO);
7637c5dfcdSGanbold Tsagaankhuu }
7737c5dfcdSGanbold Tsagaankhuu
7837c5dfcdSGanbold Tsagaankhuu static int
a10_sramc_attach(device_t dev)7937c5dfcdSGanbold Tsagaankhuu a10_sramc_attach(device_t dev)
8037c5dfcdSGanbold Tsagaankhuu {
8137c5dfcdSGanbold Tsagaankhuu struct a10_sramc_softc *sc = device_get_softc(dev);
8237c5dfcdSGanbold Tsagaankhuu int rid = 0;
8337c5dfcdSGanbold Tsagaankhuu
8437c5dfcdSGanbold Tsagaankhuu sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
8537c5dfcdSGanbold Tsagaankhuu if (!sc->res) {
8637c5dfcdSGanbold Tsagaankhuu device_printf(dev, "could not allocate resource\n");
8737c5dfcdSGanbold Tsagaankhuu return (ENXIO);
8837c5dfcdSGanbold Tsagaankhuu }
8937c5dfcdSGanbold Tsagaankhuu
9037c5dfcdSGanbold Tsagaankhuu sc->bst = rman_get_bustag(sc->res);
9137c5dfcdSGanbold Tsagaankhuu sc->bsh = rman_get_bushandle(sc->res);
9237c5dfcdSGanbold Tsagaankhuu
9337c5dfcdSGanbold Tsagaankhuu a10_sramc_sc = sc;
9437c5dfcdSGanbold Tsagaankhuu
9537c5dfcdSGanbold Tsagaankhuu return (0);
9637c5dfcdSGanbold Tsagaankhuu }
9737c5dfcdSGanbold Tsagaankhuu
9837c5dfcdSGanbold Tsagaankhuu static device_method_t a10_sramc_methods[] = {
9937c5dfcdSGanbold Tsagaankhuu DEVMETHOD(device_probe, a10_sramc_probe),
10037c5dfcdSGanbold Tsagaankhuu DEVMETHOD(device_attach, a10_sramc_attach),
10137c5dfcdSGanbold Tsagaankhuu { 0, 0 }
10237c5dfcdSGanbold Tsagaankhuu };
10337c5dfcdSGanbold Tsagaankhuu
10437c5dfcdSGanbold Tsagaankhuu static driver_t a10_sramc_driver = {
10537c5dfcdSGanbold Tsagaankhuu "a10_sramc",
10637c5dfcdSGanbold Tsagaankhuu a10_sramc_methods,
10737c5dfcdSGanbold Tsagaankhuu sizeof(struct a10_sramc_softc),
10837c5dfcdSGanbold Tsagaankhuu };
10937c5dfcdSGanbold Tsagaankhuu
1107e1e2ba1SJohn Baldwin EARLY_DRIVER_MODULE(a10_sramc, simplebus, a10_sramc_driver, 0, 0,
1117e1e2ba1SJohn Baldwin BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_FIRST);
11237c5dfcdSGanbold Tsagaankhuu
11337c5dfcdSGanbold Tsagaankhuu int
a10_map_to_emac(void)11437c5dfcdSGanbold Tsagaankhuu a10_map_to_emac(void)
11537c5dfcdSGanbold Tsagaankhuu {
11637c5dfcdSGanbold Tsagaankhuu struct a10_sramc_softc *sc = a10_sramc_sc;
11737c5dfcdSGanbold Tsagaankhuu uint32_t reg_value;
11837c5dfcdSGanbold Tsagaankhuu
11937c5dfcdSGanbold Tsagaankhuu if (sc == NULL)
12037c5dfcdSGanbold Tsagaankhuu return (ENXIO);
12137c5dfcdSGanbold Tsagaankhuu
12237c5dfcdSGanbold Tsagaankhuu /* Map SRAM to EMAC, set bit 2 and 4. */
12337c5dfcdSGanbold Tsagaankhuu reg_value = sramc_read_4(sc, SRAM_CTL1_CFG);
12437c5dfcdSGanbold Tsagaankhuu reg_value |= 0x5 << 2;
12537c5dfcdSGanbold Tsagaankhuu sramc_write_4(sc, SRAM_CTL1_CFG, reg_value);
12637c5dfcdSGanbold Tsagaankhuu
12737c5dfcdSGanbold Tsagaankhuu return (0);
12837c5dfcdSGanbold Tsagaankhuu }
129ce459692SJared McNeill
130ce459692SJared McNeill int
a10_map_to_otg(void)131ce459692SJared McNeill a10_map_to_otg(void)
132ce459692SJared McNeill {
133ce459692SJared McNeill struct a10_sramc_softc *sc = a10_sramc_sc;
134ce459692SJared McNeill uint32_t reg_value;
135ce459692SJared McNeill
136ce459692SJared McNeill if (sc == NULL)
137ce459692SJared McNeill return (ENXIO);
138ce459692SJared McNeill
139ce459692SJared McNeill /* Map SRAM to OTG */
140ce459692SJared McNeill reg_value = sramc_read_4(sc, SRAM_CTL1_CFG);
141ce459692SJared McNeill reg_value |= CTL1_CFG_SRAMD_MAP_USB0;
142ce459692SJared McNeill sramc_write_4(sc, SRAM_CTL1_CFG, reg_value);
143ce459692SJared McNeill
144ce459692SJared McNeill return (0);
145ce459692SJared McNeill }
146