1 /*======================================================================
3 Device driver for the PCMCIA control functionality of PXA2xx
6 The contents of this file may be used under the
7 terms of the GNU Public License version 2 (the "GPL")
9 (c) Ian Molton (spyro@f2s.com) 2003
10 (c) Stefan Eletzhofer (stefan.eletzhofer@inquant.de) 2003,4
12 derived from sa11xx_base.c
14 Portions created by John G. Dorsey are
15 Copyright (C) 1999 John G. Dorsey.
17 ======================================================================*/
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/config.h>
22 #include <linux/cpufreq.h>
23 #include <linux/ioport.h>
24 #include <linux/kernel.h>
25 #include <linux/notifier.h>
26 #include <linux/spinlock.h>
28 #include <asm/hardware.h>
31 #include <asm/system.h>
33 #include <pcmcia/cs_types.h>
34 #include <pcmcia/ss.h>
35 #include <pcmcia/bulkmem.h>
36 #include <pcmcia/cistpl.h>
38 #include "cs_internal.h"
39 #include "soc_common.h"
40 #include "pxa2xx_base.h"
43 #define MCXX_SETUP_MASK (0x7f)
44 #define MCXX_ASST_MASK (0x1f)
45 #define MCXX_HOLD_MASK (0x3f)
46 #define MCXX_SETUP_SHIFT (0)
47 #define MCXX_ASST_SHIFT (7)
48 #define MCXX_HOLD_SHIFT (14)
50 static inline u_int pxa2xx_mcxx_hold(u_int pcmcia_cycle_ns,
53 u_int code = pcmcia_cycle_ns * mem_clk_10khz;
54 return (code / 300000) + ((code % 300000) ? 1 : 0) - 1;
57 static inline u_int pxa2xx_mcxx_asst(u_int pcmcia_cycle_ns,
60 u_int code = pcmcia_cycle_ns * mem_clk_10khz;
61 return (code / 300000) + ((code % 300000) ? 1 : 0) - 1;
64 static inline u_int pxa2xx_mcxx_setup(u_int pcmcia_cycle_ns,
67 u_int code = pcmcia_cycle_ns * mem_clk_10khz;
68 return (code / 100000) + ((code % 100000) ? 1 : 0) - 1;
71 /* This function returns the (approximate) command assertion period, in
72 * nanoseconds, for a given CPU clock frequency and MCXX_ASST value:
74 static inline u_int pxa2xx_pcmcia_cmd_time(u_int mem_clk_10khz,
75 u_int pcmcia_mcxx_asst)
77 return (300000 * (pcmcia_mcxx_asst + 1) / mem_clk_10khz);
80 static int pxa2xx_pcmcia_set_mcmem( int sock, int speed, int clock )
82 MCMEM(sock) = ((pxa2xx_mcxx_setup(speed, clock)
83 & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
84 | ((pxa2xx_mcxx_asst(speed, clock)
85 & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
86 | ((pxa2xx_mcxx_hold(speed, clock)
87 & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
92 static int pxa2xx_pcmcia_set_mcio( int sock, int speed, int clock )
94 MCIO(sock) = ((pxa2xx_mcxx_setup(speed, clock)
95 & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
96 | ((pxa2xx_mcxx_asst(speed, clock)
97 & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
98 | ((pxa2xx_mcxx_hold(speed, clock)
99 & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
104 static int pxa2xx_pcmcia_set_mcatt( int sock, int speed, int clock )
106 MCATT(sock) = ((pxa2xx_mcxx_setup(speed, clock)
107 & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
108 | ((pxa2xx_mcxx_asst(speed, clock)
109 & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
110 | ((pxa2xx_mcxx_hold(speed, clock)
111 & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
116 static int pxa2xx_pcmcia_set_mcxx(struct soc_pcmcia_socket *skt, unsigned int lclk)
118 struct soc_pcmcia_timing timing;
121 soc_common_pcmcia_get_timing(skt, &timing);
123 pxa2xx_pcmcia_set_mcmem(sock, timing.mem, lclk);
124 pxa2xx_pcmcia_set_mcatt(sock, timing.attr, lclk);
125 pxa2xx_pcmcia_set_mcio(sock, timing.io, lclk);
130 static int pxa2xx_pcmcia_set_timing(struct soc_pcmcia_socket *skt)
132 unsigned int lclk = get_lclk_frequency_10khz();
133 return pxa2xx_pcmcia_set_mcxx(skt, lclk);
136 int pxa2xx_drv_pcmcia_probe(struct device *dev)
139 struct pcmcia_low_level *ops;
142 if (!dev || !dev->platform_data)
145 ops = (struct pcmcia_low_level *)dev->platform_data;
149 /* Setup GPIOs for PCMCIA/CF alternate function mode.
151 * It would be nice if set_GPIO_mode included support
152 * for driving GPIO outputs to default high/low state
153 * before programming GPIOs as outputs. Setting GPIO
154 * outputs to default high/low state via GPSR/GPCR
155 * before defining them as outputs should reduce
156 * the possibility of glitching outputs during GPIO
157 * setup. This of course assumes external terminators
158 * are present to hold GPIOs in a defined state.
160 * In the meantime, setup default state of GPIO
161 * outputs before we enable them as outputs.
164 GPSR(GPIO48_nPOE) = GPIO_bit(GPIO48_nPOE) |
165 GPIO_bit(GPIO49_nPWE) |
166 GPIO_bit(GPIO50_nPIOR) |
167 GPIO_bit(GPIO51_nPIOW) |
168 GPIO_bit(GPIO52_nPCE_1) |
169 GPIO_bit(GPIO53_nPCE_2);
171 pxa_gpio_mode(GPIO48_nPOE_MD);
172 pxa_gpio_mode(GPIO49_nPWE_MD);
173 pxa_gpio_mode(GPIO50_nPIOR_MD);
174 pxa_gpio_mode(GPIO51_nPIOW_MD);
175 pxa_gpio_mode(GPIO52_nPCE_1_MD);
176 pxa_gpio_mode(GPIO53_nPCE_2_MD);
177 pxa_gpio_mode(GPIO54_pSKTSEL_MD); /* REVISIT: s/b dependent on num sockets */
178 pxa_gpio_mode(GPIO55_nPREG_MD);
179 pxa_gpio_mode(GPIO56_nPWAIT_MD);
180 pxa_gpio_mode(GPIO57_nIOIS16_MD);
182 /* Provide our PXA2xx specific timing routines. */
183 ops->set_timing = pxa2xx_pcmcia_set_timing;
185 ret = soc_common_drv_pcmcia_probe(dev, ops, first, nr);
189 * We have at least one socket, so set MECR:CIT
194 /* Set MECR:NOS (Number Of Sockets) */
203 EXPORT_SYMBOL(pxa2xx_drv_pcmcia_probe);
205 static int pxa2xx_drv_pcmcia_suspend(struct device *dev, u32 state, u32 level)
208 if (level == SUSPEND_SAVE_STATE)
209 ret = pcmcia_socket_dev_suspend(dev, state);
213 static int pxa2xx_drv_pcmcia_resume(struct device *dev, u32 level)
216 if (level == RESUME_RESTORE_STATE)
217 ret = pcmcia_socket_dev_resume(dev);
221 static struct device_driver pxa2xx_pcmcia_driver = {
222 .probe = pxa2xx_drv_pcmcia_probe,
223 .remove = soc_common_drv_pcmcia_remove,
224 .suspend = pxa2xx_drv_pcmcia_suspend,
225 .resume = pxa2xx_drv_pcmcia_resume,
226 .name = "pxa2xx-pcmcia",
227 .bus = &platform_bus_type,
230 #ifdef CONFIG_CPU_FREQ
233 * When pxa2xx_pcmcia_notifier() decides that a MC{IO,MEM,ATT} adjustment (due
234 * to a core clock frequency change) is needed, this routine establishes
235 * new values consistent with the clock speed `clock'.
237 static void pxa2xx_pcmcia_update_mcxx(unsigned int clock)
239 struct soc_pcmcia_socket *skt;
241 down(&soc_sockets_lock);
242 list_for_each_entry(skt, &soc_sockets, node) {
243 pxa2xx_pcmcia_set_mcxx(skt, clock);
245 up(&soc_sockets_lock);
249 * When changing the processor L clock frequency, it is necessary
250 * to adjust the MCXX timings accordingly. We've recorded the timings
251 * requested by Card Services, so this is just a matter of finding
252 * out what our current speed is, and then recomputing the new MCXX
255 * Returns: 0 on success, -1 on error
258 pxa2xx_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
260 struct cpufreq_freqs *freqs = data;
262 #warning "it's not clear if this is right since the core CPU (N) clock has no effect on the memory (L) clock"
264 case CPUFREQ_PRECHANGE:
265 if (freqs->new > freqs->old) {
266 debug( 2, "new frequency %u.%uMHz > %u.%uMHz, "
268 freqs->new / 1000, (freqs->new / 100) % 10,
269 freqs->old / 1000, (freqs->old / 100) % 10);
270 pxa2xx_pcmcia_update_mcxx(freqs->new);
274 case CPUFREQ_POSTCHANGE:
275 if (freqs->new < freqs->old) {
276 debug( 2, "new frequency %u.%uMHz < %u.%uMHz, "
278 freqs->new / 1000, (freqs->new / 100) % 10,
279 freqs->old / 1000, (freqs->old / 100) % 10);
280 pxa2xx_pcmcia_update_mcxx(freqs->new);
288 static struct notifier_block pxa2xx_pcmcia_notifier_block = {
289 .notifier_call = pxa2xx_pcmcia_notifier
292 static int __init pxa2xx_pcmcia_cpufreq_init(void)
296 ret = cpufreq_register_notifier(&pxa2xx_pcmcia_notifier_block,
297 CPUFREQ_TRANSITION_NOTIFIER);
299 printk(KERN_ERR "Unable to register CPU frequency change "
300 "notifier for PCMCIA (%d)\n", ret);
304 static void __exit pxa2xx_pcmcia_cpufreq_exit(void)
306 cpufreq_unregister_notifier(&pxa2xx_pcmcia_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
310 #define pxa2xx_pcmcia_cpufreq_init()
311 #define pxa2xx_pcmcia_cpufreq_exit()
314 static int __init pxa2xx_pcmcia_init(void)
316 int ret = driver_register(&pxa2xx_pcmcia_driver);
318 pxa2xx_pcmcia_cpufreq_init();
322 static void __exit pxa2xx_pcmcia_exit(void)
324 pxa2xx_pcmcia_cpufreq_exit();
325 driver_unregister(&pxa2xx_pcmcia_driver);
328 module_init(pxa2xx_pcmcia_init);
329 module_exit(pxa2xx_pcmcia_exit);
331 MODULE_AUTHOR("Stefan Eletzhofer <stefan.eletzhofer@inquant.de> and Ian Molton <spyro@f2s.com>");
332 MODULE_DESCRIPTION("Linux PCMCIA Card Services: PXA2xx core socket driver");
333 MODULE_LICENSE("GPL");