Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_idm.c revision 1.1.2.1
      1 /*-
      2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "opt_broadcom.h"
     31 #include "locators.h"
     32 
     33 #define CRU_PRIVATE
     34 #define IDM_PRIVATE
     35 
     36 #include <sys/cdefs.h>
     37 
     38 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_idm.c,v 1.1.2.1 2012/11/20 03:01:03 tls Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/bus.h>
     42 #include <sys/device.h>
     43 #include <sys/intr.h>
     44 #include <sys/systm.h>
     45 
     46 #include <arm/mainbus/mainbus.h>
     47 
     48 #include <arm/broadcom/bcm53xx_reg.h>
     49 #include <arm/broadcom/bcm53xx_var.h>
     50 
     51 struct idm_info {
     52 	bus_size_t idm_offset;
     53 	const char *idm_name;
     54 	int idm_port;
     55 	bool (*idm_unreset)(bus_space_tag_t, bus_space_handle_t,
     56 	    const struct idm_info *);
     57 };
     58 
     59 static bool
     60 bcmeth_unreset(bus_space_tag_t bst, bus_space_handle_t bsh,
     61     const struct idm_info *idm)
     62 {
     63 	/*
     64 	 * To enable any GMAC, we must enable all off them.
     65 	 */
     66 	static const bus_size_t regoff[] = {
     67 		IDM_BASE + IDM_AMAC0_BASE,
     68 		IDM_BASE + IDM_AMAC1_BASE,
     69 		IDM_BASE + IDM_AMAC2_BASE,
     70 		IDM_BASE + IDM_AMAC3_BASE,
     71 	};
     72 	static bool bcmeth_init_done;
     73 	if (!bcmeth_init_done) {
     74 		for (size_t idx = 0; idx < __arraycount(regoff); idx++) {
     75 			const bus_size_t off = regoff[idx];
     76 			bus_space_write_4(bst, bsh, off + IDM_RESET_CONTROL, 0);
     77 			uint32_t v = bus_space_read_4(bst, bsh,
     78 			    off + IDM_IO_CONTROL_DIRECT);
     79 			/*
     80 			 * Clear read-allocate and write-allocate bits from
     81 			 * ACP cache access so we don't pollute the caches.
     82 			 */
     83 			v &= ~IO_CONTROL_DIRECT_ARCACHE;
     84 			v &= ~IO_CONTROL_DIRECT_AWCACHE;
     85 			v |= __SHIFTIN(AXCACHE_C|AXCACHE_B, IO_CONTROL_DIRECT_ARCACHE);
     86 			v |= __SHIFTIN(AXCACHE_C|AXCACHE_B, IO_CONTROL_DIRECT_AWCACHE);
     87 			bus_space_write_4(bst, bsh, off + IDM_IO_CONTROL_DIRECT,
     88 			    v);
     89 		}
     90 		bcmeth_init_done = true;
     91 	}
     92 	return true;
     93 }
     94 
     95 static bool
     96 bcmccb_idm_unreset(bus_space_tag_t bst, bus_space_handle_t bsh,
     97     const struct idm_info *idm)
     98 {
     99 	if (idm->idm_offset == 0)
    100 		return true;
    101 
    102 	/*
    103 	 * If the device might be in reset, let's try to take it out of it.
    104 	 */
    105 	bus_size_t o = IDM_BASE + idm->idm_offset + IDM_RESET_CONTROL;
    106 	uint32_t v = bus_space_read_4(bst, bsh, o);
    107 	if (v & 1) {
    108 		v &= ~1;
    109 		bus_space_write_4(bst, bsh, o, v);
    110 	}
    111 	return true;
    112 }
    113 
    114 static bool
    115 bcmpax2_idm_unreset(bus_space_tag_t bst, bus_space_handle_t bsh,
    116     const struct idm_info *idm)
    117 {
    118 	uint32_t v = bus_space_read_4(bst, bsh, CRU_BASE + CRU_STRAPS_CONTROL);
    119 
    120 	if (v & STRAP_USB3_SEL)
    121 		return false;
    122 
    123 	return bcmccb_idm_unreset(bst, bsh, idm);
    124 }
    125 
    126 static bool
    127 bcmxhci_idm_unreset(bus_space_tag_t bst, bus_space_handle_t bsh,
    128     const struct idm_info *idm)
    129 {
    130 	uint32_t v = bus_space_read_4(bst, bsh, CRU_BASE + CRU_STRAPS_CONTROL);
    131 
    132 	if ((v & STRAP_USB3_SEL) == 0)
    133 		return false;
    134 
    135 	return bcmccb_idm_unreset(bst, bsh, idm);
    136 }
    137 
    138 static const struct idm_info bcm53xx_idm_info[] = {
    139 	{ 0, "bcmi2c", BCMCCBCF_PORT_DEFAULT, bcmccb_idm_unreset },
    140 	{ 0, "bcmmdio", BCMCCBCF_PORT_DEFAULT, bcmccb_idm_unreset },
    141 	{ 0, "bcmrng", BCMCCBCF_PORT_DEFAULT, bcmccb_idm_unreset },
    142 	{ IDM_PCIE_M0_BASE, "bcmpax", 0, bcmccb_idm_unreset },
    143 	{ IDM_PCIE_M1_BASE, "bcmpax", 1, bcmccb_idm_unreset },
    144 	{ IDM_PCIE_M2_BASE, "bcmpax", 2, bcmpax2_idm_unreset },
    145 	{ IDM_AMAC0_BASE, "bcmeth", 0, bcmeth_unreset },
    146 	{ IDM_AMAC1_BASE, "bcmeth", 1, bcmeth_unreset },
    147 	{ IDM_AMAC2_BASE, "bcmeth", 2, bcmeth_unreset },
    148 	{ IDM_AMAC3_BASE, "bcmeth", 3, bcmeth_unreset },
    149 	{ IDM_USB3_BASE, "xhci", BCMCCBCF_PORT_DEFAULT, bcmxhci_idm_unreset },
    150 	{ IDM_SDIO_BASE, "sdhc", BCMCCBCF_PORT_DEFAULT, bcmccb_idm_unreset },
    151 	{ IDM_USB2_BASE, "bcmusb", BCMCCBCF_PORT_DEFAULT, bcmccb_idm_unreset },
    152 };
    153 
    154 static const struct idm_info *
    155 bcmccb_idm_lookup(const struct bcm_locators * const loc)
    156 {
    157 	const struct idm_info *idm = bcm53xx_idm_info;
    158 	for (size_t i = 0; i < __arraycount(bcm53xx_idm_info); i++, idm++) {
    159 		if (strcmp(idm->idm_name, loc->loc_name) == 0
    160 		    && idm->idm_port == loc->loc_port) {
    161 			return idm;
    162 		}
    163 	}
    164 	return NULL;
    165 }
    166 
    167 bool
    168 bcm53xx_idm_device_init(const struct bcm_locators *loc, bus_space_tag_t bst,
    169 	bus_space_handle_t bsh)
    170 {
    171 	const struct idm_info * const idm = bcmccb_idm_lookup(loc);
    172 	if (idm == NULL)
    173 		return false;
    174 
    175 	/*
    176 	 * If the device might be in reset, let's try to take it out of it.
    177 	 */
    178 	return (*idm->idm_unreset)(bst, bsh, idm);
    179 }
    180