| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414 | /* * Copyright (C) 2005-2006 Atmel Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/clk.h>#include <linux/delay.h>#include <linux/dw_dmac.h>#include <linux/fb.h>#include <linux/init.h>#include <linux/platform_device.h>#include <linux/dma-mapping.h>#include <linux/slab.h>#include <linux/gpio.h>#include <linux/spi/spi.h>#include <linux/usb/atmel_usba_udc.h>#include <mach/atmel-mci.h>#include <linux/atmel-mci.h>#include <asm/io.h>#include <asm/irq.h>#include <mach/at32ap700x.h>#include <mach/board.h>#include <mach/hmatrix.h>#include <mach/portmux.h>#include <mach/sram.h>#include <sound/atmel-abdac.h>#include <sound/atmel-ac97c.h>#include <video/atmel_lcdc.h>#include "clock.h"#include "pio.h"#include "pm.h"#define PBMEM(base)					\	{						\		.start		= base,			\		.end		= base + 0x3ff,		\		.flags		= IORESOURCE_MEM,	\	}#define IRQ(num)					\	{						\		.start		= num,			\		.end		= num,			\		.flags		= IORESOURCE_IRQ,	\	}#define NAMED_IRQ(num, _name)				\	{						\		.start		= num,			\		.end		= num,			\		.name		= _name,		\		.flags		= IORESOURCE_IRQ,	\	}/* REVISIT these assume *every* device supports DMA, but several * don't ... tc, smc, pio, rtc, watchdog, pwm, ps2, and more. */#define DEFINE_DEV(_name, _id)					\static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32);		\static struct platform_device _name##_id##_device = {		\	.name		= #_name,				\	.id		= _id,					\	.dev		= {					\		.dma_mask = &_name##_id##_dma_mask,		\		.coherent_dma_mask = DMA_BIT_MASK(32),		\	},							\	.resource	= _name##_id##_resource,		\	.num_resources	= ARRAY_SIZE(_name##_id##_resource),	\}#define DEFINE_DEV_DATA(_name, _id)				\static u64 _name##_id##_dma_mask = DMA_BIT_MASK(32);		\static struct platform_device _name##_id##_device = {		\	.name		= #_name,				\	.id		= _id,					\	.dev		= {					\		.dma_mask = &_name##_id##_dma_mask,		\		.platform_data	= &_name##_id##_data,		\		.coherent_dma_mask = DMA_BIT_MASK(32),		\	},							\	.resource	= _name##_id##_resource,		\	.num_resources	= ARRAY_SIZE(_name##_id##_resource),	\}#define select_peripheral(port, pin_mask, periph, flags)	\	at32_select_periph(GPIO_##port##_BASE, pin_mask,	\			   GPIO_##periph, flags)#define DEV_CLK(_name, devname, bus, _index)			\static struct clk devname##_##_name = {				\	.name		= #_name,				\	.dev		= &devname##_device.dev,		\	.parent		= &bus##_clk,				\	.mode		= bus##_clk_mode,			\	.get_rate	= bus##_clk_get_rate,			\	.index		= _index,				\}static DEFINE_SPINLOCK(pm_lock);static struct clk osc0;static struct clk osc1;static unsigned long osc_get_rate(struct clk *clk){	return at32_board_osc_rates[clk->index];}static unsigned long pll_get_rate(struct clk *clk, unsigned long control){	unsigned long div, mul, rate;	div = PM_BFEXT(PLLDIV, control) + 1;	mul = PM_BFEXT(PLLMUL, control) + 1;	rate = clk->parent->get_rate(clk->parent);	rate = (rate + div / 2) / div;	rate *= mul;	return rate;}static long pll_set_rate(struct clk *clk, unsigned long rate,			 u32 *pll_ctrl){	unsigned long mul;	unsigned long mul_best_fit = 0;	unsigned long div;	unsigned long div_min;	unsigned long div_max;	unsigned long div_best_fit = 0;	unsigned long base;	unsigned long pll_in;	unsigned long actual = 0;	unsigned long rate_error;	unsigned long rate_error_prev = ~0UL;	u32 ctrl;	/* Rate must be between 80 MHz and 200 Mhz. */	if (rate < 80000000UL || rate > 200000000UL)		return -EINVAL;	ctrl = PM_BF(PLLOPT, 4);	base = clk->parent->get_rate(clk->parent);	/* PLL input frequency must be between 6 MHz and 32 MHz. */	div_min = DIV_ROUND_UP(base, 32000000UL);	div_max = base / 6000000UL;	if (div_max < div_min)		return -EINVAL;	for (div = div_min; div <= div_max; div++) {		pll_in = (base + div / 2) / div;		mul = (rate + pll_in / 2) / pll_in;		if (mul == 0)			continue;		actual = pll_in * mul;		rate_error = abs(actual - rate);		if (rate_error < rate_error_prev) {			mul_best_fit = mul;			div_best_fit = div;			rate_error_prev = rate_error;		}		if (rate_error == 0)			break;	}	if (div_best_fit == 0)		return -EINVAL;	ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);	ctrl |= PM_BF(PLLDIV, div_best_fit - 1);	ctrl |= PM_BF(PLLCOUNT, 16);	if (clk->parent == &osc1)		ctrl |= PM_BIT(PLLOSC);	*pll_ctrl = ctrl;	return actual;}static unsigned long pll0_get_rate(struct clk *clk){	u32 control;	control = pm_readl(PLL0);	return pll_get_rate(clk, control);}static void pll1_mode(struct clk *clk, int enabled){	unsigned long timeout;	u32 status;	u32 ctrl;	ctrl = pm_readl(PLL1);	if (enabled) {		if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {			pr_debug("clk %s: failed to enable, rate not set\n",					clk->name);			return;		}		ctrl |= PM_BIT(PLLEN);		pm_writel(PLL1, ctrl);		/* Wait for PLL lock. */		for (timeout = 10000; timeout; timeout--) {			status = pm_readl(ISR);			if (status & PM_BIT(LOCK1))				break;			udelay(10);		}		if (!(status & PM_BIT(LOCK1)))			printk(KERN_ERR "clk %s: timeout waiting for lock\n",					clk->name);	} else {		ctrl &= ~PM_BIT(PLLEN);		pm_writel(PLL1, ctrl);	}}static unsigned long pll1_get_rate(struct clk *clk){	u32 control;	control = pm_readl(PLL1);	return pll_get_rate(clk, control);}static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply){	u32 ctrl = 0;	unsigned long actual_rate;	actual_rate = pll_set_rate(clk, rate, &ctrl);	if (apply) {		if (actual_rate != rate)			return -EINVAL;		if (clk->users > 0)			return -EBUSY;		pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",				clk->name, rate, actual_rate);		pm_writel(PLL1, ctrl);	}	return actual_rate;}static int pll1_set_parent(struct clk *clk, struct clk *parent){	u32 ctrl;	if (clk->users > 0)		return -EBUSY;	ctrl = pm_readl(PLL1);	WARN_ON(ctrl & PM_BIT(PLLEN));	if (parent == &osc0)		ctrl &= ~PM_BIT(PLLOSC);	else if (parent == &osc1)		ctrl |= PM_BIT(PLLOSC);	else		return -EINVAL;	pm_writel(PLL1, ctrl);	clk->parent = parent;	return 0;}/* * The AT32AP7000 has five primary clock sources: One 32kHz * oscillator, two crystal oscillators and two PLLs. */static struct clk osc32k = {	.name		= "osc32k",	.get_rate	= osc_get_rate,	.users		= 1,	.index		= 0,};static struct clk osc0 = {	.name		= "osc0",	.get_rate	= osc_get_rate,	.users		= 1,	.index		= 1,};static struct clk osc1 = {	.name		= "osc1",	.get_rate	= osc_get_rate,	.index		= 2,};static struct clk pll0 = {	.name		= "pll0",	.get_rate	= pll0_get_rate,	.parent		= &osc0,};static struct clk pll1 = {	.name		= "pll1",	.mode		= pll1_mode,	.get_rate	= pll1_get_rate,	.set_rate	= pll1_set_rate,	.set_parent	= pll1_set_parent,	.parent		= &osc0,};/* * The main clock can be either osc0 or pll0.  The boot loader may * have chosen one for us, so we don't really know which one until we * have a look at the SM. */static struct clk *main_clock;/* * Synchronous clocks are generated from the main clock. The clocks * must satisfy the constraint *   fCPU >= fHSB >= fPB * i.e. each clock must not be faster than its parent. */static unsigned long bus_clk_get_rate(struct clk *clk, unsigned int shift){	return main_clock->get_rate(main_clock) >> shift;};static void cpu_clk_mode(struct clk *clk, int enabled){	unsigned long flags;	u32 mask;	spin_lock_irqsave(&pm_lock, flags);	mask = pm_readl(CPU_MASK);	if (enabled)		mask |= 1 << clk->index;	else		mask &= ~(1 << clk->index);	pm_writel(CPU_MASK, mask);	spin_unlock_irqrestore(&pm_lock, flags);}static unsigned long cpu_clk_get_rate(struct clk *clk){	unsigned long cksel, shift = 0;	cksel = pm_readl(CKSEL);	if (cksel & PM_BIT(CPUDIV))		shift = PM_BFEXT(CPUSEL, cksel) + 1;	return bus_clk_get_rate(clk, shift);}static long cpu_clk_set_rate(struct clk *clk, unsigned long rate, int apply){	u32 control;	unsigned long parent_rate, child_div, actual_rate, div;	parent_rate = clk->parent->get_rate(clk->parent);	control = pm_readl(CKSEL);	if (control & PM_BIT(HSBDIV))		child_div = 1 << (PM_BFEXT(HSBSEL, control) + 1);	else		child_div = 1;	if (rate > 3 * (parent_rate / 4) || child_div == 1) {		actual_rate = parent_rate;		control &= ~PM_BIT(CPUDIV);	} else {		unsigned int cpusel;		div = (parent_rate + rate / 2) / rate;		if (div > child_div)			div = child_div;		cpusel = (div > 1) ? (fls(div) - 2) : 0;		control = PM_BIT(CPUDIV) | PM_BFINS(CPUSEL, cpusel, control);		actual_rate = parent_rate / (1 << (cpusel + 1));	}	pr_debug("clk %s: new rate %lu (actual rate %lu)\n",			clk->name, rate, actual_rate);	if (apply)		pm_writel(CKSEL, control);	return actual_rate;}static void hsb_clk_mode(struct clk *clk, int enabled){	unsigned long flags;	u32 mask;	spin_lock_irqsave(&pm_lock, flags);	mask = pm_readl(HSB_MASK);	if (enabled)		mask |= 1 << clk->index;	else		mask &= ~(1 << clk->index);	pm_writel(HSB_MASK, mask);	spin_unlock_irqrestore(&pm_lock, flags);}static unsigned long hsb_clk_get_rate(struct clk *clk){	unsigned long cksel, shift = 0;	cksel = pm_readl(CKSEL);	if (cksel & PM_BIT(HSBDIV))		shift = PM_BFEXT(HSBSEL, cksel) + 1;	return bus_clk_get_rate(clk, shift);}void pba_clk_mode(struct clk *clk, int enabled){	unsigned long flags;	u32 mask;	spin_lock_irqsave(&pm_lock, flags);	mask = pm_readl(PBA_MASK);	if (enabled)		mask |= 1 << clk->index;	else		mask &= ~(1 << clk->index);	pm_writel(PBA_MASK, mask);	spin_unlock_irqrestore(&pm_lock, flags);}unsigned long pba_clk_get_rate(struct clk *clk){	unsigned long cksel, shift = 0;	cksel = pm_readl(CKSEL);	if (cksel & PM_BIT(PBADIV))		shift = PM_BFEXT(PBASEL, cksel) + 1;	return bus_clk_get_rate(clk, shift);}static void pbb_clk_mode(struct clk *clk, int enabled){	unsigned long flags;	u32 mask;	spin_lock_irqsave(&pm_lock, flags);	mask = pm_readl(PBB_MASK);	if (enabled)		mask |= 1 << clk->index;	else		mask &= ~(1 << clk->index);	pm_writel(PBB_MASK, mask);	spin_unlock_irqrestore(&pm_lock, flags);}static unsigned long pbb_clk_get_rate(struct clk *clk){	unsigned long cksel, shift = 0;	cksel = pm_readl(CKSEL);	if (cksel & PM_BIT(PBBDIV))		shift = PM_BFEXT(PBBSEL, cksel) + 1;	return bus_clk_get_rate(clk, shift);}static struct clk cpu_clk = {	.name		= "cpu",	.get_rate	= cpu_clk_get_rate,	.set_rate	= cpu_clk_set_rate,	.users		= 1,};static struct clk hsb_clk = {	.name		= "hsb",	.parent		= &cpu_clk,	.get_rate	= hsb_clk_get_rate,};static struct clk pba_clk = {	.name		= "pba",	.parent		= &hsb_clk,	.mode		= hsb_clk_mode,	.get_rate	= pba_clk_get_rate,	.index		= 1,};static struct clk pbb_clk = {	.name		= "pbb",	.parent		= &hsb_clk,	.mode		= hsb_clk_mode,	.get_rate	= pbb_clk_get_rate,	.users		= 1,	.index		= 2,};/* -------------------------------------------------------------------- *  Generic Clock operations * -------------------------------------------------------------------- */static void genclk_mode(struct clk *clk, int enabled){	u32 control;	control = pm_readl(GCCTRL(clk->index));	if (enabled)		control |= PM_BIT(CEN);	else		control &= ~PM_BIT(CEN);	pm_writel(GCCTRL(clk->index), control);}static unsigned long genclk_get_rate(struct clk *clk){	u32 control;	unsigned long div = 1;	control = pm_readl(GCCTRL(clk->index));	if (control & PM_BIT(DIVEN))		div = 2 * (PM_BFEXT(DIV, control) + 1);	return clk->parent->get_rate(clk->parent) / div;}static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply){	u32 control;	unsigned long parent_rate, actual_rate, div;	parent_rate = clk->parent->get_rate(clk->parent);	control = pm_readl(GCCTRL(clk->index));	if (rate > 3 * parent_rate / 4) {		actual_rate = parent_rate;		control &= ~PM_BIT(DIVEN);	} else {		div = (parent_rate + rate) / (2 * rate) - 1;		control = PM_BFINS(DIV, div, control) | PM_BIT(DIVEN);		actual_rate = parent_rate / (2 * (div + 1));	}	dev_dbg(clk->dev, "clk %s: new rate %lu (actual rate %lu)\n",		clk->name, rate, actual_rate);	if (apply)		pm_writel(GCCTRL(clk->index), control);	return actual_rate;}int genclk_set_parent(struct clk *clk, struct clk *parent){	u32 control;	dev_dbg(clk->dev, "clk %s: new parent %s (was %s)\n",		clk->name, parent->name, clk->parent->name);	control = pm_readl(GCCTRL(clk->index));	if (parent == &osc1 || parent == &pll1)		control |= PM_BIT(OSCSEL);	else if (parent == &osc0 || parent == &pll0)		control &= ~PM_BIT(OSCSEL);	else		return -EINVAL;	if (parent == &pll0 || parent == &pll1)		control |= PM_BIT(PLLSEL);	else		control &= ~PM_BIT(PLLSEL);	pm_writel(GCCTRL(clk->index), control);	clk->parent = parent;	return 0;}static void __init genclk_init_parent(struct clk *clk){	u32 control;	struct clk *parent;	BUG_ON(clk->index > 7);	control = pm_readl(GCCTRL(clk->index));	if (control & PM_BIT(OSCSEL))		parent = (control & PM_BIT(PLLSEL)) ? &pll1 : &osc1;	else		parent = (control & PM_BIT(PLLSEL)) ? &pll0 : &osc0;	clk->parent = parent;}static struct dw_dma_platform_data dw_dmac0_data = {	.nr_channels	= 3,	.block_size	= 4095U,	.nr_masters	= 2,	.data_width	= { 2, 2, 0, 0 },};static struct resource dw_dmac0_resource[] = {	PBMEM(0xff200000),	IRQ(2),};DEFINE_DEV_DATA(dw_dmac, 0);DEV_CLK(hclk, dw_dmac0, hsb, 10);/* -------------------------------------------------------------------- *  System peripherals * -------------------------------------------------------------------- */static struct resource at32_pm0_resource[] = {	{		.start	= 0xfff00000,		.end	= 0xfff0007f,		.flags	= IORESOURCE_MEM,	},	IRQ(20),};static struct resource at32ap700x_rtc0_resource[] = {	{		.start	= 0xfff00080,		.end	= 0xfff000af,		.flags	= IORESOURCE_MEM,	},	IRQ(21),};static struct resource at32_wdt0_resource[] = {	{		.start	= 0xfff000b0,		.end	= 0xfff000cf,		.flags	= IORESOURCE_MEM,	},};static struct resource at32_eic0_resource[] = {	{		.start	= 0xfff00100,		.end	= 0xfff0013f,		.flags	= IORESOURCE_MEM,	},	IRQ(19),};DEFINE_DEV(at32_pm, 0);DEFINE_DEV(at32ap700x_rtc, 0);DEFINE_DEV(at32_wdt, 0);DEFINE_DEV(at32_eic, 0);/* * Peripheral clock for PM, RTC, WDT and EIC. PM will ensure that this * is always running. */static struct clk at32_pm_pclk = {	.name		= "pclk",	.dev		= &at32_pm0_device.dev,	.parent		= &pbb_clk,	.mode		= pbb_clk_mode,	.get_rate	= pbb_clk_get_rate,	.users		= 1,	.index		= 0,};static struct resource intc0_resource[] = {	PBMEM(0xfff00400),};struct platform_device at32_intc0_device = {	.name		= "intc",	.id		= 0,	.resource	= intc0_resource,	.num_resources	= ARRAY_SIZE(intc0_resource),};DEV_CLK(pclk, at32_intc0, pbb, 1);static struct clk ebi_clk = {	.name		= "ebi",	.parent		= &hsb_clk,	.mode		= hsb_clk_mode,	.get_rate	= hsb_clk_get_rate,	.users		= 1,};static struct clk hramc_clk = {	.name		= "hramc",	.parent		= &hsb_clk,	.mode		= hsb_clk_mode,	.get_rate	= hsb_clk_get_rate,	.users		= 1,	.index		= 3,};static struct clk sdramc_clk = {	.name		= "sdramc_clk",	.parent		= &pbb_clk,	.mode		= pbb_clk_mode,	.get_rate	= pbb_clk_get_rate,	.users		= 1,	.index		= 14,};static struct resource smc0_resource[] = {	PBMEM(0xfff03400),};DEFINE_DEV(smc, 0);DEV_CLK(pclk, smc0, pbb, 13);DEV_CLK(mck, smc0, hsb, 0);static struct platform_device pdc_device = {	.name		= "pdc",	.id		= 0,};DEV_CLK(hclk, pdc, hsb, 4);DEV_CLK(pclk, pdc, pba, 16);static struct clk pico_clk = {	.name		= "pico",	.parent		= &cpu_clk,	.mode		= cpu_clk_mode,	.get_rate	= cpu_clk_get_rate,	.users		= 1,};/* -------------------------------------------------------------------- * HMATRIX * -------------------------------------------------------------------- */struct clk at32_hmatrix_clk = {	.name		= "hmatrix_clk",	.parent		= &pbb_clk,	.mode		= pbb_clk_mode,	.get_rate	= pbb_clk_get_rate,	.index		= 2,	.users		= 1,};/* * Set bits in the HMATRIX Special Function Register (SFR) used by the * External Bus Interface (EBI). This can be used to enable special * features like CompactFlash support, NAND Flash support, etc. on * certain chipselects. */static inline void set_ebi_sfr_bits(u32 mask){	hmatrix_sfr_set_bits(HMATRIX_SLAVE_EBI, mask);}/* -------------------------------------------------------------------- *  Timer/Counter (TC) * -------------------------------------------------------------------- */static struct resource at32_tcb0_resource[] = {	PBMEM(0xfff00c00),	IRQ(22),};static struct platform_device at32_tcb0_device = {	.name		= "atmel_tcb",	.id		= 0,	.resource	= at32_tcb0_resource,	.num_resources	= ARRAY_SIZE(at32_tcb0_resource),};DEV_CLK(t0_clk, at32_tcb0, pbb, 3);static struct resource at32_tcb1_resource[] = {	PBMEM(0xfff01000),	IRQ(23),};static struct platform_device at32_tcb1_device = {	.name		= "atmel_tcb",	.id		= 1,	.resource	= at32_tcb1_resource,	.num_resources	= ARRAY_SIZE(at32_tcb1_resource),};DEV_CLK(t0_clk, at32_tcb1, pbb, 4);/* -------------------------------------------------------------------- *  PIO * -------------------------------------------------------------------- */static struct resource pio0_resource[] = {	PBMEM(0xffe02800),	IRQ(13),};DEFINE_DEV(pio, 0);DEV_CLK(mck, pio0, pba, 10);static struct resource pio1_resource[] = {	PBMEM(0xffe02c00),	IRQ(14),};DEFINE_DEV(pio, 1);DEV_CLK(mck, pio1, pba, 11);static struct resource pio2_resource[] = {	PBMEM(0xffe03000),	IRQ(15),};DEFINE_DEV(pio, 2);DEV_CLK(mck, pio2, pba, 12);static struct resource pio3_resource[] = {	PBMEM(0xffe03400),	IRQ(16),};DEFINE_DEV(pio, 3);DEV_CLK(mck, pio3, pba, 13);static struct resource pio4_resource[] = {	PBMEM(0xffe03800),	IRQ(17),};DEFINE_DEV(pio, 4);DEV_CLK(mck, pio4, pba, 14);static int __init system_device_init(void){	platform_device_register(&at32_pm0_device);	platform_device_register(&at32_intc0_device);	platform_device_register(&at32ap700x_rtc0_device);	platform_device_register(&at32_wdt0_device);	platform_device_register(&at32_eic0_device);	platform_device_register(&smc0_device);	platform_device_register(&pdc_device);	platform_device_register(&dw_dmac0_device);	platform_device_register(&at32_tcb0_device);	platform_device_register(&at32_tcb1_device);	platform_device_register(&pio0_device);	platform_device_register(&pio1_device);	platform_device_register(&pio2_device);	platform_device_register(&pio3_device);	platform_device_register(&pio4_device);	return 0;}core_initcall(system_device_init);/* -------------------------------------------------------------------- *  PSIF * -------------------------------------------------------------------- */static struct resource atmel_psif0_resource[] __initdata = {	{		.start	= 0xffe03c00,		.end	= 0xffe03cff,		.flags	= IORESOURCE_MEM,	},	IRQ(18),};static struct clk atmel_psif0_pclk = {	.name		= "pclk",	.parent		= &pba_clk,	.mode		= pba_clk_mode,	.get_rate	= pba_clk_get_rate,	.index		= 15,};static struct resource atmel_psif1_resource[] __initdata = {	{		.start	= 0xffe03d00,		.end	= 0xffe03dff,		.flags	= IORESOURCE_MEM,	},	IRQ(18),};static struct clk atmel_psif1_pclk = {	.name		= "pclk",	.parent		= &pba_clk,	.mode		= pba_clk_mode,	.get_rate	= pba_clk_get_rate,	.index		= 15,};struct platform_device *__init at32_add_device_psif(unsigned int id){	struct platform_device *pdev;	u32 pin_mask;	if (!(id == 0 || id == 1))		return NULL;	pdev = platform_device_alloc("atmel_psif", id);	if (!pdev)		return NULL;	switch (id) {	case 0:		pin_mask  = (1 << 8) | (1 << 9); /* CLOCK & DATA */		if (platform_device_add_resources(pdev, atmel_psif0_resource,					ARRAY_SIZE(atmel_psif0_resource)))			goto err_add_resources;		atmel_psif0_pclk.dev = &pdev->dev;		select_peripheral(PIOA, pin_mask, PERIPH_A, 0);		break;	case 1:		pin_mask  = (1 << 11) | (1 << 12); /* CLOCK & DATA */		if (platform_device_add_resources(pdev, atmel_psif1_resource,					ARRAY_SIZE(atmel_psif1_resource)))			goto err_add_resources;		atmel_psif1_pclk.dev = &pdev->dev;		select_peripheral(PIOB, pin_mask, PERIPH_A, 0);		break;	default:		return NULL;	}	platform_device_add(pdev);	return pdev;err_add_resources:	platform_device_put(pdev);	return NULL;}/* -------------------------------------------------------------------- *  USART * -------------------------------------------------------------------- */static struct atmel_uart_data atmel_usart0_data = {	.use_dma_tx	= 1,	.use_dma_rx	= 1,};static struct resource atmel_usart0_resource[] = {	PBMEM(0xffe00c00),	IRQ(6),};DEFINE_DEV_DATA(atmel_usart, 0);DEV_CLK(usart, atmel_usart0, pba, 3);static struct atmel_uart_data atmel_usart1_data = {	.use_dma_tx	= 1,	.use_dma_rx	= 1,};static struct resource atmel_usart1_resource[] = {	PBMEM(0xffe01000),	IRQ(7),};DEFINE_DEV_DATA(atmel_usart, 1);DEV_CLK(usart, atmel_usart1, pba, 4);static struct atmel_uart_data atmel_usart2_data = {	.use_dma_tx	= 1,	.use_dma_rx	= 1,};static struct resource atmel_usart2_resource[] = {	PBMEM(0xffe01400),	IRQ(8),};DEFINE_DEV_DATA(atmel_usart, 2);DEV_CLK(usart, atmel_usart2, pba, 5);static struct atmel_uart_data atmel_usart3_data = {	.use_dma_tx	= 1,	.use_dma_rx	= 1,};static struct resource atmel_usart3_resource[] = {	PBMEM(0xffe01800),	IRQ(9),};DEFINE_DEV_DATA(atmel_usart, 3);DEV_CLK(usart, atmel_usart3, pba, 6);static inline void configure_usart0_pins(int flags){	u32 pin_mask = (1 << 8) | (1 << 9); /* RXD & TXD */	if (flags & ATMEL_USART_RTS)	pin_mask |= (1 << 6);	if (flags & ATMEL_USART_CTS)	pin_mask |= (1 << 7);	if (flags & ATMEL_USART_CLK)	pin_mask |= (1 << 10);	select_peripheral(PIOA, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);}static inline void configure_usart1_pins(int flags){	u32 pin_mask = (1 << 17) | (1 << 18); /* RXD & TXD */	if (flags & ATMEL_USART_RTS)	pin_mask |= (1 << 19);	if (flags & ATMEL_USART_CTS)	pin_mask |= (1 << 20);	if (flags & ATMEL_USART_CLK)	pin_mask |= (1 << 16);	select_peripheral(PIOA, pin_mask, PERIPH_A, AT32_GPIOF_PULLUP);}static inline void configure_usart2_pins(int flags){	u32 pin_mask = (1 << 26) | (1 << 27); /* RXD & TXD */	if (flags & ATMEL_USART_RTS)	pin_mask |= (1 << 30);	if (flags & ATMEL_USART_CTS)	pin_mask |= (1 << 29);	if (flags & ATMEL_USART_CLK)	pin_mask |= (1 << 28);	select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);}static inline void configure_usart3_pins(int flags){	u32 pin_mask = (1 << 18) | (1 << 17); /* RXD & TXD */	if (flags & ATMEL_USART_RTS)	pin_mask |= (1 << 16);	if (flags & ATMEL_USART_CTS)	pin_mask |= (1 << 15);	if (flags & ATMEL_USART_CLK)	pin_mask |= (1 << 19);	select_peripheral(PIOB, pin_mask, PERIPH_B, AT32_GPIOF_PULLUP);}static struct platform_device *__initdata at32_usarts[4];void __init at32_map_usart(unsigned int hw_id, unsigned int line, int flags){	struct platform_device *pdev;	struct atmel_uart_data *pdata;	switch (hw_id) {	case 0:		pdev = &atmel_usart0_device;		configure_usart0_pins(flags);		break;	case 1:		pdev = &atmel_usart1_device;		configure_usart1_pins(flags);		break;	case 2:		pdev = &atmel_usart2_device;		configure_usart2_pins(flags);		break;	case 3:		pdev = &atmel_usart3_device;		configure_usart3_pins(flags);		break;	default:		return;	}	if (PXSEG(pdev->resource[0].start) == P4SEG) {		/* Addresses in the P4 segment are permanently mapped 1:1 */		struct atmel_uart_data *data = pdev->dev.platform_data;		data->regs = (void __iomem *)pdev->resource[0].start;	}	pdev->id = line;	pdata = pdev->dev.platform_data;	pdata->num = line;	at32_usarts[line] = pdev;}struct platform_device *__init at32_add_device_usart(unsigned int id){	platform_device_register(at32_usarts[id]);	return at32_usarts[id];}void __init at32_setup_serial_console(unsigned int usart_id){	atmel_default_console_device = at32_usarts[usart_id];}/* -------------------------------------------------------------------- *  Ethernet * -------------------------------------------------------------------- */#ifdef CONFIG_CPU_AT32AP7000static struct macb_platform_data macb0_data;static struct resource macb0_resource[] = {	PBMEM(0xfff01800),	IRQ(25),};DEFINE_DEV_DATA(macb, 0);DEV_CLK(hclk, macb0, hsb, 8);DEV_CLK(pclk, macb0, pbb, 6);static struct macb_platform_data macb1_data;static struct resource macb1_resource[] = {	PBMEM(0xfff01c00),	IRQ(26),};DEFINE_DEV_DATA(macb, 1);DEV_CLK(hclk, macb1, hsb, 9);DEV_CLK(pclk, macb1, pbb, 7);struct platform_device *__initat32_add_device_eth(unsigned int id, struct macb_platform_data *data){	struct platform_device *pdev;	u32 pin_mask;	switch (id) {	case 0:		pdev = &macb0_device;		pin_mask  = (1 << 3);	/* TXD0 */		pin_mask |= (1 << 4);	/* TXD1 */		pin_mask |= (1 << 7);	/* TXEN */		pin_mask |= (1 << 8);	/* TXCK */		pin_mask |= (1 << 9);	/* RXD0 */		pin_mask |= (1 << 10);	/* RXD1 */		pin_mask |= (1 << 13);	/* RXER */		pin_mask |= (1 << 15);	/* RXDV */		pin_mask |= (1 << 16);	/* MDC  */		pin_mask |= (1 << 17);	/* MDIO */		if (!data->is_rmii) {			pin_mask |= (1 << 0);	/* COL  */			pin_mask |= (1 << 1);	/* CRS  */			pin_mask |= (1 << 2);	/* TXER */			pin_mask |= (1 << 5);	/* TXD2 */			pin_mask |= (1 << 6);	/* TXD3 */			pin_mask |= (1 << 11);	/* RXD2 */			pin_mask |= (1 << 12);	/* RXD3 */			pin_mask |= (1 << 14);	/* RXCK */#ifndef CONFIG_BOARD_MIMC200			pin_mask |= (1 << 18);	/* SPD  */#endif		}		select_peripheral(PIOC, pin_mask, PERIPH_A, 0);		break;	case 1:		pdev = &macb1_device;		pin_mask  = (1 << 13);	/* TXD0 */		pin_mask |= (1 << 14);	/* TXD1 */		pin_mask |= (1 << 11);	/* TXEN */		pin_mask |= (1 << 12);	/* TXCK */		pin_mask |= (1 << 10);	/* RXD0 */		pin_mask |= (1 << 6);	/* RXD1 */		pin_mask |= (1 << 5);	/* RXER */		pin_mask |= (1 << 4);	/* RXDV */		pin_mask |= (1 << 3);	/* MDC  */		pin_mask |= (1 << 2);	/* MDIO */#ifndef CONFIG_BOARD_MIMC200		if (!data->is_rmii)			pin_mask |= (1 << 15);	/* SPD  */#endif		select_peripheral(PIOD, pin_mask, PERIPH_B, 0);		if (!data->is_rmii) {			pin_mask  = (1 << 19);	/* COL  */			pin_mask |= (1 << 23);	/* CRS  */			pin_mask |= (1 << 26);	/* TXER */			pin_mask |= (1 << 27);	/* TXD2 */			pin_mask |= (1 << 28);	/* TXD3 */			pin_mask |= (1 << 29);	/* RXD2 */			pin_mask |= (1 << 30);	/* RXD3 */			pin_mask |= (1 << 24);	/* RXCK */			select_peripheral(PIOC, pin_mask, PERIPH_B, 0);		}		break;	default:		return NULL;	}	memcpy(pdev->dev.platform_data, data, sizeof(struct macb_platform_data));	platform_device_register(pdev);	return pdev;}#endif/* -------------------------------------------------------------------- *  SPI * -------------------------------------------------------------------- */static struct resource atmel_spi0_resource[] = {	PBMEM(0xffe00000),	IRQ(3),};DEFINE_DEV(atmel_spi, 0);DEV_CLK(spi_clk, atmel_spi0, pba, 0);static struct resource atmel_spi1_resource[] = {	PBMEM(0xffe00400),	IRQ(4),};DEFINE_DEV(atmel_spi, 1);DEV_CLK(spi_clk, atmel_spi1, pba, 1);void __initat32_spi_setup_slaves(unsigned int bus_num, struct spi_board_info *b, unsigned int n){	/*	 * Manage the chipselects as GPIOs, normally using the same pins	 * the SPI controller expects; but boards can use other pins.	 */	static u8 __initdata spi_pins[][4] = {		{ GPIO_PIN_PA(3), GPIO_PIN_PA(4),		  GPIO_PIN_PA(5), GPIO_PIN_PA(20) },		{ GPIO_PIN_PB(2), GPIO_PIN_PB(3),		  GPIO_PIN_PB(4), GPIO_PIN_PA(27) },	};	unsigned int pin, mode;	/* There are only 2 SPI controllers */	if (bus_num > 1)		return;	for (; n; n--, b++) {		b->bus_num = bus_num;		if (b->chip_select >= 4)			continue;		pin = (unsigned)b->controller_data;		if (!pin) {			pin = spi_pins[bus_num][b->chip_select];			b->controller_data = (void *)pin;		}		mode = AT32_GPIOF_OUTPUT;		if (!(b->mode & SPI_CS_HIGH))			mode |= AT32_GPIOF_HIGH;		at32_select_gpio(pin, mode);	}}struct platform_device *__initat32_add_device_spi(unsigned int id, struct spi_board_info *b, unsigned int n){	struct platform_device *pdev;	u32 pin_mask;	switch (id) {	case 0:		pdev = &atmel_spi0_device;		pin_mask  = (1 << 1) | (1 << 2);	/* MOSI & SCK */		/* pullup MISO so a level is always defined */		select_peripheral(PIOA, (1 << 0), PERIPH_A, AT32_GPIOF_PULLUP);		select_peripheral(PIOA, pin_mask, PERIPH_A, 0);		at32_spi_setup_slaves(0, b, n);		break;	case 1:		pdev = &atmel_spi1_device;		pin_mask  = (1 << 1) | (1 << 5);	/* MOSI */		/* pullup MISO so a level is always defined */		select_peripheral(PIOB, (1 << 0), PERIPH_B, AT32_GPIOF_PULLUP);		select_peripheral(PIOB, pin_mask, PERIPH_B, 0);		at32_spi_setup_slaves(1, b, n);		break;	default:		return NULL;	}	spi_register_board_info(b, n);	platform_device_register(pdev);	return pdev;}/* -------------------------------------------------------------------- *  TWI * -------------------------------------------------------------------- */static struct resource atmel_twi0_resource[] __initdata = {	PBMEM(0xffe00800),	IRQ(5),};static struct clk atmel_twi0_pclk = {	.name		= "twi_pclk",	.parent		= &pba_clk,	.mode		= pba_clk_mode,	.get_rate	= pba_clk_get_rate,	.index		= 2,};struct platform_device *__init at32_add_device_twi(unsigned int id,						    struct i2c_board_info *b,						    unsigned int n){	struct platform_device *pdev;	u32 pin_mask;	if (id != 0)		return NULL;	pdev = platform_device_alloc("atmel_twi", id);	if (!pdev)		return NULL;	if (platform_device_add_resources(pdev, atmel_twi0_resource,				ARRAY_SIZE(atmel_twi0_resource)))		goto err_add_resources;	pin_mask  = (1 << 6) | (1 << 7);	/* SDA & SDL */	select_peripheral(PIOA, pin_mask, PERIPH_A, 0);	atmel_twi0_pclk.dev = &pdev->dev;	if (b)		i2c_register_board_info(id, b, n);	platform_device_add(pdev);	return pdev;err_add_resources:	platform_device_put(pdev);	return NULL;}/* -------------------------------------------------------------------- * MMC * -------------------------------------------------------------------- */static struct resource atmel_mci0_resource[] __initdata = {	PBMEM(0xfff02400),	IRQ(28),};static struct clk atmel_mci0_pclk = {	.name		= "mci_clk",	.parent		= &pbb_clk,	.mode		= pbb_clk_mode,	.get_rate	= pbb_clk_get_rate,	.index		= 9,};struct platform_device *__initat32_add_device_mci(unsigned int id, struct mci_platform_data *data){	struct platform_device		*pdev;	struct mci_dma_data	        *slave;	u32				pioa_mask;	u32				piob_mask;	if (id != 0 || !data)		return NULL;	/* Must have at least one usable slot */	if (!data->slot[0].bus_width && !data->slot[1].bus_width)		return NULL;	pdev = platform_device_alloc("atmel_mci", id);	if (!pdev)		goto fail;	if (platform_device_add_resources(pdev, atmel_mci0_resource,				ARRAY_SIZE(atmel_mci0_resource)))		goto fail;	slave = kzalloc(sizeof(struct mci_dma_data), GFP_KERNEL);	if (!slave)		goto fail;	slave->sdata.dma_dev = &dw_dmac0_device.dev;	slave->sdata.cfg_hi = (DWC_CFGH_SRC_PER(0)				| DWC_CFGH_DST_PER(1));	slave->sdata.cfg_lo &= ~(DWC_CFGL_HS_DST_POL				| DWC_CFGL_HS_SRC_POL);	data->dma_slave = slave;	if (platform_device_add_data(pdev, data,				sizeof(struct mci_platform_data)))		goto fail_free;	/* CLK line is common to both slots */	pioa_mask = 1 << 10;	switch (data->slot[0].bus_width) {	case 4:		pioa_mask |= 1 << 13;		/* DATA1 */		pioa_mask |= 1 << 14;		/* DATA2 */		pioa_mask |= 1 << 15;		/* DATA3 */		/* fall through */	case 1:		pioa_mask |= 1 << 11;		/* CMD	 */		pioa_mask |= 1 << 12;		/* DATA0 */		if (gpio_is_valid(data->slot[0].detect_pin))			at32_select_gpio(data->slot[0].detect_pin, 0);		if (gpio_is_valid(data->slot[0].wp_pin))			at32_select_gpio(data->slot[0].wp_pin, 0);		break;	case 0:		/* Slot is unused */		break;	default:		goto fail_free;	}	select_peripheral(PIOA, pioa_mask, PERIPH_A, 0);	piob_mask = 0;	switch (data->slot[1].bus_width) {	case 4:		piob_mask |= 1 <<  8;		/* DATA1 */		piob_mask |= 1 <<  9;		/* DATA2 */		piob_mask |= 1 << 10;		/* DATA3 */		/* fall through */	case 1:		piob_mask |= 1 <<  6;		/* CMD	 */		piob_mask |= 1 <<  7;		/* DATA0 */		select_peripheral(PIOB, piob_mask, PERIPH_B, 0);		if (gpio_is_valid(data->slot[1].detect_pin))			at32_select_gpio(data->slot[1].detect_pin, 0);		if (gpio_is_valid(data->slot[1].wp_pin))			at32_select_gpio(data->slot[1].wp_pin, 0);		break;	case 0:		/* Slot is unused */		break;
 |