1/*-
2 * Copyright (c) 2014 M. Warner Losh.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD$");
28
29#include <sys/types.h>
30
31#include <arm/at91/at91reg.h>
32#include <arm/at91/at91_smc.h>
33#include <arm/at91/at91sam9260reg.h>
34
35/*
36 * RD4HW()/WR4HW() read and write at91 hardware register space directly. They
37 * serve the same purpose as the RD4()/WR4() idiom you see in many drivers,
38 * except that those translate to bus_space calls, but in this code we need to
39 * access some devices before bus_space is ready to use.  Of course for this to
40 * work the appropriate static device mappings need to be made in machdep.c.
41 */
42static inline uint32_t
43RD4HW(uint32_t devbase, uint32_t regoff)
44{
45
46	return *(volatile uint32_t *)(AT91_BASE + devbase + regoff);
47}
48
49
50static inline void
51WR4HW(uint32_t devbase, uint32_t regoff, uint32_t val)
52{
53
54	*(volatile uint32_t *)(AT91_BASE + devbase + regoff) = val;
55}
56
57
58void
59at91_smc_setup(int id, int cs, const struct at91_smc_init *smc)
60{
61	// Need a generic way to get this address for all SoCs... Assume 9260 for now...
62	uint32_t base = AT91SAM9260_SMC_BASE + SMC_CS_OFF(cs);
63
64	WR4HW(base, SMC_SETUP, SMC_SETUP_NCS_RD_SETUP(smc->ncs_rd_setup) |
65	      SMC_SETUP_NRD_SETUP(smc->nrd_setup) |
66	      SMC_SETUP_NCS_WR_SETUP(smc->ncs_wr_setup) |
67	      SMC_SETUP_NWE_SETUP(smc->nwe_setup));
68	WR4HW(base, SMC_PULSE, SMC_PULSE_NCS_RD_PULSE(smc->ncs_rd_pulse) |
69	      SMC_PULSE_NRD_PULSE(smc->nrd_pulse) |
70	      SMC_PULSE_NCS_WR_PULSE(smc->ncs_wr_pulse) |
71	      SMC_PULSE_NWE_PULSE(smc->nwe_pulse));
72	WR4HW(base, SMC_CYCLE, SMC_CYCLE_NRD_CYCLE(smc->nrd_cycle) |
73	      SMC_CYCLE_NWE_CYCLE(smc->nwe_cycle));
74	WR4HW(base, SMC_MODE, smc->mode | SMC_MODE_TDF_CYCLES(smc->tdf_cycles));
75}
76
77void
78at91_ebi_enable(int bank)
79{
80
81	WR4HW(AT91SAM9260_MATRIX_BASE, AT91SAM9260_EBICSA, (1 << bank) |
82	      RD4HW(AT91SAM9260_MATRIX_BASE, AT91SAM9260_EBICSA));
83}
84
85void
86at91_ebi_disable(int bank)
87{
88
89	WR4HW(AT91SAM9260_MATRIX_BASE, AT91SAM9260_EBICSA, ~(1 << bank) &
90	      RD4HW(AT91SAM9260_MATRIX_BASE, AT91SAM9260_EBICSA));
91}
92