1239675Srwatson/*-
2239675Srwatson * Copyright (c) 2012 Robert N. M. Watson
3239675Srwatson * All rights reserved.
4239675Srwatson *
5239675Srwatson * This software was developed by SRI International and the University of
6239675Srwatson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7239675Srwatson * ("CTSRD"), as part of the DARPA CRASH research programme.
8239675Srwatson *
9239675Srwatson * Redistribution and use in source and binary forms, with or without
10239675Srwatson * modification, are permitted provided that the following conditions
11239675Srwatson * are met:
12239675Srwatson * 1. Redistributions of source code must retain the above copyright
13239675Srwatson *    notice, this list of conditions and the following disclaimer.
14239675Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15239675Srwatson *    notice, this list of conditions and the following disclaimer in the
16239675Srwatson *    documentation and/or other materials provided with the distribution.
17239675Srwatson *
18239675Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19239675Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20239675Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21239675Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22239675Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23239675Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24239675Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25239675Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26239675Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27239675Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28239675Srwatson * SUCH DAMAGE.
29239675Srwatson */
30239675Srwatson
31239675Srwatson#include <sys/cdefs.h>
32239675Srwatson__FBSDID("$FreeBSD$");
33239675Srwatson
34239675Srwatson#include <sys/param.h>
35239675Srwatson#include <sys/bus.h>
36239675Srwatson#include <sys/condvar.h>
37239675Srwatson#include <sys/conf.h>
38239675Srwatson#include <sys/bio.h>
39239675Srwatson#include <sys/kernel.h>
40239675Srwatson#include <sys/lock.h>
41239675Srwatson#include <sys/malloc.h>
42239675Srwatson#include <sys/module.h>
43239675Srwatson#include <sys/mutex.h>
44239675Srwatson#include <sys/rman.h>
45239675Srwatson#include <sys/systm.h>
46239675Srwatson#include <sys/taskqueue.h>
47239675Srwatson
48239675Srwatson#include <machine/bus.h>
49239675Srwatson#include <machine/resource.h>
50239675Srwatson
51239675Srwatson#include <geom/geom_disk.h>
52239675Srwatson
53239675Srwatson#include <dev/altera/sdcard/altera_sdcard.h>
54239675Srwatson
55245369Srwatson#include <dev/fdt/fdt_common.h>
56245369Srwatson#include <dev/ofw/openfirm.h>
57245369Srwatson#include <dev/ofw/ofw_bus.h>
58245369Srwatson#include <dev/ofw/ofw_bus_subr.h>
59245369Srwatson
60239675Srwatson/*
61245369Srwatson * FDT bus attachment for the Altera SD Card IP core.
62239675Srwatson */
63239675Srwatsonstatic int
64245369Srwatsonaltera_sdcard_fdt_probe(device_t dev)
65239675Srwatson{
66239675Srwatson
67261410Sian	if (!ofw_bus_status_okay(dev))
68261410Sian		return (ENXIO);
69261410Sian
70245369Srwatson	if (ofw_bus_is_compatible(dev, "altera,sdcard_11_2011")) {
71245369Srwatson		device_set_desc(dev, "Altera Secure Data Card IP Core");
72245369Srwatson		return (BUS_PROBE_DEFAULT);
73245369Srwatson	}
74245369Srwatson	return (ENXIO);
75239675Srwatson}
76239675Srwatson
77239675Srwatsonstatic int
78245369Srwatsonaltera_sdcard_fdt_attach(device_t dev)
79239675Srwatson{
80239675Srwatson	struct altera_sdcard_softc *sc;
81239675Srwatson
82239675Srwatson	sc = device_get_softc(dev);
83239675Srwatson	sc->as_dev = dev;
84239675Srwatson	sc->as_unit = device_get_unit(dev);
85239675Srwatson	sc->as_rid = 0;
86239675Srwatson	sc->as_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
87239675Srwatson	    &sc->as_rid, RF_ACTIVE);
88239675Srwatson	if (sc->as_res == NULL) {
89239675Srwatson		device_printf(dev, "couldn't map memory\n");
90239675Srwatson		return (ENXIO);
91239675Srwatson	}
92239675Srwatson	altera_sdcard_attach(sc);
93239675Srwatson	return (0);
94239675Srwatson}
95239675Srwatson
96239675Srwatsonstatic int
97245369Srwatsonaltera_sdcard_fdt_detach(device_t dev)
98239675Srwatson{
99239675Srwatson	struct altera_sdcard_softc *sc;
100239675Srwatson
101239675Srwatson	sc = device_get_softc(dev);
102239675Srwatson	KASSERT(sc->as_res != NULL, ("%s: resources not allocated",
103239675Srwatson	    __func__));
104239675Srwatson	altera_sdcard_detach(sc);
105239675Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, sc->as_rid, sc->as_res);
106239675Srwatson	return (0);
107239675Srwatson}
108239675Srwatson
109245369Srwatsonstatic device_method_t altera_sdcard_fdt_methods[] = {
110245369Srwatson	DEVMETHOD(device_probe,		altera_sdcard_fdt_probe),
111245369Srwatson	DEVMETHOD(device_attach,	altera_sdcard_fdt_attach),
112245369Srwatson	DEVMETHOD(device_detach,	altera_sdcard_fdt_detach),
113239675Srwatson	{ 0, 0 }
114239675Srwatson};
115239675Srwatson
116245369Srwatsonstatic driver_t altera_sdcard_fdt_driver = {
117239675Srwatson	"altera_sdcardc",
118245369Srwatson	altera_sdcard_fdt_methods,
119239675Srwatson	sizeof(struct altera_sdcard_softc),
120239675Srwatson};
121239675Srwatson
122245369SrwatsonDRIVER_MODULE(altera_sdcard, simplebus, altera_sdcard_fdt_driver,
123239675Srwatson    altera_sdcard_devclass, 0, 0);
124