2 * linux/arch/arm/mach-pxa/mfp.c
4 * PXA3xx Multi-Function Pin Support
6 * Copyright (C) 2007 Marvell Internation Ltd.
8 * 2007-08-21: eric miao <eric.miao@marvell.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
20 #include <linux/sysdev.h>
22 #include <asm/hardware.h>
23 #include <asm/arch/mfp.h>
24 #include <asm/arch/mfp-pxa3xx.h>
26 /* mfp_spin_lock is used to ensure that MFP register configuration
27 * (most likely a read-modify-write operation) is atomic, and that
28 * mfp_table[] is consistent
30 static DEFINE_SPINLOCK(mfp_spin_lock);
32 static void __iomem *mfpr_mmio_base = (void __iomem *)&__REG(MFPR_BASE);
34 struct pxa3xx_mfp_pin {
35 unsigned long config; /* -1 for not configured */
36 unsigned long mfpr_off; /* MFPRxx Register offset */
37 unsigned long mfpr_run; /* Run-Mode Register Value */
38 unsigned long mfpr_lpm; /* Low Power Mode Register Value */
41 static struct pxa3xx_mfp_pin mfp_table[MFP_PIN_MAX];
43 /* mapping of MFP_LPM_* definitions to MFPR_LPM_* register bits */
44 const static unsigned long mfpr_lpm[] = {
53 /* mapping of MFP_PULL_* definitions to MFPR_PULL_* register bits */
54 const static unsigned long mfpr_pull[] = {
61 /* mapping of MFP_LPM_EDGE_* definitions to MFPR_EDGE_* register bits */
62 const static unsigned long mfpr_edge[] = {
69 #define mfpr_readl(off) \
70 __raw_readl(mfpr_mmio_base + (off))
72 #define mfpr_writel(off, val) \
73 __raw_writel(val, mfpr_mmio_base + (off))
75 #define mfp_configured(p) ((p)->config != -1)
78 * perform a read-back of any MFPR register to make sure the
79 * previous writings are finished
81 #define mfpr_sync() (void)__raw_readl(mfpr_mmio_base + 0)
83 static inline void __mfp_config_run(struct pxa3xx_mfp_pin *p)
85 if (mfp_configured(p))
86 mfpr_writel(p->mfpr_off, p->mfpr_run);
89 static inline void __mfp_config_lpm(struct pxa3xx_mfp_pin *p)
91 if (mfp_configured(p)) {
92 unsigned long mfpr_clr = (p->mfpr_run & ~MFPR_EDGE_BOTH) | MFPR_EDGE_CLEAR;
93 if (mfpr_clr != p->mfpr_run)
94 mfpr_writel(p->mfpr_off, mfpr_clr);
95 if (p->mfpr_lpm != mfpr_clr)
96 mfpr_writel(p->mfpr_off, p->mfpr_lpm);
100 void pxa3xx_mfp_config(unsigned long *mfp_cfgs, int num)
105 spin_lock_irqsave(&mfp_spin_lock, flags);
107 for (i = 0; i < num; i++, mfp_cfgs++) {
108 unsigned long tmp, c = *mfp_cfgs;
109 struct pxa3xx_mfp_pin *p;
110 int pin, af, drv, lpm, edge, pull;
113 BUG_ON(pin >= MFP_PIN_MAX);
118 lpm = MFP_LPM_STATE(c);
119 edge = MFP_LPM_EDGE(c);
122 /* run-mode pull settings will conflict with MFPR bits of
123 * low power mode state, calculate mfpr_run and mfpr_lpm
124 * individually if pull != MFP_PULL_NONE
126 tmp = MFPR_AF_SEL(af) | MFPR_DRIVE(drv);
128 if (likely(pull == MFP_PULL_NONE)) {
129 p->mfpr_run = tmp | mfpr_lpm[lpm] | mfpr_edge[edge];
130 p->mfpr_lpm = p->mfpr_run;
132 p->mfpr_lpm = tmp | mfpr_lpm[lpm] | mfpr_edge[edge];
133 p->mfpr_run = tmp | mfpr_pull[pull];
136 p->config = c; __mfp_config_run(p);
140 spin_unlock_irqrestore(&mfp_spin_lock, flags);
143 unsigned long pxa3xx_mfp_read(int mfp)
145 unsigned long val, flags;
147 BUG_ON(mfp >= MFP_PIN_MAX);
149 spin_lock_irqsave(&mfp_spin_lock, flags);
150 val = mfpr_readl(mfp_table[mfp].mfpr_off);
151 spin_unlock_irqrestore(&mfp_spin_lock, flags);
156 void pxa3xx_mfp_write(int mfp, unsigned long val)
160 BUG_ON(mfp >= MFP_PIN_MAX);
162 spin_lock_irqsave(&mfp_spin_lock, flags);
163 mfpr_writel(mfp_table[mfp].mfpr_off, val);
165 spin_unlock_irqrestore(&mfp_spin_lock, flags);
168 void __init pxa3xx_mfp_init_addr(struct pxa3xx_mfp_addr_map *map)
170 struct pxa3xx_mfp_addr_map *p;
171 unsigned long offset, flags;
174 spin_lock_irqsave(&mfp_spin_lock, flags);
176 for (p = map; p->start != MFP_PIN_INVALID; p++) {
181 mfp_table[i].mfpr_off = offset;
182 mfp_table[i].mfpr_run = 0;
183 mfp_table[i].mfpr_lpm = 0;
185 } while ((i <= p->end) && (p->end != -1));
188 spin_unlock_irqrestore(&mfp_spin_lock, flags);
191 void __init pxa3xx_init_mfp(void)
195 for (i = 0; i < ARRAY_SIZE(mfp_table); i++)
196 mfp_table[i].config = -1;
201 * Configure the MFPs appropriately for suspend/resume.
202 * FIXME: this should probably depend on which system state we're
203 * entering - for instance, we might not want to place MFP pins in
204 * a pull-down mode if they're an active low chip select, and we're
205 * just entering standby.
207 static int pxa3xx_mfp_suspend(struct sys_device *d, pm_message_t state)
211 for (pin = 0; pin < ARRAY_SIZE(mfp_table); pin++) {
212 struct pxa3xx_mfp_pin *p = &mfp_table[pin];
218 static int pxa3xx_mfp_resume(struct sys_device *d)
222 for (pin = 0; pin < ARRAY_SIZE(mfp_table); pin++) {
223 struct pxa3xx_mfp_pin *p = &mfp_table[pin];
229 static struct sysdev_class mfp_sysclass = {
230 set_kset_name("mfp"),
231 .suspend = pxa3xx_mfp_suspend,
232 .resume = pxa3xx_mfp_resume,
235 static struct sys_device mfp_device = {
237 .cls = &mfp_sysclass,
240 static int __init mfp_init_devicefs(void)
242 sysdev_class_register(&mfp_sysclass);
243 return sysdev_register(&mfp_device);
245 device_initcall(mfp_init_devicefs);