at91sam9x5.c revision 238331
1237742Simp/*-
2237742Simp * Copyright (c) 2005 Olivier Houchard.  All rights reserved.
3237742Simp * Copyright (c) 2010 Greg Ansley.  All rights reserved.
4237742Simp * Copyright (c) 2012 M. Warner Losh..  All rights reserved.
5237742Simp *
6237742Simp * Redistribution and use in source and binary forms, with or without
7237742Simp * modification, are permitted provided that the following conditions
8237742Simp * are met:
9237742Simp * 1. Redistributions of source code must retain the above copyright
10237742Simp *    notice, this list of conditions and the following disclaimer.
11237742Simp * 2. Redistributions in binary form must reproduce the above copyright
12237742Simp *    notice, this list of conditions and the following disclaimer in the
13237742Simp *    documentation and/or other materials provided with the distribution.
14237742Simp *
15237742Simp * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16237742Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17237742Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18237742Simp * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19237742Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20237742Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21237742Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22237742Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23237742Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24237742Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25237742Simp * SUCH DAMAGE.
26237742Simp */
27237742Simp
28237742Simp#include <sys/cdefs.h>
29237742Simp__FBSDID("$FreeBSD: head/sys/arm/at91/at91sam9x25.c 238331 2012-07-10 02:39:03Z imp $");
30237742Simp
31237742Simp#include <sys/param.h>
32237742Simp#include <sys/systm.h>
33237742Simp#include <sys/bus.h>
34237742Simp#include <sys/kernel.h>
35237742Simp#include <sys/malloc.h>
36237742Simp#include <sys/module.h>
37237742Simp
38237742Simp#include <machine/bus.h>
39237742Simp
40237742Simp#include <arm/at91/at91var.h>
41238331Simp#include <arm/at91/at91reg.h>
42237742Simp#include <arm/at91/at91_aicreg.h>
43237742Simp#include <arm/at91/at91sam9x25reg.h>
44237742Simp#include <arm/at91/at91_pmcreg.h>
45237742Simp#include <arm/at91/at91_pmcvar.h>
46237742Simp
47237742Simpstruct at91sam9x25_softc {
48237742Simp	device_t dev;
49237742Simp	bus_space_tag_t sc_st;
50237742Simp	bus_space_handle_t sc_sh;
51237742Simp	bus_space_handle_t sc_sys_sh;
52237742Simp	bus_space_handle_t sc_aic_sh;
53237742Simp	bus_space_handle_t sc_dbg_sh;
54237742Simp	bus_space_handle_t sc_matrix_sh;
55237742Simp};
56237742Simp
57237742Simp/*
58237742Simp * Standard priority levels for the system.  0 is lowest and 7 is highest.
59237742Simp * These values are the ones Atmel uses for its Linux port
60237742Simp */
61237742Simpstatic const int at91_irq_prio[32] =
62237742Simp{
63237743Simp	7,	/* Advanced Interrupt Controller (FIQ) */
64237742Simp	7,	/* System Peripherals */
65237743Simp	1,	/* Parallel IO Controller A and B */
66237743Simp	1,	/* Parallel IO Controller C and D */
67237743Simp	4,	/* Soft Modem */
68237742Simp	5,	/* USART 0 */
69237742Simp	5,	/* USART 1 */
70237742Simp	5,	/* USART 2 */
71237743Simp	5,	/* USART 3 */
72237743Simp	6,	/* Two-Wire Interface 0 */
73237743Simp	6,	/* Two-Wire Interface 1 */
74237743Simp	6,	/* Two-Wire Interface 2 */
75237743Simp	0,	/* Multimedia Card Interface 0 */
76237742Simp	5,	/* Serial Peripheral Interface 0 */
77237742Simp	5,	/* Serial Peripheral Interface 1 */
78237743Simp	5,	/* UART 0 */
79237743Simp	5,	/* UART 1 */
80237743Simp	0,	/* Timer Counter 0, 1, 2, 3, 4 and 5 */
81237743Simp	0,	/* Pulse Width Modulation Controller */
82237743Simp	0,	/* ADC Controller */
83237743Simp	0,	/* DMA Controller 0 */
84237743Simp	0,	/* DMA Controller 1 */
85237743Simp	2,	/* USB Host High Speed port */
86237743Simp	2,	/* USB Device High speed port */
87237743Simp	3,	/* Ethernet MAC 0 */
88237743Simp	3,	/* LDC Controller or Image Sensor Interface */
89237743Simp	0,	/* Multimedia Card Interface 1 */
90237743Simp	3,	/* Ethernet MAC 1 */
91237743Simp	4,	/* Synchronous Serial Interface */
92237743Simp	4,	/* CAN Controller 0 */
93237743Simp	4,	/* CAN Controller 1 */
94237743Simp	0,	/* Advanced Interrupt Controller (IRQ0) */
95237742Simp};
96237742Simp
97237742Simp#define DEVICE(_name, _id, _unit)		\
98237742Simp	{					\
99237742Simp		_name, _unit,			\
100237742Simp		AT91SAM9X25_ ## _id ##_BASE,	\
101237742Simp		AT91SAM9X25_ ## _id ## _SIZE,	\
102237742Simp		AT91SAM9X25_IRQ_ ## _id		\
103237742Simp	}
104237742Simp
105237742Simpstatic const struct cpu_devs at91_devs[] =
106237742Simp{
107237742Simp	DEVICE("at91_pmc", PMC,  0),
108237742Simp	DEVICE("at91_wdt", WDT,  0),
109237742Simp	DEVICE("at91_rst", RSTC, 0),
110237742Simp	DEVICE("at91_pit", PIT,  0),
111237742Simp	DEVICE("at91_pio", PIOA, 0),
112237742Simp	DEVICE("at91_pio", PIOB, 1),
113237742Simp	DEVICE("at91_pio", PIOC, 2),
114237744Simp	DEVICE("at91_pio", PIOD, 3),
115237742Simp	DEVICE("at91_twi", TWI0, 0),
116237742Simp	DEVICE("at91_twi", TWI1, 1),
117237742Simp	DEVICE("at91_twi", TWI2, 2),
118237742Simp	DEVICE("at91_mci", HSMCI0, 0),
119237742Simp	DEVICE("at91_mci", HSMCI1, 1),
120237742Simp	DEVICE("uart", DBGU,   0),
121237742Simp	DEVICE("uart", USART0, 1),
122237742Simp	DEVICE("uart", USART1, 2),
123237742Simp	DEVICE("uart", USART2, 3),
124237742Simp	DEVICE("uart", USART3, 4),
125237742Simp	DEVICE("spi",  SPI0,   0),
126237742Simp	DEVICE("spi",  SPI1,   1),
127237742Simp	DEVICE("macb", EMAC0,  0),
128237742Simp	DEVICE("macb", EMAC1,  0),
129237742Simp	DEVICE("nand", NAND,   0),
130237742Simp	DEVICE("ohci", OHCI,   0),
131237742Simp	DEVICE("ehci", EHCI,   0),
132237742Simp	{ 0, 0, 0, 0, 0 }
133237742Simp};
134237742Simp
135237742Simpstatic void
136237742Simpat91_add_child(device_t dev, int prio, const char *name, int unit,
137237742Simp    bus_addr_t addr, bus_size_t size, int irq0, int irq1, int irq2)
138237742Simp{
139237742Simp	device_t kid;
140237742Simp	struct at91_ivar *ivar;
141237742Simp
142237742Simp	kid = device_add_child_ordered(dev, prio, name, unit);
143237742Simp	if (kid == NULL) {
144237742Simp	    printf("Can't add child %s%d ordered\n", name, unit);
145237742Simp	    return;
146237742Simp	}
147237742Simp	ivar = malloc(sizeof(*ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
148237742Simp	if (ivar == NULL) {
149237742Simp		device_delete_child(dev, kid);
150237742Simp		printf("Can't add alloc ivar\n");
151237742Simp		return;
152237742Simp	}
153237742Simp	device_set_ivars(kid, ivar);
154237742Simp	resource_list_init(&ivar->resources);
155237742Simp	if (irq0 != -1) {
156237742Simp		bus_set_resource(kid, SYS_RES_IRQ, 0, irq0, 1);
157238331Simp		if (irq0 != AT91_IRQ_SYSTEM)
158237742Simp			at91_pmc_clock_add(device_get_nameunit(kid), irq0, 0);
159237742Simp	}
160237742Simp	if (irq1 != 0)
161237742Simp		bus_set_resource(kid, SYS_RES_IRQ, 1, irq1, 1);
162237742Simp	if (irq2 != 0)
163237742Simp		bus_set_resource(kid, SYS_RES_IRQ, 2, irq2, 1);
164238330Simp	if (addr != 0 && addr < AT91_BASE)
165238330Simp		addr += AT91_BASE;
166237742Simp	if (addr != 0)
167237742Simp		bus_set_resource(kid, SYS_RES_MEMORY, 0, addr, size);
168237742Simp}
169237742Simp
170237742Simpstatic void
171237742Simpat91_cpu_add_builtin_children(device_t dev)
172237742Simp{
173237742Simp	int i;
174237742Simp	const struct cpu_devs *walker;
175237742Simp
176237742Simp	for (i = 1, walker = at91_devs; walker->name; i++, walker++) {
177237742Simp		at91_add_child(dev, i, walker->name, walker->unit,
178237742Simp		    walker->mem_base, walker->mem_len, walker->irq0,
179237742Simp		    walker->irq1, walker->irq2);
180237742Simp	}
181237742Simp}
182237742Simp
183237742Simpstatic uint32_t
184237742Simpat91_pll_outa(int freq)
185237742Simp{
186237742Simp
187237742Simp	switch (freq / 10000000) {
188237742Simp		case 747 ... 801: return ((1 << 29) | (0 << 14));
189237742Simp		case 697 ... 746: return ((1 << 29) | (1 << 14));
190237742Simp		case 647 ... 696: return ((1 << 29) | (2 << 14));
191237742Simp		case 597 ... 646: return ((1 << 29) | (3 << 14));
192237742Simp		case 547 ... 596: return ((1 << 29) | (1 << 14));
193237742Simp		case 497 ... 546: return ((1 << 29) | (2 << 14));
194237742Simp		case 447 ... 496: return ((1 << 29) | (3 << 14));
195237742Simp		case 397 ... 446: return ((1 << 29) | (4 << 14));
196237742Simp		default: return (1 << 29);
197237742Simp	}
198237742Simp}
199237742Simp
200237742Simpstatic uint32_t
201237742Simpat91_pll_outb(int freq)
202237742Simp{
203237742Simp
204237742Simp	return (0);
205237742Simp}
206237742Simp
207237742Simpstatic void
208237742Simpat91_identify(driver_t *drv, device_t parent)
209237742Simp{
210237742Simp
211237742Simp	if (soc_data.type == AT91_T_SAM9X5 && soc_data.subtype == AT91_ST_SAM9X25) {
212237742Simp		at91_add_child(parent, 0, "at91sam9x25", 0, 0, 0, -1, 0, 0);
213237742Simp		at91_cpu_add_builtin_children(parent);
214237742Simp	}
215237742Simp}
216237742Simp
217237742Simpstatic int
218237742Simpat91_probe(device_t dev)
219237742Simp{
220237742Simp
221237742Simp	device_set_desc(dev, "AT91SAM9X25");
222237742Simp	return (0);
223237742Simp}
224237742Simp
225237742Simpstatic int
226237742Simpat91_attach(device_t dev)
227237742Simp{
228237742Simp	struct at91_pmc_clock *clk;
229237742Simp	struct at91sam9x25_softc *sc = device_get_softc(dev);
230237742Simp	int i;
231237742Simp
232237742Simp	struct at91_softc *at91sc = device_get_softc(device_get_parent(dev));
233237742Simp
234237742Simp	sc->sc_st = at91sc->sc_st;
235237742Simp	sc->sc_sh = at91sc->sc_sh;
236237742Simp	sc->dev = dev;
237237742Simp
238237742Simp	/*
239237742Simp	 * XXX These values work for the RM9200, SAM926[01], and SAM9X25
240237742Simp	 * will have to fix this when we want to support anything else. XXX
241237742Simp	 */
242237742Simp	if (bus_space_subregion(sc->sc_st, sc->sc_sh, AT91SAM9X25_SYS_BASE,
243237742Simp	    AT91SAM9X25_SYS_SIZE, &sc->sc_sys_sh) != 0)
244237742Simp		panic("Enable to map system registers");
245237742Simp
246237742Simp	if (bus_space_subregion(sc->sc_st, sc->sc_sh, AT91SAM9X25_DBGU_BASE,
247237742Simp	    AT91SAM9X25_DBGU_SIZE, &sc->sc_dbg_sh) != 0)
248237742Simp		panic("Enable to map DBGU registers");
249237742Simp
250237742Simp	if (bus_space_subregion(sc->sc_st, sc->sc_sh, AT91SAM9X25_AIC_BASE,
251237742Simp	    AT91SAM9X25_AIC_SIZE, &sc->sc_aic_sh) != 0)
252237742Simp		panic("Enable to map system registers");
253237742Simp
254237742Simp	/* XXX Hack to tell atmelarm about the AIC */
255237742Simp	at91sc->sc_aic_sh = sc->sc_aic_sh;
256238331Simp	at91sc->sc_irq_system = AT91_IRQ_SYSTEM;
257237742Simp
258237742Simp	for (i = 0; i < 32; i++) {
259237742Simp		bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SVR +
260237742Simp		    i * 4, i);
261237742Simp		/* Priority. */
262237742Simp		bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SMR + i * 4,
263237742Simp		    at91_irq_prio[i]);
264237742Simp		if (i < 8)
265237742Simp			bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_EOICR,
266237742Simp			    1);
267237742Simp	}
268237742Simp
269237742Simp	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SPU, 32);
270237742Simp	/* No debug. */
271237742Simp	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_DCR, 0);
272237742Simp	/* Disable and clear all interrupts. */
273237742Simp	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_IDCR, 0xffffffff);
274237742Simp	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_ICCR, 0xffffffff);
275237742Simp
276237742Simp	/* Disable all interrupts for DBGU */
277237742Simp	bus_space_write_4(sc->sc_st, sc->sc_dbg_sh, 0x0c, 0xffffffff);
278237742Simp
279237742Simp	if (bus_space_subregion(sc->sc_st, sc->sc_sh,
280237742Simp	    AT91SAM9X25_MATRIX_BASE, AT91SAM9X25_MATRIX_SIZE,
281237742Simp	    &sc->sc_matrix_sh) != 0)
282237742Simp		panic("Enable to map matrix registers");
283237742Simp
284237742Simp#if 0 /* wrong, placeholder */
285237742Simp	/* activate NAND*/
286237742Simp	i = bus_space_read_4(sc->sc_st, sc->sc_matrix_sh,
287237742Simp	    AT91SAM9X25_EBICSA);
288237742Simp	bus_space_write_4(sc->sc_st, sc->sc_matrix_sh,
289237742Simp	    AT91SAM9X25_EBICSA,
290237742Simp	    i | AT91_MATRIX_EBI_CS3A_SMC_SMARTMEDIA);
291237742Simp#endif
292237742Simp
293237742Simp	/* Update USB device port clock info */
294237742Simp	clk = at91_pmc_clock_ref("udpck");
295237742Simp	clk->pmc_mask  = PMC_SCER_UDP_SAM9;
296237742Simp	at91_pmc_clock_deref(clk);
297237742Simp
298237742Simp	/* Update USB host port clock info */
299237742Simp	clk = at91_pmc_clock_ref("uhpck");
300237742Simp	clk->pmc_mask  = PMC_SCER_UHP_SAM9;
301237742Simp	at91_pmc_clock_deref(clk);
302237742Simp
303237742Simp	/* Each SOC has different PLL contraints */
304237742Simp	clk = at91_pmc_clock_ref("plla");
305237742Simp	clk->pll_min_in    = SAM9X25_PLL_A_MIN_IN_FREQ;		/*   2 MHz */
306237742Simp	clk->pll_max_in    = SAM9X25_PLL_A_MAX_IN_FREQ;		/*  32 MHz */
307237742Simp	clk->pll_min_out   = SAM9X25_PLL_A_MIN_OUT_FREQ;	/* 400 MHz */
308237742Simp	clk->pll_max_out   = SAM9X25_PLL_A_MAX_OUT_FREQ;	/* 800 MHz */
309237742Simp	clk->pll_mul_shift = SAM9X25_PLL_A_MUL_SHIFT;
310237742Simp	clk->pll_mul_mask  = SAM9X25_PLL_A_MUL_MASK;
311237742Simp	clk->pll_div_shift = SAM9X25_PLL_A_DIV_SHIFT;
312237742Simp	clk->pll_div_mask  = SAM9X25_PLL_A_DIV_MASK;
313237742Simp	clk->set_outb      = at91_pll_outa;
314237742Simp	at91_pmc_clock_deref(clk);
315237742Simp
316237742Simp	clk = at91_pmc_clock_ref("pllb");
317237742Simp	clk->pll_min_in    = SAM9X25_PLL_B_MIN_IN_FREQ;		/*   2 MHz */
318237742Simp	clk->pll_max_in    = SAM9X25_PLL_B_MAX_IN_FREQ;		/*  32 MHz */
319237742Simp	clk->pll_min_out   = SAM9X25_PLL_B_MIN_OUT_FREQ;	/*  30 MHz */
320237742Simp	clk->pll_max_out   = SAM9X25_PLL_B_MAX_OUT_FREQ;	/* 100 MHz */
321237742Simp	clk->pll_mul_shift = SAM9X25_PLL_B_MUL_SHIFT;
322237742Simp	clk->pll_mul_mask  = SAM9X25_PLL_B_MUL_MASK;
323237742Simp	clk->pll_div_shift = SAM9X25_PLL_B_DIV_SHIFT;
324237742Simp	clk->pll_div_mask  = SAM9X25_PLL_B_DIV_MASK;
325237742Simp	clk->set_outb      = at91_pll_outb;
326237742Simp	at91_pmc_clock_deref(clk);
327237742Simp	return (0);
328237742Simp}
329237742Simp
330237742Simpstatic device_method_t at91sam9x25_methods[] = {
331237742Simp	DEVMETHOD(device_probe, at91_probe),
332237742Simp	DEVMETHOD(device_attach, at91_attach),
333237742Simp	DEVMETHOD(device_identify, at91_identify),
334237742Simp	{0, 0},
335237742Simp};
336237742Simp
337237742Simpstatic driver_t at91sam9x25_driver = {
338237742Simp	"at91sam9x25",
339237742Simp	at91sam9x25_methods,
340237742Simp	sizeof(struct at91sam9x25_softc),
341237742Simp};
342237742Simp
343237742Simpstatic devclass_t at91sam9x25_devclass;
344237742Simp
345237742SimpDRIVER_MODULE(at91sam9x25, atmelarm, at91sam9x25_driver, at91sam9x25_devclass, 0, 0);
346