eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 1 | /* |
| 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 miao | e9bba8e | 2007-10-30 08:01:38 +0100 | [diff] [blame] | 8 | * 2007-08-21: eric miao <eric.miao@marvell.com> |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 9 | * 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 miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 23 | #include <asm/arch/mfp-pxa3xx.h> |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 24 | |
| 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 | */ |
| 29 | static DEFINE_SPINLOCK(mfp_spin_lock); |
| 30 | |
| 31 | static void __iomem *mfpr_mmio_base = (void __iomem *)&__REG(MFPR_BASE); |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 32 | |
| 33 | struct 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 miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 40 | static struct pxa3xx_mfp_pin mfp_table[MFP_PIN_MAX]; |
| 41 | |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 42 | /* mapping of MFP_LPM_* definitions to MFPR_LPM_* register bits */ |
| 43 | const 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 */ |
| 53 | const 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 */ |
| 61 | const static unsigned long mfpr_edge[] = { |
| 62 | MFPR_EDGE_NONE, |
| 63 | MFPR_EDGE_RISE, |
| 64 | MFPR_EDGE_FALL, |
| 65 | MFPR_EDGE_BOTH, |
| 66 | }; |
| 67 | |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 68 | #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 miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 74 | #define mfp_configured(p) ((p)->config != -1) |
| 75 | |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 76 | /* |
| 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 miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 82 | static inline void __mfp_config_run(struct pxa3xx_mfp_pin *p) |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 83 | { |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 84 | if (mfp_configured(p)) |
| 85 | mfpr_writel(p->mfpr_off, p->mfpr_run); |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 86 | } |
| 87 | |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 88 | static inline void __mfp_config_lpm(struct pxa3xx_mfp_pin *p) |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 89 | { |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 90 | if (mfp_configured(p) && p->mfpr_lpm != p->mfpr_run) |
| 91 | mfpr_writel(p->mfpr_off, p->mfpr_lpm); |
| 92 | } |
| 93 | |
| 94 | void pxa3xx_mfp_config(unsigned long *mfp_cfgs, int num) |
| 95 | { |
| 96 | unsigned long flags; |
| 97 | int i; |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 98 | |
| 99 | spin_lock_irqsave(&mfp_spin_lock, flags); |
| 100 | |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 101 | 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 miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 105 | |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 106 | pin = MFP_PIN(c); |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 107 | BUG_ON(pin >= MFP_PIN_MAX); |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 108 | p = &mfp_table[pin]; |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 109 | |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 110 | 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 miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | mfpr_sync(); |
| 134 | spin_unlock_irqrestore(&mfp_spin_lock, flags); |
| 135 | } |
| 136 | |
| 137 | unsigned 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 | |
| 150 | void 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 miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 162 | void __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 miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 176 | mfp_table[i].mfpr_run = 0; |
| 177 | mfp_table[i].mfpr_lpm = 0; |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 178 | offset += 4; i++; |
| 179 | } while ((i <= p->end) && (p->end != -1)); |
| 180 | } |
| 181 | |
| 182 | spin_unlock_irqrestore(&mfp_spin_lock, flags); |
| 183 | } |
| 184 | |
| 185 | void __init pxa3xx_init_mfp(void) |
| 186 | { |
eric miao | 7f7c8a6 | 2008-01-03 11:25:56 +0800 | [diff] [blame^] | 187 | int i; |
| 188 | |
| 189 | for (i = 0; i < ARRAY_SIZE(mfp_table); i++) |
| 190 | mfp_table[i].config = -1; |
eric miao | 2c8086a | 2007-09-11 19:13:17 -0700 | [diff] [blame] | 191 | } |