a10_sramc.c revision 263711
1110285Snyan/*- 2110285Snyan * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@freebsd.org> 3110329Stakawata * All rights reserved. 4110333Snyan * 5110285Snyan * Redistribution and use in source and binary forms, with or without 6110285Snyan * modification, are permitted provided that the following conditions 7110285Snyan * are met: 8110285Snyan * 1. Redistributions of source code must retain the above copyright 9110285Snyan * notice, this list of conditions and the following disclaimer. 10110285Snyan * 2. Redistributions in binary form must reproduce the above copyright 11110285Snyan * notice, this list of conditions and the following disclaimer in the 12110285Snyan * documentation and/or other materials provided with the distribution. 13110285Snyan * 14110285Snyan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15110285Snyan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16110285Snyan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17110285Snyan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18110285Snyan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19110285Snyan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20110285Snyan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION 21110285Snyan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22110285Snyan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY 23110285Snyan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24110285Snyan * SUCH DAMAGE. 25110333Snyan * 26110285Snyan * $FreeBSD: head/sys/arm/allwinner/a10_sramc.c 263711 2014-03-25 08:31:47Z ganbold $ 27110285Snyan */ 28110285Snyan 29110285Snyan#include <sys/cdefs.h> 30110285Snyan__FBSDID("$FreeBSD: head/sys/arm/allwinner/a10_sramc.c 263711 2014-03-25 08:31:47Z ganbold $"); 31110285Snyan 32110285Snyan#include <sys/param.h> 33110285Snyan#include <sys/systm.h> 34110285Snyan#include <sys/bus.h> 35110285Snyan#include <sys/kernel.h> 36110285Snyan#include <sys/module.h> 37110285Snyan#include <sys/malloc.h> 38110285Snyan#include <sys/rman.h> 39110285Snyan#include <sys/timeet.h> 40110285Snyan#include <sys/timetc.h> 41110285Snyan#include <sys/watchdog.h> 42110285Snyan#include <machine/bus.h> 43110285Snyan#include <machine/cpu.h> 44110285Snyan#include <machine/frame.h> 45110285Snyan#include <machine/intr.h> 46110285Snyan#include <machine/fdt.h> 47110285Snyan 48110285Snyan#include <dev/fdt/fdt_common.h> 49110285Snyan#include <dev/ofw/openfirm.h> 50110285Snyan#include <dev/ofw/ofw_bus.h> 51110285Snyan#include <dev/ofw/ofw_bus_subr.h> 52110285Snyan 53110285Snyan#include "a10_sramc.h" 54110285Snyan 55110285Snyan#define SRAM_CTL1_CFG 0x04 56110285Snyan 57110285Snyanstruct a10_sramc_softc { 58110285Snyan struct resource *res; 59110285Snyan bus_space_tag_t bst; 60110285Snyan bus_space_handle_t bsh; 61110285Snyan}; 62110285Snyan 63110285Snyanstatic struct a10_sramc_softc *a10_sramc_sc; 64110285Snyan 65249586Sgabor#define sramc_read_4(sc, reg) \ 66110285Snyan bus_space_read_4((sc)->bst, (sc)->bsh, (reg)) 67110285Snyan#define sramc_write_4(sc, reg, val) \ 68110285Snyan bus_space_write_4((sc)->bst, (sc)->bsh, (reg), (val)) 69110285Snyan 70110285Snyan 71249586Sgaborstatic int 72110285Snyana10_sramc_probe(device_t dev) 73110285Snyan{ 74110285Snyan 75110285Snyan if (ofw_bus_is_compatible(dev, "allwinner,sun4i-sramc")) { 76110285Snyan device_set_desc(dev, "Allwinner sramc module"); 77110285Snyan return (BUS_PROBE_DEFAULT); 78110285Snyan } 79110285Snyan 80110285Snyan return (ENXIO); 81110285Snyan} 82110285Snyan 83110285Snyanstatic int 84110285Snyana10_sramc_attach(device_t dev) 85212413Savg{ 86110285Snyan struct a10_sramc_softc *sc = device_get_softc(dev); 87294883Sjhibbits int rid = 0; 88110285Snyan 89110285Snyan sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 90110285Snyan if (!sc->res) { 91110285Snyan device_printf(dev, "could not allocate resource\n"); 92110285Snyan return (ENXIO); 93110285Snyan } 94110285Snyan 95294883Sjhibbits sc->bst = rman_get_bustag(sc->res); 96110285Snyan sc->bsh = rman_get_bushandle(sc->res); 97110285Snyan 98110285Snyan a10_sramc_sc = sc; 99110285Snyan 100110285Snyan return (0); 101110285Snyan} 102110285Snyan 103110285Snyanstatic device_method_t a10_sramc_methods[] = { 104110285Snyan DEVMETHOD(device_probe, a10_sramc_probe), 105110285Snyan DEVMETHOD(device_attach, a10_sramc_attach), 106110285Snyan { 0, 0 } 107110285Snyan}; 108110285Snyan 109110285Snyanstatic driver_t a10_sramc_driver = { 110110285Snyan "a10_sramc", 111110285Snyan a10_sramc_methods, 112110285Snyan sizeof(struct a10_sramc_softc), 113110285Snyan}; 114110285Snyan 115110285Snyanstatic devclass_t a10_sramc_devclass; 116110285Snyan 117110285SnyanDRIVER_MODULE(a10_sramc, simplebus, a10_sramc_driver, a10_sramc_devclass, 0, 0); 118110285Snyan 119110285Snyanint 120110285Snyana10_map_to_emac(void) 121110285Snyan{ 122110285Snyan struct a10_sramc_softc *sc = a10_sramc_sc; 123110285Snyan uint32_t reg_value; 124110285Snyan 125110285Snyan if (sc == NULL) 126110285Snyan return (ENXIO); 127110285Snyan 128110285Snyan /* Map SRAM to EMAC, set bit 2 and 4. */ 129110285Snyan reg_value = sramc_read_4(sc, SRAM_CTL1_CFG); 130110285Snyan reg_value |= 0x5 << 2; 131110285Snyan sramc_write_4(sc, SRAM_CTL1_CFG, reg_value); 132110285Snyan 133110285Snyan return (0); 134110285Snyan} 135110285Snyan