1/* $FreeBSD: releng/11.0/sys/dev/usb/controller/saf1761_otg_boot.c 276717 2015-01-05 20:22:18Z hselasky $ */
2/*-
3 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
4 * All rights reserved.
5 *
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include USB_GLOBAL_INCLUDE_FILE
33
34#include <dev/usb/controller/saf1761_otg.h>
35#include <dev/usb/controller/saf1761_otg_reg.h>
36
37static device_probe_t saf1761_otg_fdt_probe;
38static device_attach_t saf1761_otg_fdt_attach;
39static device_detach_t saf1761_otg_fdt_detach;
40
41static device_method_t saf1761_otg_methods[] = {
42	/* Device interface */
43	DEVMETHOD(device_probe, saf1761_otg_fdt_probe),
44	DEVMETHOD(device_attach, saf1761_otg_fdt_attach),
45	DEVMETHOD(device_detach, saf1761_otg_fdt_detach),
46	DEVMETHOD(device_suspend, bus_generic_suspend),
47	DEVMETHOD(device_resume, bus_generic_resume),
48	DEVMETHOD(device_shutdown, bus_generic_shutdown),
49
50	DEVMETHOD_END
51};
52
53static driver_t saf1761_otg_driver = {
54	.name = "saf1761otg",
55	.methods = saf1761_otg_methods,
56	.size = sizeof(struct saf1761_otg_softc),
57};
58
59static devclass_t saf1761_otg_devclass;
60
61DRIVER_MODULE(saf1761otg, pci, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
62MODULE_DEPEND(saf1761otg, usb, 1, 1, 1);
63
64static int
65saf1761_otg_fdt_probe(device_t dev)
66{
67	if (device_get_unit(dev) != 0)
68		return (ENXIO);
69
70	device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller");
71
72	return (0);
73}
74
75static int
76saf1761_otg_fdt_attach(device_t dev)
77{
78	struct saf1761_otg_softc *sc = device_get_softc(dev);
79	int err;
80
81	/* 32-bit data bus */
82	sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
83
84	/* initialise some bus fields */
85	sc->sc_bus.parent = dev;
86	sc->sc_bus.devices = sc->sc_devices;
87	sc->sc_bus.devices_max = SOTG_MAX_DEVICES;
88	sc->sc_bus.dma_bits = 32;
89
90	/* get all DMA memory */
91	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev), NULL))
92		return (ENOMEM);
93
94	sc->sc_io_res = (void *)1;
95	sc->sc_io_tag = (void *)1;
96	sc->sc_io_hdl = (void *)USB_PCI_MEMORY_ADDRESS;
97	sc->sc_io_size = USB_PCI_MEMORY_SIZE;
98
99	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
100	if (sc->sc_bus.bdev == NULL)
101		goto error;
102
103	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
104	device_set_interrupt(dev, &saf1761_otg_filter_interrupt, &saf1761_otg_interrupt, sc);
105
106	err = saf1761_otg_init(sc);
107	if (err) {
108		device_printf(dev, "Init failed\n");
109		goto error;
110	}
111	err = device_probe_and_attach(sc->sc_bus.bdev);
112	if (err) {
113		device_printf(dev, "USB probe and attach failed\n");
114		goto error;
115	}
116	return (0);
117
118error:
119	saf1761_otg_fdt_detach(dev);
120	return (ENXIO);
121}
122
123static int
124saf1761_otg_fdt_detach(device_t dev)
125{
126	struct saf1761_otg_softc *sc = device_get_softc(dev);
127	device_t bdev;
128
129	if (sc->sc_bus.bdev) {
130		bdev = sc->sc_bus.bdev;
131		device_detach(bdev);
132		device_delete_child(dev, bdev);
133	}
134
135	/* during module unload there are lots of children leftover */
136	device_delete_children(dev);
137
138	if (sc->sc_irq_res) {
139		/*
140		 * Only call uninit() after init()
141		 */
142		saf1761_otg_uninit(sc);
143	}
144	usb_bus_mem_free_all(&sc->sc_bus, NULL);
145
146	return (0);
147}
148