at91_ohci.c revision 190174
141502Swpaul/*-
241502Swpaul * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
341502Swpaul *
441502Swpaul * Redistribution and use in source and binary forms, with or without
541502Swpaul * modification, are permitted provided that the following conditions
641502Swpaul * are met:
741502Swpaul * 1. Redistributions of source code must retain the above copyright
841502Swpaul *    notice, this list of conditions and the following disclaimer.
941502Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1041502Swpaul *    notice, this list of conditions and the following disclaimer in the
1141502Swpaul *    documentation and/or other materials provided with the distribution.
1241502Swpaul *
1341502Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1441502Swpaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1541502Swpaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1641502Swpaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1741502Swpaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1841502Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1941502Swpaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2041502Swpaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2141502Swpaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2241502Swpaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2341502Swpaul */
2441502Swpaul
2541502Swpaul#include <sys/cdefs.h>
2641502Swpaul__FBSDID("$FreeBSD: head/sys/dev/usb/controller/ohci_atmelarm.c 190174 2009-03-20 19:04:31Z thompsa $");
2741502Swpaul
2841502Swpaul#include <dev/usb/usb_mfunc.h>
2941502Swpaul#include <dev/usb/usb.h>
3041502Swpaul
3141502Swpaul#include <dev/usb/usb_core.h>
3250477Speter#include <dev/usb/usb_busdma.h>
3341502Swpaul#include <dev/usb/usb_process.h>
3441502Swpaul#include <dev/usb/usb_sw_transfer.h>
3541502Swpaul#include <dev/usb/usb_util.h>
3641502Swpaul
3741502Swpaul#include <dev/usb/usb_controller.h>
3841502Swpaul#include <dev/usb/usb_bus.h>
3941502Swpaul#include <dev/usb/controller/ohci.h>
4041502Swpaul
4141502Swpaul#include <sys/rman.h>
4241502Swpaul
4341502Swpaul#include <arm/at91/at91_pmcvar.h>
4441502Swpaul
4541502Swpaul#define	MEM_RID	0
4641502Swpaul
4741502Swpaulstatic device_probe_t ohci_atmelarm_probe;
4841502Swpaulstatic device_attach_t ohci_atmelarm_attach;
4941502Swpaulstatic device_detach_t ohci_atmelarm_detach;
5041502Swpaul
5141502Swpaulstruct at91_ohci_softc {
5241502Swpaul	struct ohci_softc sc_ohci;	/* must be first */
5341502Swpaul	struct at91_pmc_clock *iclk;
5441502Swpaul	struct at91_pmc_clock *fclk;
5541502Swpaul};
5641502Swpaul
5741502Swpaulstatic int
5841502Swpaulohci_atmelarm_probe(device_t dev)
5941502Swpaul{
6041502Swpaul	device_set_desc(dev, "AT91 integrated OHCI controller");
6141502Swpaul	return (BUS_PROBE_DEFAULT);
6241502Swpaul}
6341502Swpaul
6441502Swpaulstatic int
6541502Swpaulohci_atmelarm_attach(device_t dev)
6641502Swpaul{
6741502Swpaul	struct at91_ohci_softc *sc = device_get_softc(dev);
6841502Swpaul	int err;
6941502Swpaul	int rid;
7041502Swpaul
7141502Swpaul	/* initialise some bus fields */
7241502Swpaul	sc->sc_ohci.sc_bus.parent = dev;
7341502Swpaul	sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
7441502Swpaul	sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
7541502Swpaul
7641502Swpaul	/* get all DMA memory */
7741502Swpaul	if (usb2_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
7841502Swpaul	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
7941502Swpaul		return (ENOMEM);
8041502Swpaul	}
8141502Swpaul	sc->iclk = at91_pmc_clock_ref("ohci_clk");
8241502Swpaul	sc->fclk = at91_pmc_clock_ref("uhpck");
8349610Swpaul
8449610Swpaul	sc->sc_ohci.sc_dev = dev;
8549610Swpaul
8641502Swpaul	rid = MEM_RID;
8751432Swpaul	sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
8851432Swpaul	    &rid, RF_ACTIVE);
8951432Swpaul
9041502Swpaul	if (!(sc->sc_ohci.sc_io_res)) {
9141502Swpaul		err = ENOMEM;
9241502Swpaul		goto error;
9341502Swpaul	}
9441502Swpaul	sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
9541502Swpaul	sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
9641502Swpaul	sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
9759758Speter
9859758Speter	rid = 0;
9951432Swpaul	sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
10051432Swpaul	    RF_ACTIVE);
10151432Swpaul	if (!(sc->sc_ohci.sc_irq_res)) {
10241502Swpaul		goto error;
10341591Sarchie	}
10450477Speter	sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
10541502Swpaul	if (!(sc->sc_ohci.sc_bus.bdev)) {
10641502Swpaul		goto error;
10741502Swpaul	}
10841502Swpaul	device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
10941502Swpaul
11041502Swpaul	strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
11141502Swpaul
11241502Swpaul#if (__FreeBSD_version >= 700031)
11341502Swpaul	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
11441502Swpaul	    NULL, (void *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
11562653Swpaul#else
11662653Swpaul	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
11744238Swpaul	    (void *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
11844238Swpaul#endif
11944238Swpaul	if (err) {
12044238Swpaul		sc->sc_ohci.sc_intr_hdl = NULL;
12141502Swpaul		goto error;
12241502Swpaul	}
12341502Swpaul	/*
12449610Swpaul	 * turn on the clocks from the AT91's point of view.  Keep the unit in reset.
12549610Swpaul	 */
12649610Swpaul	at91_pmc_clock_enable(sc->iclk);
12741502Swpaul	at91_pmc_clock_enable(sc->fclk);
12841502Swpaul	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
12949610Swpaul	    OHCI_CONTROL, 0);
13049610Swpaul
13141502Swpaul	err = ohci_init(&sc->sc_ohci);
13241502Swpaul	if (!err) {
13341502Swpaul		err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
13441502Swpaul	}
13541502Swpaul	if (err) {
13641502Swpaul		goto error;
13741502Swpaul	}
13851432Swpaul	return (0);
13941502Swpaul
14041502Swpaulerror:
14141502Swpaul	ohci_atmelarm_detach(dev);
14241502Swpaul	return (ENXIO);
14341502Swpaul}
14441502Swpaul
14549610Swpaulstatic int
14641502Swpaulohci_atmelarm_detach(device_t dev)
14741502Swpaul{
14841502Swpaul	struct at91_ohci_softc *sc = device_get_softc(dev);
14941502Swpaul	device_t bdev;
15041502Swpaul	int err;
15141502Swpaul
15241502Swpaul	if (sc->sc_ohci.sc_bus.bdev) {
15351432Swpaul		bdev = sc->sc_ohci.sc_bus.bdev;
15451432Swpaul		device_detach(bdev);
15551432Swpaul		device_delete_child(dev, bdev);
15641502Swpaul	}
15751432Swpaul	/* during module unload there are lots of children leftover */
15841502Swpaul	device_delete_all_children(dev);
15941502Swpaul
16041502Swpaul	/*
16141502Swpaul	 * Put the controller into reset, then disable clocks and do
16241502Swpaul	 * the MI tear down.  We have to disable the clocks/hardware
16341502Swpaul	 * after we do the rest of the teardown.  We also disable the
16449610Swpaul	 * clocks in the opposite order we acquire them, but that
16549610Swpaul	 * doesn't seem to be absolutely necessary.  We free up the
16649610Swpaul	 * clocks after we disable them, so the system could, in
16749610Swpaul	 * theory, reuse them.
16849610Swpaul	 */
16949610Swpaul	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
17049610Swpaul	    OHCI_CONTROL, 0);
17149610Swpaul
17249610Swpaul	at91_pmc_clock_disable(sc->fclk);
17349610Swpaul	at91_pmc_clock_disable(sc->iclk);
17449610Swpaul	at91_pmc_clock_deref(sc->fclk);
17549610Swpaul	at91_pmc_clock_deref(sc->iclk);
17649610Swpaul
17749610Swpaul	if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
17851432Swpaul		/*
17951432Swpaul		 * only call ohci_detach() after ohci_init()
18051432Swpaul		 */
18151432Swpaul		ohci_detach(&sc->sc_ohci);
18251432Swpaul
18351432Swpaul		err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
18451432Swpaul		sc->sc_ohci.sc_intr_hdl = NULL;
18551432Swpaul	}
18651432Swpaul	if (sc->sc_ohci.sc_irq_res) {
18751432Swpaul		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
18849610Swpaul		sc->sc_ohci.sc_irq_res = NULL;
18949610Swpaul	}
19049610Swpaul	if (sc->sc_ohci.sc_io_res) {
19149610Swpaul		bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
19251455Swpaul		    sc->sc_ohci.sc_io_res);
19349610Swpaul		sc->sc_ohci.sc_io_res = NULL;
19449610Swpaul	}
19549610Swpaul	usb2_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
19649610Swpaul
19749610Swpaul	return (0);
19849610Swpaul}
19951533Swpaul
20051473Swpaulstatic device_method_t ohci_methods[] = {
20149610Swpaul	/* Device interface */
20241502Swpaul	DEVMETHOD(device_probe, ohci_atmelarm_probe),
20341502Swpaul	DEVMETHOD(device_attach, ohci_atmelarm_attach),
20441502Swpaul	DEVMETHOD(device_detach, ohci_atmelarm_detach),
20541502Swpaul	DEVMETHOD(device_shutdown, bus_generic_shutdown),
20641502Swpaul
20741502Swpaul	/* Bus interface */
20841502Swpaul	DEVMETHOD(bus_print_child, bus_generic_print_child),
20941502Swpaul
21041502Swpaul	{0, 0}
21141502Swpaul};
21241502Swpaul
21341502Swpaulstatic driver_t ohci_driver = {
21441502Swpaul	"ohci",
21541502Swpaul	ohci_methods,
21641502Swpaul	sizeof(struct at91_ohci_softc),
21741502Swpaul};
21841502Swpaul
21941502Swpaulstatic devclass_t ohci_devclass;
22041502Swpaul
22141502SwpaulDRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);
22241502SwpaulMODULE_DEPEND(ohci, usb, 1, 1, 1);
22341502Swpaul