Home | History | Annotate | Line # | Download | only in ti
      1  1.2  yurix /* $NetBSD: am18xx_platform.c,v 1.2 2026/07/30 15:08:08 yurix Exp $ */
      2  1.1  yurix 
      3  1.1  yurix /*-
      4  1.1  yurix  * Copyright (c) 2026 The NetBSD Foundation, Inc.
      5  1.1  yurix  * All rights reserved.
      6  1.1  yurix  *
      7  1.1  yurix  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  yurix  * by Yuri Honegger.
      9  1.1  yurix  *
     10  1.1  yurix  * Redistribution and use in source and binary forms, with or without
     11  1.1  yurix  * modification, are permitted provided that the following conditions
     12  1.1  yurix  * are met:
     13  1.1  yurix  * 1. Redistributions of source code must retain the above copyright
     14  1.1  yurix  *    notice, this list of conditions and the following disclaimer.
     15  1.1  yurix  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  yurix  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  yurix  *    documentation and/or other materials provided with the distribution.
     18  1.1  yurix  *
     19  1.1  yurix  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  yurix  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  yurix  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  yurix  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  yurix  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  yurix  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  yurix  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  yurix  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  yurix  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  yurix  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  yurix  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  yurix  */
     31  1.1  yurix 
     32  1.1  yurix /*
     33  1.1  yurix  * Platform code for the TI AM18XX family of SOCs (AM1808, AM1810). In linux
     34  1.1  yurix  * land and in the device trees, this platform is sometimes also called DA830
     35  1.1  yurix  * and DA850 (Davinci 8XX) because their silicon has a lot in common.
     36  1.1  yurix  */
     37  1.1  yurix 
     38  1.1  yurix #include "opt_console.h"
     39  1.1  yurix 
     40  1.1  yurix #include <sys/cdefs.h>
     41  1.2  yurix __KERNEL_RCSID(0, "$NetBSD: am18xx_platform.c,v 1.2 2026/07/30 15:08:08 yurix Exp $");
     42  1.1  yurix 
     43  1.1  yurix #include <uvm/uvm_extern.h>
     44  1.1  yurix 
     45  1.1  yurix #include <dev/fdt/fdt_platform.h>
     46  1.1  yurix #include <dev/fdt/fdtvar.h>
     47  1.1  yurix #include <dev/ic/comreg.h>
     48  1.1  yurix 
     49  1.1  yurix #include <arch/evbarm/fdt/platform.h>
     50  1.1  yurix #include <arm/fdt/arm_fdtvar.h>
     51  1.1  yurix 
     52  1.1  yurix #define AM18XX_IO_VBASE KERNEL_IO_VBASE
     53  1.1  yurix #define AM18XX_IO_PBASE 0x01c00000
     54  1.1  yurix #define AM18XX_IO_SIZE 0x00400000
     55  1.1  yurix #define AM18XX_INTC_VBASE (AM18XX_IO_VBASE + AM18XX_IO_SIZE)
     56  1.1  yurix #define AM18XX_INTC_PBASE 0xfffee000
     57  1.1  yurix #define AM18XX_INTC_SIZE 0x2000
     58  1.1  yurix 
     59  1.1  yurix #define AM18XX_TIMER1_BASE 0x01C21000
     60  1.1  yurix #define AM18XX_TIMER1_SIZE 0x1000
     61  1.1  yurix #define AM18XX_TIMER1_TIM12 0x10
     62  1.1  yurix #define AM18XX_TIMER1_TIM34 0x14
     63  1.1  yurix #define AM18XX_TIMER1_PRD12 0x18
     64  1.1  yurix #define AM18XX_TIMER1_PRD34 0x1C
     65  1.1  yurix #define AM18XX_TIMER1_TCR 0x20
     66  1.1  yurix #define AM18XX_TIMER1_TGCR 0x24
     67  1.1  yurix #define AM18XX_TIMER1_WDTCR 0x28
     68  1.1  yurix 
     69  1.1  yurix #define AM18XX_TIMER_TCR_ENAMODE12_CONTINUOUS 0x80
     70  1.1  yurix #define AM18XX_TIMER_TGCR_TIMMODE32_UNCHAINED 0x4
     71  1.1  yurix #define AM18XX_TIMER_TGCR_TIMMODE64_WATCHDOG 0x8
     72  1.1  yurix #define AM18XX_TIMER_TGCR_TIM12EN 1
     73  1.1  yurix #define AM18XX_TIMER_TGCR_TIM34EN 2
     74  1.1  yurix #define AM18XX_TIMER_WDTCR_WDEN 1
     75  1.1  yurix #define AM18XX_TIMER_WDTCR_KEY0 0xa5c60000
     76  1.1  yurix #define AM18XX_TIMER_WDTCR_KEY1 0xda7e0000
     77  1.1  yurix 
     78  1.1  yurix 
     79  1.1  yurix void am18xx_platform_early_putchar(char);
     80  1.1  yurix 
     81  1.1  yurix extern struct arm32_bus_dma_tag arm_generic_dma_tag;
     82  1.1  yurix extern struct bus_space arm_generic_bs_tag;
     83  1.1  yurix 
     84  1.1  yurix void __noasan
     85  1.1  yurix am18xx_platform_early_putchar(char c)
     86  1.1  yurix {
     87  1.1  yurix #ifdef CONSADDR
     88  1.1  yurix #define CONSADDR_VA (CONSADDR - AM18XX_IO_PBASE + KERNEL_IO_VBASE)
     89  1.1  yurix 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p()
     90  1.1  yurix 					  ? (volatile uint32_t *)CONSADDR_VA
     91  1.1  yurix 					  : (volatile uint32_t *)CONSADDR;
     92  1.1  yurix 
     93  1.1  yurix 	while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
     94  1.1  yurix 		continue;
     95  1.1  yurix 
     96  1.1  yurix 	uartaddr[com_data] = htole32(c);
     97  1.1  yurix #endif
     98  1.1  yurix }
     99  1.1  yurix 
    100  1.1  yurix 
    101  1.1  yurix static const struct pmap_devmap *
    102  1.1  yurix am18xx_platform_devmap(void)
    103  1.1  yurix {
    104  1.1  yurix 	static const struct pmap_devmap devmap[] = {
    105  1.1  yurix 		/* input/output registers */
    106  1.1  yurix 		DEVMAP_ENTRY(AM18XX_IO_VBASE,
    107  1.1  yurix 			     AM18XX_IO_PBASE,
    108  1.1  yurix 			     AM18XX_IO_SIZE),
    109  1.1  yurix 		/* interrupt controller */
    110  1.1  yurix 		DEVMAP_ENTRY(AM18XX_INTC_VBASE,
    111  1.1  yurix 			     AM18XX_INTC_PBASE,
    112  1.1  yurix 			     AM18XX_INTC_SIZE),
    113  1.1  yurix 		DEVMAP_ENTRY_END
    114  1.1  yurix 	};
    115  1.1  yurix 
    116  1.1  yurix 	return devmap;
    117  1.1  yurix }
    118  1.1  yurix 
    119  1.1  yurix static void
    120  1.1  yurix am18xx_platform_init_attach_args(struct fdt_attach_args *faa)
    121  1.1  yurix {
    122  1.1  yurix 	faa->faa_bst = &arm_generic_bs_tag;
    123  1.1  yurix 	faa->faa_dmat = &arm_generic_dma_tag;
    124  1.1  yurix }
    125  1.1  yurix 
    126  1.1  yurix static void
    127  1.1  yurix am18xx_platform_delay(u_int n)
    128  1.1  yurix {
    129  1.1  yurix 	/* Use Timer1 for delay. Timer0 is used*/
    130  1.1  yurix 	static bus_space_tag_t bst = &arm_generic_bs_tag;
    131  1.1  yurix 	static bus_space_handle_t bsh = 0;
    132  1.1  yurix 
    133  1.1  yurix 	if (bsh == 0) {
    134  1.1  yurix 		/* map Timer1 */
    135  1.1  yurix 		bus_space_map(bst, AM18XX_TIMER1_BASE, AM18XX_TIMER1_SIZE,
    136  1.1  yurix 			      0, &bsh);
    137  1.1  yurix 
    138  1.1  yurix 		/* disable counter to allow changing mode */
    139  1.1  yurix 		bus_space_write_4(bst, bsh, AM18XX_TIMER1_TCR, 0);
    140  1.1  yurix 		/* set mode to 32-bit unchained */
    141  1.1  yurix 		bus_space_write_4(bst, bsh, AM18XX_TIMER1_TGCR,
    142  1.1  yurix 				  AM18XX_TIMER_TGCR_TIMMODE32_UNCHAINED |
    143  1.1  yurix 				  AM18XX_TIMER_TGCR_TIM12EN);
    144  1.1  yurix 		/* load period registers with maximum period */
    145  1.1  yurix 		bus_space_write_4(bst, bsh, AM18XX_TIMER1_PRD12, 0xFFFFFFFF);
    146  1.1  yurix 		/* enable timer */
    147  1.1  yurix 		bus_space_write_4(bst, bsh, AM18XX_TIMER1_TCR,
    148  1.1  yurix 				  AM18XX_TIMER_TCR_ENAMODE12_CONTINUOUS);
    149  1.1  yurix 
    150  1.1  yurix 	}
    151  1.1  yurix 
    152  1.1  yurix 	/*
    153  1.1  yurix 	 * The counter is driven by PLL0_AUXCLK, which is taken from OSCIN.
    154  1.1  yurix 	 * On the EV3, that is 24MHz.
    155  1.1  yurix 	 *
    156  1.1  yurix 	 * n is in microseconds (us)
    157  1.1  yurix 	 *
    158  1.1  yurix 	 * TODO: The frequency is board-dependent and we should get it from the
    159  1.1  yurix 	 * device tree.
    160  1.1  yurix 	 */
    161  1.1  yurix 	long ticks = n * (24000000 / 1000000);
    162  1.1  yurix 
    163  1.1  yurix 	uint32_t prev, cur;
    164  1.1  yurix 	prev = bus_space_read_4(bst, bsh, AM18XX_TIMER1_TIM12);
    165  1.1  yurix 	while (ticks > 0) {
    166  1.1  yurix 		cur = bus_space_read_4(bst, bsh, AM18XX_TIMER1_TIM12);
    167  1.1  yurix 		if (cur >= prev)
    168  1.1  yurix 			ticks -= (cur - prev);
    169  1.1  yurix 		else
    170  1.1  yurix 			ticks -= (UINT32_MAX - cur + prev);
    171  1.1  yurix 		prev = cur;
    172  1.1  yurix 	}
    173  1.1  yurix }
    174  1.1  yurix 
    175  1.1  yurix /*
    176  1.1  yurix  * To reset the AM1808, you have to crash the watchdog.
    177  1.1  yurix  */
    178  1.1  yurix static void
    179  1.1  yurix am18xx_platform_reset(void)
    180  1.1  yurix {
    181  1.1  yurix 	/* map TIMER1 */
    182  1.1  yurix 	bus_space_tag_t bst = &arm_generic_bs_tag;
    183  1.1  yurix 	bus_space_handle_t bsh = 0;
    184  1.1  yurix 	bus_space_map(bst, AM18XX_TIMER1_BASE, AM18XX_TIMER1_SIZE, 0, &bsh);
    185  1.1  yurix 
    186  1.1  yurix 	/* disable counter to allow changing mode */
    187  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_TCR, 0);
    188  1.1  yurix 	/* set mode to watchdog unchained */
    189  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_TGCR,
    190  1.1  yurix 			  AM18XX_TIMER_TGCR_TIMMODE64_WATCHDOG |
    191  1.1  yurix 			  AM18XX_TIMER_TGCR_TIM12EN |
    192  1.1  yurix 			  AM18XX_TIMER_TGCR_TIM34EN);
    193  1.1  yurix 	/* set counter and reload registers */
    194  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_TIM12, 0);
    195  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_TIM34, 0);
    196  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_PRD12, 0);
    197  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_PRD34, 0);
    198  1.1  yurix 
    199  1.1  yurix 	/* execute the watchdog enable sequence */
    200  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_WDTCR,
    201  1.1  yurix 			  AM18XX_TIMER_WDTCR_KEY0 | AM18XX_TIMER_WDTCR_WDEN);
    202  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_WDTCR,
    203  1.1  yurix 			  AM18XX_TIMER_WDTCR_KEY1 | AM18XX_TIMER_WDTCR_WDEN);
    204  1.1  yurix 	/* trigger a reset by writing an invalid value to WDTCR*/
    205  1.1  yurix 	bus_space_write_4(bst, bsh, AM18XX_TIMER1_WDTCR, 0xaffe);
    206  1.1  yurix 
    207  1.1  yurix 	/* NOTREACHED */
    208  1.1  yurix }
    209  1.1  yurix 
    210  1.1  yurix static const struct fdt_platform am18xx_platform = {
    211  1.1  yurix 	.fp_devmap = am18xx_platform_devmap,
    212  1.1  yurix 	.fp_bootstrap = arm_fdt_cpu_bootstrap,
    213  1.1  yurix 	.fp_init_attach_args = am18xx_platform_init_attach_args,
    214  1.1  yurix 	.fp_uart_freq = NULL,
    215  1.1  yurix 	.fp_delay = am18xx_platform_delay,
    216  1.1  yurix 	.fp_reset = am18xx_platform_reset,
    217  1.1  yurix };
    218  1.1  yurix 
    219  1.1  yurix /* The device tree name for the AM1808 is da850 (davinci 850) */
    220  1.1  yurix FDT_PLATFORM(am18xx, "ti,da850", &am18xx_platform);
    221