blob: c66b1cd7df64d6a520e9484ed5fe454473600d06 [file] [log] [blame]
eric miao2c8086a2007-09-11 19:13:17 -07001/*
2 * linux/arch/arm/mach-pxa/mfp.c
3 *
4 * PXA3xx Multi-Function Pin Support
5 *
6 * Copyright (C) 2007 Marvell Internation Ltd.
7 *
eric miaoe9bba8e2007-10-30 08:01:38 +01008 * 2007-08-21: eric miao <eric.miao@marvell.com>
eric miao2c8086a2007-09-11 19:13:17 -07009 * initial version
10 *
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.
14 */
15
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/io.h>
20
21#include <asm/hardware.h>
22#include <asm/arch/mfp.h>
eric miao7f7c8a62008-01-03 11:25:56 +080023#include <asm/arch/mfp-pxa3xx.h>
eric miao2c8086a2007-09-11 19:13:17 -070024
25/* mfp_spin_lock is used to ensure that MFP register configuration
26 * (most likely a read-modify-write operation) is atomic, and that
27 * mfp_table[] is consistent
28 */
29static DEFINE_SPINLOCK(mfp_spin_lock);
30
31static void __iomem *mfpr_mmio_base = (void __iomem *)&__REG(MFPR_BASE);
eric miao7f7c8a62008-01-03 11:25:56 +080032
33struct pxa3xx_mfp_pin {
34 unsigned long config; /* -1 for not configured */
35 unsigned long mfpr_off; /* MFPRxx Register offset */
36 unsigned long mfpr_run; /* Run-Mode Register Value */
37 unsigned long mfpr_lpm; /* Low Power Mode Register Value */
38};
39
eric miao2c8086a2007-09-11 19:13:17 -070040static struct pxa3xx_mfp_pin mfp_table[MFP_PIN_MAX];
41
eric miao7f7c8a62008-01-03 11:25:56 +080042/* mapping of MFP_LPM_* definitions to MFPR_LPM_* register bits */
43const static unsigned long mfpr_lpm[] = {
44 MFPR_LPM_INPUT,
45 MFPR_LPM_DRIVE_LOW,
46 MFPR_LPM_DRIVE_HIGH,
47 MFPR_LPM_PULL_LOW,
48 MFPR_LPM_PULL_HIGH,
49 MFPR_LPM_FLOAT,
50};
51
52/* mapping of MFP_PULL_* definitions to MFPR_PULL_* register bits */
53const static unsigned long mfpr_pull[] = {
54 MFPR_PULL_NONE,
55 MFPR_PULL_LOW,
56 MFPR_PULL_HIGH,
57 MFPR_PULL_BOTH,
58};
59
60/* mapping of MFP_LPM_EDGE_* definitions to MFPR_EDGE_* register bits */
61const static unsigned long mfpr_edge[] = {
62 MFPR_EDGE_NONE,
63 MFPR_EDGE_RISE,
64 MFPR_EDGE_FALL,
65 MFPR_EDGE_BOTH,
66};
67
eric miao2c8086a2007-09-11 19:13:17 -070068#define mfpr_readl(off) \
69 __raw_readl(mfpr_mmio_base + (off))
70
71#define mfpr_writel(off, val) \
72 __raw_writel(val, mfpr_mmio_base + (off))
73
eric miao7f7c8a62008-01-03 11:25:56 +080074#define mfp_configured(p) ((p)->config != -1)
75
eric miao2c8086a2007-09-11 19:13:17 -070076/*
77 * perform a read-back of any MFPR register to make sure the
78 * previous writings are finished
79 */
80#define mfpr_sync() (void)__raw_readl(mfpr_mmio_base + 0)
81
eric miao7f7c8a62008-01-03 11:25:56 +080082static inline void __mfp_config_run(struct pxa3xx_mfp_pin *p)
eric miao2c8086a2007-09-11 19:13:17 -070083{
eric miao7f7c8a62008-01-03 11:25:56 +080084 if (mfp_configured(p))
85 mfpr_writel(p->mfpr_off, p->mfpr_run);
eric miao2c8086a2007-09-11 19:13:17 -070086}
87
eric miao7f7c8a62008-01-03 11:25:56 +080088static inline void __mfp_config_lpm(struct pxa3xx_mfp_pin *p)
eric miao2c8086a2007-09-11 19:13:17 -070089{
eric miao7f7c8a62008-01-03 11:25:56 +080090 if (mfp_configured(p) && p->mfpr_lpm != p->mfpr_run)
91 mfpr_writel(p->mfpr_off, p->mfpr_lpm);
92}
93
94void pxa3xx_mfp_config(unsigned long *mfp_cfgs, int num)
95{
96 unsigned long flags;
97 int i;
eric miao2c8086a2007-09-11 19:13:17 -070098
99 spin_lock_irqsave(&mfp_spin_lock, flags);
100
eric miao7f7c8a62008-01-03 11:25:56 +0800101 for (i = 0; i < num; i++, mfp_cfgs++) {
102 unsigned long tmp, c = *mfp_cfgs;
103 struct pxa3xx_mfp_pin *p;
104 int pin, af, drv, lpm, edge, pull;
eric miao2c8086a2007-09-11 19:13:17 -0700105
eric miao7f7c8a62008-01-03 11:25:56 +0800106 pin = MFP_PIN(c);
eric miao2c8086a2007-09-11 19:13:17 -0700107 BUG_ON(pin >= MFP_PIN_MAX);
eric miao7f7c8a62008-01-03 11:25:56 +0800108 p = &mfp_table[pin];
eric miao2c8086a2007-09-11 19:13:17 -0700109
eric miao7f7c8a62008-01-03 11:25:56 +0800110 af = MFP_AF(c);
111 drv = MFP_DS(c);
112 lpm = MFP_LPM_STATE(c);
113 edge = MFP_LPM_EDGE(c);
114 pull = MFP_PULL(c);
115
116 /* run-mode pull settings will conflict with MFPR bits of
117 * low power mode state, calculate mfpr_run and mfpr_lpm
118 * individually if pull != MFP_PULL_NONE
119 */
120 tmp = MFPR_AF_SEL(af) | MFPR_DRIVE(drv);
121
122 if (likely(pull == MFP_PULL_NONE)) {
123 p->mfpr_run = tmp | mfpr_lpm[lpm] | mfpr_edge[edge];
124 p->mfpr_lpm = p->mfpr_run;
125 } else {
126 p->mfpr_lpm = tmp | mfpr_lpm[lpm] | mfpr_edge[edge];
127 p->mfpr_run = tmp | mfpr_pull[pull];
128 }
129
130 p->config = c; __mfp_config_run(p);
eric miao2c8086a2007-09-11 19:13:17 -0700131 }
132
133 mfpr_sync();
134 spin_unlock_irqrestore(&mfp_spin_lock, flags);
135}
136
137unsigned long pxa3xx_mfp_read(int mfp)
138{
139 unsigned long val, flags;
140
141 BUG_ON(mfp >= MFP_PIN_MAX);
142
143 spin_lock_irqsave(&mfp_spin_lock, flags);
144 val = mfpr_readl(mfp_table[mfp].mfpr_off);
145 spin_unlock_irqrestore(&mfp_spin_lock, flags);
146
147 return val;
148}
149
150void pxa3xx_mfp_write(int mfp, unsigned long val)
151{
152 unsigned long flags;
153
154 BUG_ON(mfp >= MFP_PIN_MAX);
155
156 spin_lock_irqsave(&mfp_spin_lock, flags);
157 mfpr_writel(mfp_table[mfp].mfpr_off, val);
158 mfpr_sync();
159 spin_unlock_irqrestore(&mfp_spin_lock, flags);
160}
161
eric miao2c8086a2007-09-11 19:13:17 -0700162void __init pxa3xx_mfp_init_addr(struct pxa3xx_mfp_addr_map *map)
163{
164 struct pxa3xx_mfp_addr_map *p;
165 unsigned long offset, flags;
166 int i;
167
168 spin_lock_irqsave(&mfp_spin_lock, flags);
169
170 for (p = map; p->start != MFP_PIN_INVALID; p++) {
171 offset = p->offset;
172 i = p->start;
173
174 do {
175 mfp_table[i].mfpr_off = offset;
eric miao7f7c8a62008-01-03 11:25:56 +0800176 mfp_table[i].mfpr_run = 0;
177 mfp_table[i].mfpr_lpm = 0;
eric miao2c8086a2007-09-11 19:13:17 -0700178 offset += 4; i++;
179 } while ((i <= p->end) && (p->end != -1));
180 }
181
182 spin_unlock_irqrestore(&mfp_spin_lock, flags);
183}
184
185void __init pxa3xx_init_mfp(void)
186{
eric miao7f7c8a62008-01-03 11:25:56 +0800187 int i;
188
189 for (i = 0; i < ARRAY_SIZE(mfp_table); i++)
190 mfp_table[i].config = -1;
eric miao2c8086a2007-09-11 19:13:17 -0700191}