This commit was manufactured by cvs2svn to create branch 'fedora'.
[linux-2.6.git] / drivers / pcmcia / pxa2xx_base.c
1 /*======================================================================
2
3   Device driver for the PCMCIA control functionality of PXA2xx
4   microprocessors.
5
6     The contents of this file may be used under the
7     terms of the GNU Public License version 2 (the "GPL")
8
9     (c) Ian Molton (spyro@f2s.com) 2003
10     (c) Stefan Eletzhofer (stefan.eletzhofer@inquant.de) 2003,4
11
12     derived from sa11xx_base.c
13
14      Portions created by John G. Dorsey are
15      Copyright (C) 1999 John G. Dorsey.
16
17   ======================================================================*/
18
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>
27
28 #include <asm/hardware.h>
29 #include <asm/io.h>
30 #include <asm/irq.h>
31 #include <asm/system.h>
32
33 #include <pcmcia/cs_types.h>
34 #include <pcmcia/ss.h>
35 #include <pcmcia/bulkmem.h>
36 #include <pcmcia/cistpl.h>
37
38 #include "cs_internal.h"
39 #include "soc_common.h"
40 #include "pxa2xx_base.h"
41
42
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)
49
50 static inline u_int pxa2xx_mcxx_hold(u_int pcmcia_cycle_ns,
51                                      u_int mem_clk_10khz)
52 {
53         u_int code = pcmcia_cycle_ns * mem_clk_10khz;
54         return (code / 300000) + ((code % 300000) ? 1 : 0) - 1;
55 }
56
57 static inline u_int pxa2xx_mcxx_asst(u_int pcmcia_cycle_ns,
58                                      u_int mem_clk_10khz)
59 {
60         u_int code = pcmcia_cycle_ns * mem_clk_10khz;
61         return (code / 300000) + ((code % 300000) ? 1 : 0) - 1;
62 }
63
64 static inline u_int pxa2xx_mcxx_setup(u_int pcmcia_cycle_ns,
65                                       u_int mem_clk_10khz)
66 {
67         u_int code = pcmcia_cycle_ns * mem_clk_10khz;
68         return (code / 100000) + ((code % 100000) ? 1 : 0) - 1;
69 }
70
71 /* This function returns the (approximate) command assertion period, in
72  * nanoseconds, for a given CPU clock frequency and MCXX_ASST value:
73  */
74 static inline u_int pxa2xx_pcmcia_cmd_time(u_int mem_clk_10khz,
75                                            u_int pcmcia_mcxx_asst)
76 {
77         return (300000 * (pcmcia_mcxx_asst + 1) / mem_clk_10khz);
78 }
79
80 static int pxa2xx_pcmcia_set_mcmem( int sock, int speed, int clock )
81 {
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);
88
89         return 0;
90 }
91
92 static int pxa2xx_pcmcia_set_mcio( int sock, int speed, int clock )
93 {
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);
100
101         return 0;
102 }
103
104 static int pxa2xx_pcmcia_set_mcatt( int sock, int speed, int clock )
105 {
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);
112
113         return 0;
114 }
115
116 static int pxa2xx_pcmcia_set_mcxx(struct soc_pcmcia_socket *skt, unsigned int lclk)
117 {
118         struct soc_pcmcia_timing timing;
119         int sock = skt->nr;
120
121         soc_common_pcmcia_get_timing(skt, &timing);
122
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);
126
127         return 0;
128 }
129
130 static int pxa2xx_pcmcia_set_timing(struct soc_pcmcia_socket *skt)
131 {
132         unsigned int lclk = get_lclk_frequency_10khz();
133         return pxa2xx_pcmcia_set_mcxx(skt, lclk);
134 }
135
136 int pxa2xx_drv_pcmcia_probe(struct device *dev)
137 {
138         int ret;
139         struct pcmcia_low_level *ops;
140         int first, nr;
141
142         if (!dev || !dev->platform_data)
143                 return -ENODEV;
144
145         ops = (struct pcmcia_low_level *)dev->platform_data;
146         first = ops->first;
147         nr = ops->nr;
148
149         /* Setup GPIOs for PCMCIA/CF alternate function mode.
150          *
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.
159          *
160          * In the meantime, setup default state of GPIO
161          * outputs before we enable them as outputs.
162          */
163
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);
170
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);
181
182         /* Provide our PXA2xx specific timing routines. */
183         ops->set_timing  = pxa2xx_pcmcia_set_timing;
184
185         ret = soc_common_drv_pcmcia_probe(dev, ops, first, nr);
186
187         if (ret == 0) {
188                 /*
189                  * We have at least one socket, so set MECR:CIT
190                  * (Card Is There)
191                  */
192                 MECR |= MECR_CIT;
193
194                 /* Set MECR:NOS (Number Of Sockets) */
195                 if (nr > 1)
196                         MECR |= MECR_NOS;
197                 else
198                         MECR &= ~MECR_NOS;
199         }
200
201         return ret;
202 }
203 EXPORT_SYMBOL(pxa2xx_drv_pcmcia_probe);
204
205 static int pxa2xx_drv_pcmcia_suspend(struct device *dev, u32 state, u32 level)
206 {
207         int ret = 0;
208         if (level == SUSPEND_SAVE_STATE)
209                 ret = pcmcia_socket_dev_suspend(dev, state);
210         return ret;
211 }
212
213 static int pxa2xx_drv_pcmcia_resume(struct device *dev, u32 level)
214 {
215         int ret = 0;
216         if (level == RESUME_RESTORE_STATE)
217                 ret = pcmcia_socket_dev_resume(dev);
218         return ret;
219 }
220
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,
228 };
229
230 #ifdef CONFIG_CPU_FREQ
231
232 /*
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'.
236  */
237 static void pxa2xx_pcmcia_update_mcxx(unsigned int clock)
238 {
239         struct soc_pcmcia_socket *skt;
240
241         down(&soc_sockets_lock);
242         list_for_each_entry(skt, &soc_sockets, node) {
243                 pxa2xx_pcmcia_set_mcxx(skt, clock);
244         }
245         up(&soc_sockets_lock);
246 }
247
248 /*
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
253  * values.
254  *
255  * Returns: 0 on success, -1 on error
256  */
257 static int
258 pxa2xx_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
259 {
260         struct cpufreq_freqs *freqs = data;
261
262 #warning "it's not clear if this is right since the core CPU (N) clock has no effect on the memory (L) clock"
263         switch (val) {
264                 case CPUFREQ_PRECHANGE:
265                         if (freqs->new > freqs->old) {
266                                 debug( 2, "new frequency %u.%uMHz > %u.%uMHz, "
267                                                 "pre-updating\n",
268                                                 freqs->new / 1000, (freqs->new / 100) % 10,
269                                                 freqs->old / 1000, (freqs->old / 100) % 10);
270                                 pxa2xx_pcmcia_update_mcxx(freqs->new);
271                         }
272                         break;
273
274                 case CPUFREQ_POSTCHANGE:
275                         if (freqs->new < freqs->old) {
276                                 debug( 2, "new frequency %u.%uMHz < %u.%uMHz, "
277                                                 "post-updating\n",
278                                                 freqs->new / 1000, (freqs->new / 100) % 10,
279                                                 freqs->old / 1000, (freqs->old / 100) % 10);
280                                 pxa2xx_pcmcia_update_mcxx(freqs->new);
281                         }
282                         break;
283         }
284
285         return 0;
286 }
287
288 static struct notifier_block pxa2xx_pcmcia_notifier_block = {
289         .notifier_call  = pxa2xx_pcmcia_notifier
290 };
291
292 static int __init pxa2xx_pcmcia_cpufreq_init(void)
293 {
294         int ret;
295
296         ret = cpufreq_register_notifier(&pxa2xx_pcmcia_notifier_block,
297                                         CPUFREQ_TRANSITION_NOTIFIER);
298         if (ret < 0)
299                 printk(KERN_ERR "Unable to register CPU frequency change "
300                                 "notifier for PCMCIA (%d)\n", ret);
301         return ret;
302 }
303
304 static void __exit pxa2xx_pcmcia_cpufreq_exit(void)
305 {
306         cpufreq_unregister_notifier(&pxa2xx_pcmcia_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
307 }
308
309 #else
310 #define pxa2xx_pcmcia_cpufreq_init()
311 #define pxa2xx_pcmcia_cpufreq_exit()
312 #endif
313
314 static int __init pxa2xx_pcmcia_init(void)
315 {
316         int ret = driver_register(&pxa2xx_pcmcia_driver);
317         if (ret == 0)
318                 pxa2xx_pcmcia_cpufreq_init();
319         return ret;
320 }
321
322 static void __exit pxa2xx_pcmcia_exit(void)
323 {
324         pxa2xx_pcmcia_cpufreq_exit();
325         driver_unregister(&pxa2xx_pcmcia_driver);
326 }
327
328 module_init(pxa2xx_pcmcia_init);
329 module_exit(pxa2xx_pcmcia_exit);
330
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");