Home | History | Annotate | Line # | Download | only in ingenic
apbus.c revision 1.11.2.2
      1  1.11.2.2  skrll /*	$NetBSD: apbus.c,v 1.11.2.2 2015/04/06 15:17:59 skrll Exp $ */
      2  1.11.2.2  skrll 
      3  1.11.2.2  skrll /*-
      4  1.11.2.2  skrll  * Copyright (c) 2014 Michael Lorenz
      5  1.11.2.2  skrll  * All rights reserved.
      6  1.11.2.2  skrll  *
      7  1.11.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.11.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.11.2.2  skrll  * are met:
     10  1.11.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.11.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.11.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.11.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.11.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     15  1.11.2.2  skrll  *
     16  1.11.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.11.2.2  skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.11.2.2  skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.11.2.2  skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.11.2.2  skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.11.2.2  skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.11.2.2  skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.11.2.2  skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.11.2.2  skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.11.2.2  skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.11.2.2  skrll  * POSSIBILITY OF SUCH DAMAGE.
     27  1.11.2.2  skrll  */
     28  1.11.2.2  skrll 
     29  1.11.2.2  skrll /* catch-all for on-chip peripherals */
     30  1.11.2.2  skrll 
     31  1.11.2.2  skrll #include <sys/cdefs.h>
     32  1.11.2.2  skrll __KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.11.2.2 2015/04/06 15:17:59 skrll Exp $");
     33  1.11.2.2  skrll 
     34  1.11.2.2  skrll #include "locators.h"
     35  1.11.2.2  skrll #define	_MIPS_BUS_DMA_PRIVATE
     36  1.11.2.2  skrll 
     37  1.11.2.2  skrll #include <sys/param.h>
     38  1.11.2.2  skrll #include <sys/bus.h>
     39  1.11.2.2  skrll #include <sys/device.h>
     40  1.11.2.2  skrll #include <sys/extent.h>
     41  1.11.2.2  skrll #include <sys/systm.h>
     42  1.11.2.2  skrll 
     43  1.11.2.2  skrll #include <mips/ingenic/ingenic_var.h>
     44  1.11.2.2  skrll #include <mips/ingenic/ingenic_regs.h>
     45  1.11.2.2  skrll 
     46  1.11.2.2  skrll #include "opt_ingenic.h"
     47  1.11.2.2  skrll 
     48  1.11.2.2  skrll static int apbus_match(device_t, cfdata_t, void *);
     49  1.11.2.2  skrll static void apbus_attach(device_t, device_t, void *);
     50  1.11.2.2  skrll static int apbus_print(void *, const char *);
     51  1.11.2.2  skrll static void apbus_bus_mem_init(bus_space_tag_t, void *);
     52  1.11.2.2  skrll 
     53  1.11.2.2  skrll CFATTACH_DECL_NEW(apbus, 0, apbus_match, apbus_attach, NULL, NULL);
     54  1.11.2.2  skrll 
     55  1.11.2.2  skrll static struct mips_bus_space	apbus_mbst;
     56  1.11.2.2  skrll bus_space_tag_t	apbus_memt = NULL;
     57  1.11.2.2  skrll 
     58  1.11.2.2  skrll struct mips_bus_dma_tag	apbus_dmat = {
     59  1.11.2.2  skrll 	._bounce_alloc_hi = 0x10000000,
     60  1.11.2.2  skrll 	._dmamap_ops = _BUS_DMAMAP_OPS_INITIALIZER,
     61  1.11.2.2  skrll 	._dmamem_ops = _BUS_DMAMEM_OPS_INITIALIZER,
     62  1.11.2.2  skrll 	._dmatag_ops = _BUS_DMATAG_OPS_INITIALIZER,
     63  1.11.2.2  skrll };
     64  1.11.2.2  skrll 
     65  1.11.2.2  skrll typedef struct apbus_dev {
     66  1.11.2.2  skrll 	const char *name;
     67  1.11.2.2  skrll 	bus_addr_t addr;
     68  1.11.2.2  skrll 	uint32_t irq;
     69  1.11.2.2  skrll } apbus_dev_t;
     70  1.11.2.2  skrll 
     71  1.11.2.2  skrll static const apbus_dev_t apbus_devs[] = {
     72  1.11.2.2  skrll 	{ "dwctwo",	JZ_DWC2_BASE,   21},
     73  1.11.2.2  skrll 	{ "ohci",	JZ_OHCI_BASE,    5 },
     74  1.11.2.2  skrll 	{ "ehci",	JZ_EHCI_BASE,   20},
     75  1.11.2.2  skrll 	{ "dme",	JZ_DME_BASE,    -1},	/* irq via gpio abuse */
     76  1.11.2.2  skrll 	{ "jzgpio",	JZ_GPIO_A_BASE, 17},
     77  1.11.2.2  skrll 	{ "jzgpio",	JZ_GPIO_B_BASE, 16},
     78  1.11.2.2  skrll 	{ "jzgpio",	JZ_GPIO_C_BASE, 15},
     79  1.11.2.2  skrll 	{ "jzgpio",	JZ_GPIO_D_BASE, 14},
     80  1.11.2.2  skrll 	{ "jzgpio",	JZ_GPIO_E_BASE, 13},
     81  1.11.2.2  skrll 	{ "jzgpio",	JZ_GPIO_F_BASE, 12},
     82  1.11.2.2  skrll 	{ "jziic",	JZ_SMB0_BASE,   60},
     83  1.11.2.2  skrll 	{ "jziic",	JZ_SMB1_BASE,   59},
     84  1.11.2.2  skrll 	{ "jziic",	JZ_SMB2_BASE,   58},
     85  1.11.2.2  skrll 	{ "jziic",	JZ_SMB3_BASE,   57},
     86  1.11.2.2  skrll 	{ "jziic",	JZ_SMB4_BASE,   56},
     87  1.11.2.2  skrll 	{ "jzfb",	-1,           -1},
     88  1.11.2.2  skrll 	{ NULL,		-1,           -1}
     89  1.11.2.2  skrll };
     90  1.11.2.2  skrll 
     91  1.11.2.2  skrll void
     92  1.11.2.2  skrll apbus_init(void)
     93  1.11.2.2  skrll {
     94  1.11.2.2  skrll 	static bool done = false;
     95  1.11.2.2  skrll 	if (done)
     96  1.11.2.2  skrll 		return;
     97  1.11.2.2  skrll 	done = true;
     98  1.11.2.2  skrll 
     99  1.11.2.2  skrll 	apbus_bus_mem_init(&apbus_mbst, NULL);
    100  1.11.2.2  skrll 	apbus_memt = &apbus_mbst;
    101  1.11.2.2  skrll }
    102  1.11.2.2  skrll 
    103  1.11.2.2  skrll int
    104  1.11.2.2  skrll apbus_match(device_t parent, cfdata_t match, void *aux)
    105  1.11.2.2  skrll {
    106  1.11.2.2  skrll 	struct mainbusdev {
    107  1.11.2.2  skrll 		const char *md_name;
    108  1.11.2.2  skrll 	} *aa = aux;
    109  1.11.2.2  skrll 	if (strcmp(aa->md_name, "apbus") == 0) return 1;
    110  1.11.2.2  skrll 	return 0;
    111  1.11.2.2  skrll }
    112  1.11.2.2  skrll 
    113  1.11.2.2  skrll void
    114  1.11.2.2  skrll apbus_attach(device_t parent, device_t self, void *aux)
    115  1.11.2.2  skrll {
    116  1.11.2.2  skrll 	uint32_t reg, mpll, m, n, p, mclk, pclk, pdiv;
    117  1.11.2.2  skrll 	aprint_normal("\n");
    118  1.11.2.2  skrll 
    119  1.11.2.2  skrll 	/* should have been called early on */
    120  1.11.2.2  skrll 	apbus_init();
    121  1.11.2.2  skrll 
    122  1.11.2.2  skrll #ifdef INGENIC_DEBUG
    123  1.11.2.2  skrll 	printf("core ctrl:   %08x\n", MFC0(12, 2));
    124  1.11.2.2  skrll 	printf("core status: %08x\n", MFC0(12, 3));
    125  1.11.2.2  skrll 	printf("REIM: %08x\n", MFC0(12, 4));
    126  1.11.2.2  skrll 	printf("ID: %08x\n", MFC0(15, 1));
    127  1.11.2.2  skrll #endif
    128  1.11.2.2  skrll 	/* assuming we're using MPLL */
    129  1.11.2.2  skrll 	mpll = readreg(JZ_CPMPCR);
    130  1.11.2.2  skrll 	m = (mpll & JZ_PLLM_M) >> JZ_PLLM_S;
    131  1.11.2.2  skrll 	n = (mpll & JZ_PLLN_M) >> JZ_PLLN_S;
    132  1.11.2.2  skrll 	p = (mpll & JZ_PLLP_M) >> JZ_PLLP_S;
    133  1.11.2.2  skrll 
    134  1.11.2.2  skrll 	/* assuming 48MHz EXTCLK */
    135  1.11.2.2  skrll 	mclk = (48000 * (m + 1) / (n + 1)) / (p + 1);
    136  1.11.2.2  skrll 
    137  1.11.2.2  skrll 	reg = readreg(JZ_CPCCR);
    138  1.11.2.2  skrll 	pdiv = (reg & JZ_PDIV_M) >> JZ_PDIV_S;
    139  1.11.2.2  skrll 	pclk = mclk / pdiv;
    140  1.11.2.2  skrll #ifdef INGENIC_DEBUG
    141  1.11.2.2  skrll 	printf("mclk %d kHz\n", mclk);
    142  1.11.2.2  skrll 	printf("pclk %d kHz\n", pclk);
    143  1.11.2.2  skrll #endif
    144  1.11.2.2  skrll 
    145  1.11.2.2  skrll 	/* enable clocks */
    146  1.11.2.2  skrll 	reg = readreg(JZ_CLKGR1);
    147  1.11.2.2  skrll 	reg &= ~(1 << 0);	/* SMB3 clock */
    148  1.11.2.2  skrll 	reg &= ~(1 << 8);	/* OTG1 clock */
    149  1.11.2.2  skrll 	reg &= ~(1 << 11);	/* AHB_MON clock */
    150  1.11.2.2  skrll 	reg &= ~(1 << 12);	/* SMB4 clock */
    151  1.11.2.2  skrll 	writereg(JZ_CLKGR1, reg);
    152  1.11.2.2  skrll 
    153  1.11.2.2  skrll 	reg = readreg(JZ_CLKGR0);
    154  1.11.2.2  skrll 	reg &= ~(1 << 2);	/* OTG0 clock */
    155  1.11.2.2  skrll 	reg &= ~(1 << 5);	/* SMB0 clock */
    156  1.11.2.2  skrll 	reg &= ~(1 << 6);	/* SMB1 clock */
    157  1.11.2.2  skrll 	reg &= ~(1 << 24);	/* UHC clock */
    158  1.11.2.2  skrll 	reg &= ~(1 << 25);	/* SMB2 clock */
    159  1.11.2.2  skrll 	writereg(JZ_CLKGR0, reg);
    160  1.11.2.2  skrll 
    161  1.11.2.2  skrll 	/* wake up the USB part */
    162  1.11.2.2  skrll 	reg = readreg(JZ_OPCR);
    163  1.11.2.2  skrll 	reg |= OPCR_SPENDN0 | OPCR_SPENDN1;
    164  1.11.2.2  skrll 	writereg(JZ_OPCR, reg);
    165  1.11.2.2  skrll 
    166  1.11.2.2  skrll 	/* setup GPIOs for I2C buses */
    167  1.11.2.2  skrll 	/* iic0 */
    168  1.11.2.2  skrll 	gpio_as_dev0(3, 30);
    169  1.11.2.2  skrll 	gpio_as_dev0(3, 31);
    170  1.11.2.2  skrll 	/* iic1 */
    171  1.11.2.2  skrll 	gpio_as_dev0(4, 30);
    172  1.11.2.2  skrll 	gpio_as_dev0(4, 31);
    173  1.11.2.2  skrll 	/* iic2 */
    174  1.11.2.2  skrll 	gpio_as_dev2(5, 16);
    175  1.11.2.2  skrll 	gpio_as_dev2(5, 17);
    176  1.11.2.2  skrll 	/* iic3 */
    177  1.11.2.2  skrll 	gpio_as_dev1(3, 10);
    178  1.11.2.2  skrll 	gpio_as_dev1(3, 11);
    179  1.11.2.2  skrll 	/* iic4 */
    180  1.11.2.2  skrll 	/* make sure these aren't SMB4 */
    181  1.11.2.2  skrll 	gpio_as_dev3(4, 3);
    182  1.11.2.2  skrll 	gpio_as_dev3(4, 4);
    183  1.11.2.2  skrll 	/* these are supposed to be connected to the RTC */
    184  1.11.2.2  skrll 	gpio_as_dev1(4, 12);
    185  1.11.2.2  skrll 	gpio_as_dev1(4, 13);
    186  1.11.2.2  skrll 	/* these can be DDC2 or SMB4, set them to DDC2 */
    187  1.11.2.2  skrll 	gpio_as_dev0(5, 24);
    188  1.11.2.2  skrll 	gpio_as_dev0(5, 25);
    189  1.11.2.2  skrll 
    190  1.11.2.2  skrll #ifdef INGENIC_DEBUG
    191  1.11.2.2  skrll 	printf("JZ_CLKGR0 %08x\n", readreg(JZ_CLKGR0));
    192  1.11.2.2  skrll 	printf("JZ_CLKGR1 %08x\n", readreg(JZ_CLKGR1));
    193  1.11.2.2  skrll 	printf("JZ_SPCR0  %08x\n", readreg(JZ_SPCR0));
    194  1.11.2.2  skrll 	printf("JZ_SPCR1  %08x\n", readreg(JZ_SPCR1));
    195  1.11.2.2  skrll 	printf("JZ_SRBC   %08x\n", readreg(JZ_SRBC));
    196  1.11.2.2  skrll 	printf("JZ_OPCR   %08x\n", readreg(JZ_OPCR));
    197  1.11.2.2  skrll 	printf("JZ_UHCCDR %08x\n", readreg(JZ_UHCCDR));
    198  1.11.2.2  skrll #endif
    199  1.11.2.2  skrll 
    200  1.11.2.2  skrll 	for (const apbus_dev_t *adv = apbus_devs; adv->name != NULL; adv++) {
    201  1.11.2.2  skrll 		struct apbus_attach_args aa;
    202  1.11.2.2  skrll 		aa.aa_name = adv->name;
    203  1.11.2.2  skrll 		aa.aa_addr = adv->addr;
    204  1.11.2.2  skrll 		aa.aa_irq  = adv->irq;
    205  1.11.2.2  skrll 		aa.aa_dmat = &apbus_dmat;
    206  1.11.2.2  skrll 		aa.aa_bst = apbus_memt;
    207  1.11.2.2  skrll 		aa.aa_pclk = pclk;
    208  1.11.2.2  skrll 
    209  1.11.2.2  skrll 		(void) config_found_ia(self, "apbus", &aa, apbus_print);
    210  1.11.2.2  skrll 	}
    211  1.11.2.2  skrll }
    212  1.11.2.2  skrll 
    213  1.11.2.2  skrll int
    214  1.11.2.2  skrll apbus_print(void *aux, const char *pnp)
    215  1.11.2.2  skrll {
    216  1.11.2.2  skrll 	struct apbus_attach_args *aa = aux;
    217  1.11.2.2  skrll 
    218  1.11.2.2  skrll 	if (pnp) {
    219  1.11.2.2  skrll 		aprint_normal("%s at %s", aa->aa_name, pnp);
    220  1.11.2.2  skrll 	}
    221  1.11.2.2  skrll 	if (aa->aa_addr != -1)
    222  1.11.2.2  skrll 		aprint_normal(" addr 0x%" PRIxBUSADDR, aa->aa_addr);
    223  1.11.2.2  skrll 	if ((pnp == NULL) && (aa->aa_irq != -1))
    224  1.11.2.2  skrll 		aprint_normal(" irq %d", aa->aa_irq);
    225  1.11.2.2  skrll 	return (UNCONF);
    226  1.11.2.2  skrll }
    227  1.11.2.2  skrll 
    228  1.11.2.2  skrll #define CHIP	   		apbus
    229  1.11.2.2  skrll #define	CHIP_MEM		/* defined */
    230  1.11.2.2  skrll #define	CHIP_W1_BUS_START(v)	0x10000000UL
    231  1.11.2.2  skrll #define CHIP_W1_BUS_END(v)	0x20000000UL
    232  1.11.2.2  skrll #define	CHIP_W1_SYS_START(v)	0x10000000UL
    233  1.11.2.2  skrll #define	CHIP_W1_SYS_END(v)	0x20000000UL
    234  1.11.2.2  skrll 
    235  1.11.2.2  skrll #include <mips/mips/bus_space_alignstride_chipdep.c>
    236