Add support for enabling the USB on the Raspberry Pi boards when it hasn't

been done by U-Boot. This allows the USB to work when we load the kernel
directly.

No dma sync is performed after these operations as the data we read/write
is not used by the cpu after the calls to the maimbox driver.

Differential Revision:	https://reviews.freebsd.org/D1940
Reviewed by:	imp, Michal Meloun (meloun AT miracle.cz)
MFC after:	1 Week
Sponsored by:	ABT Systems Ltd
This commit is contained in:
andrew 2015-03-08 13:52:07 +00:00
parent cb8a3dcc1e
commit a542258e7d
4 changed files with 224 additions and 11 deletions

View File

@ -0,0 +1,176 @@
/*
* Copyright 2015 Andrew Turner.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/callout.h>
#include <sys/condvar.h>
#include <sys/module.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usb_busdma.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/usb_controller.h>
#include <dev/usb/usb_bus.h>
#include <dev/usb/controller/dwc_otg.h>
#include <dev/usb/controller/dwc_otg_fdt.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <machine/pmap.h>
#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
#include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
#include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
#include "mbox_if.h"
static device_probe_t bcm283x_dwc_otg_probe;
static device_attach_t bcm283x_dwc_otg_attach;
static int
bcm283x_dwc_otg_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-usb"))
return (ENXIO);
device_set_desc(dev, "DWC OTG 2.0 integrated USB controller (bcm283x)");
return (BUS_PROBE_VENDOR);
}
static void
bcm283x_dwc_otg_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
{
bus_addr_t *addr;
if (err)
return;
addr = (bus_addr_t *)arg;
*addr = PHYS_TO_VCBUS(segs[0].ds_addr);
}
static int
bcm283x_dwc_otg_attach(device_t dev)
{
struct msg_set_power_state *msg;
bus_dma_tag_t msg_tag;
bus_dmamap_t msg_map;
bus_addr_t msg_phys;
void *msg_buf;
uint32_t reg;
device_t mbox;
int err;
/* get mbox device */
mbox = devclass_get_device(devclass_find("mbox"), 0);
if (mbox == NULL) {
device_printf(dev, "can't find mbox\n");
return (ENXIO);
}
err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0,
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
sizeof(struct msg_set_power_state), 1,
sizeof(struct msg_set_power_state), 0,
NULL, NULL, &msg_tag);
if (err != 0) {
device_printf(dev, "can't create DMA tag\n");
return (ENXIO);
}
err = bus_dmamem_alloc(msg_tag, (void **)&msg_buf, 0, &msg_map);
if (err != 0) {
bus_dma_tag_destroy(msg_tag);
device_printf(dev, "can't allocate dmamem\n");
return (ENXIO);
}
err = bus_dmamap_load(msg_tag, msg_map, msg_buf,
sizeof(struct msg_set_power_state), bcm283x_dwc_otg_cb,
&msg_phys, 0);
if (err != 0) {
bus_dmamem_free(msg_tag, msg_buf, msg_map);
bus_dma_tag_destroy(msg_tag);
device_printf(dev, "can't load DMA map\n");
return (ENXIO);
}
msg = msg_buf;
memset(msg, 0, sizeof(*msg));
msg->hdr.buf_size = sizeof(*msg);
msg->hdr.code = BCM2835_MBOX_CODE_REQ;
msg->tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE;
msg->tag_hdr.val_buf_size = sizeof(msg->body);
msg->tag_hdr.val_len = sizeof(msg->body.req);
msg->body.req.device_id = BCM2835_MBOX_POWER_ID_USB_HCD;
msg->body.req.state = BCM2835_MBOX_POWER_ON | BCM2835_MBOX_POWER_WAIT;
msg->end_tag = 0;
bus_dmamap_sync(msg_tag, msg_map,
BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys);
MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, &reg);
bus_dmamap_unload(msg_tag, msg_map);
bus_dmamem_free(msg_tag, msg_buf, msg_map);
bus_dma_tag_destroy(msg_tag);
return (dwc_otg_attach(dev));
}
static device_method_t bcm283x_dwc_otg_methods[] = {
/* bus interface */
DEVMETHOD(device_probe, bcm283x_dwc_otg_probe),
DEVMETHOD(device_attach, bcm283x_dwc_otg_attach),
DEVMETHOD_END
};
static devclass_t bcm283x_dwc_otg_devclass;
DEFINE_CLASS_1(bcm283x_dwcotg, bcm283x_dwc_otg_driver, bcm283x_dwc_otg_methods,
sizeof(struct dwc_otg_fdt_softc), dwc_otg_driver);
DRIVER_MODULE(bcm283x_dwcotg, simplebus, bcm283x_dwc_otg_driver,
bcm283x_dwc_otg_devclass, 0, 0);
MODULE_DEPEND(bcm283x_dwcotg, usb, 1, 1, 1);

View File

@ -15,6 +15,8 @@ arm/broadcom/bcm2835/bcm2835_spi.c optional bcm2835_spi
arm/broadcom/bcm2835/bcm2835_systimer.c standard
arm/broadcom/bcm2835/bcm2835_wdog.c standard
arm/broadcom/bcm2835/bcm283x_dwc_fdt.c optional dwcotg fdt
arm/arm/bus_space_base.c standard
arm/arm/bus_space_generic.c standard
arm/arm/bus_space_asm_generic.S standard

View File

@ -53,15 +53,11 @@ __FBSDID("$FreeBSD$");
#include <dev/usb/usb_bus.h>
#include <dev/usb/controller/dwc_otg.h>
#include <dev/usb/controller/dwc_otg_fdt.h>
static device_probe_t dwc_otg_probe;
static device_attach_t dwc_otg_attach;
static device_detach_t dwc_otg_detach;
struct dwc_otg_super_softc {
struct dwc_otg_softc sc_otg; /* must be first */
};
static int
dwc_otg_probe(device_t dev)
{
@ -74,13 +70,13 @@ dwc_otg_probe(device_t dev)
device_set_desc(dev, "DWC OTG 2.0 integrated USB controller");
return (0);
return (BUS_PROBE_DEFAULT);
}
static int
int
dwc_otg_attach(device_t dev)
{
struct dwc_otg_super_softc *sc = device_get_softc(dev);
struct dwc_otg_fdt_softc *sc = device_get_softc(dev);
char usb_mode[24];
int err;
int rid;
@ -161,7 +157,7 @@ error:
static int
dwc_otg_detach(device_t dev)
{
struct dwc_otg_super_softc *sc = device_get_softc(dev);
struct dwc_otg_fdt_softc *sc = device_get_softc(dev);
device_t bdev;
int err;
@ -212,10 +208,10 @@ static device_method_t dwc_otg_methods[] = {
DEVMETHOD_END
};
static driver_t dwc_otg_driver = {
driver_t dwc_otg_driver = {
.name = "dwcotg",
.methods = dwc_otg_methods,
.size = sizeof(struct dwc_otg_super_softc),
.size = sizeof(struct dwc_otg_fdt_softc),
};
static devclass_t dwc_otg_devclass;

View File

@ -0,0 +1,39 @@
/*-
* Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DWC_OTG_FDT_H_
#define _DWC_OTG_FDT_H_
struct dwc_otg_fdt_softc {
struct dwc_otg_softc sc_otg; /* must be first */
};
extern driver_t dwc_otg_driver;
device_attach_t dwc_otg_attach;
#endif