import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / drivers / pcmcia / sa1100_generic.c
1 /*======================================================================
2
3     Device driver for the PCMCIA control functionality of StrongARM
4     SA-1100 microprocessors.
5
6     The contents of this file are subject to the Mozilla Public
7     License Version 1.1 (the "License"); you may not use this file
8     except in compliance with the License. You may obtain a copy of
9     the License at http://www.mozilla.org/MPL/
10
11     Software distributed under the License is distributed on an "AS
12     IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
13     implied. See the License for the specific language governing
14     rights and limitations under the License.
15
16     The initial developer of the original code is John G. Dorsey
17     <john+@cs.cmu.edu>.  Portions created by John G. Dorsey are
18     Copyright (C) 1999 John G. Dorsey.  All Rights Reserved.
19
20     Alternatively, the contents of this file may be used under the
21     terms of the GNU Public License version 2 (the "GPL"), in which
22     case the provisions of the GPL are applicable instead of the
23     above.  If you wish to allow the use of your version of this file
24     only under the terms of the GPL and not to allow others to use
25     your version of this file under the MPL, indicate your decision
26     by deleting the provisions above and replace them with the notice
27     and other provisions required by the GPL.  If you do not delete
28     the provisions above, a recipient may use your version of this
29     file under either the MPL or the GPL.
30     
31 ======================================================================*/
32 /*
33  * Please see linux/Documentation/arm/SA1100/PCMCIA for more information
34  * on the low-level kernel interface.
35  */
36
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/config.h>
40 #include <linux/cpufreq.h>
41 #include <linux/delay.h>
42 #include <linux/ioport.h>
43 #include <linux/kernel.h>
44 #include <linux/tqueue.h>
45 #include <linux/timer.h>
46 #include <linux/mm.h>
47 #include <linux/notifier.h>
48 #include <linux/proc_fs.h>
49 #include <linux/version.h>
50
51 #include <pcmcia/version.h>
52 #include <pcmcia/cs_types.h>
53 #include <pcmcia/cs.h>
54 #include <pcmcia/ss.h>
55 #include <pcmcia/bus_ops.h>
56
57 #include <asm/hardware.h>
58 #include <asm/io.h>
59 #include <asm/irq.h>
60 #include <asm/system.h>
61 #include <asm/arch/assabet.h>
62
63 #include "sa1100.h"
64
65 #ifdef PCMCIA_DEBUG
66 static int pc_debug;
67 #endif
68
69 /* This structure maintains housekeeping state for each socket, such
70  * as the last known values of the card detect pins, or the Card Services
71  * callback value associated with the socket:
72  */
73 static int sa1100_pcmcia_socket_count;
74 static struct sa1100_pcmcia_socket sa1100_pcmcia_socket[SA1100_PCMCIA_MAX_SOCK];
75
76 #define PCMCIA_SOCKET(x)        (sa1100_pcmcia_socket + (x))
77
78 /* Returned by the low-level PCMCIA interface: */
79 static struct pcmcia_low_level *pcmcia_low_level;
80
81 static struct timer_list poll_timer;
82 static struct tq_struct sa1100_pcmcia_task;
83
84 /*
85  * sa1100_pcmcia_state_to_config
86  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87  *
88  * Convert PCMCIA socket state to our socket configure structure.
89  */
90 static struct pcmcia_configure
91 sa1100_pcmcia_state_to_config(unsigned int sock, socket_state_t *state)
92 {
93   struct pcmcia_configure conf;
94
95   conf.sock    = sock;
96   conf.vcc     = state->Vcc;
97   conf.vpp     = state->Vpp;
98   conf.output  = state->flags & SS_OUTPUT_ENA ? 1 : 0;
99   conf.speaker = state->flags & SS_SPKR_ENA ? 1 : 0;
100   conf.reset   = state->flags & SS_RESET ? 1 : 0;
101   conf.irq     = state->io_irq != 0;
102
103   return conf;
104 }
105
106 /* sa1100_pcmcia_init()
107  * ^^^^^^^^^^^^^^^^^^^^
108  *
109  * (Re-)Initialise the socket, turning on status interrupts
110  * and PCMCIA bus.  This must wait for power to stabilise
111  * so that the card status signals report correctly.
112  *
113  * Returns: 0
114  */
115 static int sa1100_pcmcia_init(unsigned int sock)
116 {
117   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
118   struct pcmcia_configure conf;
119
120   DEBUG(2, "%s(): initializing socket %u\n", __FUNCTION__, sock);
121
122   skt->cs_state = dead_socket;
123
124   conf = sa1100_pcmcia_state_to_config(sock, &dead_socket);
125
126   pcmcia_low_level->configure_socket(&conf);
127
128   return pcmcia_low_level->socket_init(sock);
129 }
130
131
132 /*
133  * sa1100_pcmcia_suspend()
134  * ^^^^^^^^^^^^^^^^^^^^^^^
135  *
136  * Remove power on the socket, disable IRQs from the card.
137  * Turn off status interrupts, and disable the PCMCIA bus.
138  *
139  * Returns: 0
140  */
141 static int sa1100_pcmcia_suspend(unsigned int sock)
142 {
143   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
144   struct pcmcia_configure conf;
145   int ret;
146
147   DEBUG(2, "%s(): suspending socket %u\n", __FUNCTION__, sock);
148
149   conf = sa1100_pcmcia_state_to_config(sock, &dead_socket);
150
151   ret = pcmcia_low_level->configure_socket(&conf);
152
153   if (ret == 0) {
154     skt->cs_state = dead_socket;
155     ret = pcmcia_low_level->socket_suspend(sock);
156   }
157
158   return ret;
159 }
160
161
162 /* sa1100_pcmcia_events()
163  * ^^^^^^^^^^^^^^^^^^^^^^
164  * Helper routine to generate a Card Services event mask based on
165  * state information obtained from the kernel low-level PCMCIA layer
166  * in a recent (and previous) sampling. Updates `prev_state'.
167  *
168  * Returns: an event mask for the given socket state.
169  */
170 static inline unsigned int
171 sa1100_pcmcia_events(struct pcmcia_state *state,
172                      struct pcmcia_state *prev_state,
173                      unsigned int mask, unsigned int flags)
174 {
175   unsigned int events = 0;
176
177   if (state->detect != prev_state->detect) {
178     DEBUG(3, "%s(): card detect value %u\n", __FUNCTION__, state->detect);
179
180     events |= SS_DETECT;
181   }
182
183   if (state->ready != prev_state->ready) {
184     DEBUG(3, "%s(): card ready value %u\n", __FUNCTION__, state->ready);
185
186     events |= flags & SS_IOCARD ? 0 : SS_READY;
187   }
188
189   if (state->bvd1 != prev_state->bvd1) {
190     DEBUG(3, "%s(): card BVD1 value %u\n", __FUNCTION__, state->bvd1);
191
192     events |= flags & SS_IOCARD ? SS_STSCHG : SS_BATDEAD;
193   }
194
195   if (state->bvd2 != prev_state->bvd2) {
196     DEBUG(3, "%s(): card BVD2 value %u\n", __FUNCTION__, state->bvd2);
197
198     events |= flags & SS_IOCARD ? 0 : SS_BATWARN;
199   }
200
201   *prev_state = *state;
202
203   events &= mask;
204
205   DEBUG(2, "events: %s%s%s%s%s%s\n",
206         events == 0         ? "<NONE>"   : "",
207         events & SS_DETECT  ? "DETECT "  : "",
208         events & SS_READY   ? "READY "   : "",
209         events & SS_BATDEAD ? "BATDEAD " : "",
210         events & SS_BATWARN ? "BATWARN " : "",
211         events & SS_STSCHG  ? "STSCHG "  : "");
212
213   return events;
214 }  /* sa1100_pcmcia_events() */
215
216
217 /* sa1100_pcmcia_task_handler()
218  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
219  * Processes serviceable socket events using the "eventd" thread context.
220  *
221  * Event processing (specifically, the invocation of the Card Services event
222  * callback) occurs in this thread rather than in the actual interrupt
223  * handler due to the use of scheduling operations in the PCMCIA core.
224  */
225 static void sa1100_pcmcia_task_handler(void *data)
226 {
227   struct pcmcia_state state[SA1100_PCMCIA_MAX_SOCK];
228   struct pcmcia_state_array state_array;
229   unsigned int all_events;
230
231   DEBUG(4, "%s(): entering PCMCIA monitoring thread\n", __FUNCTION__);
232
233   state_array.size = sa1100_pcmcia_socket_count;
234   state_array.state = state;
235
236   do {
237     unsigned int events;
238     int ret, i;
239
240     memset(state, 0, sizeof(state));
241
242     DEBUG(4, "%s(): interrogating low-level PCMCIA service\n", __FUNCTION__);
243
244     ret = pcmcia_low_level->socket_state(&state_array);
245     if (ret < 0) {
246       printk(KERN_ERR "sa1100_pcmcia: unable to read socket status\n");
247       break;
248     }
249
250     all_events = 0;
251
252     for (i = 0; i < state_array.size; i++, all_events |= events) {
253       struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(i);
254
255       events = sa1100_pcmcia_events(&state[i], &skt->k_state,
256                                     skt->cs_state.csc_mask,
257                                     skt->cs_state.flags);
258
259       if (events && sa1100_pcmcia_socket[i].handler != NULL)
260         skt->handler(skt->handler_info, events);
261     }
262   } while(all_events);
263 }  /* sa1100_pcmcia_task_handler() */
264
265 static struct tq_struct sa1100_pcmcia_task = {
266         routine: sa1100_pcmcia_task_handler
267 };
268
269
270 /* sa1100_pcmcia_poll_event()
271  * ^^^^^^^^^^^^^^^^^^^^^^^^^^
272  * Let's poll for events in addition to IRQs since IRQ only is unreliable...
273  */
274 static void sa1100_pcmcia_poll_event(unsigned long dummy)
275 {
276   DEBUG(4, "%s(): polling for events\n", __FUNCTION__);
277   poll_timer.function = sa1100_pcmcia_poll_event;
278   poll_timer.expires = jiffies + SA1100_PCMCIA_POLL_PERIOD;
279   add_timer(&poll_timer);
280   schedule_task(&sa1100_pcmcia_task);
281 }
282
283
284 /* sa1100_pcmcia_interrupt()
285  * ^^^^^^^^^^^^^^^^^^^^^^^^^
286  * Service routine for socket driver interrupts (requested by the
287  * low-level PCMCIA init() operation via sa1100_pcmcia_thread()).
288  * The actual interrupt-servicing work is performed by
289  * sa1100_pcmcia_thread(), largely because the Card Services event-
290  * handling code performs scheduling operations which cannot be
291  * executed from within an interrupt context.
292  */
293 static void sa1100_pcmcia_interrupt(int irq, void *dev, struct pt_regs *regs)
294 {
295   DEBUG(3, "%s(): servicing IRQ %d\n", __FUNCTION__, irq);
296   schedule_task(&sa1100_pcmcia_task);
297 }
298
299
300 /* sa1100_pcmcia_register_callback()
301  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
302  * Implements the register_callback() operation for the in-kernel
303  * PCMCIA service (formerly SS_RegisterCallback in Card Services). If 
304  * the function pointer `handler' is not NULL, remember the callback 
305  * location in the state for `sock', and increment the usage counter 
306  * for the driver module. (The callback is invoked from the interrupt
307  * service routine, sa1100_pcmcia_interrupt(), to notify Card Services
308  * of interesting events.) Otherwise, clear the callback pointer in the
309  * socket state and decrement the module usage count.
310  *
311  * Returns: 0
312  */
313 static int
314 sa1100_pcmcia_register_callback(unsigned int sock,
315                                 void (*handler)(void *, unsigned int),
316                                 void *info)
317 {
318   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
319
320   if (handler == NULL) {
321     skt->handler = NULL;
322     MOD_DEC_USE_COUNT;
323   } else {
324     MOD_INC_USE_COUNT;
325     skt->handler_info = info;
326     skt->handler = handler;
327   }
328
329   return 0;
330 }
331
332
333 /* sa1100_pcmcia_inquire_socket()
334  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
335  * Implements the inquire_socket() operation for the in-kernel PCMCIA
336  * service (formerly SS_InquireSocket in Card Services). Of note is
337  * the setting of the SS_CAP_PAGE_REGS bit in the `features' field of
338  * `cap' to "trick" Card Services into tolerating large "I/O memory" 
339  * addresses. Also set is SS_CAP_STATIC_MAP, which disables the memory
340  * resource database check. (Mapped memory is set up within the socket
341  * driver itself.)
342  *
343  * In conjunction with the STATIC_MAP capability is a new field,
344  * `io_offset', recommended by David Hinds. Rather than go through
345  * the SetIOMap interface (which is not quite suited for communicating
346  * window locations up from the socket driver), we just pass up
347  * an offset which is applied to client-requested base I/O addresses
348  * in alloc_io_space().
349  *
350  * SS_CAP_PAGE_REGS: used by setup_cis_mem() in cistpl.c to set the
351  *   force_low argument to validate_mem() in rsrc_mgr.c -- since in
352  *   general, the mapped * addresses of the PCMCIA memory regions
353  *   will not be within 0xffff, setting force_low would be
354  *   undesirable.
355  *
356  * SS_CAP_STATIC_MAP: don't bother with the (user-configured) memory
357  *   resource database; we instead pass up physical address ranges
358  *   and allow other parts of Card Services to deal with remapping.
359  *
360  * SS_CAP_PCCARD: we can deal with 16-bit PCMCIA & CF cards, but
361  *   not 32-bit CardBus devices.
362  *
363  * Return value is irrelevant; the pcmcia subsystem ignores it.
364  */
365 static int
366 sa1100_pcmcia_inquire_socket(unsigned int sock, socket_cap_t *cap)
367 {
368   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
369   int ret = -1;
370
371   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
372
373   if (sock < sa1100_pcmcia_socket_count) {
374     cap->features  = SS_CAP_PAGE_REGS | SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
375     cap->irq_mask  = 0;
376     cap->map_size  = PAGE_SIZE;
377     cap->pci_irq   = skt->irq;
378     cap->io_offset = (unsigned long)skt->virt_io;
379
380     ret = 0;
381   }
382
383   return ret;
384 }
385
386
387 /* sa1100_pcmcia_get_status()
388  * ^^^^^^^^^^^^^^^^^^^^^^^^^^
389  * Implements the get_status() operation for the in-kernel PCMCIA
390  * service (formerly SS_GetStatus in Card Services). Essentially just
391  * fills in bits in `status' according to internal driver state or
392  * the value of the voltage detect chipselect register.
393  *
394  * As a debugging note, during card startup, the PCMCIA core issues
395  * three set_socket() commands in a row the first with RESET deasserted,
396  * the second with RESET asserted, and the last with RESET deasserted
397  * again. Following the third set_socket(), a get_status() command will
398  * be issued. The kernel is looking for the SS_READY flag (see
399  * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
400  *
401  * Returns: 0
402  */
403 static int
404 sa1100_pcmcia_get_status(unsigned int sock, unsigned int *status)
405 {
406   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
407   struct pcmcia_state state[SA1100_PCMCIA_MAX_SOCK];
408   struct pcmcia_state_array state_array;
409   unsigned int stat;
410
411   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
412
413   state_array.size = sa1100_pcmcia_socket_count;
414   state_array.state = state;
415
416   memset(state, 0, sizeof(state));
417
418   if ((pcmcia_low_level->socket_state(&state_array)) < 0) {
419     printk(KERN_ERR "sa1100_pcmcia: unable to get socket status\n");
420     return -1;
421   }
422
423   skt->k_state = state[sock];
424
425   stat = state[sock].detect ? SS_DETECT : 0;
426   stat |= state[sock].ready ? SS_READY  : 0;
427   stat |= state[sock].vs_3v ? SS_3VCARD : 0;
428   stat |= state[sock].vs_Xv ? SS_XVCARD : 0;
429
430   /* The power status of individual sockets is not available
431    * explicitly from the hardware, so we just remember the state
432    * and regurgitate it upon request:
433    */
434   stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
435
436   if (skt->cs_state.flags & SS_IOCARD)
437     stat |= state[sock].bvd1 ? SS_STSCHG : 0;
438   else {
439     if (state[sock].bvd1 == 0)
440       stat |= SS_BATDEAD;
441     else if (state[sock].bvd2 == 0)
442       stat |= SS_BATWARN;
443   }
444
445   DEBUG(3, "\tstatus: %s%s%s%s%s%s%s%s\n",
446         stat & SS_DETECT  ? "DETECT "  : "",
447         stat & SS_READY   ? "READY "   : "", 
448         stat & SS_BATDEAD ? "BATDEAD " : "",
449         stat & SS_BATWARN ? "BATWARN " : "",
450         stat & SS_POWERON ? "POWERON " : "",
451         stat & SS_STSCHG  ? "STSCHG "  : "",
452         stat & SS_3VCARD  ? "3VCARD "  : "",
453         stat & SS_XVCARD  ? "XVCARD "  : "");
454
455   *status = stat;
456
457   return 0;
458 }  /* sa1100_pcmcia_get_status() */
459
460
461 /* sa1100_pcmcia_get_socket()
462  * ^^^^^^^^^^^^^^^^^^^^^^^^^^
463  * Implements the get_socket() operation for the in-kernel PCMCIA
464  * service (formerly SS_GetSocket in Card Services). Not a very 
465  * exciting routine.
466  *
467  * Returns: 0
468  */
469 static int
470 sa1100_pcmcia_get_socket(unsigned int sock, socket_state_t *state)
471 {
472   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
473
474   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
475
476   *state = skt->cs_state;
477
478   return 0;
479 }
480
481 /* sa1100_pcmcia_set_socket()
482  * ^^^^^^^^^^^^^^^^^^^^^^^^^^
483  * Implements the set_socket() operation for the in-kernel PCMCIA
484  * service (formerly SS_SetSocket in Card Services). We more or
485  * less punt all of this work and let the kernel handle the details
486  * of power configuration, reset, &c. We also record the value of
487  * `state' in order to regurgitate it to the PCMCIA core later.
488  *
489  * Returns: 0
490  */
491 static int
492 sa1100_pcmcia_set_socket(unsigned int sock, socket_state_t *state)
493 {
494   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
495   struct pcmcia_configure conf;
496
497   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
498
499   DEBUG(3, "\tmask:  %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
500         (state->csc_mask==0)?"<NONE>":"",
501         (state->csc_mask&SS_DETECT)?"DETECT ":"",
502         (state->csc_mask&SS_READY)?"READY ":"",
503         (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
504         (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
505         (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
506         (state->flags==0)?"<NONE>":"",
507         (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
508         (state->flags&SS_IOCARD)?"IOCARD ":"",
509         (state->flags&SS_RESET)?"RESET ":"",
510         (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
511         (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
512   DEBUG(3, "\tVcc %d  Vpp %d  irq %d\n",
513         state->Vcc, state->Vpp, state->io_irq);
514
515   conf = sa1100_pcmcia_state_to_config(sock, state);
516
517   if (pcmcia_low_level->configure_socket(&conf) < 0) {
518     printk(KERN_ERR "sa1100_pcmcia: unable to configure socket %d\n", sock);
519     return -1;
520   }
521
522   skt->cs_state = *state;
523   
524   return 0;
525 }  /* sa1100_pcmcia_set_socket() */
526
527
528 /* sa1100_pcmcia_get_io_map()
529  * ^^^^^^^^^^^^^^^^^^^^^^^^^^
530  * Implements the get_io_map() operation for the in-kernel PCMCIA
531  * service (formerly SS_GetIOMap in Card Services). Just returns an
532  * I/O map descriptor which was assigned earlier by a set_io_map().
533  *
534  * Returns: 0 on success, -1 if the map index was out of range
535  */
536 static int
537 sa1100_pcmcia_get_io_map(unsigned int sock, struct pccard_io_map *map)
538 {
539   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
540   int ret = -1;
541
542   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
543
544   if (map->map < MAX_IO_WIN) {
545     *map = skt->io_map[map->map];
546     ret = 0;
547   }
548
549   return ret;
550 }
551
552
553 /* sa1100_pcmcia_set_io_map()
554  * ^^^^^^^^^^^^^^^^^^^^^^^^^^
555  * Implements the set_io_map() operation for the in-kernel PCMCIA
556  * service (formerly SS_SetIOMap in Card Services). We configure
557  * the map speed as requested, but override the address ranges
558  * supplied by Card Services.
559  *
560  * Returns: 0 on success, -1 on error
561  */
562 static int
563 sa1100_pcmcia_set_io_map(unsigned int sock, struct pccard_io_map *map)
564 {
565   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
566
567   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
568
569   DEBUG(3, "\tmap %u  speed %u\n\tstart 0x%08x  stop 0x%08x\n",
570         map->map, map->speed, map->start, map->stop);
571   DEBUG(3, "\tflags: %s%s%s%s%s%s%s%s\n",
572         (map->flags==0)?"<NONE>":"",
573         (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
574         (map->flags&MAP_16BIT)?"16BIT ":"",
575         (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
576         (map->flags&MAP_0WS)?"0WS ":"",
577         (map->flags&MAP_WRPROT)?"WRPROT ":"",
578         (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"",
579         (map->flags&MAP_PREFETCH)?"PREFETCH ":"");
580
581   if (map->map >= MAX_IO_WIN) {
582     printk(KERN_ERR "%s(): map (%d) out of range\n", __FUNCTION__,
583            map->map);
584     return -1;
585   }
586
587   if (map->flags & MAP_ACTIVE) {
588     unsigned int clock, speed = map->speed;
589     unsigned long mecr;
590
591     if (speed == 0)
592       speed = SA1100_PCMCIA_IO_ACCESS;
593
594     clock = cpufreq_get(0);
595
596     mecr = MECR;
597
598     MECR_BSIO_SET(mecr, sock, sa1100_pcmcia_mecr_bs(speed, clock));
599
600     skt->speed_io = speed;
601
602     DEBUG(4, "%s(): FAST%u %lx  BSM%u %lx  BSA%u %lx  BSIO%u %lx\n",
603           __FUNCTION__, sock, MECR_FAST_GET(mecr, sock), sock,
604           MECR_BSM_GET(mecr, sock), sock, MECR_BSA_GET(mecr, sock), 
605           sock, MECR_BSIO_GET(mecr, sock));
606
607     MECR = mecr;
608   }
609
610   if (map->stop == 1)
611     map->stop = PAGE_SIZE-1;
612
613   map->stop -= map->start;
614   map->stop += (unsigned long)skt->virt_io;
615   map->start = (unsigned long)skt->virt_io;
616
617   skt->io_map[map->map] = *map;
618
619   return 0;
620 }  /* sa1100_pcmcia_set_io_map() */
621
622
623 /* sa1100_pcmcia_get_mem_map()
624  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
625  * Implements the get_mem_map() operation for the in-kernel PCMCIA
626  * service (formerly SS_GetMemMap in Card Services). Just returns a
627  *  memory map descriptor which was assigned earlier by a
628  *  set_mem_map() request.
629  *
630  * Returns: 0 on success, -1 if the map index was out of range
631  */
632 static int
633 sa1100_pcmcia_get_mem_map(unsigned int sock, struct pccard_mem_map *map)
634 {
635   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
636   int ret = -1;
637
638   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
639
640   if (map->map < MAX_WIN) {
641     *map = skt->mem_map[map->map];
642     ret = 0;
643   }
644
645   return ret;
646 }
647
648
649 /* sa1100_pcmcia_set_mem_map()
650  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
651  * Implements the set_mem_map() operation for the in-kernel PCMCIA
652  * service (formerly SS_SetMemMap in Card Services). We configure
653  * the map speed as requested, but override the address ranges
654  * supplied by Card Services.
655  *
656  * Returns: 0 on success, -1 on error
657  */
658 static int
659 sa1100_pcmcia_set_mem_map(unsigned int sock, struct pccard_mem_map *map)
660 {
661   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
662   unsigned long start;
663
664   DEBUG(2, "%s() for sock %u\n", __FUNCTION__, sock);
665
666   DEBUG(3, "\tmap %u speed %u sys_start %08lx sys_stop %08lx card_start %08x\n",
667         map->map, map->speed, map->sys_start, map->sys_stop, map->card_start);
668   DEBUG(3, "\tflags: %s%s%s%s%s%s%s%s\n",
669         (map->flags==0)?"<NONE>":"",
670         (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
671         (map->flags&MAP_16BIT)?"16BIT ":"",
672         (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
673         (map->flags&MAP_0WS)?"0WS ":"",
674         (map->flags&MAP_WRPROT)?"WRPROT ":"",
675         (map->flags&MAP_ATTRIB)?"ATTRIB ":"",
676         (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"");
677
678   if (map->map >= MAX_WIN){
679     printk(KERN_ERR "%s(): map (%d) out of range\n", __FUNCTION__,
680            map->map);
681     return -1;
682   }
683
684   if (map->flags & MAP_ACTIVE) {
685     unsigned int clock, speed = map->speed;
686     unsigned long mecr;
687
688     /*
689      * When clients issue RequestMap, the access speed is not always
690      * properly configured.  Choose some sensible defaults.
691      */
692     if (speed == 0) {
693       if (skt->cs_state.Vcc == 33)
694         speed = SA1100_PCMCIA_3V_MEM_ACCESS;
695       else
696         speed = SA1100_PCMCIA_5V_MEM_ACCESS;
697     }
698
699     clock = cpufreq_get(0);
700
701     /* Fixme: MECR is not pre-empt safe. */
702     mecr = MECR;
703
704     if (map->flags & MAP_ATTRIB) {
705       MECR_BSA_SET(mecr, sock, sa1100_pcmcia_mecr_bs(speed, clock));
706       skt->speed_attr = speed;
707     } else {
708       MECR_BSM_SET(mecr, sock, sa1100_pcmcia_mecr_bs(speed, clock));
709       skt->speed_mem = speed;
710     }
711
712     DEBUG(4, "%s(): FAST%u %lx  BSM%u %lx  BSA%u %lx  BSIO%u %lx\n",
713           __FUNCTION__, sock, MECR_FAST_GET(mecr, sock), sock,
714           MECR_BSM_GET(mecr, sock), sock, MECR_BSA_GET(mecr, sock), 
715           sock, MECR_BSIO_GET(mecr, sock));
716
717     MECR = mecr;
718   }
719
720   start = (map->flags & MAP_ATTRIB) ? skt->phys_attr : skt->phys_mem;
721
722   if (map->sys_stop == 0)
723     map->sys_stop = PAGE_SIZE-1;
724
725   map->sys_stop -= map->sys_start;
726   map->sys_stop += start;
727   map->sys_start = start;
728
729   skt->mem_map[map->map] = *map;
730
731   return 0;
732 }  /* sa1100_pcmcia_set_mem_map() */
733
734
735 #if defined(CONFIG_PROC_FS)
736
737 /* sa1100_pcmcia_proc_status()
738  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
739  * Implements the /proc/bus/pccard/??/status file.
740  *
741  * Returns: the number of characters added to the buffer
742  */
743 static int
744 sa1100_pcmcia_proc_status(char *buf, char **start, off_t pos,
745                           int count, int *eof, void *data)
746 {
747   unsigned int sock = (unsigned int)data;
748   unsigned int clock = cpufreq_get(0);
749   struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(sock);
750   unsigned long mecr = MECR;
751   char *p = buf;
752
753   p+=sprintf(p, "k_state  : %s%s%s%s%s%s%s\n", 
754              skt->k_state.detect ? "detect " : "",
755              skt->k_state.ready  ? "ready "  : "",
756              skt->k_state.bvd1   ? "bvd1 "   : "",
757              skt->k_state.bvd2   ? "bvd2 "   : "",
758              skt->k_state.wrprot ? "wrprot " : "",
759              skt->k_state.vs_3v  ? "vs_3v "  : "",
760              skt->k_state.vs_Xv  ? "vs_Xv "  : "");
761
762   p+=sprintf(p, "status   : %s%s%s%s%s%s%s%s%s\n",
763              skt->k_state.detect ? "SS_DETECT " : "",
764              skt->k_state.ready  ? "SS_READY " : "",
765              skt->cs_state.Vcc   ? "SS_POWERON " : "",
766              skt->cs_state.flags & SS_IOCARD ? "SS_IOCARD " : "",
767              (skt->cs_state.flags & SS_IOCARD &&
768               skt->k_state.bvd1) ? "SS_STSCHG " : "",
769              ((skt->cs_state.flags & SS_IOCARD)==0 &&
770               (skt->k_state.bvd1==0)) ? "SS_BATDEAD " : "",
771              ((skt->cs_state.flags & SS_IOCARD)==0 &&
772               (skt->k_state.bvd2==0)) ? "SS_BATWARN " : "",
773              skt->k_state.vs_3v  ? "SS_3VCARD " : "",
774              skt->k_state.vs_Xv  ? "SS_XVCARD " : "");
775
776   p+=sprintf(p, "mask     : %s%s%s%s%s\n",
777              skt->cs_state.csc_mask & SS_DETECT  ? "SS_DETECT "  : "",
778              skt->cs_state.csc_mask & SS_READY   ? "SS_READY "   : "",
779              skt->cs_state.csc_mask & SS_BATDEAD ? "SS_BATDEAD " : "",
780              skt->cs_state.csc_mask & SS_BATWARN ? "SS_BATWARN " : "",
781              skt->cs_state.csc_mask & SS_STSCHG  ? "SS_STSCHG "  : "");
782
783   p+=sprintf(p, "cs_flags : %s%s%s%s%s\n",
784              skt->cs_state.flags & SS_PWR_AUTO   ? "SS_PWR_AUTO "   : "",
785              skt->cs_state.flags & SS_IOCARD     ? "SS_IOCARD "     : "",
786              skt->cs_state.flags & SS_RESET      ? "SS_RESET "      : "",
787              skt->cs_state.flags & SS_SPKR_ENA   ? "SS_SPKR_ENA "   : "",
788              skt->cs_state.flags & SS_OUTPUT_ENA ? "SS_OUTPUT_ENA " : "");
789
790   p+=sprintf(p, "Vcc      : %d\n", skt->cs_state.Vcc);
791   p+=sprintf(p, "Vpp      : %d\n", skt->cs_state.Vpp);
792   p+=sprintf(p, "IRQ      : %d\n", skt->cs_state.io_irq);
793
794   p+=sprintf(p, "I/O      : %u (%u)\n", skt->speed_io,
795              sa1100_pcmcia_cmd_time(clock, MECR_BSIO_GET(mecr, sock)));
796
797   p+=sprintf(p, "attribute: %u (%u)\n", skt->speed_attr,
798              sa1100_pcmcia_cmd_time(clock, MECR_BSA_GET(mecr, sock)));
799
800   p+=sprintf(p, "common   : %u (%u)\n", skt->speed_mem,
801              sa1100_pcmcia_cmd_time(clock, MECR_BSM_GET(mecr, sock)));
802
803   return p-buf;
804 }
805
806 /* sa1100_pcmcia_proc_setup()
807  * ^^^^^^^^^^^^^^^^^^^^^^^^^^
808  * Implements the proc_setup() operation for the in-kernel PCMCIA
809  * service (formerly SS_ProcSetup in Card Services).
810  *
811  * Returns: 0 on success, -1 on error
812  */
813 static void
814 sa1100_pcmcia_proc_setup(unsigned int sock, struct proc_dir_entry *base)
815 {
816   struct proc_dir_entry *entry;
817
818   DEBUG(4, "%s() for sock %u\n", __FUNCTION__, sock);
819
820   if ((entry = create_proc_entry("status", 0, base)) == NULL){
821     printk(KERN_ERR "unable to install \"status\" procfs entry\n");
822     return;
823   }
824
825   entry->read_proc = sa1100_pcmcia_proc_status;
826   entry->data = (void *)sock;
827 }
828
829 #endif  /* defined(CONFIG_PROC_FS) */
830
831 static struct pccard_operations sa1100_pcmcia_operations = {
832   init:                 sa1100_pcmcia_init,
833   suspend:              sa1100_pcmcia_suspend,
834   register_callback:    sa1100_pcmcia_register_callback,
835   inquire_socket:       sa1100_pcmcia_inquire_socket,
836   get_status:           sa1100_pcmcia_get_status,
837   get_socket:           sa1100_pcmcia_get_socket,
838   set_socket:           sa1100_pcmcia_set_socket,
839   get_io_map:           sa1100_pcmcia_get_io_map,
840   set_io_map:           sa1100_pcmcia_set_io_map,
841   get_mem_map:          sa1100_pcmcia_get_mem_map,
842   set_mem_map:          sa1100_pcmcia_set_mem_map,
843 #ifdef CONFIG_PROC_FS
844   proc_setup:           sa1100_pcmcia_proc_setup
845 #endif
846 };
847
848 #ifdef CONFIG_CPU_FREQ
849
850 /* sa1100_pcmcia_update_mecr()
851  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
852  * When sa1100_pcmcia_notifier() decides that a MECR adjustment (due
853  * to a core clock frequency change) is needed, this routine establishes
854  * new BS_xx values consistent with the clock speed `clock'.
855  */
856 static void sa1100_pcmcia_update_mecr(unsigned int clock)
857 {
858   unsigned int sock;
859   unsigned long mecr = MECR;
860
861   for(sock = 0; sock < SA1100_PCMCIA_MAX_SOCK; ++sock){
862
863     MECR_BSIO_SET(mecr, sock,
864                   sa1100_pcmcia_mecr_bs(sa1100_pcmcia_socket[sock].speed_io,
865                                         clock));
866     MECR_BSA_SET(mecr, sock,
867                  sa1100_pcmcia_mecr_bs(sa1100_pcmcia_socket[sock].speed_attr,
868                                        clock));
869     MECR_BSM_SET(mecr, sock,
870                  sa1100_pcmcia_mecr_bs(sa1100_pcmcia_socket[sock].speed_mem,
871                                        clock));
872   }
873
874   MECR = mecr;
875
876 }
877
878 /* sa1100_pcmcia_notifier()
879  * ^^^^^^^^^^^^^^^^^^^^^^^^
880  * When changing the processor core clock frequency, it is necessary
881  * to adjust the MECR timings accordingly. We've recorded the timings
882  * requested by Card Services, so this is just a matter of finding
883  * out what our current speed is, and then recomputing the new MECR
884  * values.
885  *
886  * Returns: 0 on success, -1 on error
887  */
888 static int
889 sa1100_pcmcia_notifier(struct notifier_block *nb, unsigned long val,
890                        void *data)
891 {
892   struct cpufreq_info *ci = data;
893
894   switch (val) {
895   case CPUFREQ_PRECHANGE:
896     if (ci->new_freq > ci->old_freq) {
897       DEBUG(2, "%s(): new frequency %u.%uMHz > %u.%uMHz, pre-updating\n",
898             __FUNCTION__,
899             ci->new_freq / 1000, (ci->new_freq / 100) % 10,
900             ci->old_freq / 1000, (ci->old_freq / 100) % 10);
901       sa1100_pcmcia_update_mecr(ci->new_freq);
902     }
903     break;
904
905   case CPUFREQ_POSTCHANGE:
906     if (ci->new_freq < ci->old_freq) {
907       DEBUG(2, "%s(): new frequency %u.%uMHz < %u.%uMHz, post-updating\n",
908             __FUNCTION__,
909             ci->new_freq / 1000, (ci->new_freq / 100) % 10,
910             ci->old_freq / 1000, (ci->old_freq / 100) % 10);
911       sa1100_pcmcia_update_mecr(ci->new_freq);
912     }
913     break;
914   }
915
916   return 0;
917 }
918
919 static struct notifier_block sa1100_pcmcia_notifier_block = {
920   notifier_call: sa1100_pcmcia_notifier
921 };
922 #endif
923
924 static int __init sa1100_pcmcia_machine_init(void)
925 {
926 #ifdef CONFIG_SA1100_ASSABET
927   if(machine_is_assabet()) {
928     if(machine_has_neponset()) {
929 #ifdef CONFIG_ASSABET_NEPONSET
930       pcmcia_low_level = &neponset_pcmcia_ops;
931 #else
932       printk(KERN_ERR "Card Services disabled: missing Neponset support\n");
933       return -1;
934 #endif
935     } else
936       pcmcia_low_level = &assabet_pcmcia_ops;
937   }
938 #endif
939 #ifdef CONFIG_SA1100_BADGE4
940   if (machine_is_badge4())
941     pcmcia_low_level = &badge4_pcmcia_ops;
942 #endif
943 #ifdef CONFIG_SA1100_FREEBIRD
944   if (machine_is_freebird())
945     pcmcia_low_level = &freebird_pcmcia_ops;
946 #endif
947 #ifdef CONFIG_SA1100_H3600
948   if (machine_is_h3600())
949     pcmcia_low_level = &h3600_pcmcia_ops;
950 #endif
951 #ifdef CONFIG_SA1100_CERF
952   if (machine_is_cerf())
953     pcmcia_low_level = &cerf_pcmcia_ops;
954 #endif
955 #ifdef CONFIG_SA1100_GRAPHICSCLIENT
956   if (machine_is_graphicsclient())
957     pcmcia_low_level = &gcplus_pcmcia_ops;
958 #endif
959 #ifdef CONFIG_SA1100_XP860
960   if (machine_is_xp860())
961     pcmcia_low_level = &xp860_pcmcia_ops;
962 #endif
963 #ifdef CONFIG_SA1100_YOPY
964   if (machine_is_yopy())
965     pcmcia_low_level = &yopy_pcmcia_ops;
966 #endif
967 #ifdef CONFIG_SA1100_SHANNON
968   if (machine_is_shannon())
969     pcmcia_low_level = &shannon_pcmcia_ops;
970 #endif
971 #ifdef CONFIG_SA1100_PANGOLIN
972   if (machine_is_pangolin())
973     pcmcia_low_level = &pangolin_pcmcia_ops;
974 #endif
975 #ifdef CONFIG_SA1100_JORNADA720
976   if (machine_is_jornada720())
977     pcmcia_low_level = &jornada720_pcmcia_ops;
978 #endif
979 #ifdef CONFIG_SA1100_PFS168
980   if(machine_is_pfs168())
981     pcmcia_low_level = &pfs168_pcmcia_ops;
982 #endif
983 #ifdef CONFIG_SA1100_FLEXANET
984   if(machine_is_flexanet())
985     pcmcia_low_level = &flexanet_pcmcia_ops;
986 #endif
987 #ifdef CONFIG_SA1100_SIMPAD
988   if(machine_is_simpad())
989     pcmcia_low_level = &simpad_pcmcia_ops;
990 #endif
991 #ifdef CONFIG_SA1100_GRAPHICSMASTER
992   if(machine_is_graphicsmaster())
993     pcmcia_low_level = &graphicsmaster_pcmcia_ops;
994 #endif
995 #ifdef CONFIG_SA1100_ADSBITSY
996   if(machine_is_adsbitsy())
997     pcmcia_low_level = &adsbitsy_pcmcia_ops;
998 #endif
999 #ifdef CONFIG_SA1100_STORK
1000   if(machine_is_stork())
1001     pcmcia_low_level = &stork_pcmcia_ops;
1002 #endif
1003
1004   if (!pcmcia_low_level) {
1005     printk(KERN_ERR "This hardware is not supported by the SA1100 Card Service driver\n");
1006     return -ENODEV;
1007   }
1008
1009   return 0;
1010 }
1011
1012 /* sa1100_pcmcia_driver_init()
1013  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1014  *
1015  * This routine performs a basic sanity check to ensure that this
1016  * kernel has been built with the appropriate board-specific low-level
1017  * PCMCIA support, performs low-level PCMCIA initialization, registers
1018  * this socket driver with Card Services, and then spawns the daemon
1019  * thread which is the real workhorse of the socket driver.
1020  *
1021  * Returns: 0 on success, -1 on error
1022  */
1023 static int __init sa1100_pcmcia_driver_init(void)
1024 {
1025   servinfo_t info;
1026   struct pcmcia_init pcmcia_init;
1027   struct pcmcia_state state[SA1100_PCMCIA_MAX_SOCK];
1028   struct pcmcia_state_array state_array;
1029   unsigned int i, clock;
1030   unsigned long mecr;
1031   int ret;
1032
1033   printk(KERN_INFO "SA-1100 PCMCIA (CS release %s)\n", CS_RELEASE);
1034
1035   CardServices(GetCardServicesInfo, &info);
1036
1037   if (info.Revision != CS_RELEASE_CODE) {
1038     printk(KERN_ERR "Card Services release codes do not match\n");
1039     return -EINVAL;
1040   }
1041
1042   ret = sa1100_pcmcia_machine_init();
1043   if (ret)
1044     return ret;
1045
1046   pcmcia_init.handler = sa1100_pcmcia_interrupt;
1047
1048   ret = pcmcia_low_level->init(&pcmcia_init);
1049   if (ret < 0) {
1050     printk(KERN_ERR "Unable to initialize kernel PCMCIA service (%d).\n", ret);
1051     return ret == -1 ? -EIO : ret;
1052   }
1053
1054   sa1100_pcmcia_socket_count = ret;
1055   state_array.size  = sa1100_pcmcia_socket_count;
1056   state_array.state = state;
1057
1058   memset(state, 0, sizeof(state));
1059
1060   if (pcmcia_low_level->socket_state(&state_array) < 0) {
1061     pcmcia_low_level->shutdown();
1062     printk(KERN_ERR "Unable to get PCMCIA status from kernel.\n");
1063     return -EIO;
1064   }
1065
1066   /*
1067    * We initialize the MECR to default values here, because we are
1068    * not guaranteed to see a SetIOMap operation at runtime.
1069    */
1070   mecr = 0;
1071
1072   clock = cpufreq_get(0);
1073
1074   for (i = 0; i < sa1100_pcmcia_socket_count; i++) {
1075     struct sa1100_pcmcia_socket *skt = PCMCIA_SOCKET(i);
1076     struct pcmcia_irq_info irq_info;
1077
1078     if (!request_mem_region(_PCMCIA(i), PCMCIASp, "PCMCIA")) {
1079       ret = -EBUSY;
1080       goto out_err;
1081     }
1082
1083     irq_info.sock = i;
1084     irq_info.irq  = -1;
1085     ret = pcmcia_low_level->get_irq_info(&irq_info);
1086     if (ret < 0)
1087       printk(KERN_ERR "Unable to get IRQ for socket %u (%d)\n", i, ret);
1088
1089     skt->irq        = irq_info.irq;
1090     skt->k_state    = state[i];
1091     skt->speed_io   = SA1100_PCMCIA_IO_ACCESS;
1092     skt->speed_attr = SA1100_PCMCIA_5V_MEM_ACCESS;
1093     skt->speed_mem  = SA1100_PCMCIA_5V_MEM_ACCESS;
1094     skt->phys_attr  = _PCMCIAAttr(i);
1095     skt->phys_mem   = _PCMCIAMem(i);
1096     skt->virt_io    = ioremap(_PCMCIAIO(i), 0x10000);
1097
1098     if (skt->virt_io == NULL) {
1099       ret = -ENOMEM;
1100       goto out_err;
1101     }
1102
1103     MECR_FAST_SET(mecr, i, 0);
1104     MECR_BSIO_SET(mecr, i, sa1100_pcmcia_mecr_bs(skt->speed_io, clock));
1105     MECR_BSA_SET(mecr, i, sa1100_pcmcia_mecr_bs(skt->speed_attr, clock));
1106     MECR_BSM_SET(mecr, i, sa1100_pcmcia_mecr_bs(skt->speed_mem, clock));
1107   }
1108
1109   MECR = mecr;
1110
1111 #ifdef CONFIG_CPU_FREQ
1112   ret = cpufreq_register_notifier(&sa1100_pcmcia_notifier_block);
1113   if (ret < 0) {
1114     printk(KERN_ERR "Unable to register CPU frequency change notifier (%d)\n", ret);
1115     goto out_err;
1116   }
1117 #endif
1118
1119   /* Only advertise as many sockets as we can detect */
1120   ret = register_ss_entry(sa1100_pcmcia_socket_count,
1121                           &sa1100_pcmcia_operations);
1122   if (ret < 0) {
1123     printk(KERN_ERR "Unable to register sockets\n");
1124     goto out_err;
1125   }
1126
1127   /*
1128    * Start the event poll timer.  It will reschedule by itself afterwards.
1129    */
1130   sa1100_pcmcia_poll_event(0);
1131
1132   return 0;
1133
1134  out_err:
1135   for (i = 0; i < sa1100_pcmcia_socket_count; i++) {
1136     iounmap(sa1100_pcmcia_socket[i].virt_io);
1137     release_mem_region(_PCMCIA(i), PCMCIASp);
1138   }
1139
1140   pcmcia_low_level->shutdown();
1141
1142   return ret;
1143 }  /* sa1100_pcmcia_driver_init() */
1144
1145 /* sa1100_pcmcia_driver_shutdown()
1146  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1147  * Invokes the low-level kernel service to free IRQs associated with this
1148  * socket controller and reset GPIO edge detection.
1149  */
1150 static void __exit sa1100_pcmcia_driver_shutdown(void)
1151 {
1152   int i;
1153
1154   del_timer_sync(&poll_timer);
1155
1156   unregister_ss_entry(&sa1100_pcmcia_operations);
1157
1158 #ifdef CONFIG_CPU_FREQ
1159   cpufreq_unregister_notifier(&sa1100_pcmcia_notifier_block);
1160 #endif
1161
1162   for (i = 0; i < sa1100_pcmcia_socket_count; i++) {
1163     iounmap(sa1100_pcmcia_socket[i].virt_io);
1164     release_mem_region(_PCMCIA(i), PCMCIASp);
1165   }
1166
1167   pcmcia_low_level->shutdown();
1168
1169   flush_scheduled_tasks();
1170 }
1171
1172 MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
1173 MODULE_DESCRIPTION("Linux PCMCIA Card Services: SA-1100 Socket Controller");
1174 MODULE_LICENSE("Dual MPL/GPL");
1175
1176 module_init(sa1100_pcmcia_driver_init);
1177 module_exit(sa1100_pcmcia_driver_shutdown);